Skip to content

Commit

Permalink
Merge branch 'mdgeorge/remove-core-dep' into mdgeorge/remove-features
Browse files Browse the repository at this point in the history
  • Loading branch information
mdgeorge4153 committed Dec 20, 2024
2 parents 9d2fc4e + 8569788 commit 6ca512f
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 151 deletions.
2 changes: 1 addition & 1 deletion crates/sui-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions crates/sui-core/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
146 changes: 0 additions & 146 deletions crates/sui-core/src/authority/authority_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Item = (&'static str, ObjectID)>,
dep_ids: impl IntoIterator<Item = (&'static str, ObjectID)>,
) -> 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<Signature>,
gas_payment: ObjectRef,
dep_original_addresses: impl IntoIterator<Item = (&'static str, ObjectID)>,
dep_ids: Vec<ObjectID>,
state: &Arc<AuthorityState>,
) -> 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<Signature>,
gas_payment: ObjectRef,
package_id: ObjectID,
upgrade_cap: ObjectRef,
dep_original_addresses: impl IntoIterator<Item = (&'static str, ObjectID)>,
dep_id_mapping: impl IntoIterator<Item = (&'static str, ObjectID)>,
state: &Arc<AuthorityState>,
) -> 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))
}
153 changes: 153 additions & 0 deletions crates/sui-core/src/unit_tests/auth_unit_test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// 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<Item = (&'static str, ObjectID)>,
dep_ids: impl IntoIterator<Item = (&'static str, ObjectID)>,
) -> 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<Signature>,
gas_payment: ObjectRef,
dep_original_addresses: impl IntoIterator<Item = (&'static str, ObjectID)>,
dep_ids: Vec<ObjectID>,
state: &Arc<AuthorityState>,
) -> 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<Signature>,
gas_payment: ObjectRef,
package_id: ObjectID,
upgrade_cap: ObjectRef,
dep_original_addresses: impl IntoIterator<Item = (&'static str, ObjectID)>,
dep_id_mapping: impl IntoIterator<Item = (&'static str, ObjectID)>,
state: &Arc<AuthorityState>,
) -> 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))
}
6 changes: 3 additions & 3 deletions crates/sui-core/src/unit_tests/move_package_upgrade_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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) }
"#,
},
Expand Down Expand Up @@ -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) }
"#,
},
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-core/src/unit_tests/transaction_deny_tests.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 6ca512f

Please sign in to comment.