Polkadot Omni Node (#418)

* add to link to omni node in the docs

* clean and test both templates locally

* add genesis config presets to runtime

* add genesis config presets to evm template and toml sort generic template

* update abstractions to use runtime genesis presets

* runtime tests for genesis config presets for both templates

* H160 type does not require clone so satisfy clippy
This commit is contained in:
Amar Singh
2025-08-21 08:50:58 -04:00
committed by GitHub
parent 2bfcbf94d5
commit a52909422a
11 changed files with 586 additions and 8 deletions
+5 -2
View File
@@ -4234,6 +4234,7 @@ dependencies = [
"cumulus-primitives-core",
"cumulus-primitives-timestamp",
"cumulus-primitives-utility",
"docify",
"dp-consensus",
"frame-benchmarking",
"frame-executive",
@@ -4287,6 +4288,7 @@ dependencies = [
"polkadot-runtime-common",
"polkadot-runtime-parachains",
"scale-info",
"serde_json",
"smallvec",
"sp-api",
"sp-arithmetic",
@@ -4296,6 +4298,7 @@ dependencies = [
"sp-genesis-builder",
"sp-inherents",
"sp-io",
"sp-keyring",
"sp-offchain",
"sp-runtime",
"sp-session",
@@ -7170,7 +7173,7 @@ checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e"
[[package]]
name = "openzeppelin-pallet-abstractions"
version = "0.1.0"
source = "git+https://github.com/OpenZeppelin/openzeppelin-pallet-abstractions?branch=polkadot-stable2503#971a66b7513adbbd1c51fcf74a3f85bf102f7bea"
source = "git+https://github.com/OpenZeppelin/openzeppelin-pallet-abstractions?branch=polkadot-stable2503#bc6dcabdc7b66f218c39c0f3496710c7b81a4a09"
dependencies = [
"cumulus-primitives-core",
"frame-support",
@@ -7183,7 +7186,7 @@ dependencies = [
[[package]]
name = "openzeppelin-pallet-abstractions-proc"
version = "0.1.0"
source = "git+https://github.com/OpenZeppelin/openzeppelin-pallet-abstractions?branch=polkadot-stable2503#971a66b7513adbbd1c51fcf74a3f85bf102f7bea"
source = "git+https://github.com/OpenZeppelin/openzeppelin-pallet-abstractions?branch=polkadot-stable2503#bc6dcabdc7b66f218c39c0f3496710c7b81a4a09"
dependencies = [
"darling",
"openzeppelin-pallet-abstractions",
+3 -1
View File
@@ -12,6 +12,7 @@ repository = "https://github.com/OpenZeppelin/polkadot-runtime-templates"
[workspace.dependencies]
clap = { version = "4.5.3", features = [ "derive" ] }
color-print = "0.3.4"
docify = { version = "0.2.9" }
futures = "0.3.30"
hex-literal = "0.4.1"
jsonrpsee = { version = "0.24.3", features = [ "server" ] }
@@ -22,7 +23,7 @@ parity-scale-codec = { version = "3.6.12", default-features = false, features =
] }
scale-info = { version = "2.11.1", default-features = false }
serde = { version = "1.0.197", default-features = false }
serde_json = "1.0.121"
serde_json = { version = "1.0.121", default-features = false }
smallvec = "1.11.0"
# TODO: update to release
@@ -84,6 +85,7 @@ sp-core = { git = "https://github.com/paritytech/polkadot-sdk", default-features
sp-genesis-builder = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, tag = "polkadot-stable2503" }
sp-inherents = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, tag = "polkadot-stable2503" }
sp-io = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, tag = "polkadot-stable2503" }
sp-keyring = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, tag = "polkadot-stable2503" }
sp-keystore = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, tag = "polkadot-stable2503" }
sp-offchain = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, tag = "polkadot-stable2503" }
sp-runtime = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, tag = "polkadot-stable2503" }
+5
View File
@@ -11,10 +11,12 @@ version = "3.0.0"
targets = [ "x86_64-unknown-linux-gnu" ]
[dependencies]
docify = { workspace = true }
hex-literal = { workspace = true, optional = true }
log = { workspace = true }
parity-scale-codec = { workspace = true, features = [ "derive" ] }
scale-info = { workspace = true, features = [ "derive" ] }
serde_json = { workspace = true, default-features = false, features = [ "alloc" ] }
smallvec = { workspace = true }
openzeppelin-pallet-abstractions = { workspace = true }
@@ -54,6 +56,7 @@ sp-consensus-aura = { workspace = true }
sp-core = { workspace = true }
sp-genesis-builder = { workspace = true }
sp-inherents = { workspace = true }
sp-keyring = { workspace = true }
sp-offchain = { workspace = true }
sp-runtime = { workspace = true }
sp-session = { workspace = true }
@@ -178,6 +181,7 @@ std = [
"polkadot-parachain-primitives/std",
"polkadot-runtime-common/std",
"scale-info/std",
"serde_json/std",
"sp-api/std",
"sp-arithmetic/std",
"sp-block-builder/std",
@@ -185,6 +189,7 @@ std = [
"sp-core/std",
"sp-genesis-builder/std",
"sp-inherents/std",
"sp-keyring/std",
"sp-offchain/std",
"sp-runtime/std",
"sp-session/std",
@@ -0,0 +1,232 @@
use alloc::{vec, vec::Vec};
use cumulus_primitives_core::ParaId;
use frame_support::build_struct_json_patch;
use parachains_common::AuraId;
use serde_json::Value;
use sp_genesis_builder::PresetId;
use sp_keyring::Sr25519Keyring;
use crate::{
constants::currency::EXISTENTIAL_DEPOSIT, AccountId, BalancesConfig, CollatorSelectionConfig,
ParachainInfoConfig, PolkadotXcmConfig, RuntimeGenesisConfig, SessionConfig, SessionKeys,
SudoConfig,
};
/// The default XCM version to set in genesis config.
const SAFE_XCM_VERSION: u32 = xcm::prelude::XCM_VERSION;
/// Parachain id used for genesis config presets of parachain template.
#[docify::export_content]
pub const PARACHAIN_ID: u32 = 1000;
/// Generate the session keys from individual elements.
///
/// The input must be a tuple of individual keys (a single arg for now since we have just one key).
pub fn template_session_keys(keys: AuraId) -> SessionKeys {
SessionKeys { aura: keys }
}
fn testnet_genesis(
invulnerables: Vec<(AccountId, AuraId)>,
endowed_accounts: Vec<AccountId>,
root: AccountId,
id: ParaId,
) -> Value {
build_struct_json_patch!(RuntimeGenesisConfig {
balances: BalancesConfig {
balances: endowed_accounts
.iter()
.cloned()
.map(|k| (k, 1u128 << 60))
.collect::<Vec<_>>(),
},
parachain_info: ParachainInfoConfig { parachain_id: id },
collator_selection: CollatorSelectionConfig {
invulnerables: invulnerables.iter().cloned().map(|(acc, _)| acc).collect::<Vec<_>>(),
candidacy_bond: EXISTENTIAL_DEPOSIT * 16,
},
session: SessionConfig {
keys: invulnerables
.into_iter()
.map(|(acc, aura)| {
(
acc.clone(), // account id
acc, // validator id
template_session_keys(aura), // session keys
)
})
.collect::<Vec<_>>(),
},
polkadot_xcm: PolkadotXcmConfig { safe_xcm_version: Some(SAFE_XCM_VERSION) },
sudo: SudoConfig { key: Some(root) },
})
}
fn local_testnet_genesis() -> Value {
testnet_genesis(
// initial collators.
vec![
(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
],
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
Sr25519Keyring::Alice.to_account_id(),
PARACHAIN_ID.into(),
)
}
fn development_config_genesis() -> Value {
testnet_genesis(
// initial collators.
vec![
(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
],
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
Sr25519Keyring::Alice.to_account_id(),
PARACHAIN_ID.into(),
)
}
/// Provides the JSON representation of predefined genesis config for given `id`.
pub fn get_preset(id: &PresetId) -> Option<vec::Vec<u8>> {
let patch = match id.as_ref() {
sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET => local_testnet_genesis(),
sp_genesis_builder::DEV_RUNTIME_PRESET => development_config_genesis(),
_ => return None,
};
Some(
serde_json::to_string(&patch)
.expect("serialization to json is expected to work. qed.")
.into_bytes(),
)
}
/// List of supported presets.
pub fn preset_names() -> Vec<PresetId> {
vec![
PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET),
PresetId::from(sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET),
]
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
/// Parse a preset bytes blob with serde_json.
fn parse_bytes(b: Vec<u8>) -> Value {
serde_json::from_slice::<Value>(&b).expect("preset must be valid JSON")
}
/// Extract a JSON value for an AccountId by serializing the runtime AccountId directly.
/// This avoids guessing about how AccountId32 is encoded (hex string with 0x…).
fn account_json(acc: &AccountId) -> Value {
serde_json::to_value(acc).expect("AccountId must serialize to JSON")
}
#[test]
fn local_testnet_genesis_shape_is_reasonable() {
let v = super::local_testnet_genesis();
// Top-level keys we care about exist
assert!(v.get("balances").is_some(), "balances missing");
assert!(v.get("parachainInfo").is_some(), "parachainInfo missing");
assert!(v.get("collatorSelection").is_some(), "collatorSelection missing");
assert!(v.get("session").is_some(), "session missing");
assert!(v.get("polkadotXcm").is_some(), "polkadotXcm missing");
assert!(v.get("sudo").is_some(), "sudo missing");
// parachainId
assert_eq!(v["parachainInfo"]["parachainId"], json!(PARACHAIN_ID), "wrong parachain id");
// XCM version
assert_eq!(
v["polkadotXcm"]["safeXcmVersion"],
json!(Some(SAFE_XCM_VERSION)),
"wrong SAFE_XCM_VERSION"
);
// Endowed accounts length should match well_known()
let expected_endowed: Vec<AccountId> =
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect();
let balances =
v["balances"]["balances"].as_array().expect("balances.balances must be an array");
assert_eq!(balances.len(), expected_endowed.len(), "endowed accounts length mismatch");
// Sudo key must be Alice
let alice = Sr25519Keyring::Alice.to_account_id();
assert_eq!(v["sudo"]["key"], json!(Some(account_json(&alice))), "sudo key is not Alice");
// Collators (invulnerables) must be Alice and Bob
let invuls = v["collatorSelection"]["invulnerables"]
.as_array()
.expect("collatorSelection.invulnerables must be an array");
assert_eq!(invuls.len(), 2, "expected two invulnerables");
let expected_alice = account_json(&Sr25519Keyring::Alice.to_account_id());
let expected_bob = account_json(&Sr25519Keyring::Bob.to_account_id());
assert_eq!(invuls[0], expected_alice, "first invulnerable must be Alice");
assert_eq!(invuls[1], expected_bob, "second invulnerable must be Bob");
// candidacyBond must be EXISTENTIAL_DEPOSIT * 16
assert_eq!(
v["collatorSelection"]["candidacyBond"],
json!(EXISTENTIAL_DEPOSIT * 16),
"wrong candidacy bond"
);
// Session keys: one entry per invulnerable; controller == validator
let sess = v["session"]["keys"].as_array().expect("session.keys must be an array");
assert_eq!(sess.len(), invuls.len(), "session keys length must equal invulnerables length");
for entry in sess {
let arr = entry
.as_array()
.expect("each session entry must be a 3-tuple [acc, validator, keys]");
assert_eq!(arr.len(), 3, "session entry must have 3 elements");
assert_eq!(arr[0], arr[1], "controller and validator account must match");
// We don't assert on the actual Aura key bytes here—just presence/shape.
assert!(arr[2].get("aura").is_some(), "session keys must contain aura");
}
}
#[test]
fn development_genesis_mirrors_local_testnet() {
// In this template, development == local_testnet (per implementation).
let dev = super::development_config_genesis();
let local = super::local_testnet_genesis();
assert_eq!(dev, local, "development config should mirror local_testnet");
}
#[test]
fn get_preset_roundtrips_known_ids() {
// LOCAL_TESTNET
let p =
super::get_preset(&PresetId::from(sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET))
.expect("local preset should exist");
let from_api = parse_bytes(p);
let from_fn = super::local_testnet_genesis();
assert_eq!(from_api, from_fn, "local_testnet preset must match function output");
// DEV
let p = super::get_preset(&PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET))
.expect("dev preset should exist");
let from_api = parse_bytes(p);
let from_fn = super::development_config_genesis();
assert_eq!(from_api, from_fn, "dev preset must match function output");
}
#[test]
fn preset_names_lists_supported() {
let names = super::preset_names();
assert!(
names.contains(&PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET)),
"DEV preset should be listed"
);
assert!(
names.contains(&PresetId::from(sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET)),
"LOCAL_TESTNET preset should be listed"
);
}
}
+2
View File
@@ -10,12 +10,14 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
pub mod apis;
pub mod configs;
pub mod constants;
mod genesis_config_presets;
mod types;
mod weights;
use frame_support::weights::{
WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
};
pub use genesis_config_presets::PARACHAIN_ID;
use smallvec::smallvec;
pub use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_runtime::impl_opaque_keys;