Skip to content

Commit 34ccb7e

Browse files
starknet_os_runner: aviv refactor the rpc state reader to compute the compiled class hash
1 parent 73f5b9d commit 34ccb7e

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

crates/blockifier_reexecution/src/state_reader/offline_state_reader.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use blockifier::state::state_reader_and_contract_manager::{
1616
FetchCompiledClasses,
1717
StateReaderAndContractManager,
1818
};
19+
use blockifier::state::utils::get_compiled_class_hash_v2 as default_get_compiled_class_hash_v2;
1920
use blockifier::transaction::transaction_execution::Transaction as BlockifierTransaction;
2021
use serde::{Deserialize, Serialize};
2122
use starknet_api::block::{BlockHash, BlockHashAndNumber, BlockInfo, BlockNumber, StarknetVersion};
@@ -31,7 +32,6 @@ use crate::errors::ReexecutionResult;
3132
use crate::state_reader::reexecution_state_reader::{
3233
ConsecutiveReexecutionStateReaders,
3334
ReexecutionStateReader,
34-
DUMMY_COMPILED_CLASS_HASH,
3535
};
3636
use crate::state_reader::rpc_state_reader::StarknetContractClassMapping;
3737
use crate::utils::get_chain_info;
@@ -171,23 +171,16 @@ impl StateReader for OfflineStateReader {
171171
}
172172
}
173173

174-
/// Returns a dummy compiled class hash for reexecution purposes.
175-
///
176-
/// This method is required since v0.14.1 for checking if compiled class hashes
177-
/// need to be migrated from v1 to v2 format.
178-
/// In reexecution we use a dummy value for both get_compiled_class_hash and
179-
/// get_compiled_class_hash_v2, to avoid the migration process.
180174
fn get_compiled_class_hash(&self, _class_hash: ClassHash) -> StateResult<CompiledClassHash> {
181-
Ok(DUMMY_COMPILED_CLASS_HASH)
175+
unimplemented!("The offline state reader does not support get_compiled_class_hash.")
182176
}
183177

184-
/// returns the same value as get_compiled_class_hash, to avoid the migration process.
185178
fn get_compiled_class_hash_v2(
186179
&self,
187180
class_hash: ClassHash,
188-
_compiled_class: &RunnableCompiledClass,
181+
compiled_class: &RunnableCompiledClass,
189182
) -> StateResult<CompiledClassHash> {
190-
self.get_compiled_class_hash(class_hash)
183+
default_get_compiled_class_hash_v2(self, class_hash, compiled_class)
191184
}
192185
}
193186

crates/blockifier_reexecution/src/state_reader/reexecution_state_reader.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@ use blockifier::transaction::account_transaction::ExecutionFlags;
99
use blockifier::transaction::transaction_execution::Transaction as BlockifierTransaction;
1010
use starknet_api::block::{BlockHash, BlockNumber};
1111
use starknet_api::contract_class::{ClassInfo, SierraVersion};
12-
use starknet_api::core::{ClassHash, CompiledClassHash};
12+
use starknet_api::core::ClassHash;
1313
use starknet_api::state::SierraContractClass;
1414
use starknet_api::test_utils::MAX_FEE;
1515
use starknet_api::transaction::{Transaction, TransactionHash};
16-
use starknet_core::types::{ContractClass as StarknetContractClass, Felt};
16+
use starknet_core::types::ContractClass as StarknetContractClass;
1717

1818
use crate::assert_eq_state_diff;
1919
use crate::compile::{legacy_to_contract_class_v0, sierra_to_versioned_contract_class_v1};
2020
use crate::errors::ReexecutionResult;
2121
use crate::utils::contract_class_to_compiled_classes;
2222

23-
pub const DUMMY_COMPILED_CLASS_HASH: CompiledClassHash = CompiledClassHash(Felt::ONE);
24-
2523
pub trait ReexecutionStateReader {
2624
fn get_contract_class(&self, class_hash: &ClassHash) -> StateResult<StarknetContractClass>;
2725

crates/blockifier_reexecution/src/state_reader/rpc_state_reader.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use blockifier::state::state_reader_and_contract_manager::{
2626
FetchCompiledClasses,
2727
StateReaderAndContractManager,
2828
};
29+
use blockifier::state::utils::get_compiled_class_hash_v2 as default_get_compiled_class_hash_v2;
2930
use blockifier::transaction::transaction_execution::Transaction as BlockifierTransaction;
3031
use serde::Serialize;
3132
use serde_json::{json, Value};
@@ -54,7 +55,6 @@ use crate::state_reader::offline_state_reader::{
5455
use crate::state_reader::reexecution_state_reader::{
5556
ConsecutiveReexecutionStateReaders,
5657
ReexecutionStateReader,
57-
DUMMY_COMPILED_CLASS_HASH,
5858
};
5959
use crate::utils::{
6060
disjoint_hashmap_union,
@@ -156,23 +156,18 @@ impl StateReader for RpcStateReader {
156156
}
157157
}
158158

159-
/// Returns a dummy compiled class hash for reexecution purposes.
160-
///
161-
/// This method is required since v0.14.1 for checking if compiled class hashes
162-
/// need to be migrated from v1 to v2 format.
163-
/// In reexecution we use a dummy value for both get_compiled_class_hash and
164-
/// get_compiled_class_hash_v2, to avoid the migration process.
165159
fn get_compiled_class_hash(&self, _class_hash: ClassHash) -> StateResult<CompiledClassHash> {
166-
Ok(DUMMY_COMPILED_CLASS_HASH)
160+
unimplemented!("The rpc state reader does not support get_compiled_class_hash.")
167161
}
168162

169-
/// returns the same value as get_compiled_class_hash, to avoid the migration process.
163+
/// Use the default implementation of get_compiled_class_hash_v2.
164+
/// Which compute the hash.
170165
fn get_compiled_class_hash_v2(
171166
&self,
172167
class_hash: ClassHash,
173-
_compiled_class: &RunnableCompiledClass,
168+
compiled_class: &RunnableCompiledClass,
174169
) -> StateResult<CompiledClassHash> {
175-
self.get_compiled_class_hash(class_hash)
170+
default_get_compiled_class_hash_v2(self, class_hash, compiled_class)
176171
}
177172
}
178173

@@ -291,15 +286,19 @@ impl RpcStateReader {
291286
.collect::<Result<_, _>>()
292287
}
293288

294-
pub fn get_versioned_constants(&self) -> ReexecutionResult<&'static VersionedConstants> {
295-
Ok(VersionedConstants::get(&self.get_starknet_version()?)?)
289+
pub fn get_versioned_constants(&self) -> ReexecutionResult<VersionedConstants> {
290+
let mut vc = VersionedConstants::get(&self.get_starknet_version()?)?.clone();
291+
// Casm hash migration is not supported. It requires compiled class hashes, and the
292+
// reexecution state readers do not have them.
293+
vc.enable_casm_hash_migration = false;
294+
Ok(vc)
296295
}
297296

298297
pub fn get_block_context(&self) -> ReexecutionResult<BlockContext> {
299298
Ok(BlockContext::new(
300299
self.get_block_info()?,
301300
get_chain_info(&self.chain_id),
302-
self.get_versioned_constants()?.clone(),
301+
self.get_versioned_constants()?,
303302
BouncerConfig::max(),
304303
))
305304
}
@@ -330,6 +329,8 @@ impl RpcStateReader {
330329
)?)
331330
}
332331

332+
/// Get the commitment state diff of the current block.
333+
/// Note: the commitment state diff does not include migrated_compiled_classes.
333334
pub fn get_state_diff(&self) -> ReexecutionResult<CommitmentStateDiff> {
334335
let raw_statediff =
335336
&retry_request!(self.retry_config, || self.rpc_state_reader.send_rpc_request(

0 commit comments

Comments
 (0)