-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(template): allow resource actions to be restricted by component (#…
…868) Description --- Allows resource access rules to restrict actions to one or more specific components/templates Adds the ability to get pre-allocate component addresses in code before component creation Motivation and Context --- Thanks to @mrnaveira for this idea! Some use cases may want a user to mint their own badge by executing a method on a specific template without explicit permission from the component owner (permissionless). This PR allows a template author to be permissable in who can perform resource actions, but force all users to interact with their component/template to control how those resource actions are used. To set component-restricted rules in code, the component address is required. Often access rules are set in the constructor of the component, where the address is not known. This PR adds the ability ask the runtime for a "pre-allocated" component address and later apply that to the component that is to be created. How Has This Been Tested? --- New unit tests What process can a PR reviewer use to test or verify this change? --- Template using the new access rules, e.g ```rust let allocation = CallerContext::allocate_component_address(); let tokens = ResourceBuilder::fungible() .initial_supply(1000) .mintable(AccessRule::Restricted(RestrictedAccessRule::Require( RequireRule::Require(allocation.address().clone().into()), ))) .build_bucket(); Component::new(Self { tokens: Vault::from_bucket(tokens), }) .with_address_allocation(allocation) .create() ``` Breaking Changes --- - [x] None - [ ] Requires data directory to be deleted - [ ] Other - Please specify
- Loading branch information
Showing
24 changed files
with
405 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2023 The Tari Project | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
use tari_dan_engine::runtime::TransactionCommitError; | ||
use tari_template_lib::{args, models::ComponentAddress}; | ||
use tari_template_test_tooling::{support::assert_error::assert_reject_reason, TemplateTest}; | ||
use tari_transaction::Transaction; | ||
|
||
#[test] | ||
fn it_uses_allocation_address() { | ||
let mut test = TemplateTest::new(["tests/templates/address_allocation"]); | ||
|
||
let result = test.execute_expect_success( | ||
Transaction::builder() | ||
.call_function(test.get_template_address("AddressAllocationTest"), "create", args![]) | ||
.sign(test.get_test_secret_key()) | ||
.build(), | ||
vec![], | ||
); | ||
|
||
let actual = result | ||
.finalize | ||
.result | ||
.accept() | ||
.unwrap() | ||
.up_iter() | ||
.find_map(|(k, _)| k.as_component_address()) | ||
.unwrap(); | ||
|
||
let allocated = result.finalize.execution_results[0] | ||
.indexed | ||
.get_value::<ComponentAddress>("$.1") | ||
.unwrap() | ||
.unwrap(); | ||
assert_eq!(actual, allocated); | ||
} | ||
|
||
#[test] | ||
fn it_fails_if_allocation_is_not_used() { | ||
let mut test = TemplateTest::new(["tests/templates/address_allocation"]); | ||
let template_addr = test.get_template_address("AddressAllocationTest"); | ||
|
||
let reason = test.execute_expect_failure( | ||
Transaction::builder() | ||
.call_function(template_addr, "drop_allocation", args![]) | ||
.sign(test.get_test_secret_key()) | ||
.build(), | ||
vec![], | ||
); | ||
|
||
assert_reject_reason(reason, TransactionCommitError::DanglingAddressAllocations { count: 1 }); | ||
} |
Oops, something went wrong.