Skip to content

Commit

Permalink
tweak: Standardize resource assertion errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dhedey committed Oct 25, 2024
1 parent 2e0cd66 commit 950d185
Show file tree
Hide file tree
Showing 13 changed files with 394 additions and 359 deletions.
346 changes: 211 additions & 135 deletions radix-common/src/data/manifest/model/manifest_resource_assertion.rs

Large diffs are not rendered by default.

33 changes: 2 additions & 31 deletions radix-common/src/math/bnum_integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use num_bigint::BigInt;
use num_integer::Roots;
use num_traits::{FromPrimitive, One, Pow, ToPrimitive, Zero};
use paste::paste;
use sbor::rust::cmp::{Ord, Ordering, PartialEq, PartialOrd};
use sbor::rust::cmp::{Ord, PartialEq, PartialOrd};
use sbor::rust::convert::{From, TryFrom};
use sbor::rust::fmt;
use sbor::rust::ops::{Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign};
Expand Down Expand Up @@ -48,7 +48,7 @@ macro_rules! types {
#[doc = "`" $t "` will have the same methods and traits as"]
/// the built-in counterpart.
#[cfg_attr(feature = "fuzzing", derive(Arbitrary, Serialize, Deserialize))]
#[derive(Clone , Copy)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
#[repr(transparent)]
pub struct $t(pub $wrap);

Expand Down Expand Up @@ -100,35 +100,6 @@ macro_rules! types {
Self::ONE
}
}

impl Ord for $t {
fn cmp(&self, other: &Self) -> Ordering {
self.0.cmp(&other.0)
}
}

impl PartialOrd for $t {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

// The following three trait implementations must be aligned.

impl PartialEq for $t {
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}

impl Eq for $t {
}

impl sbor::rust::hash::Hash for $t {
fn hash<H>(&self, state: &mut H) where H: sbor::rust::hash::Hasher {
self.0.hash(state)
}
}
)*
}
};
Expand Down
2 changes: 2 additions & 0 deletions radix-common/src/math/bnum_integer/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use paste::paste;
use num_bigint::{BigInt, Sign};

use radix_common::*;
#[allow(unused_imports)] // It's needed by the `test_impl!` macro
use sbor::rust::cmp::Ordering;

test_impl! {I192, I256, I320, I384, I448, I512, I768}
test_impl! {U192, U256, U320, U384, U448, U512, U768}
Expand Down
76 changes: 27 additions & 49 deletions radix-engine-tests/tests/system/assert_bucket_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn asserting_incorrect_exact_amount_should_fail() {
ManifestResourceConstraint::ExactAmount(expected_exact_amount),
Some(ResourceConstraintError::ExpectedExactAmount {
actual_amount,
expected_exact_amount,
expected_amount: expected_exact_amount,
}),
)
}
Expand Down Expand Up @@ -79,14 +79,8 @@ fn asserting_incorrect_at_least_non_fungibles_should_fail() {
test_non_fungible_constraint(
actual_ids.clone(),
ManifestResourceConstraint::AtLeastNonFungibles(expected_at_least_ids.clone()),
Some(ResourceConstraintError::ExpectedAtLeastNonFungibles {
actual_ids: Box::new(
actual_ids
.into_iter()
.map(NonFungibleLocalId::from)
.collect(),
),
expected_at_least_ids: Box::new(expected_at_least_ids),
Some(ResourceConstraintError::NonFungibleMissing {
missing_id: NonFungibleLocalId::from(2),
}),
)
}
Expand Down Expand Up @@ -115,14 +109,8 @@ fn asserting_incorrect_exact_non_fungibles_should_fail() {
test_non_fungible_constraint(
actual_ids.clone(),
ManifestResourceConstraint::ExactNonFungibles(expected_exact_ids.clone()),
Some(ResourceConstraintError::ExpectedExactNonFungibles {
actual_ids: Box::new(
actual_ids
.into_iter()
.map(NonFungibleLocalId::from)
.collect(),
),
expected_exact_ids: Box::new(expected_exact_ids),
Some(ResourceConstraintError::NonFungibleMissing {
missing_id: NonFungibleLocalId::from(3),
}),
)
}
Expand Down Expand Up @@ -203,19 +191,17 @@ fn asserting_incorrect_fungible_lower_bound_general_constraint_should_fail() {
let lower_bound = dec!(10) + Decimal::from_attos(I192::ONE);
let constraint = GeneralResourceConstraint {
required_ids: Default::default(),
allowed_ids: AllowedIds::Any,
lower_bound: LowerBound::Inclusive(lower_bound),
upper_bound: UpperBound::Unbounded,
allowed_ids: AllowedIds::Any,
};
test_fungible_constraint(
amount,
ManifestResourceConstraint::General(constraint),
Some(ResourceConstraintError::GeneralResourceConstraintError(
GeneralResourceConstraintError::LowerBoundAmountNotSatisfied {
actual: amount,
lower_bound_inclusive: lower_bound,
},
)),
Some(ResourceConstraintError::ExpectedAtLeastAmount {
expected_at_least_amount: lower_bound,
actual_amount: amount,
}),
);
}

Expand All @@ -225,19 +211,17 @@ fn asserting_incorrect_fungible_upper_bound_general_constraint_should_fail() {
let upper_bound = dec!(10) - Decimal::from_attos(I192::ONE);
let constraint = GeneralResourceConstraint {
required_ids: Default::default(),
allowed_ids: AllowedIds::Any,
lower_bound: LowerBound::Inclusive(Decimal::zero()),
upper_bound: UpperBound::Inclusive(upper_bound),
allowed_ids: AllowedIds::Any,
};
test_fungible_constraint(
amount,
ManifestResourceConstraint::General(constraint),
Some(ResourceConstraintError::GeneralResourceConstraintError(
GeneralResourceConstraintError::UpperBoundAmountNotSatisfied {
actual: amount,
upper_bound_inclusive: upper_bound,
},
)),
Some(ResourceConstraintError::ExpectedAtMostAmount {
expected_at_most_amount: upper_bound,
actual_amount: amount,
}),
);
}

Expand All @@ -246,18 +230,16 @@ fn asserting_incorrect_non_fungible_required_ids_general_constraint_should_fail(
let actual_ids = vec![1, 2];
let constraint = GeneralResourceConstraint {
required_ids: indexset!(NonFungibleLocalId::from(3)),
allowed_ids: AllowedIds::Any,
lower_bound: LowerBound::Inclusive(Decimal::from(1)),
upper_bound: UpperBound::Unbounded,
allowed_ids: AllowedIds::Any,
};
test_non_fungible_constraint(
actual_ids.clone(),
ManifestResourceConstraint::General(constraint),
Some(ResourceConstraintError::GeneralResourceConstraintError(
GeneralResourceConstraintError::MissingRequiredNonFungible {
missing_id: NonFungibleLocalId::from(3),
},
)),
Some(ResourceConstraintError::NonFungibleMissing {
missing_id: NonFungibleLocalId::from(3),
}),
);
}

Expand All @@ -266,21 +248,19 @@ fn asserting_incorrect_non_fungible_allowed_ids_general_constraint_should_fail()
let actual_ids = vec![1, 3];
let constraint = GeneralResourceConstraint {
required_ids: indexset!(),
lower_bound: LowerBound::NonZero,
upper_bound: UpperBound::Inclusive(Decimal::from(2)),
allowed_ids: AllowedIds::Allowlist(indexset!(
NonFungibleLocalId::from(3),
NonFungibleLocalId::from(4)
)),
lower_bound: LowerBound::NonZero,
upper_bound: UpperBound::Inclusive(Decimal::from(2)),
};
test_non_fungible_constraint(
actual_ids.clone(),
ManifestResourceConstraint::General(constraint),
Some(ResourceConstraintError::GeneralResourceConstraintError(
GeneralResourceConstraintError::InvalidNonFungible {
invalid_id: NonFungibleLocalId::from(1),
},
)),
Some(ResourceConstraintError::NonFungibleNotAllowed {
disallowed_id: NonFungibleLocalId::from(1),
}),
);
}

Expand All @@ -295,9 +275,9 @@ fn asserting_correct_empty_bucket_general_constraints_should_succeed() {
for allowed_ids in &allowed_ids_list {
let constraint = GeneralResourceConstraint {
required_ids: Default::default(),
allowed_ids: allowed_ids.clone(),
lower_bound: LowerBound::Inclusive(amount),
upper_bound,
allowed_ids: allowed_ids.clone(),
};
test_fungible_constraint(
amount,
Expand All @@ -313,16 +293,14 @@ fn asserting_incorrect_empty_bucket_lower_bound_general_constraint_should_fail()
let amount = dec!(0);
let constraint = GeneralResourceConstraint {
required_ids: Default::default(),
allowed_ids: AllowedIds::Any,
lower_bound: LowerBound::NonZero,
upper_bound: UpperBound::Unbounded,
allowed_ids: AllowedIds::Any,
};
test_fungible_constraint(
amount,
ManifestResourceConstraint::General(constraint),
Some(ResourceConstraintError::GeneralResourceConstraintError(
GeneralResourceConstraintError::ExpectedNonZeroAmount,
)),
Some(ResourceConstraintError::ExpectedNonZeroAmount),
);
}

Expand Down
39 changes: 20 additions & 19 deletions radix-engine-tests/tests/system/assert_next_call_returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ fn when_more_is_returned_assert_next_call_returns_only_should_fail() {
true,
next_call_type,
|_, resource2| {
Some(ManifestResourceConstraintsError::UnwantedResourcesExist(
resource2,
))
Some(
ResourceConstraintsError::UnexpectedNonZeroBalanceOfUnspecifiedResource {
resource_address: resource2,
},
)
},
);
}
Expand Down Expand Up @@ -104,12 +106,13 @@ fn when_less_is_returned_assert_next_call_returns_include_should_fail() {
false,
next_call_type,
|_, _| {
Some(ManifestResourceConstraintsError::ResourceConstraint(
ResourceConstraintError::ExpectedAtLeastAmount {
Some(ResourceConstraintsError::ResourceConstraintFailed {
resource_address: XRD,
error: ResourceConstraintError::ExpectedAtLeastAmount {
expected_at_least_amount: dec!(1),
actual_amount: dec!(0),
},
))
})
},
);
}
Expand All @@ -129,13 +132,12 @@ fn when_less_is_returned_assert_next_call_returns_only_should_fail() {
},
true,
next_call_type,
|_, _| {
Some(ManifestResourceConstraintsError::ResourceConstraint(
ResourceConstraintError::ExpectedAtLeastAmount {
expected_at_least_amount: dec!(1),
actual_amount: dec!(0),
|_, resource2| {
Some(
ResourceConstraintsError::UnexpectedNonZeroBalanceOfUnspecifiedResource {
resource_address: resource2,
},
))
)
},
);
}
Expand All @@ -161,9 +163,11 @@ fn when_empty_constraints_on_assert_next_call_returns_only_should_fail() {
true,
next_call_type,
|resource1, _resource2| {
Some(ManifestResourceConstraintsError::UnwantedResourcesExist(
resource1,
))
Some(
ResourceConstraintsError::UnexpectedNonZeroBalanceOfUnspecifiedResource {
resource_address: resource1,
},
)
},
);
}
Expand Down Expand Up @@ -275,10 +279,7 @@ fn run_return_two_resources_test(
constraints: fn(ResourceAddress, ResourceAddress) -> ManifestResourceConstraints,
exact: bool,
next_call_type: NextCallType,
expected_result: fn(
ResourceAddress,
ResourceAddress,
) -> Option<ManifestResourceConstraintsError>,
expected_result: fn(ResourceAddress, ResourceAddress) -> Option<ResourceConstraintsError>,
) {
// Arrange
let mut ledger = LedgerSimulatorBuilder::new().build();
Expand Down
Loading

0 comments on commit 950d185

Please sign in to comment.