From e7e6291bd9f2ffc95883b4942003400a2b770894 Mon Sep 17 00:00:00 2001 From: Michael George Date: Tue, 17 Dec 2024 11:06:16 -0500 Subject: [PATCH 1/5] removed feature flags for sui-move --- crates/sui-framework-tests/Cargo.toml | 2 +- crates/sui-move/Cargo.toml | 12 +----------- crates/sui-move/src/lib.rs | 13 ------------- crates/sui-source-validation-service/Cargo.toml | 4 ++-- crates/sui/Cargo.toml | 2 +- 5 files changed, 5 insertions(+), 28 deletions(-) diff --git a/crates/sui-framework-tests/Cargo.toml b/crates/sui-framework-tests/Cargo.toml index 188b0ef65ba61..fa757d3dda98b 100644 --- a/crates/sui-framework-tests/Cargo.toml +++ b/crates/sui-framework-tests/Cargo.toml @@ -19,7 +19,7 @@ datatest-stable.workspace = true prometheus.workspace = true sui-framework.workspace = true -sui-move = { workspace = true, features = ["unit_test"] } +sui-move.workspace = true sui-move-build.workspace = true sui-protocol-config.workspace = true sui-types.workspace = true diff --git a/crates/sui-move/Cargo.toml b/crates/sui-move/Cargo.toml index 17068ff6a903f..4b1afd221eb39 100644 --- a/crates/sui-move/Cargo.toml +++ b/crates/sui-move/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" anyhow.workspace = true clap.workspace = true colored.workspace = true -once_cell = { workspace = true, optional = true } +once_cell.workspace = true serde_json.workspace = true serde_yaml.workspace = true tracing.workspace = true @@ -56,13 +56,3 @@ sui-simulator.workspace = true [package.metadata.cargo-udeps.ignore] normal = ["jemalloc-ctl"] - -[features] -default = [] -build = [] -coverage = [] -disassemble = [] -prove = [] -unit_test = ["build", "dep:once_cell"] -calibrate = [] -all = ["build", "coverage", "disassemble", "prove", "unit_test", "calibrate"] diff --git a/crates/sui-move/src/lib.rs b/crates/sui-move/src/lib.rs index 93b8cf22cc063..74b4bdc5fc4e1 100644 --- a/crates/sui-move/src/lib.rs +++ b/crates/sui-move/src/lib.rs @@ -2,36 +2,27 @@ // SPDX-License-Identifier: Apache-2.0 use clap::Parser; -#[cfg(feature = "unit_test")] use move_cli::base::test::UnitTestResult; use move_package::BuildConfig; use std::path::Path; use sui_move_build::set_sui_flavor; -#[cfg(feature = "build")] pub mod build; -#[cfg(feature = "coverage")] pub mod coverage; -#[cfg(feature = "disassemble")] pub mod disassemble; pub mod manage_package; pub mod migrate; pub mod new; -#[cfg(feature = "unit_test")] pub mod unit_test; #[derive(Parser)] pub enum Command { - #[cfg(feature = "build")] Build(build::Build), - #[cfg(feature = "coverage")] Coverage(coverage::Coverage), - #[cfg(feature = "disassemble")] Disassemble(disassemble::Disassemble), ManagePackage(manage_package::ManagePackage), Migrate(migrate::Migrate), New(new::New), - #[cfg(feature = "unit_test")] Test(unit_test::Test), } #[derive(Parser)] @@ -51,17 +42,13 @@ pub fn execute_move_command( anyhow::bail!(err_msg); } match command { - #[cfg(feature = "build")] Command::Build(c) => c.execute(package_path, build_config), - #[cfg(feature = "coverage")] Command::Coverage(c) => c.execute(package_path, build_config), - #[cfg(feature = "disassemble")] Command::Disassemble(c) => c.execute(package_path, build_config), Command::ManagePackage(c) => c.execute(package_path, build_config), Command::Migrate(c) => c.execute(package_path, build_config), Command::New(c) => c.execute(package_path), - #[cfg(feature = "unit_test")] Command::Test(c) => { let result = c.execute(package_path, build_config)?; diff --git a/crates/sui-source-validation-service/Cargo.toml b/crates/sui-source-validation-service/Cargo.toml index 53dc311ffc3d2..36f1bf325d4f1 100644 --- a/crates/sui-source-validation-service/Cargo.toml +++ b/crates/sui-source-validation-service/Cargo.toml @@ -25,7 +25,7 @@ tracing = "0.1.36" serde = { version = "1.0.144", features = ["derive"] } url = "2.3.1" -sui-move.workspace = true +sui-move.workspace = true sui-move-build.workspace = true sui-sdk.workspace = true sui-source-validation.workspace = true @@ -48,6 +48,6 @@ fs_extra = "1.3.0" reqwest.workspace = true sui.workspace = true -sui-move = { workspace = true, features = ["all"] } +sui-move.workspace = true sui-json-rpc-types.workspace = true test-cluster.workspace = true diff --git a/crates/sui/Cargo.toml b/crates/sui/Cargo.toml index 9078ceac6b6dd..0d5d1dff035f6 100644 --- a/crates/sui/Cargo.toml +++ b/crates/sui/Cargo.toml @@ -67,7 +67,7 @@ sui-json-rpc-types.workspace = true sui-sdk.workspace = true sui-keys.workspace = true sui-source-validation.workspace = true -sui-move = { workspace = true, features = ["all"] } +sui-move.workspace = true sui-move-build.workspace = true sui-package-management.workspace = true sui-protocol-config.workspace = true From f499afa5e93efd12f1af930c7d32111a583473fe Mon Sep 17 00:00:00 2001 From: Michael George Date: Fri, 20 Dec 2024 13:34:37 -0500 Subject: [PATCH 2/5] factored move dependency into its own submodule --- crates/sui-core/src/test_utils.rs | 38 ++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/crates/sui-core/src/test_utils.rs b/crates/sui-core/src/test_utils.rs index c9fcc1033fe0c..fb0ac3dc146ad 100644 --- a/crates/sui-core/src/test_utils.rs +++ b/crates/sui-core/src/test_utils.rs @@ -17,7 +17,6 @@ use sui_config::node::AuthorityOverloadConfig; use sui_framework::BuiltInFramework; use sui_genesis_builder::validator_info::ValidatorInfo; use sui_macros::nondeterministic; -use sui_move_build::{BuildConfig, CompiledPackage, SuiPackageHooks}; use sui_protocol_config::ProtocolConfig; use sui_types::base_types::{random_object_ref, ObjectID}; use sui_types::crypto::{ @@ -190,22 +189,29 @@ pub fn create_fake_cert_and_effect_digest<'a>( ) } -pub fn compile_basics_package() -> CompiledPackage { - compile_example_package("../../examples/move/basics") -} +#[cfg(test)] +mod build_utils { + use std::path::PathBuf; + use sui_move_build::{BuildConfig, CompiledPackage, SuiPackageHooks}; -pub fn compile_managed_coin_package() -> CompiledPackage { - compile_example_package("../../crates/sui-core/src/unit_tests/data/managed_coin") -} + pub fn compile_basics_package() -> CompiledPackage { + compile_example_package("../../examples/move/basics") + } -pub fn compile_example_package(relative_path: &str) -> CompiledPackage { - move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooks)); - let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - path.push(relative_path); + pub fn compile_managed_coin_package() -> CompiledPackage { + compile_example_package("../../crates/sui-core/src/unit_tests/data/managed_coin") + } - BuildConfig::new_for_testing().build(&path).unwrap() + pub fn compile_example_package(relative_path: &str) -> CompiledPackage { + move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooks)); + let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + path.push(relative_path); + + BuildConfig::new_for_testing().build(&path).unwrap() + } } +#[cfg(test)] async fn init_genesis( committee_size: usize, mut genesis_objects: Vec, @@ -215,7 +221,10 @@ async fn init_genesis( ObjectID, ) { // add object_basics package object to genesis - let modules: Vec<_> = compile_basics_package().get_modules().cloned().collect(); + let modules: Vec<_> = build_utils::compile_basics_package() + .get_modules() + .cloned() + .collect(); let genesis_move_packages: Vec<_> = BuiltInFramework::genesis_move_packages().collect(); let config = ProtocolConfig::get_for_max_version_UNSAFE(); let pkg = Object::new_package( @@ -265,6 +274,7 @@ async fn init_genesis( (genesis, key_pairs, pkg_id) } +#[cfg(test)] pub async fn init_local_authorities( committee_size: usize, genesis_objects: Vec, @@ -285,6 +295,7 @@ pub async fn init_local_authorities( (aggregator, authorities, genesis, framework) } +#[cfg(test)] pub async fn init_local_authorities_with_overload_thresholds( committee_size: usize, genesis_objects: Vec, @@ -307,6 +318,7 @@ pub async fn init_local_authorities_with_overload_thresholds( (aggregator, authorities, genesis, framework) } +#[cfg(test)] pub async fn init_local_authorities_with_genesis( genesis: &Genesis, authorities: Vec>, From 25d0de2d5ace4df5944a82e72361dcfe0007d083 Mon Sep 17 00:00:00 2001 From: Michael George Date: Fri, 20 Dec 2024 14:35:50 -0500 Subject: [PATCH 3/5] moved sui-build dependent parts into sui-move-build --- Cargo.lock | 1 + crates/sui-cluster-test/Cargo.toml | 1 + .../src/test_case/coin_index_test.rs | 2 +- ...fullnode_build_publish_transaction_test.rs | 2 +- crates/sui-core/src/lib.rs | 3 + crates/sui-core/src/quorum_driver/tests.rs | 2 +- crates/sui-core/src/test_utils.rs | 174 +----------------- .../unit_tests/authority_aggregator_tests.rs | 2 +- .../src/unit_tests/execution_driver_tests.rs | 5 +- .../src/unit_tests/unit_test_utils.rs | 150 +++++++++++++++ crates/sui-move-build/src/lib.rs | 21 +++ 11 files changed, 186 insertions(+), 177 deletions(-) create mode 100644 crates/sui-core/src/unit_tests/unit_test_utils.rs diff --git a/Cargo.lock b/Cargo.lock index a4984024d8bf6..7327e1fb993d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13218,6 +13218,7 @@ dependencies = [ "sui-json", "sui-json-rpc-types", "sui-keys", + "sui-move-build", "sui-pg-db", "sui-sdk", "sui-swarm", diff --git a/crates/sui-cluster-test/Cargo.toml b/crates/sui-cluster-test/Cargo.toml index 4b890ba8a6c7a..0bf0699b1b119 100644 --- a/crates/sui-cluster-test/Cargo.toml +++ b/crates/sui-cluster-test/Cargo.toml @@ -43,3 +43,4 @@ telemetry-subscribers.workspace = true test-cluster.workspace = true move-core-types.workspace = true +sui-move-build.workspace = true diff --git a/crates/sui-cluster-test/src/test_case/coin_index_test.rs b/crates/sui-cluster-test/src/test_case/coin_index_test.rs index 460180d2b8b22..74ff1de5b7c69 100644 --- a/crates/sui-cluster-test/src/test_case/coin_index_test.rs +++ b/crates/sui-cluster-test/src/test_case/coin_index_test.rs @@ -7,11 +7,11 @@ use jsonrpsee::rpc_params; use move_core_types::language_storage::StructTag; use serde_json::json; use std::collections::HashMap; -use sui_core::test_utils::compile_managed_coin_package; use sui_json::SuiJsonValue; use sui_json_rpc_types::ObjectChange; use sui_json_rpc_types::SuiTransactionBlockResponse; use sui_json_rpc_types::{Balance, SuiTransactionBlockResponseOptions}; +use sui_move_build::test_utils::compile_managed_coin_package; use sui_test_transaction_builder::make_staking_transaction; use sui_types::base_types::{ObjectID, ObjectRef}; use sui_types::gas_coin::GAS; diff --git a/crates/sui-cluster-test/src/test_case/fullnode_build_publish_transaction_test.rs b/crates/sui-cluster-test/src/test_case/fullnode_build_publish_transaction_test.rs index eef1107892b0f..049378b40148d 100644 --- a/crates/sui-cluster-test/src/test_case/fullnode_build_publish_transaction_test.rs +++ b/crates/sui-cluster-test/src/test_case/fullnode_build_publish_transaction_test.rs @@ -4,8 +4,8 @@ use crate::{TestCaseImpl, TestContext}; use async_trait::async_trait; use jsonrpsee::rpc_params; -use sui_core::test_utils::compile_basics_package; use sui_json_rpc_types::SuiTransactionBlockEffectsAPI; +use sui_move_build::test_utils::compile_basics_package; use sui_types::{base_types::ObjectID, object::Owner}; pub struct FullNodeBuildPublishTransactionTest; diff --git a/crates/sui-core/src/lib.rs b/crates/sui-core/src/lib.rs index 2852750d8b067..8f64954350ffa 100644 --- a/crates/sui-core/src/lib.rs +++ b/crates/sui-core/src/lib.rs @@ -73,6 +73,9 @@ mod transfer_to_object_tests; #[cfg(test)] #[path = "unit_tests/type_param_tests.rs"] mod type_param_tests; +#[cfg(test)] +#[path = "unit_tests/unit_test_utils.rs"] +mod unit_test_utils; pub mod signature_verifier; diff --git a/crates/sui-core/src/quorum_driver/tests.rs b/crates/sui-core/src/quorum_driver/tests.rs index 665ee2cd2a1b8..9be7fc832b08b 100644 --- a/crates/sui-core/src/quorum_driver/tests.rs +++ b/crates/sui-core/src/quorum_driver/tests.rs @@ -8,7 +8,7 @@ use crate::quorum_driver::{ use crate::test_authority_clients::LocalAuthorityClient; use crate::test_authority_clients::LocalAuthorityClientFaultConfig; use crate::test_utils::make_transfer_sui_transaction; -use crate::{quorum_driver::QuorumDriverMetrics, test_utils::init_local_authorities}; +use crate::{quorum_driver::QuorumDriverMetrics, unit_test_utils::init_local_authorities}; use mysten_common::sync::notify_read::{NotifyRead, Registration}; use std::net::SocketAddr; use std::sync::atomic::{AtomicUsize, Ordering}; diff --git a/crates/sui-core/src/test_utils.rs b/crates/sui-core/src/test_utils.rs index fb0ac3dc146ad..cbe62293e59ea 100644 --- a/crates/sui-core/src/test_utils.rs +++ b/crates/sui-core/src/test_utils.rs @@ -3,27 +3,15 @@ use fastcrypto::hash::MultisetHash; use fastcrypto::traits::KeyPair; -use futures::future::join_all; -use move_core_types::account_address::AccountAddress; -use move_core_types::ident_str; +use move_core_types::{account_address::AccountAddress, ident_str}; use shared_crypto::intent::{Intent, IntentScope}; -use std::collections::BTreeMap; -use std::path::PathBuf; use std::sync::Arc; use std::time::Duration; use sui_config::genesis::Genesis; -use sui_config::local_ip_utils; -use sui_config::node::AuthorityOverloadConfig; -use sui_framework::BuiltInFramework; -use sui_genesis_builder::validator_info::ValidatorInfo; use sui_macros::nondeterministic; -use sui_protocol_config::ProtocolConfig; use sui_types::base_types::{random_object_ref, ObjectID}; -use sui_types::crypto::{ - generate_proof_of_possession, get_key_pair, AccountKeyPair, AuthorityPublicKeyBytes, - NetworkKeyPair, SuiKeyPair, -}; -use sui_types::crypto::{AuthorityKeyPair, Signer}; +use sui_types::crypto::AuthorityKeyPair; +use sui_types::crypto::{AccountKeyPair, AuthorityPublicKeyBytes, Signer}; use sui_types::effects::{SignedTransactionEffects, TestEffectsBuilder}; use sui_types::error::SuiError; use sui_types::signature_verification::VerifiedDigestCache; @@ -38,16 +26,13 @@ use sui_types::{ committee::Committee, crypto::{AuthoritySignInfo, AuthoritySignature}, message_envelope::Message, - object::Object, transaction::CertifiedTransaction, }; use tokio::time::timeout; use tracing::{info, warn}; -use crate::authority::{test_authority_builder::TestAuthorityBuilder, AuthorityState}; -use crate::authority_aggregator::{AuthorityAggregator, AuthorityAggregatorBuilder, TimeoutConfig}; +use crate::authority::AuthorityState; use crate::state_accumulator::StateAccumulator; -use crate::test_authority_clients::LocalAuthorityClient; const WAIT_FOR_TX_TIMEOUT: Duration = Duration::from_secs(15); @@ -189,157 +174,6 @@ pub fn create_fake_cert_and_effect_digest<'a>( ) } -#[cfg(test)] -mod build_utils { - use std::path::PathBuf; - use sui_move_build::{BuildConfig, CompiledPackage, SuiPackageHooks}; - - pub fn compile_basics_package() -> CompiledPackage { - compile_example_package("../../examples/move/basics") - } - - pub fn compile_managed_coin_package() -> CompiledPackage { - compile_example_package("../../crates/sui-core/src/unit_tests/data/managed_coin") - } - - pub fn compile_example_package(relative_path: &str) -> CompiledPackage { - move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooks)); - let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - path.push(relative_path); - - BuildConfig::new_for_testing().build(&path).unwrap() - } -} - -#[cfg(test)] -async fn init_genesis( - committee_size: usize, - mut genesis_objects: Vec, -) -> ( - Genesis, - Vec<(AuthorityPublicKeyBytes, AuthorityKeyPair)>, - ObjectID, -) { - // add object_basics package object to genesis - let modules: Vec<_> = build_utils::compile_basics_package() - .get_modules() - .cloned() - .collect(); - let genesis_move_packages: Vec<_> = BuiltInFramework::genesis_move_packages().collect(); - let config = ProtocolConfig::get_for_max_version_UNSAFE(); - let pkg = Object::new_package( - &modules, - TransactionDigest::genesis_marker(), - config.max_move_package_size(), - config.move_binary_format_version(), - &genesis_move_packages, - ) - .unwrap(); - let pkg_id = pkg.id(); - genesis_objects.push(pkg); - - let mut builder = sui_genesis_builder::Builder::new().add_objects(genesis_objects); - let mut key_pairs = Vec::new(); - for i in 0..committee_size { - let key_pair: AuthorityKeyPair = get_key_pair().1; - let authority_name = key_pair.public().into(); - let worker_key_pair: NetworkKeyPair = get_key_pair().1; - let worker_name = worker_key_pair.public().clone(); - let account_key_pair: SuiKeyPair = get_key_pair::().1.into(); - let network_key_pair: NetworkKeyPair = get_key_pair().1; - let validator_info = ValidatorInfo { - name: format!("validator-{i}"), - protocol_key: authority_name, - worker_key: worker_name, - account_address: SuiAddress::from(&account_key_pair.public()), - network_key: network_key_pair.public().clone(), - gas_price: 1, - commission_rate: 0, - network_address: local_ip_utils::new_local_tcp_address_for_testing(), - p2p_address: local_ip_utils::new_local_udp_address_for_testing(), - narwhal_primary_address: local_ip_utils::new_local_udp_address_for_testing(), - narwhal_worker_address: local_ip_utils::new_local_udp_address_for_testing(), - description: String::new(), - image_url: String::new(), - project_url: String::new(), - }; - let pop = generate_proof_of_possession(&key_pair, (&account_key_pair.public()).into()); - builder = builder.add_validator(validator_info, pop); - key_pairs.push((authority_name, key_pair)); - } - for (_, key) in &key_pairs { - builder = builder.add_validator_signature(key); - } - let genesis = builder.build(); - (genesis, key_pairs, pkg_id) -} - -#[cfg(test)] -pub async fn init_local_authorities( - committee_size: usize, - genesis_objects: Vec, -) -> ( - AuthorityAggregator, - Vec>, - Genesis, - ObjectID, -) { - let (genesis, key_pairs, framework) = init_genesis(committee_size, genesis_objects).await; - let authorities = join_all(key_pairs.iter().map(|(_, key_pair)| { - TestAuthorityBuilder::new() - .with_genesis_and_keypair(&genesis, key_pair) - .build() - })) - .await; - let aggregator = init_local_authorities_with_genesis(&genesis, authorities.clone()).await; - (aggregator, authorities, genesis, framework) -} - -#[cfg(test)] -pub async fn init_local_authorities_with_overload_thresholds( - committee_size: usize, - genesis_objects: Vec, - overload_thresholds: AuthorityOverloadConfig, -) -> ( - AuthorityAggregator, - Vec>, - Genesis, - ObjectID, -) { - let (genesis, key_pairs, framework) = init_genesis(committee_size, genesis_objects).await; - let authorities = join_all(key_pairs.iter().map(|(_, key_pair)| { - TestAuthorityBuilder::new() - .with_genesis_and_keypair(&genesis, key_pair) - .with_authority_overload_config(overload_thresholds.clone()) - .build() - })) - .await; - let aggregator = init_local_authorities_with_genesis(&genesis, authorities.clone()).await; - (aggregator, authorities, genesis, framework) -} - -#[cfg(test)] -pub async fn init_local_authorities_with_genesis( - genesis: &Genesis, - authorities: Vec>, -) -> AuthorityAggregator { - telemetry_subscribers::init_for_testing(); - let mut clients = BTreeMap::new(); - for state in authorities { - let name = state.name; - let client = LocalAuthorityClient::new_from_authority(state); - clients.insert(name, client); - } - let timeouts = TimeoutConfig { - pre_quorum_timeout: Duration::from_secs(5), - post_quorum_timeout: Duration::from_secs(5), - serial_authority_request_interval: Duration::from_secs(1), - }; - AuthorityAggregatorBuilder::from_genesis(genesis) - .with_timeouts_config(timeouts) - .build_custom_clients(clients) -} - pub fn make_transfer_sui_transaction( gas_object: ObjectRef, recipient: SuiAddress, diff --git a/crates/sui-core/src/unit_tests/authority_aggregator_tests.rs b/crates/sui-core/src/unit_tests/authority_aggregator_tests.rs index da73141e0eabe..719483909b18d 100644 --- a/crates/sui-core/src/unit_tests/authority_aggregator_tests.rs +++ b/crates/sui-core/src/unit_tests/authority_aggregator_tests.rs @@ -29,7 +29,7 @@ use crate::test_authority_clients::{ HandleTransactionTestAuthorityClient, LocalAuthorityClient, LocalAuthorityClientFaultConfig, MockAuthorityApi, }; -use crate::test_utils::init_local_authorities; +use crate::unit_test_utils::init_local_authorities; use sui_framework::BuiltInFramework; use sui_types::utils::to_sender_signed_transaction; use tokio::time::Instant; diff --git a/crates/sui-core/src/unit_tests/execution_driver_tests.rs b/crates/sui-core/src/unit_tests/execution_driver_tests.rs index ecb96709cdfb4..e8b7e8a31755d 100644 --- a/crates/sui-core/src/unit_tests/execution_driver_tests.rs +++ b/crates/sui-core/src/unit_tests/execution_driver_tests.rs @@ -13,10 +13,9 @@ use crate::consensus_adapter::ConsensusAdapterMetrics; use crate::consensus_adapter::{ConnectionMonitorStatusForTests, MockConsensusClient}; use crate::safe_client::SafeClient; use crate::test_authority_clients::LocalAuthorityClient; -use crate::test_utils::make_transfer_object_transaction; -use crate::test_utils::{ +use crate::test_utils::{make_transfer_object_move_transaction, make_transfer_object_transaction}; +use crate::unit_test_utils::{ init_local_authorities, init_local_authorities_with_overload_thresholds, - make_transfer_object_move_transaction, }; use sui_protocol_config::ProtocolConfig; use sui_types::error::SuiError; diff --git a/crates/sui-core/src/unit_tests/unit_test_utils.rs b/crates/sui-core/src/unit_tests/unit_test_utils.rs new file mode 100644 index 0000000000000..9291d10b5310c --- /dev/null +++ b/crates/sui-core/src/unit_tests/unit_test_utils.rs @@ -0,0 +1,150 @@ +// Copyright (c) Mysten Labs, Inc. +// SPDX-License-Identifier: Apache-2.0 + +use crate::authority::{test_authority_builder::TestAuthorityBuilder, AuthorityState}; +use crate::authority_aggregator::{AuthorityAggregator, AuthorityAggregatorBuilder, TimeoutConfig}; +use crate::test_authority_clients::LocalAuthorityClient; +use fastcrypto::traits::KeyPair; +use futures::future::join_all; +use std::collections::BTreeMap; +use std::sync::Arc; +use std::time::Duration; +use sui_config::genesis::Genesis; +use sui_config::local_ip_utils; +use sui_config::node::AuthorityOverloadConfig; +use sui_framework::BuiltInFramework; +use sui_genesis_builder::validator_info::ValidatorInfo; +use sui_move_build::test_utils::compile_basics_package; +use sui_protocol_config::ProtocolConfig; +use sui_types::base_types::{ObjectID, SuiAddress, TransactionDigest}; +use sui_types::crypto::AuthorityKeyPair; +use sui_types::crypto::{ + generate_proof_of_possession, get_key_pair, AccountKeyPair, AuthorityPublicKeyBytes, + NetworkKeyPair, SuiKeyPair, +}; +use sui_types::object::Object; + +async fn init_genesis( + committee_size: usize, + mut genesis_objects: Vec, +) -> ( + Genesis, + Vec<(AuthorityPublicKeyBytes, AuthorityKeyPair)>, + ObjectID, +) { + // add object_basics package object to genesis + let modules: Vec<_> = compile_basics_package().get_modules().cloned().collect(); + let genesis_move_packages: Vec<_> = BuiltInFramework::genesis_move_packages().collect(); + let config = ProtocolConfig::get_for_max_version_UNSAFE(); + let pkg = Object::new_package( + &modules, + TransactionDigest::genesis_marker(), + config.max_move_package_size(), + config.move_binary_format_version(), + &genesis_move_packages, + ) + .unwrap(); + let pkg_id = pkg.id(); + genesis_objects.push(pkg); + + let mut builder = sui_genesis_builder::Builder::new().add_objects(genesis_objects); + let mut key_pairs = Vec::new(); + for i in 0..committee_size { + let key_pair: AuthorityKeyPair = get_key_pair().1; + let authority_name = key_pair.public().into(); + let worker_key_pair: NetworkKeyPair = get_key_pair().1; + let worker_name = worker_key_pair.public().clone(); + let account_key_pair: SuiKeyPair = get_key_pair::().1.into(); + let network_key_pair: NetworkKeyPair = get_key_pair().1; + let validator_info = ValidatorInfo { + name: format!("validator-{i}"), + protocol_key: authority_name, + worker_key: worker_name, + account_address: SuiAddress::from(&account_key_pair.public()), + network_key: network_key_pair.public().clone(), + gas_price: 1, + commission_rate: 0, + network_address: local_ip_utils::new_local_tcp_address_for_testing(), + p2p_address: local_ip_utils::new_local_udp_address_for_testing(), + narwhal_primary_address: local_ip_utils::new_local_udp_address_for_testing(), + narwhal_worker_address: local_ip_utils::new_local_udp_address_for_testing(), + description: String::new(), + image_url: String::new(), + project_url: String::new(), + }; + let pop = generate_proof_of_possession(&key_pair, (&account_key_pair.public()).into()); + builder = builder.add_validator(validator_info, pop); + key_pairs.push((authority_name, key_pair)); + } + for (_, key) in &key_pairs { + builder = builder.add_validator_signature(key); + } + let genesis = builder.build(); + (genesis, key_pairs, pkg_id) +} + +#[cfg(test)] +pub async fn init_local_authorities( + committee_size: usize, + genesis_objects: Vec, +) -> ( + AuthorityAggregator, + Vec>, + Genesis, + ObjectID, +) { + let (genesis, key_pairs, framework) = init_genesis(committee_size, genesis_objects).await; + let authorities = join_all(key_pairs.iter().map(|(_, key_pair)| { + TestAuthorityBuilder::new() + .with_genesis_and_keypair(&genesis, key_pair) + .build() + })) + .await; + let aggregator = init_local_authorities_with_genesis(&genesis, authorities.clone()).await; + (aggregator, authorities, genesis, framework) +} + +#[cfg(test)] +pub async fn init_local_authorities_with_overload_thresholds( + committee_size: usize, + genesis_objects: Vec, + overload_thresholds: AuthorityOverloadConfig, +) -> ( + AuthorityAggregator, + Vec>, + Genesis, + ObjectID, +) { + let (genesis, key_pairs, framework) = init_genesis(committee_size, genesis_objects).await; + let authorities = join_all(key_pairs.iter().map(|(_, key_pair)| { + TestAuthorityBuilder::new() + .with_genesis_and_keypair(&genesis, key_pair) + .with_authority_overload_config(overload_thresholds.clone()) + .build() + })) + .await; + let aggregator = init_local_authorities_with_genesis(&genesis, authorities.clone()).await; + (aggregator, authorities, genesis, framework) +} + +#[cfg(test)] +pub async fn init_local_authorities_with_genesis( + genesis: &Genesis, + authorities: Vec>, +) -> AuthorityAggregator { + telemetry_subscribers::init_for_testing(); + let mut clients = BTreeMap::new(); + for state in authorities { + let name = state.name; + let client = LocalAuthorityClient::new_from_authority(state); + clients.insert(name, client); + } + let timeouts = TimeoutConfig { + pre_quorum_timeout: Duration::from_secs(5), + post_quorum_timeout: Duration::from_secs(5), + serial_authority_request_interval: Duration::from_secs(1), + }; + AuthorityAggregatorBuilder::from_genesis(genesis) + .with_timeouts_config(timeouts) + .build_custom_clients(clients) +} diff --git a/crates/sui-move-build/src/lib.rs b/crates/sui-move-build/src/lib.rs index ce628c3894e75..57af07944427e 100644 --- a/crates/sui-move-build/src/lib.rs +++ b/crates/sui-move-build/src/lib.rs @@ -58,6 +58,27 @@ use sui_verifier::verifier as sui_bytecode_verifier; #[path = "unit_tests/build_tests.rs"] mod build_tests; +pub mod test_utils { + use crate::{BuildConfig, CompiledPackage, SuiPackageHooks}; + use std::path::PathBuf; + + pub fn compile_basics_package() -> CompiledPackage { + compile_example_package("../../examples/move/basics") + } + + pub fn compile_managed_coin_package() -> CompiledPackage { + compile_example_package("../../crates/sui-core/src/unit_tests/data/managed_coin") + } + + pub fn compile_example_package(relative_path: &str) -> CompiledPackage { + move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooks)); + let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + path.push(relative_path); + + BuildConfig::new_for_testing().build(&path).unwrap() + } +} + /// Wrapper around the core Move `CompiledPackage` with some Sui-specific traits and info #[derive(Debug, Clone)] pub struct CompiledPackage { From c6dc3352ebbd907299fe60a061c5f4c4dd25e3b3 Mon Sep 17 00:00:00 2001 From: Michael George Date: Fri, 20 Dec 2024 16:28:42 -0500 Subject: [PATCH 4/5] refactored authority_test_utils similarly --- crates/sui-core/Cargo.toml | 2 +- crates/sui-core/src/authority.rs | 4 + .../src/authority/authority_test_utils.rs | 146 ----------------- .../src/unit_tests/auth_unit_test_utils.rs | 154 ++++++++++++++++++ .../unit_tests/move_package_upgrade_tests.rs | 6 +- .../src/unit_tests/transaction_deny_tests.rs | 2 +- 6 files changed, 163 insertions(+), 151 deletions(-) create mode 100644 crates/sui-core/src/unit_tests/auth_unit_test_utils.rs diff --git a/crates/sui-core/Cargo.toml b/crates/sui-core/Cargo.toml index 1724bf0e8ca67..db3d3a1a49906 100644 --- a/crates/sui-core/Cargo.toml +++ b/crates/sui-core/Cargo.toml @@ -81,7 +81,6 @@ sui-swarm-config.workspace = true sui-genesis-builder.workspace = true sui-json-rpc-types.workspace = true sui-macros.workspace = true -sui-move-build.workspace = true sui-network.workspace = true sui-protocol-config.workspace = true sui-transaction-checks.workspace = true @@ -93,6 +92,7 @@ zeroize.workspace = true nonempty.workspace = true [dev-dependencies] +sui-move-build.workspace = true clap.workspace = true criterion.workspace = true expect-test.workspace = true diff --git a/crates/sui-core/src/authority.rs b/crates/sui-core/src/authority.rs index 0beef2d71c4e0..4f31e6c6aa5ad 100644 --- a/crates/sui-core/src/authority.rs +++ b/crates/sui-core/src/authority.rs @@ -199,6 +199,10 @@ mod batch_verification_tests; #[path = "unit_tests/coin_deny_list_tests.rs"] mod coin_deny_list_tests; +#[cfg(test)] +#[path = "unit_tests/auth_unit_test_utils.rs"] +pub mod auth_unit_test_utils; + pub mod authority_test_utils; pub mod authority_per_epoch_store; diff --git a/crates/sui-core/src/authority/authority_test_utils.rs b/crates/sui-core/src/authority/authority_test_utils.rs index a55c820334da5..37880f6388b5f 100644 --- a/crates/sui-core/src/authority/authority_test_utils.rs +++ b/crates/sui-core/src/authority/authority_test_utils.rs @@ -7,14 +7,8 @@ use crate::consensus_handler::SequencedConsensusTransaction; use core::default::Default; use fastcrypto::hash::MultisetHash; use fastcrypto::traits::KeyPair; -use move_core_types::account_address::AccountAddress; -use move_symbol_pool::Symbol; -use sui_move_build::{BuildConfig, CompiledPackage}; -use sui_types::crypto::Signature; use sui_types::crypto::{AccountKeyPair, AuthorityKeyPair}; use sui_types::messages_consensus::ConsensusTransaction; -use sui_types::move_package::UpgradePolicy; -use sui_types::programmable_transaction_builder::ProgrammableTransactionBuilder; use sui_types::utils::to_sender_signed_transaction; use super::test_authority_builder::TestAuthorityBuilder; @@ -468,143 +462,3 @@ pub async fn send_batch_consensus_no_execution( .await .unwrap() } - -pub fn build_test_modules_with_dep_addr( - path: &Path, - dep_original_addresses: impl IntoIterator, - dep_ids: impl IntoIterator, -) -> CompiledPackage { - let mut build_config = BuildConfig::new_for_testing(); - for (addr_name, obj_id) in dep_original_addresses { - build_config - .config - .additional_named_addresses - .insert(addr_name.to_string(), AccountAddress::from(obj_id)); - } - let mut package = build_config.build(path).unwrap(); - - let dep_id_mapping: BTreeMap<_, _> = dep_ids - .into_iter() - .map(|(dep_name, obj_id)| (Symbol::from(dep_name), obj_id)) - .collect(); - - assert_eq!( - dep_id_mapping.len(), - package.dependency_ids.unpublished.len() - ); - for unpublished_dep in &package.dependency_ids.unpublished { - let published_id = dep_id_mapping.get(unpublished_dep).unwrap(); - // Make sure we aren't overriding a package - assert!(package - .dependency_ids - .published - .insert(*unpublished_dep, *published_id) - .is_none()) - } - - // No unpublished deps - package.dependency_ids.unpublished.clear(); - package -} - -/// Returns the new package's ID and the upgrade cap object ref. -/// `dep_original_addresses` allows us to fill out mappings in the addresses section of the package manifest. These IDs -/// must be the original IDs of names. -/// dep_ids are the IDs of the dependencies of the package, in the latest version (if there were upgrades). -pub async fn publish_package_on_single_authority( - path: &Path, - sender: SuiAddress, - sender_key: &dyn Signer, - gas_payment: ObjectRef, - dep_original_addresses: impl IntoIterator, - dep_ids: Vec, - state: &Arc, -) -> SuiResult<(TransactionDigest, (ObjectID, ObjectRef))> { - let mut build_config = BuildConfig::new_for_testing(); - for (addr_name, obj_id) in dep_original_addresses { - build_config - .config - .additional_named_addresses - .insert(addr_name.to_string(), AccountAddress::from(obj_id)); - } - let modules = build_config.build(path).unwrap().get_package_bytes(false); - - let mut builder = ProgrammableTransactionBuilder::new(); - let cap = builder.publish_upgradeable(modules, dep_ids); - builder.transfer_arg(sender, cap); - let pt = builder.finish(); - - let rgp = state.epoch_store_for_testing().reference_gas_price(); - let txn_data = TransactionData::new_programmable( - sender, - vec![gas_payment], - pt, - rgp * TEST_ONLY_GAS_UNIT_FOR_PUBLISH, - rgp, - ); - - let signed = to_sender_signed_transaction(txn_data, sender_key); - let (_cert, effects) = send_and_confirm_transaction(state, signed).await?; - assert!(effects.data().status().is_ok()); - let package_id = effects - .data() - .created() - .iter() - .find(|c| c.1 == Owner::Immutable) - .unwrap() - .0 - .0; - let cap_object = effects - .data() - .created() - .iter() - .find(|c| matches!(c.1, Owner::AddressOwner(..))) - .unwrap() - .0; - Ok((*effects.transaction_digest(), (package_id, cap_object))) -} - -pub async fn upgrade_package_on_single_authority( - path: &Path, - sender: SuiAddress, - sender_key: &dyn Signer, - gas_payment: ObjectRef, - package_id: ObjectID, - upgrade_cap: ObjectRef, - dep_original_addresses: impl IntoIterator, - dep_id_mapping: impl IntoIterator, - state: &Arc, -) -> SuiResult<(TransactionDigest, ObjectID)> { - let package = build_test_modules_with_dep_addr(path, dep_original_addresses, dep_id_mapping); - - let with_unpublished_deps = false; - let modules = package.get_package_bytes(with_unpublished_deps); - let digest = package.get_package_digest(with_unpublished_deps).to_vec(); - - let rgp = state.epoch_store_for_testing().reference_gas_price(); - let data = TransactionData::new_upgrade( - sender, - gas_payment, - package_id, - modules, - package.published_dependency_ids(), - (upgrade_cap, Owner::AddressOwner(sender)), - UpgradePolicy::COMPATIBLE, - digest, - rgp * TEST_ONLY_GAS_UNIT_FOR_PUBLISH, - rgp, - ) - .unwrap(); - let signed = to_sender_signed_transaction(data, sender_key); - let (_cert, effects) = send_and_confirm_transaction(state, signed).await?; - assert!(effects.data().status().is_ok()); - let package_id = effects - .data() - .created() - .iter() - .find(|c| c.1 == Owner::Immutable) - .unwrap() - .0 - .0; - Ok((*effects.transaction_digest(), package_id)) -} diff --git a/crates/sui-core/src/unit_tests/auth_unit_test_utils.rs b/crates/sui-core/src/unit_tests/auth_unit_test_utils.rs new file mode 100644 index 0000000000000..d009b90a15886 --- /dev/null +++ b/crates/sui-core/src/unit_tests/auth_unit_test_utils.rs @@ -0,0 +1,154 @@ +// Copyright (c) 2021, Facebook, Inc. and its affiliates +// Copyright (c) Mysten Labs, Inc. +// SPDX-License-Identifier: Apache-2.0 + +use move_core_types::account_address::AccountAddress; +use move_symbol_pool::Symbol; +use sui_move_build::{BuildConfig, CompiledPackage}; +use sui_types::crypto::Signature; +use sui_types::move_package::UpgradePolicy; +use sui_types::programmable_transaction_builder::ProgrammableTransactionBuilder; +use sui_types::utils::to_sender_signed_transaction; + +use super::authority_test_utils::*; +use super::*; + +pub fn build_test_modules_with_dep_addr( + path: &Path, + dep_original_addresses: impl IntoIterator, + dep_ids: impl IntoIterator, +) -> CompiledPackage { + let mut build_config = BuildConfig::new_for_testing(); + for (addr_name, obj_id) in dep_original_addresses { + build_config + .config + .additional_named_addresses + .insert(addr_name.to_string(), AccountAddress::from(obj_id)); + } + let mut package = build_config.build(path).unwrap(); + + let dep_id_mapping: BTreeMap<_, _> = dep_ids + .into_iter() + .map(|(dep_name, obj_id)| (Symbol::from(dep_name), obj_id)) + .collect(); + + assert_eq!( + dep_id_mapping.len(), + package.dependency_ids.unpublished.len() + ); + for unpublished_dep in &package.dependency_ids.unpublished { + let published_id = dep_id_mapping.get(unpublished_dep).unwrap(); + // Make sure we aren't overriding a package + assert!(package + .dependency_ids + .published + .insert(*unpublished_dep, *published_id) + .is_none()) + } + + // No unpublished deps + package.dependency_ids.unpublished.clear(); + package +} + +/// Returns the new package's ID and the upgrade cap object ref. +/// `dep_original_addresses` allows us to fill out mappings in the addresses section of the package manifest. These IDs +/// must be the original IDs of names. +/// dep_ids are the IDs of the dependencies of the package, in the latest version (if there were upgrades). +pub async fn publish_package_on_single_authority( + path: &Path, + sender: SuiAddress, + sender_key: &dyn Signer, + gas_payment: ObjectRef, + dep_original_addresses: impl IntoIterator, + dep_ids: Vec, + state: &Arc, +) -> SuiResult<(TransactionDigest, (ObjectID, ObjectRef))> { + let mut build_config = BuildConfig::new_for_testing(); + for (addr_name, obj_id) in dep_original_addresses { + build_config + .config + .additional_named_addresses + .insert(addr_name.to_string(), AccountAddress::from(obj_id)); + } + let modules = build_config.build(path).unwrap().get_package_bytes(false); + + let mut builder = ProgrammableTransactionBuilder::new(); + let cap = builder.publish_upgradeable(modules, dep_ids); + builder.transfer_arg(sender, cap); + let pt = builder.finish(); + + let rgp = state.epoch_store_for_testing().reference_gas_price(); + let txn_data = TransactionData::new_programmable( + sender, + vec![gas_payment], + pt, + rgp * TEST_ONLY_GAS_UNIT_FOR_PUBLISH, + rgp, + ); + + let signed = to_sender_signed_transaction(txn_data, sender_key); + let (_cert, effects) = send_and_confirm_transaction(state, signed).await?; + assert!(effects.data().status().is_ok()); + let package_id = effects + .data() + .created() + .iter() + .find(|c| c.1 == Owner::Immutable) + .unwrap() + .0 + .0; + let cap_object = effects + .data() + .created() + .iter() + .find(|c| matches!(c.1, Owner::AddressOwner(..))) + .unwrap() + .0; + Ok((*effects.transaction_digest(), (package_id, cap_object))) +} + +pub async fn upgrade_package_on_single_authority( + path: &Path, + sender: SuiAddress, + sender_key: &dyn Signer, + gas_payment: ObjectRef, + package_id: ObjectID, + upgrade_cap: ObjectRef, + dep_original_addresses: impl IntoIterator, + dep_id_mapping: impl IntoIterator, + state: &Arc, +) -> SuiResult<(TransactionDigest, ObjectID)> { + let package = build_test_modules_with_dep_addr(path, dep_original_addresses, dep_id_mapping); + + let with_unpublished_deps = false; + let modules = package.get_package_bytes(with_unpublished_deps); + let digest = package.get_package_digest(with_unpublished_deps).to_vec(); + + let rgp = state.epoch_store_for_testing().reference_gas_price(); + let data = TransactionData::new_upgrade( + sender, + gas_payment, + package_id, + modules, + package.published_dependency_ids(), + (upgrade_cap, Owner::AddressOwner(sender)), + UpgradePolicy::COMPATIBLE, + digest, + rgp * TEST_ONLY_GAS_UNIT_FOR_PUBLISH, + rgp, + ) + .unwrap(); + let signed = to_sender_signed_transaction(data, sender_key); + let (_cert, effects) = send_and_confirm_transaction(state, signed).await?; + assert!(effects.data().status().is_ok()); + let package_id = effects + .data() + .created() + .iter() + .find(|c| c.1 == Owner::Immutable) + .unwrap() + .0 + .0; + Ok((*effects.transaction_digest(), package_id)) +} diff --git a/crates/sui-core/src/unit_tests/move_package_upgrade_tests.rs b/crates/sui-core/src/unit_tests/move_package_upgrade_tests.rs index 1139cd4081a40..6bda706a98bcf 100644 --- a/crates/sui-core/src/unit_tests/move_package_upgrade_tests.rs +++ b/crates/sui-core/src/unit_tests/move_package_upgrade_tests.rs @@ -35,7 +35,7 @@ use crate::authority::move_integration_tests::{ }; use crate::authority::test_authority_builder::TestAuthorityBuilder; use crate::authority::{ - authority_test_utils::build_test_modules_with_dep_addr, + auth_unit_test_utils::build_test_modules_with_dep_addr, authority_tests::execute_programmable_transaction, move_integration_tests::build_and_publish_test_package_with_upgrade_cap, AuthorityState, }; @@ -556,7 +556,7 @@ async fn test_upgrade_package_invalid_dep_only_upgrade_pre_v68() { FileOverlay::Add { file_name: "new_friend_module.move", contents: r#" -module base_addr::new_friend_module; +module base_addr::new_friend_module; public fun friend_call(): u64 { base_addr::base::friend_fun(1) } "#, }, @@ -595,7 +595,7 @@ async fn test_invalid_dep_only_upgrades() { FileOverlay::Add { file_name: "new_friend_module.move", contents: r#" -module base_addr::new_friend_module; +module base_addr::new_friend_module; public fun friend_call(): u64 { base_addr::base::friend_fun(1) } "#, }, diff --git a/crates/sui-core/src/unit_tests/transaction_deny_tests.rs b/crates/sui-core/src/unit_tests/transaction_deny_tests.rs index 49d2a7d7ab489..b31049319b31d 100644 --- a/crates/sui-core/src/unit_tests/transaction_deny_tests.rs +++ b/crates/sui-core/src/unit_tests/transaction_deny_tests.rs @@ -1,7 +1,7 @@ // Copyright (c) Mysten Labs, Inc. // SPDX-License-Identifier: Apache-2.0 -use crate::authority::authority_test_utils::{ +use crate::authority::auth_unit_test_utils::{ publish_package_on_single_authority, upgrade_package_on_single_authority, }; use crate::authority::test_authority_builder::TestAuthorityBuilder; From 85697883ce1f71dcb4162cea18b029a2a10c6028 Mon Sep 17 00:00:00 2001 From: Michael George Date: Fri, 20 Dec 2024 16:29:22 -0500 Subject: [PATCH 5/5] license fix --- crates/sui-core/src/unit_tests/auth_unit_test_utils.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/sui-core/src/unit_tests/auth_unit_test_utils.rs b/crates/sui-core/src/unit_tests/auth_unit_test_utils.rs index d009b90a15886..d66f8452884b0 100644 --- a/crates/sui-core/src/unit_tests/auth_unit_test_utils.rs +++ b/crates/sui-core/src/unit_tests/auth_unit_test_utils.rs @@ -1,4 +1,3 @@ -// Copyright (c) 2021, Facebook, Inc. and its affiliates // Copyright (c) Mysten Labs, Inc. // SPDX-License-Identifier: Apache-2.0