Skip to content

Commit a3aaa55

Browse files
Merge pull request #1549 from eqlabs/krisztian/fix-get-block-hash-syscall-for-traces
fix(executor): add block hash value to the state for trace execution
2 parents 4de98c2 + 95acd77 commit a3aaa55

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ More expansive patch notes and explanations may be found in the specific [pathfi
77
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
88
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
99

10+
## Unreleased
11+
12+
### Fixed
13+
14+
- `get_block_hash` syscall returns `0x0` for the latest available block (current - 10) when executing `starknet_trace*` methods
15+
1016
## [0.9.6] - 2023-11-20
1117

1218
### Fixed

crates/executor/src/execution_state.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use super::state_reader::PathfinderStateReader;
2-
use crate::state_reader::LruCachedReader;
3-
use blockifier::{block_context::BlockContext, state::cached_state::CachedState};
2+
use crate::{state_reader::LruCachedReader, IntoStarkFelt};
3+
use anyhow::Context;
4+
use blockifier::{
5+
block_context::BlockContext,
6+
state::{cached_state::CachedState, state_api::State},
7+
};
48
use pathfinder_common::{BlockHeader, ChainId, StateUpdate};
59

610
pub struct ExecutionState<'tx> {
@@ -33,6 +37,31 @@ impl<'tx> ExecutionState<'tx> {
3337
);
3438
let mut cached_state = LruCachedReader::new_cached_state(raw_reader)?;
3539

40+
// Perform system contract updates if we are executing ontop of a parent block.
41+
// Currently this is only the block hash from 10 blocks ago.
42+
if self.execute_on_parent_state && self.header.number.get() >= 10 {
43+
let block_number_whose_hash_becomes_available =
44+
pathfinder_common::BlockNumber::new_or_panic(self.header.number.get() - 10);
45+
let (_, block_hash) = self
46+
.transaction
47+
.block_id(block_number_whose_hash_becomes_available.into())?
48+
.context("Getting historical block hash")?;
49+
50+
tracing::trace!(%block_number_whose_hash_becomes_available, %block_hash, "Setting historical block hash");
51+
52+
cached_state.set_storage_at(
53+
starknet_api::core::ContractAddress(starknet_api::core::PatriciaKey::try_from(
54+
starknet_api::hash::StarkFelt::from(1u8),
55+
)?),
56+
starknet_api::state::StorageKey(starknet_api::core::PatriciaKey::try_from(
57+
starknet_api::hash::StarkFelt::from(
58+
block_number_whose_hash_becomes_available.get(),
59+
),
60+
)?),
61+
block_hash.0.into_starkfelt(),
62+
)
63+
}
64+
3665
self.pending_state.as_ref().map(|pending_state| {
3766
super::pending::apply_pending_update(&mut cached_state, pending_state)
3867
});

crates/executor/src/simulate.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ pub fn trace_one(
104104
) -> Result<TransactionTrace, CallError> {
105105
let (mut state, block_context) = execution_state.starknet_state()?;
106106

107-
for tx in transactions {
107+
for (transaction_idx, tx) in transactions.into_iter().enumerate() {
108+
let _span = tracing::debug_span!("simulate", transaction_hash=%super::transaction::transaction_hash(&tx), %transaction_idx).entered();
109+
108110
let hash = transaction_hash(&tx);
109111
let tx_type = transaction_type(&tx);
110112
let tx_declared_deprecated_class_hash = transaction_declared_deprecated_class(&tx);
@@ -135,7 +137,9 @@ pub fn trace_all(
135137
let (mut state, block_context) = execution_state.starknet_state()?;
136138

137139
let mut ret = Vec::with_capacity(transactions.len());
138-
for tx in transactions {
140+
for (transaction_idx, tx) in transactions.into_iter().enumerate() {
141+
let _span = tracing::debug_span!("simulate", transaction_hash=%super::transaction::transaction_hash(&tx), %transaction_idx).entered();
142+
139143
let hash = transaction_hash(&tx);
140144
let tx_type = transaction_type(&tx);
141145
let tx_declared_deprecated_class_hash = transaction_declared_deprecated_class(&tx);
@@ -272,8 +276,6 @@ fn to_trace(
272276
execution_info: blockifier::transaction::objects::TransactionExecutionInfo,
273277
state_diff: StateDiff,
274278
) -> Result<TransactionTrace, TransactionExecutionError> {
275-
tracing::trace!(?execution_info, "Transforming trace");
276-
277279
let validate_invocation = execution_info
278280
.validate_call_info
279281
.map(TryInto::try_into)

0 commit comments

Comments
 (0)