From 51cf7ede8fe241cadaf8930bfb2a17bd3fade0c2 Mon Sep 17 00:00:00 2001 From: Nimrod Weiss Date: Tue, 26 Nov 2024 10:59:09 +0200 Subject: [PATCH] feat(blockifier): add OS constants --- .../resources/versioned_constants_0_13_0.json | 6 ++++- .../resources/versioned_constants_0_13_1.json | 6 ++++- .../versioned_constants_0_13_1_1.json | 6 ++++- .../resources/versioned_constants_0_13_2.json | 6 ++++- .../versioned_constants_0_13_2_1.json | 6 ++++- .../resources/versioned_constants_0_13_3.json | 6 ++++- .../resources/versioned_constants_0_13_4.json | 6 ++++- crates/blockifier/src/abi/constants.rs | 7 ------ crates/blockifier/src/blockifier/block.rs | 5 ++-- .../blockifier/src/blockifier/block_test.rs | 9 ++++--- .../src/blockifier/transaction_executor.rs | 1 + .../src/execution/syscalls/syscall_base.rs | 11 ++++++-- .../syscalls/syscall_tests/get_block_hash.rs | 9 +++++-- crates/blockifier/src/versioned_constants.rs | 25 ++++++++++++++++--- crates/papyrus_execution/src/lib.rs | 7 +++++- 15 files changed, 89 insertions(+), 27 deletions(-) diff --git a/crates/blockifier/resources/versioned_constants_0_13_0.json b/crates/blockifier/resources/versioned_constants_0_13_0.json index 892a6ce418..11bc94b69f 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_0.json +++ b/crates/blockifier/resources/versioned_constants_0_13_0.json @@ -57,7 +57,6 @@ "validate_deploy_entry_point_selector": "0x36fcbf06cd96843058359e1a75928beacfac10727dab22a3972f0af8aa92895", "transfer_entry_point_selector": "0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e", "default_entry_point_selector": 0, - "block_hash_contract_address": 1, "stored_block_hash_buffer": 10, "step_gas_cost": 100, "range_check_gas_cost": 70, @@ -68,6 +67,11 @@ "add_mod_gas_cost": 0, "mul_mod_gas_cost": 0, "memory_hole_gas_cost": 10, + "os_contract_addresses": { + "block_hash_contract_address": 1, + "alias_contract_address": 2, + "reserved_contract_address": 3 + }, "default_initial_gas_cost": { "step_gas_cost": 100000000 }, diff --git a/crates/blockifier/resources/versioned_constants_0_13_1.json b/crates/blockifier/resources/versioned_constants_0_13_1.json index 70bc1de137..9c4b69dc4b 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_1.json +++ b/crates/blockifier/resources/versioned_constants_0_13_1.json @@ -57,7 +57,6 @@ "validate_deploy_entry_point_selector": "0x36fcbf06cd96843058359e1a75928beacfac10727dab22a3972f0af8aa92895", "transfer_entry_point_selector": "0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e", "default_entry_point_selector": 0, - "block_hash_contract_address": 1, "stored_block_hash_buffer": 10, "step_gas_cost": 100, "range_check_gas_cost": 70, @@ -67,6 +66,11 @@ "poseidon_gas_cost": 0, "add_mod_gas_cost": 0, "mul_mod_gas_cost": 0, + "os_contract_addresses": { + "block_hash_contract_address": 1, + "alias_contract_address": 2, + "reserved_contract_address": 3 + }, "memory_hole_gas_cost": 10, "default_initial_gas_cost": { "step_gas_cost": 100000000 diff --git a/crates/blockifier/resources/versioned_constants_0_13_1_1.json b/crates/blockifier/resources/versioned_constants_0_13_1_1.json index 8509c4097e..a3b751ed52 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_1_1.json +++ b/crates/blockifier/resources/versioned_constants_0_13_1_1.json @@ -57,7 +57,6 @@ "validate_deploy_entry_point_selector": "0x36fcbf06cd96843058359e1a75928beacfac10727dab22a3972f0af8aa92895", "transfer_entry_point_selector": "0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e", "default_entry_point_selector": 0, - "block_hash_contract_address": 1, "stored_block_hash_buffer": 10, "step_gas_cost": 100, "range_check_gas_cost": 70, @@ -68,6 +67,11 @@ "add_mod_gas_cost": 0, "mul_mod_gas_cost": 0, "memory_hole_gas_cost": 10, + "os_contract_addresses": { + "block_hash_contract_address": 1, + "alias_contract_address": 2, + "reserved_contract_address": 3 + }, "default_initial_gas_cost": { "step_gas_cost": 100000000 }, diff --git a/crates/blockifier/resources/versioned_constants_0_13_2.json b/crates/blockifier/resources/versioned_constants_0_13_2.json index c6ef3031c9..e38fd65fb5 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_2.json +++ b/crates/blockifier/resources/versioned_constants_0_13_2.json @@ -44,7 +44,6 @@ "max_recursion_depth": 50, "segment_arena_cells": false, "os_constants": { - "block_hash_contract_address": 1, "call_contract_gas_cost": { "entry_point_gas_cost": 1, "step_gas_cost": 10, @@ -113,6 +112,11 @@ }, "memory_hole_gas_cost": 10, "nop_entry_point_offset": -1, + "os_contract_addresses": { + "block_hash_contract_address": 1, + "alias_contract_address": 2, + "reserved_contract_address": 3 + }, "range_check_gas_cost": 70, "pedersen_gas_cost": 0, "bitwise_builtin_gas_cost": 594, diff --git a/crates/blockifier/resources/versioned_constants_0_13_2_1.json b/crates/blockifier/resources/versioned_constants_0_13_2_1.json index 35310a1e0c..d467cecf2a 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_2_1.json +++ b/crates/blockifier/resources/versioned_constants_0_13_2_1.json @@ -44,7 +44,6 @@ "enable_reverts": false, "segment_arena_cells": false, "os_constants": { - "block_hash_contract_address": 1, "call_contract_gas_cost": { "entry_point_gas_cost": 1, "step_gas_cost": 10, @@ -113,6 +112,11 @@ }, "memory_hole_gas_cost": 10, "nop_entry_point_offset": -1, + "os_contract_addresses": { + "block_hash_contract_address": 1, + "alias_contract_address": 2, + "reserved_contract_address": 3 + }, "range_check_gas_cost": 70, "pedersen_gas_cost": 0, "bitwise_builtin_gas_cost": 594, diff --git a/crates/blockifier/resources/versioned_constants_0_13_3.json b/crates/blockifier/resources/versioned_constants_0_13_3.json index 35310a1e0c..d467cecf2a 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_3.json +++ b/crates/blockifier/resources/versioned_constants_0_13_3.json @@ -44,7 +44,6 @@ "enable_reverts": false, "segment_arena_cells": false, "os_constants": { - "block_hash_contract_address": 1, "call_contract_gas_cost": { "entry_point_gas_cost": 1, "step_gas_cost": 10, @@ -113,6 +112,11 @@ }, "memory_hole_gas_cost": 10, "nop_entry_point_offset": -1, + "os_contract_addresses": { + "block_hash_contract_address": 1, + "alias_contract_address": 2, + "reserved_contract_address": 3 + }, "range_check_gas_cost": 70, "pedersen_gas_cost": 0, "bitwise_builtin_gas_cost": 594, diff --git a/crates/blockifier/resources/versioned_constants_0_13_4.json b/crates/blockifier/resources/versioned_constants_0_13_4.json index 1bf7c6fd66..89214ff5b7 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_4.json +++ b/crates/blockifier/resources/versioned_constants_0_13_4.json @@ -44,7 +44,6 @@ "max_recursion_depth": 50, "segment_arena_cells": false, "os_constants": { - "block_hash_contract_address": 1, "call_contract_gas_cost": { "entry_point_gas_cost": 1, "step_gas_cost": 860, @@ -114,6 +113,11 @@ }, "memory_hole_gas_cost": 10, "nop_entry_point_offset": -1, + "os_contract_addresses": { + "block_hash_contract_address": 1, + "alias_contract_address": 2, + "reserved_contract_address": 3 + }, "range_check_gas_cost": 70, "pedersen_gas_cost": 4050, "bitwise_builtin_gas_cost": 583, diff --git a/crates/blockifier/src/abi/constants.rs b/crates/blockifier/src/abi/constants.rs index ecdab0e133..45031271db 100644 --- a/crates/blockifier/src/abi/constants.rs +++ b/crates/blockifier/src/abi/constants.rs @@ -39,12 +39,5 @@ pub const N_MEMORY_HOLES: &str = "n_memory_holes"; pub const CAIRO0_ENTRY_POINT_STRUCT_SIZE: usize = 2; pub const N_STEPS_PER_PEDERSEN: usize = 8; -// OS reserved contract addresses. - -// This contract stores the block number -> block hash mapping. -// TODO(Arni, 14/6/2023): Replace BLOCK_HASH_CONSTANT_ADDRESS with a lazy calculation. -// pub static BLOCK_HASH_CONTRACT_ADDRESS: Lazy = ... -pub const BLOCK_HASH_CONTRACT_ADDRESS: u64 = 1; - // The block number -> block hash mapping is written for the current block number minus this number. pub const STORED_BLOCK_HASH_BUFFER: u64 = 10; diff --git a/crates/blockifier/src/blockifier/block.rs b/crates/blockifier/src/blockifier/block.rs index 83aca48b1c..b227fc7b49 100644 --- a/crates/blockifier/src/blockifier/block.rs +++ b/crates/blockifier/src/blockifier/block.rs @@ -14,7 +14,7 @@ use crate::abi::constants; use crate::state::errors::StateError; use crate::state::state_api::{State, StateResult}; use crate::transaction::objects::FeeType; -use crate::versioned_constants::VersionedConstants; +use crate::versioned_constants::{OsConstants, VersionedConstants}; #[cfg(test)] #[path = "block_test.rs"] @@ -111,12 +111,13 @@ pub fn pre_process_block( state: &mut dyn State, old_block_number_and_hash: Option, next_block_number: BlockNumber, + os_constants: &OsConstants, ) -> StateResult<()> { let should_block_hash_be_provided = next_block_number >= BlockNumber(constants::STORED_BLOCK_HASH_BUFFER); if let Some(BlockHashAndNumber { number, hash }) = old_block_number_and_hash { let block_hash_contract_address = - ContractAddress::from(constants::BLOCK_HASH_CONTRACT_ADDRESS); + ContractAddress::from(os_constants.os_contract_addresses.block_hash_contract_address); let block_number_as_storage_key = StorageKey::from(number.0); state.set_storage_at(block_hash_contract_address, block_number_as_storage_key, hash.0)?; } else if should_block_hash_be_provided { diff --git a/crates/blockifier/src/blockifier/block_test.rs b/crates/blockifier/src/blockifier/block_test.rs index 39dd8e5336..071c4ac9f4 100644 --- a/crates/blockifier/src/blockifier/block_test.rs +++ b/crates/blockifier/src/blockifier/block_test.rs @@ -10,11 +10,13 @@ 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 mut state = test_state(&ChainInfo::create_for_testing(), BALANCE, &[(test_contract, 1)]); + let os_constants = VersionedConstants::create_for_testing().os_constants; // Test the positive flow of pre_process_block inside the allowed block number interval let block_number = BlockNumber(constants::STORED_BLOCK_HASH_BUFFER); @@ -23,11 +25,12 @@ fn test_pre_process_block() { &mut state, Some(BlockHashAndNumber { hash: BlockHash(block_hash), number: block_number }), block_number, + &os_constants, ) .unwrap(); let written_hash = state.get_storage_at( - ContractAddress::from(constants::BLOCK_HASH_CONTRACT_ADDRESS), + ContractAddress::from(os_constants.os_contract_addresses.block_hash_contract_address), StorageKey::from(block_number.0), ); assert_eq!(written_hash.unwrap(), block_hash); @@ -35,10 +38,10 @@ fn test_pre_process_block() { // Test that block pre-process with block hash None is successful only within the allowed // block number interval. let block_number = BlockNumber(constants::STORED_BLOCK_HASH_BUFFER - 1); - assert!(pre_process_block(&mut state, None, block_number).is_ok()); + assert!(pre_process_block(&mut state, None, block_number, &os_constants).is_ok()); let block_number = BlockNumber(constants::STORED_BLOCK_HASH_BUFFER); - let error = pre_process_block(&mut state, None, block_number); + let error = pre_process_block(&mut state, None, block_number, &os_constants); assert_eq!( format!( "A block hash must be provided for block number > {}.", diff --git a/crates/blockifier/src/blockifier/transaction_executor.rs b/crates/blockifier/src/blockifier/transaction_executor.rs index af9d216e1b..b67bdf2f79 100644 --- a/crates/blockifier/src/blockifier/transaction_executor.rs +++ b/crates/blockifier/src/blockifier/transaction_executor.rs @@ -68,6 +68,7 @@ impl TransactionExecutor { &mut block_state, old_block_number_and_hash, block_context.block_info().block_number, + &block_context.versioned_constants.os_constants, )?; Ok(Self::new(block_state, block_context, config)) } diff --git a/crates/blockifier/src/execution/syscalls/syscall_base.rs b/crates/blockifier/src/execution/syscalls/syscall_base.rs index ccdc1ecf0f..e61342072e 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_base.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_base.rs @@ -96,8 +96,15 @@ impl<'state> SyscallHandlerBase<'state> { } let key = StorageKey::try_from(Felt::from(requested_block_number))?; - let block_hash_contract_address = - ContractAddress::try_from(Felt::from(constants::BLOCK_HASH_CONTRACT_ADDRESS))?; + let block_hash_contract_address = ContractAddress::try_from(Felt::from( + self.context + .tx_context + .block_context + .versioned_constants + .os_constants + .os_contract_addresses + .block_hash_contract_address, + ))?; Ok(self.state.get_storage_at(block_hash_contract_address, key)?) } diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/get_block_hash.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/get_block_hash.rs index 83c7af1a63..70e154c53a 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/get_block_hash.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/get_block_hash.rs @@ -22,6 +22,7 @@ use crate::test_utils::{ BALANCE, CURRENT_BLOCK_NUMBER, }; +use crate::versioned_constants::VersionedConstants; use crate::{check_entry_point_execution_error_for_custom_hint, retdata}; fn initialize_state(test_contract: FeatureContract) -> (CachedState, Felt, Felt) { @@ -33,8 +34,12 @@ fn initialize_state(test_contract: FeatureContract) -> (CachedState for OsConstants { fn try_from(raw_json_data: OsConstantsRawJson) -> Result { let gas_costs = GasCosts::try_from(&raw_json_data)?; let validate_rounding_consts = raw_json_data.validate_rounding_consts; - let os_constants = OsConstants { gas_costs, validate_rounding_consts }; + let os_contract_addresses = raw_json_data.os_contract_addresses; + let os_constants = + OsConstants { gas_costs, validate_rounding_consts, os_contract_addresses }; Ok(os_constants) } } +#[derive(Debug, Deserialize)] +pub struct OsContractAddresses { + pub block_hash_contract_address: u8, + pub alias_contract_address: u8, + pub reserved_contract_address: u8, +} +impl Default for OsContractAddresses { + fn default() -> Self { + Self { + block_hash_contract_address: 1, + alias_contract_address: 2, + reserved_contract_address: 3, + } + } +} // Intermediate representation of the JSON file in order to make the deserialization easier, using a // regular try_from. #[derive(Debug, Deserialize)] @@ -713,6 +730,8 @@ struct OsConstantsRawJson { raw_json_file_as_dict: IndexMap, #[serde(default)] validate_rounding_consts: ValidateRoundingConsts, + #[serde(default)] + os_contract_addresses: OsContractAddresses, } impl OsConstantsRawJson { diff --git a/crates/papyrus_execution/src/lib.rs b/crates/papyrus_execution/src/lib.rs index 4e7fada6bd..c5ba8236fc 100644 --- a/crates/papyrus_execution/src/lib.rs +++ b/crates/papyrus_execution/src/lib.rs @@ -399,7 +399,12 @@ fn create_block_context( ); let next_block_number = block_context.block_info().block_number; - pre_process_block(cached_state, ten_blocks_ago, next_block_number)?; + pre_process_block( + cached_state, + ten_blocks_ago, + next_block_number, + &versioned_constants.os_constants, + )?; Ok(block_context) }