Skip to content

Commit

Permalink
build(blockifier_reexecution): add dummy consecutive state readers (#…
Browse files Browse the repository at this point in the history
…1703)

* build(blockifier_reexecution): add offline reexecution data struct

* build(blockifier_reexecution): add dummy consecutive state readers
  • Loading branch information
aner-starkware authored Nov 3, 2024
1 parent fb62086 commit 0b906af
Showing 1 changed file with 135 additions and 1 deletion.
136 changes: 135 additions & 1 deletion crates/blockifier_reexecution/src/state_reader/test_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use blockifier::blockifier::transaction_executor::TransactionExecutor;
use blockifier::bouncer::BouncerConfig;
use blockifier::context::BlockContext;
use blockifier::execution::contract_class::RunnableContractClass;
use blockifier::state::cached_state::{CachedState, CommitmentStateDiff};
use blockifier::state::cached_state::{CachedState, CommitmentStateDiff, StateMaps};
use blockifier::state::errors::StateError;
use blockifier::state::state_api::{StateReader, StateResult};
use blockifier::transaction::transaction_execution::Transaction as BlockifierTransaction;
Expand Down Expand Up @@ -42,6 +42,14 @@ pub type ReexecutionResult<T> = Result<T, ReexecutionError>;

pub type StarknetContractClassMapping = HashMap<ClassHash, StarknetContractClass>;

pub struct OfflineReexecutionData {
state_maps: StateMaps,
contract_class_mapping: StarknetContractClassMapping,
block_context_next_block: BlockContext,
transactions_next_block: Vec<BlockifierTransaction>,
state_diff_next_block: CommitmentStateDiff,
}

pub struct TestStateReader {
rpc_state_reader: RpcStateReader,
#[allow(dead_code)]
Expand Down Expand Up @@ -322,3 +330,129 @@ impl ConsecutiveStateReaders<TestStateReader> for ConsecutiveTestStateReaders {
self.next_block_state_reader.get_state_diff()
}
}

pub struct OfflineStateReader {
pub state_maps: StateMaps,
pub contract_class_mapping: StarknetContractClassMapping,
}

impl StateReader for OfflineStateReader {
fn get_storage_at(
&self,
contract_address: ContractAddress,
key: StorageKey,
) -> StateResult<Felt> {
Ok(*self.state_maps.storage.get(&(contract_address, key)).ok_or(
StateError::StateReadError(format!(
"Missing Storage Value at contract_address: {}, key:{:?}",
contract_address, key
)),
)?)
}

fn get_nonce_at(&self, contract_address: ContractAddress) -> StateResult<Nonce> {
Ok(*self.state_maps.nonces.get(&contract_address).ok_or(StateError::StateReadError(
format!("Missing nonce at contract_address: {contract_address}"),
))?)
}

fn get_class_hash_at(&self, contract_address: ContractAddress) -> StateResult<ClassHash> {
Ok(*self.state_maps.class_hashes.get(&contract_address).ok_or(
StateError::StateReadError(format!(
"Missing class hash at contract_address: {contract_address}"
)),
)?)
}

fn get_compiled_contract_class(
&self,
class_hash: ClassHash,
) -> StateResult<RunnableContractClass> {
match self.get_contract_class(&class_hash)? {
StarknetContractClass::Sierra(sierra) => sierra_to_contact_class_v1(sierra),
StarknetContractClass::Legacy(legacy) => legacy_to_contract_class_v0(legacy),
}
}

fn get_compiled_class_hash(&self, class_hash: ClassHash) -> StateResult<CompiledClassHash> {
Ok(*self.state_maps.compiled_class_hashes.get(&class_hash).ok_or(
StateError::StateReadError(format!(
"Missing compiled class hash at class hash: {class_hash}"
)),
)?)
}
}

impl OfflineStateReader {
pub fn get_transaction_executor(
self,
block_context_next_block: BlockContext,
transaction_executor_config: Option<TransactionExecutorConfig>,
) -> ReexecutionResult<TransactionExecutor<OfflineStateReader>> {
Ok(TransactionExecutor::<OfflineStateReader>::new(
CachedState::new(self),
block_context_next_block,
transaction_executor_config.unwrap_or_default(),
))
}
}

impl ReexecutionStateReader for OfflineStateReader {
fn get_contract_class(&self, class_hash: &ClassHash) -> StateResult<StarknetContractClass> {
Ok(self
.contract_class_mapping
.get(class_hash)
.ok_or(StateError::StateReadError(format!(
"Missing contract class at class hash: {class_hash}"
)))?
.clone())
}
}

pub struct OfflineConsecutiveStateReaders {
pub offline_state_reader_prev_block: OfflineStateReader,
pub block_context_next_block: BlockContext,
pub transactions_next_block: Vec<BlockifierTransaction>,
pub state_diff_next_block: CommitmentStateDiff,
}

impl OfflineConsecutiveStateReaders {
// TODO(Aner): create directly from json.
pub fn new(
OfflineReexecutionData {
state_maps,
contract_class_mapping,
block_context_next_block,
transactions_next_block,
state_diff_next_block,
}: OfflineReexecutionData,
) -> Self {
OfflineConsecutiveStateReaders {
offline_state_reader_prev_block: OfflineStateReader {
state_maps,
contract_class_mapping,
},
block_context_next_block,
transactions_next_block,
state_diff_next_block,
}
}
}

impl ConsecutiveStateReaders<OfflineStateReader> for OfflineConsecutiveStateReaders {
fn get_transaction_executor(
self,
transaction_executor_config: Option<TransactionExecutorConfig>,
) -> ReexecutionResult<TransactionExecutor<OfflineStateReader>> {
self.offline_state_reader_prev_block
.get_transaction_executor(self.block_context_next_block, transaction_executor_config)
}

fn get_next_block_txs(&self) -> ReexecutionResult<Vec<BlockifierTransaction>> {
Ok(self.transactions_next_block.clone())
}

fn get_next_block_state_diff(&self) -> ReexecutionResult<CommitmentStateDiff> {
Ok(self.state_diff_next_block.clone())
}
}

0 comments on commit 0b906af

Please sign in to comment.