diff --git a/runtime/common/src/types.rs b/runtime/common/src/types.rs index 5fc9b9c949..38c0fc3215 100644 --- a/runtime/common/src/types.rs +++ b/runtime/common/src/types.rs @@ -29,9 +29,11 @@ impl BoundedU128 { Ok(Self(value)) } - pub fn new_or_min(value: u128) -> Self { - if value < L || value > U { + pub fn safe_new(value: u128) -> Self { + if value < L { Self(L) + } else if value > U { + Self(U) } else { Self(value) } @@ -87,14 +89,14 @@ mod tests { let bounded = BoundedU128::<1, 10>::new(11); assert_eq!(bounded, Err("Value out of bounds")); - let bounded = BoundedU128::<1, 10>::new_or_min(0); + let bounded = BoundedU128::<1, 10>::safe_new(0); assert_eq!(bounded.value(), 1); - let bounded = BoundedU128::<1, 10>::new_or_min(5); + let bounded = BoundedU128::<1, 10>::safe_new(5); assert_eq!(bounded.value(), 5); - let bounded = BoundedU128::<1, 10>::new_or_min(11); - assert_eq!(bounded.value(), 1); + let bounded = BoundedU128::<1, 10>::safe_new(11); + assert_eq!(bounded.value(), 10); } #[test] diff --git a/runtime/moonbase/src/runtime_params.rs b/runtime/moonbase/src/runtime_params.rs index 97f390e423..6310d84ebc 100644 --- a/runtime/moonbase/src/runtime_params.rs +++ b/runtime/moonbase/src/runtime_params.rs @@ -41,7 +41,7 @@ pub mod dynamic_params { pub static Deposit: BoundedU128< { 1 * currency::UNIT * currency::SUPPLY_FACTOR }, { 1_000 * currency::UNIT * currency::SUPPLY_FACTOR }, - > = BoundedU128::new_or_min(1 * currency::UNIT * currency::SUPPLY_FACTOR); + > = BoundedU128::safe_new(1 * currency::UNIT * currency::SUPPLY_FACTOR); } } diff --git a/test/suites/dev/moonbase/test-parameters/test-parameters.ts b/test/suites/dev/moonbase/test-parameters/test-parameters.ts index 1148e59d77..cbe11258ef 100644 --- a/test/suites/dev/moonbase/test-parameters/test-parameters.ts +++ b/test/suites/dev/moonbase/test-parameters/test-parameters.ts @@ -1,7 +1,7 @@ import { describeSuite, DevModeContext, expect } from "@moonwall/cli"; import "@moonbeam-network/api-augment"; import { alith } from "@moonwall/util"; - +import { fail } from "assert"; const UNIT = 1_000_000_000_000_000_000n; const RUNTIME = "MoonbaseRuntime"; @@ -83,6 +83,78 @@ describeSuite({ } testParam("RuntimeConfig", "FeesTreasuryProportion", ["Perbill", 200_000_000]); - testParam("PalletRandomness", "Deposit", ["u128", 1_000_000_000_000_000_000n * 100n]); + testParam("PalletRandomness", "Deposit", ["u128", UNIT * 100n]); + + it({ + id: `T${testCounter++} - PalletRandomness - Deposit - CustomTests`, + title: "Deposit parameter should only be accepted in bounds", + test: async () => { + const MIN = 1n * UNIT; + const MAX = 1000n * UNIT; + + // used as an acceptable value + const AVG = (MIN + MAX) / 2n; + + + const param1 = parameterType( + context, + "PalletRandomness", + "Deposit", + MIN - 1n, + ); + try { + await context.createBlock( + context + .polkadotJs() + .tx.sudo.sudo(context.polkadotJs().tx.parameters.setParameter(param1.toU8a())) + .signAsync(alith), + { allowFailures: false } + ); + fail("An extrinsic should not be created, since the parameter is invalid"); + } catch (error) { + expect(error.toString().toLowerCase()).to.contain("value out of bounds"); + } + + const param2 = parameterType( + context, + "PalletRandomness", + "Deposit", + MAX + 1n, + ); + try { + await context.createBlock( + context + .polkadotJs() + .tx.sudo.sudo(context.polkadotJs().tx.parameters.setParameter(param2.toU8a())) + .signAsync(alith), + { allowFailures: false } + ); + fail("An extrinsic should not be created, since the parameter is invalid"); + } catch (error) { + expect(error.toString().toLowerCase()).to.contain("value out of bounds"); + } + + + const param3 = parameterType( + context, + "PalletRandomness", + "Deposit", + AVG, + ); + const res3 = await context.createBlock( + context + .polkadotJs() + .tx.sudo.sudo(context.polkadotJs().tx.parameters.setParameter(param3.toU8a())) + .signAsync(alith), + { allowFailures: false } + ); + expect( + res3.result?.successful, + "An extrinsic should be created, since the parameter is valid" + ).to.be.true; + + + }, + }); }, });