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 all 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
1 change: 1 addition & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod impl_self_contained_call;
mod impl_xcm_evm_runner;
pub mod migrations;
pub mod timestamp;
pub mod types;
pub mod weights;

#[cfg(feature = "runtime-benchmarks")]
Expand Down
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
22 changes: 20 additions & 2 deletions runtime/moonbase/src/runtime_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
// 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;
//! Dynamic runtime parameters for moonbase.

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>)]
Expand All @@ -29,8 +32,23 @@ pub mod dynamic_params {
#[codec(index = 0)]
pub static FeesTreasuryProportion: Perbill = Perbill::from_percent(20);
}

#[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 },
> = BoundedU128::const_new::<{ 1 * currency::UNIT * currency::SUPPLY_FACTOR }>();
}
}

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

#[cfg(feature = "runtime-benchmarks")]
impl Default for RuntimeParameters {
fn default() -> Self {
Expand Down
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
20 changes: 17 additions & 3 deletions runtime/moonbeam/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ use sp_std::{convert::TryFrom, prelude::*};
use xcm::{VersionedAssetId, VersionedAssets, VersionedLocation, VersionedXcm};
use xcm_fee_payment_runtime_api::Error as XcmPaymentApiError;

use runtime_params::*;

#[cfg(feature = "std")]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
Expand All @@ -120,6 +122,7 @@ pub type Precompiles = MoonbeamPrecompiles<Runtime>;

pub mod asset_config;
pub mod governance;
pub mod runtime_params;
pub mod xcm_config;
use governance::councils::*;

Expand Down Expand Up @@ -345,8 +348,11 @@ where
mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pallet_balances::Pallet<R>>>,
) {
if let Some(fees) = fees_then_tips.next() {
// for fees, 80% are burned, 20% to the treasury
let (_, to_treasury) = fees.ration(80, 20);
let treasury_perbill =
runtime_params::dynamic_params::runtime_config::FeesTreasuryProportion::get();
let treasury_part = treasury_perbill.deconstruct();
let burn_part = Perbill::one().deconstruct() - treasury_part;
let (_, to_treasury) = fees.ration(burn_part, treasury_part);
// Balances pallet automatically burns dropped Credits by decreasing
// total_supply accordingly
ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(
Expand Down Expand Up @@ -1345,7 +1351,7 @@ impl pallet_randomness::Config for Runtime {
type Currency = Balances;
type BabeDataGetter = BabeDataGetter<Runtime>;
type VrfKeyLookup = AuthorMapping;
type Deposit = ConstU128<{ 1 * currency::GLMR * currency::SUPPLY_FACTOR }>;
type Deposit = runtime_params::PalletRandomnessDepositU128;
type MaxRandomWords = ConstU8<100>;
type MinBlockDelay = ConstU32<2>;
type MaxBlockDelay = ConstU32<2_000>;
Expand Down Expand Up @@ -1386,6 +1392,13 @@ impl pallet_precompile_benchmarks::Config for Runtime {
type WeightInfo = moonbeam_weights::pallet_precompile_benchmarks::WeightInfo<Runtime>;
}

impl pallet_parameters::Config for Runtime {
type AdminOrigin = EnsureRoot<AccountId>;
type RuntimeEvent = RuntimeEvent;
type RuntimeParameters = RuntimeParameters;
type WeightInfo = moonbeam_weights::pallet_parameters::WeightInfo<Runtime>;
}

construct_runtime! {
pub enum Runtime
{
Expand Down Expand Up @@ -1418,6 +1431,7 @@ construct_runtime! {
ProxyGenesisCompanion: pallet_proxy_genesis_companion::{Pallet, Config<T>} = 35,
Multisig: pallet_multisig::{Pallet, Call, Storage, Event<T>} = 36,
MoonbeamLazyMigrations: pallet_moonbeam_lazy_migrations::{Pallet, Call, Storage} = 37,
Parameters: pallet_parameters = 38,

// Has been permanently removed for safety reasons.
// Sudo: pallet_sudo::{Pallet, Call, Config<T>, Storage, Event<T>} = 40,
Expand Down
62 changes: 62 additions & 0 deletions runtime/moonbeam/src/runtime_params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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/>.

//! Dynamic runtime parameters for moonbeam.

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
#[codec(index = 0)]
pub static FeesTreasuryProportion: Perbill = Perbill::from_percent(20);
}

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

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)),
),
)
}
}
Loading
Loading