Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mstrug-rdx committed Jan 29, 2024
1 parent f7b1e2f commit 87778e1
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use transaction::prelude::*;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TrustedWorktopInstruction {
trusted: bool,
resources: Option<ResourceSpecifier>,
pub trusted: bool,
pub resources: Option<ResourceSpecifier>,
}

#[derive(Default)]
Expand Down Expand Up @@ -400,8 +400,11 @@ impl TrustedWorktop {
args,
);
} else {
// other global or internal component call
self.add_new_instruction(false, None);
self.handle_global_generic_component_method_call(
address,
method_name,
args,
);
}
}
}
Expand All @@ -423,6 +426,23 @@ impl TrustedWorktop {
_ => self.add_new_instruction(true, None),
}
}

fn handle_global_generic_component_method_call(
&mut self,
address: &GlobalAddress,
_method_name: &String,
_args: &ManifestValue,
) {
if FAUCET_COMPONENT.as_node_id() == address.as_node_id()
|| TRANSACTION_TRACKER.as_node_id() == address.as_node_id()
|| GENESIS_HELPER.as_node_id() == address.as_node_id()
{
self.add_new_instruction(true, None);
} else {
// other unknown global or internal component call
self.add_new_instruction(false, None);
}
}
}

impl ManifestSummaryCallback for TrustedWorktop {
Expand Down
12 changes: 12 additions & 0 deletions crates/radix-engine-toolkit/src/transaction_types/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,18 @@ pub impl ResourceSpecifier {
Self::Amount(x, ..) | Self::Ids(x, ..) => *x,
}
}
fn amount(&self) -> Option<&Decimal> {
match self {
Self::Amount(.., amount) => Some(amount),
_ => None,
}
}
fn ids(&self) -> Option<&IndexSet<NonFungibleLocalId>> {
match self {
Self::Ids(.., ids) => Some(ids),
_ => None,
}
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down
202 changes: 109 additions & 93 deletions crates/radix-engine-toolkit/tests/worktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use radix_engine_toolkit::transaction_types::ResourceSpecifierExt;
use scrypto_unit::*;
use transaction::prelude::*;

Expand All @@ -36,39 +37,40 @@ fn worktop_simple() {
.take_from_worktop(address, 10, "bucket_1")
.try_deposit_or_abort(account2, None, "bucket_1")
.build();
let (_, execution_summary) = test_runner.summarize(manifest);
let (manifest_summary, _) = test_runner.summarize(manifest);

// Assert
// assert_eq!(execution_summary.trusted_worktop_content.len(), 4);
// assert!(execution_summary.trusted_worktop_content[0]
// .0
// .content
// .get(&address)
// .is_none());
// assert!(execution_summary.trusted_worktop_content[0].1);
// assert_eq!(
// execution_summary.trusted_worktop_content[1]
// .0
// .content
// .get(&address)
// .unwrap()
// .amount()
// .unwrap(),
// dec!(10)
// );
// assert!(execution_summary.trusted_worktop_content[1].1);
// assert!(execution_summary.trusted_worktop_content[2]
// .0
// .content
// .get(&address)
// .is_none());
// assert!(execution_summary.trusted_worktop_content[2].1);
// assert!(execution_summary.trusted_worktop_content[3]
// .0
// .content
// .get(&address)
// .is_none());
// assert!(execution_summary.trusted_worktop_content[3].1);
assert_eq!(manifest_summary.trusted_worktop_instructions.len(), 4);
assert!(manifest_summary.trusted_worktop_instructions[0].trusted);
assert!(manifest_summary.trusted_worktop_instructions[0]
.resources
.is_none());
assert_eq!(
manifest_summary.trusted_worktop_instructions[1]
.resources
.as_ref()
.unwrap()
.resource_address(),
address
);
assert_eq!(
*manifest_summary.trusted_worktop_instructions[1]
.resources
.as_ref()
.unwrap()
.amount()
.unwrap(),
dec!(10)
);
assert!(manifest_summary.trusted_worktop_instructions[1].trusted);
assert!(manifest_summary.trusted_worktop_instructions[2]
.resources
.is_none());
assert!(manifest_summary.trusted_worktop_instructions[2].trusted);
assert!(manifest_summary.trusted_worktop_instructions[3]
.resources
.is_none());
assert!(!manifest_summary.trusted_worktop_instructions[3].trusted);
}

#[test]
Expand All @@ -90,39 +92,40 @@ fn worktop_simple2() {
.withdraw_from_account(account, address, 10)
.burn_all_from_worktop(address)
.build();
let (_, execution_summary) = test_runner.summarize(manifest);
let (manifest_summary, _) = test_runner.summarize(manifest);

// Assert
// assert_eq!(execution_summary.trusted_worktop_content.len(), 4);
// assert!(execution_summary.trusted_worktop_content[0]
// .0
// .content
// .get(&address)
// .is_none());
// assert!(execution_summary.trusted_worktop_content[0].1);
// assert_eq!(
// execution_summary.trusted_worktop_content[1]
// .0
// .content
// .get(&address)
// .unwrap()
// .amount()
// .unwrap(),
// dec!(10)
// );
// assert!(execution_summary.trusted_worktop_content[1].1);
// assert!(execution_summary.trusted_worktop_content[2]
// .0
// .content
// .get(&address)
// .is_none()); // automatically inserted instructino TakeAllFromWorktop
// assert!(execution_summary.trusted_worktop_content[2].1);
// assert!(execution_summary.trusted_worktop_content[3]
// .0
// .content
// .get(&address)
// .is_none());
// assert!(execution_summary.trusted_worktop_content[3].1);
assert_eq!(manifest_summary.trusted_worktop_instructions.len(), 4);
assert!(manifest_summary.trusted_worktop_instructions[0]
.resources
.is_none());
assert!(manifest_summary.trusted_worktop_instructions[0].trusted);
assert_eq!(
manifest_summary.trusted_worktop_instructions[1]
.resources
.as_ref()
.unwrap()
.resource_address(),
address
);
assert_eq!(
*manifest_summary.trusted_worktop_instructions[1]
.resources
.as_ref()
.unwrap()
.amount()
.unwrap(),
dec!(10)
);
assert!(manifest_summary.trusted_worktop_instructions[1].trusted);
assert!(manifest_summary.trusted_worktop_instructions[2]
.resources
.is_none()); // automatically inserted instruction TakeAllFromWorktop
assert!(manifest_summary.trusted_worktop_instructions[2].trusted);
assert!(manifest_summary.trusted_worktop_instructions[3]
.resources
.is_none());
assert!(manifest_summary.trusted_worktop_instructions[3].trusted);
}

#[test]
Expand All @@ -146,53 +149,66 @@ fn worktop_simple3() {
.return_to_worktop("bucket_1")
.try_deposit_entire_worktop_or_abort(account, None)
.build();
let (_, execution_summary) = test_runner.summarize(manifest);
let (manifest_summary, _) = test_runner.summarize(manifest);

// Assert
// assert_eq!(execution_summary.trusted_worktop_content.len(), 5);
// assert!(execution_summary.trusted_worktop_content[0]
// .0
// .content
// .get(&address)
// .is_none());
// assert!(execution_summary.trusted_worktop_content[0].1);
assert_eq!(manifest_summary.trusted_worktop_instructions.len(), 5);
assert!(manifest_summary.trusted_worktop_instructions[0]
.resources
.is_none());
assert!(manifest_summary.trusted_worktop_instructions[0].trusted);

assert_eq!(
manifest_summary.trusted_worktop_instructions[1]
.resources
.as_ref()
.unwrap()
.resource_address(),
address
);
assert_eq!(
*manifest_summary.trusted_worktop_instructions[1]
.resources
.as_ref()
.unwrap()
.amount()
.unwrap(),
dec!(10)
);
assert!(manifest_summary.trusted_worktop_instructions[1].trusted);
// assert_eq!(
// execution_summary.trusted_worktop_content[1]
// .0
// .content
// .get(&address)
// manifest_summary.trusted_worktop_instructions[2]
// .resources.as_ref()
// .unwrap()
// .amount()
// .unwrap(),
// dec!(10)
// .resource_address(),
// address
// );
// assert!(execution_summary.trusted_worktop_content[1].1);
// assert_eq!(
// execution_summary.trusted_worktop_content[2]
// .0
// .content
// .get(&address)
// *manifest_summary.trusted_worktop_instructions[2]
// .resources.as_ref()
// .unwrap()
// .amount()
// .unwrap(),
// dec!(4)
// );
// assert!(execution_summary.trusted_worktop_content[2].1);
// assert!(manifest_summary.trusted_worktop_instructions[2].trusted);
// assert_eq!(
// manifest_summary.trusted_worktop_instructions[3]
// .resources.as_ref()
// .unwrap()
// .resource_address(),
// address
// );
// assert_eq!(
// execution_summary.trusted_worktop_content[3]
// .0
// .content
// .get(&address)
// *manifest_summary.trusted_worktop_instructions[3]
// .resources.as_ref()
// .unwrap()
// .amount()
// .unwrap(),
// dec!(10)
// );
// assert!(!execution_summary.trusted_worktop_content[3].1);
// assert!(execution_summary.trusted_worktop_content[4]
// .0
// .content
// .get(&address)
// assert!(!manifest_summary.trusted_worktop_instructions[3].trusted);
// assert!(manifest_summary.trusted_worktop_instructions[4].resources
// .is_none());
// assert!(execution_summary.trusted_worktop_content[4].1);
// assert!(manifest_summary.trusted_worktop_instructions[4].trusted);
}

0 comments on commit 87778e1

Please sign in to comment.