Skip to content

Commit

Permalink
Updated implementation, added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mstrug-rdx committed Feb 6, 2024
1 parent 2ea90dd commit 04df093
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,86 @@
use crate::sbor::indexed_manifest_value::IndexedManifestValue;
use crate::utils::*;
use radix_engine::system::system_modules::execution_trace::ResourceSpecifier;
use radix_engine_interface::api::node_modules::royalty::*;
use radix_engine_interface::blueprints::{
access_controller::*, account::*, consensus_manager::*, identity::*,
package::*, pool::*,
pool::*,
};
use scrypto::prelude::*;
use transaction::prelude::*;

use super::TrustedWorktop;

impl TrustedWorktop {
fn unknown_function_call(&mut self) {
self.untrack_buckets = true;
self.untrack_worktop_content = true;
self.add_new_instruction(false, None);
}

pub fn handle_call_functions(
&mut self,
package_address: &DynamicPackageAddress,
blueprint_name: &str,
address: &DynamicPackageAddress,
_blueprint_name: &str,
function_name: &str,
args: &ManifestValue,
) {
// todo
if is_account(address) {
match function_name {
ACCOUNT_CREATE_ADVANCED_IDENT => {
self.add_new_instruction(true, None)
}
ACCOUNT_CREATE_IDENT => {
// resturns bucket with newly generated address
self.new_bucket_unknown_resources();
self.add_new_instruction(false, None);
}
_ => self.unknown_function_call(),
}
} else if is_validator(address) {
match function_name {
CONSENSUS_MANAGER_CREATE_IDENT => {
self.add_new_instruction(true, None)
}
_ => self.unknown_function_call(),
}
} else if is_identity(address) {
match function_name {
IDENTITY_CREATE_ADVANCED_IDENT => {
self.add_new_instruction(true, None)
}
IDENTITY_CREATE_IDENT => {
// resturns unknown bucket
self.new_bucket_unknown_resources();
self.add_new_instruction(false, None)
}
_ => self.unknown_function_call(),
}
} else if is_access_controller(address) {
match function_name {
ACCESS_CONTROLLER_CREATE_IDENT => {
if !self.untrack_buckets {
// invalidate input bucket
let input_args = IndexedManifestValue::from_typed(args);
assert_eq!(input_args.buckets().len(), 1);
let bucket_id = input_args
.buckets()
.first()
.expect("Expected bucket");
let resources = self
.bucket_consumed(bucket_id)
.expect("Bucket not found");
self.add_new_instruction(
resources.is_some(),
resources,
);
} else {
self.add_new_instruction(false, None);
}
}
_ => self.unknown_function_call(),
}
} else {
// todo: check for global comonents
self.unknown_function_call();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ use super::TrustedWorktop;
impl TrustedWorktop {
fn handle_account_methods(
&mut self,
method_name: &String,
method_name: &str,
args: &ManifestValue,
) {
match method_name.as_str() {
match method_name {
// withdraw resources from account by address and amount
ACCOUNT_WITHDRAW_IDENT => {
let input_args: AccountWithdrawInput =
Expand Down Expand Up @@ -109,7 +109,7 @@ impl TrustedWorktop {
}

// setting untracked buckets mode as we are not supporting handling vectors of buckets
self.untrack_buckets = true;
//self.untrack_buckets = true;
}
_ => self.add_new_instruction(false, None),
}
Expand All @@ -122,7 +122,10 @@ impl TrustedWorktop {
let resources = self
.bucket_consumed(bucket_id)
.expect("Bucket not found");
self.add_new_instruction(true, resources);
self.add_new_instruction(
resources.is_some(),
resources,
);
}
} else {
self.add_new_instruction(false, None);
Expand Down Expand Up @@ -193,10 +196,10 @@ impl TrustedWorktop {

fn handle_validator_methods(
&mut self,
method_name: &String,
method_name: &str,
args: &ManifestValue,
) {
match method_name.as_str() {
match method_name {
VALIDATOR_APPLY_REWARD_IDENT
| VALIDATOR_APPLY_EMISSION_IDENT
| VALIDATOR_LOCK_OWNER_STAKE_UNITS_IDENT => {
Expand All @@ -209,7 +212,7 @@ impl TrustedWorktop {
let resources = self
.bucket_consumed(bucket_id)
.expect("Bucket not found");
self.add_new_instruction(true, resources);
self.add_new_instruction(resources.is_some(), resources);
} else {
self.add_new_instruction(false, None);
}
Expand All @@ -228,15 +231,14 @@ impl TrustedWorktop {

fn handle_identity_methods(
&mut self,
method_name: &String,
method_name: &str,
_args: &ManifestValue,
) {
match method_name.as_str() {
IDENTITY_CREATE_IDENT | IDENTITY_SECURIFY_IDENT => {
// todo check if it is not fn
match method_name {
IDENTITY_SECURIFY_IDENT => {
// returns unknown bucket
self.untrack_buckets = true;
self.add_new_instruction(false, None);
self.new_bucket_unknown_resources();
self.add_new_instruction(true, None);
}

// all other methods are trusted as they doesn't change the worktop state
Expand All @@ -246,27 +248,10 @@ impl TrustedWorktop {

fn handle_access_controller_methods(
&mut self,
method_name: &String,
method_name: &str,
args: &ManifestValue,
) {
match method_name.as_str() {
ACCESS_CONTROLLER_CREATE_IDENT => {
// todo: it is a function
if !self.untrack_buckets {
// invalidate input bucket
let input_args = IndexedManifestValue::from_typed(args);
assert_eq!(input_args.buckets().len(), 1);
let bucket_id =
input_args.buckets().first().expect("Expected bucket");
let resources = self
.bucket_consumed(bucket_id)
.expect("Bucket not found");
self.add_new_instruction(true, resources);
} else {
self.add_new_instruction(false, None);
}
}

match method_name {
ACCESS_CONTROLLER_QUICK_CONFIRM_PRIMARY_ROLE_BADGE_WITHDRAW_ATTEMPT_IDENT
| ACCESS_CONTROLLER_QUICK_CONFIRM_RECOVERY_ROLE_BADGE_WITHDRAW_ATTEMPT_IDENT
| ACCESS_CONTROLLER_MINT_RECOVERY_BADGES_IDENT => {
Expand All @@ -282,10 +267,10 @@ impl TrustedWorktop {

fn handle_package_methods(
&mut self,
method_name: &String,
method_name: &str,
_args: &ManifestValue,
) {
match method_name.as_str() {
match method_name {
PACKAGE_PUBLISH_WASM_IDENT | PACKAGE_CLAIM_ROYALTIES_IDENT => {
// returns unknown bucket
self.untrack_buckets = true;
Expand All @@ -299,10 +284,10 @@ impl TrustedWorktop {

fn handle_fungible_resource_manager_methods(
&mut self,
method_name: &String,
method_name: &str,
_args: &ManifestValue,
) {
match method_name.as_str() {
match method_name {
FUNGIBLE_RESOURCE_MANAGER_CREATE_WITH_INITIAL_SUPPLY_IDENT
| FUNGIBLE_RESOURCE_MANAGER_MINT_IDENT => {
// todo: mint: global address is res.addr and it is trusted
Expand All @@ -318,10 +303,10 @@ impl TrustedWorktop {

fn handle_non_fungible_resource_manager_methods(
&mut self,
method_name: &String,
method_name: &str,
_args: &ManifestValue,
) {
match method_name.as_str() {
match method_name {
NON_FUNGIBLE_RESOURCE_MANAGER_CREATE_WITH_INITIAL_SUPPLY_IDENT
| NON_FUNGIBLE_RESOURCE_MANAGER_CREATE_RUID_WITH_INITIAL_SUPPLY_IDENT
| NON_FUNGIBLE_RESOURCE_MANAGER_MINT_IDENT // todo: trusted
Expand All @@ -339,10 +324,10 @@ impl TrustedWorktop {

fn handle_one_resource_pool_methods(
&mut self,
method_name: &String,
method_name: &str,
args: &ManifestValue,
) {
match method_name.as_str() {
match method_name {
ONE_RESOURCE_POOL_CONTRIBUTE_IDENT
| ONE_RESOURCE_POOL_REDEEM_IDENT
| ONE_RESOURCE_POOL_PROTECTED_WITHDRAW_IDENT => {
Expand All @@ -359,7 +344,7 @@ impl TrustedWorktop {
let resources = self
.bucket_consumed(&input_args.bucket)
.expect("Bucket not found");
self.add_new_instruction(true, resources);
self.add_new_instruction(resources.is_some(), resources);
} else {
self.add_new_instruction(false, None);
}
Expand All @@ -372,10 +357,10 @@ impl TrustedWorktop {

fn handle_two_resource_pool_methods(
&mut self,
method_name: &String,
method_name: &str,
args: &ManifestValue,
) {
match method_name.as_str() {
match method_name {
TWO_RESOURCE_POOL_CONTRIBUTE_IDENT
| TWO_RESOURCE_POOL_REDEEM_IDENT
| TWO_RESOURCE_POOL_PROTECTED_WITHDRAW_IDENT => {
Expand All @@ -392,7 +377,7 @@ impl TrustedWorktop {
let resources = self
.bucket_consumed(&input_args.bucket)
.expect("Bucket not found");
self.add_new_instruction(true, resources);
self.add_new_instruction(resources.is_some(), resources);
} else {
self.add_new_instruction(false, None);
}
Expand All @@ -405,10 +390,10 @@ impl TrustedWorktop {

fn handle_multi_resource_pool_methods(
&mut self,
method_name: &String,
method_name: &str,
_args: &ManifestValue,
) {
match method_name.as_str() {
match method_name {
MULTI_RESOURCE_POOL_CONTRIBUTE_IDENT
| MULTI_RESOURCE_POOL_REDEEM_IDENT
| MULTI_RESOURCE_POOL_PROTECTED_DEPOSIT_IDENT
Expand All @@ -427,7 +412,7 @@ impl TrustedWorktop {
pub fn handle_call_methods(
&mut self,
address: &DynamicGlobalAddress,
method_name: &String,
method_name: &str,
args: &ManifestValue,
) {
if is_account(address) {
Expand Down Expand Up @@ -503,10 +488,10 @@ impl TrustedWorktop {

pub fn handle_call_royalty_methods(
&mut self,
method_name: &String,
method_name: &str,
_args: &ManifestValue,
) {
match method_name.as_str() {
match method_name {
COMPONENT_ROYALTY_CLAIM_ROYALTIES_IDENT => {
// returns unknown bucket
self.untrack_buckets = true;
Expand All @@ -521,12 +506,12 @@ impl TrustedWorktop {
fn handle_global_generic_component_method_call(
&mut self,
address: &GlobalAddress,
method_name: &String,
method_name: &str,
_args: &ManifestValue,
) {
if FAUCET_COMPONENT.as_node_id() == address.as_node_id() {
if method_name == "free" {
// puts on worktop 10k XRD // todo: change to constant
// puts on worktop faucet::FAUCET_FREE_AMOUNT XRD count
let resources = ResourceSpecifier::Amount(
XRD,
faucet::FAUCET_FREE_AMOUNT.into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,38 @@ pub struct TrustedWorktopInstruction {
}

#[derive(Default)]
// Instruction is trusted when we know exact resources transfer assiociated
// to that instruction (so we need to know what instruction is doing and if
// it transfers resources including exact count/list of these resources or not
// deals with resources at all).
//
// Worktop content tracker operation logic:
// If Instruction doesn't change worktop state and doesn't use buckets then it is trusted.
// If Instruction changes worktop state:
// 1. Puts resources on the worktop (ex. Account withdraws, Return to workotop, etc.)
// - if we know what resources has been put on the worktop then instruction is trusted
// - if we don't know what has been put on the worktop then instruction is untrasted
// and we are entering into untracked worktop content mode (from now we don't know
// exactly what is on the worktop)
// 2. Takes resources from the worktop (ex. Take from worktop instructions)
// - if we are in untracked worktop content mode then instruction is untrasted
// - if we know the resources then instruction is trusted
// If Instruction uses a bucket and we are not in bucket untracked mode:
// 1. If bucket is known and resources are known, then it is consumed and instruction is trusted
// 2. If bucket is known but resources are unknown then it is consumed and instruction is untrasted
// 3. If bucket is unknown then we are entering into bucket untracked mode and instruction is untrusted
//
// Bucket tracker operaion logic:
// Function/method call
// 1. Returns a bucket and we are not in untracked buckets mode:
// - if we know what is in the bucket -> call new_bucket_known_resources()
// - if we don't know what is in the bucket -> call new_bucket_unknown_resources()
// 2. We don't know what is returned:
// - enter untracked buckets mode
//
// We can indentify an instruction as trusted if we are in untracked worktop mode in
// case of an instruction which returns known bucket and that bucket is later consumed.
//
pub struct TrustedWorktop {
trusted_state_per_instruction: Vec<TrustedWorktopInstruction>,

Expand Down Expand Up @@ -423,14 +455,6 @@ impl ManifestSummaryCallback for TrustedWorktop {
args,
} => self.handle_call_functions(package_address, blueprint_name, function_name,
args),
/*{ // todo: check for global comonents
// we don't know if bucket is returned -> enter untracked buckets mode
self.untrack_buckets = true;
// we don't know if something was put on worktop -> enter untracked worktop content mode
self.untrack_worktop_content = true;
// we don't know if something is put on worktop
self.add_new_instruction(false, None)
}*/

InstructionV1::CallRoyaltyMethod {
method_name, args, ..
Expand Down

0 comments on commit 04df093

Please sign in to comment.