Skip to content

Commit

Permalink
fix: update genesis (#772)
Browse files Browse the repository at this point in the history
update genesis
  • Loading branch information
greged93 authored Feb 14, 2024
1 parent c165e26 commit 11a7dc4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 34 deletions.
11 changes: 10 additions & 1 deletion src/bin/hive_genesis.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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");
}
18 changes: 1 addition & 17 deletions src/bin/katana_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<String, Hex>,
deployments: HashMap<String, Hex>,
}

fn main() {
// Load the env vars.
dotenv().ok();
Expand All @@ -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::<HashMap<_, _>>();
let class_hashes = builder.class_hashes().clone().into_iter().map(|(k, v)| (k, Hex(v))).collect::<HashMap<_, _>>();
let manifest = KatanaManifest { declarations: class_hashes, deployments: cache };
let manifest = builder.manifest();

let genesis = builder.build().expect("Failed to build genesis");

Expand Down
64 changes: 48 additions & 16 deletions src/test_utils/katana/genesis.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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};
Expand All @@ -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<String, Hex>,
pub deployments: HashMap<String, Hex>,
}

#[derive(Debug, Clone)]
pub struct Uninitialized;
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -99,25 +114,35 @@ impl KatanaGenesisBuilder<Uninitialized> {
#[must_use]
pub fn load_classes(mut self, path: PathBuf) -> KatanaGenesisBuilder<Loaded> {
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::<Vec<_>>();

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::<HashMap<_, _>>();
self.classes = classes.into_iter().map(|(_, class)| class).collect();

self.update_state()
}
Expand Down Expand Up @@ -274,6 +299,13 @@ impl KatanaGenesisBuilder<Initialized> {
})
}

/// 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::<HashMap<_, _>>();
let class_hashes = self.class_hashes().clone().into_iter().map(|(k, v)| (k, Hex(v))).collect::<HashMap<_, _>>();
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<ContractAddress> {
let kakarot_address = self.cache_load("kakarot_address")?;
Expand Down Expand Up @@ -301,15 +333,15 @@ impl KatanaGenesisBuilder<Initialized> {
}
}

fn compute_class_hash(class_path: &Path) -> Result<FieldElement> {
let class_code = std::fs::read_to_string(class_path).expect("Failed to read class code");
match serde_json::from_str::<ContractClass>(&class_code) {
fn compute_class_hash(class: &Value) -> Result<FieldElement> {
match serde_json::from_value::<ContractClass>(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()?)
}
}
Expand Down

0 comments on commit 11a7dc4

Please sign in to comment.