From 368475b3569bd9aca3ef91478a57de0408e88b4f Mon Sep 17 00:00:00 2001 From: eitanm-starkware Date: Tue, 24 Dec 2024 16:04:17 +0200 Subject: [PATCH] feat(starknet_state_sync_types): add BlockInfo to SyncBlock --- crates/papyrus_p2p_sync/src/client/header.rs | 14 ++-------- crates/starknet_batcher/src/batcher.rs | 10 ++++--- crates/starknet_batcher/src/batcher_test.rs | 15 +++++++---- crates/starknet_state_sync/src/lib.rs | 27 +++++++++++-------- .../src/communication.rs | 2 ++ .../src/state_sync_types.rs | 6 ++--- 6 files changed, 40 insertions(+), 34 deletions(-) diff --git a/crates/papyrus_p2p_sync/src/client/header.rs b/crates/papyrus_p2p_sync/src/client/header.rs index 37f5d101de..c74948ea95 100644 --- a/crates/papyrus_p2p_sync/src/client/header.rs +++ b/crates/papyrus_p2p_sync/src/client/header.rs @@ -7,13 +7,7 @@ use papyrus_network::network_manager::ClientResponsesManager; use papyrus_protobuf::sync::{DataOrFin, SignedBlockHeader}; use papyrus_storage::header::{HeaderStorageReader, HeaderStorageWriter}; use papyrus_storage::{StorageError, StorageReader, StorageWriter}; -use starknet_api::block::{ - BlockHash, - BlockHeader, - BlockHeaderWithoutHash, - BlockNumber, - BlockSignature, -}; +use starknet_api::block::{BlockHash, BlockHeader, BlockNumber, BlockSignature}; use starknet_api::hash::StarkHash; use starknet_state_sync_types::state_sync_types::SyncBlock; use tracing::debug; @@ -122,7 +116,6 @@ impl DataStreamBuilder for HeaderStreamBuilder { } // TODO(Eitan): Use real header once SyncBlock contains data required by full nodes - // TODO(Eitan): Fill this with real header once SyncBlock has it. fn convert_sync_block_to_block_data( block_number: BlockNumber, sync_block: SyncBlock, @@ -130,10 +123,7 @@ impl DataStreamBuilder for HeaderStreamBuilder { Some(SignedBlockHeader { block_header: BlockHeader { block_hash: BlockHash(StarkHash::from(block_number.0)), - block_header_without_hash: BlockHeaderWithoutHash { - block_number, - ..Default::default() - }, + block_header_without_hash: sync_block.block_header_without_hash, state_diff_length: Some(sync_block.state_diff.len()), n_transactions: sync_block.transaction_hashes.len(), ..Default::default() diff --git a/crates/starknet_batcher/src/batcher.rs b/crates/starknet_batcher/src/batcher.rs index 0aabda1a2a..6e6e4db2e8 100644 --- a/crates/starknet_batcher/src/batcher.rs +++ b/crates/starknet_batcher/src/batcher.rs @@ -5,7 +5,7 @@ use blockifier::state::contract_class_manager::ContractClassManager; #[cfg(test)] use mockall::automock; use papyrus_storage::state::{StateStorageReader, StateStorageWriter}; -use starknet_api::block::BlockNumber; +use starknet_api::block::{BlockHeaderWithoutHash, BlockNumber}; use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::executable_transaction::Transaction; use starknet_api::state::ThinStateDiff; @@ -384,8 +384,12 @@ impl Batcher { self.abort_active_height().await; self.active_height = None; } - - let SyncBlock { state_diff, transaction_hashes, block_number } = sync_block; + // TODO(AlonH): Use additional data from the sync block. + let SyncBlock { + state_diff, + transaction_hashes, + block_header_without_hash: BlockHeaderWithoutHash { block_number, .. }, + } = sync_block; let address_to_nonce = state_diff.nonces.iter().map(|(k, v)| (*k, *v)).collect(); let tx_hashes = transaction_hashes.into_iter().collect(); let height = self.get_height_from_storage()?; diff --git a/crates/starknet_batcher/src/batcher_test.rs b/crates/starknet_batcher/src/batcher_test.rs index 704f31f3ca..cc01e21213 100644 --- a/crates/starknet_batcher/src/batcher_test.rs +++ b/crates/starknet_batcher/src/batcher_test.rs @@ -6,7 +6,7 @@ use blockifier::abi::constants; use indexmap::indexmap; use mockall::predicate::eq; use rstest::rstest; -use starknet_api::block::{BlockInfo, BlockNumber}; +use starknet_api::block::{BlockHeaderWithoutHash, BlockInfo, BlockNumber}; use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::executable_transaction::Transaction; use starknet_api::state::ThinStateDiff; @@ -509,7 +509,10 @@ async fn add_sync_block() { let mut batcher = create_batcher(mock_dependencies); let sync_block = SyncBlock { - block_number: INITIAL_HEIGHT, + block_header_without_hash: BlockHeaderWithoutHash { + block_number: INITIAL_HEIGHT, + ..Default::default() + }, state_diff: test_state_diff(), transaction_hashes: test_tx_hashes().into_iter().collect(), }; @@ -523,9 +526,11 @@ async fn add_sync_block_mismatch_block_number() { let mut batcher = create_batcher(MockDependencies::default()); let sync_block = SyncBlock { - block_number: INITIAL_HEIGHT.unchecked_next(), - state_diff: Default::default(), - transaction_hashes: Default::default(), + block_header_without_hash: BlockHeaderWithoutHash { + block_number: INITIAL_HEIGHT.unchecked_next(), + ..Default::default() + }, + ..Default::default() }; batcher.add_sync_block(sync_block).await.unwrap(); } diff --git a/crates/starknet_state_sync/src/lib.rs b/crates/starknet_state_sync/src/lib.rs index 322e5cd70f..041dd8c840 100644 --- a/crates/starknet_state_sync/src/lib.rs +++ b/crates/starknet_state_sync/src/lib.rs @@ -7,6 +7,7 @@ use futures::SinkExt; use papyrus_storage::body::BodyStorageReader; use papyrus_storage::compiled_class::CasmStorageReader; use papyrus_storage::db::TransactionKind; +use papyrus_storage::header::HeaderStorageReader; use papyrus_storage::state::StateStorageReader; use papyrus_storage::{StorageReader, StorageTxn}; use starknet_api::block::BlockNumber; @@ -80,17 +81,21 @@ impl ComponentRequestHandler for StateSync impl StateSync { fn get_block(&self, block_number: BlockNumber) -> StateSyncResult> { let txn = self.storage_reader.begin_ro_txn()?; - if let Some(block_transaction_hashes) = txn.get_block_transaction_hashes(block_number)? { - if let Some(thin_state_diff) = txn.get_state_diff(block_number)? { - return Ok(Some(SyncBlock { - block_number, - state_diff: thin_state_diff, - transaction_hashes: block_transaction_hashes, - })); - } - } - - Ok(None) + let block_header = txn.get_block_header(block_number)?; + let Some(block_transaction_hashes) = txn.get_block_transaction_hashes(block_number)? else { + return Ok(None); + }; + let Some(thin_state_diff) = txn.get_state_diff(block_number)? else { + return Ok(None); + }; + let Some(block_header) = block_header else { + return Ok(None); + }; + Ok(Some(SyncBlock { + state_diff: thin_state_diff, + block_header_without_hash: block_header.block_header_without_hash, + transaction_hashes: block_transaction_hashes, + })) } fn get_storage_at( diff --git a/crates/starknet_state_sync_types/src/communication.rs b/crates/starknet_state_sync_types/src/communication.rs index 4c1a56c794..5c6b58cce3 100644 --- a/crates/starknet_state_sync_types/src/communication.rs +++ b/crates/starknet_state_sync_types/src/communication.rs @@ -85,6 +85,7 @@ pub type StateSyncRequestAndResponseSender = ComponentRequestAndResponseSender; #[derive(Clone, Debug, Serialize, Deserialize)] +#[allow(clippy::large_enum_variant)] pub enum StateSyncRequest { GetBlock(BlockNumber), AddNewBlock(BlockNumber, SyncBlock), @@ -95,6 +96,7 @@ pub enum StateSyncRequest { } #[derive(Clone, Debug, Serialize, Deserialize)] +#[allow(clippy::large_enum_variant)] pub enum StateSyncResponse { GetBlock(StateSyncResult>), AddNewBlock(StateSyncResult<()>), diff --git a/crates/starknet_state_sync_types/src/state_sync_types.rs b/crates/starknet_state_sync_types/src/state_sync_types.rs index a44350272b..0ebcb8ef05 100644 --- a/crates/starknet_state_sync_types/src/state_sync_types.rs +++ b/crates/starknet_state_sync_types/src/state_sync_types.rs @@ -1,5 +1,5 @@ use serde::{Deserialize, Serialize}; -use starknet_api::block::BlockNumber; +use starknet_api::block::BlockHeaderWithoutHash; use starknet_api::state::ThinStateDiff; use starknet_api::transaction::TransactionHash; @@ -12,10 +12,10 @@ pub type StateSyncResult = Result; /// /// Blocks that came from the state sync are trusted. Therefore, SyncBlock doesn't contain data /// needed for verifying the block -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Default, Clone, Serialize, Deserialize)] pub struct SyncBlock { - pub block_number: BlockNumber, pub state_diff: ThinStateDiff, // TODO: decide if we want block hash, parent block hash and full classes here. pub transaction_hashes: Vec, + pub block_header_without_hash: BlockHeaderWithoutHash, }