From 9da340f63124a3d09b396f116aef0ea85a788c4e Mon Sep 17 00:00:00 2001 From: Yonatan Iluz Date: Wed, 18 Dec 2024 19:15:59 +0200 Subject: [PATCH] chore(blockifier): share get_block_info code --- crates/blockifier/src/context.rs | 25 +++++++++++++- .../src/execution/native/syscall_handler.rs | 33 +++++-------------- .../src/execution/syscalls/hint_processor.rs | 32 ++++++------------ 3 files changed, 43 insertions(+), 47 deletions(-) diff --git a/crates/blockifier/src/context.rs b/crates/blockifier/src/context.rs index 152524f5c5..4159292672 100644 --- a/crates/blockifier/src/context.rs +++ b/crates/blockifier/src/context.rs @@ -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::{ @@ -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)] diff --git a/crates/blockifier/src/execution/native/syscall_handler.rs b/crates/blockifier/src/execution/native/syscall_handler.rs index 5bd0640978..b53bd380de 100644 --- a/crates/blockifier/src/execution/native/syscall_handler.rs +++ b/crates/blockifier/src/execution/native/syscall_handler.rs @@ -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), } } diff --git a/crates/blockifier/src/execution/syscalls/hint_processor.rs b/crates/blockifier/src/execution/syscalls/hint_processor.rs index 6bdfc8f905..4e783ce1d6 100644 --- a/crates/blockifier/src/execution/syscalls/hint_processor.rs +++ b/crates/blockifier/src/execution/syscalls/hint_processor.rs @@ -528,29 +528,17 @@ impl<'a> SyscallHintProcessor<'a> { &mut self, vm: &mut VirtualMachine, ) -> SyscallResult { - 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 = 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)