Skip to content

Commit

Permalink
feat(starknet_state_sync_types): add BlockInfo to SyncBlock (#2927)
Browse files Browse the repository at this point in the history
  • Loading branch information
eitanm-starkware authored Dec 26, 2024
1 parent 5d383cf commit 8bc7599
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 34 deletions.
14 changes: 2 additions & 12 deletions crates/papyrus_p2p_sync/src/client/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -122,18 +116,14 @@ impl DataStreamBuilder<SignedBlockHeader> 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,
) -> Option<SignedBlockHeader> {
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()
Expand Down
10 changes: 7 additions & 3 deletions crates/starknet_batcher/src/batcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()?;
Expand Down
15 changes: 10 additions & 5 deletions crates/starknet_batcher/src/batcher_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -508,7 +508,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(),
};
Expand All @@ -522,9 +525,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();
}
Expand Down
27 changes: 16 additions & 11 deletions crates/starknet_state_sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,17 +81,21 @@ impl ComponentRequestHandler<StateSyncRequest, StateSyncResponse> for StateSync
impl StateSync {
fn get_block(&self, block_number: BlockNumber) -> StateSyncResult<Option<SyncBlock>> {
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(
Expand Down
2 changes: 2 additions & 0 deletions crates/starknet_state_sync_types/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub type StateSyncRequestAndResponseSender =
ComponentRequestAndResponseSender<StateSyncRequest, StateSyncResponse>;

#[derive(Clone, Debug, Serialize, Deserialize)]
#[allow(clippy::large_enum_variant)]
pub enum StateSyncRequest {
GetBlock(BlockNumber),
AddNewBlock(BlockNumber, SyncBlock),
Expand All @@ -98,6 +99,7 @@ pub enum StateSyncRequest {
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[allow(clippy::large_enum_variant)]
pub enum StateSyncResponse {
GetBlock(StateSyncResult<Option<SyncBlock>>),
AddNewBlock(StateSyncResult<()>),
Expand Down
6 changes: 3 additions & 3 deletions crates/starknet_state_sync_types/src/state_sync_types.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,10 +12,10 @@ pub type StateSyncResult<T> = Result<T, StateSyncError>;
///
/// 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<TransactionHash>,
pub block_header_without_hash: BlockHeaderWithoutHash,
}

0 comments on commit 8bc7599

Please sign in to comment.