Skip to content

Commit

Permalink
Add test - pass references, address reservations and named addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
iamyulong committed Oct 23, 2024
1 parent 756d240 commit 9d93b20
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 0 deletions.
19 changes: 19 additions & 0 deletions radix-engine-tests/assets/blueprints/threading/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Threading> = address.into();
component.method();
}

pub fn method(&self) {}
}
}
181 changes: 181 additions & 0 deletions radix-engine-tests/tests/system/threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 9d93b20

Please sign in to comment.