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

Add pallet_parameters (moonbase only) #2923

Merged
merged 27 commits into from
Aug 30, 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
2 changes: 2 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ pallet-identity = { git = "https://github.com/moonbeam-foundation/polkadot-sdk",
pallet-message-queue = { git = "https://github.com/moonbeam-foundation/polkadot-sdk", branch = "moonbeam-polkadot-v1.11.0", default-features = false }
pallet-multisig = { git = "https://github.com/moonbeam-foundation/polkadot-sdk", branch = "moonbeam-polkadot-v1.11.0", default-features = false }
pallet-preimage = { git = "https://github.com/moonbeam-foundation/polkadot-sdk", branch = "moonbeam-polkadot-v1.11.0", default-features = false }
pallet-parameters = { git = "https://github.com/moonbeam-foundation/polkadot-sdk", branch = "moonbeam-polkadot-v1.11.0", default-features = false }
pallet-proxy = { git = "https://github.com/moonbeam-foundation/polkadot-sdk", branch = "moonbeam-polkadot-v1.11.0", default-features = false }
pallet-referenda = { git = "https://github.com/moonbeam-foundation/polkadot-sdk", branch = "moonbeam-polkadot-v1.11.0", default-features = false }
pallet-root-testing = { git = "https://github.com/moonbeam-foundation/polkadot-sdk", branch = "moonbeam-polkadot-v1.11.0", default-features = false }
Expand Down
3 changes: 3 additions & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pallet-identity = { workspace = true }
pallet-moonbeam-orbiters = { workspace = true }
pallet-multisig = { workspace = true }
pallet-preimage = { workspace = true }
pallet-parameters = { workspace = true }
pallet-proxy = { workspace = true }
pallet-referenda = { workspace = true }
pallet-scheduler = { workspace = true }
Expand Down Expand Up @@ -100,6 +101,7 @@ std = [
"pallet-evm/std",
"pallet-migrations/std",
"pallet-parachain-staking/std",
"pallet-parameters/std",
"pallet-randomness/std",
"pallet-referenda/std",
"pallet-scheduler/std",
Expand Down Expand Up @@ -139,6 +141,7 @@ runtime-benchmarks = [
"pallet-multisig/runtime-benchmarks",
"pallet-parachain-staking/runtime-benchmarks",
"pallet-preimage/runtime-benchmarks",
"pallet-parameters/runtime-benchmarks",
"pallet-proxy/runtime-benchmarks",
"pallet-randomness/runtime-benchmarks",
"pallet-referenda/runtime-benchmarks",
Expand Down
7 changes: 7 additions & 0 deletions runtime/common/src/apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,13 @@ macro_rules! impl_runtime_apis_plus_common {
hex_literal::hex!( "0d715f2646c8f85767b5d2764bb27826"
"04a74d81251e398fd8a0a4d55023bb3f")
.to_vec().into(),
// Parameters Parameters
hex_literal::hex!( "c63bdd4a39095ccf55623a6f2872bf8a" // Pallet: "Parameters"
"c63bdd4a39095ccf55623a6f2872bf8a" // Storage Prefix: "Parameters"
// MoonbaseRuntimeRuntimeParamsRuntimeParametersKey(FeesTreasuryProportion)
"71d0aacb690b61280d0c97c6b6a666640000"
)
.to_vec().into(),

];

Expand Down
1 change: 1 addition & 0 deletions runtime/common/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub mod pallet_moonbeam_lazy_migrations;
pub mod pallet_moonbeam_orbiters;
pub mod pallet_multisig;
pub mod pallet_parachain_staking;
pub mod pallet_parameters;
pub mod pallet_precompile_benchmarks;
pub mod pallet_preimage;
pub mod pallet_proxy;
Expand Down
60 changes: 60 additions & 0 deletions runtime/common/src/weights/pallet_parameters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2019-2022 PureStake Inc.
// 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/>.

//! Autogenerated weights for `pallet_parameters`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 32.0.0
//! DATE: 2024-08-06, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `COV0706`, CPU: `AMD Ryzen 9 7950X 16-Core Processor`
//! WASM-EXECUTION: Compiled, CHAIN: Some("moonbase-dev"), DB CACHE: 1024

// Executed Command:
// ./target/release/moonbeam
// benchmark
// pallet
// --chain=moonbase-dev
// --steps=50
// --repeat=20
// --pallet=pallet_parameters
// --extrinsic=*
// --wasm-execution=compiled
// --header=./file_header.txt
// --template=./benchmarking/frame-weight-template.hbs
// --output=./runtime/common/src/weights/

#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use sp_std::marker::PhantomData;

/// Weights for `pallet_parameters`.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> pallet_parameters::WeightInfo for WeightInfo<T> {
/// Storage: `Parameters::Parameters` (r:1 w:1)
/// Proof: `Parameters::Parameters` (`max_values`: None, `max_size`: Some(36), added: 2511, mode: `MaxEncodedLen`)
fn set_parameter() -> Weight {
// Proof Size summary in bytes:
// Measured: `3`
// Estimated: `3501`
// Minimum execution time: 5_480_000 picoseconds.
Weight::from_parts(5_610_000, 3501)
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
}
4 changes: 4 additions & 0 deletions runtime/moonbase/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,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 @@ -274,6 +275,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 @@ -365,6 +367,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 @@ -406,6 +409,7 @@ try-runtime = [
"pallet-moonbeam-lazy-migrations/try-runtime",
"pallet-parachain-staking/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
19 changes: 17 additions & 2 deletions runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

pub mod asset_config;
pub mod governance;
pub mod runtime_params;
pub mod xcm_config;

mod migrations;
Expand Down Expand Up @@ -125,6 +126,8 @@ use xcm_config::AssetType;
use xcm_fee_payment_runtime_api::Error as XcmPaymentApiError;
use xcm_primitives::UnitsToWeightRatio;

use runtime_params::*;

use smallvec::smallvec;
use sp_runtime::serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -348,8 +351,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 @@ -1379,6 +1385,13 @@ impl pallet_precompile_benchmarks::Config for Runtime {
type WeightInfo = moonbase_weights::pallet_precompile_benchmarks::WeightInfo<Runtime>;
}

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

construct_runtime! {
pub enum Runtime
{
Expand Down Expand Up @@ -1441,6 +1454,7 @@ construct_runtime! {
MessageQueue: pallet_message_queue::{Pallet, Call, Storage, Event<T>} = 54,
EmergencyParaXcm: pallet_emergency_para_xcm::{Pallet, Call, Storage, Event} = 55,
EvmForeignAssets: pallet_moonbeam_foreign_assets::{Pallet, Call, Storage, Event<T>} = 56,
Parameters: pallet_parameters = 57,
}
}

Expand Down Expand Up @@ -1518,6 +1532,7 @@ mod benches {
[pallet_relay_storage_roots, RelayStorageRoots]
[pallet_precompile_benchmarks, PrecompileBenchmarks]
[pallet_moonbeam_lazy_migrations, MoonbeamLazyMigrations]
[pallet_parameters, Parameters]
);
}

Expand Down
44 changes: 44 additions & 0 deletions runtime/moonbase/src/runtime_params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 parametes.
use crate::Runtime;
use frame_support::dynamic_params::{dynamic_pallet_params, dynamic_params};
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);
}
}

#[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)),
),
)
}
}
87 changes: 87 additions & 0 deletions test/suites/dev/moonbase/test-parameters/test-parameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { describeSuite, DevModeContext, expect } from "@moonwall/cli";
import "@moonbeam-network/api-augment";
import { alith } from "@moonwall/util";

const UNIT = 1_000_000_000_000_000_000n;

const RUNTIME = "MoonbaseRuntime";
const CRATE = "RuntimeParams";
const ALL_PARAMS = "DynamicParams";

function parameterType(context: DevModeContext, module: string, name: string, value: unknown) {
const paramWrapper = context
.polkadotJs()
.createType(`${RUNTIME}${CRATE}${ALL_PARAMS}${module}Parameters`, {
[name]: [null, value],
});

const runtimeParameter = context.polkadotJs().createType(`${RUNTIME}${CRATE}RuntimeParameters`, {
[module]: paramWrapper,
});

return runtimeParameter;
}

function parameterKey(context: DevModeContext, module: string, name: string) {
const key = context
.polkadotJs()
.createType(`${RUNTIME}${CRATE}${ALL_PARAMS}${module}ParametersKey`, {
[name]: null,
});

const keyWrapper = context.polkadotJs().createType(`${RUNTIME}${CRATE}RuntimeParametersKey`, {
[module]: key,
});

return keyWrapper;
}

describeSuite({
id: "DTemp01",
title: "Parameters",
foundationMethods: "dev",
testCases: ({ it, context, log }) => {
let testCounter = 0;
function testParam(module: string, name: string, valueCreation: [string, unknown]) {
it({
id: `T${testCounter++} - ${module} - ${name}`,
title: "Parameters cannot be changed by normal user",
test: async () => {
const value = context.polkadotJs().createType(valueCreation[0], valueCreation[1]);
const param = parameterType(context, module, name, value);

const res = await context.createBlock(
context.polkadotJs().tx.parameters.setParameter(param.toU8a()).signAsync(alith),
{ allowFailures: true }
);
expect(res.result?.error?.name).toEqual("BadOrigin");
},
});

it({
id: `T${testCounter++} - ${module} - ${name}`,
title: "Parameters can be changed by root user",
test: async () => {
const value = context.polkadotJs().createType(valueCreation[0], valueCreation[1]);
const param = parameterType(context, module, name, value);

await context.createBlock(
context
.polkadotJs()
.tx.sudo.sudo(context.polkadotJs().tx.parameters.setParameter(param.toU8a()))
.signAsync(alith),
{ allowFailures: false }
);

const key = parameterKey(context, module, name);

const wrappedValue = await context.polkadotJs().query.parameters.parameters(key.toU8a());
const gotValue = wrappedValue.value.value.value.toU8a();
expect(gotValue).toEqual(value.toU8a());
},
});
}

testParam("RuntimeConfig", "FeesTreasuryProportion", ["Perbill", 200_000_000]);
},
});
Loading