Skip to content

Commit

Permalink
build(blockifier_reexecution): add preprocess before execute txs
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware committed Nov 6, 2024
1 parent f196be5 commit efeaeac
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
5 changes: 1 addition & 4 deletions crates/blockifier_reexecution/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use blockifier_reexecution::state_reader::test_state_reader::{
use blockifier_reexecution::state_reader::utils::JSON_RPC_VERSION;
use clap::{Args, Parser, Subcommand};
use starknet_api::block::BlockNumber;
use starknet_api::core::ContractAddress;
use starknet_gateway::config::RpcStateReaderConfig;

/// BlockifierReexecution CLI.
Expand Down Expand Up @@ -77,7 +76,7 @@ fn main() {
let all_txs_in_next_block =
test_state_readers_last_and_current_block.get_next_block_txs().unwrap();

let mut expected_state_diff =
let expected_state_diff =
test_state_readers_last_and_current_block.get_next_block_state_diff().unwrap();

let mut transaction_executor =
Expand All @@ -87,8 +86,6 @@ fn main() {
// Finalize block and read actual statediff.
let (actual_state_diff, _, _) =
transaction_executor.finalize().expect("Couldn't finalize block");
// TODO(Aner): compute correct block hash at storage slot 0x1 instead of removing it.
expected_state_diff.storage_updates.shift_remove(&ContractAddress(1_u128.into()));

// Compare the expected and actual state differences
// by avoiding discrepancies caused by insertion order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use blockifier::state::state_api::StateResult;
use blockifier::test_utils::MAX_FEE;
use blockifier::transaction::transaction_execution::Transaction as BlockifierTransaction;
use papyrus_execution::DEPRECATED_CONTRACT_SIERRA_SIZE;
use starknet_api::block::{BlockHash, BlockNumber};
use starknet_api::core::ClassHash;
use starknet_api::transaction::{Transaction, TransactionHash};
use starknet_core::types::ContractClass as StarknetContractClass;
Expand Down Expand Up @@ -72,4 +73,6 @@ pub(crate) trait ReexecutionStateReader {
})
.collect()
}

fn get_old_block_hash(&self, old_block_number: BlockNumber) -> ReexecutionResult<BlockHash>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::fs;
use std::sync::{Arc, Mutex};

use blockifier::abi::constants;
use blockifier::blockifier::block::BlockInfo;
use blockifier::blockifier::config::TransactionExecutorConfig;
use blockifier::blockifier::transaction_executor::TransactionExecutor;
Expand All @@ -15,14 +16,19 @@ use blockifier::transaction::transaction_execution::Transaction as BlockifierTra
use blockifier::versioned_constants::VersionedConstants;
use serde::{Deserialize, Serialize};
use serde_json::{json, to_value};
use starknet_api::block::{BlockNumber, StarknetVersion};
use starknet_api::block::{BlockHash, BlockHashAndNumber, BlockNumber, StarknetVersion};
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::state::StorageKey;
use starknet_api::transaction::{Transaction, TransactionHash};
use starknet_core::types::ContractClass as StarknetContractClass;
use starknet_gateway::config::RpcStateReaderConfig;
use starknet_gateway::errors::{serde_err_to_state_err, RPCStateReaderError};
use starknet_gateway::rpc_objects::{BlockHeader, GetBlockWithTxHashesParams, ResourcePrice};
use starknet_gateway::rpc_objects::{
BlockHeader,
BlockId,
GetBlockWithTxHashesParams,
ResourcePrice,
};
use starknet_gateway::rpc_state_reader::RpcStateReader;
use starknet_types_core::felt::Felt;

Expand Down Expand Up @@ -295,11 +301,17 @@ impl TestStateReader {
block_context_next_block: BlockContext,
transaction_executor_config: Option<TransactionExecutorConfig>,
) -> ReexecutionResult<TransactionExecutor<TestStateReader>> {
Ok(TransactionExecutor::<TestStateReader>::new(
CachedState::new(self),
let old_block_number = BlockNumber(
block_context_next_block.block_info().block_number.0
- constants::STORED_BLOCK_HASH_BUFFER,
);
let old_block_hash = self.get_old_block_hash(old_block_number)?;
Ok(TransactionExecutor::<TestStateReader>::pre_process_and_create(
self,
block_context_next_block,
Some(BlockHashAndNumber { number: old_block_number, hash: old_block_hash }),
transaction_executor_config.unwrap_or_default(),
))
)?)
}

pub fn get_state_diff(&self) -> ReexecutionResult<CommitmentStateDiff> {
Expand Down Expand Up @@ -375,6 +387,15 @@ impl ReexecutionStateReader for TestStateReader {
}
Ok(contract_class)
}

fn get_old_block_hash(&self, old_block_number: BlockNumber) -> ReexecutionResult<BlockHash> {
let block_id = BlockId::Number(old_block_number);
let params = GetBlockWithTxHashesParams { block_id };
let response =
self.rpc_state_reader.send_rpc_request("starknet_getBlockWithTxHashes", params)?;
let block_hash_raw: String = serde_json::from_value(response["block_hash"].clone())?;
Ok(BlockHash(Felt::from_hex(&block_hash_raw).unwrap()))
}
}

/// Trait of the functions \ queries required for reexecution.
Expand Down Expand Up @@ -503,6 +524,10 @@ impl ReexecutionStateReader for OfflineStateReader {
)))?
.clone())
}

fn get_old_block_hash(&self, _old_block_number: BlockNumber) -> ReexecutionResult<BlockHash> {
todo!()
}
}

impl OfflineStateReader {
Expand Down

0 comments on commit efeaeac

Please sign in to comment.