Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
frame: Enable GenesisConfig in no_std (#14108)
Browse files Browse the repository at this point in the history
* frame: Default for GenesisConfig in no_std

`Default` for `GenesisConfig` will be required for no_std in no native
runtime world. It must be possible to instantiate default GenesisConfig
for pallets and runtime.

* ".git/.scripts/commands/fmt/fmt.sh"

* hash69 in no_std reverted

* derive(DefaultNoBound) for GenesisConfig used when possible

* treasury: derive(Default)

* Cargo.lock update

* genesis_config: compiler error improved

When std feature is not enabled for pallet, the GenesisConfig will be
defined, but serde::{Serialize,Deserialize} traits will not be
implemented.

The compiler error indicates the reason of latter errors.

This is temporary and serde traits will be enabled with together with
`serde` support in frame.

---------

Co-authored-by: command-bot <>
  • Loading branch information
michalkucharczyk authored May 20, 2023
1 parent 2c3b923 commit 0046337
Show file tree
Hide file tree
Showing 31 changed files with 149 additions and 173 deletions.
8 changes: 1 addition & 7 deletions frame/alliance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,19 +401,13 @@ pub mod pallet {
}

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
pub fellows: Vec<T::AccountId>,
pub allies: Vec<T::AccountId>,
pub phantom: PhantomData<(T, I)>,
}

#[cfg(feature = "std")]
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
fn default() -> Self {
Self { fellows: Vec::new(), allies: Vec::new(), phantom: Default::default() }
}
}

#[pallet::genesis_build]
impl<T: Config<I>, I: 'static> GenesisBuild<T, I> for GenesisConfig<T, I> {
fn build(&self) {
Expand Down
12 changes: 1 addition & 11 deletions frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ pub mod pallet {
>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
/// Genesis assets: id, owner, is_sufficient, min_balance
pub assets: Vec<(T::AssetId, T::AccountId, bool, T::Balance)>,
Expand All @@ -378,17 +379,6 @@ pub mod pallet {
pub accounts: Vec<(T::AssetId, T::AccountId, T::Balance)>,
}

#[cfg(feature = "std")]
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
fn default() -> Self {
Self {
assets: Default::default(),
metadata: Default::default(),
accounts: Default::default(),
}
}
}

#[pallet::genesis_build]
impl<T: Config<I>, I: 'static> GenesisBuild<T, I> for GenesisConfig<T, I> {
fn build(&self) {
Expand Down
8 changes: 1 addition & 7 deletions frame/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,11 @@ pub mod pallet {
pub(super) type CurrentSlot<T: Config> = StorageValue<_, Slot, ValueQuery>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
pub authorities: Vec<T::AuthorityId>,
}

#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self { authorities: Vec::new() }
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
Expand Down
2 changes: 1 addition & 1 deletion frame/authority-discovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub mod pallet {
pub(super) type NextKeys<T: Config> =
StorageValue<_, WeakBoundedVec<AuthorityId, T::MaxAuthorities>, ValueQuery>;

#[cfg_attr(feature = "std", derive(Default))]
#[derive(Default)]
#[pallet::genesis_config]
pub struct GenesisConfig {
pub keys: Vec<AuthorityId>,
Expand Down
2 changes: 1 addition & 1 deletion frame/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ pub mod pallet {
pub(super) type SkippedEpochs<T> =
StorageValue<_, BoundedVec<(u64, SessionIndex), ConstU32<100>>, ValueQuery>;

#[cfg_attr(feature = "std", derive(Default))]
#[derive(Default)]
#[pallet::genesis_config]
pub struct GenesisConfig {
pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
Expand Down
3 changes: 1 addition & 2 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,6 @@ pub mod pallet {
pub balances: Vec<(T::AccountId, T::Balance)>,
}

#[cfg(feature = "std")]
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
fn default() -> Self {
Self { balances: Default::default() }
Expand All @@ -483,7 +482,7 @@ pub mod pallet {
.iter()
.map(|(x, _)| x)
.cloned()
.collect::<std::collections::BTreeSet<_>>();
.collect::<sp_std::collections::btree_set::BTreeSet<_>>();

assert!(
endowed_accounts.len() == self.balances.len(),
Expand Down
1 change: 0 additions & 1 deletion frame/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ pub mod pallet {
pub genesis_block: Option<BlockNumberFor<T>>,
}

#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
// BEEFY genesis will be first BEEFY-MANDATORY block,
Expand Down
8 changes: 1 addition & 7 deletions frame/collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,12 @@ pub mod pallet {
}

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
pub phantom: PhantomData<I>,
pub members: Vec<T::AccountId>,
}

#[cfg(feature = "std")]
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
fn default() -> Self {
Self { phantom: Default::default(), members: Default::default() }
}
}

#[pallet::genesis_build]
impl<T: Config<I>, I: 'static> GenesisBuild<T, I> for GenesisConfig<T, I> {
fn build(&self) {
Expand Down
8 changes: 1 addition & 7 deletions frame/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,17 +439,11 @@ pub mod pallet {
pub type MetadataOf<T: Config> = StorageMap<_, Blake2_128Concat, MetadataOwner, PreimageHash>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
_phantom: sp_std::marker::PhantomData<T>,
}

#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
GenesisConfig { _phantom: Default::default() }
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
Expand Down
8 changes: 1 addition & 7 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,17 +711,11 @@ pub mod pallet {
StorageMap<_, Twox64Concat, T::AccountId, Voter<T::AccountId, BalanceOf<T>>, ValueQuery>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
pub members: Vec<(T::AccountId, BalanceOf<T>)>,
}

#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self { members: Default::default() }
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
Expand Down
9 changes: 1 addition & 8 deletions frame/examples/basic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,20 +618,13 @@ pub mod pallet {

// The genesis config type.
#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
pub dummy: T::Balance,
pub bar: Vec<(T::AccountId, T::Balance)>,
pub foo: T::Balance,
}

// The default value for the genesis config type.
#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self { dummy: Default::default(), bar: Default::default(), foo: Default::default() }
}
}

// The build of genesis for the pallet.
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
Expand Down
2 changes: 1 addition & 1 deletion frame/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ pub mod pallet {
#[pallet::getter(fn session_for_set)]
pub(super) type SetIdSession<T: Config> = StorageMap<_, Twox64Concat, SetId, SessionIndex>;

#[cfg_attr(feature = "std", derive(Default))]
#[derive(Default)]
#[pallet::genesis_config]
pub struct GenesisConfig {
pub authorities: AuthorityList,
Expand Down
8 changes: 1 addition & 7 deletions frame/im-online/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,17 +442,11 @@ pub mod pallet {
>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
pub keys: Vec<T::AuthorityId>,
}

#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
GenesisConfig { keys: Default::default() }
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
Expand Down
8 changes: 1 addition & 7 deletions frame/indices/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,11 @@ pub mod pallet {
StorageMap<_, Blake2_128Concat, T::AccountIndex, (T::AccountId, BalanceOf<T>, bool)>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
pub indices: Vec<(T::AccountIndex, T::AccountId)>,
}

#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self { indices: Default::default() }
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
Expand Down
8 changes: 1 addition & 7 deletions frame/membership/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,12 @@ pub mod pallet {
pub type Prime<T: Config<I>, I: 'static = ()> = StorageValue<_, T::AccountId, OptionQuery>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
pub members: BoundedVec<T::AccountId, T::MaxMembers>,
pub phantom: PhantomData<I>,
}

#[cfg(feature = "std")]
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
fn default() -> Self {
Self { members: Default::default(), phantom: Default::default() }
}
}

#[pallet::genesis_build]
impl<T: Config<I>, I: 'static> GenesisBuild<T, I> for GenesisConfig<T, I> {
fn build(&self) {
Expand Down
8 changes: 1 addition & 7 deletions frame/node-authorization/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,11 @@ pub mod pallet {
StorageMap<_, Blake2_128Concat, PeerId, BTreeSet<PeerId>, ValueQuery>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
pub nodes: Vec<(PeerId, T::AccountId)>,
}

#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self { nodes: Vec::new() }
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
Expand Down
1 change: 0 additions & 1 deletion frame/nomination-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,6 @@ pub mod pallet {
pub global_max_commission: Option<Perbill>,
}

#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self {
Expand Down
8 changes: 1 addition & 7 deletions frame/scored-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,18 +249,12 @@ pub mod pallet {
pub(crate) type MemberCount<T, I = ()> = StorageValue<_, u32, ValueQuery>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
pub pool: PoolT<T, I>,
pub member_count: u32,
}

#[cfg(feature = "std")]
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
fn default() -> Self {
Self { pool: Default::default(), member_count: Default::default() }
}
}

#[pallet::genesis_build]
impl<T: Config<I>, I: 'static> GenesisBuild<T, I> for GenesisConfig<T, I> {
fn build(&self) {
Expand Down
8 changes: 1 addition & 7 deletions frame/session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,17 +414,11 @@ pub mod pallet {
}

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
pub keys: Vec<(T::AccountId, T::ValidatorId, T::Keys)>,
}

#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self { keys: Default::default() }
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
Expand Down
12 changes: 1 addition & 11 deletions frame/society/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,23 +643,13 @@ pub mod pallet {
}

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
pub pot: BalanceOf<T, I>,
pub members: Vec<T::AccountId>,
pub max_members: u32,
}

#[cfg(feature = "std")]
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
fn default() -> Self {
Self {
pot: Default::default(),
members: Default::default(),
max_members: Default::default(),
}
}
}

#[pallet::genesis_build]
impl<T: Config<I>, I: 'static> GenesisBuild<T, I> for GenesisConfig<T, I> {
fn build(&self) {
Expand Down
20 changes: 1 addition & 19 deletions frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ pub mod pallet {
pub(crate) type ChillThreshold<T: Config> = StorageValue<_, Percent, OptionQuery>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
pub validator_count: u32,
pub minimum_validator_count: u32,
Expand All @@ -594,25 +595,6 @@ pub mod pallet {
pub max_nominator_count: Option<u32>,
}

#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
GenesisConfig {
validator_count: Default::default(),
minimum_validator_count: Default::default(),
invulnerables: Default::default(),
force_era: Default::default(),
slash_reward_fraction: Default::default(),
canceled_payout: Default::default(),
stakers: Default::default(),
min_nominator_bond: Default::default(),
min_validator_bond: Default::default(),
max_validator_count: None,
max_nominator_count: None,
}
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
Expand Down
Loading

0 comments on commit 0046337

Please sign in to comment.