From 6563509dd45c373ff77792d9604d36317f8a751d Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Fri, 11 Oct 2024 11:57:55 +0200 Subject: [PATCH] make some lazy_static pub const (#770) * make some lazy_static pub const * clean up --- .../ef-testing/src/evm_sequencer/constants.rs | 32 +++++++---------- .../src/evm_sequencer/evm_state/mod.rs | 4 +-- .../src/evm_sequencer/sequencer/mod.rs | 2 +- crates/ef-testing/src/models/case.rs | 2 +- crates/sequencer/src/constants.rs | 17 ++-------- crates/sequencer/src/sequencer.rs | 20 +++++------ crates/sequencer/src/serde.rs | 13 +++---- crates/sequencer/src/state.rs | 34 ++++++++++--------- 8 files changed, 52 insertions(+), 72 deletions(-) diff --git a/crates/ef-testing/src/evm_sequencer/constants.rs b/crates/ef-testing/src/evm_sequencer/constants.rs index e9373953..7458db81 100644 --- a/crates/ef-testing/src/evm_sequencer/constants.rs +++ b/crates/ef-testing/src/evm_sequencer/constants.rs @@ -27,6 +27,11 @@ pub const CHAIN_ID: u64 = 0x1; // Block params pub const BLOCK_GAS_LIMIT: u64 = 20_000_000; +pub const RELAYER_BALANCE: Felt = Felt::from_hex_unchecked("0xd3c21bcecceda1000000"); + +// EVM Addresses +pub const BEACON_ROOT_ADDRESS: Address = address!("000f3df6d732807ef1319fb7b8bb8522d0beac02"); + lazy_static! { // Vm resources: maps resource name to fee cost. pub static ref VM_RESOURCES: HashMap = [ @@ -44,9 +49,6 @@ lazy_static! { .into_iter() .collect(); - // EVM Addresses - pub static ref BEACON_ROOT_ADDRESS: Address = address!("000f3df6d732807ef1319fb7b8bb8522d0beac02"); - // Main addresses pub static ref ETH_FEE_TOKEN_ADDRESS: ContractAddress = contract_address!("0x049D36570D4e46f48e99674bd3fcc84644DdD6b96F7C741B1562B82f9e004dC7"); pub static ref STRK_FEE_TOKEN_ADDRESS: ContractAddress = contract_address!("0xCa14007Eff0dB1f8135f4C25B34De49AB0d42766"); @@ -65,8 +67,6 @@ lazy_static! { pub static ref RELAYER_SIGNING_KEY: SigningKey = SigningKey::from_random(); pub static ref RELAYER_VERIFYING_KEY: VerifyingKey = RELAYER_SIGNING_KEY.verifying_key(); - pub static ref RELAYER_BALANCE: Felt = Felt::from(1_000_000_000_000_000_000_000_000u128); - } #[cfg(feature = "v0")] @@ -147,18 +147,12 @@ pub mod storage_variables { #[cfg(test)] pub mod tests { - use std::str::FromStr; - - use alloy_primitives::{Address, B256}; - use lazy_static::lazy_static; - - lazy_static! { - pub static ref PRIVATE_KEY: B256 = - B256::from_str("0x6ae82d865482a203603ecbf25c865e082396d7705a6bbce92c1ff1d6ab9b503c") - .unwrap(); - pub static ref PUBLIC_KEY: Address = - Address::from_str("0x7513A12F74fFF533ee12F20EE524e4883CBd1945").unwrap(); - pub static ref TEST_CONTRACT_ADDRESS: Address = - Address::left_padding_from(&0xdeadbeefu64.to_be_bytes()); - } + use alloy_primitives::{address, b256, Address, B256}; + + pub const PUBLIC_KEY: Address = address!("7513A12F74fFF533ee12F20EE524e4883CBd1945"); + + pub const PRIVATE_KEY: B256 = + b256!("6ae82d865482a203603ecbf25c865e082396d7705a6bbce92c1ff1d6ab9b503c"); + + pub const TEST_CONTRACT_ADDRESS: Address = address!("00000000000000000000000000000000deadbeef"); } diff --git a/crates/ef-testing/src/evm_sequencer/evm_state/mod.rs b/crates/ef-testing/src/evm_sequencer/evm_state/mod.rs index aa37691a..c48cac8b 100644 --- a/crates/ef-testing/src/evm_sequencer/evm_state/mod.rs +++ b/crates/ef-testing/src/evm_sequencer/evm_state/mod.rs @@ -388,14 +388,14 @@ mod tests { gas_limit: 1_000_000, max_fee_per_gas: 0, max_priority_fee_per_gas: 0, - to: alloy_primitives::TxKind::Call(*TEST_CONTRACT_ADDRESS), + to: alloy_primitives::TxKind::Call(TEST_CONTRACT_ADDRESS), value: U256::ZERO, access_list: AccessList::default(), input: Bytes::default(), }), }; let signature = - sign_message(*PRIVATE_KEY, transaction.transaction.signature_hash()).unwrap(); + sign_message(PRIVATE_KEY, transaction.transaction.signature_hash()).unwrap(); transaction.signature = signature; let eoa_nonce = U256::ZERO; let contract_bytecode = Bytes::from(vec![96, 1, 96, 0, 85]); // PUSH 01 PUSH 00 SSTORE diff --git a/crates/ef-testing/src/evm_sequencer/sequencer/mod.rs b/crates/ef-testing/src/evm_sequencer/sequencer/mod.rs index 34462f4b..58dc573c 100644 --- a/crates/ef-testing/src/evm_sequencer/sequencer/mod.rs +++ b/crates/ef-testing/src/evm_sequencer/sequencer/mod.rs @@ -269,7 +269,7 @@ lazy_static! { ).expect("failed to set openzeppelin account contract class"); (&mut state).set_class_hash_at(*RELAYER_ADDRESS, *OPENZEPPELIN_ACCOUNT_CLASS_HASH).expect("failed to set relayer class hash"); (&mut state).set_storage_at(*RELAYER_ADDRESS, get_storage_var_address(ACCOUNT_PUBLIC_KEY, &[]), RELAYER_VERIFYING_KEY.scalar()).expect("failed to set relayer public key"); - (&mut state).set_storage_at(*ETH_FEE_TOKEN_ADDRESS, get_storage_var_address(ERC20_BALANCES, &[*RELAYER_ADDRESS.0.key()]), *RELAYER_BALANCE).expect("failed to set relayer balance"); + (&mut state).set_storage_at(*ETH_FEE_TOKEN_ADDRESS, get_storage_var_address(ERC20_BALANCES, &[*RELAYER_ADDRESS.0.key()]), RELAYER_BALANCE).expect("failed to set relayer balance"); state }; diff --git a/crates/ef-testing/src/models/case.rs b/crates/ef-testing/src/models/case.rs index 4a4060bc..efc40541 100644 --- a/crates/ef-testing/src/models/case.rs +++ b/crates/ef-testing/src/models/case.rs @@ -159,7 +159,7 @@ impl BlockchainTestCase { //TODO: this should not be a part of the post-state of EF-Tests and can // be removed once we base ourself on the next EF-Tests release, which fixes this issue // Beacon-related features are not supported in Kakarot - if *address == *BEACON_ROOT_ADDRESS { + if *address == BEACON_ROOT_ADDRESS { continue; } diff --git a/crates/sequencer/src/constants.rs b/crates/sequencer/src/constants.rs index dbe59c89..21e82f5b 100644 --- a/crates/sequencer/src/constants.rs +++ b/crates/sequencer/src/constants.rs @@ -2,8 +2,7 @@ pub mod test_constants { use starknet::core::types::Felt; use starknet_api::{ - block::{BlockNumber, BlockTimestamp}, - core::{ClassHash, CompiledClassHash, ContractAddress, Nonce, PatriciaKey}, + core::{ContractAddress, PatriciaKey}, state::StorageKey, }; @@ -11,21 +10,11 @@ pub mod test_constants { pub static ref TEST_CONTRACT: ContractAddress = ContractAddress(*ONE_PATRICIA); pub static ref TEST_ACCOUNT: ContractAddress = ContractAddress(*TWO_PATRICIA); pub static ref TEST_STORAGE_KEY: StorageKey = StorageKey(*ONE_PATRICIA); - pub static ref TEST_NONCE: Nonce = Nonce(*ONE_FELT); - pub static ref SENDER_ADDRESS: Felt = Felt::TWO; pub static ref SEQUENCER_ADDRESS: ContractAddress = Felt::from(1234u16).try_into().unwrap(); pub static ref ETH_FEE_TOKEN_ADDRESS: ContractAddress = Felt::from(12345u16).try_into().unwrap(); pub static ref STRK_FEE_TOKEN_ADDRESS: ContractAddress = Felt::from(123456u32).try_into().unwrap(); - pub static ref ZERO_FELT: Felt = Felt::ZERO; - pub static ref ONE_FELT: Felt = Felt::ONE; - pub static ref TWO_FELT: Felt = Felt::TWO; - pub static ref ONE_PATRICIA: PatriciaKey = (*ONE_FELT).try_into().unwrap(); - pub static ref TWO_PATRICIA: PatriciaKey = (*TWO_FELT).try_into().unwrap(); - pub static ref ONE_CLASS_HASH: ClassHash = ClassHash(*ONE_FELT); - pub static ref TWO_CLASS_HASH: ClassHash = ClassHash(*TWO_FELT); - pub static ref ONE_COMPILED_CLASS_HASH: CompiledClassHash = CompiledClassHash(*ONE_FELT); - pub static ref ONE_BLOCK_NUMBER: BlockNumber = BlockNumber(1); - pub static ref ONE_BLOCK_TIMESTAMP: BlockTimestamp = BlockTimestamp(1); + pub static ref ONE_PATRICIA: PatriciaKey = (Felt::ONE).try_into().unwrap(); + pub static ref TWO_PATRICIA: PatriciaKey = (Felt::TWO).try_into().unwrap(); } } diff --git a/crates/sequencer/src/sequencer.rs b/crates/sequencer/src/sequencer.rs index b56131fb..a3964e21 100644 --- a/crates/sequencer/src/sequencer.rs +++ b/crates/sequencer/src/sequencer.rs @@ -129,6 +129,7 @@ mod tests { use blockifier::versioned_constants::VersionedConstants; use starknet::core::types::Felt; use starknet::macros::selector; + use starknet_api::block::{BlockNumber, BlockTimestamp}; use starknet_api::core::{ChainId, ClassHash, ContractAddress, Nonce}; use starknet_api::executable_transaction::InvokeTransaction; use starknet_api::transaction::{ @@ -138,10 +139,7 @@ mod tests { use crate::constants::test_constants::{ ETH_FEE_TOKEN_ADDRESS, SEQUENCER_ADDRESS, STRK_FEE_TOKEN_ADDRESS, }; - use crate::constants::test_constants::{ - ONE_BLOCK_NUMBER, ONE_BLOCK_TIMESTAMP, ONE_CLASS_HASH, TEST_ACCOUNT, TEST_CONTRACT, - TWO_CLASS_HASH, ZERO_FELT, - }; + use crate::constants::test_constants::{TEST_ACCOUNT, TEST_CONTRACT}; use crate::state::State; use super::*; @@ -217,7 +215,7 @@ mod tests { $cairo_version ), *TEST_CONTRACT, - *ONE_CLASS_HASH, + ClassHash(Felt::ONE), mutable, $cairo_version, ); @@ -227,7 +225,7 @@ mod tests { $cairo_version ), *TEST_ACCOUNT, - *TWO_CLASS_HASH, + ClassHash(Felt::TWO), mutable, $cairo_version, ); @@ -252,8 +250,8 @@ mod tests { fn block_context() -> BlockContext { let block_info = BlockInfo { - block_number: *ONE_BLOCK_NUMBER, - block_timestamp: *ONE_BLOCK_TIMESTAMP, + block_number: BlockNumber(1), + block_timestamp: BlockTimestamp(1), sequencer_address: *SEQUENCER_ADDRESS, gas_prices: GasPrices::new( NonZeroU128::new(1).unwrap(), @@ -292,15 +290,15 @@ mod tests { vec![ *TEST_CONTRACT.0.key(), // destination selector!("inc"), - *ZERO_FELT, // no data + Felt::ZERO, // no data ] .into(), ), max_fee: Fee(1_000_000), signature: TransactionSignature(vec![]), - nonce: Nonce(*ZERO_FELT), + nonce: Nonce(Felt::ZERO), }), - tx_hash: TransactionHash(*ZERO_FELT), + tx_hash: TransactionHash(Felt::ZERO), }, only_query: false, })) diff --git a/crates/sequencer/src/serde.rs b/crates/sequencer/src/serde.rs index 68687714..1539f8e6 100644 --- a/crates/sequencer/src/serde.rs +++ b/crates/sequencer/src/serde.rs @@ -140,10 +140,7 @@ mod tests { }; use crate::{ - constants::test_constants::{ - ONE_CLASS_HASH, ONE_COMPILED_CLASS_HASH, ONE_FELT, TEST_CONTRACT, TEST_NONCE, - TEST_STORAGE_KEY, - }, + constants::test_constants::{TEST_CONTRACT, TEST_STORAGE_KEY}, state::State, }; @@ -152,15 +149,15 @@ mod tests { let mut state = State::default(); // setting up entry for state.classes - let class_hash = *ONE_CLASS_HASH; + let class_hash = ClassHash(Felt::ONE); let contract_class = include_str!("./test_data/cairo_0/compiled_classes/counter.json"); let contract_class: ContractClassV0 = serde_json::from_str(contract_class).expect("failed to deserialize ContractClass from ./crates/sequencer/test_data/cairo_1/compiled_classes/account.json"); let contract_class = ContractClass::V0(contract_class); - let compiled_class_hash = *ONE_COMPILED_CLASS_HASH; + let compiled_class_hash = CompiledClassHash(Felt::ONE); let contract_address = *TEST_CONTRACT; - let storage_value = *ONE_FELT; - let nonce = *TEST_NONCE; + let storage_value = Felt::ONE; + let nonce = Nonce(Felt::ONE); (&mut state) .set_contract_class(class_hash, contract_class) diff --git a/crates/sequencer/src/state.rs b/crates/sequencer/src/state.rs index 4065ae7c..f4581440 100644 --- a/crates/sequencer/src/state.rs +++ b/crates/sequencer/src/state.rs @@ -186,9 +186,7 @@ impl BlockifierStateReader for &mut State { mod tests { use blockifier::execution::contract_class::ContractClassV0; - use crate::constants::test_constants::{ - ONE_CLASS_HASH, ONE_COMPILED_CLASS_HASH, ONE_FELT, ONE_PATRICIA, TEST_CONTRACT, - }; + use crate::constants::test_constants::{ONE_PATRICIA, TEST_CONTRACT}; use super::*; @@ -199,11 +197,11 @@ mod tests { // When state - .set_storage_at(*TEST_CONTRACT, StorageKey(*ONE_PATRICIA), *ONE_FELT) + .set_storage_at(*TEST_CONTRACT, StorageKey(*ONE_PATRICIA), Felt::ONE) .expect("failed to set storage"); // Then - let expected = *ONE_FELT; + let expected = Felt::ONE; let actual = state .get_storage_at(*TEST_CONTRACT, StorageKey(*ONE_PATRICIA)) .unwrap(); @@ -219,7 +217,7 @@ mod tests { state.increment_nonce(*TEST_CONTRACT).unwrap(); // Then - let expected = Nonce(*ONE_FELT); + let expected = Nonce(Felt::ONE); let actual = state.get_nonce_at(*TEST_CONTRACT).unwrap(); assert_eq!(expected, actual); } @@ -231,12 +229,12 @@ mod tests { // When state - .set_class_hash_at(*TEST_CONTRACT, *ONE_CLASS_HASH) + .set_class_hash_at(*TEST_CONTRACT, ClassHash(Felt::ONE)) .unwrap(); // Then - let expected = *ONE_CLASS_HASH; - let actual = state.get_class_hash_at(*TEST_CONTRACT).unwrap(); + let expected = ClassHash(Felt::ONE); + let actual: ClassHash = state.get_class_hash_at(*TEST_CONTRACT).unwrap(); assert_eq!(expected, actual); } @@ -248,14 +246,16 @@ mod tests { // When state .set_contract_class( - *ONE_CLASS_HASH, + ClassHash(Felt::ONE), ContractClass::V0(ContractClassV0::default()), ) .unwrap(); // Then let expected = ContractClass::V0(ContractClassV0::default()); - let actual = state.get_compiled_contract_class(*ONE_CLASS_HASH).unwrap(); + let actual = state + .get_compiled_contract_class(ClassHash(Felt::ONE)) + .unwrap(); assert_eq!(expected, actual); } @@ -266,7 +266,9 @@ mod tests { let state = &mut State::default(); // When - state.get_compiled_contract_class(*ONE_CLASS_HASH).unwrap(); + state + .get_compiled_contract_class(ClassHash(Felt::ONE)) + .unwrap(); } #[test] @@ -276,12 +278,12 @@ mod tests { // When state - .set_compiled_class_hash(*ONE_CLASS_HASH, *ONE_COMPILED_CLASS_HASH) + .set_compiled_class_hash(ClassHash(Felt::ONE), CompiledClassHash(Felt::ONE)) .unwrap(); // Then - let expected = *ONE_COMPILED_CLASS_HASH; - let actual = state.get_compiled_class_hash(*ONE_CLASS_HASH).unwrap(); + let expected = CompiledClassHash(Felt::ONE); + let actual = state.get_compiled_class_hash(ClassHash(Felt::ONE)).unwrap(); assert_eq!(expected, actual); } @@ -292,6 +294,6 @@ mod tests { let state = &mut State::default(); // When - state.get_compiled_class_hash(*ONE_CLASS_HASH).unwrap(); + state.get_compiled_class_hash(ClassHash(Felt::ONE)).unwrap(); } }