diff --git a/Cargo.lock b/Cargo.lock index bc2cbc293..52f7f8c68 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4875,6 +4875,7 @@ name = "kakarot-test-utils" version = "0.1.0" dependencies = [ "bytes", + "ctor", "dojo-test-utils", "dotenv", "ethers", @@ -4897,6 +4898,8 @@ dependencies = [ "starknet-crypto 0.5.1", "starknet_api", "tokio", + "tracing", + "tracing-subscriber", "url", ] diff --git a/Cargo.toml b/Cargo.toml index 75909f6af..559ccb32a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,6 +51,9 @@ url = "2.3.1" tokio = { version = "1.21.2", features = ["macros"] } rstest = "0.18.1" git2 = "0.18.0" +tracing = "0.1.37" +tracing-subscriber = { version = "0.3.3", features = ["env-filter"] } +ctor = "0.2.4" # In order to use dojo-test-utils, we need to explicitly declare the same patches as them in our Cargo.toml # Otherwise, underlying dependencies of dojo will not be patched and we will get a compilation error diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index c36fe912a..f4e272e08 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -48,11 +48,11 @@ walkdir = "2.3.3" [dev-dependencies] cargo-husky = { workspace = true } -ctor = "0.2.4" +ctor = { workspace = true } dojo-test-utils = { workspace = true } starknet-crypto = { workspace = true } toml = "0.7.5" -tracing = "0.1.37" -tracing-subscriber = "0.3.17" +tracing = { workspace = true } +tracing-subscriber = { workspace = true } kakarot-test-utils = { path = "../test-utils" } kakarot-rpc-core = { path = "." } diff --git a/crates/eth-rpc/Cargo.toml b/crates/eth-rpc/Cargo.toml index 9784a4487..1267bb10a 100644 --- a/crates/eth-rpc/Cargo.toml +++ b/crates/eth-rpc/Cargo.toml @@ -37,8 +37,8 @@ starknet = { workspace = true } thiserror = "1.0.38" tower = "0.4.13" tower-http = "0.4.1" -tracing = "0.1.34" -tracing-subscriber = { version = "0.3.3", features = ["env-filter"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true } # for cross-compiling openssl = { version = "0.10", features = ["vendored"] } diff --git a/crates/test-utils/Cargo.toml b/crates/test-utils/Cargo.toml index b8b2681d0..2825c7542 100644 --- a/crates/test-utils/Cargo.toml +++ b/crates/test-utils/Cargo.toml @@ -43,6 +43,11 @@ serde = { workspace = true } serde_json = { workspace = true, features = ["preserve_order"] } serde_with = { workspace = true } +# testing +ctor = { workspace = true } +tracing = { workspace = true } +tracing-subscriber = { workspace = true } + [features] dump = ["git2"] diff --git a/crates/test-utils/src/bin/dump-katana.rs b/crates/test-utils/src/bin/dump-katana.rs index 914aff4b4..65887f6a0 100644 --- a/crates/test-utils/src/bin/dump-katana.rs +++ b/crates/test-utils/src/bin/dump-katana.rs @@ -24,43 +24,40 @@ async fn main() { }) .await; - tokio::task::spawn_blocking(move || { - // Get a serializable state for the sequencer - let sequencer = test_context.sequencer(); - let dump_state = sequencer - .sequencer - .backend - .state - .blocking_write() - .dump_state() - .expect("Failed to call dump_state on Katana state"); + // Get a serializable state for the sequencer + let sequencer = test_context.sequencer(); + let dump_state = sequencer + .sequencer + .backend + .state + .write() + .await + .dump_state() + .expect("Failed to call dump_state on Katana state"); - let state = serde_json::to_string(&dump_state).expect("Failed to serialize state"); + let state = serde_json::to_string(&dump_state).expect("Failed to serialize state"); - // Dump the state - std::fs::create_dir_all(".katana/").expect("Failed to create Kakata dump dir"); - std::fs::write(".katana/dump.json", state).expect("Failed to write dump to .katana/dump.json"); + // Dump the state + std::fs::create_dir_all(".katana/").expect("Failed to create Kakata dump dir"); + std::fs::write(".katana/dump.json", state).expect("Failed to write dump to .katana/dump.json"); - let deployer_account = DeployerAccount { - address: test_context.client().deployer_account().address(), - private_key: *STARKNET_DEPLOYER_ACCOUNT_PRIVATE_KEY, - }; + let deployer_account = DeployerAccount { + address: test_context.client().deployer_account().address(), + private_key: *STARKNET_DEPLOYER_ACCOUNT_PRIVATE_KEY, + }; - // Store contracts information - let mut contracts = HashMap::new(); - contracts.insert("Kakarot", serde_json::to_value(test_context.kakarot()).unwrap()); - contracts.insert("ERC20", serde_json::to_value(test_context.evm_contract("ERC20")).unwrap()); - contracts.insert("Counter", serde_json::to_value(test_context.evm_contract("Counter")).unwrap()); - contracts.insert("PlainOpcodes", serde_json::to_value(test_context.evm_contract("PlainOpcodes")).unwrap()); - contracts.insert("DeployerAccount", serde_json::to_value(deployer_account).unwrap()); + // Store contracts information + let mut contracts = HashMap::new(); + contracts.insert("Kakarot", serde_json::to_value(test_context.kakarot()).unwrap()); + contracts.insert("ERC20", serde_json::to_value(test_context.evm_contract("ERC20")).unwrap()); + contracts.insert("Counter", serde_json::to_value(test_context.evm_contract("Counter")).unwrap()); + contracts.insert("PlainOpcodes", serde_json::to_value(test_context.evm_contract("PlainOpcodes")).unwrap()); + contracts.insert("DeployerAccount", serde_json::to_value(deployer_account).unwrap()); - // Dump the contracts information - let contracts = serde_json::to_string(&contracts).expect("Failed to serialize contract addresses"); - std::fs::write(".katana/contracts.json", contracts) - .expect("Failed to write contracts informations to .katana/contracts.json"); - }) - .await - .expect("Failed to dump state"); + // Dump the contracts information + let contracts = serde_json::to_string(&contracts).expect("Failed to serialize contract addresses"); + std::fs::write(".katana/contracts.json", contracts) + .expect("Failed to write contracts informations to .katana/contracts.json"); // Get the sha of the kakarot submodule let repo = Repository::open(".").unwrap(); diff --git a/crates/test-utils/src/hive_utils/madara/utils.rs b/crates/test-utils/src/hive_utils/madara/utils.rs index 2a4a7d5fe..ad728f676 100644 --- a/crates/test-utils/src/hive_utils/madara/utils.rs +++ b/crates/test-utils/src/hive_utils/madara/utils.rs @@ -157,6 +157,7 @@ mod tests { use std::str::FromStr; use std::sync::Arc; + use ctor::ctor; use kakarot_rpc_core::client::api::KakarotStarknetApi; use kakarot_rpc_core::client::constants::STARKNET_NATIVE_TOKEN; use kakarot_rpc_core::client::helpers::split_u256_into_field_elements; @@ -170,11 +171,19 @@ mod tests { use starknet_api::core::{ClassHash, ContractAddress as StarknetContractAddress, Nonce}; use starknet_api::hash::StarkFelt; use starknet_api::state::StorageKey as StarknetStorageKey; + use tracing_subscriber::{filter, FmtSubscriber}; use super::*; use crate::deploy_helpers::KakarotTestEnvironmentContext; use crate::fixtures::kakarot_test_env_ctx; + #[ctor] + fn setup() { + let filter = filter::EnvFilter::new("info"); + let subscriber = FmtSubscriber::builder().with_env_filter(filter).finish(); + tracing::subscriber::set_global_default(subscriber).expect("setting tracing default failed"); + } + /// This test verifies that the `genesis_set_storage_starknet_contract` function generates the /// correct storage data tuples for a given Starknet address, storage variable name, keys, /// storage value, and storage key offset. @@ -257,37 +266,35 @@ mod tests { // Create an atomic reference to the test environment to avoid dropping it let env = Arc::clone(&test_environment); - // It is not possible to block the async test task, so we need to spawn a blocking task - tokio::task::spawn_blocking(move || { - // Get lock on the Starknet sequencer - let mut starknet = env.sequencer().sequencer.backend.state.blocking_write(); - let mut counter_storage = HashMap::new(); - - // Set the counter bytecode length into the contract - let key = get_starknet_storage_key("bytecode_len_", &[]); - let value = Into::::into(StarkFelt::from(deployed_evm_bytecode_len as u64)); + + // Get lock on the Starknet sequencer + let mut starknet = env.sequencer().sequencer.backend.state.write().await; + let mut counter_storage = HashMap::new(); + + // Set the counter bytecode length into the contract + let key = get_starknet_storage_key("bytecode_len_", &[]); + let value = Into::::into(StarkFelt::from(deployed_evm_bytecode_len as u64)); + counter_storage.insert(key, value); + + // Set the counter bytecode into the contract + counter_genesis_storage.into_iter().for_each(|((_, k), v)| { + let key = StarknetStorageKey(Into::::into(k.0).try_into().unwrap()); + let value = Into::::into(v.0); counter_storage.insert(key, value); + }); - // Set the counter bytecode into the contract - counter_genesis_storage.into_iter().for_each(|((_, k), v)| { - let key = StarknetStorageKey(Into::::into(k.0).try_into().unwrap()); - let value = Into::::into(v.0); - counter_storage.insert(key, value); - }); - - // Deploy the contract account at genesis address - let contract_account_class_hash = env.kakarot().contract_account_class_hash; - let counter_address = - StarknetContractAddress(Into::::into(counter_genesis_address).try_into().unwrap()); - - starknet.set_class_hash_at(counter_address, ClassHash(contract_account_class_hash.into())).unwrap(); - starknet.set_nonce(counter_address, Nonce(StarkFelt::from(1u8))); - for (key, value) in counter_storage.into_iter() { - starknet.set_storage_at(counter_address, key, value); - } - }) - .await - .unwrap(); + // Deploy the contract account at genesis address + let contract_account_class_hash = env.kakarot().contract_account_class_hash; + let counter_address = + StarknetContractAddress(Into::::into(counter_genesis_address).try_into().unwrap()); + + starknet.set_class_hash_at(counter_address, ClassHash(contract_account_class_hash.into())).unwrap(); + starknet.set_nonce(counter_address, Nonce(StarkFelt::from(1u8))); + for (key, value) in counter_storage.into_iter() { + starknet.set_storage_at(counter_address, key, value); + } + // Need to drop the lock on the sequencer to avoid deadlock, so we can then get the bytecode + drop(starknet); // Create a new counter contract pointing to the genesis initialized storage let counter_genesis = ContractAccount::new(counter_genesis_address, &starknet_client); @@ -401,21 +408,21 @@ mod tests { // Create an atomic reference to the test environment to avoid dropping it let env = Arc::clone(&test_environment); - // It is not possible to block the async test task, so we need to spawn a blocking task - tokio::task::spawn_blocking(move || { - // Get lock on the Starknet sequencer - let mut starknet = env.sequencer().sequencer.backend.state.blocking_write(); - let mut storage = HashMap::new(); - - // Prepare the record to be inserted into the storage - genesis_storage_data.into_iter().for_each(|((_, k), v)| { - let storage_key = StarknetStorageKey(Into::::into(k.0).try_into().unwrap()); - let storage_value = Into::::into(v.0); - storage.insert(storage_key, storage_value); - }); - - // Set the storage record for the contract - let contract_account_class_hash = env.kakarot().contract_account_class_hash; + + // Get lock on the Starknet sequencer + let mut starknet = env.sequencer().sequencer.backend.state.write().await; + let mut storage = HashMap::new(); + + // Prepare the record to be inserted into the storage + genesis_storage_data.into_iter().for_each(|((_, k), v)| { + let storage_key = StarknetStorageKey(Into::::into(k.0).try_into().unwrap()); + let storage_value = Into::::into(v.0); + storage.insert(storage_key, storage_value); + }); + + // Set the storage record for the contract + let contract_account_class_hash = env.kakarot().contract_account_class_hash; + { let genesis_address = StarknetContractAddress(Into::::into(genesis_address).try_into().unwrap()); starknet.set_class_hash_at(genesis_address, ClassHash(contract_account_class_hash.into())).unwrap(); @@ -423,9 +430,9 @@ mod tests { for (key, value) in storage.into_iter() { starknet.set_storage_at(genesis_address, key, value); } - }) - .await - .unwrap(); + } + // Need to drop the lock on the sequencer to avoid deadlock, so we can then get the storage + drop(starknet); // Deploy the contract account with the set genesis storage and retrieve the storage on the contract let starknet_client = test_environment.client().starknet_provider();