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

Commit

Permalink
refactor: rename context modules
Browse files Browse the repository at this point in the history
- Rename block_context.rs -> context.rs. This will hold all context
  types.
- Rename block_execution.rs -> block.rs and move `BlockInfo` and
  `GasPrices` there (`GasPrices` is only used inside `BlockInfo`).

No other changes.
  • Loading branch information
Gilad Chase committed Jan 22, 2024
1 parent 0716d4d commit 1603e60
Show file tree
Hide file tree
Showing 30 changed files with 139 additions and 134 deletions.
67 changes: 67 additions & 0 deletions crates/blockifier/src/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use starknet_api::block::{BlockHash, BlockNumber, BlockTimestamp};
use starknet_api::core::ContractAddress;
use starknet_api::hash::StarkFelt;
use starknet_api::state::StorageKey;

use crate::abi::constants;
use crate::state::state_api::{State, StateResult};
use crate::transaction::objects::FeeType;

#[cfg(test)]
#[path = "block_test.rs"]
pub mod block_test;

#[derive(Clone, Debug)]
pub struct BlockInfo {
pub block_number: BlockNumber,
pub block_timestamp: BlockTimestamp,

// Fee-related.
pub sequencer_address: ContractAddress,
pub gas_prices: GasPrices,
pub use_kzg_da: bool,
}

#[derive(Clone, Debug)]
pub struct GasPrices {
pub eth_l1_gas_price: u128, // In wei.
pub strk_l1_gas_price: u128, // In fri.
pub eth_l1_data_gas_price: u128, // In wei.
pub strk_l1_data_gas_price: u128, // In fri.
}

impl GasPrices {
pub fn get_gas_price_by_fee_type(&self, fee_type: &FeeType) -> u128 {
match fee_type {
FeeType::Strk => self.strk_l1_gas_price,
FeeType::Eth => self.eth_l1_gas_price,
}
}

pub fn get_data_gas_price_by_fee_type(&self, fee_type: &FeeType) -> u128 {
match fee_type {
FeeType::Strk => self.strk_l1_data_gas_price,
FeeType::Eth => self.eth_l1_data_gas_price,
}
}
}

// 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)>,
) -> StateResult<()> {
if let Some((block_number, 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."),
StorageKey::try_from(StarkFelt::from(block_number.0))
.expect("Failed to convert BlockNumber to StorageKey."),
block_hash.0,
)?;
}

Ok(())
}
74 changes: 0 additions & 74 deletions crates/blockifier/src/block_context.rs

This file was deleted.

31 changes: 0 additions & 31 deletions crates/blockifier/src/block_execution.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use starknet_api::hash::StarkFelt;
use starknet_api::state::StorageKey;

use crate::abi::constants;
use crate::block_execution::pre_process_block;
use crate::block::pre_process_block;
use crate::state::state_api::StateReader;
use crate::test_utils::cached_state::create_test_state;

Expand Down
39 changes: 39 additions & 0 deletions crates/blockifier/src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use starknet_api::core::{ChainId, ContractAddress};

use crate::block::BlockInfo;
use crate::transaction::objects::FeeType;
use crate::versioned_constants::VersionedConstants;

#[derive(Clone, Debug)]
pub struct BlockContext {
pub block_info: BlockInfo,
pub chain_info: ChainInfo,
pub versioned_constants: VersionedConstants,
}

#[derive(Clone, Debug)]
pub struct ChainInfo {
pub chain_id: ChainId,
pub fee_token_addresses: FeeTokenAddresses,
}

impl ChainInfo {
pub fn fee_token_address(&self, fee_type: &FeeType) -> ContractAddress {
self.fee_token_addresses.get_by_fee_type(fee_type)
}
}

#[derive(Clone, Debug)]
pub struct FeeTokenAddresses {
pub strk_fee_token_address: ContractAddress,
pub eth_fee_token_address: ContractAddress,
}

impl FeeTokenAddresses {
pub fn get_by_fee_type(&self, fee_type: &FeeType) -> ContractAddress {
match fee_type {
FeeType::Strk => self.strk_fee_token_address,
FeeType::Eth => self.eth_fee_token_address,
}
}
}
2 changes: 1 addition & 1 deletion crates/blockifier/src/execution/contract_address_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use starknet_api::{calldata, stark_felt};

use crate::abi::abi_utils::selector_from_name;
use crate::abi::constants;
use crate::block_context::ChainInfo;
use crate::context::ChainInfo;
use crate::execution::call_info::{CallExecution, Retdata};
use crate::execution::entry_point::CallEntryPoint;
use crate::retdata;
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/execution/entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use starknet_api::transaction::{Calldata, TransactionVersion};

use crate::abi::abi_utils::selector_from_name;
use crate::abi::constants;
use crate::block_context::BlockContext;
use crate::context::BlockContext;
use crate::execution::call_info::CallInfo;
use crate::execution::common_hints::ExecutionMode;
use crate::execution::deprecated_syscalls::hint_processor::SyscallCounter;
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/execution/entry_point_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use starknet_api::{calldata, patricia_key, stark_felt};

use crate::abi::abi_utils::{get_storage_var_address, selector_from_name};
use crate::abi::constants;
use crate::block_context::ChainInfo;
use crate::context::ChainInfo;
use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
use crate::execution::contract_class::ContractClass;
use crate::execution::entry_point::CallEntryPoint;
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/execution/syscalls/syscalls_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use test_case::test_case;

use crate::abi::abi_utils::selector_from_name;
use crate::abi::constants;
use crate::block_context::ChainInfo;
use crate::context::ChainInfo;
use crate::execution::call_info::{
CallExecution, CallInfo, MessageToL1, OrderedEvent, OrderedL2ToL1Message, Retdata,
};
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/fee/actual_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use starknet_api::transaction::Fee;

use super::gas_usage::calculate_tx_gas_usage;
use crate::abi::constants as abi_constants;
use crate::block_context::BlockContext;
use crate::context::BlockContext;
use crate::execution::call_info::CallInfo;
use crate::execution::entry_point::ExecutionResources;
use crate::state::cached_state::{CachedState, StateChanges, StateChangesCount};
Expand Down
3 changes: 2 additions & 1 deletion crates/blockifier/src/fee/fee_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use starknet_api::hash::StarkFelt;
use starknet_api::transaction::Fee;
use thiserror::Error;

use crate::block_context::{BlockContext, BlockInfo, ChainInfo};
use crate::block::BlockInfo;
use crate::context::{BlockContext, ChainInfo};
use crate::fee::actual_cost::ActualCost;
use crate::fee::fee_utils::{
calculate_tx_l1_gas_usage, get_balance_and_if_covers_fee, get_fee_by_l1_gas_usage,
Expand Down
3 changes: 2 additions & 1 deletion crates/blockifier/src/fee/fee_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use starknet_api::hash::StarkFelt;
use starknet_api::transaction::Fee;

use crate::abi::constants;
use crate::block_context::{BlockContext, BlockInfo, ChainInfo};
use crate::block::BlockInfo;
use crate::context::{BlockContext, ChainInfo};
use crate::state::state_api::StateReader;
use crate::transaction::errors::TransactionFeeError;
use crate::transaction::objects::{
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/fee/gas_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use starknet_api::transaction::Fee;

use super::fee_utils::{calculate_tx_l1_gas_usage, get_fee_by_l1_gas_usage};
use crate::abi::constants;
use crate::block_context::BlockContext;
use crate::context::BlockContext;
use crate::execution::call_info::CallInfo;
use crate::fee::eth_gas_constants;
use crate::fee::os_resources::OS_RESOURCES;
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod abi;
pub mod block_context;
pub mod block_execution;
pub mod block;
pub mod context;
pub mod execution;
pub mod fee;
pub mod state;
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/state/cached_state_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use starknet_api::core::PatriciaKey;
use starknet_api::hash::StarkHash;
use starknet_api::{class_hash, contract_address, patricia_key, stark_felt};

use crate::block_context::BlockContext;
use crate::context::BlockContext;
use crate::state::cached_state::*;
use crate::test_utils::cached_state::deprecated_create_test_state;
use crate::test_utils::dict_state_reader::DictStateReader;
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/test_utils/initial_test_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use starknet_api::stark_felt;
use strum::IntoEnumIterator;

use crate::abi::abi_utils::get_fee_token_var_address;
use crate::block_context::ChainInfo;
use crate::context::ChainInfo;
use crate::state::cached_state::CachedState;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::dict_state_reader::DictStateReader;
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/test_utils/prices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use starknet_api::transaction::Calldata;
use starknet_api::{calldata, stark_felt};

use crate::abi::abi_utils::{get_fee_token_var_address, selector_from_name};
use crate::block_context::BlockContext;
use crate::context::BlockContext;
use crate::execution::common_hints::ExecutionMode;
use crate::execution::entry_point::{
CallEntryPoint, EntryPointExecutionContext, ExecutionResources,
Expand Down
3 changes: 2 additions & 1 deletion crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use super::{
TEST_ERC20_CONTRACT_ADDRESS, TEST_ERC20_CONTRACT_ADDRESS2, TEST_SEQUENCER_ADDRESS,
};
use crate::abi::constants;
use crate::block_context::{BlockContext, BlockInfo, ChainInfo, FeeTokenAddresses, GasPrices};
use crate::block::{BlockInfo, GasPrices};
use crate::context::{BlockContext, ChainInfo, FeeTokenAddresses};
use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
use crate::execution::contract_class::{ContractClassV0, ContractClassV1};
use crate::execution::entry_point::{
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use starknet_api::transaction::{Calldata, Fee, ResourceBounds, TransactionVersio

use crate::abi::abi_utils::selector_from_name;
use crate::abi::constants as abi_constants;
use crate::block_context::BlockContext;
use crate::context::BlockContext;
use crate::execution::call_info::{CallInfo, Retdata};
use crate::execution::contract_class::ContractClass;
use crate::execution::entry_point::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::abi::abi_utils::{
get_fee_token_var_address, get_storage_var_address, selector_from_name,
};
use crate::abi::constants as abi_constants;
use crate::block_context::BlockContext;
use crate::context::BlockContext;
use crate::execution::contract_class::{ContractClass, ContractClassV1};
use crate::execution::entry_point::EntryPointExecutionContext;
use crate::execution::errors::{EntryPointExecutionError, VirtualMachineExecutionError};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use starknet_api::hash::StarkFelt;
use starknet_api::stark_felt;
use starknet_api::transaction::{Calldata, Fee, TransactionSignature, TransactionVersion};

use crate::block_context::{BlockContext, ChainInfo};
use crate::context::{BlockContext, ChainInfo};
use crate::execution::errors::EntryPointExecutionError;
use crate::execution::execution_utils::{felt_to_stark_felt, stark_felt_to_felt};
use crate::fee::fee_utils::{calculate_tx_fee, calculate_tx_l1_gas_usage, get_fee_by_l1_gas_usage};
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/transaction/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use starknet_api::transaction::{
};
use strum_macros::EnumIter;

use crate::block_context::BlockContext;
use crate::context::BlockContext;
use crate::execution::call_info::CallInfo;
use crate::execution::execution_utils::{felt_to_stark_felt, stark_felt_to_felt};
use crate::fee::fee_utils::calculate_tx_fee;
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/transaction/post_execution_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use starknet_api::transaction::{Calldata, Fee, ResourceBoundsMapping, Transactio
use starknet_api::{patricia_key, stark_felt};
use starknet_crypto::FieldElement;

use crate::block_context::{BlockContext, ChainInfo};
use crate::context::{BlockContext, ChainInfo};
use crate::fee::fee_checks::FeeCheckError;
use crate::fee::fee_utils::calculate_tx_l1_gas_usage;
use crate::invoke_tx_args;
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/transaction/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use starknet_api::{calldata, class_hash, contract_address, patricia_key, stark_f
use strum::IntoEnumIterator;

use crate::abi::abi_utils::{get_fee_token_var_address, get_storage_var_address};
use crate::block_context::{BlockContext, ChainInfo, FeeTokenAddresses};
use crate::context::{BlockContext, ChainInfo, FeeTokenAddresses};
use crate::execution::contract_class::{ContractClass, ContractClassV0};
use crate::state::cached_state::CachedState;
use crate::state::state_api::State;
Expand Down
Loading

0 comments on commit 1603e60

Please sign in to comment.