Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

polkadot-parachain: Add omni-node variant with u64 block number #5269

Merged
merged 12 commits into from
Aug 28, 2024
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.

2 changes: 2 additions & 0 deletions cumulus/polkadot-parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ sp-tracing = { workspace = true, default-features = true }
frame-support = { workspace = true, default-features = true }
sc-cli = { workspace = true, default-features = true }
sc-client-api = { workspace = true, default-features = true }
sc-client-db = { workspace = true, default-features = true }
sc-executor = { workspace = true, default-features = true }
sc-service = { workspace = true, default-features = true }
sc-telemetry = { workspace = true, default-features = true }
Expand Down Expand Up @@ -149,6 +150,7 @@ runtime-benchmarks = [
"polkadot-primitives/runtime-benchmarks",
"polkadot-service/runtime-benchmarks",
"rococo-parachain-runtime/runtime-benchmarks",
"sc-client-db/runtime-benchmarks",
"sc-service/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
Expand Down
58 changes: 36 additions & 22 deletions cumulus/polkadot-parachain/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@
// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.

#[cfg(feature = "runtime-benchmarks")]
use crate::service::Block;
use crate::{
chain_spec::{self, GenericChainSpec},
cli::{Cli, RelayChainCli, Subcommand},
common::NodeExtraArgs,
common::{
spec::DynNodeSpec,
types::{Block, CustomBlock},
NodeExtraArgs,
},
fake_runtime_api,
fake_runtime_api::{
asset_hub_polkadot_aura::RuntimeApi as AssetHubPolkadotRuntimeApi,
aura::RuntimeApi as AuraRuntimeApi,
asset_hub_polkadot::RuntimeApi as AssetHubPolkadotRuntimeApi,
aura::default::RuntimeApi as AuraRuntimeApi,
},
service::{new_aura_node_spec, DynNodeSpec, ShellNode},
service::{new_aura_node_spec, ShellNode},
};
#[cfg(feature = "runtime-benchmarks")]
use cumulus_client_service::storage_proof_size::HostFunctions as ReclaimHostFunctions;
Expand All @@ -42,18 +45,22 @@ use sp_runtime::traits::AccountIdConversion;
use sp_runtime::traits::HashingFor;
use std::{net::SocketAddr, path::PathBuf};

/// The choice of block number for the parachain omni-node.
#[derive(PartialEq, Eq, Debug, Default)]
enum BlockNumber {
#[default]
U32,
// TODO: Expose this option in the CLI or read it from the chain spec if possible
#[allow(dead_code)]
U64,
}

/// The choice of consensus for the parachain omni-node.
#[derive(PartialEq, Eq, Debug, Default)]
pub enum Consensus {
enum Consensus {
/// Aura consensus.
#[default]
Aura,
/// Use the relay chain consensus.
// TODO: atm this is just a demonstration, not really reach-able. We can add it to the CLI,
// env, or the chain spec. Or, just don't, and when we properly refactor this mess we will
// re-introduce it.
#[allow(unused)]
Relay,
}

/// Helper enum that is used for better distinction of different parachain/runtime configuration
Expand All @@ -62,7 +69,7 @@ pub enum Consensus {
enum Runtime {
/// None of the system-chain runtimes, rather the node will act agnostic to the runtime ie. be
/// an omni-node, and simply run a node with the given consensus algorithm.
Omni(Consensus),
Omni(BlockNumber, Consensus),
Shell,
Seedling,
AssetHubPolkadot,
Expand Down Expand Up @@ -146,7 +153,7 @@ fn runtime(id: &str) -> Runtime {
so Runtime::Omni(Consensus::Aura) will be used",
id
);
Runtime::Omni(Consensus::Aura)
Runtime::Omni(BlockNumber::U32, Consensus::Aura)
}
}

Expand Down Expand Up @@ -391,19 +398,26 @@ fn new_node_spec(
) -> std::result::Result<Box<dyn DynNodeSpec>, sc_cli::Error> {
Ok(match config.chain_spec.runtime()? {
Runtime::AssetHubPolkadot =>
new_aura_node_spec::<AssetHubPolkadotRuntimeApi, AssetHubPolkadotAuraId>(extra_args),
new_aura_node_spec::<Block, AssetHubPolkadotRuntimeApi, AssetHubPolkadotAuraId>(
extra_args,
),
Runtime::AssetHub |
Runtime::BridgeHub(_) |
Runtime::Collectives |
Runtime::Coretime(_) |
Runtime::People(_) |
Runtime::ContractsRococo |
Runtime::Glutton |
Runtime::Penpal(_) => new_aura_node_spec::<AuraRuntimeApi, AuraId>(extra_args),
Runtime::Penpal(_) => new_aura_node_spec::<Block, AuraRuntimeApi, AuraId>(extra_args),
Runtime::Shell | Runtime::Seedling => Box::new(ShellNode),
Runtime::Omni(consensus) => match consensus {
Consensus::Aura => new_aura_node_spec::<AuraRuntimeApi, AuraId>(extra_args),
Consensus::Relay => Box::new(ShellNode),
Runtime::Omni(block_num, consensus) => match (block_num, consensus) {
(BlockNumber::U32, Consensus::Aura) =>
new_aura_node_spec::<Block, AuraRuntimeApi, AuraId>(extra_args),
(BlockNumber::U64, Consensus::Aura) => new_aura_node_spec::<
CustomBlock<u64>,
fake_runtime_api::aura::u64_block::RuntimeApi,
AuraId,
>(extra_args),
},
})
}
Expand Down Expand Up @@ -742,7 +756,7 @@ impl CliConfiguration<Self> for RelayChainCli {
mod tests {
use crate::{
chain_spec::{get_account_id_from_seed, get_from_seed},
command::{Consensus, Runtime, RuntimeResolver},
command::{BlockNumber, Consensus, Runtime, RuntimeResolver},
};
use sc_chain_spec::{ChainSpec, ChainSpecExtension, ChainSpecGroup, ChainType, Extension};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -850,6 +864,6 @@ mod tests {
&temp_dir,
&crate::chain_spec::rococo_parachain::rococo_parachain_local_config(),
);
assert_eq!(Runtime::Omni(Consensus::Aura), path.runtime().unwrap());
assert_eq!(Runtime::Omni(BlockNumber::U32, Consensus::Aura), path.runtime().unwrap());
}
}
161 changes: 161 additions & 0 deletions cumulus/polkadot-parachain/src/common/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// 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/>.

use crate::common::spec::NodeSpec;
use cumulus_client_cli::ExportGenesisHeadCommand;
use frame_benchmarking_cli::BlockCmd;
#[cfg(any(feature = "runtime-benchmarks"))]
use frame_benchmarking_cli::StorageCmd;
use sc_cli::{CheckBlockCmd, ExportBlocksCmd, ExportStateCmd, ImportBlocksCmd, RevertCmd};
use sc_service::{Configuration, TaskManager};
use std::{future::Future, pin::Pin};

type SyncCmdResult = sc_cli::Result<()>;

type AsyncCmdResult<'a> =
sc_cli::Result<(Pin<Box<dyn Future<Output = SyncCmdResult> + 'a>>, TaskManager)>;

pub trait NodeCommandRunner {
fn prepare_check_block_cmd(
self: Box<Self>,
config: Configuration,
cmd: &CheckBlockCmd,
) -> AsyncCmdResult<'_>;

fn prepare_export_blocks_cmd(
self: Box<Self>,
config: Configuration,
cmd: &ExportBlocksCmd,
) -> AsyncCmdResult<'_>;

fn prepare_export_state_cmd(
self: Box<Self>,
config: Configuration,
cmd: &ExportStateCmd,
) -> AsyncCmdResult<'_>;

fn prepare_import_blocks_cmd(
self: Box<Self>,
config: Configuration,
cmd: &ImportBlocksCmd,
) -> AsyncCmdResult<'_>;

fn prepare_revert_cmd(
self: Box<Self>,
config: Configuration,
cmd: &RevertCmd,
) -> AsyncCmdResult<'_>;

fn run_export_genesis_head_cmd(
self: Box<Self>,
config: Configuration,
cmd: &ExportGenesisHeadCommand,
) -> SyncCmdResult;

fn run_benchmark_block_cmd(
self: Box<Self>,
config: Configuration,
cmd: &BlockCmd,
) -> SyncCmdResult;

#[cfg(any(feature = "runtime-benchmarks"))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've talked about this before, so possibly it is duplicate:

we anticipate omni nodes to not have any benchmarking functionality, so I do encourage you to explore the state of this (e.g. how far we are from omni-bencher being self-sufficient) this and possibly NOT add code that we are going to remove later.

Probably what I am suggesting today is not feasible, because we use the benchmark subcommand in many places in our CI.

I am okay with you making this compromise now, but reminder that we need plan + tracking issue to remove benchmarking subcommand from here. The first step couple be keeping the subcommand but marking it as deprecated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upon further thought, I questioned my own opinion.

Keeping benchmark, try-runtime or build-spec subcommand is fine, as long as they don't require a hard dependency to the runtime. In the past they did rely on the runtime being linked as a dependency.

Especially now that we have the omni-node library in place (#5288) and teams can abstract away from needing to define these commands in e.g. struct Commands, in fact it is beneficial to keep these subcommand in the omni-node, so that teams don't need multiple binaries to do their day-to-day work.

Imagine how nicer it is to have, instead of:

  • polkadot-parachain
  • frame-omni-bencher
  • chain-spec-builder

have one called polkadot-omni-node?

It seems to me that we mainly separated these binaries because we had leaky abstraction in the node side (e.g. sturct Command that had to be updated by each and every team to support a new subcommand).

TLDR; in light of #5288 which is a step in the direction of the original vision of #5, I don't see why things like omni-bencher and chain-spec-builder cannot be part of the omni-node again.

@michalkucharczyk @ggwpez @bkchr wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have one called polkadot-omni-node?

Yeah could work. Generally I think doing this to get rid of the included chain specs etc could be a good idea. So, that we have the new binary as you proposed and can remove stuff.

Copy link
Member

@ggwpez ggwpez Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why things like omni-bencher and chain-spec-builder cannot be part of the omni-node again.

Yes the omni-node can definitely re-expose the benchmark commands with a flag. The advantage of the omni-bencher is faster compile time and smaller binary for CI. Since building a complete node just for benchmarking is a bit of a waste.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am okay with you making this compromise now, but reminder that we need plan + tracking issue to remove benchmarking subcommand from here. The first step couple be keeping the subcommand but marking it as deprecated.

Yes, agree. This is tracked here: #4966 . From what I udnerstand we still need #4405 to be merged, in order to remove the benchmarks from the node.

Especially now that we have the omni-node library in place (#5288) and teams can abstract away from needing to define these commands in e.g. struct Commands, in fact it is beneficial to keep these subcommand in the omni-node, so that teams don't need multiple binaries to do their day-to-day work.

Keeping it also sounds good.

fn run_benchmark_storage_cmd(
self: Box<Self>,
config: Configuration,
cmd: &StorageCmd,
) -> SyncCmdResult;
}

impl<T> NodeCommandRunner for T
where
T: NodeSpec,
{
fn prepare_check_block_cmd(
self: Box<Self>,
config: Configuration,
cmd: &CheckBlockCmd,
) -> AsyncCmdResult<'_> {
let partial = T::new_partial(&config).map_err(sc_cli::Error::Service)?;
Ok((Box::pin(cmd.run(partial.client, partial.import_queue)), partial.task_manager))
}

fn prepare_export_blocks_cmd(
self: Box<Self>,
config: Configuration,
cmd: &ExportBlocksCmd,
) -> AsyncCmdResult<'_> {
let partial = T::new_partial(&config).map_err(sc_cli::Error::Service)?;
Ok((Box::pin(cmd.run(partial.client, config.database)), partial.task_manager))
}

fn prepare_export_state_cmd(
self: Box<Self>,
config: Configuration,
cmd: &ExportStateCmd,
) -> AsyncCmdResult<'_> {
let partial = T::new_partial(&config).map_err(sc_cli::Error::Service)?;
Ok((Box::pin(cmd.run(partial.client, config.chain_spec)), partial.task_manager))
}

fn prepare_import_blocks_cmd(
self: Box<Self>,
config: Configuration,
cmd: &ImportBlocksCmd,
) -> AsyncCmdResult<'_> {
let partial = T::new_partial(&config).map_err(sc_cli::Error::Service)?;
Ok((Box::pin(cmd.run(partial.client, partial.import_queue)), partial.task_manager))
}

fn prepare_revert_cmd(
self: Box<Self>,
config: Configuration,
cmd: &RevertCmd,
) -> AsyncCmdResult<'_> {
let partial = T::new_partial(&config).map_err(sc_cli::Error::Service)?;
Ok((Box::pin(cmd.run(partial.client, partial.backend, None)), partial.task_manager))
}

fn run_export_genesis_head_cmd(
self: Box<Self>,
config: Configuration,
cmd: &ExportGenesisHeadCommand,
) -> SyncCmdResult {
let partial = T::new_partial(&config).map_err(sc_cli::Error::Service)?;
cmd.run(partial.client)
}

fn run_benchmark_block_cmd(
self: Box<Self>,
config: Configuration,
cmd: &BlockCmd,
) -> SyncCmdResult {
let partial = T::new_partial(&config).map_err(sc_cli::Error::Service)?;
cmd.run(partial.client)
}

#[cfg(any(feature = "runtime-benchmarks"))]
fn run_benchmark_storage_cmd(
self: Box<Self>,
config: Configuration,
cmd: &StorageCmd,
) -> SyncCmdResult {
let partial = T::new_partial(&config).map_err(sc_cli::Error::Service)?;
let db = partial.backend.expose_db();
let storage = partial.backend.expose_storage();

cmd.run(config, partial.client, db, storage)
}
}
32 changes: 30 additions & 2 deletions cumulus/polkadot-parachain/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,42 @@
#![warn(missing_docs)]

pub mod aura;
pub mod command;
pub mod rpc;
pub mod spec;
pub mod types;

use cumulus_primitives_core::CollectCollationInfo;
use sc_client_db::DbHash;
use sp_api::{ApiExt, CallApiAt, ConstructRuntimeApi, Metadata};
use sp_block_builder::BlockBuilder;
use sp_runtime::traits::Block as BlockT;
use sp_runtime::{
traits::{Block as BlockT, BlockNumber, Header as HeaderT, NumberFor},
OpaqueExtrinsic,
};
use sp_session::SessionKeys;
use sp_transaction_pool::runtime_api::TaggedTransactionQueue;
use std::path::PathBuf;
use std::{fmt::Debug, path::PathBuf, str::FromStr};

pub trait NodeBlock:
BlockT<Extrinsic = OpaqueExtrinsic, Header = Self::BoundedHeader, Hash = DbHash>
+ for<'de> serde::Deserialize<'de>
{
type BoundedFromStrErr: Debug;
type BoundedNumber: FromStr<Err = Self::BoundedFromStrErr> + BlockNumber;
type BoundedHeader: HeaderT<Number = Self::BoundedNumber> + Unpin;
}

impl<T> NodeBlock for T
where
T: BlockT<Extrinsic = OpaqueExtrinsic, Hash = DbHash> + for<'de> serde::Deserialize<'de>,
<T as BlockT>::Header: Unpin,
<NumberFor<T> as FromStr>::Err: Debug,
{
type BoundedFromStrErr = <NumberFor<T> as FromStr>::Err;
type BoundedNumber = NumberFor<T>;
type BoundedHeader = <T as BlockT>::Header;
}

/// Convenience trait that defines the basic bounds for the `RuntimeApi` of a parachain node.
pub trait NodeRuntimeApi<Block: BlockT>:
Expand Down
Loading
Loading