Skip to content

Commit

Permalink
Configure Runtime API and Chainspec for BABE
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin committed May 5, 2024
1 parent 5bde4e0 commit fa0a4d6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 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.

37 changes: 22 additions & 15 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cumulus_primitives_core::ParaId;
use parachain_template_runtime::{AccountId, AuraId, Signature, EXISTENTIAL_DEPOSIT};
use parachain_template_runtime::{AccountId, AuraId, BabeId, Signature, EXISTENTIAL_DEPOSIT};
use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};
use sc_service::ChainType;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -41,8 +41,8 @@ type AccountPublic = <Signature as Verify>::Signer;
/// Generate collator keys from seed.
///
/// This function's return type must always match the session keys of the chain in tuple format.
pub fn get_collator_keys_from_seed(seed: &str) -> AuraId {
get_from_seed::<AuraId>(seed)
pub fn get_collator_keys_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
get_from_seed::<TPublic>(seed)
}

/// Helper function to generate an account ID from seed
Expand All @@ -56,8 +56,11 @@ where
/// 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) -> parachain_template_runtime::SessionKeys {
parachain_template_runtime::SessionKeys { aura: keys }
pub fn template_session_keys(
aura: AuraId,
babe: BabeId,
) -> parachain_template_runtime::SessionKeys {
parachain_template_runtime::SessionKeys { aura, babe }
}

pub fn development_config() -> ChainSpec {
Expand All @@ -84,11 +87,13 @@ pub fn development_config() -> ChainSpec {
vec![
(
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_collator_keys_from_seed("Alice"),
get_collator_keys_from_seed::<AuraId>("Alice"),
get_collator_keys_from_seed::<BabeId>("Alice"),
),
(
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_collator_keys_from_seed("Bob"),
get_collator_keys_from_seed::<AuraId>("Bob"),
get_collator_keys_from_seed::<BabeId>("Bob"),
),
],
vec![
Expand Down Expand Up @@ -136,11 +141,13 @@ pub fn local_testnet_config() -> ChainSpec {
vec![
(
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_collator_keys_from_seed("Alice"),
get_collator_keys_from_seed::<AuraId>("Alice"),
get_collator_keys_from_seed::<BabeId>("Alice"),
),
(
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_collator_keys_from_seed("Bob"),
get_collator_keys_from_seed::<AuraId>("Bob"),
get_collator_keys_from_seed::<BabeId>("Bob"),
),
],
vec![
Expand All @@ -166,7 +173,7 @@ pub fn local_testnet_config() -> ChainSpec {
}

fn testnet_genesis(
invulnerables: Vec<(AccountId, AuraId)>,
invulnerables: Vec<(AccountId, AuraId, BabeId)>,
endowed_accounts: Vec<AccountId>,
root: AccountId,
id: ParaId,
Expand All @@ -179,17 +186,17 @@ fn testnet_genesis(
"parachainId": id,
},
"collatorSelection": {
"invulnerables": invulnerables.iter().cloned().map(|(acc, _)| acc).collect::<Vec<_>>(),
"invulnerables": invulnerables.iter().cloned().map(|(acc, _collator_aura, _collator_babe)| acc).collect::<Vec<_>>(),
"candidacyBond": EXISTENTIAL_DEPOSIT * 16,
},
"session": {
"keys": invulnerables
.into_iter()
.map(|(acc, aura)| {
.map(|x| {
(
acc.clone(), // account id
acc, // validator id
template_session_keys(aura), // session keys
x.0.clone(), // account id
x.0.clone(), // validator id
template_session_keys(x.1.clone(), x.2.clone()), // session keys
)
})
.collect::<Vec<_>>(),
Expand Down
2 changes: 2 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pallet-transaction-payment-rpc-runtime-api = { workspace = true }
sp-api = { workspace = true }
sp-block-builder = { workspace = true }
sp-consensus-aura = { workspace = true }
sp-consensus-babe = { workspace = true }
sp-core = { workspace = true }
sp-genesis-builder = { workspace = true }
sp-inherents = { workspace = true }
Expand Down Expand Up @@ -118,6 +119,7 @@ std = [
"sp-api/std",
"sp-block-builder/std",
"sp-consensus-aura/std",
"sp-consensus-babe/std",
"sp-core/std",
"sp-genesis-builder/std",
"sp-inherents/std",
Expand Down
34 changes: 34 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use frame_system::{
use pallet_xcm::{EnsureXcm, IsVoiceOfBody};
use parachains_common::message_queue::{NarrowOriginToSibling, ParaIdToSibling};
pub use sp_consensus_aura::sr25519::AuthorityId as AuraId;
pub use sp_consensus_babe::AuthorityId as BabeId;
pub use sp_runtime::{MultiAddress, Perbill, Permill};
use xcm_config::{RelayLocation, XcmOriginToTransactDispatchOrigin};

Expand Down Expand Up @@ -178,6 +179,7 @@ pub mod opaque {
impl_opaque_keys! {
pub struct SessionKeys {
pub aura: Aura,
pub babe: Babe,
}
}

Expand Down Expand Up @@ -635,6 +637,38 @@ impl_runtime_apis! {
}
}

impl sp_consensus_babe::BabeApi<Block> for Runtime {
fn configuration() -> sp_consensus_babe::BabeConfiguration {
unimplemented!();
}

fn current_epoch_start() -> sp_consensus_babe::Slot {
Babe::current_epoch_start()
}

fn current_epoch() -> sp_consensus_babe::Epoch {
Babe::current_epoch()
}

fn next_epoch() -> sp_consensus_babe::Epoch {
Babe::next_epoch()
}

fn generate_key_ownership_proof(
_slot: sp_consensus_babe::Slot,
authority_id: sp_consensus_babe::AuthorityId,
) -> Option<sp_consensus_babe::OpaqueKeyOwnershipProof> {
unimplemented!();
}

fn submit_report_equivocation_unsigned_extrinsic(
equivocation_proof: sp_consensus_babe::EquivocationProof<<Block as BlockT>::Header>,
key_owner_proof: sp_consensus_babe::OpaqueKeyOwnershipProof,
) -> Option<()> {
None
}
}

impl sp_api::Core<Block> for Runtime {
fn version() -> RuntimeVersion {
VERSION
Expand Down

0 comments on commit fa0a4d6

Please sign in to comment.