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 26, 2024
1 parent 04f0dbf commit 7e525a8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 9 deletions.
4 changes: 2 additions & 2 deletions crates/starknet_gateway/src/rpc_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ pub struct RpcStateReaderFactory {
}

impl StateReaderFactory for RpcStateReaderFactory {
fn get_state_reader_from_latest_block(&self) -> Box<dyn MempoolStateReader> {
Box::new(RpcStateReader::from_latest(&self.config))
fn get_state_reader_from_latest_block(&self) -> StateResult<Box<dyn MempoolStateReader>> {
Ok(Box::new(RpcStateReader::from_latest(&self.config)))
}

fn get_state_reader(&self, block_number: BlockNumber) -> Box<dyn MempoolStateReader> {
Expand Down
2 changes: 1 addition & 1 deletion crates/starknet_gateway/src/state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait MempoolStateReader: BlockifierStateReader + Send + Sync {

#[cfg_attr(test, automock)]
pub trait StateReaderFactory: Send + Sync {
fn get_state_reader_from_latest_block(&self) -> Box<dyn MempoolStateReader>;
fn get_state_reader_from_latest_block(&self) -> StateResult<Box<dyn MempoolStateReader>>;
fn get_state_reader(&self, block_number: BlockNumber) -> Box<dyn MempoolStateReader>;
}

Expand Down
4 changes: 2 additions & 2 deletions crates/starknet_gateway/src/state_reader_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ pub struct TestStateReaderFactory {
}

impl StateReaderFactory for TestStateReaderFactory {
fn get_state_reader_from_latest_block(&self) -> Box<dyn MempoolStateReader> {
Box::new(self.state_reader.clone())
fn get_state_reader_from_latest_block(&self) -> StateResult<Box<dyn MempoolStateReader>> {
Ok(Box::new(self.state_reader.clone()))
}

fn get_state_reader(&self, _block_number: BlockNumber) -> Box<dyn MempoolStateReader> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ fn skip_stateful_validations(tx: &ExecutableTransaction, account_nonce: Nonce) -
pub fn get_latest_block_info(
state_reader_factory: &dyn StateReaderFactory,
) -> StatefulTransactionValidatorResult<BlockInfo> {
let state_reader = state_reader_factory.get_state_reader_from_latest_block();
let state_reader = state_reader_factory.get_state_reader_from_latest_block().map_err(|e| {
error!("Failed to get state reader from latest block: {}", e);
GatewaySpecError::UnexpectedError { data: "Internal server error.".to_owned() }
})?;
state_reader.get_block_info().map_err(|e| {
error!("Failed to get latest block info: {}", e);
GatewaySpecError::UnexpectedError { data: "Internal server error.".to_owned() }
Expand Down
12 changes: 9 additions & 3 deletions crates/starknet_gateway/src/sync_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,15 @@ 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!()
fn get_state_reader_from_latest_block(&self) -> StateResult<Box<dyn MempoolStateReader>> {
let latest_block_number = block_on(self.shared_state_sync_client.get_latest_block_number())
.map_err(|e| StateError::StateReadError(e.to_string()))?
.ok_or(StateError::StateReadError("Block not found, Empty state diff".to_string()))?;

Ok(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> {
Expand Down
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 @@ -188,6 +191,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
26 changes: 26 additions & 0 deletions crates/starknet_state_sync_types/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ 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
}

Expand Down Expand Up @@ -95,6 +97,7 @@ pub enum StateSyncRequest {
GetNonceAt(BlockNumber, ContractAddress),
GetClassHashAt(BlockNumber, ContractAddress),
GetCompiledClassDeprecated(BlockNumber, ClassHash),
GetLatestBlockNumber(),
}

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

#[async_trait]
Expand Down Expand Up @@ -194,6 +198,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 @@ -282,4 +297,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 7e525a8

Please sign in to comment.