Skip to content

Commit

Permalink
Adds derive_impl to relay-chain and parachain runtimes (paritytech#2476)
Browse files Browse the repository at this point in the history
  • Loading branch information
codekitz authored Dec 5, 2023
1 parent e795aa4 commit cf7a9f6
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 43 deletions.
36 changes: 6 additions & 30 deletions substrate/bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
traits::{
AccountIdLookup, BlakeTwo256, Block as BlockT, IdentifyAccount, NumberFor, One, Verify,
},
traits::{BlakeTwo256, Block as BlockT, IdentifyAccount, NumberFor, One, Verify},
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult, MultiSignature,
};
Expand All @@ -26,7 +24,7 @@ use sp_version::RuntimeVersion;
use frame_support::genesis_builder_helper::{build_config, create_default_config};
// A few exports that help ease life for downstream crates.
pub use frame_support::{
construct_runtime, parameter_types,
construct_runtime, derive_impl, parameter_types,
traits::{
ConstBool, ConstU128, ConstU32, ConstU64, ConstU8, KeyOwnerProofSystem, Randomness,
StorageInfo,
Expand Down Expand Up @@ -151,11 +149,11 @@ parameter_types! {
pub const SS58Prefix: u8 = 42;
}

// Configure FRAME pallets to include in runtime.

/// The default types are being injected by [`derive_impl`](`frame_support::derive_impl`) from
/// [`SoloChainDefaultConfig`](`struct@frame_system::config_preludes::SolochainDefaultConfig`),
/// but overridden as needed.
#[derive_impl(frame_system::config_preludes::SolochainDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Runtime {
/// The basic call filter to use in dispatchable.
type BaseCallFilter = frame_support::traits::Everything;
/// The block type for the runtime.
type Block = Block;
/// Block & extrinsics weights: base values and limits.
Expand All @@ -164,42 +162,20 @@ impl frame_system::Config for Runtime {
type BlockLength = BlockLength;
/// The identifier used to distinguish between accounts.
type AccountId = AccountId;
/// The aggregated dispatch type that is available for extrinsics.
type RuntimeCall = RuntimeCall;
/// The lookup mechanism to get account ID from whatever is passed in dispatchers.
type Lookup = AccountIdLookup<AccountId, ()>;
/// The type for storing how many extrinsics an account has signed.
type Nonce = Nonce;
/// The type for hashing blocks and tries.
type Hash = Hash;
/// The hashing algorithm used.
type Hashing = BlakeTwo256;
/// The ubiquitous event type.
type RuntimeEvent = RuntimeEvent;
/// The ubiquitous origin type.
type RuntimeOrigin = RuntimeOrigin;
/// Maximum number of block number to block hash mappings to keep (oldest pruned first).
type BlockHashCount = BlockHashCount;
/// The weight of database operations that the runtime can invoke.
type DbWeight = RocksDbWeight;
/// Version of the runtime.
type Version = Version;
/// Converts a module to the index of the module in `construct_runtime!`.
///
/// This type is being generated by `construct_runtime!`.
type PalletInfo = PalletInfo;
/// What to do if a new account is created.
type OnNewAccount = ();
/// What to do if an account is fully reaped from the system.
type OnKilledAccount = ();
/// The data to be stored in an account.
type AccountData = pallet_balances::AccountData<Balance>;
/// Weight information for the extrinsics of this pallet.
type SystemWeightInfo = ();
/// This is used as an identifier of the chain. 42 is the generic substrate prefix.
type SS58Prefix = SS58Prefix;
/// The set code logic, just the default since we're not a parachain.
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

Expand Down
11 changes: 2 additions & 9 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use frame_election_provider_support::{
onchain, BalancingConfig, ElectionDataProvider, SequentialPhragmen, VoteWeight,
};
use frame_support::{
construct_runtime,
construct_runtime, derive_impl,
dispatch::DispatchClass,
genesis_builder_helper::{build_config, create_default_config},
instances::{Instance1, Instance2},
Expand Down Expand Up @@ -283,29 +283,22 @@ impl pallet_safe_mode::Config for Runtime {
type WeightInfo = pallet_safe_mode::weights::SubstrateWeight<Runtime>;
}

#[derive_impl(frame_system::config_preludes::SolochainDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Runtime {
type BaseCallFilter = InsideBoth<SafeMode, TxPause>;
type BlockWeights = RuntimeBlockWeights;
type BlockLength = RuntimeBlockLength;
type DbWeight = RocksDbWeight;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Nonce = Nonce;
type Hash = Hash;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = Indices;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = BlockHashCount;
type Version = Version;
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = frame_system::weights::SubstrateWeight<Runtime>;
type SS58Prefix = ConstU16<42>;
type OnSetCode = ();
type MaxConsumers = ConstU32<16>;
}

Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pallet-examples = { path = "./examples" }

[features]
default = ["runtime", "std"]
experimental = ["frame-support/experimental", "frame-system/experimental"]
experimental = ["frame-support/experimental"]
runtime = [
"frame-executive",
"frame-system-rpc-runtime-api",
Expand Down
1 change: 0 additions & 1 deletion substrate/frame/system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ runtime-benchmarks = [
"sp-runtime/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime", "sp-runtime/try-runtime"]
experimental = []

[[bench]]
name = "bench"
Expand Down
64 changes: 62 additions & 2 deletions substrate/frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ pub mod pallet {
/// Default implementations of [`DefaultConfig`], which can be used to implement [`Config`].
pub mod config_preludes {
use super::{inject_runtime_type, DefaultConfig};
use frame_support::derive_impl;

/// Provides a viable default config that can be used with
/// [`derive_impl`](`frame_support::derive_impl`) to derive a testing pallet config
Expand Down Expand Up @@ -258,39 +259,98 @@ pub mod pallet {
/// if you use `pallet-balances` or similar.
/// * Make sure to overwrite [`DefaultConfig::Version`].
/// * 2s block time, and a default 5mb block size is used.
#[cfg(feature = "experimental")]
pub struct SolochainDefaultConfig;

#[cfg(feature = "experimental")]
#[frame_support::register_default_impl(SolochainDefaultConfig)]
impl DefaultConfig for SolochainDefaultConfig {
/// The default type for storing how many extrinsics an account has signed.
type Nonce = u32;

/// The default type for hashing blocks and tries.
type Hash = sp_core::hash::H256;

/// The default hashing algorithm used.
type Hashing = sp_runtime::traits::BlakeTwo256;

/// The default identifier used to distinguish between accounts.
type AccountId = sp_runtime::AccountId32;

/// The lookup mechanism to get account ID from whatever is passed in dispatchers.
type Lookup = sp_runtime::traits::AccountIdLookup<Self::AccountId, ()>;

/// The maximum number of consumers allowed on a single account. Using 128 as default.
type MaxConsumers = frame_support::traits::ConstU32<128>;

/// The default data to be stored in an account.
type AccountData = crate::AccountInfo<Self::Nonce, ()>;

/// What to do if a new account is created.
type OnNewAccount = ();

/// What to do if an account is fully reaped from the system.
type OnKilledAccount = ();

/// Weight information for the extrinsics of this pallet.
type SystemWeightInfo = ();

/// This is used as an identifier of the chain.
type SS58Prefix = ();

/// Version of the runtime.
type Version = ();

/// Block & extrinsics weights: base values and limits.
type BlockWeights = ();

/// The maximum length of a block (in bytes).
type BlockLength = ();

/// The weight of database operations that the runtime can invoke.
type DbWeight = ();

/// The ubiquitous event type injected by `construct_runtime!`.
#[inject_runtime_type]
type RuntimeEvent = ();

/// The ubiquitous origin type injected by `construct_runtime!`.
#[inject_runtime_type]
type RuntimeOrigin = ();

/// The aggregated dispatch type available for extrinsics, injected by
/// `construct_runtime!`.
#[inject_runtime_type]
type RuntimeCall = ();

/// Converts a module to the index of the module, injected by `construct_runtime!`.
#[inject_runtime_type]
type PalletInfo = ();

/// The basic call filter to use in dispatchable. Supports everything as the default.
type BaseCallFilter = frame_support::traits::Everything;

/// Maximum number of block number to block hash mappings to keep (oldest pruned first).
/// Using 256 as default.
type BlockHashCount = frame_support::traits::ConstU32<256>;

/// The set code logic, just the default since we're not a parachain.
type OnSetCode = ();
}

/// Default configurations of this pallet in a relay-chain environment.
pub struct RelayChainDefaultConfig;

/// It currently uses the same configuration as `SolochainDefaultConfig`.
#[derive_impl(SolochainDefaultConfig as DefaultConfig, no_aggregated_types)]
#[frame_support::register_default_impl(RelayChainDefaultConfig)]
impl DefaultConfig for RelayChainDefaultConfig {}

/// Default configurations of this pallet in a parachain environment.
pub struct ParaChainDefaultConfig;

/// It currently uses the same configuration as `SolochainDefaultConfig`.
#[derive_impl(SolochainDefaultConfig as DefaultConfig, no_aggregated_types)]
#[frame_support::register_default_impl(ParaChainDefaultConfig)]
impl DefaultConfig for ParaChainDefaultConfig {}
}

/// System configuration trait. Implemented by runtime.
Expand Down

0 comments on commit cf7a9f6

Please sign in to comment.