Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test - moving proofs and buckets #1621

Merged
merged 14 commits into from
Oct 16, 2023
125 changes: 125 additions & 0 deletions radix-engine-tests/tests/blueprints/component/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ mod component_test {
"name" => "TestToken".to_owned(), locked;
}
})
.burn_roles(burn_roles! {
burner => rule!(allow_all);
burner_updater => rule!(deny_all);
})
.mint_initial_supply(amount)
.into()
}
Expand Down Expand Up @@ -44,6 +48,10 @@ mod component_test {
bucket
}

pub fn burn_bucket(&mut self, bucket: Bucket) {
bucket.burn();
}

pub fn blueprint_name_function() -> String {
Runtime::blueprint_name()
}
Expand All @@ -53,3 +61,120 @@ mod component_test {
}
}
}

#[derive(Debug, PartialEq, Eq, ScryptoSbor, NonFungibleData)]
pub struct ComponentTest2NonFungible {
pub val: String,
}

#[blueprint]
mod component_test2 {

use component_test3::ComponentTest3;

struct ComponentTest2 {
vault: Vault,
}

impl ComponentTest2 {
pub fn create_component() -> Global<ComponentTest2> {
let resource_manager = ResourceBuilder::new_integer_non_fungible::<
ComponentTest2NonFungible,
>(OwnerRole::None)
.mint_roles(mint_roles! {
minter => rule!(allow_all);
minter_updater => rule!(deny_all);
})
.burn_roles(burn_roles! {
burner => rule!(allow_all);
burner_updater => rule!(deny_all);
})
.create_with_no_initial_supply();

let vault = resource_manager.create_empty_vault();

Self { vault }
.instantiate()
.prepare_to_globalize(OwnerRole::None)
.globalize()
}

pub fn generate_nft(&mut self) -> Bucket {
let resource_manager = self.vault.resource_manager();

let id = NonFungibleLocalId::integer(1);

let bucket = resource_manager.mint_non_fungible(
&id,
ComponentTest2NonFungible {
val: String::from("FirstNFT"),
},
);
self.vault.put(bucket);
let bucket = self.vault.as_non_fungible().take_non_fungible(&id);
bucket.into()
}

pub fn generate_nft_proof(&mut self) -> (Bucket, Proof) {
let bucket = self.generate_nft();
let proof = bucket.create_proof_of_all();
(bucket, proof)
}

pub fn pass_vault_to_new_component(&mut self) -> Global<ComponentTest3> {
let mut vault = Vault::with_bucket(self.generate_nft());
let bucket = vault.take(dec!(1));
let proof = bucket.create_proof_of_all();

let return_value = ComponentTest3::create_component_with_vault_and_proof(vault, proof);

bucket.burn();

return_value
}
}
}

#[blueprint]
mod component_test3 {

struct ComponentTest3 {
vault: Vault,
}

impl ComponentTest3 {
pub fn create_component(resource_id: ResourceAddress) -> Global<ComponentTest3> {
Self {
vault: Vault::new(resource_id),
}
.instantiate()
.prepare_to_globalize(OwnerRole::None)
.globalize()
}

pub fn create_component_with_vault_and_proof(
vault: Vault,
proof: Proof,
) -> Global<ComponentTest3> {
proof.check(vault.resource_address()).drop();
Self { vault }
.instantiate()
.prepare_to_globalize(OwnerRole::None)
.globalize()
}

pub fn check_proof(&self, proof: Proof) {
proof.check(self.vault.resource_address()).drop();
}

pub fn check_proof_and_burn_bucket(&self, bucket: Bucket, proof: Proof) {
proof.check(self.vault.resource_address()).drop();
bucket.burn();
}

pub fn burn_bucket_and_check_proof(&self, bucket: Bucket, proof: Proof) {
bucket.burn();
mstrug-rdx marked this conversation as resolved.
Show resolved Hide resolved
proof.check(self.vault.resource_address()).drop();
}
}
}
169 changes: 168 additions & 1 deletion radix-engine-tests/tests/component.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
mod package_loader;

use package_loader::PackageLoader;
use radix_engine::errors::RuntimeError;
use radix_engine::errors::{CallFrameError, KernelError, RuntimeError};
use radix_engine::kernel::call_frame::DropNodeError;
use radix_engine::types::*;
use scrypto_unit::*;
use transaction::prelude::*;
Expand Down Expand Up @@ -113,3 +114,169 @@ fn blueprint_name_can_be_obtained_from_a_method() {
// Assert
assert_eq!(blueprint_name, "ComponentTest")
}

#[test]
fn pass_bucket_and_proof_to_other_component() {
let mut test_runner = TestRunnerBuilder::new().build();
let package_address = test_runner.publish_package_simple(PackageLoader::get("component"));

// create 1st component
let receipt = test_runner.execute_manifest(
ManifestBuilder::new()
.lock_fee_from_faucet()
.call_function(
package_address,
"ComponentTest2",
"create_component",
manifest_args!(),
)
.build(),
vec![],
);
let result = receipt.expect_commit_success();

let component_address_1 = result.new_component_addresses().first().cloned().unwrap();
let resource_address_1 = result.new_resource_addresses().first().cloned().unwrap();

// create 2nd component passing resource address from 1st component
let receipt = test_runner.execute_manifest(
ManifestBuilder::new()
.lock_fee_from_faucet()
.call_function(
package_address,
"ComponentTest3",
"create_component",
manifest_args!(resource_address_1),
)
.build(),
vec![],
);
let result = receipt.expect_commit_success();
let component_address_2 = result.new_component_addresses().first().cloned().unwrap();

// take bucket and proof from the 1st component and pass
// to the 2nd component for proof check and bucket burn
let receipt = test_runner.execute_manifest(
ManifestBuilder::new()
.lock_fee_from_faucet()
.call_method(component_address_1, "generate_nft_proof", manifest_args!())
.take_all_from_worktop(resource_address_1, "bucket_1")
.pop_from_auth_zone("proof_1")
.call_method_with_name_lookup(
component_address_2,
"check_proof_and_burn_bucket",
|lookup| (lookup.bucket("bucket_1"), lookup.proof("proof_1")),
)
.build(),
vec![],
);

// verify if manifest executed with success
receipt.expect_commit_success();
}

#[test]
fn pass_bucket_and_proof_to_other_component_fail() {
let mut test_runner = TestRunnerBuilder::new().build();
let package_address = test_runner.publish_package_simple(PackageLoader::get("component"));

// create 1st component
let receipt = test_runner.execute_manifest(
ManifestBuilder::new()
.lock_fee_from_faucet()
.call_function(
package_address,
"ComponentTest2",
"create_component",
manifest_args!(),
)
.build(),
vec![],
);
let result = receipt.expect_commit_success();

let component_address_1 = result.new_component_addresses().first().cloned().unwrap();
let resource_address_1 = result.new_resource_addresses().first().cloned().unwrap();

// create 2nd component passing resource address from 1st component
let receipt = test_runner.execute_manifest(
ManifestBuilder::new()
.lock_fee_from_faucet()
.call_function(
package_address,
"ComponentTest3",
"create_component",
manifest_args!(resource_address_1),
)
.build(),
vec![],
);
let result = receipt.expect_commit_success();
let component_address_2 = result.new_component_addresses().first().cloned().unwrap();

// take bucket and proof from the 1st component and pass
// to the 2nd component for proof check and bucket burn
let receipt = test_runner.execute_manifest(
ManifestBuilder::new()
.lock_fee_from_faucet()
.call_method(component_address_1, "generate_nft_proof", manifest_args!())
.take_all_from_worktop(resource_address_1, "bucket_1")
.pop_from_auth_zone("proof_1")
.call_method_with_name_lookup(
component_address_2,
"burn_bucket_and_check_proof",
|lookup| (lookup.bucket("bucket_1"), lookup.proof("proof_1")),
)
.build(),
vec![],
);

// verify if manifest executed with an error
receipt.expect_specific_failure(|e| {
matches!(
e,
RuntimeError::KernelError(KernelError::CallFrameError(CallFrameError::DropNodeError(
DropNodeError::NodeBorrowed(..)
)))
)
});
}

#[test]
fn pass_vault_and_proof_to_other_component() {
let mut test_runner = TestRunnerBuilder::new().build();
let package_address = test_runner.publish_package_simple(PackageLoader::get("component"));

// create 1st component
let receipt = test_runner.execute_manifest(
ManifestBuilder::new()
.lock_fee_from_faucet()
.call_function(
package_address,
"ComponentTest2",
"create_component",
manifest_args!(),
)
.build(),
vec![],
);
let result = receipt.expect_commit_success();

let component_address_1 = result.new_component_addresses().first().cloned().unwrap();

// 1st component creates proof and vault and passes it to newly created 2nd component
let receipt = test_runner.execute_manifest(
ManifestBuilder::new()
.lock_fee_from_faucet()
.call_method(
component_address_1,
"pass_vault_to_new_component",
manifest_args!(),
)
.build(),
vec![],
);

// verify if manifest executed with success
receipt.expect_commit_success();
}