Skip to content

Commit

Permalink
feat(starknet_state_sync): implement state sync get latest block number
Browse files Browse the repository at this point in the history
  • Loading branch information
noamsp-starkware committed Dec 25, 2024
1 parent dbe2a1a commit e25ec06
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 18 deletions.
60 changes: 43 additions & 17 deletions crates/starknet_gateway/src/sync_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,31 @@ use starknet_api::data_availability::L1DataAvailabilityMode;
use starknet_api::state::StorageKey;
use starknet_state_sync_types::communication::SharedStateSyncClient;
use starknet_types_core::felt::Felt;
use tracing::error;

use crate::state_reader::{MempoolStateReader, StateReaderFactory};

#[allow(dead_code)]
struct SyncStateReader {
block_number: BlockNumber,
block_number: Option<BlockNumber>,
state_sync_client: SharedStateSyncClient,
}

impl SyncStateReader {
pub fn from_number(
state_sync_client: SharedStateSyncClient,
block_number: BlockNumber,
block_number: Option<BlockNumber>,
) -> Self {
Self { block_number, state_sync_client }
}
}

impl MempoolStateReader for SyncStateReader {
fn get_block_info(&self) -> StateResult<BlockInfo> {
let block = block_on(self.state_sync_client.get_block(self.block_number))
let block_number = self
.block_number
.ok_or(StateError::StateReadError("Empty block number".to_string()))?;
let block = block_on(self.state_sync_client.get_block(block_number))
.map_err(|e| StateError::StateReadError(e.to_string()))?
.ok_or(StateError::StateReadError("Block not found".to_string()))?;

Expand Down Expand Up @@ -62,27 +66,32 @@ impl BlockifierStateReader for SyncStateReader {
contract_address: ContractAddress,
key: StorageKey,
) -> StateResult<Felt> {
let res = block_on(self.state_sync_client.get_storage_at(
self.block_number,
contract_address,
key,
))
.map_err(|e| StateError::StateReadError(e.to_string()))?;
let block_number = self
.block_number
.ok_or(StateError::StateReadError("Empty block number".to_string()))?;
let res =
block_on(self.state_sync_client.get_storage_at(block_number, contract_address, key))
.map_err(|e| StateError::StateReadError(e.to_string()))?;

Ok(res)
}

fn get_nonce_at(&self, contract_address: ContractAddress) -> StateResult<Nonce> {
let res =
block_on(self.state_sync_client.get_nonce_at(self.block_number, contract_address))
.map_err(|e| StateError::StateReadError(e.to_string()))?;
let block_number = self
.block_number
.ok_or(StateError::StateReadError("Empty block number".to_string()))?;
let res = block_on(self.state_sync_client.get_nonce_at(block_number, contract_address))
.map_err(|e| StateError::StateReadError(e.to_string()))?;

Ok(res)
}

fn get_compiled_class(&self, class_hash: ClassHash) -> StateResult<RunnableCompiledClass> {
let block_number = self
.block_number
.ok_or(StateError::StateReadError("Empty block number".to_string()))?;
let contract_class = block_on(
self.state_sync_client.get_compiled_class_deprecated(self.block_number, class_hash),
self.state_sync_client.get_compiled_class_deprecated(block_number, class_hash),
)
.map_err(|e| StateError::StateReadError(e.to_string()))?;

Expand All @@ -97,8 +106,11 @@ impl BlockifierStateReader for SyncStateReader {
}

fn get_class_hash_at(&self, contract_address: ContractAddress) -> StateResult<ClassHash> {
let block_number = self
.block_number
.ok_or(StateError::StateReadError("Empty block number".to_string()))?;
let res =
block_on(self.state_sync_client.get_class_hash_at(self.block_number, contract_address))
block_on(self.state_sync_client.get_class_hash_at(block_number, contract_address))
.map_err(|e| StateError::StateReadError(e.to_string()))?;

Ok(res)
Expand All @@ -114,12 +126,26 @@ pub struct SyncStateReaderFactory {
}

impl StateReaderFactory for SyncStateReaderFactory {
// TODO(noamsp): Decide if we need this
fn get_state_reader_from_latest_block(&self) -> Box<dyn MempoolStateReader> {
todo!()
let latest_block_number =
match block_on(self.shared_state_sync_client.get_latest_block_number()) {
Ok(latest_block_number) => latest_block_number,
Err(e) => {
error!("Unexpected StateSyncClientError {}", e);
None
}
};

Box::new(SyncStateReader::from_number(
self.shared_state_sync_client.clone(),
latest_block_number,
))
}

fn get_state_reader(&self, block_number: BlockNumber) -> Box<dyn MempoolStateReader> {
Box::new(SyncStateReader::from_number(self.shared_state_sync_client.clone(), block_number))
Box::new(SyncStateReader::from_number(
self.shared_state_sync_client.clone(),
Some(block_number),
))
}
}
9 changes: 9 additions & 0 deletions crates/starknet_state_sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ impl ComponentRequestHandler<StateSyncRequest, StateSyncResponse> for StateSync
self.get_compiled_class_deprecated(block_number, class_hash),
)
}
StateSyncRequest::GetLatestBlockNumber() => {
StateSyncResponse::GetLatestBlockNumber(self.get_latest_block_number())
}
}
}
}
Expand Down Expand Up @@ -203,6 +206,12 @@ impl StateSync {
.ok_or(StateSyncError::ClassNotFound(class_hash))?;
Ok(ContractClass::V0(deprecated_compiled_contract_class))
}

fn get_latest_block_number(&self) -> StateSyncResult<Option<BlockNumber>> {
let txn = self.storage_reader.begin_ro_txn()?;
let latest_block_number = txn.get_state_marker()?.prev();
Ok(latest_block_number)
}
}

fn verify_synced_up_to<Mode: TransactionKind>(
Expand Down
27 changes: 26 additions & 1 deletion crates/starknet_state_sync_types/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ pub trait StateSyncClient: Send + Sync {
class_hash: ClassHash,
) -> StateSyncClientResult<ContractClass>;

async fn get_latest_block_number(&self) -> StateSyncClientResult<Option<BlockNumber>>;

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

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

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

#[async_trait]
Expand Down Expand Up @@ -191,6 +194,17 @@ impl StateSyncClient for LocalStateSyncClient {
StateSyncError
)
}

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

#[async_trait]
Expand Down Expand Up @@ -279,4 +293,15 @@ impl StateSyncClient for RemoteStateSyncClient {
StateSyncError
)
}

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

0 comments on commit e25ec06

Please sign in to comment.