From 11a7dc48a57b10d1bbd17a219c5db2ddeeddbfe0 Mon Sep 17 00:00:00 2001 From: greged93 <82421016+greged93@users.noreply.github.com> Date: Wed, 14 Feb 2024 16:25:21 +0100 Subject: [PATCH] fix: update genesis (#772) update genesis --- src/bin/hive_genesis.rs | 11 +++++- src/bin/katana_genesis.rs | 18 +-------- src/test_utils/katana/genesis.rs | 64 ++++++++++++++++++++++++-------- 3 files changed, 59 insertions(+), 34 deletions(-) diff --git a/src/bin/hive_genesis.rs b/src/bin/hive_genesis.rs index 9a54e0d78..4b255a3a9 100644 --- a/src/bin/hive_genesis.rs +++ b/src/bin/hive_genesis.rs @@ -1,5 +1,6 @@ use dotenv::dotenv; use kakarot_rpc::test_utils::{hive::HiveGenesisConfig, katana::genesis::KatanaGenesisBuilder}; +use starknet_crypto::FieldElement; use std::{env::var, path::Path}; fn main() { @@ -21,10 +22,18 @@ fn main() { // Convert the hive genesis to a katana genesis. let genesis = - hive_genesis.try_into_genesis_json(builder).expect("Failed to convert hive genesis to katana genesis"); + hive_genesis.try_into_genesis_json(builder.clone()).expect("Failed to convert hive genesis to katana genesis"); + + let builder = builder.with_kakarot(FieldElement::ZERO).expect("Failed to set up Kakarot"); + let manifest = builder.manifest(); // Write the genesis json to the file. let genesis_path = Path::new(&var("GENESIS_OUTPUT").expect("Failed to load GENESIS_OUTPUT var")).to_path_buf(); std::fs::write(genesis_path, serde_json::to_string(&genesis).expect("Failed to serialize genesis json")) .expect("Failed to write genesis json"); + + // Write the manifest to the file. + let manifest_path = Path::new(&var("MANIFEST_OUTPUT").expect("Failed to load MANIFEST_OUTPUT var")).to_path_buf(); + std::fs::write(manifest_path, serde_json::to_string(&manifest).expect("Failed to serialize manifest json")) + .expect("Failed to write manifest json"); } diff --git a/src/bin/katana_genesis.rs b/src/bin/katana_genesis.rs index 82c4f76a1..435ace869 100644 --- a/src/bin/katana_genesis.rs +++ b/src/bin/katana_genesis.rs @@ -3,12 +3,8 @@ use ethers::types::U256; use kakarot_rpc::test_utils::katana::genesis::KatanaGenesisBuilder; use lazy_static::lazy_static; use reth_primitives::B256; -use serde::Serialize; -use serde_with::serde_as; -use starknet::core::serde::unsigned_field_element::UfeHex; use starknet_crypto::FieldElement; use std::{ - collections::HashMap, env::var, path::{Path, PathBuf}, str::FromStr, @@ -22,16 +18,6 @@ lazy_static! { static ref SALT: FieldElement = FieldElement::ZERO; } -#[serde_as] -#[derive(Serialize)] -struct Hex(#[serde_as(as = "UfeHex")] pub FieldElement); - -#[derive(Serialize)] -struct KatanaManifest { - declarations: HashMap, - deployments: HashMap, -} - fn main() { // Load the env vars. dotenv().ok(); @@ -47,9 +33,7 @@ fn main() { .expect("Failed to set up Kakarot"); builder = builder.with_eoa(pk, None).expect("Failed to set up EOA").fund(pk, U256::from(u128::MAX)).unwrap(); - let cache = builder.cache().clone().into_iter().map(|(k, v)| (k, Hex(v))).collect::>(); - let class_hashes = builder.class_hashes().clone().into_iter().map(|(k, v)| (k, Hex(v))).collect::>(); - let manifest = KatanaManifest { declarations: class_hashes, deployments: cache }; + let manifest = builder.manifest(); let genesis = builder.build().expect("Failed to build genesis"); diff --git a/src/test_utils/katana/genesis.rs b/src/test_utils/katana/genesis.rs index 86f3f8a9b..e28cfbaf6 100644 --- a/src/test_utils/katana/genesis.rs +++ b/src/test_utils/katana/genesis.rs @@ -1,6 +1,7 @@ +use std::collections::HashMap; +use std::fs; use std::marker::PhantomData; use std::path::PathBuf; -use std::{collections::HashMap, path::Path}; use cairo_lang_starknet::casm_contract_class::CasmContractClass; use cairo_lang_starknet::contract_class::ContractClass; @@ -19,6 +20,10 @@ use katana_primitives::{ use lazy_static::lazy_static; use rayon::prelude::*; use reth_primitives::{Address, B256}; +use serde::Serialize; +use serde_json::Value; +use serde_with::serde_as; +use starknet::core::serde::unsigned_field_element::UfeHex; use starknet::core::types::contract::legacy::LegacyContractClass; use starknet::core::types::FieldElement; use starknet::core::utils::{get_contract_address, get_storage_var_address, get_udc_deployed_address, UdcUniqueness}; @@ -28,6 +33,16 @@ lazy_static! { static ref SALT: FieldElement = FieldElement::from_bytes_be(&[0u8; 32]).unwrap(); } +#[serde_as] +#[derive(Serialize)] +pub struct Hex(#[serde_as(as = "UfeHex")] pub FieldElement); + +#[derive(Serialize)] +pub struct KatanaManifest { + pub declarations: HashMap, + pub deployments: HashMap, +} + #[derive(Debug, Clone)] pub struct Uninitialized; #[derive(Debug, Clone)] @@ -99,25 +114,35 @@ impl KatanaGenesisBuilder { #[must_use] pub fn load_classes(mut self, path: PathBuf) -> KatanaGenesisBuilder { let entries = WalkDir::new(path).into_iter().filter(|e| e.is_ok() && e.as_ref().unwrap().file_type().is_file()); - self.classes = entries - .map(|entry| GenesisClassJson { - class: PathOrFullArtifact::Path(entry.unwrap().path().to_path_buf()), - class_hash: None, + let classes = entries + .par_bridge() + .map(|entry| { + let path = entry.unwrap().path().to_path_buf(); + let artifact = fs::read_to_string(&path).expect("Failed to read artifact"); + ( + path, + GenesisClassJson { + class: PathOrFullArtifact::Artifact( + serde_json::from_str(&artifact).expect("Failed to parse artifact"), + ), + class_hash: None, + }, + ) }) .collect::>(); - self.class_hashes = self - .classes + self.class_hashes = classes .par_iter() - .filter_map(|class| { - let path = match &class.class { - PathOrFullArtifact::Path(path) => path, - _ => unreachable!("Expected path"), + .filter_map(|(path, class)| { + let artifact = match &class.class { + PathOrFullArtifact::Artifact(artifact) => artifact, + PathOrFullArtifact::Path(_) => unreachable!("Expected artifact"), }; - let class_hash = compute_class_hash(path).ok()?; + let class_hash = compute_class_hash(artifact).ok()?; Some((path.file_stem().unwrap().to_str().unwrap().to_string(), class_hash)) }) .collect::>(); + self.classes = classes.into_iter().map(|(_, class)| class).collect(); self.update_state() } @@ -274,6 +299,13 @@ impl KatanaGenesisBuilder { }) } + /// Returns the manifest of the genesis. + pub fn manifest(&self) -> KatanaManifest { + let cache = self.cache().clone().into_iter().map(|(k, v)| (k, Hex(v))).collect::>(); + let class_hashes = self.class_hashes().clone().into_iter().map(|(k, v)| (k, Hex(v))).collect::>(); + KatanaManifest { declarations: class_hashes, deployments: cache } + } + /// Compute the Starknet address for the given Ethereum address. pub fn compute_starknet_address(&self, evm_address: FieldElement) -> Result { let kakarot_address = self.cache_load("kakarot_address")?; @@ -301,15 +333,15 @@ impl KatanaGenesisBuilder { } } -fn compute_class_hash(class_path: &Path) -> Result { - let class_code = std::fs::read_to_string(class_path).expect("Failed to read class code"); - match serde_json::from_str::(&class_code) { +fn compute_class_hash(class: &Value) -> Result { + match serde_json::from_value::(class.clone()) { Ok(casm) => { let casm = CasmContractClass::from_contract_class(casm, true).expect("Failed to convert class"); Ok(FieldElement::from_bytes_be(&casm.compiled_class_hash().to_be_bytes())?) } Err(_) => { - let casm: LegacyContractClass = serde_json::from_str(&class_code).expect("Failed to parse class code v0"); + let casm: LegacyContractClass = + serde_json::from_value(class.clone()).expect("Failed to parse class code v0"); Ok(casm.class_hash()?) } }