Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions crates/humanode-peer/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//! Command line interface.

mod root;
mod run;
mod runner;
mod subcommand;

pub use root::*;
pub use run::*;
pub use runner::*;
pub use subcommand::*;
74 changes: 74 additions & 0 deletions crates/humanode-peer/src/cli/root.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//! Commands hierarchy root.

use sc_cli::{ChainSpec, CliConfiguration, RuntimeVersion, SubstrateCli};
use structopt::StructOpt;

use crate::chain_spec;

use super::{runner::Runner, subcommand::Subcommand};

/// The root of the CLI commands hierarchy.
#[derive(Debug, StructOpt)]
pub struct Root {
/// Additional subcommands.
#[structopt(subcommand)]
pub subcommand: Option<Subcommand>,

/// The `run` command used to run a node.
#[structopt(flatten)]
pub run: sc_cli::RunCmd,
}

impl SubstrateCli for Root {
fn impl_name() -> String {
"Humanode Node".into()
}

fn impl_version() -> String {
"0".to_owned()
}

fn description() -> String {
"Biologically verified human-nodes as a basis for a fair financial system.".into()
}

fn author() -> String {
env!("CARGO_PKG_AUTHORS").into()
}

fn support_url() -> String {
"https://github.com/humanode-network/humanode/issues/new".into()
}

fn copyright_start_year() -> i32 {
2021
}

fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn ChainSpec>, String> {
if id != "local" && !id.is_empty() {
return Err(format!(
"chain {:?} is not supported, only {:?} is currently available",
id, "local"
));
}

Ok(Box::new(chain_spec::local_testnet_config()?))
}

fn native_runtime_version(_chain_spec: &Box<dyn ChainSpec>) -> &'static RuntimeVersion {
&humanode_runtime::VERSION
}
}

impl Root {
/// Create a [`Runner`] for the command provided in argument.
/// This will create a [`Configuration`] from the command line arguments and the rest of
/// the environemnt.
pub fn create_humanode_runner<T: CliConfiguration>(
&self,
command: &T,
) -> sc_cli::Result<Runner<Self>> {
command.init::<Self>()?;
Runner::new(self, command)
}
}
Original file line number Diff line number Diff line change
@@ -1,80 +1,24 @@
//! Command line arguments processing.

use crate::{
chain_spec,
cli::{Cli, Subcommand},
runner, service,
};
//! The main entrypoint.

use humanode_runtime::Block;
use sc_cli::{ChainSpec, CliConfiguration, RuntimeVersion, SubstrateCli};
use sc_service::PartialComponents;

impl SubstrateCli for Cli {
fn impl_name() -> String {
"Humanode Node".into()
}

fn impl_version() -> String {
"0".to_owned()
}

fn description() -> String {
"Biologically verified human-nodes as a basis for a fair financial system.".into()
}
use crate::service;

fn author() -> String {
env!("CARGO_PKG_AUTHORS").into()
}

fn support_url() -> String {
"https://github.com/humanode-network/humanode/issues/new".into()
}

fn copyright_start_year() -> i32 {
2021
}

fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn ChainSpec>, String> {
if id != "local" && !id.is_empty() {
return Err(format!(
"chain {:?} is not supported, only {:?} is currently available",
id, "local"
));
}

Ok(Box::new(chain_spec::local_testnet_config()?))
}

fn native_runtime_version(_chain_spec: &Box<dyn ChainSpec>) -> &'static RuntimeVersion {
&humanode_runtime::VERSION
}
}

impl Cli {
/// Create a runner for the command provided in argument. This will create a Configuration and
/// a tokio runtime
fn create_humanode_runner<T: CliConfiguration>(
&self,
command: &T,
) -> sc_cli::Result<runner::Runner<Self>> {
command.init::<Self>()?;
runner::Runner::new(self, command)
}
}
use super::{Root, Subcommand};

/// Parse and run command line arguments
/// Parse command line arguments and run the requested operation.
pub async fn run() -> sc_cli::Result<()> {
let cli = Cli::from_args();
let root: Root = sc_cli::SubstrateCli::from_args();

match &cli.subcommand {
Some(Subcommand::Key(cmd)) => cmd.run(&cli),
match &root.subcommand {
Some(Subcommand::Key(cmd)) => cmd.run(&root),
Some(Subcommand::BuildSpec(cmd)) => {
let runner = cli.create_humanode_runner(cmd)?;
let runner = root.create_humanode_runner(cmd)?;
runner.sync_run(|config| cmd.run(config.chain_spec, config.network))
}
Some(Subcommand::CheckBlock(cmd)) => {
let runner = cli.create_humanode_runner(cmd)?;
let runner = root.create_humanode_runner(cmd)?;
runner
.async_run(|config| async move {
let PartialComponents {
Expand All @@ -88,7 +32,7 @@ pub async fn run() -> sc_cli::Result<()> {
.await
}
Some(Subcommand::ExportBlocks(cmd)) => {
let runner = cli.create_humanode_runner(cmd)?;
let runner = root.create_humanode_runner(cmd)?;
runner
.async_run(|config| async move {
let PartialComponents {
Expand All @@ -101,7 +45,7 @@ pub async fn run() -> sc_cli::Result<()> {
.await
}
Some(Subcommand::ExportState(cmd)) => {
let runner = cli.create_humanode_runner(cmd)?;
let runner = root.create_humanode_runner(cmd)?;
runner
.async_run(|config| async move {
let PartialComponents {
Expand All @@ -114,7 +58,7 @@ pub async fn run() -> sc_cli::Result<()> {
.await
}
Some(Subcommand::ImportBlocks(cmd)) => {
let runner = cli.create_humanode_runner(cmd)?;
let runner = root.create_humanode_runner(cmd)?;
runner
.async_run(|config| async move {
let PartialComponents {
Expand All @@ -128,11 +72,11 @@ pub async fn run() -> sc_cli::Result<()> {
.await
}
Some(Subcommand::PurgeChain(cmd)) => {
let runner = cli.create_humanode_runner(cmd)?;
let runner = root.create_humanode_runner(cmd)?;
runner.sync_run(|config| cmd.run(config.database))
}
Some(Subcommand::Revert(cmd)) => {
let runner = cli.create_humanode_runner(cmd)?;
let runner = root.create_humanode_runner(cmd)?;
runner
.async_run(|config| async move {
let PartialComponents {
Expand All @@ -147,7 +91,7 @@ pub async fn run() -> sc_cli::Result<()> {
}
Some(Subcommand::Benchmark(cmd)) => {
if cfg!(feature = "runtime-benchmarks") {
let runner = cli.create_humanode_runner(cmd)?;
let runner = root.create_humanode_runner(cmd)?;
runner.sync_run(|config| cmd.run::<Block, service::Executor>(config))
} else {
Err(
Expand All @@ -158,8 +102,8 @@ pub async fn run() -> sc_cli::Result<()> {
}
}
None => {
let runner = cli.create_humanode_runner(&cli.run)?;
sc_cli::print_node_infos::<Cli>(runner.config());
let runner = root.create_humanode_runner(&root.run)?;
sc_cli::print_node_infos::<Root>(runner.config());
runner
.run_node(|config| async move {
service::new_full(config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl<C: SubstrateCli> Runner<C> {
runner(self.config)
}

/// Get an immutable reference to the node Configuration
/// Get an immutable reference to the node [`Configuration`].
pub fn config(&self) -> &Configuration {
&self.config
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
//! Substrate-based Cli definition.
//! Humanode peer subcommands.
//! The `substrate` built-in commands are embedded as-is, additional commands are introduced as
//! nested `mod`s in this `mod`.

use sc_cli::RunCmd;
use structopt::StructOpt;

/// A cli struct.
#[derive(Debug, StructOpt)]
pub struct Cli {
/// Additional subcommands.
#[structopt(subcommand)]
pub subcommand: Option<Subcommand>,

/// The `run` command used to run a node.
#[structopt(flatten)]
pub run: RunCmd,
}

/// Subcommand enum.
/// Humanode peer subcommands.
#[derive(Debug, StructOpt)]
pub enum Subcommand {
/// Key management cli utilities
Expand Down
4 changes: 1 addition & 3 deletions crates/humanode-peer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

mod chain_spec;
mod cli;
mod command;
mod qrcode;
mod runner;
mod service;
mod validator_key;

#[tokio::main]
async fn main() -> sc_cli::Result<()> {
command::run().await
cli::run().await
}