Skip to content

Commit

Permalink
Merge pull request #1549 from eqlabs/krisztian/fix-get-block-hash-sys…
Browse files Browse the repository at this point in the history
…call-for-traces

fix(executor): add block hash value to the state for trace execution
  • Loading branch information
Mirko-von-Leipzig authored Nov 21, 2023
2 parents 4de98c2 + 95acd77 commit a3aaa55
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ More expansive patch notes and explanations may be found in the specific [pathfi
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

- `get_block_hash` syscall returns `0x0` for the latest available block (current - 10) when executing `starknet_trace*` methods

## [0.9.6] - 2023-11-20

### Fixed
Expand Down
33 changes: 31 additions & 2 deletions crates/executor/src/execution_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use super::state_reader::PathfinderStateReader;
use crate::state_reader::LruCachedReader;
use blockifier::{block_context::BlockContext, state::cached_state::CachedState};
use crate::{state_reader::LruCachedReader, IntoStarkFelt};
use anyhow::Context;
use blockifier::{
block_context::BlockContext,
state::{cached_state::CachedState, state_api::State},
};
use pathfinder_common::{BlockHeader, ChainId, StateUpdate};

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

// Perform system contract updates if we are executing ontop of a parent block.
// Currently this is only the block hash from 10 blocks ago.
if self.execute_on_parent_state && self.header.number.get() >= 10 {
let block_number_whose_hash_becomes_available =
pathfinder_common::BlockNumber::new_or_panic(self.header.number.get() - 10);
let (_, block_hash) = self
.transaction
.block_id(block_number_whose_hash_becomes_available.into())?
.context("Getting historical block hash")?;

tracing::trace!(%block_number_whose_hash_becomes_available, %block_hash, "Setting historical block hash");

cached_state.set_storage_at(
starknet_api::core::ContractAddress(starknet_api::core::PatriciaKey::try_from(
starknet_api::hash::StarkFelt::from(1u8),
)?),
starknet_api::state::StorageKey(starknet_api::core::PatriciaKey::try_from(
starknet_api::hash::StarkFelt::from(
block_number_whose_hash_becomes_available.get(),
),
)?),
block_hash.0.into_starkfelt(),
)
}

self.pending_state.as_ref().map(|pending_state| {
super::pending::apply_pending_update(&mut cached_state, pending_state)
});
Expand Down
10 changes: 6 additions & 4 deletions crates/executor/src/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ pub fn trace_one(
) -> Result<TransactionTrace, CallError> {
let (mut state, block_context) = execution_state.starknet_state()?;

for tx in transactions {
for (transaction_idx, tx) in transactions.into_iter().enumerate() {
let _span = tracing::debug_span!("simulate", transaction_hash=%super::transaction::transaction_hash(&tx), %transaction_idx).entered();

let hash = transaction_hash(&tx);
let tx_type = transaction_type(&tx);
let tx_declared_deprecated_class_hash = transaction_declared_deprecated_class(&tx);
Expand Down Expand Up @@ -135,7 +137,9 @@ pub fn trace_all(
let (mut state, block_context) = execution_state.starknet_state()?;

let mut ret = Vec::with_capacity(transactions.len());
for tx in transactions {
for (transaction_idx, tx) in transactions.into_iter().enumerate() {
let _span = tracing::debug_span!("simulate", transaction_hash=%super::transaction::transaction_hash(&tx), %transaction_idx).entered();

let hash = transaction_hash(&tx);
let tx_type = transaction_type(&tx);
let tx_declared_deprecated_class_hash = transaction_declared_deprecated_class(&tx);
Expand Down Expand Up @@ -272,8 +276,6 @@ fn to_trace(
execution_info: blockifier::transaction::objects::TransactionExecutionInfo,
state_diff: StateDiff,
) -> Result<TransactionTrace, TransactionExecutionError> {
tracing::trace!(?execution_info, "Transforming trace");

let validate_invocation = execution_info
.validate_call_info
.map(TryInto::try_into)
Expand Down

0 comments on commit a3aaa55

Please sign in to comment.