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

Commit

Permalink
chore: clean pre_process_block (#1969)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware authored Jun 9, 2024
1 parent 58e3bee commit 3442796
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 183 deletions.
21 changes: 4 additions & 17 deletions crates/blockifier/src/blockifier/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ use starknet_api::hash::StarkFelt;
use starknet_api::state::StorageKey;

use crate::abi::constants;
use crate::bouncer::BouncerConfig;
use crate::context::{BlockContext, ChainInfo};
use crate::state::errors::StateError;
use crate::state::state_api::{State, StateResult};
use crate::transaction::objects::FeeType;
use crate::versioned_constants::VersionedConstants;

#[cfg(test)]
#[path = "block_test.rs"]
Expand Down Expand Up @@ -60,14 +57,10 @@ impl GasPrices {
pub fn pre_process_block(
state: &mut dyn State,
old_block_number_and_hash: Option<BlockNumberHashPair>,
block_info: BlockInfo,
chain_info: ChainInfo,
versioned_constants: VersionedConstants,
bouncer_config: BouncerConfig,
concurrency_mode: bool,
) -> StateResult<BlockContext> {
next_block_number: BlockNumber,
) -> StateResult<()> {
let should_block_hash_be_provided =
block_info.block_number >= BlockNumber(constants::STORED_BLOCK_HASH_BUFFER);
next_block_number >= BlockNumber(constants::STORED_BLOCK_HASH_BUFFER);
if let Some(BlockNumberHashPair { number: block_number, hash: block_hash }) =
old_block_number_and_hash
{
Expand All @@ -83,13 +76,7 @@ pub fn pre_process_block(
return Err(StateError::OldBlockHashNotProvided);
}

Ok(BlockContext {
block_info,
chain_info,
versioned_constants,
bouncer_config,
concurrency_mode,
})
Ok(())
}

pub struct BlockNumberHashPair {
Expand Down
50 changes: 10 additions & 40 deletions crates/blockifier/src/blockifier/block_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,41 @@ use starknet_api::hash::StarkFelt;
use starknet_api::state::StorageKey;

use crate::abi::constants;
use crate::blockifier::block::{pre_process_block, BlockInfo, BlockNumberHashPair};
use crate::bouncer::BouncerConfig;
use crate::blockifier::block::{pre_process_block, BlockNumberHashPair};
use crate::context::ChainInfo;
use crate::state::state_api::StateReader;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{CairoVersion, BALANCE};
use crate::versioned_constants::VersionedConstants;

#[test]
fn test_pre_process_block() {
let test_contract = FeatureContract::TestContract(CairoVersion::Cairo1);
let chain_info = &ChainInfo::create_for_testing();
let mut state = test_state(chain_info, BALANCE, &[(test_contract, 1)]);
let mut state = test_state(&ChainInfo::create_for_testing(), BALANCE, &[(test_contract, 1)]);

// Test the positive flow of pre_process_block inside the allowed block number interval
let block_number = constants::STORED_BLOCK_HASH_BUFFER;
let block_number = BlockNumber(constants::STORED_BLOCK_HASH_BUFFER);
let block_hash = StarkFelt::from(20_u8);
let mut block_info = BlockInfo::create_for_testing();
block_info.block_number = BlockNumber(block_number);
pre_process_block(
&mut state,
Some(BlockNumberHashPair::new(block_number, block_hash)),
block_info,
ChainInfo::default(),
VersionedConstants::default(),
BouncerConfig::max(),
false,
Some(BlockNumberHashPair::new(block_number.0, block_hash)),
block_number,
)
.unwrap();

let written_hash = state.get_storage_at(
ContractAddress::from(constants::BLOCK_HASH_CONTRACT_ADDRESS),
StorageKey::from(block_number),
StorageKey::from(block_number.0),
);
assert_eq!(written_hash.unwrap(), block_hash);

// Test that block pre-process with block hash None is successful only within the allowed
// block number interval.
let mut block_info = BlockInfo::create_for_testing();
block_info.block_number = BlockNumber(constants::STORED_BLOCK_HASH_BUFFER - 1);
assert!(
pre_process_block(
&mut state,
None,
block_info,
ChainInfo::default(),
VersionedConstants::default(),
BouncerConfig::max(),
false,
)
.is_ok()
);
let block_number = BlockNumber(constants::STORED_BLOCK_HASH_BUFFER - 1);
assert!(pre_process_block(&mut state, None, block_number).is_ok());

let mut block_info = BlockInfo::create_for_testing();
block_info.block_number = BlockNumber(constants::STORED_BLOCK_HASH_BUFFER);
let error = pre_process_block(
&mut state,
None,
block_info,
ChainInfo::default(),
VersionedConstants::default(),
BouncerConfig::max(),
false,
);
let block_number = BlockNumber(constants::STORED_BLOCK_HASH_BUFFER);
let error = pre_process_block(&mut state, None, block_number);
assert_eq!(
format!(
"A block hash must be provided for block number > {}.",
Expand Down
30 changes: 12 additions & 18 deletions crates/blockifier/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl TransactionContext {

#[derive(Clone, Debug)]
pub struct BlockContext {
// TODO(Yoni, 1/10/2024): consider making these fields public.
pub(crate) block_info: BlockInfo,
pub(crate) chain_info: ChainInfo,
pub(crate) versioned_constants: VersionedConstants,
Expand All @@ -30,20 +31,19 @@ pub struct BlockContext {
}

impl BlockContext {
/// Note: Prefer using the recommended constructor methods as detailed in the struct
/// documentation. This method is intended for internal use and will be deprecated in future
/// versions.
pub fn new_unchecked(
block_info: &BlockInfo,
chain_info: &ChainInfo,
versioned_constants: &VersionedConstants,
pub fn new(
block_info: BlockInfo,
chain_info: ChainInfo,
versioned_constants: VersionedConstants,
bouncer_config: BouncerConfig,
concurrency_mode: bool,
) -> Self {
BlockContext {
block_info: block_info.clone(),
chain_info: chain_info.clone(),
versioned_constants: versioned_constants.clone(),
bouncer_config: BouncerConfig::max(),
concurrency_mode: false,
block_info,
chain_info,
versioned_constants,
bouncer_config,
concurrency_mode,
}
}

Expand All @@ -59,12 +59,6 @@ impl BlockContext {
&self.versioned_constants
}

pub fn concurrency_mode(&self) -> bool {
self.concurrency_mode
}
}

impl BlockContext {
pub fn to_tx_context(
&self,
tx_info_creator: &impl TransactionInfoCreator,
Expand Down
Loading

0 comments on commit 3442796

Please sign in to comment.