From 9d93b20dac9ee7bedab69a48f1a7ee0d3e16d213 Mon Sep 17 00:00:00 2001 From: Yulong Wu Date: Wed, 23 Oct 2024 13:57:15 +0100 Subject: [PATCH] Add test - pass references, address reservations and named addresses --- .../assets/blueprints/threading/src/lib.rs | 19 ++ radix-engine-tests/tests/system/threading.rs | 181 ++++++++++++++++++ 2 files changed, 200 insertions(+) diff --git a/radix-engine-tests/assets/blueprints/threading/src/lib.rs b/radix-engine-tests/assets/blueprints/threading/src/lib.rs index 8d06894e5c..f371a903cf 100644 --- a/radix-engine-tests/assets/blueprints/threading/src/lib.rs +++ b/radix-engine-tests/assets/blueprints/threading/src/lib.rs @@ -21,10 +21,29 @@ mod threading { .address() } + pub fn new2(reservation: GlobalAddressReservation) -> ComponentAddress { + Self { + vault: Vault::new(XRD), + } + .instantiate() + .prepare_to_globalize(OwnerRole::None) + .with_address(reservation) + .globalize() + .address() + } + pub fn create_locked_bucket(&mut self, amount: Decimal) -> (Bucket, Proof) { let bucket = self.vault.take(amount); let proof = bucket.create_proof_of_all(); (bucket, proof) } + + pub fn call(node_id: NodeId) { + let address = unsafe { ComponentAddress::new_unchecked(node_id.into()) }; + let component: Global = address.into(); + component.method(); + } + + pub fn method(&self) {} } } diff --git a/radix-engine-tests/tests/system/threading.rs b/radix-engine-tests/tests/system/threading.rs index f402736469..26e224c00a 100644 --- a/radix-engine-tests/tests/system/threading.rs +++ b/radix-engine-tests/tests/system/threading.rs @@ -85,3 +85,184 @@ fn can_transfer_locked_bucket_between_threads() { let receipt = ledger.execute_transaction(&transaction, ExecutionConfig::for_test_transaction()); receipt.expect_commit_success(); } + +// Arguably, we may disallow transferring references +#[test] +fn can_pass_global_and_direct_access_references() { + let mut ledger = LedgerSimulatorBuilder::new().build(); + let (pk1, sk1, account1) = ledger.new_allocated_account(); + let (_, _, account) = ledger.new_allocated_account(); + let vault = ledger.get_component_vaults(account, XRD).pop().unwrap(); + + let start_epoch_inclusive = ledger.get_current_epoch(); + let end_epoch_exclusive = start_epoch_inclusive.after(1).unwrap(); + let transaction = TransactionV2Builder::new() + .add_signed_child( + "child", + PartialTransactionV2Builder::new() + .intent_header(IntentHeaderV2 { + network_id: NetworkDefinition::simulator().id, + start_epoch_inclusive, + end_epoch_exclusive, + min_proposer_timestamp_inclusive: None, + max_proposer_timestamp_exclusive: None, + intent_discriminator: 1, + }) + .manifest_builder(|builder| { + builder + // Unfortunately, there is no way to grab the received references + .yield_to_parent(()) + }) + .sign(&sk1) + .build(), + ) + .intent_header(IntentHeaderV2 { + network_id: NetworkDefinition::simulator().id, + start_epoch_inclusive, + end_epoch_exclusive, + min_proposer_timestamp_inclusive: None, + max_proposer_timestamp_exclusive: None, + intent_discriminator: 2, + }) + .manifest_builder(|builder| { + builder.lock_fee(account1, 3).yield_to_child( + "child", + ( + ManifestAddress::Static(account.into_node_id()), + ManifestAddress::Static(vault), + ), + ) + }) + .transaction_header(TransactionHeaderV2 { + notary_public_key: pk1.into(), + notary_is_signatory: false, + tip_basis_points: 0, + }) + .sign(&sk1) + .notarize(&sk1) + .build(); + + let receipt = ledger.execute_transaction(&transaction, ExecutionConfig::for_test_transaction()); + receipt.expect_commit_success(); +} + +#[test] +fn can_not_pass_address_reservation() { + let mut ledger = LedgerSimulatorBuilder::new().with_kernel_trace().build(); + let (pk1, sk1, account1) = ledger.new_allocated_account(); + let package_address = ledger.publish_package_simple(PackageLoader::get("threading")); + + let start_epoch_inclusive = ledger.get_current_epoch(); + let end_epoch_exclusive = start_epoch_inclusive.after(1).unwrap(); + let transaction = TransactionV2Builder::new() + .add_signed_child( + "child", + PartialTransactionV2Builder::new() + .intent_header(IntentHeaderV2 { + network_id: NetworkDefinition::simulator().id, + start_epoch_inclusive, + end_epoch_exclusive, + min_proposer_timestamp_inclusive: None, + max_proposer_timestamp_exclusive: None, + intent_discriminator: 1, + }) + .manifest_builder(|builder| builder.yield_to_parent(())) + .sign(&sk1) + .build(), + ) + .intent_header(IntentHeaderV2 { + network_id: NetworkDefinition::simulator().id, + start_epoch_inclusive, + end_epoch_exclusive, + min_proposer_timestamp_inclusive: None, + max_proposer_timestamp_exclusive: None, + intent_discriminator: 2, + }) + .manifest_builder(|builder| { + builder + .lock_fee(account1, 3) + .allocate_global_address( + package_address, + "Threading", + "address_reservation", + "address", + ) + .yield_to_child_with_name_lookup("child", |lookup| { + (lookup.address_reservation("address_reservation"),) + }) + }) + .transaction_header(TransactionHeaderV2 { + notary_public_key: pk1.into(), + notary_is_signatory: false, + tip_basis_points: 0, + }) + .sign(&sk1) + .notarize(&sk1) + .build(); + + let receipt = ledger.execute_transaction(&transaction, ExecutionConfig::for_test_transaction()); + receipt.expect_specific_failure(|e| { + matches!(e, RuntimeError::SystemError(SystemError::NotAnObject)) + }); +} + +#[test] +fn can_pass_named_address() { + let mut ledger = LedgerSimulatorBuilder::new().with_kernel_trace().build(); + let (pk1, sk1, account1) = ledger.new_allocated_account(); + let package_address = ledger.publish_package_simple(PackageLoader::get("threading")); + + let start_epoch_inclusive = ledger.get_current_epoch(); + let end_epoch_exclusive = start_epoch_inclusive.after(1).unwrap(); + let transaction = TransactionV2Builder::new() + .add_signed_child( + "child", + PartialTransactionV2Builder::new() + .intent_header(IntentHeaderV2 { + network_id: NetworkDefinition::simulator().id, + start_epoch_inclusive, + end_epoch_exclusive, + min_proposer_timestamp_inclusive: None, + max_proposer_timestamp_exclusive: None, + intent_discriminator: 1, + }) + .manifest_builder(|builder| builder.yield_to_parent(())) + .sign(&sk1) + .build(), + ) + .intent_header(IntentHeaderV2 { + network_id: NetworkDefinition::simulator().id, + start_epoch_inclusive, + end_epoch_exclusive, + min_proposer_timestamp_inclusive: None, + max_proposer_timestamp_exclusive: None, + intent_discriminator: 2, + }) + .manifest_builder(|builder| { + builder + .lock_fee(account1, 3) + .allocate_global_address( + package_address, + "Threading", + "address_reservation", + "address", + ) + .yield_to_child_with_name_lookup("child", |lookup| { + (lookup.named_address("address"),) + }) + .call_function_with_name_lookup(package_address, "Threading", "new2", |lookup| { + (lookup.address_reservation("address_reservation"),) + }) + }) + .transaction_header(TransactionHeaderV2 { + notary_public_key: pk1.into(), + notary_is_signatory: false, + tip_basis_points: 0, + }) + .sign(&sk1) + .notarize(&sk1) + .build(); + + let receipt = ledger.execute_transaction(&transaction, ExecutionConfig::for_test_transaction()); + receipt.expect_commit_success(); +}