Skip to content

Commit

Permalink
feat(starknet_batcher): implement sync_block
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Dec 15, 2024
1 parent 490a9be commit aac90c3
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions crates/starknet_batcher/src/batcher.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use blockifier::abi::constants;
Expand All @@ -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,
Expand Down Expand Up @@ -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)]
Expand All @@ -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<ContractAddress, Nonce>,
tx_hashes: HashSet<TransactionHash>,
) -> 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);
Expand Down

0 comments on commit aac90c3

Please sign in to comment.