From 007c21ef62711d1aa7b5350d3389299df3305137 Mon Sep 17 00:00:00 2001 From: Noam Spiegelstein Date: Mon, 16 Dec 2024 17:43:48 +0200 Subject: [PATCH] chore(gateway): create sync state reader infra with todos --- Cargo.lock | 1 + crates/starknet_gateway/Cargo.toml | 1 + crates/starknet_gateway/src/lib.rs | 1 + .../starknet_gateway/src/sync_state_reader.rs | 71 +++++++++++++++++++ .../src/communication.rs | 7 +- 5 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 crates/starknet_gateway/src/sync_state_reader.rs diff --git a/Cargo.lock b/Cargo.lock index 318b2ae6af..31b699dbad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10437,6 +10437,7 @@ dependencies = [ "starknet_mempool_types", "starknet_sequencer_infra", "starknet_sierra_compile", + "starknet_state_sync_types", "thiserror", "tokio", "tracing", diff --git a/crates/starknet_gateway/Cargo.toml b/crates/starknet_gateway/Cargo.toml index 611f9e9b6c..51a43817b4 100644 --- a/crates/starknet_gateway/Cargo.toml +++ b/crates/starknet_gateway/Cargo.toml @@ -29,6 +29,7 @@ starknet_gateway_types.workspace = true starknet_mempool_types.workspace = true starknet_sequencer_infra.workspace = true starknet_sierra_compile.workspace = true +starknet_state_sync_types.workspace = true thiserror.workspace = true tokio.workspace = true tracing.workspace = true diff --git a/crates/starknet_gateway/src/lib.rs b/crates/starknet_gateway/src/lib.rs index fe5bc05321..057e1cef10 100644 --- a/crates/starknet_gateway/src/lib.rs +++ b/crates/starknet_gateway/src/lib.rs @@ -13,6 +13,7 @@ pub mod state_reader; mod state_reader_test_utils; mod stateful_transaction_validator; mod stateless_transaction_validator; +mod sync_state_reader; #[cfg(test)] mod test_utils; mod utils; diff --git a/crates/starknet_gateway/src/sync_state_reader.rs b/crates/starknet_gateway/src/sync_state_reader.rs new file mode 100644 index 0000000000..838283e1ee --- /dev/null +++ b/crates/starknet_gateway/src/sync_state_reader.rs @@ -0,0 +1,71 @@ +use blockifier::execution::contract_class::RunnableCompiledClass; +use blockifier::state::state_api::{StateReader as BlockifierStateReader, StateResult}; +use starknet_api::block::{BlockInfo, BlockNumber}; +use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; +use starknet_api::state::StorageKey; +use starknet_state_sync_types::communication::SharedStateSyncClient; +use starknet_types_core::felt::Felt; + +use crate::state_reader::{MempoolStateReader, StateReaderFactory}; + +#[allow(dead_code)] +struct SyncStateReader { + block_number: BlockNumber, + shared_state_sync_client: SharedStateSyncClient, +} + +impl SyncStateReader { + pub fn from_number( + shared_state_sync_client: &SharedStateSyncClient, + block_number: BlockNumber, + ) -> Self { + Self { shared_state_sync_client: shared_state_sync_client.clone(), block_number } + } +} + +impl MempoolStateReader for SyncStateReader { + fn get_block_info(&self) -> StateResult { + todo!() + } +} + +impl BlockifierStateReader for SyncStateReader { + fn get_storage_at( + &self, + _contract_address: ContractAddress, + _key: StorageKey, + ) -> StateResult { + todo!() + } + + fn get_nonce_at(&self, _contract_address: ContractAddress) -> StateResult { + todo!() + } + + fn get_compiled_class(&self, _class_hash: ClassHash) -> StateResult { + todo!() + } + + fn get_class_hash_at(&self, _contract_address: ContractAddress) -> StateResult { + todo!() + } + + fn get_compiled_class_hash(&self, _class_hash: ClassHash) -> StateResult { + todo!() + } +} + +pub struct SyncStateReaderFactory { + pub shared_state_sync_client: SharedStateSyncClient, +} + +impl StateReaderFactory for SyncStateReaderFactory { + // TODO(noamsp): Decide if we need this + fn get_state_reader_from_latest_block(&self) -> Box { + todo!() + } + + fn get_state_reader(&self, block_number: BlockNumber) -> Box { + Box::new(SyncStateReader::from_number(&self.shared_state_sync_client, block_number)) + } +} diff --git a/crates/starknet_state_sync_types/src/communication.rs b/crates/starknet_state_sync_types/src/communication.rs index feedb17d24..cfc97cddee 100644 --- a/crates/starknet_state_sync_types/src/communication.rs +++ b/crates/starknet_state_sync_types/src/communication.rs @@ -34,7 +34,12 @@ pub trait StateSyncClient: Send + Sync { sync_block: SyncBlock, ) -> StateSyncClientResult<()>; - // TODO: Add state reader methods for gateway. + // TODO: add get_storage_at for BlockifierStateReader trait + // TODO: add get_nonce_at for BlockifierStateReader trait + // 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 + // TODO: add get_block_info for MempoolStateReader trait } pub type StateSyncResult = Result;