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 nonce at #2757

Open
wants to merge 1 commit into
base: noam.s/sync_state_reader_get_storage_at
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions crates/starknet_gateway/src/sync_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ impl BlockifierStateReader for SyncStateReader {
Ok(res.unwrap_or_default())
}

fn get_nonce_at(&self, _contract_address: ContractAddress) -> StateResult<Nonce> {
todo!()
fn get_nonce_at(&self, contract_address: ContractAddress) -> StateResult<Nonce> {
let res = block_on(
self.shared_state_sync_client.get_nonce_at(self.block_number, contract_address),
)?;

Ok(res.unwrap_or_default())
}

fn get_compiled_class(&self, _class_hash: ClassHash) -> StateResult<RunnableCompiledClass> {
Expand Down
23 changes: 22 additions & 1 deletion crates/starknet_state_sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use papyrus_storage::body::BodyStorageReader;
use papyrus_storage::state::StateStorageReader;
use papyrus_storage::StorageReader;
use starknet_api::block::BlockNumber;
use starknet_api::core::{ContractAddress, BLOCK_HASH_TABLE_ADDRESS};
use starknet_api::core::{ContractAddress, Nonce, BLOCK_HASH_TABLE_ADDRESS};
use starknet_api::state::{StateNumber, StorageKey};
use starknet_sequencer_infra::component_definitions::{ComponentRequestHandler, ComponentStarter};
use starknet_sequencer_infra::component_server::{LocalComponentServer, RemoteComponentServer};
Expand Down Expand Up @@ -50,6 +50,9 @@ impl ComponentRequestHandler<StateSyncRequest, StateSyncResponse> for StateSync
storage_key,
))
}
StateSyncRequest::GetNonceAt(block_number, contract_address) => {
StateSyncResponse::GetNonceAt(self.get_nonce_at(block_number, contract_address))
}
}
}
}
Expand Down Expand Up @@ -97,6 +100,24 @@ impl StateSync {

Ok(None)
}

fn get_nonce_at(
&self,
block_number: BlockNumber,
contract_address: ContractAddress,
) -> StateSyncResult<Option<Nonce>> {
let txn = self.storage_reader.begin_ro_txn()?;

if let Some(state_number) = StateNumber::right_after_block(block_number) {
let res = txn
.get_state_reader()?
.get_nonce_at(state_number, &contract_address)?
.ok_or_else(|| StateSyncError::StorageError("Contract not found".to_string()))?;
return Ok(Some(res));
}

Ok(None)
}
}

pub type LocalStateSyncServer =
Expand Down
41 changes: 39 additions & 2 deletions crates/starknet_state_sync_types/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use async_trait::async_trait;
use papyrus_proc_macros::handle_response_variants;
use serde::{Deserialize, Serialize};
use starknet_api::block::BlockNumber;
use starknet_api::core::ContractAddress;
use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::state::StorageKey;
use starknet_sequencer_infra::component_client::{
ClientError,
Expand Down Expand Up @@ -44,7 +44,12 @@ pub trait StateSyncClient: Send + Sync {
storage_key: StorageKey,
) -> StateSyncClientResult<Option<Felt>>;

// TODO: add get_nonce_at for BlockifierStateReader trait
async fn get_nonce_at(
&self,
block_number: BlockNumber,
contract_address: ContractAddress,
) -> StateSyncClientResult<Option<Nonce>>;

// TODO: add get_compiled_class for BlockifierStateReader trait
// TODO: add get_class_hash_at for BlockifierStateReader trait
// TODO: add get_compiled_class_hash for BlockifierStateReader trait
Expand Down Expand Up @@ -73,13 +78,15 @@ pub enum StateSyncRequest {
GetBlock(BlockNumber),
AddNewBlock(BlockNumber, SyncBlock),
GetStorageAt(BlockNumber, ContractAddress, StorageKey),
GetNonceAt(BlockNumber, ContractAddress),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum StateSyncResponse {
GetBlock(StateSyncResult<Option<SyncBlock>>),
AddNewBlock(StateSyncResult<()>),
GetStorageAt(StateSyncResult<Option<Felt>>),
GetNonceAt(StateSyncResult<Option<Nonce>>),
}

#[async_trait]
Expand Down Expand Up @@ -123,6 +130,21 @@ impl StateSyncClient for LocalStateSyncClient {
StateSyncError
)
}

async fn get_nonce_at(
&self,
block_number: BlockNumber,
contract_address: ContractAddress,
) -> StateSyncClientResult<Option<Nonce>> {
let request = StateSyncRequest::GetNonceAt(block_number, contract_address);
let response = self.send(request).await;
handle_response_variants!(
StateSyncResponse,
GetNonceAt,
StateSyncClientError,
StateSyncError
)
}
}

#[async_trait]
Expand Down Expand Up @@ -166,4 +188,19 @@ impl StateSyncClient for RemoteStateSyncClient {
StateSyncError
)
}

async fn get_nonce_at(
&self,
block_number: BlockNumber,
contract_address: ContractAddress,
) -> StateSyncClientResult<Option<Nonce>> {
let request = StateSyncRequest::GetNonceAt(block_number, contract_address);
let response = self.send(request).await;
handle_response_variants!(
StateSyncResponse,
GetNonceAt,
StateSyncClientError,
StateSyncError
)
}
}