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

refactor: move validate_block_number_rounding and validate_timestamp_… #1506

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/blockifier/resources/versioned_constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,9 @@
}
}
},
"validate_block_number_rounding": 100,
"validate_max_n_steps": 1000000,
"validate_timestamp_rounding": 3600,
"vm_resource_fee_cost": {
"bitwise_builtin": 0.16,
"ec_op_builtin": 2.56,
Expand Down
11 changes: 6 additions & 5 deletions crates/blockifier/src/execution/deprecated_syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ use crate::execution::execution_utils::{
execute_deployment, stark_felt_from_ptr, write_maybe_relocatable, write_stark_felt,
ReadOnlySegment,
};
use crate::execution::syscalls::hint_processor::{
VALIDATE_BLOCK_NUMBER_ROUNDING, VALIDATE_TIMESTAMP_ROUNDING,
};

#[cfg(test)]
#[path = "deprecated_syscalls_test.rs"]
Expand Down Expand Up @@ -407,10 +404,12 @@ pub fn get_block_number(
_vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<GetBlockNumberResponse> {
let versioned_constants = syscall_handler.context.versioned_constants();
let block_number = syscall_handler.get_block_info().block_number;
let block_number = match syscall_handler.execution_mode() {
ExecutionMode::Validate => BlockNumber(
(block_number.0 / VALIDATE_BLOCK_NUMBER_ROUNDING) * VALIDATE_BLOCK_NUMBER_ROUNDING,
(block_number.0 / versioned_constants.validate_block_number_rounding)
* versioned_constants.validate_block_number_rounding,
),
ExecutionMode::Execute => block_number,
};
Expand Down Expand Up @@ -438,10 +437,12 @@ pub fn get_block_timestamp(
_vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<GetBlockTimestampResponse> {
let versioned_constants = syscall_handler.context.versioned_constants();
let block_timestamp = syscall_handler.get_block_info().block_timestamp;
let block_timestamp = match syscall_handler.execution_mode() {
ExecutionMode::Validate => BlockTimestamp(
(block_timestamp.0 / VALIDATE_TIMESTAMP_ROUNDING) * VALIDATE_TIMESTAMP_ROUNDING,
(block_timestamp.0 / versioned_constants.validate_timestamp_rounding)
* versioned_constants.validate_timestamp_rounding,
),
ExecutionMode::Execute => block_timestamp,
};
Expand Down
21 changes: 9 additions & 12 deletions crates/blockifier/src/execution/syscalls/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,6 @@ pub const L1_GAS: &str = "0x0000000000000000000000000000000000000000000000000000
// "L2_GAS";
pub const L2_GAS: &str = "0x00000000000000000000000000000000000000000000000000004c325f474153";

// TODO(Tzahi, 1/4/2024): Move to an appropriate constants file.
// Flooring factor for block number in validate mode.
pub const VALIDATE_BLOCK_NUMBER_ROUNDING: u64 = 100;
// Flooring factor for timestamp in validate mode.
pub const VALIDATE_TIMESTAMP_ROUNDING: u64 = 3600;

/// Executes Starknet syscalls (stateful protocol hints) during the execution of an entry point
/// call.
pub struct SyscallHintProcessor<'a> {
Expand Down Expand Up @@ -507,13 +501,16 @@ impl<'a> SyscallHintProcessor<'a> {
let block_info = &self.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.context.versioned_constants();
let block_data: Vec<StarkFelt> = if self.is_validate_mode() {
// Round down to the nearest multiple of 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 rounded_timestamp =
(block_timestamp / VALIDATE_TIMESTAMP_ROUNDING) * VALIDATE_TIMESTAMP_ROUNDING;
// Round down to the nearest multiple of validate_block_number_rounding.
let rounded_block_number = (block_number
/ versioned_constants.validate_block_number_rounding)
* versioned_constants.validate_block_number_rounding;
// Round down to the nearest multiple of validate_timestamp_rounding.
let rounded_timestamp = (block_timestamp
/ versioned_constants.validate_timestamp_rounding)
* versioned_constants.validate_timestamp_rounding;

vec![
StarkFelt::from(rounded_block_number),
Expand Down
4 changes: 4 additions & 0 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ pub struct VersionedConstants {
pub invoke_tx_max_n_steps: u32,
pub l2_resource_gas_costs: L2ResourceGasCosts,
pub max_recursion_depth: usize,
// Flooring factor for block number in validate mode.
pub validate_block_number_rounding: u64,
pub validate_max_n_steps: u32,
// Flooring factor for timestamp in validate mode.
pub validate_timestamp_rounding: u64,

// Cairo OS constants.
// Note: if loaded from a json file, there are some assumptions made on its structure.
Expand Down
Loading