diff --git a/Cargo.lock b/Cargo.lock index 0293793f4bab2..077c52f2aa629 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4458,7 +4458,7 @@ dependencies = [ [[package]] name = "msim" version = "0.1.0" -source = "git+https://github.com/MystenLabs/mysten-sim.git?rev=e0355e2836fd3a6df7952b6aaca2c178d6055197#e0355e2836fd3a6df7952b6aaca2c178d6055197" +source = "git+https://github.com/MystenLabs/mysten-sim.git?rev=dad61af1709543ec31529a78c489bc4b9c973375#dad61af1709543ec31529a78c489bc4b9c973375" dependencies = [ "ahash", "async-task", @@ -4485,7 +4485,7 @@ dependencies = [ [[package]] name = "msim-macros" version = "0.1.0" -source = "git+https://github.com/MystenLabs/mysten-sim.git?rev=e0355e2836fd3a6df7952b6aaca2c178d6055197#e0355e2836fd3a6df7952b6aaca2c178d6055197" +source = "git+https://github.com/MystenLabs/mysten-sim.git?rev=dad61af1709543ec31529a78c489bc4b9c973375#dad61af1709543ec31529a78c489bc4b9c973375" dependencies = [ "darling 0.14.1", "proc-macro2 1.0.43", @@ -7386,8 +7386,10 @@ dependencies = [ "sui-config", "sui-core", "sui-json-rpc-types", + "sui-macros", "sui-node", "sui-sdk", + "sui-simulator", "sui-types", "telemetry-subscribers", "tempfile", @@ -7869,6 +7871,9 @@ name = "sui-simulator" version = "0.7.0" dependencies = [ "msim", + "sui-framework", + "telemetry-subscribers", + "tracing", "workspace-hack 0.1.0", ] diff --git a/crates/sui-benchmark/Cargo.toml b/crates/sui-benchmark/Cargo.toml index fc53a6c03d6cb..48e3ca3820ef2 100644 --- a/crates/sui-benchmark/Cargo.toml +++ b/crates/sui-benchmark/Cargo.toml @@ -48,5 +48,9 @@ narwhal-node = { git = "https://github.com/MystenLabs/narwhal", rev = "9d667b470 workspace-hack = { path = "../workspace-hack"} test-utils = { path = "../test-utils" } +[target.'cfg(msim)'.dependencies] +sui-macros = { path = "../sui-macros" } +sui-simulator = { path = "../sui-simulator" } + [features] benchmark = ["narwhal-node/benchmark"] diff --git a/crates/sui-benchmark/src/bin/stress.rs b/crates/sui-benchmark/src/bin/stress.rs index b1728802c0d95..f7fe694cc8132 100644 --- a/crates/sui-benchmark/src/bin/stress.rs +++ b/crates/sui-benchmark/src/bin/stress.rs @@ -7,7 +7,6 @@ use futures::future::try_join_all; use futures::StreamExt; use prometheus::Registry; use rand::seq::SliceRandom; -use std::collections::HashMap; use std::path::PathBuf; use std::sync::Arc; use std::time::Duration; @@ -17,14 +16,11 @@ use sui_benchmark::drivers::driver::Driver; use sui_benchmark::drivers::BenchmarkCmp; use sui_benchmark::drivers::BenchmarkStats; use sui_benchmark::drivers::Interval; -use sui_benchmark::workloads::shared_counter::SharedCounterWorkload; -use sui_benchmark::workloads::transfer_object::TransferObjectWorkload; +use sui_benchmark::util::get_ed25519_keypair_from_keystore; use sui_benchmark::workloads::workload::get_latest; -use sui_benchmark::workloads::workload::CombinationWorkload; -use sui_benchmark::workloads::workload::Payload; -use sui_benchmark::workloads::workload::Workload; -use sui_benchmark::workloads::workload::WorkloadInfo; -use sui_benchmark::workloads::workload::WorkloadType; +use sui_benchmark::workloads::{ + make_combination_workload, make_shared_counter_workload, make_transfer_object_workload, +}; use sui_config::gateway::GatewayConfig; use sui_config::Config; use sui_config::PersistedConfig; @@ -37,13 +33,10 @@ use sui_core::gateway_state::GatewayState; use sui_core::safe_client::SafeClientMetrics; use sui_node::metrics; use sui_node::SuiNode; -use sui_sdk::crypto::FileBasedKeystore; use sui_types::base_types::ObjectID; use sui_types::base_types::SuiAddress; use sui_types::batch::UpdateItem; use sui_types::crypto::AccountKeyPair; -use sui_types::crypto::EncodeDecodeBase64; -use sui_types::crypto::SuiKeyPair; use sui_types::messages::BatchInfoRequest; use sui_types::messages::BatchInfoResponseItem; use sui_types::messages::TransactionInfoRequest; @@ -225,103 +218,6 @@ pub async fn follow(authority_client: NetworkAuthorityClient, download_txes: boo }); } -fn make_combination_workload( - target_qps: u64, - num_workers: u64, - in_flight_ratio: u64, - primary_gas_id: ObjectID, - primary_gas_account_owner: SuiAddress, - primary_gas_account_keypair: Arc, - opts: &Opts, -) -> WorkloadInfo { - let mut workloads = HashMap::>)>::new(); - match opts.run_spec { - RunSpec::Bench { - shared_counter, - transfer_object, - .. - } => { - if shared_counter > 0 { - let workload = SharedCounterWorkload::new_boxed( - primary_gas_id, - primary_gas_account_owner, - primary_gas_account_keypair.clone(), - None, - ); - workloads - .entry(WorkloadType::SharedCounter) - .or_insert((shared_counter, workload)); - } - if transfer_object > 0 { - let workload = TransferObjectWorkload::new_boxed( - opts.num_transfer_accounts, - primary_gas_id, - primary_gas_account_owner, - primary_gas_account_keypair, - ); - workloads - .entry(WorkloadType::TransferObject) - .or_insert((transfer_object, workload)); - } - } - } - let workload = CombinationWorkload::new_boxed(workloads); - WorkloadInfo { - target_qps, - num_workers, - max_in_flight_ops: in_flight_ratio * target_qps, - workload, - } -} - -fn make_shared_counter_workload( - target_qps: u64, - num_workers: u64, - max_in_flight_ops: u64, - primary_gas_id: ObjectID, - owner: SuiAddress, - keypair: Arc, -) -> Option { - if target_qps == 0 || max_in_flight_ops == 0 || num_workers == 0 { - None - } else { - let workload = SharedCounterWorkload::new_boxed(primary_gas_id, owner, keypair, None); - Some(WorkloadInfo { - target_qps, - num_workers, - max_in_flight_ops, - workload, - }) - } -} - -fn make_transfer_object_workload( - target_qps: u64, - num_workers: u64, - max_in_flight_ops: u64, - num_transfer_accounts: u64, - primary_gas_id: &ObjectID, - owner: SuiAddress, - keypair: Arc, -) -> Option { - if target_qps == 0 || max_in_flight_ops == 0 || num_workers == 0 { - None - } else { - let workload = TransferObjectWorkload::new_boxed( - num_transfer_accounts, - *primary_gas_id, - owner, - keypair, - ); - Some(WorkloadInfo { - target_qps, - num_workers, - max_in_flight_ops, - workload, - }) - } -} - /// To spin up a local cluster and direct some load /// at it with 50/50 shared and owned traffic, use /// it something like: @@ -478,22 +374,8 @@ async fn main() -> Result<()> { &opts.keystore_path )) })?; - let keystore = FileBasedKeystore::load_or_create(&keystore_path)?; - let keypair = keystore - .key_pairs() - .iter() - .find(|x| { - let address: SuiAddress = Into::::into(&x.public()); - address == primary_gas_account - }) - .map(|x| x.encode_base64()) - .unwrap(); - // TODO(joyqvq): This is a hack to decode base64 keypair with added flag, ok for now since it is for benchmark use. - // Rework to get the typed keypair directly from above. - let ed25519_keypair = match SuiKeyPair::decode_base64(&keypair).unwrap() { - SuiKeyPair::Ed25519SuiKeyPair(x) => x, - _ => panic!("Unexpected keypair type"), - }; + let ed25519_keypair = + get_ed25519_keypair_from_keystore(keystore_path, &primary_gas_account)?; ( *primary_gas_id, primary_gas_account, @@ -550,7 +432,9 @@ async fn main() -> Result<()> { primary_gas_id, owner, keypair, - &opts, + opts.num_transfer_accounts, + shared_counter, + transfer_object, ); combination_workload.workload.init(&aggregator).await; vec![combination_workload] diff --git a/crates/sui-benchmark/src/drivers/bench_driver.rs b/crates/sui-benchmark/src/drivers/bench_driver.rs index 4cc44875db336..ef5dd6281e8d2 100644 --- a/crates/sui-benchmark/src/drivers/bench_driver.rs +++ b/crates/sui-benchmark/src/drivers/bench_driver.rs @@ -208,6 +208,17 @@ impl BenchDriver { } } +#[cfg(not(msim))] +async fn ctrl_c() -> std::io::Result<()> { + tokio::signal::ctrl_c().await +} + +// TODO: if more use is made of tokio::signal we should just add support for it to the sim. +#[cfg(msim)] +async fn ctrl_c() -> std::io::Result<()> { + futures::future::pending().await +} + #[async_trait] impl Driver for BenchDriver { async fn run( @@ -524,7 +535,7 @@ impl Driver for BenchDriver { drop(tx); let all_tasks = try_join_all(tasks); let _res = tokio::select! { - _ = tokio::signal::ctrl_c() => { + _ = ctrl_c() => { self.terminate(); vec![] } diff --git a/crates/sui-benchmark/src/lib.rs b/crates/sui-benchmark/src/lib.rs index a5f11a282199f..65fb94a52a27a 100644 --- a/crates/sui-benchmark/src/lib.rs +++ b/crates/sui-benchmark/src/lib.rs @@ -2,4 +2,5 @@ // SPDX-License-Identifier: Apache-2.0 pub mod drivers; +pub mod util; pub mod workloads; diff --git a/crates/sui-benchmark/src/util.rs b/crates/sui-benchmark/src/util.rs new file mode 100644 index 0000000000000..5ca79a74519bc --- /dev/null +++ b/crates/sui-benchmark/src/util.rs @@ -0,0 +1,33 @@ +// Copyright (c) 2022, Mysten Labs, Inc. +// SPDX-License-Identifier: Apache-2.0 + +use anyhow::Result; +use sui_sdk::crypto::FileBasedKeystore; +use sui_types::{ + base_types::SuiAddress, + crypto::{AccountKeyPair, EncodeDecodeBase64, SuiKeyPair}, +}; + +use std::path::PathBuf; + +pub fn get_ed25519_keypair_from_keystore( + keystore_path: PathBuf, + requested_address: &SuiAddress, +) -> Result { + let keystore = FileBasedKeystore::load_or_create(&keystore_path)?; + let keypair = keystore + .key_pairs() + .iter() + .find(|x| { + let address: SuiAddress = Into::::into(&x.public()); + address == *requested_address + }) + .map(|x| x.encode_base64()) + .unwrap(); + // TODO(joyqvq): This is a hack to decode base64 keypair with added flag, ok for now since it is for benchmark use. + // Rework to get the typed keypair directly from above. + Ok(match SuiKeyPair::decode_base64(&keypair).unwrap() { + SuiKeyPair::Ed25519SuiKeyPair(x) => x, + _ => panic!("Unexpected keypair type"), + }) +} diff --git a/crates/sui-benchmark/src/workloads/mod.rs b/crates/sui-benchmark/src/workloads/mod.rs index 76e34179fa89f..f5db8229cb754 100644 --- a/crates/sui-benchmark/src/workloads/mod.rs +++ b/crates/sui-benchmark/src/workloads/mod.rs @@ -4,3 +4,106 @@ pub mod shared_counter; pub mod transfer_object; pub mod workload; + +use std::collections::HashMap; +use std::sync::Arc; + +use shared_counter::SharedCounterWorkload; +use transfer_object::TransferObjectWorkload; +use workload::*; + +use sui_types::{ + base_types::{ObjectID, SuiAddress}, + crypto::AccountKeyPair, +}; + +pub fn make_combination_workload( + target_qps: u64, + num_workers: u64, + in_flight_ratio: u64, + primary_gas_id: ObjectID, + primary_gas_account_owner: SuiAddress, + primary_gas_account_keypair: Arc, + num_transfer_accounts: u64, + shared_counter_weight: u32, + transfer_object_weight: u32, +) -> WorkloadInfo { + let mut workloads = HashMap::>)>::new(); + if shared_counter_weight > 0 { + let workload = SharedCounterWorkload::new_boxed( + primary_gas_id, + primary_gas_account_owner, + primary_gas_account_keypair.clone(), + None, + ); + workloads + .entry(WorkloadType::SharedCounter) + .or_insert((shared_counter_weight, workload)); + } + if transfer_object_weight > 0 { + let workload = TransferObjectWorkload::new_boxed( + num_transfer_accounts, + primary_gas_id, + primary_gas_account_owner, + primary_gas_account_keypair, + ); + workloads + .entry(WorkloadType::TransferObject) + .or_insert((transfer_object_weight, workload)); + } + let workload = CombinationWorkload::new_boxed(workloads); + WorkloadInfo { + target_qps, + num_workers, + max_in_flight_ops: in_flight_ratio * target_qps, + workload, + } +} + +pub fn make_shared_counter_workload( + target_qps: u64, + num_workers: u64, + max_in_flight_ops: u64, + primary_gas_id: ObjectID, + owner: SuiAddress, + keypair: Arc, +) -> Option { + if target_qps == 0 || max_in_flight_ops == 0 || num_workers == 0 { + None + } else { + let workload = SharedCounterWorkload::new_boxed(primary_gas_id, owner, keypair, None); + Some(WorkloadInfo { + target_qps, + num_workers, + max_in_flight_ops, + workload, + }) + } +} + +pub fn make_transfer_object_workload( + target_qps: u64, + num_workers: u64, + max_in_flight_ops: u64, + num_transfer_accounts: u64, + primary_gas_id: &ObjectID, + owner: SuiAddress, + keypair: Arc, +) -> Option { + if target_qps == 0 || max_in_flight_ops == 0 || num_workers == 0 { + None + } else { + let workload = TransferObjectWorkload::new_boxed( + num_transfer_accounts, + *primary_gas_id, + owner, + keypair, + ); + Some(WorkloadInfo { + target_qps, + num_workers, + max_in_flight_ops, + workload, + }) + } +} diff --git a/crates/sui-benchmark/src/workloads/workload.rs b/crates/sui-benchmark/src/workloads/workload.rs index 8c45602e21004..e34c20be7f353 100644 --- a/crates/sui-benchmark/src/workloads/workload.rs +++ b/crates/sui-benchmark/src/workloads/workload.rs @@ -24,7 +24,7 @@ use sui_types::{ }, }; use test_utils::messages::make_transfer_sui_transaction; -use tracing::log::error; +use tracing::error; use rand::{prelude::*, rngs::OsRng}; use rand_distr::WeightedAliasIndex; diff --git a/crates/sui-benchmark/tests/simtest.rs b/crates/sui-benchmark/tests/simtest.rs new file mode 100644 index 0000000000000..bebaa68a687dc --- /dev/null +++ b/crates/sui-benchmark/tests/simtest.rs @@ -0,0 +1,101 @@ +// Copyright (c) 2022, Mysten Labs, Inc. +// SPDX-License-Identifier: Apache-2.0 + +#[cfg(msim)] +mod test { + + use std::str::FromStr; + use std::sync::Arc; + use std::time::Duration; + use sui_config::SUI_KEYSTORE_FILENAME; + use sui_core::test_utils::test_authority_aggregator; + use test_utils::{ + messages::get_gas_object_with_wallet_context, network::setup_network_and_wallet, + }; + + use sui_benchmark::{ + drivers::{bench_driver::BenchDriver, driver::Driver, Interval}, + util::get_ed25519_keypair_from_keystore, + workloads::make_combination_workload, + }; + + use sui_macros::sim_test; + use sui_simulator::{configs::*, SimConfig}; + + fn test_config() -> SimConfig { + env_config( + uniform_latency_ms(10..20), + [ + ( + "regional_high_variance", + bimodal_latency_ms(30..40, 300..800, 0.005), + ), + ( + "global_high_variance", + bimodal_latency_ms(60..80, 500..1500, 0.01), + ), + ], + ) + } + + fn get_var(name: &str, default: T) -> T + where + ::Err: std::fmt::Debug, + { + std::env::var(name) + .ok() + .map(|v| v.parse().unwrap()) + .unwrap_or(default) + } + + #[sim_test(config = "test_config()")] + async fn test_simulated_load() { + let (swarm, context, sender) = setup_network_and_wallet().await.unwrap(); + let keystore_path = swarm.dir().join(SUI_KEYSTORE_FILENAME); + let ed25519_keypair = get_ed25519_keypair_from_keystore(keystore_path, &sender).unwrap(); + + let gas = get_gas_object_with_wallet_context(&context, &sender) + .await + .expect("Expect {sender} to have at least one gas object"); + + // The default test parameters are somewhat conservative in order to keep the running time + // of the test reasonable in CI. + let mut workloads = vec![make_combination_workload( + get_var("SIM_STRESS_TEST_QPS", 10), + get_var("SIM_STRESS_TEST_WORKERS", 10), + get_var("SIM_STRESS_TEST_IFR", 2), + gas.0, + sender, + Arc::new(ed25519_keypair), + 10, // num_transfer_accounts + 1, // shared_counter_weight + 1, // transfer_object_weight + )]; + + let aggregator = test_authority_aggregator(swarm.config()); + + for w in workloads.iter_mut() { + w.workload.init(&aggregator).await; + } + + let driver = BenchDriver::new(5); + let registry = prometheus::Registry::new(); + + // Use 0 for unbounded + let test_duration_secs = get_var("SIM_STRESS_TEST_DURATION_SECS", 10); + let test_duration = if test_duration_secs == 0 { + Duration::MAX + } else { + Duration::from_secs(test_duration_secs) + }; + let interval = Interval::Time(test_duration); + + let show_progress = interval.is_unbounded(); + let stats = driver + .run(workloads, aggregator, ®istry, show_progress, interval) + .await + .unwrap(); + + assert_eq!(stats.num_error, 0); + } +} diff --git a/crates/sui-config/src/genesis_config.rs b/crates/sui-config/src/genesis_config.rs index 998c08589a284..b5139404cfb0f 100644 --- a/crates/sui-config/src/genesis_config.rs +++ b/crates/sui-config/src/genesis_config.rs @@ -190,7 +190,7 @@ fn default_gas_value() -> u64 { DEFAULT_GAS_AMOUNT } -const DEFAULT_GAS_AMOUNT: u64 = 100000000; +const DEFAULT_GAS_AMOUNT: u64 = 100000000000000; const DEFAULT_NUMBER_OF_AUTHORITIES: usize = 4; const DEFAULT_NUMBER_OF_ACCOUNT: usize = 5; const DEFAULT_NUMBER_OF_OBJECT_PER_ACCOUNT: usize = 5; diff --git a/crates/sui-config/tests/snapshots/snapshot_tests__genesis_config_snapshot_matches.snap b/crates/sui-config/tests/snapshots/snapshot_tests__genesis_config_snapshot_matches.snap index fe9613a4f8906..de6714837634d 100644 --- a/crates/sui-config/tests/snapshots/snapshot_tests__genesis_config_snapshot_matches.snap +++ b/crates/sui-config/tests/snapshots/snapshot_tests__genesis_config_snapshot_matches.snap @@ -10,66 +10,66 @@ accounts: - address: 28cc79994ae5c36d715efef4fc6f8661c203ff86 gas_objects: - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 gas_object_ranges: [] - address: 28cc79994ae5c36d715efef4fc6f8661c203ff86 gas_objects: - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 gas_object_ranges: [] - address: 28cc79994ae5c36d715efef4fc6f8661c203ff86 gas_objects: - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 gas_object_ranges: [] - address: 28cc79994ae5c36d715efef4fc6f8661c203ff86 gas_objects: - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 gas_object_ranges: [] - address: 28cc79994ae5c36d715efef4fc6f8661c203ff86 gas_objects: - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 - object_id: "0x28cc79994ae5c36d715efef4fc6f8661c203ff86" - gas_value: 100000000 + gas_value: 100000000000000 gas_object_ranges: [] diff --git a/crates/sui-core/src/lib.rs b/crates/sui-core/src/lib.rs index aa14076941aed..982015d632dd8 100644 --- a/crates/sui-core/src/lib.rs +++ b/crates/sui-core/src/lib.rs @@ -20,7 +20,6 @@ pub mod streamer; pub mod transaction_input_checker; pub mod transaction_streamer; -#[cfg(test)] pub mod test_utils; mod histogram; diff --git a/crates/sui-macros/Cargo.toml b/crates/sui-macros/Cargo.toml index 66f49fa43ed7d..51f8dd70806f0 100644 --- a/crates/sui-macros/Cargo.toml +++ b/crates/sui-macros/Cargo.toml @@ -16,4 +16,4 @@ syn = "1" workspace-hack = { path = "../workspace-hack"} [target.'cfg(msim)'.dependencies] -msim-macros = { git = "https://github.com/MystenLabs/mysten-sim.git", rev = "e0355e2836fd3a6df7952b6aaca2c178d6055197", package = "msim-macros" } +msim-macros = { git = "https://github.com/MystenLabs/mysten-sim.git", rev = "dad61af1709543ec31529a78c489bc4b9c973375", package = "msim-macros" } diff --git a/crates/sui-macros/src/lib.rs b/crates/sui-macros/src/lib.rs index 2e0db77af8296..03b37ee6f3ffa 100644 --- a/crates/sui-macros/src/lib.rs +++ b/crates/sui-macros/src/lib.rs @@ -27,9 +27,9 @@ pub fn init_static_initializers(_args: TokenStream, item: TokenStream) -> TokenS // be very important for being able to reproduce a failure that occurs in the Nth // iteration of a multi-iteration test run. std::thread::spawn(|| { - ::telemetry_subscribers::init_for_testing(); - ::sui_framework::get_move_stdlib(); - ::sui_framework::get_sui_framework(); + ::sui_simulator::telemetry_subscribers::init_for_testing(); + ::sui_simulator::sui_framework::get_move_stdlib(); + ::sui_simulator::sui_framework::get_sui_framework(); }).join().unwrap(); #body @@ -56,13 +56,13 @@ pub fn sui_test(args: TokenStream, item: TokenStream) -> TokenStream { let header = if cfg!(msim) { quote! { #[::sui_simulator::sim_test(crate = "sui_simulator", #(#args)*)] - #[init_static_initializers] + #[::sui_macros::init_static_initializers] } } else { quote! { #[::tokio::test(#(#args)*)] // though this is not required for tokio, we do it to get logs as well. - #[init_static_initializers] + #[::sui_macros::init_static_initializers] } }; @@ -88,7 +88,7 @@ pub fn sim_test(args: TokenStream, item: TokenStream) -> TokenStream { let result = if cfg!(msim) { quote! { #[::sui_simulator::sim_test(crate = "sui_simulator", #(#args)*)] - #[init_static_initializers] + #[::sui_macros::init_static_initializers] #input } } else { diff --git a/crates/sui-simulator/Cargo.toml b/crates/sui-simulator/Cargo.toml index 37c77cc589903..16b8e2bca7f7c 100644 --- a/crates/sui-simulator/Cargo.toml +++ b/crates/sui-simulator/Cargo.toml @@ -8,6 +8,9 @@ edition = "2021" [dependencies] workspace-hack = { path = "../workspace-hack"} +sui-framework = { path = "../sui-framework" } +telemetry-subscribers = "0.1.0" +tracing = "0.1" [target.'cfg(msim)'.dependencies] -msim = { git = "https://github.com/MystenLabs/mysten-sim.git", rev = "e0355e2836fd3a6df7952b6aaca2c178d6055197", package = "msim" } +msim = { git = "https://github.com/MystenLabs/mysten-sim.git", rev = "dad61af1709543ec31529a78c489bc4b9c973375", package = "msim" } diff --git a/crates/sui-simulator/src/lib.rs b/crates/sui-simulator/src/lib.rs index 41bb62025a54f..1959e0e3c4e3c 100644 --- a/crates/sui-simulator/src/lib.rs +++ b/crates/sui-simulator/src/lib.rs @@ -3,3 +3,79 @@ #[cfg(msim)] pub use msim::*; + +// Re-export things used by sui-macros +pub use sui_framework; +pub use telemetry_subscribers; + +#[cfg(msim)] +pub mod configs { + use msim::*; + use std::collections::HashMap; + use std::ops::Range; + use std::time::Duration; + + use tracing::info; + + fn ms_to_dur(range: Range) -> Range { + Duration::from_millis(range.start)..Duration::from_millis(range.end) + } + + pub fn uniform_latency_ms(range: Range) -> SimConfig { + let range = ms_to_dur(range); + SimConfig { + net: NetworkConfig { + latency: LatencyConfig { + default_latency: LatencyDistribution::uniform(range), + ..Default::default() + }, + ..Default::default() + }, + } + } + + pub fn bimodal_latency_ms( + baseline: Range, + degraded: Range, + degraded_freq: f64, + ) -> SimConfig { + let baseline = ms_to_dur(baseline); + let degraded = ms_to_dur(degraded); + SimConfig { + net: NetworkConfig { + latency: LatencyConfig { + default_latency: LatencyDistribution::bimodal( + baseline, + degraded, + degraded_freq, + ), + ..Default::default() + }, + ..Default::default() + }, + } + } + + /// Select from among a number of configs using the SUI_SIM_CONFIG env var. + pub fn env_config( + default: SimConfig, + env_configs: impl IntoIterator, + ) -> SimConfig { + let mut env_configs = HashMap::<&'static str, SimConfig>::from_iter(env_configs); + if let Some(env) = std::env::var("SUI_SIM_CONFIG").ok() { + if let Some(cfg) = env_configs.remove(env.as_str()) { + info!("Using test config for SUI_SIM_CONFIG={}", env); + cfg + } else { + panic!( + "No config found for SUI_SIM_CONFIG={}. Available configs are: {:?}", + env, + env_configs.keys() + ); + } + } else { + info!("Using default test config"); + default + } + } +} diff --git a/crates/sui/Cargo.toml b/crates/sui/Cargo.toml index 37e8edc9b1385..440ab7f20f789 100644 --- a/crates/sui/Cargo.toml +++ b/crates/sui/Cargo.toml @@ -60,9 +60,6 @@ hex = "0.4.3" jemallocator = { version = "^0.5", features = ["profiling"] } jemalloc-ctl = "^0.5" -[target.'cfg(msim)'.dependencies] -sui-simulator = { path = "../sui-simulator" } - [dev-dependencies] tempfile = "3.3.0" futures = "0.3.23" @@ -78,6 +75,7 @@ move-package = { git = "https://github.com/move-language/move", rev = "e1e647b73 sui-core = { path = "../sui-core" } sui-node = { path = "../sui-node" } sui-macros = { path = "../sui-macros" } +sui-simulator = { path = "../sui-simulator" } [package.metadata.cargo-udeps.ignore] normal = ["jemallocator", "jemalloc-ctl"] diff --git a/crates/sui/tests/full_node_tests.rs b/crates/sui/tests/full_node_tests.rs index 4dfcd16bb16ae..804b4119b3d91 100644 --- a/crates/sui/tests/full_node_tests.rs +++ b/crates/sui/tests/full_node_tests.rs @@ -211,7 +211,7 @@ async fn test_full_node_indexes() -> Result<(), anyhow::Error> { object_id: transferred_object, version: SequenceNumber::from_u64(1), type_: TransferType::Coin, - amount: Some(100000000), + amount: Some(100000000000000), }; // query all events @@ -614,7 +614,7 @@ async fn test_full_node_event_read_api_ok() -> Result<(), anyhow::Error> { object_id: transferred_object, version: SequenceNumber::from_u64(1), type_: TransferType::Coin, - amount: Some(100000000), + amount: Some(100000000000000), }; // query by sender diff --git a/scripts/simtest/cargo-simtest b/scripts/simtest/cargo-simtest index 6c318b8bf3f63..9569be4afe9ac 100755 --- a/scripts/simtest/cargo-simtest +++ b/scripts/simtest/cargo-simtest @@ -58,7 +58,7 @@ if [ -n "$LOCAL_MSIM_PATH" ]; then else cargo_patch_args+=( --config 'patch.crates-io.tokio.git = "https://github.com/MystenLabs/mysten-sim.git"' - --config 'patch.crates-io.tokio.rev = "e0355e2836fd3a6df7952b6aaca2c178d6055197"' + --config 'patch.crates-io.tokio.rev = "dad61af1709543ec31529a78c489bc4b9c973375"' ) fi @@ -85,6 +85,9 @@ if [ "$1" = "build" ]; then CARGO_COMMAND=(build --profile simulator) fi +# Must supply a new temp dir - the test is deterministic and can't choose one randomly itself. +export TMPDIR=$(mktemp -d) + cargo ${CARGO_COMMAND[@]} \ --config "build.rustflags = [$RUST_FLAGS]" \ "${cargo_patch_args[@]}" \