From 75355c7b785357251273b65b592ee5add90b1e39 Mon Sep 17 00:00:00 2001 From: Aviv Yossef Date: Tue, 30 Dec 2025 17:06:32 +0200 Subject: [PATCH] starknet_os_runner: aviv refactor the rpc state reader to compute the compiled class hash --- .../src/state_reader/offline_state_reader.rs | 21 +++++++-------- .../state_reader/reexecution_state_reader.rs | 6 ++--- .../src/state_reader/rpc_state_reader.rs | 27 +++++++++---------- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/crates/blockifier_reexecution/src/state_reader/offline_state_reader.rs b/crates/blockifier_reexecution/src/state_reader/offline_state_reader.rs index 6cf5c96ed37..dbfebc2917e 100644 --- a/crates/blockifier_reexecution/src/state_reader/offline_state_reader.rs +++ b/crates/blockifier_reexecution/src/state_reader/offline_state_reader.rs @@ -16,6 +16,7 @@ use blockifier::state::state_reader_and_contract_manager::{ FetchCompiledClasses, StateReaderAndContractManager, }; +use blockifier::state::utils::get_compiled_class_hash_v2 as default_get_compiled_class_hash_v2; use blockifier::transaction::transaction_execution::Transaction as BlockifierTransaction; use serde::{Deserialize, Serialize}; use starknet_api::block::{BlockHash, BlockHashAndNumber, BlockInfo, BlockNumber, StarknetVersion}; @@ -31,7 +32,6 @@ use crate::errors::ReexecutionResult; use crate::state_reader::reexecution_state_reader::{ ConsecutiveReexecutionStateReaders, ReexecutionStateReader, - DUMMY_COMPILED_CLASS_HASH, }; use crate::state_reader::rpc_state_reader::StarknetContractClassMapping; use crate::utils::get_chain_info; @@ -110,12 +110,16 @@ impl From for OfflineReexecutionData { .api_txs_to_blockifier_txs_next_block(transactions_next_block) .expect("Failed to convert starknet-api transactions to blockifier transactions."); + // Disable casm hash migration for reexecution. + let mut versioned_constants = VersionedConstants::get(&starknet_version).unwrap().clone(); + versioned_constants.enable_casm_hash_migration = false; + Self { offline_state_reader_prev_block, block_context_next_block: BlockContext::new( block_info_next_block, get_chain_info(&chain_id), - VersionedConstants::get(&starknet_version).unwrap().clone(), + versioned_constants, BouncerConfig::max(), ), transactions_next_block, @@ -171,23 +175,16 @@ impl StateReader for OfflineStateReader { } } - /// Returns a dummy compiled class hash for reexecution purposes. - /// - /// This method is required since v0.14.1 for checking if compiled class hashes - /// need to be migrated from v1 to v2 format. - /// In reexecution we use a dummy value for both get_compiled_class_hash and - /// get_compiled_class_hash_v2, to avoid the migration process. fn get_compiled_class_hash(&self, _class_hash: ClassHash) -> StateResult { - Ok(DUMMY_COMPILED_CLASS_HASH) + unimplemented!("The offline state reader does not support get_compiled_class_hash.") } - /// returns the same value as get_compiled_class_hash, to avoid the migration process. fn get_compiled_class_hash_v2( &self, class_hash: ClassHash, - _compiled_class: &RunnableCompiledClass, + compiled_class: &RunnableCompiledClass, ) -> StateResult { - self.get_compiled_class_hash(class_hash) + default_get_compiled_class_hash_v2(self, class_hash, compiled_class) } } diff --git a/crates/blockifier_reexecution/src/state_reader/reexecution_state_reader.rs b/crates/blockifier_reexecution/src/state_reader/reexecution_state_reader.rs index 45763cd5fab..fd09f29ae9c 100644 --- a/crates/blockifier_reexecution/src/state_reader/reexecution_state_reader.rs +++ b/crates/blockifier_reexecution/src/state_reader/reexecution_state_reader.rs @@ -9,19 +9,17 @@ use blockifier::transaction::account_transaction::ExecutionFlags; use blockifier::transaction::transaction_execution::Transaction as BlockifierTransaction; use starknet_api::block::{BlockHash, BlockNumber}; use starknet_api::contract_class::{ClassInfo, SierraVersion}; -use starknet_api::core::{ClassHash, CompiledClassHash}; +use starknet_api::core::ClassHash; use starknet_api::state::SierraContractClass; use starknet_api::test_utils::MAX_FEE; use starknet_api::transaction::{Transaction, TransactionHash}; -use starknet_core::types::{ContractClass as StarknetContractClass, Felt}; +use starknet_core::types::ContractClass as StarknetContractClass; use crate::assert_eq_state_diff; use crate::compile::{legacy_to_contract_class_v0, sierra_to_versioned_contract_class_v1}; use crate::errors::ReexecutionResult; use crate::utils::contract_class_to_compiled_classes; -pub const DUMMY_COMPILED_CLASS_HASH: CompiledClassHash = CompiledClassHash(Felt::ONE); - pub trait ReexecutionStateReader { fn get_contract_class(&self, class_hash: &ClassHash) -> StateResult; diff --git a/crates/blockifier_reexecution/src/state_reader/rpc_state_reader.rs b/crates/blockifier_reexecution/src/state_reader/rpc_state_reader.rs index a2c2d065a9b..c8c0688af4f 100644 --- a/crates/blockifier_reexecution/src/state_reader/rpc_state_reader.rs +++ b/crates/blockifier_reexecution/src/state_reader/rpc_state_reader.rs @@ -26,6 +26,7 @@ use blockifier::state::state_reader_and_contract_manager::{ FetchCompiledClasses, StateReaderAndContractManager, }; +use blockifier::state::utils::get_compiled_class_hash_v2 as default_get_compiled_class_hash_v2; use blockifier::transaction::transaction_execution::Transaction as BlockifierTransaction; use serde::Serialize; use serde_json::{json, Value}; @@ -54,7 +55,6 @@ use crate::state_reader::offline_state_reader::{ use crate::state_reader::reexecution_state_reader::{ ConsecutiveReexecutionStateReaders, ReexecutionStateReader, - DUMMY_COMPILED_CLASS_HASH, }; use crate::utils::{ disjoint_hashmap_union, @@ -156,23 +156,16 @@ impl StateReader for RpcStateReader { } } - /// Returns a dummy compiled class hash for reexecution purposes. - /// - /// This method is required since v0.14.1 for checking if compiled class hashes - /// need to be migrated from v1 to v2 format. - /// In reexecution we use a dummy value for both get_compiled_class_hash and - /// get_compiled_class_hash_v2, to avoid the migration process. fn get_compiled_class_hash(&self, _class_hash: ClassHash) -> StateResult { - Ok(DUMMY_COMPILED_CLASS_HASH) + unimplemented!("The rpc state reader does not support get_compiled_class_hash.") } - /// returns the same value as get_compiled_class_hash, to avoid the migration process. fn get_compiled_class_hash_v2( &self, class_hash: ClassHash, - _compiled_class: &RunnableCompiledClass, + compiled_class: &RunnableCompiledClass, ) -> StateResult { - self.get_compiled_class_hash(class_hash) + default_get_compiled_class_hash_v2(self, class_hash, compiled_class) } } @@ -291,15 +284,19 @@ impl RpcStateReader { .collect::>() } - pub fn get_versioned_constants(&self) -> ReexecutionResult<&'static VersionedConstants> { - Ok(VersionedConstants::get(&self.get_starknet_version()?)?) + pub fn get_versioned_constants(&self) -> ReexecutionResult { + let mut vc = VersionedConstants::get(&self.get_starknet_version()?)?.clone(); + // Casm hash migration is not supported. It requires compiled class hashes, and the + // reexecution state readers do not have them. + vc.enable_casm_hash_migration = false; + Ok(vc) } pub fn get_block_context(&self) -> ReexecutionResult { Ok(BlockContext::new( self.get_block_info()?, get_chain_info(&self.chain_id), - self.get_versioned_constants()?.clone(), + self.get_versioned_constants()?, BouncerConfig::max(), )) } @@ -330,6 +327,8 @@ impl RpcStateReader { )?) } + /// Get the commitment state diff of the current block. + /// Note: the commitment state diff does not include migrated_compiled_classes. pub fn get_state_diff(&self) -> ReexecutionResult { let raw_statediff = &retry_request!(self.retry_config, || self.rpc_state_reader.send_rpc_request(