Skip to content

Commit

Permalink
frame: GenesisBuild::build allowed in no_std (paritytech#14107)
Browse files Browse the repository at this point in the history
* frame: GenesisBuild::build allowed in no_std

i`GenesisBuild::build` function will be required for no_std in no native
runtime world.

`GenesisBuild::build` macro generated function allows to build the runtime
GenesisConfig assembled from all pallets' GenesisConfigs.

* fixes

* GenesisBuild::build avaiable in no-std

- #[cfg(feature = "std")] is not longer added to GenesisBuild implementation.

* system: hash69 available for no-std

* elections-phragmen: panic message fixed for no_std

* frame::suport: doc updated

* test-runtime: default for GenesisConfig

* frame::test-pallet: serde/std added to std feature deps

* Cargo.toml: deps sorted

* Cargo.lock update

cargo update -p frame-support-test-pallet -p frame-support-test

* frame ui tests: cleanup

---------

Co-authored-by: parity-processbot <>
  • Loading branch information
michalkucharczyk authored and nathanwhit committed Jul 19, 2023
1 parent e21228a commit 2af0ef2
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 158 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,10 @@ pub mod pallet {
Members::<T>::mutate(|members| {
match members.binary_search_by(|m| m.who.cmp(member)) {
Ok(_) => {
panic!("Duplicate member in elections-phragmen genesis: {}", member)
panic!(
"Duplicate member in elections-phragmen genesis: {:?}",
member
)
},
Err(pos) => members.insert(
pos,
Expand Down
11 changes: 0 additions & 11 deletions frame/support/procedural/src/pallet/expand/genesis_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use crate::pallet::Def;

///
/// * implement the trait `sp_runtime::BuildModuleGenesisStorage`
/// * add #[cfg(feature = "std")] to GenesisBuild implementation.
pub fn expand_genesis_build(def: &mut Def) -> proc_macro2::TokenStream {
let genesis_config = if let Some(genesis_config) = &def.genesis_config {
genesis_config
Expand All @@ -41,16 +40,6 @@ pub fn expand_genesis_build(def: &mut Def) -> proc_macro2::TokenStream {

let gen_cfg_use_gen = genesis_config.gen_kind.type_use_gen(genesis_build.attr_span);

let genesis_build_item =
&mut def.item.content.as_mut().expect("Checked by def parser").1[genesis_build.index];

let genesis_build_item_impl = if let syn::Item::Impl(impl_) = genesis_build_item {
impl_
} else {
unreachable!("Checked by genesis_build parser")
};

genesis_build_item_impl.attrs.push(syn::parse_quote!( #[cfg(feature = "std")] ));
let where_clause = &genesis_build.where_clause;

quote::quote_spanned!(genesis_build.attr_span =>
Expand Down
6 changes: 2 additions & 4 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1522,8 +1522,6 @@ pub mod tests {

/// Prelude to be used alongside pallet macro, for ease of use.
pub mod pallet_prelude {
#[cfg(feature = "std")]
pub use crate::traits::GenesisBuild;
pub use crate::{
dispatch::{
DispatchClass, DispatchError, DispatchResult, DispatchResultWithPostInfo, Parameter,
Expand All @@ -1540,8 +1538,8 @@ pub mod pallet_prelude {
},
},
traits::{
ConstU32, EnsureOrigin, Get, GetDefault, GetStorageVersion, Hooks, IsType,
PalletInfoAccess, StorageInfoTrait, StorageVersion, TypedGet,
ConstU32, EnsureOrigin, GenesisBuild, Get, GetDefault, GetStorageVersion, Hooks,
IsType, PalletInfoAccess, StorageInfoTrait, StorageVersion, TypedGet,
},
Blake2_128, Blake2_128Concat, Blake2_256, CloneNoBound, DebugNoBound, EqNoBound, Identity,
PartialEqNoBound, RuntimeDebug, RuntimeDebugNoBound, Twox128, Twox256, Twox64Concat,
Expand Down
6 changes: 2 additions & 4 deletions frame/support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,9 @@ pub use metadata::{
};

mod hooks;
#[cfg(feature = "std")]
pub use hooks::GenesisBuild;
pub use hooks::{
Hooks, IntegrityTest, OnFinalize, OnGenesis, OnIdle, OnInitialize, OnRuntimeUpgrade,
OnTimestampSet,
GenesisBuild, Hooks, IntegrityTest, OnFinalize, OnGenesis, OnIdle, OnInitialize,
OnRuntimeUpgrade, OnTimestampSet,
};

pub mod schedule;
Expand Down
3 changes: 2 additions & 1 deletion frame/support/src/traits/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,20 +361,21 @@ pub trait Hooks<BlockNumber> {

/// A trait to define the build function of a genesis config, T and I are placeholder for pallet
/// trait and pallet instance.
#[cfg(feature = "std")]
pub trait GenesisBuild<T, I = ()>: Default + sp_runtime::traits::MaybeSerializeDeserialize {
/// The build function is called within an externalities allowing storage APIs.
/// Thus one can write to storage using regular pallet storages.
fn build(&self);

/// Build the storage using `build` inside default storage.
#[cfg(feature = "std")]
fn build_storage(&self) -> Result<sp_runtime::Storage, String> {
let mut storage = Default::default();
self.assimilate_storage(&mut storage)?;
Ok(storage)
}

/// Assimilate the storage for this module into pre-existing overlays.
#[cfg(feature = "std")]
fn assimilate_storage(&self, storage: &mut sp_runtime::Storage) -> Result<(), String> {
sp_state_machine::BasicExternalities::execute_with_storage(storage, || {
self.build();
Expand Down
11 changes: 6 additions & 5 deletions frame/support/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,22 @@ test-pallet = { package = "frame-support-test-pallet", default-features = false,
[features]
default = ["std"]
std = [
"serde/std",
"codec/std",
"scale-info/std",
"frame-benchmarking/std",
"frame-executive/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"serde/std",
"sp-api/std",
"sp-arithmetic/std",
"sp-core/std",
"sp-std/std",
"sp-io/std",
"sp-runtime/std",
"sp-state-machine",
"sp-arithmetic/std",
"sp-std/std",
"sp-version/std",
"sp-api/std",
"test-pallet/std",
]
try-runtime = [
"frame-support/try-runtime",
Expand Down
2 changes: 2 additions & 0 deletions frame/support/test/pallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }
scale-info = { version = "2.0.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.136", default-features = false, features = ["derive"] }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../../" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../../../system" }

Expand All @@ -24,4 +25,5 @@ std = [
"frame-support/std",
"frame-system/std",
"scale-info/std",
"serde/std",
]

This file was deleted.

1 change: 0 additions & 1 deletion frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,6 @@ pub struct EventRecord<E: Parameter + Member, T> {

// Create a Hash with 69 for each byte,
// only used to build genesis config.
#[cfg(feature = "std")]
fn hash69<T: AsMut<[u8]> + Default>() -> T {
let mut h = T::default();
h.as_mut().iter_mut().for_each(|byte| *byte = 69);
Expand Down
2 changes: 1 addition & 1 deletion test-utils/runtime/src/substrate_test_pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub mod pallet {
pub type Authorities<T> = StorageValue<_, Vec<Public>, ValueQuery>;

#[pallet::genesis_config]
#[cfg_attr(feature = "std", derive(Default))]
#[derive(Default)]
pub struct GenesisConfig {
pub authorities: Vec<Public>,
}
Expand Down

0 comments on commit 2af0ef2

Please sign in to comment.