Skip to content

Commit

Permalink
native cache mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Sep 20, 2024
1 parent 9dc2e68 commit 2356a36
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 22 deletions.
63 changes: 56 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ cairo-lang-casm = "2.8.0"
cairo-lang-starknet = "2.8.0"
cairo-lang-utils = "2.8.0"
cairo-lang-starknet-classes = "2.8.0"
cairo-lang-sierra = "2.8.0"
cairo-native = {path = "/Users/msaug/deps/cairo_native"}
cairo-vm = "1.0.1"
blockifier = { package = "blockifier", path = "../../deps/sequencer/crates/blockifier", default-features = false, features = [
"testing",
Expand Down
1 change: 1 addition & 0 deletions crates/ef-testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ zip = { workspace = true, optional = true }
[features]
v0 = []
v1 = []
native = []
ci = []

[build-dependencies]
Expand Down
26 changes: 26 additions & 0 deletions crates/ef-testing/src/evm_sequencer/constants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::fs;

use lazy_static::lazy_static;
use reth_primitives::alloy_primitives::{address, Address};
Expand All @@ -21,6 +22,13 @@ where
serde_json::from_str::<T>(&std::fs::read_to_string(path)?).map_err(eyre::Error::from)
}


pub fn get_raw_contract_class(contract_path: &str) -> String {
println!("path: {:?}", contract_path);
fs::read_to_string(contract_path).unwrap()
}


// Chain params
pub const CHAIN_ID: u64 = 0x4b4b5254;

Expand Down Expand Up @@ -89,11 +97,23 @@ lazy_static! {
pub static ref ACCOUNT_CONTRACT_CLASS: CompiledClass = load_contract_class("../../build/v1/contracts_AccountContract.compiled_contract_class.json").expect("Failed to load ContractAccount contract class");
pub static ref UNINITIALIZED_ACCOUNT_CLASS: CompiledClass = load_contract_class("../../build/v1/contracts_UninitializedAccount.compiled_contract_class.json").expect("Failed to load uninitialized account contract class");

pub static ref KAKAROT_JSON_CLASS: String = get_raw_contract_class("../../build/v1/contracts_KakarotCore.contract_class.json");
pub static ref ACCOUNT_CONTRACT_JSON_CLASS: String = get_raw_contract_class("../../build/v1/contracts_AccountContract.contract_class.json");
pub static ref UNINITIALIZED_ACCOUNT_JSON_CLASS: String = get_raw_contract_class("../../build/v1/contracts_UninitializedAccount.contract_class.json");

// Main class hashes
pub static ref KAKAROT_CLASS_HASH: ClassHash = ClassHash(KAKAROT_CLASS.class_hash().unwrap());
pub static ref ACCOUNT_CONTRACT_CLASS_HASH: ClassHash = ClassHash(ACCOUNT_CONTRACT_CLASS.class_hash().unwrap());
pub static ref UNINITIALIZED_ACCOUNT_CLASS_HASH: ClassHash = ClassHash(UNINITIALIZED_ACCOUNT_CLASS.class_hash().unwrap());
pub static ref PROXY_CLASS_HASH: ClassHash = *UNINITIALIZED_ACCOUNT_CLASS_HASH;

pub static ref CLASS_HASH_TO_JSON_CLASS: HashMap<ClassHash, String> = {
let mut map = HashMap::new();
map.insert(*KAKAROT_CLASS_HASH, KAKAROT_JSON_CLASS.clone());
map.insert(*ACCOUNT_CONTRACT_CLASS_HASH, ACCOUNT_CONTRACT_JSON_CLASS.clone());
map.insert(*UNINITIALIZED_ACCOUNT_CLASS_HASH, UNINITIALIZED_ACCOUNT_JSON_CLASS.clone());
map
};
}

#[cfg(not(any(feature = "v0", feature = "v1")))]
Expand All @@ -114,6 +134,12 @@ lazy_static! {
panic!("Kakarot class hash not defined, use features flag \"v0\" or \"v1\"");
pub static ref UNINITIALIZED_ACCOUNT_CLASS: LegacyContractClass =
panic!("Uninitialized account class not defined, use features flag \"v0\" or \"v1\"");

pub static ref KAKAROT_JSON_CLASS: String = panic!("Kakarot json class not defined, use features flag \"v0\" or \"v1\"");
pub static ref ACCOUNT_CONTRACT_JSON_CLASS: String = panic!("Account contract json class not defined, use features flag \"v0\" or \"v1\"");
pub static ref UNINITIALIZED_ACCOUNT_JSON_CLASS: String = panic!("Uninitialized account json class not defined, use features flag \"v0\" or \"v1\"");

pub static ref CLASS_HASH_TO_JSON_CLASS: HashMap<ClassHash, String> = HashMap::new();
}

pub mod storage_variables {
Expand Down
31 changes: 22 additions & 9 deletions crates/ef-testing/src/evm_sequencer/sequencer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::evm_sequencer::{
ETH_FEE_TOKEN_ADDRESS, FEE_TOKEN_CLASS, FEE_TOKEN_CLASS_HASH, KAKAROT_ADDRESS,
KAKAROT_CLASS, KAKAROT_CLASS_HASH, KAKAROT_OWNER_ADDRESS, OPENZEPPELIN_ACCOUNT_CLASS,
OPENZEPPELIN_ACCOUNT_CLASS_HASH, RELAYER_ADDRESS, RELAYER_BALANCE, RELAYER_VERIFYING_KEY,
STRK_FEE_TOKEN_ADDRESS, UNINITIALIZED_ACCOUNT_CLASS, UNINITIALIZED_ACCOUNT_CLASS_HASH,
STRK_FEE_TOKEN_ADDRESS, UNINITIALIZED_ACCOUNT_CLASS, UNINITIALIZED_ACCOUNT_CLASS_HASH, CLASS_HASH_TO_JSON_CLASS
},
types::contract_class::CasmContractClassWrapper,
utils::compute_starknet_address,
Expand All @@ -29,7 +29,7 @@ use blockifier::{
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use cairo_vm::types::errors::program_errors::ProgramError;
use reth_primitives::Address;
use sequencer::{sequencer::Sequencer, state::State};
use sequencer::{native::class_info_from_json_str, sequencer::Sequencer, state::State};
use starknet::core::types::contract::{legacy::LegacyContractClass, CompiledClass};
use starknet_api::{
block::{BlockNumber, BlockTimestamp},
Expand Down Expand Up @@ -231,30 +231,43 @@ lazy_static! {
convert_contract_class_v0(&UNINITIALIZED_ACCOUNT_CLASS).expect("failed to convert uninitialized class")
)
}

#[cfg(feature = "v1")]
{
#[cfg(feature = "native")]
{
let account_json = CLASS_HASH_TO_JSON_CLASS.get(&ACCOUNT_CONTRACT_CLASS_HASH).unwrap();
let kakarot_json = CLASS_HASH_TO_JSON_CLASS.get(&KAKAROT_CLASS_HASH).unwrap();
let uninitialized_json = CLASS_HASH_TO_JSON_CLASS.get(&UNINITIALIZED_ACCOUNT_CLASS_HASH).unwrap();
println!("Got account's json of length {}", account_json.len());
println!("Got kakarot's json of length {}", kakarot_json.len());
println!("Got uninitialized's json of length {}", uninitialized_json.len());
let account_class_info = class_info_from_json_str(account_json, *ACCOUNT_CONTRACT_CLASS_HASH).unwrap();
let kakarot_class_info = class_info_from_json_str(kakarot_json, *KAKAROT_CLASS_HASH).unwrap();
let uninitialized_class_info = class_info_from_json_str(uninitialized_json, *UNINITIALIZED_ACCOUNT_CLASS_HASH).unwrap();
println!("Got class info");
(account_class_info.contract_class(), kakarot_class_info.contract_class(), uninitialized_class_info.contract_class())
}
#[cfg(not(feature = "native"))]
{
(
convert_contract_class_v1(&KAKAROT_CLASS).expect("failed to convert kakarot class"),
convert_contract_class_v1(&ACCOUNT_CONTRACT_CLASS).expect("failed to convert account class"),
convert_contract_class_v1(&UNINITIALIZED_ACCOUNT_CLASS).expect("failed to convert uninitialized class")
)
}
}
};

let acc_class = ContractClass::V1Native(NativeContractClassV1::from_file("../../../../kkrt-labs/ef-tests/build/v1/contracts_AccountContract.contract_class.json"));
let kakarot_class = ContractClass::V1Native(NativeContractClassV1::from_file("../../../../kkrt-labs/ef-tests/build/v1/contracts_KakarotCore.contract_class.json"));
println!("Using kakarot native contract class");

// Write the kakarot class and class hash.
(&mut state).set_class_hash_at(*KAKAROT_ADDRESS, *KAKAROT_CLASS_HASH).expect("failed to set kakarot class hash");
(&mut state)
.set_contract_class(*KAKAROT_CLASS_HASH, kakarot_class).expect("failed to set kakarot contract class");
.set_contract_class(*KAKAROT_CLASS_HASH, converted_kakarot_class).expect("failed to set kakarot contract class");

// Write contract account, uninitialized_account and erc20 classes and class hashes.
(&mut state).set_contract_class(
*ACCOUNT_CONTRACT_CLASS_HASH,
acc_class,
).expect("failed to set contract account class");
converted_account_class).expect("failed to set contract account class");
(&mut state)
.set_contract_class(*UNINITIALIZED_ACCOUNT_CLASS_HASH, converted_uninitialized_class).expect("failed to set eoa contract class");

Expand Down
7 changes: 7 additions & 0 deletions crates/sequencer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ serde = { workspace = true }
serde_json = { workspace = true }
starknet_api = { workspace = true }
starknet = { workspace = true }
cairo-lang-sierra = { workspace = true }
cairo-native = { workspace = true }
cairo-lang-starknet-classes = { workspace = true }

# Other
eyre = { workspace = true }
tracing = { workspace = true }
thiserror = { workspace = true }
hashbrown = { workspace = true }
cached = "0.53.1"
once_cell = "1.19.0"
libloading = "0.8.5"
lazy_static.workspace = true

[dev-dependencies]
lazy_static = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions crates/sequencer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pub mod sequencer;
pub mod serde;
pub mod state;
pub mod transaction;
pub mod native;
Loading

0 comments on commit 2356a36

Please sign in to comment.