Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
refactor: restructure old_block_number_and_hash as a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Yael-Starkware committed Jan 29, 2024
1 parent 491a5fc commit d7507fa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
17 changes: 15 additions & 2 deletions crates/blockifier/src/block_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockNumberHashPair>,
) -> 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."),
Expand Down
7 changes: 3 additions & 4 deletions crates/blockifier/src/block_execution_test.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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(
Expand Down
5 changes: 2 additions & 3 deletions crates/native_blockifier/src/transaction_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet};
use std::vec::IntoIter;

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, MessageL1CostInfo};
use blockifier::execution::entry_point::ExecutionResources;
use blockifier::fee::actual_cost::ActualCost;
Expand All @@ -16,7 +16,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};
Expand Down Expand Up @@ -199,7 +198,7 @@ impl<S: StateReader> TransactionExecutor<S> {
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)?;

Expand Down

0 comments on commit d7507fa

Please sign in to comment.