[FRAME] Parameters pallet (#2061)

Closes #169  

Fork of the `orml-parameters-pallet` as introduced by
https://github.com/open-web3-stack/open-runtime-module-library/pull/927
(cc @xlc)
It greatly changes how the macros work, but keeps the pallet the same.
The downside of my code is now that it does only support constant keys
in the form of types, not value-bearing keys.
I think this is an acceptable trade off, give that it can be used by
*any* pallet without any changes.

The pallet allows to dynamically set parameters that can be used in
pallet configs while also restricting the updating on a per-key basis.
The rust-docs contains a complete example.

Changes:
- Add `parameters-pallet`
- Use in the kitchensink as demonstration
- Add experimental attribute to define dynamic params in the runtime.
- Adding a bunch of traits to `frame_support::traits::dynamic_params`
that can be re-used by the ORML macros

## Example

First to define the parameters in the runtime file. The syntax is very
explicit about the codec index and errors if there is no.
```rust
#[dynamic_params(RuntimeParameters, pallet_parameters::Parameters::<Runtime>))]
pub mod dynamic_params {
	use super::*;

	#[dynamic_pallet_params]
	#[codec(index = 0)]
	pub mod storage {
		/// Configures the base deposit of storing some data.
		#[codec(index = 0)]
		pub static BaseDeposit: Balance = 1 * DOLLARS;

		/// Configures the per-byte deposit of storing some data.
		#[codec(index = 1)]
		pub static ByteDeposit: Balance = 1 * CENTS;
	}

	#[dynamic_pallet_params]
	#[codec(index = 1)]
	pub mod contracts {
		#[codec(index = 0)]
		pub static DepositPerItem: Balance = deposit(1, 0);

		#[codec(index = 1)]
		pub static DepositPerByte: Balance = deposit(0, 1);
	}
}
```

Then the pallet is configured with the aggregate:  
```rust
impl pallet_parameters::Config for Runtime {
	type AggregratedKeyValue = RuntimeParameters;
	type AdminOrigin = EnsureRootWithSuccess<AccountId, ConstBool<true>>;
	...
}
```

And then the parameters can be used in a pallet config:
```rust
impl pallet_preimage::Config for Runtime {
	type DepositBase = dynamic_params::storage::DepositBase;
}
```

A custom origin an be defined like this:  
```rust
pub struct DynamicParametersManagerOrigin;

impl EnsureOriginWithArg<RuntimeOrigin, RuntimeParametersKey> for DynamicParametersManagerOrigin {
	type Success = ();

	fn try_origin(
		origin: RuntimeOrigin,
		key: &RuntimeParametersKey,
	) -> Result<Self::Success, RuntimeOrigin> {
		match key {
			RuntimeParametersKey::Storage(_) => {
				frame_system::ensure_root(origin.clone()).map_err(|_| origin)?;
				return Ok(())
			},
			RuntimeParametersKey::Contract(_) => {
				frame_system::ensure_root(origin.clone()).map_err(|_| origin)?;
				return Ok(())
			},
		}
	}

	#[cfg(feature = "runtime-benchmarks")]
	fn try_successful_origin(_key: &RuntimeParametersKey) -> Result<RuntimeOrigin, ()> {
		Ok(RuntimeOrigin::Root)
	}
}
```

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Nikhil Gupta <17176722+gupnik@users.noreply.github.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: command-bot <>
This commit is contained in:
Oliver Tale-Yazdi
2024-02-08 19:47:04 +01:00
committed by GitHub
parent aac07af03c
commit e53ebd8cd4
21 changed files with 2017 additions and 18 deletions
+53 -1
View File
@@ -18,12 +18,14 @@
//! Proc macro of Support code for the runtime.
#![recursion_limit = "512"]
#![deny(rustdoc::broken_intra_doc_links)]
mod benchmark;
mod construct_runtime;
mod crate_version;
mod derive_impl;
mod dummy_part_checker;
mod dynamic_params;
mod key_prefix;
mod match_and_insert;
mod no_bound;
@@ -890,12 +892,13 @@ pub fn inject_runtime_type(_: TokenStream, tokens: TokenStream) -> TokenStream {
item.ident != "RuntimeOrigin" &&
item.ident != "RuntimeHoldReason" &&
item.ident != "RuntimeFreezeReason" &&
item.ident != "RuntimeParameters" &&
item.ident != "PalletInfo"
{
return syn::Error::new_spanned(
item,
"`#[inject_runtime_type]` can only be attached to `RuntimeCall`, `RuntimeEvent`, \
`RuntimeTask`, `RuntimeOrigin` or `PalletInfo`",
`RuntimeTask`, `RuntimeOrigin`, `RuntimeParameters` or `PalletInfo`",
)
.to_compile_error()
.into()
@@ -1685,3 +1688,52 @@ pub fn import_section(attr: TokenStream, tokens: TokenStream) -> TokenStream {
}
.into()
}
/// Mark a module that contains dynamic parameters.
///
/// See the `pallet_parameters` for a full example.
///
/// # Arguments
///
/// The macro accepts two positional arguments, of which the second is optional.
///
/// ## Aggregated Enum Name
///
/// This sets the name that the aggregated Key-Value enum will be named after. Common names would be
/// `RuntimeParameters`, akin to `RuntimeCall`, `RuntimeOrigin` etc. There is no default value for
/// this argument.
///
/// ## Parameter Storage Backend
///
/// The second argument provides access to the storage of the parameters. It can either be set on
/// on this attribute, or on the inner ones. If set on both, the inner one takes precedence.
#[proc_macro_attribute]
pub fn dynamic_params(attrs: TokenStream, input: TokenStream) -> TokenStream {
dynamic_params::dynamic_params(attrs.into(), input.into())
.unwrap_or_else(|r| r.into_compile_error())
.into()
}
/// Define a module inside a [`macro@dynamic_params`] module that contains dynamic parameters.
///
/// See the `pallet_parameters` for a full example.
///
/// # Argument
///
/// This attribute takes one optional argument. The argument can either be put here or on the
/// surrounding `#[dynamic_params]` attribute. If set on both, the inner one takes precedence.
#[proc_macro_attribute]
pub fn dynamic_pallet_params(attrs: TokenStream, input: TokenStream) -> TokenStream {
dynamic_params::dynamic_pallet_params(attrs.into(), input.into())
.unwrap_or_else(|r| r.into_compile_error())
.into()
}
/// Used internally by [`dynamic_params`].
#[doc(hidden)]
#[proc_macro_attribute]
pub fn dynamic_aggregated_params_internal(attrs: TokenStream, input: TokenStream) -> TokenStream {
dynamic_params::dynamic_aggregated_params_internal(attrs.into(), input.into())
.unwrap_or_else(|r| r.into_compile_error())
.into()
}