diff --git a/src/bin/hive_genesis.rs b/src/bin/hive_genesis.rs index 784430c1a..4af32c7b1 100644 --- a/src/bin/hive_genesis.rs +++ b/src/bin/hive_genesis.rs @@ -37,7 +37,8 @@ fn main() { let genesis_json = hive_genesis.try_into_genesis_json(builder.clone()).expect("Failed to convert hive genesis to katana genesis"); - let builder = builder.with_kakarot(Felt::ZERO).expect("Failed to set up Kakarot"); + let builder = + builder.with_kakarot(Felt::ZERO, hive_genesis.config.chain_id.into()).expect("Failed to set up Kakarot"); let manifest = builder.manifest(); // Write the genesis json to the file. diff --git a/src/test_utils/constants.rs b/src/test_utils/constants.rs index 879ef80ad..a187a7ddd 100644 --- a/src/test_utils/constants.rs +++ b/src/test_utils/constants.rs @@ -19,3 +19,4 @@ pub const KAKAROT_COINBASE: &str = "Kakarot_coinbase"; pub const KAKAROT_BASE_FEE: &str = "Kakarot_base_fee"; pub const KAKAROT_PREV_RANDAO: &str = "Kakarot_prev_randao"; pub const KAKAROT_BLOCK_GAS_LIMIT: &str = "Kakarot_block_gas_limit"; +pub const KAKAROT_CHAIN_ID: &str = "Kakarot_chain_id"; diff --git a/src/test_utils/hive/mod.rs b/src/test_utils/hive/mod.rs index cbc4bfb73..464a3d5a2 100644 --- a/src/test_utils/hive/mod.rs +++ b/src/test_utils/hive/mod.rs @@ -1,6 +1,7 @@ use super::{ constants::{ - ACCOUNT_CAIRO1_HELPERS_CLASS_HASH, ACCOUNT_IMPLEMENTATION, KAKAROT_EVM_TO_STARKNET_ADDRESS, OWNABLE_OWNER, + ACCOUNT_CAIRO1_HELPERS_CLASS_HASH, ACCOUNT_IMPLEMENTATION, KAKAROT_CHAIN_ID, KAKAROT_EVM_TO_STARKNET_ADDRESS, + OWNABLE_OWNER, }, katana::genesis::{KatanaGenesisBuilder, Loaded}, }; @@ -53,7 +54,7 @@ impl HiveGenesisConfig { /// marker type indicates that the Kakarot contract classes need to have been loaded into the builder. pub fn try_into_genesis_json(self, builder: KatanaGenesisBuilder) -> Result { let coinbase_address = Felt::from_bytes_be_slice(self.coinbase.as_slice()); - let builder = builder.with_kakarot(coinbase_address)?; + let builder = builder.with_kakarot(coinbase_address, self.config.chain_id.into())?; // Get the current state of the builder. let kakarot_address = builder.cache_load("kakarot_address")?; @@ -96,6 +97,7 @@ impl HiveGenesisConfig { get_storage_var_address(ACCOUNT_CAIRO1_HELPERS_CLASS_HASH, &[])?, builder.cache_load("cairo1_helpers")?, ), + (get_storage_var_address(KAKAROT_CHAIN_ID, &[])?, self.config.chain_id.into()), ]); let key = get_storage_var_address("ERC20_allowances", &[starknet_address, kakarot_address])?; diff --git a/src/test_utils/katana/genesis.rs b/src/test_utils/katana/genesis.rs index 6dcce4eb9..a1384223d 100644 --- a/src/test_utils/katana/genesis.rs +++ b/src/test_utils/katana/genesis.rs @@ -3,8 +3,8 @@ use crate::{ test_utils::constants::{ ACCOUNT_AUTHORIZED_MESSAGE_HASHES, ACCOUNT_CAIRO1_HELPERS_CLASS_HASH, ACCOUNT_EVM_ADDRESS, ACCOUNT_IMPLEMENTATION, EIP_155_AUTHORIZED_MESSAGE_HASHES, KAKAROT_ACCOUNT_CONTRACT_CLASS_HASH, - KAKAROT_BASE_FEE, KAKAROT_BLOCK_GAS_LIMIT, KAKAROT_CAIRO1_HELPERS_CLASS_HASH, KAKAROT_COINBASE, - KAKAROT_EVM_TO_STARKNET_ADDRESS, KAKAROT_NATIVE_TOKEN_ADDRESS, KAKAROT_PREV_RANDAO, + KAKAROT_BASE_FEE, KAKAROT_BLOCK_GAS_LIMIT, KAKAROT_CAIRO1_HELPERS_CLASS_HASH, KAKAROT_CHAIN_ID, + KAKAROT_COINBASE, KAKAROT_EVM_TO_STARKNET_ADDRESS, KAKAROT_NATIVE_TOKEN_ADDRESS, KAKAROT_PREV_RANDAO, KAKAROT_UNINITIALIZED_ACCOUNT_CLASS_HASH, OWNABLE_OWNER, }, }; @@ -152,6 +152,11 @@ impl KatanaGenesisBuilder { .par_bridge() .filter_map(|entry| { let path = entry.unwrap().path().to_path_buf(); + // Skip class_hashes.json file + if path.file_name().map_or(false, |name| name == "class_hashes.json") { + return None; + } + let artifact = fs::read_to_string(&path).expect("Failed to read artifact"); let artifact = serde_json::from_str(&artifact).expect("Failed to parse artifact"); let class_hash = compute_class_hash(&artifact) @@ -186,13 +191,14 @@ impl KatanaGenesisBuilder { impl KatanaGenesisBuilder { /// Add the Kakarot contract to the genesis. Updates the state to [Initialized]. /// Once in the [Initialized] status, the builder can be built. - pub fn with_kakarot(mut self, coinbase_address: Felt) -> Result> { + pub fn with_kakarot(mut self, coinbase_address: Felt, chain_id: Felt) -> Result> { let kakarot_class_hash = self.kakarot_class_hash()?; let account_contract_class_hash = self.account_contract_class_hash()?; let uninitialized_account_class_hash = self.uninitialized_account_class_hash()?; let cairo1_helpers_class_hash = self.cairo1_helpers_class_hash()?; let block_gas_limit = 20_000_000u64.into(); + // Construct the kakarot contract address. Based on the constructor args from // https://github.com/kkrt-labs/kakarot/blob/main/src/kakarot/kakarot.cairo#L23 let kakarot_address = ContractAddress::new(get_udc_deployed_address( @@ -205,8 +211,8 @@ impl KatanaGenesisBuilder { account_contract_class_hash, uninitialized_account_class_hash, cairo1_helpers_class_hash, - coinbase_address, block_gas_limit, + chain_id, ], )); // Cache the address for later use. @@ -223,6 +229,7 @@ impl KatanaGenesisBuilder { (storage_addr(KAKAROT_BASE_FEE)?, Felt::ZERO), (storage_addr(KAKAROT_PREV_RANDAO)?, Felt::ZERO), (storage_addr(KAKAROT_BLOCK_GAS_LIMIT)?, block_gas_limit), + (storage_addr(KAKAROT_CHAIN_ID)?, chain_id), ] .into_iter() .collect(); diff --git a/tests/tests/eth_provider.rs b/tests/tests/eth_provider.rs index 2a150b656..43ec43185 100644 --- a/tests/tests/eth_provider.rs +++ b/tests/tests/eth_provider.rs @@ -73,7 +73,7 @@ async fn test_chain_id(#[future] katana: Katana, _setup: ()) { let chain_id = eth_provider.chain_id().await.unwrap().unwrap_or_default(); // Then - // Chain ID should correspond to "kaka_test" + // Chain ID should correspond to "kaka_test" % max safe chain id assert_eq!(chain_id, U64::from_str_radix("b615f74ebad2c", 16).unwrap()); }