Skip to content

Commit

Permalink
dev: replace blocking_write with write (#552)
Browse files Browse the repository at this point in the history
* dev: replace block_write with write

* dev: add ctor

* dev: remove spawn_blocking and add drop
  • Loading branch information
ftupas authored Sep 19, 2023
1 parent ed8c21b commit 802306c
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 84 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "." }
4 changes: 2 additions & 2 deletions crates/eth-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
5 changes: 5 additions & 0 deletions crates/test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
61 changes: 29 additions & 32 deletions crates/test-utils/src/bin/dump-katana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
101 changes: 54 additions & 47 deletions crates/test-utils/src/hive_utils/madara/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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::<StarkFelt>::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::<StarkFelt>::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::<StarkFelt>::into(k.0).try_into().unwrap());
let value = Into::<StarkFelt>::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::<StarkFelt>::into(k.0).try_into().unwrap());
let value = Into::<StarkFelt>::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::<StarkFelt>::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::<StarkFelt>::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);
Expand Down Expand Up @@ -401,31 +408,31 @@ 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::<StarkFelt>::into(k.0).try_into().unwrap());
let storage_value = Into::<StarkFelt>::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::<StarkFelt>::into(k.0).try_into().unwrap());
let storage_value = Into::<StarkFelt>::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::<StarkFelt>::into(genesis_address).try_into().unwrap());

starknet.set_class_hash_at(genesis_address, ClassHash(contract_account_class_hash.into())).unwrap();
starknet.set_nonce(genesis_address, Nonce(StarkFelt::from(1u8)));
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();
Expand Down

0 comments on commit 802306c

Please sign in to comment.