diff --git a/crates/starknet_batcher/src/batcher.rs b/crates/starknet_batcher/src/batcher.rs index 067640af408..7b470652e7d 100644 --- a/crates/starknet_batcher/src/batcher.rs +++ b/crates/starknet_batcher/src/batcher.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::sync::Arc; use blockifier::abi::constants; @@ -8,8 +8,10 @@ use chrono::Utc; use mockall::automock; use papyrus_storage::state::{StateStorageReader, StateStorageWriter}; use starknet_api::block::{BlockHashAndNumber, BlockNumber}; +use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::executable_transaction::Transaction; use starknet_api::state::ThinStateDiff; +use starknet_api::transaction::TransactionHash; use starknet_batcher_types::batcher_types::{ BatcherResult, DecisionReachedInput, @@ -354,9 +356,15 @@ impl Batcher { Ok(GetProposalContentResponse { content: GetProposalContent::Finished(commitment) }) } - // TODO(Arni): Impl add sync block - pub async fn add_sync_block(&mut self, _sync_block: SyncBlock) -> BatcherResult<()> { - todo!("Implement add sync block"); + pub async fn add_sync_block(&mut self, sync_block: SyncBlock) -> BatcherResult<()> { + let SyncBlock { state_diff, transaction_hashes } = sync_block; + let address_to_nonce = state_diff.nonces.iter().map(|(k, v)| (*k, *v)).collect(); + let tx_hashes = transaction_hashes.into_iter().collect(); + + // TODO(Arni): Get the correct height here. + let height = self.get_height_from_storage()?; + info!("Syncing block at height {} and notifying mempool of the block.", height); + self.commit_proposal_and_block(height, state_diff, address_to_nonce, tx_hashes).await } #[instrument(skip(self), err)] @@ -375,6 +383,16 @@ impl Batcher { "Committing proposal {} at height {} and notifying mempool of the block.", proposal_id, height ); + self.commit_proposal_and_block(height, state_diff, address_to_nonce, tx_hashes).await + } + + async fn commit_proposal_and_block( + &mut self, + height: BlockNumber, + state_diff: ThinStateDiff, + address_to_nonce: HashMap, + tx_hashes: HashSet, + ) -> BatcherResult<()> { trace!("Transactions: {:#?}, State diff: {:#?}.", tx_hashes, state_diff); self.storage_writer.commit_proposal(height, state_diff).map_err(|err| { error!("Failed to commit proposal to storage: {}", err);