Skip to content

Commit

Permalink
Merge pull request #126 from AurevoirXavier/darwinia-cli
Browse files Browse the repository at this point in the history
Darwinia cli
  • Loading branch information
hackfisher authored Nov 29, 2019
2 parents d5106b1 + acfb4e6 commit 4f3b6f3
Show file tree
Hide file tree
Showing 16 changed files with 2,664 additions and 25 deletions.
39 changes: 38 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ panic = 'unwind'

[workspace]
members = [
"core/cli",
"core/merkle-mountain-range",
"core/fly-client",
"core/sr-eth-primitives",
Expand Down
47 changes: 47 additions & 0 deletions core/cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[package]
name = "darwinia-cli"
version = "0.1.0"
authors = ["Darwinia Network <[email protected]>"]
description = "Darwinia CLI interface."
edition = "2018"

[dependencies]
ansi_term = "0.12.1"
app_dirs = "1.2.1"
atty = "0.2.13"
clap = "2.33.0"
derive_more = "0.15.0"
env_logger = "0.7.0"
exit-future = "0.1.4"
futures = "0.1.29"
futures03 = { package = "futures-preview", version = "=0.3.0-alpha.19", features = ["compat"] }
fdlimit = "0.1.1"
lazy_static = "1.4.0"
log = "0.4.8"
names = "0.11.0"
regex = "1.3.1"
rpassword = "4.0.1"
serde = "1.0.103"
serde_json = "1.0.41"
structopt = "0.3.3"
time = "0.1.42"
tokio = "0.1.22"

panic-handler = { package = "substrate-panic-handler", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
client = { package = "substrate-client", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
header-metadata = { package = "substrate-header-metadata", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
network = { package = "substrate-network", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
sr-primitives = { git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
primitives = { package = "substrate-primitives", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
service = { package = "substrate-service", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
state-machine = { package = "substrate-state-machine", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
substrate-telemetry = { git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
keyring = { package = "substrate-keyring", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }

[dev-dependencies]
tempdir = "0.3.7"

[features]
wasmtime = [
"service/wasmtime",
]
64 changes: 64 additions & 0 deletions core/cli/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate 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.

// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Initialization errors.

use client;

/// Result type alias for the CLI.
pub type Result<T> = std::result::Result<T, Error>;

/// Error type for the CLI.
#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum Error {
/// Io error
Io(std::io::Error),
/// Cli error
Cli(clap::Error),
/// Service error
Service(service::Error),
/// Client error
Client(client::error::Error),
/// Input error
Input(String),
/// Invalid listen multiaddress
#[display(fmt = "Invalid listen multiaddress")]
InvalidListenMultiaddress,
/// Other uncategorized error.
Other(String),
}

/// Must be implemented explicitly because `derive_more` won't generate this
/// case due to conflicting derive for `Other(String)`.
impl std::convert::From<String> for Error {
fn from(s: String) -> Error {
Error::Input(s)
}
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(ref err) => Some(err),
Error::Cli(ref err) => Some(err),
Error::Service(ref err) => Some(err),
Error::Client(ref err) => Some(err),
Error::Input(_) => None,
Error::InvalidListenMultiaddress => None,
Error::Other(_) => None,
}
}
}
36 changes: 36 additions & 0 deletions core/cli/src/execution_strategy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate 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.

// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.

#![allow(missing_docs)]

use serde::Deserialize;
use structopt::clap::arg_enum;

arg_enum! {
/// How to execute blocks
#[derive(Clone, Copy, Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ExecutionStrategy {
// Execute with native build (if available, WebAssembly otherwise).
Native,
// Only execute with the WebAssembly build.
Wasm,
// Execute with both native (where available) and WebAssembly builds.
Both,
// Execute with the native build if possible; if it fails, then execute with WebAssembly.
NativeElseWasm,
}
}
84 changes: 84 additions & 0 deletions core/cli/src/informant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate 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.

// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Console informant. Prints sync progress and block events. Runs on the calling thread.

use client::BlockchainEvents;
use futures::{Future, Stream};
use futures03::{StreamExt as _, TryStreamExt as _};
use log::{info, warn};
use service::AbstractService;
use sr_primitives::traits::Header;
use std::time::Duration;

mod display;

/// Creates an informant in the form of a `Future` that must be polled regularly.
pub fn build(service: &impl AbstractService) -> impl Future<Item = (), Error = ()> {
let client = service.client();

let mut display = display::InformantDisplay::new();

let display_notifications = service
.network_status(Duration::from_millis(5000))
.for_each(move |(net_status, _)| {
let info = client.info();
display.display(&info, net_status);
Ok(())
});

let client = service.client();
let mut last_best = {
let info = client.info();
Some((info.chain.best_number, info.chain.best_hash))
};

let display_block_import = client
.import_notification_stream()
.map(|v| Ok::<_, ()>(v))
.compat()
.for_each(move |n| {
// detect and log reorganizations.
if let Some((ref last_num, ref last_hash)) = last_best {
if n.header.parent_hash() != last_hash && n.is_new_best {
let maybe_ancestor = header_metadata::lowest_common_ancestor(&*client, last_hash.clone(), n.hash);

match maybe_ancestor {
Ok(ref ancestor) if ancestor.hash != *last_hash => info!(
"Reorg from #{},{} to #{},{}, common ancestor #{},{}",
last_num,
last_hash,
n.header.number(),
n.hash,
ancestor.number,
ancestor.hash,
),
Ok(_) => {}
Err(e) => warn!("Error computing tree route: {}", e),
}
}
}

if n.is_new_best {
last_best = Some((n.header.number().clone(), n.hash.clone()));
}

info!(target: "substrate", "Imported #{} ({})", n.header.number(), n.hash);
Ok(())
});

display_notifications.join(display_block_import).map(|((), ())| ())
}
Loading

0 comments on commit 4f3b6f3

Please sign in to comment.