Skip to content

Commit

Permalink
Fix chain_id is now a storage_var, and class_hashes file
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementWalter committed Nov 14, 2024
1 parent 933f887 commit 00e75f9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/bin/hive_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ fn main() {
let hive_genesis_content = std::fs::read_to_string(hive_genesis_path).expect("Failed to read hive genesis file");
let hive_genesis: HiveGenesisConfig =
serde_json::from_str(&hive_genesis_content).expect("Failed to parse hive genesis json");
let chain_id = hive_genesis.config.chain_id.into();

// Convert the hive genesis to a katana genesis.
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, chain_id).expect("Failed to set up Kakarot");
let manifest = builder.manifest();

// Write the genesis json to the file.
Expand Down
4 changes: 3 additions & 1 deletion src/bin/katana_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ static KAKAROT_CONTRACTS_PATH: LazyLock<PathBuf> =
/// Mock coinbase address.
static COINBASE_ADDRESS: LazyLock<Felt> = LazyLock::new(|| 0x12345u32.into());

static CHAIN_ID: LazyLock<Felt> = LazyLock::new(|| Felt::from_str("0xb615f74ebad2c").expect("Invalid chain ID"));

fn main() {
// Load the env vars.
dotenv().ok();
Expand All @@ -31,7 +33,7 @@ fn main() {
// Read all the classes.
let mut builder = KatanaGenesisBuilder::default()
.load_classes(KAKAROT_CONTRACTS_PATH.clone())
.with_kakarot(*COINBASE_ADDRESS)
.with_kakarot(*COINBASE_ADDRESS, *CHAIN_ID)
.expect("Failed to set up Kakarot");
builder = builder.with_eoa(pk).expect("Failed to set up EOA").fund(pk, U256::from(u128::MAX)).unwrap();
builder = builder.with_dev_allocation(10);
Expand Down
1 change: 1 addition & 0 deletions src/test_utils/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
8 changes: 5 additions & 3 deletions src/test_utils/hive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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<Loaded>) -> Result<GenesisJson, eyre::Error> {
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")?;
Expand Down Expand Up @@ -151,8 +151,10 @@ mod tests {
});
static GENESIS_BUILDER_LOADED: LazyLock<KatanaGenesisBuilder<Loaded>> =
LazyLock::new(|| KatanaGenesisBuilder::default().load_classes(ROOT.join("lib/kakarot/build")));
static GENESIS_BUILDER: LazyLock<KatanaGenesisBuilder<Initialized>> =
LazyLock::new(|| GENESIS_BUILDER_LOADED.clone().with_kakarot(Felt::ZERO).unwrap());
static GENESIS_BUILDER: LazyLock<KatanaGenesisBuilder<Initialized>> = LazyLock::new(|| {
// The chain ID is hardcoded in the hive genesis file src/test_utils/hive/test_data/genesis.json
GENESIS_BUILDER_LOADED.clone().with_kakarot(Felt::ZERO, Felt::from_hex_unchecked("7")).unwrap()
});
static GENESIS: LazyLock<GenesisJson> =
LazyLock::new(|| HIVE_GENESIS.clone().try_into_genesis_json(GENESIS_BUILDER_LOADED.clone()).unwrap());

Expand Down
15 changes: 11 additions & 4 deletions src/test_utils/katana/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};
Expand Down Expand Up @@ -152,6 +152,11 @@ impl KatanaGenesisBuilder<Uninitialized> {
.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)
Expand Down Expand Up @@ -186,13 +191,14 @@ impl KatanaGenesisBuilder<Uninitialized> {
impl KatanaGenesisBuilder<Loaded> {
/// 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<KatanaGenesisBuilder<Initialized>> {
pub fn with_kakarot(mut self, coinbase_address: Felt, chain_id: Felt) -> Result<KatanaGenesisBuilder<Initialized>> {
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(
Expand All @@ -205,8 +211,8 @@ impl KatanaGenesisBuilder<Loaded> {
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.
Expand All @@ -223,6 +229,7 @@ impl KatanaGenesisBuilder<Loaded> {
(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();
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/eth_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down

0 comments on commit 00e75f9

Please sign in to comment.