Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(starknet_state_sync): implement sync state reader get block info #2887

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion crates/starknet_gateway/src/sync_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ impl SyncStateReader {

impl MempoolStateReader for SyncStateReader {
fn get_block_info(&self) -> StateResult<BlockInfo> {
todo!()
let maybe_block_info = block_on(self.state_sync_client.get_block_info(self.block_number))
.map_err(|e| StateError::StateReadError(e.to_string()))?;

let block_info = match maybe_block_info {
Some(block_info) => block_info,
None => return Err(StateError::StateReadError("Block info not found".to_string())),
};

Ok(block_info)
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/starknet_state_sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ workspace = true

[dependencies]
async-trait.workspace = true
blockifier.workspace = true
futures.workspace = true
papyrus_config.workspace = true
papyrus_network.workspace = true
Expand Down
42 changes: 41 additions & 1 deletion crates/starknet_state_sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ pub mod config;
pub mod runner;

use async_trait::async_trait;
use blockifier::blockifier::block::validated_gas_prices;
use futures::channel::mpsc::{channel, Sender};
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;
use starknet_api::block::{BlockInfo, BlockNumber, GasPrice, NonzeroGasPrice};
use starknet_api::contract_class::{ContractClass, SierraVersion};
use starknet_api::core::{ClassHash, ContractAddress, Nonce, BLOCK_HASH_TABLE_ADDRESS};
use starknet_api::data_availability::L1DataAvailabilityMode;
use starknet_api::state::{StateNumber, StorageKey};
use starknet_api::StarknetApiResult;
use starknet_sequencer_infra::component_definitions::{ComponentRequestHandler, ComponentStarter};
use starknet_sequencer_infra::component_server::{LocalComponentServer, RemoteComponentServer};
use starknet_state_sync_types::communication::{StateSyncRequest, StateSyncResponse};
Expand Down Expand Up @@ -73,6 +77,9 @@ impl ComponentRequestHandler<StateSyncRequest, StateSyncResponse> for StateSync
self.get_compiled_class_deprecated(block_number, class_hash),
)
}
StateSyncRequest::GetBlockInfo(block_number) => {
StateSyncResponse::GetBlockInfo(self.get_block_info(block_number))
}
}
}
}
Expand Down Expand Up @@ -192,6 +199,35 @@ impl StateSync {
.ok_or(StateSyncError::ClassNotFound(class_hash))?;
Ok(ContractClass::V0(deprecated_compiled_contract_class))
}

fn get_block_info(&self, block_number: BlockNumber) -> StateSyncResult<Option<BlockInfo>> {
let txn = self.storage_reader.begin_ro_txn()?;

if let Some(block_header) = txn.get_block_header(block_number)? {
let block_header_without_hash = block_header.block_header_without_hash;
let block_info = BlockInfo {
block_number: block_header_without_hash.block_number,
sequencer_address: block_header_without_hash.sequencer.0,
block_timestamp: block_header_without_hash.timestamp,
gas_prices: validated_gas_prices(
parse_gas_price(block_header_without_hash.l1_gas_price.price_in_wei)?,
parse_gas_price(block_header_without_hash.l1_gas_price.price_in_fri)?,
parse_gas_price(block_header_without_hash.l1_data_gas_price.price_in_wei)?,
parse_gas_price(block_header_without_hash.l1_data_gas_price.price_in_fri)?,
parse_gas_price(block_header_without_hash.l2_gas_price.price_in_wei)?,
parse_gas_price(block_header_without_hash.l2_gas_price.price_in_fri)?,
),
use_kzg_da: matches!(
block_header_without_hash.l1_da_mode,
L1DataAvailabilityMode::Blob
),
};

return Ok(Some(block_info));
}

Ok(None)
}
}

fn verify_synced_up_to<Mode: TransactionKind>(
Expand All @@ -207,6 +243,10 @@ fn verify_synced_up_to<Mode: TransactionKind>(
Err(StateSyncError::BlockNotFound(block_number))
}

fn parse_gas_price(gas_price: GasPrice) -> StarknetApiResult<NonzeroGasPrice> {
NonzeroGasPrice::new(gas_price)
}

pub type LocalStateSyncServer =
LocalComponentServer<StateSync, StateSyncRequest, StateSyncResponse>;
pub type RemoteStateSyncServer = RemoteComponentServer<StateSyncRequest, StateSyncResponse>;
Expand Down
37 changes: 35 additions & 2 deletions crates/starknet_state_sync_types/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use async_trait::async_trait;
use papyrus_proc_macros::handle_response_variants;
use serde::{Deserialize, Serialize};
use starknet_api::block::BlockNumber;
use starknet_api::block::{BlockInfo, BlockNumber};
use starknet_api::contract_class::ContractClass;
use starknet_api::core::{ClassHash, ContractAddress, Nonce};
use starknet_api::state::StorageKey;
Expand Down Expand Up @@ -65,8 +65,12 @@ pub trait StateSyncClient: Send + Sync {
class_hash: ClassHash,
) -> StateSyncClientResult<ContractClass>;

async fn get_block_info(
&self,
block_number: BlockNumber,
) -> StateSyncClientResult<Option<BlockInfo>>;

// TODO: Add get_compiled_class_hash for StateSyncReader
// TODO: Add get_block_info for StateSyncReader
}

#[derive(Clone, Debug, Error)]
Expand All @@ -92,6 +96,7 @@ pub enum StateSyncRequest {
GetNonceAt(BlockNumber, ContractAddress),
GetClassHashAt(BlockNumber, ContractAddress),
GetCompiledClassDeprecated(BlockNumber, ClassHash),
GetBlockInfo(BlockNumber),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand All @@ -102,6 +107,7 @@ pub enum StateSyncResponse {
GetNonceAt(StateSyncResult<Nonce>),
GetClassHashAt(StateSyncResult<ClassHash>),
GetCompiledClassDeprecated(StateSyncResult<ContractClass>),
GetBlockInfo(StateSyncResult<Option<BlockInfo>>),
}

#[async_trait]
Expand Down Expand Up @@ -190,6 +196,19 @@ impl StateSyncClient for LocalStateSyncClient {
StateSyncError
)
}
async fn get_block_info(
&self,
block_number: BlockNumber,
) -> StateSyncClientResult<Option<BlockInfo>> {
let request = StateSyncRequest::GetBlockInfo(block_number);
let response = self.send(request).await;
handle_response_variants!(
StateSyncResponse,
GetBlockInfo,
StateSyncClientError,
StateSyncError
)
}
}

#[async_trait]
Expand Down Expand Up @@ -278,4 +297,18 @@ impl StateSyncClient for RemoteStateSyncClient {
StateSyncError
)
}

async fn get_block_info(
&self,
block_number: BlockNumber,
) -> StateSyncClientResult<Option<BlockInfo>> {
let request = StateSyncRequest::GetBlockInfo(block_number);
let response = self.send(request).await;
handle_response_variants!(
StateSyncResponse,
GetBlockInfo,
StateSyncClientError,
StateSyncError
)
}
}
Loading