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

(All Runtimes) Parametrize the deposit for pallet_randomness #2941

Merged
merged 22 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
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
3 changes: 3 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ xcm-fee-payment-runtime-api = { workspace = true }

# Parity
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }

account = { workspace = true }

Expand Down Expand Up @@ -118,6 +119,7 @@ std = [
"pallet-xcm-weight-trader/std",
"pallet-message-queue/std",
"parity-scale-codec/std",
"scale-info/std",
"precompile-utils/std",
"sp-consensus-slots/std",
"sp-core/std",
Expand Down
2 changes: 2 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ mod impl_on_charge_evm_transaction;
mod impl_self_contained_call;
mod impl_xcm_evm_runner;
pub mod migrations;
pub mod runtime_params;
pub mod timestamp;
pub mod types;
pub mod weights;

#[cfg(feature = "runtime-benchmarks")]
Expand Down
70 changes: 70 additions & 0 deletions runtime/common/src/runtime_params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2024 Moonbeam Foundation.
// This file is part of Moonbeam.

// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

/// Macro to generate runtime parameters.
#[macro_export]
macro_rules! gen_runtime_params {
TarekkMA marked this conversation as resolved.
Show resolved Hide resolved
(
UNIT: $UNIT:ident,
RuntimeConfig_TreasuryProportion: $RuntimeConfig_TreasuryProportion:expr,
PalletRandomness_Deposit: $PalletRandomness_Deposit:expr,
) => {
use crate::{currency, Runtime};
use frame_support::dynamic_params::{dynamic_pallet_params, dynamic_params};
use moonbeam_runtime_common::expose_u128_get;
use moonbeam_runtime_common::types::BoundedU128;
use sp_runtime::Perbill;

#[dynamic_params(RuntimeParameters, pallet_parameters::Parameters::<Runtime>)]
pub mod dynamic_params {
use super::*;
#[dynamic_pallet_params]
#[codec(index = 0)]
pub mod runtime_config {
// for fees, 80% are burned, 20% to the treasury
TarekkMA marked this conversation as resolved.
Show resolved Hide resolved
#[codec(index = 0)]
pub static FeesTreasuryProportion: Perbill = $RuntimeConfig_TreasuryProportion;
}

#[dynamic_pallet_params]
#[codec(index = 1)]
pub mod pallet_randomness {
#[codec(index = 0)]
pub static Deposit: BoundedU128<
{ 1 * currency::$UNIT * currency::SUPPLY_FACTOR },
{ 1_000 * currency::$UNIT * currency::SUPPLY_FACTOR },
> = $PalletRandomness_Deposit;
}
}

expose_u128_get!(
PalletRandomnessDepositU128,
dynamic_params::pallet_randomness::Deposit
);

#[cfg(feature = "runtime-benchmarks")]
impl Default for RuntimeParameters {
fn default() -> Self {
RuntimeParameters::RuntimeConfig(
dynamic_params::runtime_config::Parameters::FeesTreasuryProportion(
dynamic_params::runtime_config::FeesTreasuryProportion,
Some(Perbill::from_percent(20)),
),
)
}
}
};
}
136 changes: 136 additions & 0 deletions runtime/common/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright 2024 Moonbeam Foundation.
// This file is part of Moonbeam.

// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.
use parity_scale_codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_std::prelude::*;

#[derive(Debug, PartialEq, Eq, Clone, Copy, Encode, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(LOWER, UPPER))]
pub struct BoundedU128<const LOWER: u128, const UPPER: u128>(u128);

impl<const L: u128, const U: u128> BoundedU128<L, U> {
// Create a new instance with a value. If the value is out of bounds, it will return an error.
pub fn new(value: u128) -> Result<Self, &'static str> {
if value < L || value > U {
return Err("Value out of bounds");
}
Ok(Self(value))
}

/// Create a new instance with a constant value. If the value is out of bounds, it will be
/// clamped to the nearest bound.
pub fn const_new<const VAL: u128>() -> Self {
if VAL < L {
Self(L)
} else if VAL > U {
Self(U)
} else {
Self(VAL)
}
}

/// Get the value.
pub fn value(&self) -> u128 {
self.0
}
}

impl<const L: u128, const U: u128> Decode for BoundedU128<L, U> {
fn decode<I: parity_scale_codec::Input>(
input: &mut I,
) -> Result<Self, parity_scale_codec::Error> {
let value = u128::decode(input)?;
if value < L || value > U {
return Err("Value out of bounds".into());
}
Ok(Self(value))
}
}

impl<const L: u128, const U: u128> EncodeLike<u128> for BoundedU128<L, U> {}

/// Expose a `Get<u128>` implementation form a `Get<BoundedU128>` type.
#[macro_export]
macro_rules! expose_u128_get {
($name:ident,$bounded_get:ty) => {
pub struct $name;

impl sp_core::Get<u128> for $name {
fn get() -> u128 {
<$bounded_get>::get().value()
}
}
};
}

#[cfg(test)]
mod tests {
use frame_support::parameter_types;
use sp_core::Get;

use super::*;

#[test]
fn test_bounded_u128() {
let bounded = BoundedU128::<1, 10>::new(5).unwrap();
assert_eq!(bounded.value(), 5);

let bounded = BoundedU128::<1, 10>::new(0);
assert_eq!(bounded, Err("Value out of bounds"));

let bounded = BoundedU128::<1, 10>::new(11);
assert_eq!(bounded, Err("Value out of bounds"));

let bounded = BoundedU128::<1, 10>::const_new::<0>();
assert_eq!(bounded.value(), 1);

let bounded = BoundedU128::<1, 10>::const_new::<5>();
assert_eq!(bounded.value(), 5);

let bounded = BoundedU128::<1, 10>::const_new::<11>();
assert_eq!(bounded.value(), 10);
}

#[test]
fn test_expose_u128_get() {
parameter_types! {
pub Bounded: BoundedU128::<1, 10> = BoundedU128::<1, 10>::new(4).unwrap();
}
expose_u128_get!(Exposed, Bounded);
assert_eq!(Bounded::get().value(), Exposed::get());
}

#[test]
fn test_encode_decode() {
let bounded = BoundedU128::<1, 10>::new(5).unwrap();
let encoded = bounded.encode();
let decoded = BoundedU128::<1, 10>::decode(&mut &encoded[..]).unwrap();
assert_eq!(bounded, decoded);
}

#[test]
fn test_encode_invalid() {
let bounded = BoundedU128::<1, 10>::new(9);
let encoded = bounded.encode();
let decoded = BoundedU128::<1, 3>::decode(&mut &encoded[..]);
assert_eq!(decoded, Err("Value out of bounds".into()));

let bounded = BoundedU128::<1, 10>::new(9);
let encoded = bounded.encode();
let decoded = BoundedU128::<100, 500>::decode(&mut &encoded[..]);
assert_eq!(decoded, Err("Value out of bounds".into()));
}
}
2 changes: 1 addition & 1 deletion runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ impl pallet_randomness::Config for Runtime {
type Currency = Balances;
type BabeDataGetter = BabeDataGetter<Runtime>;
type VrfKeyLookup = AuthorMapping;
type Deposit = ConstU128<{ 1 * currency::UNIT * currency::SUPPLY_FACTOR }>;
type Deposit = runtime_params::PalletRandomnessDepositU128;
type MaxRandomWords = ConstU8<100>;
type MinBlockDelay = ConstU32<2>;
type MaxBlockDelay = ConstU32<2_000>;
Expand Down
34 changes: 8 additions & 26 deletions runtime/moonbase/src/runtime_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

//! Dynamic runtime parametes.
use crate::Runtime;
use frame_support::dynamic_params::{dynamic_pallet_params, dynamic_params};
use sp_runtime::Perbill;
//! Dynamic runtime parameters for moonbase.

#[dynamic_params(RuntimeParameters, pallet_parameters::Parameters::<Runtime>)]
pub mod dynamic_params {
use super::*;
#[dynamic_pallet_params]
#[codec(index = 0)]
pub mod runtime_config {
// for fees, 80% are burned, 20% to the treasury
#[codec(index = 0)]
pub static FeesTreasuryProportion: Perbill = Perbill::from_percent(20);
}
}
use moonbeam_runtime_common::gen_runtime_params;

#[cfg(feature = "runtime-benchmarks")]
impl Default for RuntimeParameters {
fn default() -> Self {
RuntimeParameters::RuntimeConfig(
dynamic_params::runtime_config::Parameters::FeesTreasuryProportion(
dynamic_params::runtime_config::FeesTreasuryProportion,
Some(Perbill::from_percent(20)),
),
)
}
}
gen_runtime_params!(
UNIT: UNIT,
RuntimeConfig_TreasuryProportion: Perbill::from_percent(20),
PalletRandomness_Deposit:
BoundedU128::const_new::<{ 1 * currency::UNIT * currency::SUPPLY_FACTOR }>(),
);
4 changes: 4 additions & 0 deletions runtime/moonbeam/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pallet-conviction-voting = { workspace = true }
pallet-identity = { workspace = true }
pallet-multisig = { workspace = true }
pallet-preimage = { workspace = true }
pallet-parameters = { workspace = true }
pallet-proxy = { workspace = true }
pallet-referenda = { workspace = true }
pallet-root-testing = { workspace = true }
Expand Down Expand Up @@ -272,6 +273,7 @@ std = [
"pallet-parachain-staking/std",
"pallet-precompile-benchmarks/std",
"pallet-preimage/std",
"pallet-parameters/std",
"pallet-proxy-genesis-companion/std",
"pallet-proxy/std",
"pallet-randomness/std",
Expand Down Expand Up @@ -362,6 +364,7 @@ runtime-benchmarks = [
"pallet-parachain-staking/runtime-benchmarks",
"pallet-precompile-benchmarks/runtime-benchmarks",
"pallet-preimage/runtime-benchmarks",
"pallet-parameters/runtime-benchmarks",
"pallet-proxy/runtime-benchmarks",
"pallet-randomness/runtime-benchmarks",
"pallet-referenda/runtime-benchmarks",
Expand Down Expand Up @@ -404,6 +407,7 @@ try-runtime = [
"pallet-parachain-staking/try-runtime",
"pallet-precompile-benchmarks/try-runtime",
"pallet-preimage/try-runtime",
"pallet-parameters/try-runtime",
"pallet-referenda/try-runtime",
"pallet-relay-storage-roots/try-runtime",
"pallet-root-testing/try-runtime",
Expand Down
Loading
Loading