Skip to content

Commit 7f7f5fa

Browse files
authored
polkadot-parachain-bin: small cosmetics and improvements (#4666)
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
1 parent 7b6b783 commit 7f7f5fa

File tree

10 files changed

+192
-165
lines changed

10 files changed

+192
-165
lines changed

cumulus/polkadot-parachain/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ path = "src/main.rs"
1818
async-trait = "0.1.79"
1919
clap = { version = "4.5.3", features = ["derive"] }
2020
codec = { package = "parity-scale-codec", version = "3.6.12" }
21+
color-print = "0.3.4"
2122
futures = "0.3.28"
2223
hex-literal = "0.4.1"
2324
log = { workspace = true, default-features = true }
@@ -111,7 +112,6 @@ cumulus-client-service = { path = "../client/service" }
111112
cumulus-primitives-aura = { path = "../primitives/aura" }
112113
cumulus-primitives-core = { path = "../primitives/core" }
113114
cumulus-relay-chain-interface = { path = "../client/relay-chain-interface" }
114-
color-print = "0.3.4"
115115

116116
[build-dependencies]
117117
substrate-build-script-utils = { path = "../../substrate/utils/build-script-utils" }

cumulus/polkadot-parachain/src/cli.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
1616

17+
use clap::{CommandFactory, FromArgMatches};
1718
use std::path::PathBuf;
1819

1920
/// Sub-commands supported by the collator.
@@ -108,18 +109,19 @@ pub struct RelayChainCli {
108109
}
109110

110111
impl RelayChainCli {
111-
/// Parse the relay chain CLI parameters using the para chain `Configuration`.
112+
/// Parse the relay chain CLI parameters using the parachain `Configuration`.
112113
pub fn new<'a>(
113114
para_config: &sc_service::Configuration,
114115
relay_chain_args: impl Iterator<Item = &'a String>,
115116
) -> Self {
117+
let polkadot_cmd = polkadot_cli::RunCmd::command().no_binary_name(true);
118+
let matches = polkadot_cmd.get_matches_from(relay_chain_args);
119+
let base = FromArgMatches::from_arg_matches(&matches).unwrap_or_else(|e| e.exit());
120+
116121
let extension = crate::chain_spec::Extensions::try_get(&*para_config.chain_spec);
117122
let chain_id = extension.map(|e| e.relay_chain.clone());
123+
118124
let base_path = para_config.base_path.path().join("polkadot");
119-
Self {
120-
base_path: Some(base_path),
121-
chain_id,
122-
base: clap::Parser::parse_from(relay_chain_args),
123-
}
125+
Self { base, chain_id, base_path: Some(base_path) }
124126
}
125127
}

cumulus/polkadot-parachain/src/command.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -530,13 +530,9 @@ pub fn run() -> Result<()> {
530530
}),
531531
Some(Subcommand::PurgeChain(cmd)) => {
532532
let runner = cli.create_runner(cmd)?;
533+
let polkadot_cli = RelayChainCli::new(runner.config(), cli.relaychain_args.iter());
533534

534535
runner.sync_run(|config| {
535-
let polkadot_cli = RelayChainCli::new(
536-
&config,
537-
[RelayChainCli::executable_name()].iter().chain(cli.relaychain_args.iter()),
538-
);
539-
540536
let polkadot_config = SubstrateCli::create_configuration(
541537
&polkadot_cli,
542538
&polkadot_cli,
@@ -603,6 +599,7 @@ pub fn run() -> Result<()> {
603599
Some(Subcommand::Key(cmd)) => Ok(cmd.run(&cli)?),
604600
None => {
605601
let runner = cli.create_runner(&cli.run.normalize())?;
602+
let polkadot_cli = RelayChainCli::new(runner.config(), cli.relaychain_args.iter());
606603
let collator_options = cli.run.collator_options();
607604

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

651-
let polkadot_cli = RelayChainCli::new(
652-
&config,
653-
[RelayChainCli::executable_name()].iter().chain(cli.relaychain_args.iter()),
654-
);
655-
656648
let id = ParaId::from(para_id);
657649

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

670-
match polkadot_config.network.network_backend {
662+
match config.network.network_backend {
671663
sc_network::config::NetworkBackendType::Libp2p =>
672664
start_node::<sc_network::NetworkWorker<_, _>>(
673665
config,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (C) Parity Technologies (UK) Ltd.
2+
// This file is part of Cumulus.
3+
4+
// Cumulus is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Cumulus is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//! Aura-related primitives for cumulus parachain collators.
18+
19+
use codec::Codec;
20+
use cumulus_primitives_aura::AuraUnincludedSegmentApi;
21+
use cumulus_primitives_core::BlockT;
22+
use sp_consensus_aura::AuraApi;
23+
use sp_runtime::app_crypto::{AppCrypto, AppPair, AppSignature, Pair};
24+
25+
/// Convenience trait for defining the basic bounds of an `AuraId`.
26+
pub trait AuraIdT: AppCrypto<Pair = Self::BoundedPair> + Codec + Send {
27+
/// Extra bounds for the `Pair`.
28+
type BoundedPair: AppPair + AppCrypto<Signature = Self::BoundedSignature>;
29+
30+
/// Extra bounds for the `Signature`.
31+
type BoundedSignature: AppSignature
32+
+ TryFrom<Vec<u8>>
33+
+ std::hash::Hash
34+
+ sp_runtime::traits::Member
35+
+ Codec;
36+
}
37+
38+
impl<T> AuraIdT for T
39+
where
40+
T: AppCrypto + Codec + Send + Sync,
41+
<<T as AppCrypto>::Pair as AppCrypto>::Signature:
42+
TryFrom<Vec<u8>> + std::hash::Hash + sp_runtime::traits::Member + Codec,
43+
{
44+
type BoundedPair = <T as AppCrypto>::Pair;
45+
type BoundedSignature = <<T as AppCrypto>::Pair as AppCrypto>::Signature;
46+
}
47+
48+
/// Convenience trait for defining the basic bounds of a parachain runtime that supports
49+
/// the Aura consensus.
50+
pub trait AuraRuntimeApi<Block: BlockT, AuraId: AuraIdT>:
51+
sp_api::ApiExt<Block>
52+
+ AuraApi<Block, <AuraId::BoundedPair as Pair>::Public>
53+
+ AuraUnincludedSegmentApi<Block>
54+
+ Sized
55+
{
56+
/// Check if the runtime has the Aura API.
57+
fn has_aura_api(&self, at: Block::Hash) -> bool {
58+
self.has_api::<dyn AuraApi<Block, <AuraId::BoundedPair as Pair>::Public>>(at)
59+
.unwrap_or(false)
60+
}
61+
}
62+
63+
impl<T, Block: BlockT, AuraId: AuraIdT> AuraRuntimeApi<Block, AuraId> for T where
64+
T: sp_api::ApiExt<Block>
65+
+ AuraApi<Block, <AuraId::BoundedPair as Pair>::Public>
66+
+ AuraUnincludedSegmentApi<Block>
67+
{
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (C) Parity Technologies (UK) Ltd.
2+
// This file is part of Cumulus.
3+
4+
// Cumulus is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Cumulus is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//! Cumulus parachain collator primitives.
18+
19+
#![warn(missing_docs)]
20+
21+
pub mod aura;
22+
23+
use cumulus_primitives_core::CollectCollationInfo;
24+
use sp_api::{ApiExt, CallApiAt, ConstructRuntimeApi, Metadata};
25+
use sp_block_builder::BlockBuilder;
26+
use sp_runtime::traits::Block as BlockT;
27+
use sp_session::SessionKeys;
28+
use sp_transaction_pool::runtime_api::TaggedTransactionQueue;
29+
30+
/// Convenience trait that defines the basic bounds for the `RuntimeApi` of a parachain node.
31+
pub trait NodeRuntimeApi<Block: BlockT>:
32+
ApiExt<Block>
33+
+ Metadata<Block>
34+
+ SessionKeys<Block>
35+
+ BlockBuilder<Block>
36+
+ TaggedTransactionQueue<Block>
37+
+ CollectCollationInfo<Block>
38+
+ Sized
39+
{
40+
}
41+
42+
impl<T, Block: BlockT> NodeRuntimeApi<Block> for T where
43+
T: ApiExt<Block>
44+
+ Metadata<Block>
45+
+ SessionKeys<Block>
46+
+ BlockBuilder<Block>
47+
+ TaggedTransactionQueue<Block>
48+
+ CollectCollationInfo<Block>
49+
{
50+
}
51+
52+
/// Convenience trait that defines the basic bounds for the `ConstructRuntimeApi` of a parachain
53+
/// node.
54+
pub trait ConstructNodeRuntimeApi<Block: BlockT, C: CallApiAt<Block>>:
55+
ConstructRuntimeApi<Block, C, RuntimeApi = Self::BoundedRuntimeApi> + Send + Sync + 'static
56+
{
57+
/// Basic bounds for the `RuntimeApi` of a parachain node.
58+
type BoundedRuntimeApi: NodeRuntimeApi<Block>;
59+
}
60+
61+
impl<T, Block: BlockT, C: CallApiAt<Block>> ConstructNodeRuntimeApi<Block, C> for T
62+
where
63+
T: ConstructRuntimeApi<Block, C> + Send + Sync + 'static,
64+
T::RuntimeApi: NodeRuntimeApi<Block>,
65+
{
66+
type BoundedRuntimeApi = T::RuntimeApi;
67+
}

cumulus/polkadot-parachain/src/fake_runtime_api/asset_hub_polkadot_aura.rs

-6
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ sp_api::impl_runtime_apis! {
105105
}
106106
}
107107

108-
impl sp_offchain::OffchainWorkerApi<Block> for Runtime {
109-
fn offchain_worker(_: &<Block as BlockT>::Header) {
110-
unimplemented!()
111-
}
112-
}
113-
114108
impl sp_session::SessionKeys<Block> for Runtime {
115109
fn generate_session_keys(_: Option<Vec<u8>>) -> Vec<u8> {
116110
unimplemented!()

cumulus/polkadot-parachain/src/fake_runtime_api/aura.rs

-6
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ sp_api::impl_runtime_apis! {
105105
}
106106
}
107107

108-
impl sp_offchain::OffchainWorkerApi<Block> for Runtime {
109-
fn offchain_worker(_: &<Block as BlockT>::Header) {
110-
unimplemented!()
111-
}
112-
}
113-
114108
impl sp_session::SessionKeys<Block> for Runtime {
115109
fn generate_session_keys(_: Option<Vec<u8>>) -> Vec<u8> {
116110
unimplemented!()

cumulus/polkadot-parachain/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
mod chain_spec;
2323
mod cli;
2424
mod command;
25+
mod common;
2526
mod fake_runtime_api;
2627
mod rpc;
2728
mod service;

0 commit comments

Comments
 (0)