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

Feature/resource movements static analysis #1909

Merged
merged 28 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ffa5a41
Implement an account resource movements visitor
0xOmarA Sep 8, 2024
2eee357
Add a static analyzer for account resource movements
0xOmarA Sep 8, 2024
7771a03
Merge remote-tracking branch 'origin/feature/subintent-validation' in…
0xOmarA Sep 8, 2024
6ac62c4
Fix tests
0xOmarA Sep 8, 2024
a3fdebe
fix tests and messagev2
0xOmarA Sep 9, 2024
54273ce
Update subintents v2 to have a public field
0xOmarA Sep 9, 2024
23888eb
Fix the SBOR schema tests
0xOmarA Sep 9, 2024
6b21e98
Merge remote-tracking branch 'origin/develop' into feature/resource-m…
0xOmarA Sep 15, 2024
f12a0f2
Update lock files minimally
0xOmarA Sep 15, 2024
f81750e
fix tests
0xOmarA Sep 15, 2024
3cb7fd9
Merge remote-tracking branch 'origin/develop' into feature/resource-m…
0xOmarA Sep 24, 2024
fe535be
Refactor `on_start_instruction` to be smaller
0xOmarA Sep 24, 2024
a9e2d5d
refactor the static resource visitor impl
0xOmarA Sep 26, 2024
1a1408d
Merge branch 'develop' into feature/resource-movements-static-analysis
dhedey Sep 27, 2024
35a0e46
fix: Fix compile errors from merge
dhedey Sep 28, 2024
f9debbb
rework: Fix various issues in resource interpreter
dhedey Sep 28, 2024
be98983
fix: Fix a no-std test compilation
dhedey Sep 28, 2024
b202629
tweak: Add more details to OnNewNamedAddress
dhedey Sep 30, 2024
5841a9c
tweak: Minor fixes to public_key_hash docs
dhedey Sep 30, 2024
9ed02ad
feature: Create BucketBatch and ProofBatch
dhedey Sep 30, 2024
f52e46c
tweak: Movements visitor native fixes
dhedey Sep 30, 2024
24f2975
tweak: Minor method name improvements
dhedey Sep 30, 2024
ee2d471
feature: Refactor and support id allow list
dhedey Sep 30, 2024
2ecb294
tweak: Change AccountDeposit to be simpler
dhedey Sep 30, 2024
28dca9b
tweak: Rework bounds to use semantic types
dhedey Sep 30, 2024
819f697
fix: Fix tests and minor bugs in static resource tracker
dhedey Sep 30, 2024
d3e2f01
Merge remote-tracking branch 'origin/develop' into feature/resource-m…
0xOmarA Oct 1, 2024
658b9ff
Merge pull request #1931 from radixdlt/feature/resource-movements-sta…
0xOmarA Oct 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/everything/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/hello-world/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions radix-clis/assets/template/Cargo.lock_template

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions radix-engine-tests/tests/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod local_component;
mod metering;
mod preview;
mod stake_reconciliation;
mod static_resource_movements_visitor;
mod storage;
mod stored_external_component;
mod stored_local_component;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,337 @@
use radix_common::prelude::*;
use radix_transactions::manifest::static_resource_movements::*;
use radix_transactions::manifest::*;
use radix_transactions::prelude::*;

#[test]
fn simple_account_transfer_with_an_explicit_take_is_correctly_classified() {
// Arrange
let account1 = account_address(1);
let account2 = account_address(2);
let manifest = ManifestBuilder::new_v2()
.lock_fee_and_withdraw(account1, 100, XRD, 10)
.take_from_worktop(XRD, 10, "bucket")
.deposit(account2, "bucket")
.build();

// Act
let (deposits, withdraws) = statically_analyze(&manifest);

// Assert
assert_eq!(withdraws.len(), 1);
assert_eq!(deposits.len(), 1);
assert_eq!(
withdraws.get(&account1),
Some(&vec![AccountWithdraw::Amount(XRD, 10.into())])
);
assert_eq!(
deposits.get(&account2),
Some(&vec![AccountDeposit::KnownFungible(
XRD,
FungibleBounds {
lower: LowerFungibleBound::Amount(10.into()),
upper: UpperFungibleBound::Amount(10.into())
}
)])
);
}

#[test]
fn simple_account_transfer_with_a_take_all_is_correctly_classified() {
// Arrange
let account1 = account_address(1);
let account2 = account_address(2);
let manifest = ManifestBuilder::new_v2()
.lock_fee_and_withdraw(account1, 100, XRD, 10)
.take_all_from_worktop(XRD, "bucket")
.deposit(account2, "bucket")
.build();

// Act
let (deposits, withdraws) = statically_analyze(&manifest);

// Assert
assert_eq!(withdraws.len(), 1);
assert_eq!(deposits.len(), 1);
assert_eq!(
withdraws.get(&account1),
Some(&vec![AccountWithdraw::Amount(XRD, 10.into())])
);
assert_eq!(
deposits.get(&account2),
Some(&vec![AccountDeposit::KnownFungible(
XRD,
FungibleBounds {
lower: LowerFungibleBound::Amount(10.into()),
upper: UpperFungibleBound::Amount(10.into())
}
)])
);
}

#[test]
fn simple_account_transfer_deposit_batch_is_correctly_classified() {
// Arrange
let account1 = account_address(1);
let account2 = account_address(2);
let manifest = ManifestBuilder::new_v2()
.lock_fee_and_withdraw(account1, 100, XRD, 10)
.deposit_batch(account2, ManifestExpression::EntireWorktop)
.build();

// Act
let (deposits, withdraws) = statically_analyze(&manifest);

// Assert
assert_eq!(withdraws.len(), 1);
assert_eq!(deposits.len(), 1);
assert_eq!(
withdraws.get(&account1),
Some(&vec![AccountWithdraw::Amount(XRD, 10.into())])
);
assert_eq!(
deposits.get(&account2),
Some(&vec![
AccountDeposit::KnownFungible(
XRD,
FungibleBounds {
lower: LowerFungibleBound::Amount(10.into()),
upper: UpperFungibleBound::Amount(10.into())
}
),
AccountDeposit::Unknown(WorktopUncertaintySource::YieldFromParent)
])
);
}

#[test]
fn simple_account_transfer_of_non_fungibles_by_amount_is_classified_correctly() {
// Arrange
let account1 = account_address(1);
let account2 = account_address(2);
let non_fungible_address = non_fungible_resource_address(1);
let manifest = ManifestBuilder::new_v2()
.lock_fee_and_withdraw(account1, 100, non_fungible_address, 10)
.deposit_batch(account2, ManifestExpression::EntireWorktop)
.build();

// Act
let (deposits, withdraws) = statically_analyze(&manifest);

// Assert
assert_eq!(withdraws.len(), 1);
assert_eq!(deposits.len(), 1);
assert_eq!(
withdraws.get(&account1),
Some(&vec![AccountWithdraw::Amount(
non_fungible_address,
10.into()
)])
);
assert_eq!(
deposits.get(&account2),
Some(&vec![
AccountDeposit::KnownNonFungible(
non_fungible_address,
NonFungibleBounds {
amount_bounds: FungibleBounds::new_exact(10.into()),
id_bounds: NonFungibleIdBounds::Unknown
}
),
AccountDeposit::Unknown(WorktopUncertaintySource::YieldFromParent)
])
);
}

#[test]
fn simple_account_transfer_of_non_fungibles_by_ids_is_classified_correctly() {
// Arrange
let account1 = account_address(1);
let account2 = account_address(2);
let non_fungible_address = non_fungible_resource_address(1);
let manifest = ManifestBuilder::new_v2()
.lock_fee_and_withdraw_non_fungibles(
account1,
100,
non_fungible_address,
indexset! {
NonFungibleLocalId::integer(1)
},
)
.deposit_batch(account2, ManifestExpression::EntireWorktop)
.build();

// Act
let (deposits, withdraws) = statically_analyze(&manifest);

// Assert
assert_eq!(withdraws.len(), 1);
assert_eq!(deposits.len(), 1);
assert_eq!(
withdraws.get(&account1),
Some(&vec![AccountWithdraw::Ids(
non_fungible_address,
indexset! { NonFungibleLocalId::integer(1) }
)])
);
assert_eq!(
deposits.get(&account2),
Some(&vec![
AccountDeposit::KnownNonFungible(
non_fungible_address,
NonFungibleBounds {
amount_bounds: FungibleBounds::new_exact(1.into()),
id_bounds: NonFungibleIdBounds::FullyKnown(indexset! {
NonFungibleLocalId::integer(1)
})
}
),
AccountDeposit::Unknown(WorktopUncertaintySource::YieldFromParent)
])
);
}

#[test]
fn assertion_of_any_gives_context_to_visitor() {
// Arrange
let account = account_address(1);
let manifest = ManifestBuilder::new_v2()
.assert_worktop_contains_any(XRD)
.deposit_batch(account, ManifestExpression::EntireWorktop)
.build();

// Act
let (deposits, withdraws) = statically_analyze(&manifest);

// Assert
assert_eq!(withdraws.len(), 0);
assert_eq!(deposits.len(), 1);
assert_eq!(withdraws.get(&account), None);
assert_eq!(
deposits.get(&account),
Some(&vec![
AccountDeposit::KnownFungible(
XRD,
FungibleBounds {
lower: LowerFungibleBound::NonZero,
upper: UpperFungibleBound::Unbounded
}
),
AccountDeposit::Unknown(WorktopUncertaintySource::YieldFromParent)
])
);
}

#[test]
fn assertion_of_ids_gives_context_to_visitor() {
// Arrange
let account = account_address(1);
let non_fungible_address = non_fungible_resource_address(1);
let manifest = ManifestBuilder::new_v2()
.assert_worktop_contains_non_fungibles(
non_fungible_address,
indexset! {
NonFungibleLocalId::integer(1)
},
)
.deposit_batch(account, ManifestExpression::EntireWorktop)
.build();

// Act
let (deposits, withdraws) = statically_analyze(&manifest);

// Assert
assert_eq!(withdraws.len(), 0);
assert_eq!(deposits.len(), 1);
assert_eq!(withdraws.get(&account), None);
assert_eq!(
deposits.get(&account),
Some(&vec![
AccountDeposit::KnownNonFungible(
non_fungible_address,
NonFungibleBounds {
amount_bounds: FungibleBounds {
lower: LowerFungibleBound::Amount(1.into()),
upper: UpperFungibleBound::Unbounded,
},
id_bounds: NonFungibleIdBounds::PartiallyKnown(indexset! {
NonFungibleLocalId::integer(1)
})
}
),
AccountDeposit::Unknown(WorktopUncertaintySource::YieldFromParent)
])
);
}

#[test]
fn assertion_of_amount_gives_context_to_visitor() {
// Arrange
let account = account_address(1);
let manifest = ManifestBuilder::new_v2()
.assert_worktop_contains(XRD, 10)
.deposit_batch(account, ManifestExpression::EntireWorktop)
.build();

// Act
let (deposits, withdraws) = statically_analyze(&manifest);

// Assert
assert_eq!(withdraws.len(), 0);
assert_eq!(deposits.len(), 1);
assert_eq!(withdraws.get(&account), None);
assert_eq!(
deposits.get(&account),
Some(&vec![
AccountDeposit::KnownFungible(
XRD,
FungibleBounds {
lower: LowerFungibleBound::Amount(10.into()),
upper: UpperFungibleBound::Unbounded
}
),
AccountDeposit::Unknown(WorktopUncertaintySource::YieldFromParent)
])
);
}

fn account_address(id: u64) -> ComponentAddress {
unsafe {
ComponentAddress::new_unchecked(node_id(EntityType::GlobalPreallocatedEd25519Account, id).0)
}
}

fn component_address(id: u64) -> ComponentAddress {
unsafe { ComponentAddress::new_unchecked(node_id(EntityType::GlobalGenericComponent, id).0) }
}

fn fungible_resource_address(id: u64) -> ResourceAddress {
unsafe {
ResourceAddress::new_unchecked(node_id(EntityType::GlobalFungibleResourceManager, id).0)
}
}

fn non_fungible_resource_address(id: u64) -> ResourceAddress {
unsafe {
ResourceAddress::new_unchecked(node_id(EntityType::GlobalNonFungibleResourceManager, id).0)
}
}

fn node_id(entity_type: EntityType, id: u64) -> NodeId {
let mut bytes = hash(id.to_be_bytes()).lower_bytes::<{ NodeId::LENGTH }>();
bytes[0] = entity_type as u8;
NodeId(bytes)
}

fn statically_analyze<M: ReadableManifest>(
manifest: &M,
) -> (
IndexMap<ComponentAddress, Vec<AccountDeposit>>,
IndexMap<ComponentAddress, Vec<AccountWithdraw>>,
) {
let interpreter = StaticManifestInterpreter::new(ValidationRuleset::v1(), manifest);
let mut visitor = StaticResourceMovementsVisitor::new(true);
interpreter.interpret_or_err(&mut visitor).expect("Error");
let output = visitor.output();
(output.account_deposits(), output.account_withdraws())
}
1 change: 1 addition & 0 deletions radix-transactions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ serde = { workspace = true, optional = true }
lazy_static = { workspace = true }
strum = { workspace = true }
bech32 = { workspace = true }
paste = { workspace = true }
annotate-snippets = { version = "0.10.2"}

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions radix-transactions/src/builder/manifest_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ impl<M: BuildableManifest> ManifestBuilder<M> {
// implementing this edge case.
ManifestInstructionEffect::Invocation { .. } => {}
ManifestInstructionEffect::WorktopAssertion { .. } => {}
ManifestInstructionEffect::Verification { .. } => {}
};

(
Expand Down
Loading
Loading