Skip to content

Commit

Permalink
polkadot-parachain-bin: small cosmetics and improvements (#4666)
Browse files Browse the repository at this point in the history
Related to: #5

A couple of cosmetics and improvements related to
`polkadot-parachain-bin`:

- Adding some convenience traits in order to avoid declaring long
duplicate bounds
- Specifically check if the runtime exposes `AuraApi` when executing
`start_lookahead_aura_consensus()`
- Some fixes for the `RelayChainCli`. Details in the commits description
  • Loading branch information
serban300 authored Jun 14, 2024
1 parent 7b6b783 commit 7f7f5fa
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 165 deletions.
2 changes: 1 addition & 1 deletion cumulus/polkadot-parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ path = "src/main.rs"
async-trait = "0.1.79"
clap = { version = "4.5.3", features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.6.12" }
color-print = "0.3.4"
futures = "0.3.28"
hex-literal = "0.4.1"
log = { workspace = true, default-features = true }
Expand Down Expand Up @@ -111,7 +112,6 @@ cumulus-client-service = { path = "../client/service" }
cumulus-primitives-aura = { path = "../primitives/aura" }
cumulus-primitives-core = { path = "../primitives/core" }
cumulus-relay-chain-interface = { path = "../client/relay-chain-interface" }
color-print = "0.3.4"

[build-dependencies]
substrate-build-script-utils = { path = "../../substrate/utils/build-script-utils" }
Expand Down
14 changes: 8 additions & 6 deletions cumulus/polkadot-parachain/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.

use clap::{CommandFactory, FromArgMatches};
use std::path::PathBuf;

/// Sub-commands supported by the collator.
Expand Down Expand Up @@ -108,18 +109,19 @@ pub struct RelayChainCli {
}

impl RelayChainCli {
/// Parse the relay chain CLI parameters using the para chain `Configuration`.
/// Parse the relay chain CLI parameters using the parachain `Configuration`.
pub fn new<'a>(
para_config: &sc_service::Configuration,
relay_chain_args: impl Iterator<Item = &'a String>,
) -> Self {
let polkadot_cmd = polkadot_cli::RunCmd::command().no_binary_name(true);
let matches = polkadot_cmd.get_matches_from(relay_chain_args);
let base = FromArgMatches::from_arg_matches(&matches).unwrap_or_else(|e| e.exit());

let extension = crate::chain_spec::Extensions::try_get(&*para_config.chain_spec);
let chain_id = extension.map(|e| e.relay_chain.clone());

let base_path = para_config.base_path.path().join("polkadot");
Self {
base_path: Some(base_path),
chain_id,
base: clap::Parser::parse_from(relay_chain_args),
}
Self { base, chain_id, base_path: Some(base_path) }
}
}
14 changes: 3 additions & 11 deletions cumulus/polkadot-parachain/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,13 +530,9 @@ pub fn run() -> Result<()> {
}),
Some(Subcommand::PurgeChain(cmd)) => {
let runner = cli.create_runner(cmd)?;
let polkadot_cli = RelayChainCli::new(runner.config(), cli.relaychain_args.iter());

runner.sync_run(|config| {
let polkadot_cli = RelayChainCli::new(
&config,
[RelayChainCli::executable_name()].iter().chain(cli.relaychain_args.iter()),
);

let polkadot_config = SubstrateCli::create_configuration(
&polkadot_cli,
&polkadot_cli,
Expand Down Expand Up @@ -603,6 +599,7 @@ pub fn run() -> Result<()> {
Some(Subcommand::Key(cmd)) => Ok(cmd.run(&cli)?),
None => {
let runner = cli.create_runner(&cli.run.normalize())?;
let polkadot_cli = RelayChainCli::new(runner.config(), cli.relaychain_args.iter());
let collator_options = cli.run.collator_options();

runner.run_node_until_exit(|config| async move {
Expand Down Expand Up @@ -648,11 +645,6 @@ pub fn run() -> Result<()> {
.map(|e| e.para_id)
.ok_or("Could not find parachain extension in chain-spec.")?;

let polkadot_cli = RelayChainCli::new(
&config,
[RelayChainCli::executable_name()].iter().chain(cli.relaychain_args.iter()),
);

let id = ParaId::from(para_id);

let parachain_account =
Expand All @@ -667,7 +659,7 @@ pub fn run() -> Result<()> {
info!("Parachain Account: {}", parachain_account);
info!("Is collating: {}", if config.role.is_authority() { "yes" } else { "no" });

match polkadot_config.network.network_backend {
match config.network.network_backend {
sc_network::config::NetworkBackendType::Libp2p =>
start_node::<sc_network::NetworkWorker<_, _>>(
config,
Expand Down
68 changes: 68 additions & 0 deletions cumulus/polkadot-parachain/src/common/aura.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Cumulus.

// Cumulus is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Cumulus is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.

//! Aura-related primitives for cumulus parachain collators.

use codec::Codec;
use cumulus_primitives_aura::AuraUnincludedSegmentApi;
use cumulus_primitives_core::BlockT;
use sp_consensus_aura::AuraApi;
use sp_runtime::app_crypto::{AppCrypto, AppPair, AppSignature, Pair};

/// Convenience trait for defining the basic bounds of an `AuraId`.
pub trait AuraIdT: AppCrypto<Pair = Self::BoundedPair> + Codec + Send {
/// Extra bounds for the `Pair`.
type BoundedPair: AppPair + AppCrypto<Signature = Self::BoundedSignature>;

/// Extra bounds for the `Signature`.
type BoundedSignature: AppSignature
+ TryFrom<Vec<u8>>
+ std::hash::Hash
+ sp_runtime::traits::Member
+ Codec;
}

impl<T> AuraIdT for T
where
T: AppCrypto + Codec + Send + Sync,
<<T as AppCrypto>::Pair as AppCrypto>::Signature:
TryFrom<Vec<u8>> + std::hash::Hash + sp_runtime::traits::Member + Codec,
{
type BoundedPair = <T as AppCrypto>::Pair;
type BoundedSignature = <<T as AppCrypto>::Pair as AppCrypto>::Signature;
}

/// Convenience trait for defining the basic bounds of a parachain runtime that supports
/// the Aura consensus.
pub trait AuraRuntimeApi<Block: BlockT, AuraId: AuraIdT>:
sp_api::ApiExt<Block>
+ AuraApi<Block, <AuraId::BoundedPair as Pair>::Public>
+ AuraUnincludedSegmentApi<Block>
+ Sized
{
/// Check if the runtime has the Aura API.
fn has_aura_api(&self, at: Block::Hash) -> bool {
self.has_api::<dyn AuraApi<Block, <AuraId::BoundedPair as Pair>::Public>>(at)
.unwrap_or(false)
}
}

impl<T, Block: BlockT, AuraId: AuraIdT> AuraRuntimeApi<Block, AuraId> for T where
T: sp_api::ApiExt<Block>
+ AuraApi<Block, <AuraId::BoundedPair as Pair>::Public>
+ AuraUnincludedSegmentApi<Block>
{
}
67 changes: 67 additions & 0 deletions cumulus/polkadot-parachain/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Cumulus.

// Cumulus is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Cumulus is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.

//! Cumulus parachain collator primitives.

#![warn(missing_docs)]

pub mod aura;

use cumulus_primitives_core::CollectCollationInfo;
use sp_api::{ApiExt, CallApiAt, ConstructRuntimeApi, Metadata};
use sp_block_builder::BlockBuilder;
use sp_runtime::traits::Block as BlockT;
use sp_session::SessionKeys;
use sp_transaction_pool::runtime_api::TaggedTransactionQueue;

/// Convenience trait that defines the basic bounds for the `RuntimeApi` of a parachain node.
pub trait NodeRuntimeApi<Block: BlockT>:
ApiExt<Block>
+ Metadata<Block>
+ SessionKeys<Block>
+ BlockBuilder<Block>
+ TaggedTransactionQueue<Block>
+ CollectCollationInfo<Block>
+ Sized
{
}

impl<T, Block: BlockT> NodeRuntimeApi<Block> for T where
T: ApiExt<Block>
+ Metadata<Block>
+ SessionKeys<Block>
+ BlockBuilder<Block>
+ TaggedTransactionQueue<Block>
+ CollectCollationInfo<Block>
{
}

/// Convenience trait that defines the basic bounds for the `ConstructRuntimeApi` of a parachain
/// node.
pub trait ConstructNodeRuntimeApi<Block: BlockT, C: CallApiAt<Block>>:
ConstructRuntimeApi<Block, C, RuntimeApi = Self::BoundedRuntimeApi> + Send + Sync + 'static
{
/// Basic bounds for the `RuntimeApi` of a parachain node.
type BoundedRuntimeApi: NodeRuntimeApi<Block>;
}

impl<T, Block: BlockT, C: CallApiAt<Block>> ConstructNodeRuntimeApi<Block, C> for T
where
T: ConstructRuntimeApi<Block, C> + Send + Sync + 'static,
T::RuntimeApi: NodeRuntimeApi<Block>,
{
type BoundedRuntimeApi = T::RuntimeApi;
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ sp_api::impl_runtime_apis! {
}
}

impl sp_offchain::OffchainWorkerApi<Block> for Runtime {
fn offchain_worker(_: &<Block as BlockT>::Header) {
unimplemented!()
}
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(_: Option<Vec<u8>>) -> Vec<u8> {
unimplemented!()
Expand Down
6 changes: 0 additions & 6 deletions cumulus/polkadot-parachain/src/fake_runtime_api/aura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ sp_api::impl_runtime_apis! {
}
}

impl sp_offchain::OffchainWorkerApi<Block> for Runtime {
fn offchain_worker(_: &<Block as BlockT>::Header) {
unimplemented!()
}
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(_: Option<Vec<u8>>) -> Vec<u8> {
unimplemented!()
Expand Down
1 change: 1 addition & 0 deletions cumulus/polkadot-parachain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
mod chain_spec;
mod cli;
mod command;
mod common;
mod fake_runtime_api;
mod rpc;
mod service;
Expand Down
Loading

0 comments on commit 7f7f5fa

Please sign in to comment.