Skip to content

Commit

Permalink
Merge pull request #29 from KasarLabs/fix/sync
Browse files Browse the repository at this point in the history
fix(latest): Retrieve latest synced block via internal client
  • Loading branch information
antiyro committed Mar 27, 2024
2 parents ff8dc52 + 2a3e406 commit 7314ae7
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 71 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ git # Deoxys Changelog

## Next release

- fix(latest): Retrieve latest synced block via internal client
- perf(l2 sync): parallelize commitment computation and refactor part of l2 io sync
- refactor: rpc methods and removed rpc-core
- feat: add an optional TUI dashboard
Expand Down
6 changes: 3 additions & 3 deletions crates/client/sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ pub mod starknet_sync_worker {
pub async fn sync<C>(
fetch_config: FetchConfig,
sender_config: SenderConfig,
rpc_port: u16,
l1_url: Url,
client: Arc<C>,
starting_block: u32,
) where
C: HeaderBackend<DBlockT> + 'static,
{
let first_block = utility::get_last_synced_block(rpc_port).await + 1;
let starting_block = starting_block + 1;

let _ = tokio::join!(
l1::sync(l1_url.clone()),
l2::sync(
sender_config,
fetch_config.clone(),
first_block,
starting_block.into(),
DeoxysBackend::bonsai_contract(),
DeoxysBackend::bonsai_storage(),
DeoxysBackend::bonsai_class(),
Expand Down
56 changes: 0 additions & 56 deletions crates/client/sync/src/utils/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,62 +47,6 @@ pub fn get_config() -> Result<FetchConfig, &'static str> {

// TODO: secure the auto calls here

/// Returns the block number of the last block (from Substrate).
pub async fn get_last_synced_block(rpc_port: u16) -> u64 {
let client = reqwest::Client::new();

let url = format!("http://localhost:{}/", rpc_port);
let request = serde_json::json!({
"id": 1,
"jsonrpc": "2.0",
"method": "chain_getBlock",
"params": []
});
let payload = serde_json::to_vec(&request).unwrap();

let response: serde_json::Value = client
.post(&url)
.header(reqwest::header::CONTENT_TYPE, "application/json")
.header(reqwest::header::ACCEPT, "application/json")
.body(payload)
.send()
.await
.unwrap()
.json()
.await
.unwrap();

let number_as_hex = response["result"]["block"]["header"]["number"].as_str().unwrap();
u64::from_str_radix(&number_as_hex[2..], 16).unwrap()
}

/// Returns the block hash for a given block number (from Substrate).
pub async fn get_block_hash_by_number(rpc_port: u16, block_number: u64) -> Option<String> {
let client = reqwest::Client::new();

let url = format!("http://localhost:{}/", rpc_port);
let request = serde_json::json!({
"id": 1,
"jsonrpc": "2.0",
"method": "chain_getBlockHash",
"params": [block_number]
});
let payload = serde_json::to_vec(&request).unwrap();
let response: serde_json::Value = client
.post(&url)
.header(reqwest::header::CONTENT_TYPE, "application/json")
.header(reqwest::header::ACCEPT, "application/json")
.body(payload)
.send()
.await
.unwrap()
.json()
.await
.unwrap();

response["result"].as_str().map(String::from)
}

pub async fn get_state_update_at(rpc_port: u16, block_number: u64) -> Result<L2StateUpdate, Box<dyn Error>> {
let client = reqwest::Client::new();
let url = format!("http://localhost:{}", rpc_port);
Expand Down
12 changes: 2 additions & 10 deletions crates/node/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,8 @@ pub fn run_node(mut cli: Cli) -> Result<()> {

let genesis_block = fetch_apply_genesis_block(fetch_block_config.clone()).await.unwrap();

service::new_full(
config,
sealing,
cli.run.base.rpc_port.unwrap(),
l1_endpoint,
cache,
fetch_block_config,
genesis_block,
)
.map_err(sc_cli::Error::Service)
service::new_full(config, sealing, l1_endpoint, cache, fetch_block_config, genesis_block)
.map_err(sc_cli::Error::Service)
})
}

Expand Down
3 changes: 1 addition & 2 deletions crates/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ where
pub fn new_full(
config: Configuration,
sealing: SealingMode,
rpc_port: u16,
l1_url: Url,
cache_more_things: bool,
fetch_config: FetchConfig,
Expand Down Expand Up @@ -440,7 +439,7 @@ pub fn new_full(
task_manager.spawn_essential_handle().spawn(
"starknet-sync-worker",
Some("madara"),
starknet_sync_worker::sync(fetch_config, sender_config, rpc_port, l1_url, Arc::clone(&client)),
starknet_sync_worker::sync(fetch_config, sender_config, l1_url, Arc::clone(&client), starting_block),
);

// manual-seal authorship
Expand Down

0 comments on commit 7314ae7

Please sign in to comment.