Skip to content

Commit

Permalink
chore(blockifier): share get_block_info code
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware committed Dec 19, 2024
1 parent 6984361 commit 9da340f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 47 deletions.
25 changes: 24 additions & 1 deletion crates/blockifier/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::BTreeMap;
use papyrus_config::dumping::{append_sub_config_name, ser_param, SerializeConfig};
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use serde::{Deserialize, Serialize};
use starknet_api::block::{BlockInfo, FeeType, GasPriceVector};
use starknet_api::block::{BlockInfo, BlockNumber, BlockTimestamp, FeeType, GasPriceVector};
use starknet_api::core::{ChainId, ContractAddress};
use starknet_api::execution_resources::GasAmount;
use starknet_api::transaction::fields::{
Expand Down Expand Up @@ -127,6 +127,29 @@ impl BlockContext {
tx_info: tx_info_creator.create_tx_info(),
}
}

pub fn block_info_for_validate(&self) -> BlockInfo {
let block_number = self.block_info.block_number.0;
let block_timestamp = self.block_info.block_timestamp.0;
// Round down to the nearest multiple of validate_block_number_rounding.
let validate_block_number_rounding =
self.versioned_constants.get_validate_block_number_rounding();
let rounded_block_number =
(block_number / validate_block_number_rounding) * validate_block_number_rounding;
// Round down to the nearest multiple of validate_timestamp_rounding.
let validate_timestamp_rounding =
self.versioned_constants.get_validate_timestamp_rounding();
let rounded_timestamp =
(block_timestamp / validate_timestamp_rounding) * validate_timestamp_rounding;
BlockInfo {
block_number: BlockNumber(rounded_block_number),
block_timestamp: BlockTimestamp(rounded_timestamp),
sequencer_address: 0_u128.into(),
// TODO(Yoni) consider setting here trivial prices if and when this field is exposed.
gas_prices: self.block_info.gas_prices.clone(),
use_kzg_da: self.block_info.use_kzg_da,
}
}
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
Expand Down
33 changes: 9 additions & 24 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,31 +146,16 @@ impl<'state> NativeSyscallHandler<'state> {
}

fn get_block_info(&self) -> BlockInfo {
let block_info = &self.base.context.tx_context.block_context.block_info;
if self.base.context.execution_mode == ExecutionMode::Validate {
let versioned_constants = self.base.context.versioned_constants();
let block_number = block_info.block_number.0;
let block_timestamp = block_info.block_timestamp.0;
// Round down to the nearest multiple of validate_block_number_rounding.
let validate_block_number_rounding =
versioned_constants.get_validate_block_number_rounding();
let rounded_block_number =
(block_number / validate_block_number_rounding) * validate_block_number_rounding;
// Round down to the nearest multiple of validate_timestamp_rounding.
let validate_timestamp_rounding = versioned_constants.get_validate_timestamp_rounding();
let rounded_timestamp =
(block_timestamp / validate_timestamp_rounding) * validate_timestamp_rounding;
BlockInfo {
block_number: rounded_block_number,
block_timestamp: rounded_timestamp,
sequencer_address: Felt::ZERO,
}
} else {
BlockInfo {
block_number: block_info.block_number.0,
block_timestamp: block_info.block_timestamp.0,
sequencer_address: Felt::from(block_info.sequencer_address),
let block_info = match self.base.context.execution_mode {
ExecutionMode::Execute => self.base.context.tx_context.block_context.block_info(),
ExecutionMode::Validate => {
&self.base.context.tx_context.block_context.block_info_for_validate()
}
};
BlockInfo {
block_number: block_info.block_number.0,
block_timestamp: block_info.block_timestamp.0,
sequencer_address: Felt::from(block_info.sequencer_address),
}
}

Expand Down
32 changes: 10 additions & 22 deletions crates/blockifier/src/execution/syscalls/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,29 +528,17 @@ impl<'a> SyscallHintProcessor<'a> {
&mut self,
vm: &mut VirtualMachine,
) -> SyscallResult<Relocatable> {
let block_info = &self.base.context.tx_context.block_context.block_info;
let block_timestamp = block_info.block_timestamp.0;
let block_number = block_info.block_number.0;
let versioned_constants = self.base.context.versioned_constants();
let block_data: Vec<Felt> = if self.is_validate_mode() {
// Round down to the nearest multiple of validate_block_number_rounding.
let validate_block_number_rounding =
versioned_constants.get_validate_block_number_rounding();
let rounded_block_number =
(block_number / validate_block_number_rounding) * validate_block_number_rounding;
// Round down to the nearest multiple of validate_timestamp_rounding.
let validate_timestamp_rounding = versioned_constants.get_validate_timestamp_rounding();
let rounded_timestamp =
(block_timestamp / validate_timestamp_rounding) * validate_timestamp_rounding;

vec![Felt::from(rounded_block_number), Felt::from(rounded_timestamp), Felt::ZERO]
} else {
vec![
Felt::from(block_number),
Felt::from(block_timestamp),
*block_info.sequencer_address.0.key(),
]
let block_info = match self.base.context.execution_mode {
ExecutionMode::Execute => self.base.context.tx_context.block_context.block_info(),
ExecutionMode::Validate => {
&self.base.context.tx_context.block_context.block_info_for_validate()
}
};
let block_data = vec![
Felt::from(block_info.block_number.0),
Felt::from(block_info.block_timestamp.0),
Felt::from(block_info.sequencer_address),
];
let (block_info_segment_start_ptr, _) = self.allocate_data_segment(vm, &block_data)?;

Ok(block_info_segment_start_ptr)
Expand Down

0 comments on commit 9da340f

Please sign in to comment.