Skip to content

Commit

Permalink
make some lazy_static pub const (#770)
Browse files Browse the repository at this point in the history
* make some lazy_static pub const

* clean up
  • Loading branch information
tcoratger authored Oct 11, 2024
1 parent 5218bc1 commit 6563509
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 72 deletions.
32 changes: 13 additions & 19 deletions crates/ef-testing/src/evm_sequencer/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, f64> = [
Expand All @@ -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");
Expand All @@ -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")]
Expand Down Expand Up @@ -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");
}
4 changes: 2 additions & 2 deletions crates/ef-testing/src/evm_sequencer/evm_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/ef-testing/src/evm_sequencer/sequencer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
2 changes: 1 addition & 1 deletion crates/ef-testing/src/models/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
17 changes: 3 additions & 14 deletions crates/sequencer/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,19 @@
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,
};

lazy_static::lazy_static! {
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();
}
}
20 changes: 9 additions & 11 deletions crates/sequencer/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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::*;
Expand Down Expand Up @@ -217,7 +215,7 @@ mod tests {
$cairo_version
),
*TEST_CONTRACT,
*ONE_CLASS_HASH,
ClassHash(Felt::ONE),
mutable,
$cairo_version,
);
Expand All @@ -227,7 +225,7 @@ mod tests {
$cairo_version
),
*TEST_ACCOUNT,
*TWO_CLASS_HASH,
ClassHash(Felt::TWO),
mutable,
$cairo_version,
);
Expand All @@ -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(),
Expand Down Expand Up @@ -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,
}))
Expand Down
13 changes: 5 additions & 8 deletions crates/sequencer/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -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)
Expand Down
34 changes: 18 additions & 16 deletions crates/sequencer/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand All @@ -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();
Expand All @@ -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);
}
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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]
Expand All @@ -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);
}

Expand All @@ -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();
}
}

0 comments on commit 6563509

Please sign in to comment.