Skip to content

Commit

Permalink
Add test checkpoint data builder
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfind committed Dec 31, 2024
1 parent 1013f93 commit 094faa4
Show file tree
Hide file tree
Showing 8 changed files with 738 additions and 38 deletions.
6 changes: 5 additions & 1 deletion crates/sui-core/src/authority/authority_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ pub async fn init_state_with_ids_and_versions<
) -> Arc<AuthorityState> {
let state = TestAuthorityBuilder::new().build().await;
for (address, object_id, version) in objects {
let obj = Object::with_id_owner_version_for_testing(object_id, version, address);
let obj = Object::with_id_owner_version_for_testing(
object_id,
version,
Owner::AddressOwner(address),
);
state.insert_genesis_object(obj).await;
}
state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,11 @@ async fn latest_object_cache_race_test() {
std::thread::spawn(move || {
let mut version = OBJECT_START_VERSION;
while start.elapsed() < Duration::from_secs(2) {
let object = Object::with_id_owner_version_for_testing(object_id, version, owner);
let object = Object::with_id_owner_version_for_testing(
object_id,
version,
Owner::AddressOwner(owner),
);

cache
.write_object_entry(&object_id, version, object.into())
Expand Down Expand Up @@ -1284,8 +1288,11 @@ async fn latest_object_cache_race_test() {
std::thread::sleep(Duration::from_micros(1));
}

let object =
Object::with_id_owner_version_for_testing(object_id, latest_version, owner);
let object = Object::with_id_owner_version_for_testing(
object_id,
latest_version,
Owner::AddressOwner(owner),
);

// because we obtained the ticket before reading the object, we will not write a stale
// version to the cache.
Expand Down
14 changes: 11 additions & 3 deletions crates/sui-core/src/unit_tests/authority_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ async fn test_dry_run_dev_inspect_max_gas_version() {
let gas_object = Object::with_id_owner_version_for_testing(
gas_object_id,
SequenceNumber::from_u64(SequenceNumber::MAX.value() - 1),
sender,
Owner::AddressOwner(sender),
);
let gas_object_ref = gas_object.compute_object_reference();
validator.insert_genesis_object(gas_object.clone()).await;
Expand Down Expand Up @@ -5949,8 +5949,16 @@ async fn test_consensus_handler_congestion_control_transaction_cancellation() {
let gas_objects = create_gas_objects(3, sender);
let gas_objects_cancelled_txn = create_gas_objects(1, sender);
let owned_objects_cancelled_txn = vec![
Object::with_id_owner_version_for_testing(ObjectID::random(), 1.into(), sender),
Object::with_id_owner_version_for_testing(ObjectID::random(), 2.into(), sender),
Object::with_id_owner_version_for_testing(
ObjectID::random(),
1.into(),
Owner::AddressOwner(sender),
),
Object::with_id_owner_version_for_testing(
ObjectID::random(),
2.into(),
Owner::AddressOwner(sender),
),
];

// Create the cluster with controlled per object congestion control and cancellation.
Expand Down
47 changes: 34 additions & 13 deletions crates/sui-core/src/unit_tests/transaction_manager_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{time::Duration, vec};

use sui_test_transaction_builder::TestTransactionBuilder;
use sui_types::executable_transaction::VerifiedExecutableTransaction;
use sui_types::object::Owner;
use sui_types::transaction::VerifiedTransaction;
use sui_types::{
base_types::{ObjectID, SequenceNumber},
Expand Down Expand Up @@ -120,8 +121,11 @@ async fn transaction_manager_basics() {
transaction_manager.check_empty_for_testing();

// Enqueue a transaction with a new gas object, empty input.
let gas_object_new =
Object::with_id_owner_version_for_testing(ObjectID::random(), 0.into(), owner);
let gas_object_new = Object::with_id_owner_version_for_testing(
ObjectID::random(),
0.into(),
Owner::AddressOwner(owner),
);
let transaction = make_transaction(gas_object_new.clone(), vec![]);
let tx_start_time = Instant::now();
transaction_manager.enqueue(vec![transaction.clone()], &state.epoch_store_for_testing());
Expand Down Expand Up @@ -386,7 +390,11 @@ async fn transaction_manager_receiving_notify_commit() {
let obj_id = ObjectID::random();
let object_arguments: Vec<_> = (0..10)
.map(|i| {
let object = Object::with_id_owner_version_for_testing(obj_id, i.into(), owner);
let object = Object::with_id_owner_version_for_testing(
obj_id,
i.into(),
Owner::AddressOwner(owner),
);
// Every other transaction receives the object, and we create a run of multiple receives in
// a row at the beginning to test that the TM doesn't get stuck in either configuration of:
// ImmOrOwnedObject => Receiving,
Expand Down Expand Up @@ -475,8 +483,10 @@ async fn transaction_manager_receiving_object_ready_notifications() {
transaction_manager.check_empty_for_testing();

let obj_id = ObjectID::random();
let receiving_object_new0 = Object::with_id_owner_version_for_testing(obj_id, 0.into(), owner);
let receiving_object_new1 = Object::with_id_owner_version_for_testing(obj_id, 1.into(), owner);
let receiving_object_new0 =
Object::with_id_owner_version_for_testing(obj_id, 0.into(), Owner::AddressOwner(owner));
let receiving_object_new1 =
Object::with_id_owner_version_for_testing(obj_id, 1.into(), Owner::AddressOwner(owner));
let receiving_object_arg0 =
ObjectArg::Receiving(receiving_object_new0.compute_object_reference());
let receive_object_transaction0 = make_transaction(
Expand Down Expand Up @@ -561,8 +571,10 @@ async fn transaction_manager_receiving_object_ready_notifications_multiple_of_sa
transaction_manager.check_empty_for_testing();

let obj_id = ObjectID::random();
let receiving_object_new0 = Object::with_id_owner_version_for_testing(obj_id, 0.into(), owner);
let receiving_object_new1 = Object::with_id_owner_version_for_testing(obj_id, 1.into(), owner);
let receiving_object_new0 =
Object::with_id_owner_version_for_testing(obj_id, 0.into(), Owner::AddressOwner(owner));
let receiving_object_new1 =
Object::with_id_owner_version_for_testing(obj_id, 1.into(), Owner::AddressOwner(owner));
let receiving_object_arg0 =
ObjectArg::Receiving(receiving_object_new0.compute_object_reference());
let receive_object_transaction0 = make_transaction(
Expand Down Expand Up @@ -661,8 +673,11 @@ async fn transaction_manager_receiving_object_ready_if_current_version_greater()
Object::with_id_owner_for_testing(gas_object_id, owner)
})
.collect();
let receiving_object =
Object::with_id_owner_version_for_testing(ObjectID::random(), 10.into(), owner);
let receiving_object = Object::with_id_owner_version_for_testing(
ObjectID::random(),
10.into(),
Owner::AddressOwner(owner),
);
gas_objects.push(receiving_object.clone());
let state = init_state_with_objects(gas_objects.clone()).await;

Expand All @@ -674,10 +689,16 @@ async fn transaction_manager_receiving_object_ready_if_current_version_greater()
// TM should be empty at the beginning.
transaction_manager.check_empty_for_testing();

let receiving_object_new0 =
Object::with_id_owner_version_for_testing(receiving_object.id(), 0.into(), owner);
let receiving_object_new1 =
Object::with_id_owner_version_for_testing(receiving_object.id(), 1.into(), owner);
let receiving_object_new0 = Object::with_id_owner_version_for_testing(
receiving_object.id(),
0.into(),
Owner::AddressOwner(owner),
);
let receiving_object_new1 = Object::with_id_owner_version_for_testing(
receiving_object.id(),
1.into(),
Owner::AddressOwner(owner),
);
let receiving_object_arg0 =
ObjectArg::Receiving(receiving_object_new0.compute_object_reference());
let receive_object_transaction0 = make_transaction(
Expand Down
113 changes: 97 additions & 16 deletions crates/sui-types/src/effects/test_effects_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub struct TestEffectsBuilder {
/// Provide the assigned versions for all shared objects.
shared_input_versions: BTreeMap<ObjectID, SequenceNumber>,
events_digest: Option<TransactionEventsDigest>,
created_objects: Vec<(ObjectID, Owner)>,
/// Objects that are mutated, old version and new owners.
mutated_objects: Vec<(ObjectID, SequenceNumber, Owner)>,
deleted_objects: Vec<(ObjectID, SequenceNumber)>,
}

impl TestEffectsBuilder {
Expand All @@ -28,6 +32,9 @@ impl TestEffectsBuilder {
status: None,
shared_input_versions: BTreeMap::new(),
events_digest: None,
created_objects: vec![],
mutated_objects: vec![],
deleted_objects: vec![],
}
}

Expand All @@ -50,7 +57,33 @@ impl TestEffectsBuilder {
self
}

pub fn with_created_objects(
mut self,
objects: impl IntoIterator<Item = (ObjectID, Owner)>,
) -> Self {
self.created_objects.extend(objects);
self
}

pub fn with_mutated_objects(
mut self,
// Object ID, old version, and new owner.
objects: impl IntoIterator<Item = (ObjectID, SequenceNumber, Owner)>,
) -> Self {
self.mutated_objects.extend(objects);
self
}

pub fn with_deleted_objects(
mut self,
objects: impl IntoIterator<Item = (ObjectID, SequenceNumber)>,
) -> Self {
self.deleted_objects.extend(objects);
self
}

pub fn build(self) -> TransactionEffects {
let lamport_version = self.get_lamport_version();
let status = self.status.unwrap_or_else(|| ExecutionStatus::Success);
// TODO: This does not yet support deleted shared objects.
let shared_objects = self
Expand All @@ -59,22 +92,6 @@ impl TestEffectsBuilder {
.map(|(id, version)| SharedInput::Existing((*id, *version, ObjectDigest::MIN)))
.collect();
let executed_epoch = 0;
let lamport_version = SequenceNumber::lamport_increment(
self.transaction
.transaction_data()
.input_objects()
.unwrap()
.iter()
.filter_map(|kind| kind.version())
.chain(
self.transaction
.transaction_data()
.receiving_objects()
.iter()
.map(|oref| oref.1),
)
.chain(self.shared_input_versions.values().copied()),
);
let sender = self.transaction.transaction_data().sender();
// TODO: Include receiving objects in the object changes as well.
let changed_objects = self
Expand Down Expand Up @@ -130,6 +147,49 @@ impl TestEffectsBuilder {
},
)),
})
.chain(self.created_objects.into_iter().map(|(id, owner)| {
(
id,
EffectsObjectChange {
input_state: ObjectIn::NotExist,
output_state: ObjectOut::ObjectWrite((ObjectDigest::random(), owner)),
id_operation: IDOperation::Created,
},
)
}))
.chain(
self.mutated_objects
.into_iter()
.map(|(id, version, owner)| {
(
id,
EffectsObjectChange {
input_state: ObjectIn::Exist((
(version, ObjectDigest::random()),
Owner::AddressOwner(sender),
)),
output_state: ObjectOut::ObjectWrite((
ObjectDigest::random(),
owner,
)),
id_operation: IDOperation::None,
},
)
}),
)
.chain(self.deleted_objects.into_iter().map(|(id, version)| {
(
id,
EffectsObjectChange {
input_state: ObjectIn::Exist((
(version, ObjectDigest::random()),
Owner::AddressOwner(sender),
)),
output_state: ObjectOut::NotExist,
id_operation: IDOperation::Deleted,
},
)
}))
.collect();
let gas_object_id = self.transaction.transaction_data().gas()[0].0;
let event_digest = self.events_digest;
Expand All @@ -148,4 +208,25 @@ impl TestEffectsBuilder {
dependencies,
)
}

fn get_lamport_version(&self) -> SequenceNumber {
SequenceNumber::lamport_increment(
self.transaction
.transaction_data()
.input_objects()
.unwrap()
.iter()
.filter_map(|kind| kind.version())
.chain(
self.transaction
.transaction_data()
.receiving_objects()
.iter()
.map(|oref| oref.1),
)
.chain(self.shared_input_versions.values().copied())
.chain(self.mutated_objects.iter().map(|(_, v, _)| *v))
.chain(self.deleted_objects.iter().map(|(_, v)| *v)),
)
}
}
1 change: 1 addition & 0 deletions crates/sui-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub mod sui_sdk_types_conversions;
pub mod sui_serde;
pub mod sui_system_state;
pub mod supported_protocol_versions;
pub mod test_checkpoint_data_builder;
pub mod traffic_control;
pub mod transaction;
pub mod transaction_executor;
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-types/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ impl Object {
pub fn with_id_owner_version_for_testing(
id: ObjectID,
version: SequenceNumber,
owner: SuiAddress,
owner: Owner,
) -> Self {
let data = Data::Move(MoveObject {
type_: GasCoin::type_().into(),
Expand All @@ -1088,7 +1088,7 @@ impl Object {
contents: GasCoin::new(id, GAS_VALUE_FOR_TESTING).to_bcs_bytes(),
});
ObjectInner {
owner: Owner::AddressOwner(owner),
owner,
data,
previous_transaction: TransactionDigest::genesis_marker(),
storage_rebate: 0,
Expand Down
Loading

0 comments on commit 094faa4

Please sign in to comment.