From 674696610b5daa8ef09c48f6c26ae1887a081384 Mon Sep 17 00:00:00 2001 From: Yael Doweck Date: Sun, 28 Jan 2024 09:42:08 +0200 Subject: [PATCH] refactor: restructure old_block_number_and_hash as a struct --- crates/blockifier/src/block_context.rs | 1 - crates/blockifier/src/block_execution.rs | 17 +++++++++++++++-- crates/blockifier/src/block_execution_test.rs | 7 +++---- .../src/transaction_executor.rs | 5 ++--- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/crates/blockifier/src/block_context.rs b/crates/blockifier/src/block_context.rs index d30163407d..dd600754bb 100644 --- a/crates/blockifier/src/block_context.rs +++ b/crates/blockifier/src/block_context.rs @@ -40,7 +40,6 @@ impl BlockContext { pub fn block_info(&self) -> &BlockInfo { &self.block_info } - } #[derive(Clone, Debug)] diff --git a/crates/blockifier/src/block_execution.rs b/crates/blockifier/src/block_execution.rs index 5303bd18e9..7621e812c0 100644 --- a/crates/blockifier/src/block_execution.rs +++ b/crates/blockifier/src/block_execution.rs @@ -10,14 +10,27 @@ use crate::state::state_api::{State, StateResult}; #[path = "block_execution_test.rs"] pub mod test; +pub struct BlockNumberHashPair { + pub number: BlockNumber, + pub hash: BlockHash, +} + +impl BlockNumberHashPair { + pub fn new(block_number: u64, block_hash: StarkFelt) -> BlockNumberHashPair { + BlockNumberHashPair { number: BlockNumber(block_number), hash: BlockHash(block_hash) } + } +} + // Block pre-processing. // Writes the hash of the (current_block_number - N) block under its block number in the dedicated // contract state, where N=STORED_BLOCK_HASH_BUFFER. pub fn pre_process_block( state: &mut dyn State, - old_block_number_and_hash: Option<(BlockNumber, BlockHash)>, + old_block_number_and_hash: Option, ) -> StateResult<()> { - if let Some((block_number, block_hash)) = old_block_number_and_hash { + if let Some(BlockNumberHashPair { number: block_number, hash: block_hash }) = + old_block_number_and_hash + { state.set_storage_at( ContractAddress::try_from(StarkFelt::from(constants::BLOCK_HASH_CONTRACT_ADDRESS)) .expect("Failed to convert `BLOCK_HASH_CONTRACT_ADDRESS` to ContractAddress."), diff --git a/crates/blockifier/src/block_execution_test.rs b/crates/blockifier/src/block_execution_test.rs index 09f72a0a9e..5edc129a60 100644 --- a/crates/blockifier/src/block_execution_test.rs +++ b/crates/blockifier/src/block_execution_test.rs @@ -1,10 +1,9 @@ -use starknet_api::block::{BlockHash, BlockNumber}; use starknet_api::core::ContractAddress; use starknet_api::hash::StarkFelt; use starknet_api::state::StorageKey; use crate::abi::constants; -use crate::block_execution::pre_process_block; +use crate::block_execution::{pre_process_block, BlockNumberHashPair}; use crate::state::state_api::StateReader; use crate::test_utils::cached_state::create_test_state; @@ -13,8 +12,8 @@ fn test_pre_process_block() { let mut state = create_test_state(); let block_number: u64 = 10; - let block_hash = StarkFelt::from(20u32); - pre_process_block(&mut state, Some((BlockNumber(block_number), BlockHash(block_hash)))) + let block_hash = StarkFelt::from(20_u8); + pre_process_block(&mut state, Some(BlockNumberHashPair::new(block_number, block_hash))) .unwrap(); let written_hash = state.get_storage_at( diff --git a/crates/native_blockifier/src/transaction_executor.rs b/crates/native_blockifier/src/transaction_executor.rs index c4996fb137..cefce6a5aa 100644 --- a/crates/native_blockifier/src/transaction_executor.rs +++ b/crates/native_blockifier/src/transaction_executor.rs @@ -1,7 +1,7 @@ use std::collections::{HashMap, HashSet}; use blockifier::block_context::BlockContext; -use blockifier::block_execution::pre_process_block; +use blockifier::block_execution::{pre_process_block, BlockNumberHashPair}; use blockifier::execution::call_info::CallInfo; use blockifier::execution::entry_point::ExecutionResources; use blockifier::fee::actual_cost::ActualCost; @@ -15,7 +15,6 @@ use blockifier::transaction::transactions::{ExecutableTransaction, ValidatableTr use cairo_vm::vm::runners::builtin_runner::HASH_BUILTIN_NAME; use cairo_vm::vm::runners::cairo_runner::ExecutionResources as VmExecutionResources; use pyo3::prelude::*; -use starknet_api::block::{BlockHash, BlockNumber}; use starknet_api::core::ClassHash; use crate::errors::{NativeBlockifierError, NativeBlockifierResult}; @@ -184,7 +183,7 @@ impl TransactionExecutor { old_block_number_and_hash: Option<(u64, PyFelt)>, ) -> NativeBlockifierResult<()> { let old_block_number_and_hash = old_block_number_and_hash - .map(|(block_number, block_hash)| (BlockNumber(block_number), BlockHash(block_hash.0))); + .map(|(block_number, block_hash)| BlockNumberHashPair::new(block_number, block_hash.0)); pre_process_block(&mut self.state, old_block_number_and_hash)?;