Skip to content

Commit

Permalink
feat(starknet_state_sync_types): add BlockInfo to SyncBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
eitanm-starkware committed Dec 24, 2024
1 parent 7c40c0f commit b575e4b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 22 deletions.
7 changes: 6 additions & 1 deletion crates/papyrus_p2p_sync/src/client/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ 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,
Expand All @@ -132,6 +131,12 @@ impl DataStreamBuilder<SignedBlockHeader> for HeaderStreamBuilder {
block_hash: BlockHash(StarkHash::from(block_number.0)),
block_header_without_hash: BlockHeaderWithoutHash {
block_number,
timestamp: sync_block.timestamp,
l1_da_mode: sync_block.l1_da_mode,
sequencer: sync_block.sequencer,
l1_gas_price: sync_block.l1_gas_price,
l1_data_gas_price: sync_block.l1_data_gas_price,
l2_gas_price: sync_block.l2_gas_price,
..Default::default()
},
state_diff_length: Some(sync_block.state_diff.len()),
Expand Down
4 changes: 2 additions & 2 deletions crates/starknet_batcher/src/batcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ 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_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
8 changes: 3 additions & 5 deletions crates/starknet_batcher/src/batcher_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ async fn add_sync_block() {
block_number: INITIAL_HEIGHT,
state_diff: test_state_diff(),
transaction_hashes: test_tx_hashes().into_iter().collect(),
..Default::default()
};
batcher.add_sync_block(sync_block).await.unwrap();
}
Expand All @@ -522,11 +523,8 @@ async fn add_sync_block() {
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(),
};
let sync_block =
SyncBlock { block_number: INITIAL_HEIGHT.unchecked_next(), ..Default::default() };
batcher.add_sync_block(sync_block).await.unwrap();
}

Expand Down
33 changes: 22 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,27 @@ 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_number,
timestamp: block_header.block_header_without_hash.timestamp,
sequencer: block_header.block_header_without_hash.sequencer,
l1_gas_price: block_header.block_header_without_hash.l1_gas_price,
l1_data_gas_price: block_header.block_header_without_hash.l1_data_gas_price,
l2_gas_price: block_header.block_header_without_hash.l2_gas_price,
l1_da_mode: block_header.block_header_without_hash.l1_da_mode,
transaction_hashes: block_transaction_hashes,
}))
}

fn get_storage_at(
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_state_sync_types/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,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
15 changes: 12 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,7 @@
use serde::{Deserialize, Serialize};
use starknet_api::block::BlockNumber;
use starknet_api::block::{BlockNumber, BlockTimestamp, GasPricePerToken};
use starknet_api::core::SequencerContractAddress;
use starknet_api::data_availability::L1DataAvailabilityMode;
use starknet_api::state::ThinStateDiff;
use starknet_api::transaction::TransactionHash;

Expand All @@ -12,10 +14,17 @@ 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_number: BlockNumber,
pub timestamp: BlockTimestamp,
pub sequencer: SequencerContractAddress,
pub l1_gas_price: GasPricePerToken,
pub l1_data_gas_price: GasPricePerToken,
pub l2_gas_price: GasPricePerToken,
pub l1_da_mode: L1DataAvailabilityMode,
}

0 comments on commit b575e4b

Please sign in to comment.