Skip to content

Commit

Permalink
chore: use BlockHashAndNumber from SN_API in blockifier (#1616)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-starkware authored Oct 30, 2024
1 parent fe129d2 commit ee6513d
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 83 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions crates/batcher/src/batcher_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ use std::sync::Arc;

use assert_matches::assert_matches;
use async_trait::async_trait;
use blockifier::blockifier::block::BlockNumberHashPair;
use futures::future::BoxFuture;
use futures::FutureExt;
use mockall::automock;
use mockall::predicate::eq;
use rstest::{fixture, rstest};
use starknet_api::block::BlockNumber;
use starknet_api::block::{BlockHashAndNumber, BlockNumber};
use starknet_api::core::{ContractAddress, Nonce, PatriciaKey, StateDiffCommitment};
use starknet_api::executable_transaction::Transaction;
use starknet_api::hash::PoseidonHash;
Expand Down Expand Up @@ -246,7 +245,7 @@ trait ProposalManagerTraitWrapper: Send + Sync {
fn wrap_build_block_proposal(
&mut self,
proposal_id: ProposalId,
retrospective_block_hash: Option<BlockNumberHashPair>,
retrospective_block_hash: Option<BlockHashAndNumber>,
deadline: tokio::time::Instant,
output_content_sender: tokio::sync::mpsc::UnboundedSender<Transaction>,
) -> BoxFuture<'_, Result<(), BuildProposalError>>;
Expand All @@ -271,7 +270,7 @@ impl<T: ProposalManagerTraitWrapper> ProposalManagerTrait for T {
async fn build_block_proposal(
&mut self,
proposal_id: ProposalId,
retrospective_block_hash: Option<BlockNumberHashPair>,
retrospective_block_hash: Option<BlockHashAndNumber>,
deadline: tokio::time::Instant,
output_content_sender: tokio::sync::mpsc::UnboundedSender<Transaction>,
) -> Result<(), BuildProposalError> {
Expand Down
10 changes: 5 additions & 5 deletions crates/batcher/src/block_builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::BTreeMap;

use async_trait::async_trait;
use blockifier::blockifier::block::{BlockInfo, BlockNumberHashPair, GasPrices};
use blockifier::blockifier::block::{BlockInfo, GasPrices};
use blockifier::blockifier::config::TransactionExecutorConfig;
use blockifier::blockifier::transaction_executor::{
TransactionExecutor,
Expand All @@ -26,7 +26,7 @@ use papyrus_config::dumping::{append_sub_config_name, ser_param, SerializeConfig
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use papyrus_storage::StorageReader;
use serde::{Deserialize, Serialize};
use starknet_api::block::{BlockNumber, BlockTimestamp, NonzeroGasPrice};
use starknet_api::block::{BlockHashAndNumber, BlockNumber, BlockTimestamp, NonzeroGasPrice};
use starknet_api::core::ContractAddress;
use starknet_api::executable_transaction::Transaction;
use starknet_api::transaction::TransactionHash;
Expand Down Expand Up @@ -216,7 +216,7 @@ pub trait BlockBuilderFactoryTrait {
fn create_block_builder(
&self,
height: BlockNumber,
retrospective_block_hash: Option<BlockNumberHashPair>,
retrospective_block_hash: Option<BlockHashAndNumber>,
) -> BlockBuilderResult<Box<dyn BlockBuilderTrait>>;
}

Expand All @@ -243,7 +243,7 @@ impl BlockBuilderFactory {
fn preprocess_and_create_transaction_executor(
&self,
height: BlockNumber,
retrospective_block_hash: Option<BlockNumberHashPair>,
retrospective_block_hash: Option<BlockHashAndNumber>,
) -> BlockBuilderResult<TransactionExecutor<PapyrusReader>> {
let block_builder_config = self.block_builder_config.clone();
let next_block_info = BlockInfo {
Expand Down Expand Up @@ -293,7 +293,7 @@ impl BlockBuilderFactoryTrait for BlockBuilderFactory {
fn create_block_builder(
&self,
height: BlockNumber,
retrospective_block_hash: Option<BlockNumberHashPair>,
retrospective_block_hash: Option<BlockHashAndNumber>,
) -> BlockBuilderResult<Box<dyn BlockBuilderTrait>> {
let executor =
self.preprocess_and_create_transaction_executor(height, retrospective_block_hash)?;
Expand Down
7 changes: 3 additions & 4 deletions crates/batcher/src/proposal_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use async_trait::async_trait;
use blockifier::blockifier::block::BlockNumberHashPair;
use indexmap::IndexMap;
use starknet_api::block::BlockNumber;
use starknet_api::block::{BlockHashAndNumber, BlockNumber};
use starknet_api::block_hash::state_diff_hash::calculate_state_diff_hash;
use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::executable_transaction::Transaction;
Expand Down Expand Up @@ -70,7 +69,7 @@ pub trait ProposalManagerTrait: Send + Sync {
async fn build_block_proposal(
&mut self,
proposal_id: ProposalId,
retrospective_block_hash: Option<BlockNumberHashPair>,
retrospective_block_hash: Option<BlockHashAndNumber>,
deadline: tokio::time::Instant,
tx_sender: tokio::sync::mpsc::UnboundedSender<Transaction>,
) -> Result<(), BuildProposalError>;
Expand Down Expand Up @@ -153,7 +152,7 @@ impl ProposalManagerTrait for ProposalManager {
async fn build_block_proposal(
&mut self,
proposal_id: ProposalId,
retrospective_block_hash: Option<BlockNumberHashPair>,
retrospective_block_hash: Option<BlockHashAndNumber>,
deadline: tokio::time::Instant,
tx_sender: tokio::sync::mpsc::UnboundedSender<Transaction>,
) -> Result<(), BuildProposalError> {
Expand Down
1 change: 0 additions & 1 deletion crates/batcher_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ workspace = true

[dependencies]
async-trait.workspace = true
blockifier.workspace = true
chrono = { workspace = true, features = ["serde"] }
derive_more.workspace = true
mockall.workspace = true
Expand Down
6 changes: 2 additions & 4 deletions crates/batcher_types/src/batcher_types.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::fmt::Debug;

// TODO(Dan): move to SN_API
pub use blockifier::blockifier::block::BlockNumberHashPair;
use chrono::prelude::*;
use serde::{Deserialize, Serialize};
use starknet_api::block::BlockNumber;
use starknet_api::block::{BlockHashAndNumber, BlockNumber};
use starknet_api::core::StateDiffCommitment;
use starknet_api::executable_transaction::Transaction;

Expand Down Expand Up @@ -36,7 +34,7 @@ pub struct ProposalCommitment {
pub struct BuildProposalInput {
pub proposal_id: ProposalId,
pub deadline: chrono::DateTime<Utc>,
pub retrospective_block_hash: Option<BlockNumberHashPair>,
pub retrospective_block_hash: Option<BlockHashAndNumber>,
// TODO: Should we get the gas price here?
}

Expand Down
3 changes: 0 additions & 3 deletions crates/batcher_types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
pub mod batcher_types;
pub mod communication;
pub mod errors;

// TODO(Dan): move to SN_API
pub use batcher_types::BlockNumberHashPair;
30 changes: 5 additions & 25 deletions crates/blockifier/src/blockifier/block.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use log::warn;
use serde::{Deserialize, Serialize};
use starknet_api::block::{
BlockHash,
BlockHashAndNumber,
BlockNumber,
BlockTimestamp,
GasPrice,
Expand All @@ -10,7 +9,6 @@ use starknet_api::block::{
};
use starknet_api::core::ContractAddress;
use starknet_api::state::StorageKey;
use starknet_types_core::felt::Felt;

use crate::abi::constants;
use crate::state::errors::StateError;
Expand Down Expand Up @@ -111,37 +109,19 @@ impl GasPrices {
// block hash table.
pub fn pre_process_block(
state: &mut dyn State,
old_block_number_and_hash: Option<BlockNumberHashPair>,
old_block_number_and_hash: Option<BlockHashAndNumber>,
next_block_number: BlockNumber,
) -> StateResult<()> {
let should_block_hash_be_provided =
next_block_number >= BlockNumber(constants::STORED_BLOCK_HASH_BUFFER);
if let Some(BlockNumberHashPair { number: block_number, hash: block_hash }) =
old_block_number_and_hash
{
if let Some(BlockHashAndNumber { number, hash }) = old_block_number_and_hash {
let block_hash_contract_address =
ContractAddress::from(constants::BLOCK_HASH_CONTRACT_ADDRESS);
let block_number_as_storage_key = StorageKey::from(block_number.0);
state.set_storage_at(
block_hash_contract_address,
block_number_as_storage_key,
block_hash.0,
)?;
let block_number_as_storage_key = StorageKey::from(number.0);
state.set_storage_at(block_hash_contract_address, block_number_as_storage_key, hash.0)?;
} else if should_block_hash_be_provided {
return Err(StateError::OldBlockHashNotProvided);
}

Ok(())
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockNumberHashPair {
pub number: BlockNumber,
pub hash: BlockHash,
}

impl BlockNumberHashPair {
pub fn new(block_number: u64, block_hash: Felt) -> BlockNumberHashPair {
BlockNumberHashPair { number: BlockNumber(block_number), hash: BlockHash(block_hash) }
}
}
6 changes: 3 additions & 3 deletions crates/blockifier/src/blockifier/block_test.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use starknet_api::block::BlockNumber;
use starknet_api::block::{BlockHash, BlockHashAndNumber, BlockNumber};
use starknet_api::core::ContractAddress;
use starknet_api::felt;
use starknet_api::state::StorageKey;

use crate::abi::constants;
use crate::blockifier::block::{pre_process_block, BlockNumberHashPair};
use crate::blockifier::block::pre_process_block;
use crate::context::ChainInfo;
use crate::state::state_api::StateReader;
use crate::test_utils::contracts::FeatureContract;
Expand All @@ -21,7 +21,7 @@ fn test_pre_process_block() {
let block_hash = felt!(20_u8);
pre_process_block(
&mut state,
Some(BlockNumberHashPair::new(block_number.0, block_hash)),
Some(BlockHashAndNumber { hash: BlockHash(block_hash), number: block_number }),
block_number,
)
.unwrap();
Expand Down
5 changes: 3 additions & 2 deletions crates/blockifier/src/blockifier/transaction_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use std::sync::{Arc, Mutex};

use itertools::FoldWhile::{Continue, Done};
use itertools::Itertools;
use starknet_api::block::BlockHashAndNumber;
use starknet_api::core::ClassHash;
use thiserror::Error;

use crate::blockifier::block::{pre_process_block, BlockNumberHashPair};
use crate::blockifier::block::pre_process_block;
use crate::blockifier::config::TransactionExecutorConfig;
use crate::bouncer::{Bouncer, BouncerWeights};
use crate::concurrency::worker_logic::WorkerExecutor;
Expand Down Expand Up @@ -59,7 +60,7 @@ impl<S: StateReader> TransactionExecutor<S> {
pub fn pre_process_and_create(
initial_state_reader: S,
block_context: BlockContext,
old_block_number_and_hash: Option<BlockNumberHashPair>,
old_block_number_and_hash: Option<BlockHashAndNumber>,
config: TransactionExecutorConfig,
) -> StateResult<Self> {
let mut block_state = CachedState::new(initial_state_reader);
Expand Down
11 changes: 8 additions & 3 deletions crates/blockifier/src/blockifier/transaction_executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use starknet_api::transaction::{Fee, TransactionVersion};
use starknet_api::{declare_tx_args, deploy_account_tx_args, felt, invoke_tx_args, nonce};
use starknet_types_core::felt::Felt;

use crate::blockifier::block::BlockNumberHashPair;
use crate::blockifier::config::TransactionExecutorConfig;
use crate::blockifier::transaction_executor::{
TransactionExecutor,
Expand All @@ -21,7 +20,13 @@ use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::declare::declare_tx;
use crate::test_utils::deploy_account::deploy_account_tx;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{create_calldata, CairoVersion, BALANCE, DEFAULT_STRK_L1_GAS_PRICE};
use crate::test_utils::{
create_calldata,
maybe_dummy_block_hash_and_number,
CairoVersion,
BALANCE,
DEFAULT_STRK_L1_GAS_PRICE,
};
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::test_utils::{
account_invoke_tx,
Expand All @@ -41,7 +46,7 @@ fn tx_executor_test_body<S: StateReader>(
expected_bouncer_weights: BouncerWeights,
) {
let block_number_hash_pair =
BlockNumberHashPair::create_dummy_given_current(block_context.block_info().block_number);
maybe_dummy_block_hash_and_number(block_context.block_info().block_number);
let mut tx_executor = TransactionExecutor::pre_process_and_create(
state,
block_context,
Expand Down
14 changes: 13 additions & 1 deletion crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ use std::path::PathBuf;

use cairo_vm::types::builtin_name::BuiltinName;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::block::{GasPrice, NonzeroGasPrice};
use starknet_api::block::{BlockHash, BlockHashAndNumber, BlockNumber, GasPrice, NonzeroGasPrice};
use starknet_api::core::{ClassHash, ContractAddress, PatriciaKey};
use starknet_api::execution_resources::{GasAmount, GasVector};
use starknet_api::hash::StarkHash;
use starknet_api::state::StorageKey;
use starknet_api::transaction::{
Calldata,
Expand All @@ -30,6 +31,7 @@ use starknet_api::{contract_address, felt, patricia_key};
use starknet_types_core::felt::Felt;

use crate::abi::abi_utils::{get_fee_token_var_address, selector_from_name};
use crate::abi::constants;
use crate::execution::call_info::ExecutionSummary;
use crate::execution::contract_class::TrackedResource;
use crate::execution::deprecated_syscalls::hint_processor::SyscallCounter;
Expand Down Expand Up @@ -459,3 +461,13 @@ pub fn get_vm_resource_usage() -> ExecutionResources {
]),
}
}

pub fn maybe_dummy_block_hash_and_number(block_number: BlockNumber) -> Option<BlockHashAndNumber> {
if block_number.0 < constants::STORED_BLOCK_HASH_BUFFER {
return None;
}
Some(BlockHashAndNumber {
number: BlockNumber(block_number.0 - constants::STORED_BLOCK_HASH_BUFFER),
hash: BlockHash(StarkHash::ONE),
})
}
18 changes: 2 additions & 16 deletions crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ use std::sync::Arc;

use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use serde_json::Value;
use starknet_api::block::{BlockHash, BlockNumber, BlockTimestamp, NonzeroGasPrice};
use starknet_api::block::{BlockNumber, BlockTimestamp, NonzeroGasPrice};
use starknet_api::core::{ChainId, ClassHash, ContractAddress, Nonce, PatriciaKey};
use starknet_api::hash::StarkHash;
use starknet_api::transaction::{Fee, TransactionHash, TransactionVersion};
use starknet_api::{calldata, contract_address, patricia_key};
use starknet_types_core::felt::Felt;

use super::update_json_value;
use crate::abi::abi_utils::selector_from_name;
use crate::abi::constants;
use crate::blockifier::block::{BlockInfo, BlockNumberHashPair, GasPrices};
use crate::blockifier::block::{BlockInfo, GasPrices};
use crate::bouncer::{BouncerConfig, BouncerWeights, BuiltinCount};
use crate::context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionContext};
use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
Expand Down Expand Up @@ -122,18 +120,6 @@ impl GasCosts {
}
}

impl BlockNumberHashPair {
pub fn create_dummy_given_current(block_number: BlockNumber) -> Option<Self> {
if block_number.0 < constants::STORED_BLOCK_HASH_BUFFER {
return None;
}
Some(Self {
number: BlockNumber(block_number.0 - constants::STORED_BLOCK_HASH_BUFFER),
hash: BlockHash(StarkHash::ONE),
})
}
}

impl ChainInfo {
pub fn create_for_testing() -> Self {
Self {
Expand Down
10 changes: 6 additions & 4 deletions crates/native_blockifier/src/py_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use blockifier::blockifier::block::BlockNumberHashPair;
use num_bigint::BigUint;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use starknet_api::block::{BlockHash, BlockHashAndNumber, BlockNumber};
use starknet_api::core::{ChainId, ClassHash, CompiledClassHash, ContractAddress, EthAddress};
use starknet_api::state::StorageKey;
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -100,7 +100,9 @@ where

pub fn into_block_number_hash_pair(
old_block_number_and_hash: Option<(u64, PyFelt)>,
) -> Option<BlockNumberHashPair> {
old_block_number_and_hash
.map(|(block_number, block_hash)| BlockNumberHashPair::new(block_number, block_hash.0))
) -> Option<BlockHashAndNumber> {
old_block_number_and_hash.map(|(block_number, block_hash)| BlockHashAndNumber {
number: BlockNumber(block_number),
hash: BlockHash(block_hash.0),
})
}
Loading

0 comments on commit ee6513d

Please sign in to comment.