Skip to content

Commit

Permalink
bump blockifier and add more elaborate test
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Oct 23, 2024
1 parent bdd637d commit 040c46b
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 59 deletions.
10 changes: 6 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/katana/cairo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ cairo-lang-starknet = "2.7.0"
cairo-lang-starknet-classes = "2.7.0"
cairo-lang-utils = "2.7.0"
cairo-vm = "1.0.1"
starknet_api = { git = "https://github.com/dojoengine/sequencer", tag = "v0.8.0-rc3.2" }
starknet_api = { git = "https://github.com/dojoengine/sequencer", tag = "v0.8.0-rc3.3" }

[features]
# Some types that we used from cairo-vm implements the `Arbitrary` trait,
Expand Down
11 changes: 10 additions & 1 deletion crates/katana/contracts/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
CONTRACT_CLASS_SUFFIX := .contract_class.json
COMPILED_CLASS_SUFFIX := .compiled_contract_class.json
# Directory where the compiled classes will be stored
BUILD_DIR := ./build

Expand All @@ -11,4 +12,12 @@ $(BUILD_DIR): ./account/src/*
scarb build -p katana_account
mv target/dev/$(ORIGINAL_CLASS_NAME) $(BUILD_DIR)/$(CLASS_NAME)

## ----
## ---- Test Contracts

# I only care about the casm class for this one, hence this
ORIGINAL_CLASS_NAME := katana_test_contracts_GetChainId$(CONTRACT_CLASS_SUFFIX)
CLASS_NAME := test_get_chain_id.json

$(BUILD_DIR): ./test-contracts/src/*
scarb build -p katana_test_contracts
mv target/dev/$(ORIGINAL_CLASS_NAME) $(BUILD_DIR)/$(CLASS_NAME)
7 changes: 7 additions & 0 deletions crates/katana/contracts/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ dependencies = [
name = "katana_messaging"
version = "0.1.0"

[[package]]
name = "katana_test_contracts"
version = "0.1.0"
dependencies = [
"openzeppelin",
]

[[package]]
name = "openzeppelin"
version = "0.17.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/katana/contracts/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = [ "account", "messaging/cairo" ]
members = [ "account", "messaging/cairo", "test-contracts"]

[workspace.package]
version = "0.1.0"
Expand Down
1 change: 1 addition & 0 deletions crates/katana/contracts/build/test_get_chain_id.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"sierra_program":["0x1","0x6","0x0","0x2","0x8","0x2","0x81","0x7f","0x1d","0x52616e6765436865636b","0x800000000000000100000000000000000000000000000000","0x436f6e7374","0x800000000000000000000000000000000000000000000002","0x1","0xb","0x2","0x4f7574206f6620676173","0x4172726179","0x800000000000000300000000000000000000000000000001","0x536e617073686f74","0x800000000000000700000000000000000000000000000001","0x537472756374","0x800000000000000700000000000000000000000000000002","0x0","0x1baeba72e79e9db2587cf44fedb2f3700b2075a5e8e39a562584862c4b71f62","0x3","0x2ee1e2b1b89f8c495f200e4956278a4d47395fe262f27b52e5865c9524c08c3","0x4","0x753332","0x800000000000000700000000000000000000000000000000","0xd","0x7","0x1597b831feeb60c71f259624b79cf66995ea4f7e383403583674ab9c33b9cec","0x8","0x75313238","0x66656c74323532","0x753634","0x800000000000000700000000000000000000000000000004","0x3342418ef16b3e2799b906b1e4e89dbb9b111332dd44f72458ce44f9895b508","0xc","0xa","0x436f6e747261637441646472657373","0x80000000000000070000000000000000000000000000000e","0x348a62b7a38c0673e61e888d83a3ac1bf334ee7361a8514593d3d9532ed8b39","0xe","0x9","0x6","0x426f78","0xf","0x12","0x3808c701a5d13e100ab11b6c02f91f752ecae7e420d21b56c90ec0a475cc7e5","0x800000000000000700000000000000000000000000000006","0x7d4d99e9ed8d285b5c61b493cedb63976bc3d9da867933d829f49ce838b5e7","0x11","0x10","0x13","0x4275696c74696e436f737473","0x53797374656d","0x800000000000000f00000000000000000000000000000001","0x16a4c8d7c05909052238a862d8cc3e7975bf05a07b3a69c6b28951083a6d672","0x800000000000000300000000000000000000000000000003","0x17","0x456e756d","0x9931c641b913035ae674b400b61a51476d506bbe8bba2ff8a6272790aba9e6","0x5","0x18","0x496e70757420746f6f206c6f6e6720666f7220617267756d656e7473","0x4761734275696c74696e","0x29","0x7265766f6b655f61705f747261636b696e67","0x77697468647261775f676173","0x6272616e63685f616c69676e","0x7374727563745f6465636f6e737472756374","0x73746f72655f74656d70","0x61727261795f736e617073686f745f706f705f66726f6e74","0x64726f70","0x1b","0x61727261795f6e6577","0x636f6e73745f61735f696d6d656469617465","0x1a","0x61727261795f617070656e64","0x7374727563745f636f6e737472756374","0x656e756d5f696e6974","0x19","0x1c","0x16","0x6765745f6275696c74696e5f636f737473","0x15","0x77697468647261775f6761735f616c6c","0x6765745f657865637574696f6e5f696e666f5f76325f73797363616c6c","0x14","0x756e626f78","0x736e617073686f745f74616b65","0x65","0xffffffffffffffff","0x57","0x4a","0x41","0x1e","0x1f","0x20","0x21","0x22","0x23","0x24","0x25","0x26","0x27","0x28","0x2a","0x2b","0x2c","0x2d","0x2e","0x2f","0x30","0x31","0x32","0x33","0x34","0x35","0x36","0x37","0x38","0x39","0x3a","0x3b","0x3c","0x3d","0x3e","0x3f","0x40","0x42","0x43","0x44","0x45","0x46","0x470","0x15141305120f0e0d1105100f0e0d07050c0b06050a09080706050403020100","0x2120051f0506051e0f1d0d151c151b151a1905180f0e0d17050c0b16050a09","0x1305260526051305200525050605060506051305200524050605230f220d15","0x5240524052e052d052c0f2b0d24051f051f052a0f1d0d29050c2728050c27","0x40339053805370f343607053505120f340d330f320d023115302f050c2706","0x543060505420f050541130505400f3f0f3e0f3d3c023b06050c273a070605","0x4a390505493505054906050548060505414705054606050545440505431105","0x520505410f510f504f0505410f4e4b0505414d0505414c05054105074b0507","0x50540280505532e05054106050543240505432d0505432f0505402f050553","0x5491305054907050543070505542605054325050543130505432005054328","0x7573813075607050f07050f0f56050f0f0f55050505460f074b05074a3805","0x756071905380f130556051305130f190556051105110f0f56050f070f1726","0x556050f190f0f56052005170f0f56052505260f0f56050f070f06054f2025","0x556050f1f0f24055605161f07060f160556051605200f160556050f250f1f","0x53805280f130556051305130f2d0556052e05240f2e055605242807160f28","0xf56050f070f2d07381313052d0556052d052d0f0705560507052e0f380556","0x7560729381311520f2905560529052f0f290556050f290f0f56050605260f","0x4b39351156070752074f0f2f0556052f05130f0f56050f070f4d4f0758522f","0x5390f000556054b05350f4b0556054b054d0f0f56050f070f4c44471159","0x440f0f56055d05470f0f56055c05470f0f56055a054b0f5e5d5c5b5a385605","0x55a0f600556050f190f5f0556055b05000f5b0556055b054c0f0f56055e05","0xf56056205470f0f56056105440f6c6b6a69686766653c646362611656055f","0x55d0f0f56056605440f0f56053c05440f0f560564055c0f0f560563055b0f","0x56056b055e0f0f56056a055e0f0f560569055c0f0f560568055b0f0f560567","0x56056d055f0f6d055605656007060f650556056505200f0f56056c055c0f0f","0x57105630f710556057005620f700556056f05610f0f56056e05600f6f6e07","0x72052d0f3905560539052e0f350556053505280f2f0556052f05130f720556","0x556054c7307160f730556050f1f0f0f56050f070f7239352f130572055605","0x560544052e0f470556054705280f2f0556052f05130f750556057405240f74","0x640f760556050f190f0f56050f070f7544472f13057505560575052d0f4405","0x160f790556050f1f0f78055605777607060f770556057705200f770556050f","0x4d0556054d05280f4f0556054f05130f7b0556057a05240f7a055605787907","0x55c0f0f56050f070f7b074d4f13057b0556057b052d0f0705560507052e0f","0x7c5907060f7c0556057c05200f7c0556050f640f590556050f190f0f560511","0x5130f800556057f05240f7f0556057d7e07160f7e0556050f1f0f7d055605","0x13058005560580052d0f0705560507052e0f170556051705280f2605560526","0xf1107050f4b4d4c0f13134d4c0f130580071726"],"sierra_program_debug_info":{"type_names":[[0,"RangeCheck"],[1,"Const<felt252, 375233589013918064796019>"],[2,"Array<felt252>"],[3,"Snapshot<Array<felt252>>"],[4,"core::array::Span::<core::felt252>"],[5,"Tuple<core::array::Span::<core::felt252>>"],[6,"u32"],[7,"Array<core::starknet::info::v2::ResourceBounds>"],[8,"Snapshot<Array<core::starknet::info::v2::ResourceBounds>>"],[9,"core::array::Span::<core::starknet::info::v2::ResourceBounds>"],[10,"u128"],[11,"felt252"],[12,"u64"],[13,"core::starknet::info::v2::ResourceBounds"],[14,"ContractAddress"],[15,"core::starknet::info::v2::TxInfo"],[16,"Box<core::starknet::info::v2::TxInfo>"],[17,"Box<core::starknet::info::BlockInfo>"],[18,"core::starknet::info::BlockInfo"],[19,"core::starknet::info::v2::ExecutionInfo"],[20,"Box<core::starknet::info::v2::ExecutionInfo>"],[21,"BuiltinCosts"],[22,"System"],[23,"core::panics::Panic"],[24,"Tuple<core::panics::Panic, Array<felt252>>"],[25,"core::panics::PanicResult::<(core::array::Span::<core::felt252>,)>"],[26,"Const<felt252, 7733229381460288120802334208475838166080759535023995805565484692595>"],[27,"Box<felt252>"],[28,"GasBuiltin"]],"libfunc_names":[[0,"revoke_ap_tracking"],[1,"withdraw_gas"],[2,"branch_align"],[3,"struct_deconstruct<core::array::Span::<core::felt252>>"],[4,"store_temp<RangeCheck>"],[5,"array_snapshot_pop_front<felt252>"],[6,"drop<Snapshot<Array<felt252>>>"],[7,"drop<Box<felt252>>"],[8,"array_new<felt252>"],[9,"const_as_immediate<Const<felt252, 7733229381460288120802334208475838166080759535023995805565484692595>>"],[10,"store_temp<felt252>"],[11,"array_append<felt252>"],[12,"struct_construct<core::panics::Panic>"],[13,"struct_construct<Tuple<core::panics::Panic, Array<felt252>>>"],[14,"enum_init<core::panics::PanicResult::<(core::array::Span::<core::felt252>,)>, 1>"],[15,"store_temp<GasBuiltin>"],[16,"store_temp<System>"],[17,"store_temp<core::panics::PanicResult::<(core::array::Span::<core::felt252>,)>>"],[18,"get_builtin_costs"],[19,"store_temp<BuiltinCosts>"],[20,"withdraw_gas_all"],[21,"get_execution_info_v2_syscall"],[22,"store_temp<Box<core::starknet::info::v2::ExecutionInfo>>"],[23,"unbox<core::starknet::info::v2::ExecutionInfo>"],[24,"struct_deconstruct<core::starknet::info::v2::ExecutionInfo>"],[25,"drop<Box<core::starknet::info::BlockInfo>>"],[26,"drop<ContractAddress>"],[27,"drop<felt252>"],[28,"store_temp<Box<core::starknet::info::v2::TxInfo>>"],[29,"unbox<core::starknet::info::v2::TxInfo>"],[30,"struct_deconstruct<core::starknet::info::v2::TxInfo>"],[31,"drop<u128>"],[32,"drop<core::array::Span::<core::felt252>>"],[33,"drop<core::array::Span::<core::starknet::info::v2::ResourceBounds>>"],[34,"drop<u32>"],[35,"snapshot_take<Array<felt252>>"],[36,"drop<Array<felt252>>"],[37,"struct_construct<core::array::Span::<core::felt252>>"],[38,"struct_construct<Tuple<core::array::Span::<core::felt252>>>"],[39,"enum_init<core::panics::PanicResult::<(core::array::Span::<core::felt252>,)>, 0>"],[40,"const_as_immediate<Const<felt252, 375233589013918064796019>>"]],"user_func_names":[[0,"katana_test_contracts::GetChainId::__wrapper__GetChainId__get"]]},"contract_class_version":"0.1.0","entry_points_by_type":{"EXTERNAL":[{"selector":"0x17c00f03de8b5bd58d2016b59d251c13056b989171c5852949903bc043bc27","function_idx":0}],"L1_HANDLER":[],"CONSTRUCTOR":[]},"abi":[{"type":"impl","name":"GetChainId","interface_name":"katana_test_contracts::IGetChainId"},{"type":"interface","name":"katana_test_contracts::IGetChainId","items":[{"type":"function","name":"get","inputs":[],"outputs":[{"type":"core::felt252"}],"state_mutability":"view"}]},{"type":"event","name":"katana_test_contracts::GetChainId::Event","kind":"enum","variants":[]}]}
13 changes: 13 additions & 0 deletions crates/katana/contracts/test-contracts/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "katana_test_contracts"
version.workspace = true
edition.workspace = true

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[dependencies]
starknet.workspace = true
openzeppelin.workspace = true

[[target.starknet-contract]]
sierra = true
22 changes: 22 additions & 0 deletions crates/katana/contracts/test-contracts/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[starknet::interface]
trait IGetChainId<TContractState> {
fn get(self: @TContractState) -> felt252;
}

#[starknet::contract]
pub mod GetChainId {
use starknet::{SyscallResultTrait, syscalls::get_execution_info_v2_syscall};

#[storage]
struct Storage { }

#[abi(embed_v0)]
pub impl GetChainId of super::IGetChainId<ContractState> {
fn get(self: @ContractState) -> felt252 {
let execution_info = get_execution_info_v2_syscall().unwrap_syscall();
let tx_info = execution_info.tx_info.unbox();
let chain_id = tx_info.chain_id;
chain_id
}
}
}
13 changes: 5 additions & 8 deletions crates/katana/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ katana-primitives.workspace = true
katana-provider.workspace = true

parking_lot = { workspace = true, optional = true }
starknet = { workspace = true, optional = true }
thiserror.workspace = true
tracing.workspace = true

blockifier = { git = "https://github.com/dojoengine/sequencer", tag = "v0.8.0-rc3.2", features = [ "testing" ], optional = true }
blockifier = { git = "https://github.com/dojoengine/sequencer", tag = "v0.8.0-rc3.3", features = [ "testing" ], optional = true }
katana-cairo = { workspace = true, optional = true }

[dev-dependencies]
alloy-primitives.workspace = true
anyhow.workspace = true
cainome.workspace = true
dojo-test-utils.workspace = true
katana-cairo.workspace = true
katana-provider = { workspace = true, features = [ "test-utils" ] }
katana-rpc-types.workspace = true
Expand All @@ -29,6 +30,7 @@ rstest.workspace = true
rstest_reuse.workspace = true
serde_json.workspace = true
similar-asserts.workspace = true
starknet.workspace = true
tokio.workspace = true

criterion.workspace = true
Expand All @@ -37,12 +39,7 @@ pprof = { version = "0.13.0", features = [ "criterion", "flamegraph" ] }
rayon.workspace = true

[features]
blockifier = [
"dep:blockifier",
"dep:katana-cairo",
"dep:parking_lot",
"dep:starknet",
]
blockifier = [ "dep:blockifier", "dep:katana-cairo", "dep:parking_lot" ]
default = [ "blockifier" ]

[[bench]]
Expand Down
44 changes: 1 addition & 43 deletions crates/katana/executor/src/implementation/blockifier/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ use katana_primitives::transaction::{
};
use katana_primitives::{class, event, message, trace, Felt};
use katana_provider::traits::contract::ContractClassProvider;
use starknet::core::utils::parse_cairo_short_string;

use super::state::{CachedState, StateDb};
use crate::abstraction::{EntryPointCall, SimulationFlag};
Expand Down Expand Up @@ -524,10 +523,7 @@ pub fn to_blk_chain_id(chain_id: katana_primitives::chain::ChainId) -> ChainId {
katana_primitives::chain::ChainId::Named(NamedChainId::Mainnet) => ChainId::Mainnet,
katana_primitives::chain::ChainId::Named(NamedChainId::Sepolia) => ChainId::Sepolia,
katana_primitives::chain::ChainId::Named(named) => ChainId::Other(named.to_string()),
katana_primitives::chain::ChainId::Id(id) => {
let id = parse_cairo_short_string(&id).expect("valid cairo string");
ChainId::Other(id)
}
katana_primitives::chain::ChainId::Id(id) => ChainId::Id(id),
}
}

Expand Down Expand Up @@ -695,44 +691,6 @@ mod tests {
assert_eq!(blockifier_id.as_hex(), katana_id.to_string());
}

/// Test to ensure that when Blockifier pass the chain id to the contract ( thru a syscall eg,
/// get_tx_inbox().unbox().chain_id ), the value is exactly the same as Katana chain id.
///
/// Issue: <https://github.com/dojoengine/dojo/issues/1595>
#[test]
fn blockifier_chain_id_invariant() {
let id = felt!("0x1337");

let katana_id = katana_primitives::chain::ChainId::Id(id);
let blockifier_id = to_blk_chain_id(katana_id);

// Mimic how blockifier convert from ChainId to FieldElement.
//
// This is how blockifier pass the chain id to the contract through a syscall.
// https://github.com/dojoengine/blockifier/blob/f2246ce2862d043e4efe2ecf149a4cb7bee689cd/crates/blockifier/src/execution/syscalls/hint_processor.rs#L600-L602
let actual_id = Felt::from_hex(blockifier_id.as_hex().as_str()).unwrap();

assert_eq!(actual_id, id)
}

#[test]
#[ignore = "The blockifier chain id conversion adds bytes for chain ids which don't have a \
valid utf8 representation"]
fn failing_blockifier_chain_id_conversion() {
let id = felt!("0xc72dd9d5e883e");

let katana_id = katana_primitives::chain::ChainId::Id(id);
let blockifier_id = to_blk_chain_id(katana_id);

// Mimic how blockifier convert from ChainId to FieldElement.
//
// This is how blockifier pass the chain id to the contract through a syscall.
// https://github.com/dojoengine/blockifier/blob/f2246ce2862d043e4efe2ecf149a4cb7bee689cd/crates/blockifier/src/execution/syscalls/hint_processor.rs#L600-L602
let actual_id = FieldElement::from_hex(blockifier_id.as_hex().as_str()).unwrap();

assert_eq!(actual_id, id)
}

fn create_blockifier_call_info() -> CallInfo {
let top_events = vec![OrderedEvent {
order: 0,
Expand Down
55 changes: 55 additions & 0 deletions crates/katana/executor/tests/chain_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
mod fixtures;

use std::sync::Arc;

use cainome::rs::abigen;
use dojo_test_utils::sequencer::{get_default_test_config, SequencingConfig, TestSequencer};
use katana_primitives::chain::ChainId;
use katana_primitives::contract::ContractAddress;
use katana_primitives::genesis::allocation::{GenesisAllocation, GenesisContractAlloc};
use katana_primitives::genesis::constant::read_compiled_class_artifact;
use katana_primitives::genesis::GenesisClass;
use katana_primitives::{address, felt};

abigen!(GetChainId, "crates/katana/executor/tests/test-data/get_chain_id.contract.json");

/// Test to ensure that when Blockifier pass the chain id to the contract ( thru a syscall eg,
/// get_tx_inbox().unbox().chain_id ), the value is exactly the same as Katana chain id.
///
/// Issue: <https://github.com/dojoengine/dojo/issues/1595>
#[tokio::test]
#[rstest::rstest]
#[case::mainnet(ChainId::MAINNET)]
#[case::goerli(ChainId::GOERLI)]
#[case::sepolia(ChainId::SEPOLIA)]
#[case::custom_1(ChainId::Id(felt!("0x4b4154414e41")))]
#[case::custom_2(ChainId::Id(felt!("0xc72dd9d5e883e")))]
async fn test_chain_id(#[case] chain_id: ChainId) {
// prepare test contract
let json = include_str!("test-data/get_chain_id.contract.json");
let test_class = read_compiled_class_artifact(json);
let class_hash = felt!("0x222");
let address = address!("0x420");

let mut config = get_default_test_config(SequencingConfig::default());
config.chain.id = chain_id;
// declare class
config.chain.genesis.classes.insert(
class_hash,
GenesisClass { sierra: None, casm: Arc::new(test_class), compiled_class_hash: class_hash },
);

// deploy the contract
let contract = GenesisAllocation::Contract(GenesisContractAlloc {
class_hash: Some(class_hash),
..Default::default()
});
config.chain.genesis.extend_allocations([(address, contract)]);

let sequencer = TestSequencer::start(config).await;
let contract = GetChainIdReader::new(address.into(), sequencer.provider());
let result = contract.get().call().await.unwrap();

// Make sure the chain id felt value is the same as the one we start the node with.
assert_eq!(chain_id.id(), result);
}
3 changes: 2 additions & 1 deletion crates/katana/primitives/src/genesis/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ pub fn get_fee_token_balance_base_storage_address(address: ContractAddress) -> F
get_storage_var_address("ERC20_balances", &[address.into()]).unwrap()
}

fn read_compiled_class_artifact(artifact: &str) -> CompiledClass {
/// Get the compiled class from the sierra contract
pub fn read_compiled_class_artifact(artifact: &str) -> CompiledClass {
let value = serde_json::from_str(artifact).unwrap();
parse_compiled_class(value).unwrap()
}
Expand Down

0 comments on commit 040c46b

Please sign in to comment.