Skip to content

Commit

Permalink
feat: ✨ add default preset for RuntimeGenesisConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelmauro committed Jan 21, 2025
1 parent aeecd0d commit 52436cb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
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.

4 changes: 4 additions & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ version = "0.8.0-dev"
hex-literal = "0.3.4"
impl-trait-for-tuples = "0.2.1"
log = "0.4"
serde = { workspace = true, default-features = false }
serde_json = { workspace = true, default-features = false }

# Moonbeam
moonbeam-core-primitives = { workspace = true }
Expand Down Expand Up @@ -121,6 +123,8 @@ std = [
"parity-scale-codec/std",
"scale-info/std",
"precompile-utils/std",
"serde/std",
"serde_json/std",
"sp-consensus-slots/std",
"sp-core/std",
"sp-io/std",
Expand Down
16 changes: 14 additions & 2 deletions runtime/common/src/apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ macro_rules! impl_runtime_apis_plus_common {
}
}

fn default_preset<T: Default + serde::Serialize>() -> Option<Vec<u8>> {
let config = T::default();
let config = serde_json::to_value(config).expect("Could not build genesis config.");

Some(
serde_json::to_string(&config)
.expect("Could not build genesis config.")
.into_bytes(),
)
}

impl_runtime_apis! {
$($custom)*

Expand Down Expand Up @@ -116,16 +127,17 @@ macro_rules! impl_runtime_apis_plus_common {
}

impl sp_genesis_builder::GenesisBuilder<Block> for Runtime {

fn build_state(config: Vec<u8>) -> sp_genesis_builder::Result {
frame_support::genesis_builder_helper::build_state::<RuntimeGenesisConfig>(config)
}

fn get_preset(id: &Option<sp_genesis_builder::PresetId>) -> Option<Vec<u8>> {
frame_support::genesis_builder_helper::get_preset::<RuntimeGenesisConfig>(id, |_| None)
frame_support::genesis_builder_helper::get_preset::<RuntimeGenesisConfig>(id, default_preset::<RuntimeGenesisConfig>)
}

fn preset_names() -> Vec<sp_genesis_builder::PresetId> {
vec![]
vec![sp_genesis_builder::PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET)]
}
}

Expand Down
4 changes: 1 addition & 3 deletions scripts/run-benches-for-runtime.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ PALLETS=($(
./target/${profile}/moonbeam benchmark pallet \
--list \
--runtime="./target/${profile}/wbuild/${runtime}-runtime/${runtime}_runtime.wasm" \
--genesis-builder=runtime \
--genesis-builder-preset=development |\
--genesis-builder=runtime |\
tail -n+2 |\
cut -d',' -f1 |\
sort |\
Expand Down Expand Up @@ -45,7 +44,6 @@ for PALLET in "${PALLETS[@]}"; do
./target/${profile}/moonbeam benchmark pallet \
--runtime="./target/${profile}/wbuild/${runtime}-runtime/${runtime}_runtime.wasm" \
--genesis-builder=runtime \
--genesis-builder-preset=development \
--steps=50 \
--repeat=20 \
--pallet="$PALLET" \
Expand Down

0 comments on commit 52436cb

Please sign in to comment.