Skip to content

Commit

Permalink
Merge pull request #113 from jbcaron/syncing
Browse files Browse the repository at this point in the history
feat: store the latest StarkNet block hash and number
  • Loading branch information
antiyro authored Feb 6, 2024
2 parents ab4fecd + bc714dc commit 2c6fb5b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"ghcr.io/devcontainers/features/git:1": {},
"ghcr.io/devcontainers/features/github-cli:1": {},
"ghcr.io/devcontainers/features/node:1": {},
"ghcr.io/devcontainers-contrib/features/protoc-asdf:1": {},
"ghcr.io/devcontainers-contrib/features/protoc-asdf:1": {}
},
"hostRequirements": {
"cpus": 8,
"memory": "16gb",
"storage": "128gb",
},
"storage": "128gb"
}
}
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 # Madara Changelog

## Next release

- feat: update and store highest block hash and number from sequencer
- feat: store events in block, return events in call get_transaction_receipt
- feat(client): on `add_declare_transaction` store sierra contract classes in
the madara backend
Expand Down
38 changes: 38 additions & 0 deletions crates/client/deoxys/src/l2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use reqwest::Url;
use serde::Deserialize;
use sp_core::H256;
use starknet_api::block::{BlockHash, BlockNumber};
use starknet_ff::FieldElement;
use starknet_providers::sequencer::models::BlockId;
use starknet_providers::SequencerGatewayProvider;
use tokio::sync::mpsc::Sender;
Expand All @@ -34,6 +35,11 @@ lazy_static! {
}));
}

lazy_static! {
/// Shared latest block number and hash of chain
static ref STARKNET_HIGHEST_BLOCK_HASH_AND_NUMBER: Arc<Mutex<(FieldElement, u64)>> = Arc::new(Mutex::new((FieldElement::default(), 0)));
}

/// The configuration of the worker responsible for fetching new blocks and state updates from the
/// feeder.
pub struct FetchConfig {
Expand Down Expand Up @@ -94,7 +100,14 @@ pub async fn sync(mut sender_config: SenderConfig, config: FetchConfig, start_at
let mut last_block_hash = None;
let mut got_block = false;
let mut got_state_update = false;
let mut last_update_highest_block = tokio::time::Instant::now() - Duration::from_secs(20);
loop {
if last_update_highest_block.elapsed() > Duration::from_secs(20) {
last_update_highest_block = tokio::time::Instant::now();
if let Err(e) = update_highest_block_hash_and_number(&client).await {
eprintln!("Failed to update highest block hash and number: {}", e);
}
}
let (block, state_update) = match (got_block, got_state_update) {
(false, false) => {
let block = fetch_block(&client, block_sender, current_block_number);
Expand Down Expand Up @@ -201,3 +214,28 @@ pub fn update_l2(state_update: L2StateUpdate) {
*new_state_update = state_update.clone();
}
}

async fn update_highest_block_hash_and_number(client: &SequencerGatewayProvider) -> Result<(), String> {
let block = client.get_block(BlockId::Latest).await.map_err(|e| format!("failed to get block: {e}"))?;

let hash = block
.block_hash
.ok_or("block hash not found")?
.try_into()
.map_err(|e| format!("failed to convert block hash: {e}"))?;
let number = block
.block_number
.ok_or("block number not found")?
.try_into()
.map_err(|e| format!("failed to convert block number: {e}"))?;

let last_highest_block_hash_and_number = STARKNET_HIGHEST_BLOCK_HASH_AND_NUMBER.clone();
let mut new_highest_block_hash_and_number = last_highest_block_hash_and_number.lock().unwrap();
*new_highest_block_hash_and_number = (hash, number);

Ok(())
}

pub fn get_highest_block_hash_and_number() -> (FieldElement, u64) {
STARKNET_HIGHEST_BLOCK_HASH_AND_NUMBER.lock().unwrap().clone()
}
5 changes: 3 additions & 2 deletions crates/client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use errors::StarknetRpcApiError;
use jsonrpsee::core::{async_trait, RpcResult};
use jsonrpsee::types::error::CallError;
use log::error;
use mc_deoxys::l2::get_highest_block_hash_and_number;
use mc_genesis_data_provider::GenesisProvider;
pub use mc_rpc_core::utils::*;
pub use mc_rpc_core::{
Expand Down Expand Up @@ -766,8 +767,8 @@ where
let current_block_num = UniqueSaturatedInto::<u64>::unique_saturated_into(best_number);
let current_block_hash = current_block?.header().hash::<H>().0;

let highest_block_num = UniqueSaturatedInto::<u64>::unique_saturated_into(highest_number);
let highest_block_hash = highest_block?.header().hash::<H>().0;
// Get the highest block number and hash from the global variable update in l2 sync()
let (highest_block_hash, highest_block_num) = get_highest_block_hash_and_number();

// Build the `SyncStatus` struct with the respective syn information
Ok(SyncStatusType::Syncing(SyncStatus {
Expand Down

0 comments on commit 2c6fb5b

Please sign in to comment.