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 all 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.

8 changes: 8 additions & 0 deletions radix-common/src/crypto/public_key_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ pub trait IsPublicKeyHash: Copy {
fn into_enum(self) -> PublicKeyHash;
}

impl<H: IsPublicKeyHash> HasPublicKeyHash for H {
type TypedPublicKeyHash = Self;

fn get_hash(&self) -> Self::TypedPublicKeyHash {
*self
}
}

pub fn hash_public_key_bytes<T: AsRef<[u8]>>(key_bytes: T) -> [u8; NodeId::RID_LENGTH] {
hash(key_bytes).lower_bytes()
}
Expand Down
150 changes: 145 additions & 5 deletions radix-common/src/data/manifest/model/manifest_expression.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,161 @@
use crate::internal_prelude::*;
#[cfg(feature = "fuzzing")]
use arbitrary::Arbitrary;
use sbor::rust::convert::TryFrom;
#[cfg(not(feature = "alloc"))]
use sbor::rust::fmt;
use sbor::rust::vec::Vec;
use sbor::*;

use crate::data::manifest::*;
use crate::*;

#[cfg_attr(feature = "fuzzing", derive(Arbitrary))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ManifestExpression {
/// Can be encoded into [`BucketBatch`]
EntireWorktop,
/// Can be encoded into [`ProofBatch`]
EntireAuthZone,
}

//========
// Alternative Representations
//========

#[cfg_attr(feature = "fuzzing", derive(Arbitrary))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum BucketBatch {
ManifestBuckets(Vec<ManifestBucket>),
EntireWorktop,
}

impl BucketBatch {
pub fn from_buckets(buckets: impl IntoIterator<Item = ManifestBucket>) -> Self {
Self::ManifestBuckets(buckets.into_iter().collect())
}
}

impl<E: sbor::Encoder<ManifestCustomValueKind>> sbor::Encode<ManifestCustomValueKind, E>
for BucketBatch
{
#[inline]
fn encode_value_kind(&self, encoder: &mut E) -> Result<(), sbor::EncodeError> {
match self {
BucketBatch::ManifestBuckets(buckets) => buckets.encode_value_kind(encoder),
BucketBatch::EntireWorktop => {
ManifestExpression::EntireWorktop.encode_value_kind(encoder)
}
}
}

#[inline]
fn encode_body(&self, encoder: &mut E) -> Result<(), sbor::EncodeError> {
match self {
BucketBatch::ManifestBuckets(buckets) => buckets.encode_body(encoder),
BucketBatch::EntireWorktop => ManifestExpression::EntireWorktop.encode_body(encoder),
}
}
}

impl<D: sbor::Decoder<ManifestCustomValueKind>> sbor::Decode<ManifestCustomValueKind, D>
for BucketBatch
{
fn decode_body_with_value_kind(
decoder: &mut D,
value_kind: sbor::ValueKind<ManifestCustomValueKind>,
) -> Result<Self, sbor::DecodeError> {
Ok(match value_kind {
ValueKind::Array => Self::ManifestBuckets(
Vec::<ManifestBucket>::decode_body_with_value_kind(decoder, value_kind)?,
),
ValueKind::Custom(_) => {
let expression =
ManifestExpression::decode_body_with_value_kind(decoder, value_kind)?;
if !matches!(expression, ManifestExpression::EntireWorktop) {
return Err(sbor::DecodeError::InvalidCustomValue);
}
Self::EntireWorktop
}
_ => {
return Err(sbor::DecodeError::UnexpectedValueKind {
expected: ManifestValueKind::Array.as_u8(),
actual: value_kind.as_u8(),
});
}
})
}
}

impl sbor::Describe<ScryptoCustomTypeKind> for BucketBatch {
const TYPE_ID: sbor::RustTypeId = Vec::<ManifestBucket>::TYPE_ID;

fn type_data() -> sbor::TypeData<ScryptoCustomTypeKind, sbor::RustTypeId> {
Vec::<ManifestBucket>::type_data()
}
}

#[cfg_attr(feature = "fuzzing", derive(Arbitrary))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ProofBatch {
ManifestProofs(Vec<ManifestProof>),
EntireAuthZone,
}

impl<E: sbor::Encoder<ManifestCustomValueKind>> sbor::Encode<ManifestCustomValueKind, E>
for ProofBatch
{
#[inline]
fn encode_value_kind(&self, encoder: &mut E) -> Result<(), sbor::EncodeError> {
match self {
ProofBatch::ManifestProofs(proofs) => proofs.encode_value_kind(encoder),
ProofBatch::EntireAuthZone => {
ManifestExpression::EntireAuthZone.encode_value_kind(encoder)
}
}
}

#[inline]
fn encode_body(&self, encoder: &mut E) -> Result<(), sbor::EncodeError> {
match self {
ProofBatch::ManifestProofs(proofs) => proofs.encode_body(encoder),
ProofBatch::EntireAuthZone => ManifestExpression::EntireAuthZone.encode_body(encoder),
}
}
}

impl<D: sbor::Decoder<ManifestCustomValueKind>> sbor::Decode<ManifestCustomValueKind, D>
for ProofBatch
{
fn decode_body_with_value_kind(
decoder: &mut D,
value_kind: sbor::ValueKind<ManifestCustomValueKind>,
) -> Result<Self, sbor::DecodeError> {
Ok(match value_kind {
ValueKind::Array => Self::ManifestProofs(
Vec::<ManifestProof>::decode_body_with_value_kind(decoder, value_kind)?,
),
ValueKind::Custom(_) => {
let expression =
ManifestExpression::decode_body_with_value_kind(decoder, value_kind)?;
if !matches!(expression, ManifestExpression::EntireAuthZone) {
return Err(sbor::DecodeError::InvalidCustomValue);
}
Self::EntireAuthZone
}
_ => {
return Err(sbor::DecodeError::UnexpectedValueKind {
expected: ManifestValueKind::Array.as_u8(),
actual: value_kind.as_u8(),
});
}
})
}
}

impl sbor::Describe<ScryptoCustomTypeKind> for ProofBatch {
const TYPE_ID: sbor::RustTypeId = Vec::<ManifestProof>::TYPE_ID;

fn type_data() -> sbor::TypeData<ScryptoCustomTypeKind, sbor::RustTypeId> {
Vec::<ManifestProof>::type_data()
}
}

//========
// error
//========
Expand Down
9 changes: 9 additions & 0 deletions radix-common/src/math/bnum_integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,15 @@ macro_rules! op_impl {
}
}

impl SaturatingAdd for $t
{
type Output = $t;

fn saturating_add(self, other: Self) -> Self::Output {
Self(self.0.saturating_add(other.0))
}
}

impl CheckedSub for $t
{
type Output = $t;
Expand Down
13 changes: 13 additions & 0 deletions radix-common/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ impl Decimal {
pub const TEN: Self = Self(I192::from_digits([10_u64.pow(Decimal::SCALE + 1), 0, 0]));
pub const ONE_HUNDRED: Self = Self(I192::from_digits([7766279631452241920, 0x5, 0]));

pub const fn from_attos(attos: I192) -> Self {
Self(attos)
}

/// Returns `Decimal` of 0.
pub const fn zero() -> Self {
Self::ZERO
Expand Down Expand Up @@ -364,6 +368,15 @@ impl CheckedAdd<Decimal> for Decimal {
}
}

impl SaturatingAdd<Decimal> for Decimal {
type Output = Self;

#[inline]
fn saturating_add(self, other: Self) -> Self::Output {
Self(self.0.saturating_add(other.0))
}
}

impl CheckedSub<Decimal> for Decimal {
type Output = Self;

Expand Down
8 changes: 8 additions & 0 deletions radix-common/src/math/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ pub trait CheckedAdd<Rhs = Self> {
Self: Sized;
}

pub trait SaturatingAdd<Rhs = Self> {
type Output;

fn saturating_add(self, other: Rhs) -> Self::Output
where
Self: Sized;
}

pub trait CheckedSub<Rhs = Self> {
type Output;

Expand Down
9 changes: 9 additions & 0 deletions radix-common/src/types/entity_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ impl EntityType {
matches!(self, EntityType::GlobalPackage)
}

pub const fn is_global_account(&self) -> bool {
matches!(
self,
EntityType::GlobalAccount
| EntityType::GlobalPreallocatedSecp256k1Account
| EntityType::GlobalPreallocatedEd25519Account
)
}

pub const fn is_global_consensus_manager(&self) -> bool {
matches!(self, EntityType::GlobalConsensusManager)
}
Expand Down
4 changes: 4 additions & 0 deletions radix-common/src/types/node_and_substate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ impl NodeId {
matches!(self.entity_type(), Some(t) if t.is_global_component())
}

pub const fn is_global_account(&self) -> bool {
matches!(self.entity_type(), Some(t) if t.is_global_account())
}

pub const fn is_global_package(&self) -> bool {
matches!(self.entity_type(), Some(t) if t.is_global_package())
}
Expand Down
4 changes: 2 additions & 2 deletions radix-common/src/types/non_fungible_global_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@ pub trait FromPublicKey: Sized {
}

impl FromPublicKey for NonFungibleGlobalId {
/// Prefer using the `signature` function or the `signature_proof()` method.
/// Prefer using the `signature(public_key)` or `public_key.signature_proof()`.
fn from_public_key<P: HasPublicKeyHash>(public_key: &P) -> Self {
Self::from_public_key_hash(public_key.get_hash())
}

/// Prefer using the `signature` function or the `signature_proof()` method.
/// Prefer using the `signature(public_key_hash)` function or the `public_key_hash.signature_proof()` method.
fn from_public_key_hash<P: IsPublicKeyHash>(public_key_hash: P) -> Self {
match public_key_hash.into_enum() {
PublicKeyHash::Secp256k1(public_key_hash) => NonFungibleGlobalId::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub struct AccountDepositBatchInput {

#[derive(Debug, Eq, PartialEq, ManifestSbor)]
pub struct AccountDepositBatchManifestInput {
pub buckets: Vec<ManifestBucket>,
pub buckets: BucketBatch,
}

pub type AccountDepositBatchOutput = ();
Expand Down Expand Up @@ -301,7 +301,7 @@ pub struct AccountTryDepositBatchOrRefundInput {

#[derive(Debug, Eq, PartialEq, ManifestSbor)]
pub struct AccountTryDepositBatchOrRefundManifestInput {
pub buckets: Vec<ManifestBucket>,
pub buckets: BucketBatch,
pub authorized_depositor_badge: Option<ResourceOrNonFungible>,
}

Expand Down Expand Up @@ -341,7 +341,7 @@ pub struct AccountTryDepositBatchOrAbortInput {

#[derive(Debug, Eq, PartialEq, ManifestSbor)]
pub struct AccountTryDepositBatchOrAbortManifestInput {
pub buckets: Vec<ManifestBucket>,
pub buckets: BucketBatch,
pub authorized_depositor_badge: Option<ResourceOrNonFungible>,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ define_invocation! {
},
output: type (Bucket, Vec<Bucket>),
manifest_input: struct {
buckets: Vec<ManifestBucket>
buckets: BucketBatch
}
}

Expand Down
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
Loading
Loading