mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-17 00:45:42 +00:00
Substrate CLI (#423)
* Moved chain ops to substrate-service * moved RPC to susbstrate-service WIP * Moved RPC to substrate-service * Version * Removed redundadnt messages * substrate CLI * Removed obsolete files * Sorted out startup messages * Pass executable name to CLI
This commit is contained in:
committed by
Gav Wood
parent
65f0c5f3af
commit
debeedfbe8
@@ -0,0 +1,34 @@
|
||||
[package]
|
||||
name = "substrate-cli"
|
||||
version = "0.2.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
description = "Substrate CLI interface."
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "~2.32", features = ["yaml"] }
|
||||
backtrace = "0.3"
|
||||
env_logger = "0.4"
|
||||
error-chain = "0.12"
|
||||
log = "0.3"
|
||||
atty = "0.2"
|
||||
regex = "1"
|
||||
time = "0.1"
|
||||
slog = "^2"
|
||||
ansi_term = "0.10"
|
||||
lazy_static = "1.0"
|
||||
app_dirs = "1.2"
|
||||
tokio = "0.1.7"
|
||||
futures = "0.1.17"
|
||||
fdlimit = "0.1"
|
||||
exit-future = "0.1"
|
||||
substrate-client = { path = "../../substrate/client" }
|
||||
substrate-extrinsic-pool = { path = "../../substrate/extrinsic-pool" }
|
||||
substrate-network = { path = "../../substrate/network" }
|
||||
substrate-runtime-primitives = { path = "../../substrate/runtime/primitives" }
|
||||
substrate-service = { path = "../../substrate/service" }
|
||||
substrate-telemetry = { path = "../../substrate/telemetry" }
|
||||
names = "0.11.0"
|
||||
|
||||
[build-dependencies]
|
||||
clap = "~2.32"
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
= Substrate CLI
|
||||
|
||||
== Summary
|
||||
|
||||
[source, toml]
|
||||
----
|
||||
include::Cargo.toml[lines=2..5]
|
||||
----
|
||||
|
||||
include::doc/shell-completion.adoc[]
|
||||
@@ -0,0 +1,59 @@
|
||||
// Copyright 2017 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/>.
|
||||
|
||||
#[macro_use]
|
||||
extern crate clap;
|
||||
|
||||
use std::fs;
|
||||
use std::env;
|
||||
use clap::Shell;
|
||||
use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
build_shell_completion();
|
||||
}
|
||||
|
||||
/// Build shell completion scripts for all known shells
|
||||
/// Full list in https://github.com/kbknapp/clap-rs/blob/e9d0562a1dc5dfe731ed7c767e6cee0af08f0cf9/src/app/parser.rs#L123
|
||||
fn build_shell_completion() {
|
||||
let shells = [Shell::Bash, Shell::Fish, Shell::Zsh, Shell::Elvish, Shell::PowerShell];
|
||||
for shell in shells.iter() {
|
||||
build_completion(shell);
|
||||
}
|
||||
}
|
||||
|
||||
/// Build the shell auto-completion for a given Shell
|
||||
fn build_completion(shell: &Shell) {
|
||||
let yml = load_yaml!("src/cli.yml");
|
||||
|
||||
let outdir = match env::var_os("OUT_DIR") {
|
||||
None => return,
|
||||
Some(dir) => dir,
|
||||
};
|
||||
let path = Path::new(&outdir)
|
||||
.parent().unwrap()
|
||||
.parent().unwrap()
|
||||
.parent().unwrap()
|
||||
.join("completion-scripts");
|
||||
|
||||
fs::create_dir(&path).ok();
|
||||
|
||||
let mut app = clap::App::from_yaml(&yml);
|
||||
app.gen_completions(
|
||||
"polkadot",
|
||||
*shell,
|
||||
&path);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
|
||||
== Shell completion
|
||||
|
||||
The Substrate cli command supports shell auto-completion. For this to work, you will need to run the completion script matching you build and system.
|
||||
|
||||
Assuming you built a release version using `cargo build --release` and use `bash` run the following:
|
||||
|
||||
[source, shell]
|
||||
source target/release/completion-scripts/substrate.bash
|
||||
|
||||
You can find completion scripts for:
|
||||
- bash
|
||||
- fish
|
||||
- zsh
|
||||
- elvish
|
||||
- powershell
|
||||
|
||||
To make this change persistent, you can proceed as follow:
|
||||
|
||||
.First install
|
||||
|
||||
[source, shell]
|
||||
----
|
||||
COMPL_DIR=$HOME/.completion
|
||||
mkdir -p $COMPL_DIR
|
||||
cp -f target/release/completion-scripts/substrate.bash $COMPL_DIR/
|
||||
echo "source $COMPL_DIR/substrate.bash" >> $HOME/.bash_profile
|
||||
source $HOME/.bash_profile
|
||||
----
|
||||
|
||||
.Update
|
||||
|
||||
When you build a new version of Substrate, the following will ensure you auto-completion script matches the current binary:
|
||||
|
||||
[source, shell]
|
||||
----
|
||||
COMPL_DIR=$HOME/.completion
|
||||
mkdir -p $COMPL_DIR
|
||||
cp -f target/release/completion-scripts/substrate.bash $COMPL_DIR/
|
||||
source $HOME/.bash_profile
|
||||
----
|
||||
@@ -0,0 +1,197 @@
|
||||
name: {name}
|
||||
author: {author}
|
||||
about: {description}
|
||||
args:
|
||||
- log:
|
||||
short: l
|
||||
long: log
|
||||
value_name: LOG_PATTERN
|
||||
help: Sets a custom logging filter
|
||||
takes_value: true
|
||||
- base-path:
|
||||
long: base-path
|
||||
short: d
|
||||
value_name: PATH
|
||||
help: Specify custom base path
|
||||
takes_value: true
|
||||
- keystore-path:
|
||||
long: keystore-path
|
||||
value_name: PATH
|
||||
help: Specify custom keystore path
|
||||
takes_value: true
|
||||
- key:
|
||||
long: key
|
||||
value_name: STRING
|
||||
help: Specify additional key seed
|
||||
takes_value: true
|
||||
- node-key:
|
||||
long: node-key
|
||||
value_name: KEY
|
||||
help: Specify node secret key (64-character hex string)
|
||||
takes_value: true
|
||||
- validator:
|
||||
long: validator
|
||||
help: Enable validator mode
|
||||
takes_value: false
|
||||
- light:
|
||||
long: light
|
||||
help: Run in light client mode
|
||||
takes_value: false
|
||||
- dev:
|
||||
long: dev
|
||||
help: Run in development mode; implies --chain=dev --validator --key Alice
|
||||
takes_value: false
|
||||
- port:
|
||||
long: port
|
||||
value_name: PORT
|
||||
help: Specify p2p protocol TCP port
|
||||
takes_value: true
|
||||
- rpc-port:
|
||||
long: rpc-port
|
||||
value_name: PORT
|
||||
help: Specify HTTP RPC server TCP port
|
||||
takes_value: true
|
||||
- ws-port:
|
||||
long: ws-port
|
||||
value_name: PORT
|
||||
help: Specify WebSockets RPC server TCP port
|
||||
takes_value: true
|
||||
- bootnodes:
|
||||
long: bootnodes
|
||||
value_name: URL
|
||||
help: Specify a list of bootnodes
|
||||
takes_value: true
|
||||
multiple: true
|
||||
- chain:
|
||||
long: chain
|
||||
value_name: CHAIN_SPEC
|
||||
help: Specify the chain specification (one of krummelanke, dev, local or staging)
|
||||
takes_value: true
|
||||
- pruning:
|
||||
long: pruning
|
||||
value_name: PRUNING_MODE
|
||||
help: Specify the pruning mode, a number of blocks to keep or "archive". Default is 256.
|
||||
takes_value: true
|
||||
- name:
|
||||
long: name
|
||||
value_name: NAME
|
||||
help: The human-readable name for this node, as reported to the telemetry server, if enabled
|
||||
takes_value: true
|
||||
- telemetry:
|
||||
short: t
|
||||
long: telemetry
|
||||
help: Should connect to the Polkadot telemetry server (telemetry is off by default on local chains)
|
||||
takes_value: false
|
||||
- no-telemetry:
|
||||
long: no-telemetry
|
||||
help: Should not connect to the Polkadot telemetry server (telemetry is on by default on global chains)
|
||||
takes_value: false
|
||||
- telemetry-url:
|
||||
long: telemetry-url
|
||||
value_name: TELEMETRY_URL
|
||||
help: The URL of the telemetry server. Implies --telemetry
|
||||
takes_value: true
|
||||
- execution:
|
||||
long: execution
|
||||
value_name: STRATEGY
|
||||
help: The means of execution used when calling into the runtime. Can be either wasm, native or both.
|
||||
- min-heap-pages:
|
||||
long: min-heap-pages
|
||||
value_name: COUNT
|
||||
help: The number of 64KB pages to allocate for Wasm execution initially.
|
||||
- max-heap-pages:
|
||||
long: max-heap-pages
|
||||
value_name: COUNT
|
||||
help: The maximum number of 64KB pages to ever allocate for Wasm execution. Don't alter this unless you know what you're doing.
|
||||
subcommands:
|
||||
- build-spec:
|
||||
about: Build a spec.json file, outputing to stdout
|
||||
args:
|
||||
- raw:
|
||||
long: raw
|
||||
help: Force raw genesis storage output.
|
||||
takes_value: false
|
||||
- chain:
|
||||
long: chain
|
||||
value_name: CHAIN_SPEC
|
||||
help: Specify the chain specification (one of krummelanke, dev, local or staging)
|
||||
takes_value: true
|
||||
- export-blocks:
|
||||
about: Export blocks to a file
|
||||
args:
|
||||
- OUTPUT:
|
||||
index: 1
|
||||
help: Output file name or stdout if unspecified.
|
||||
required: false
|
||||
- chain:
|
||||
long: chain
|
||||
value_name: CHAIN_SPEC
|
||||
help: Specify the chain specification.
|
||||
takes_value: true
|
||||
- base-path:
|
||||
long: base-path
|
||||
short: d
|
||||
value_name: PATH
|
||||
help: Specify custom base path.
|
||||
takes_value: true
|
||||
- from:
|
||||
long: from
|
||||
value_name: BLOCK
|
||||
help: Specify starting block number. 1 by default.
|
||||
takes_value: true
|
||||
- to:
|
||||
long: to
|
||||
value_name: BLOCK
|
||||
help: Specify last block number. Best block by default.
|
||||
takes_value: true
|
||||
- json:
|
||||
long: json
|
||||
help: Use JSON output rather than binary.
|
||||
takes_value: false
|
||||
- import-blocks:
|
||||
about: Import blocks from file.
|
||||
args:
|
||||
- INPUT:
|
||||
index: 1
|
||||
help: Input file or stdin if unspecified.
|
||||
required: false
|
||||
- chain:
|
||||
long: chain
|
||||
value_name: CHAIN_SPEC
|
||||
help: Specify the chain specification.
|
||||
takes_value: true
|
||||
- base-path:
|
||||
long: base-path
|
||||
short: d
|
||||
value_name: PATH
|
||||
help: Specify custom base path.
|
||||
takes_value: true
|
||||
- execution:
|
||||
long: execution
|
||||
value_name: STRATEGY
|
||||
help: The means of execution used when calling into the runtime. Can be either wasm, native or both.
|
||||
- min-heap-pages:
|
||||
long: min-heap-pages
|
||||
value_name: COUNT
|
||||
help: The number of 64KB pages to allocate for Wasm execution initially.
|
||||
- max-heap-pages:
|
||||
long: max-heap-pages
|
||||
value_name: COUNT
|
||||
help: The maximum number of 64KB pages to ever allocate for Wasm execution. Don't alter this unless you know what you're doing.
|
||||
- revert:
|
||||
about: Revert chain to the previous state
|
||||
args:
|
||||
- NUM:
|
||||
index: 1
|
||||
help: Number of blocks to revert. Default is 256.
|
||||
- chain:
|
||||
long: chain
|
||||
value_name: CHAIN_SPEC
|
||||
help: Specify the chain specification.
|
||||
takes_value: true
|
||||
- base-path:
|
||||
long: base-path
|
||||
short: d
|
||||
value_name: PATH
|
||||
help: Specify custom base path.
|
||||
takes_value: true
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright 2017 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;
|
||||
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
Io(::std::io::Error) #[doc="IO error"];
|
||||
Cli(::clap::Error) #[doc="CLI error"];
|
||||
Service(::service::Error) #[doc="Substrate service error"];
|
||||
}
|
||||
links {
|
||||
Client(client::error::Error, client::error::ErrorKind) #[doc="Client error"];
|
||||
}
|
||||
errors {
|
||||
/// Input error.
|
||||
Input(m: String) {
|
||||
description("Invalid input"),
|
||||
display("{}", m),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// Copyright 2017 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 std::time::{Duration, Instant};
|
||||
use futures::{Future, Stream};
|
||||
use service::{Service, Components};
|
||||
use tokio::runtime::TaskExecutor;
|
||||
use tokio::timer::Interval;
|
||||
use network::{SyncState, SyncProvider};
|
||||
use client::BlockchainEvents;
|
||||
use runtime_primitives::traits::{Header, As};
|
||||
use substrate_extrinsic_pool::api::ExtrinsicPool;
|
||||
|
||||
const TIMER_INTERVAL_MS: u64 = 5000;
|
||||
|
||||
/// Spawn informant on the event loop
|
||||
pub fn start<C>(service: &Service<C>, exit: ::exit_future::Exit, handle: TaskExecutor)
|
||||
where
|
||||
C: Components,
|
||||
{
|
||||
let interval = Interval::new(Instant::now(), Duration::from_millis(TIMER_INTERVAL_MS));
|
||||
|
||||
let network = service.network();
|
||||
let client = service.client();
|
||||
let txpool = service.extrinsic_pool();
|
||||
|
||||
let display_notifications = interval.map_err(|e| debug!("Timer error: {:?}", e)).for_each(move |_| {
|
||||
let sync_status = network.status();
|
||||
|
||||
if let Ok(best_block) = client.best_block_header() {
|
||||
let hash = best_block.hash();
|
||||
let num_peers = sync_status.num_peers;
|
||||
let status = match (sync_status.sync.state, sync_status.sync.best_seen_block) {
|
||||
(SyncState::Idle, _) => "Idle".into(),
|
||||
(SyncState::Downloading, None) => "Syncing".into(),
|
||||
(SyncState::Downloading, Some(n)) => format!("Syncing, target=#{}", n),
|
||||
};
|
||||
let txpool_status = txpool.light_status();
|
||||
let best_number: u64 = best_block.number().as_();
|
||||
info!(target: "substrate", "{} ({} peers), best: #{} ({})", status, sync_status.num_peers, best_number, hash);
|
||||
telemetry!("system.interval"; "status" => status, "peers" => num_peers, "height" => best_number, "best" => ?hash, "txcount" => txpool_status.transaction_count);
|
||||
} else {
|
||||
warn!("Error getting best block information");
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
|
||||
let client = service.client();
|
||||
let display_block_import = client.import_notification_stream().for_each(|n| {
|
||||
info!(target: "substrate", "Imported #{} ({})", n.header.number(), n.hash);
|
||||
Ok(())
|
||||
});
|
||||
|
||||
let txpool = service.extrinsic_pool();
|
||||
let display_txpool_import = txpool.import_notification_stream().for_each(move |_| {
|
||||
let status = txpool.light_status();
|
||||
telemetry!("txpool.import"; "mem_usage" => status.mem_usage, "count" => status.transaction_count, "sender" => status.senders);
|
||||
Ok(())
|
||||
});
|
||||
|
||||
let informant_work = display_notifications.join3(display_block_import, display_txpool_import);
|
||||
handle.spawn(exit.until(informant_work).map(|_| ()));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,502 @@
|
||||
// Copyright 2017 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/>.
|
||||
|
||||
//! Substrate CLI library.
|
||||
|
||||
#![warn(missing_docs)]
|
||||
#![warn(unused_extern_crates)]
|
||||
|
||||
extern crate app_dirs;
|
||||
extern crate env_logger;
|
||||
extern crate atty;
|
||||
extern crate ansi_term;
|
||||
extern crate regex;
|
||||
extern crate time;
|
||||
extern crate fdlimit;
|
||||
extern crate futures;
|
||||
extern crate tokio;
|
||||
extern crate names;
|
||||
extern crate backtrace;
|
||||
|
||||
extern crate substrate_client as client;
|
||||
extern crate substrate_network as network;
|
||||
extern crate substrate_runtime_primitives as runtime_primitives;
|
||||
extern crate substrate_extrinsic_pool;
|
||||
extern crate substrate_service as service;
|
||||
#[macro_use]
|
||||
extern crate slog; // needed until we can reexport `slog_info` from `substrate_telemetry`
|
||||
#[macro_use]
|
||||
extern crate substrate_telemetry;
|
||||
extern crate exit_future;
|
||||
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
#[macro_use]
|
||||
extern crate clap;
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
pub mod error;
|
||||
pub mod informant;
|
||||
mod panic_hook;
|
||||
|
||||
use runtime_primitives::traits::As;
|
||||
use service::{
|
||||
ServiceFactory, FactoryFullConfiguration, RuntimeGenesis,
|
||||
FactoryGenesis, PruningMode, ChainSpec,
|
||||
};
|
||||
|
||||
use std::io::{Write, Read, stdin, stdout};
|
||||
use std::fs::File;
|
||||
use std::net::SocketAddr;
|
||||
use std::path::{Path, PathBuf};
|
||||
use names::{Generator, Name};
|
||||
use regex::Regex;
|
||||
|
||||
use futures::Future;
|
||||
|
||||
/// Executable version. Used to pass version information from the root crate.
|
||||
pub struct VersionInfo {
|
||||
/// Implementation version.
|
||||
pub version: &'static str,
|
||||
/// SCM Commit hash.
|
||||
pub commit: &'static str,
|
||||
/// Executable file name.
|
||||
pub executable_name: &'static str,
|
||||
/// Executable file description.
|
||||
pub description: &'static str,
|
||||
/// Executable file author.
|
||||
pub author: &'static str,
|
||||
}
|
||||
|
||||
/// CLI Action
|
||||
pub enum Action<F: ServiceFactory> {
|
||||
/// Substrate handled the command. No need to do anything.
|
||||
ExecutedInternally,
|
||||
/// Service mode requested. Caller should start the service.
|
||||
RunService(FactoryFullConfiguration<F>),
|
||||
}
|
||||
|
||||
fn load_spec<F, G>(matches: &clap::ArgMatches, factory: F) -> Result<ChainSpec<G>, String>
|
||||
where G: RuntimeGenesis, F: FnOnce(&str) -> Result<Option<ChainSpec<G>>, String>,
|
||||
{
|
||||
let chain_key = matches.value_of("chain").unwrap_or_else(|| if matches.is_present("dev") { "dev" } else { "" });
|
||||
let spec = match factory(chain_key)? {
|
||||
Some(spec) => spec,
|
||||
None => ChainSpec::from_json_file(PathBuf::from(chain_key))?
|
||||
};
|
||||
Ok(spec)
|
||||
}
|
||||
|
||||
fn base_path(matches: &clap::ArgMatches) -> PathBuf {
|
||||
matches.value_of("base-path")
|
||||
.map(|x| Path::new(x).to_owned())
|
||||
.unwrap_or_else(default_base_path)
|
||||
}
|
||||
|
||||
/// Check whether a node name is considered as valid
|
||||
fn is_node_name_valid(_name: &str) -> Result<(), &str> {
|
||||
const MAX_NODE_NAME_LENGTH: usize = 32;
|
||||
let name = _name.to_string();
|
||||
if name.chars().count() >= MAX_NODE_NAME_LENGTH {
|
||||
return Err("Node name too long");
|
||||
}
|
||||
|
||||
let invalid_chars = r"[\\.@]";
|
||||
let re = Regex::new(invalid_chars).unwrap();
|
||||
if re.is_match(&name) {
|
||||
return Err("Node name should not contain invalid chars such as '.' and '@'");
|
||||
}
|
||||
|
||||
let invalid_patterns = r"(https?:\\/+)?(www)+";
|
||||
let re = Regex::new(invalid_patterns).unwrap();
|
||||
if re.is_match(&name) {
|
||||
return Err("Node name should not contain urls");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Parse command line arguments and execute commands or return service configuration.
|
||||
///
|
||||
/// IANA unassigned port ranges that we could use:
|
||||
/// 6717-6766 Unassigned
|
||||
/// 8504-8553 Unassigned
|
||||
/// 9556-9591 Unassigned
|
||||
/// 9803-9874 Unassigned
|
||||
/// 9926-9949 Unassigned
|
||||
pub fn prepare_execution<F, I, T, E, S>(
|
||||
args: I,
|
||||
exit: E,
|
||||
version: VersionInfo,
|
||||
spec_factory: S,
|
||||
impl_name: &'static str,
|
||||
) -> error::Result<Action<F>>
|
||||
where
|
||||
I: IntoIterator<Item = T>,
|
||||
T: Into<std::ffi::OsString> + Clone,
|
||||
E: Future<Item=(),Error=()> + Send + 'static,
|
||||
F: ServiceFactory,
|
||||
S: FnOnce(&str) -> Result<Option<ChainSpec<FactoryGenesis<F>>>, String>,
|
||||
{
|
||||
panic_hook::set();
|
||||
|
||||
let yaml = format!(include_str!("./cli.yml"),
|
||||
name = version.executable_name,
|
||||
description = version.description,
|
||||
author = version.author,
|
||||
);
|
||||
let yaml = &clap::YamlLoader::load_from_str(&yaml).expect("Invalid yml file")[0];
|
||||
let matches = match clap::App::from_yaml(yaml)
|
||||
.version(&(crate_version!().to_owned() + "\n")[..])
|
||||
.get_matches_from_safe(args) {
|
||||
Ok(m) => m,
|
||||
Err(e) => e.exit(),
|
||||
};
|
||||
|
||||
// TODO [ToDr] Split parameters parsing from actual execution.
|
||||
let log_pattern = matches.value_of("log").unwrap_or("");
|
||||
init_logger(log_pattern);
|
||||
fdlimit::raise_fd_limit();
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("build-spec") {
|
||||
let spec = load_spec(&matches, spec_factory)?;
|
||||
build_spec::<F>(matches, spec)?;
|
||||
return Ok(Action::ExecutedInternally);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("export-blocks") {
|
||||
let spec = load_spec(&matches, spec_factory)?;
|
||||
export_blocks::<F, _>(matches, spec, exit)?;
|
||||
return Ok(Action::ExecutedInternally);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("import-blocks") {
|
||||
let spec = load_spec(&matches, spec_factory)?;
|
||||
import_blocks::<F, _>(matches, spec, exit)?;
|
||||
return Ok(Action::ExecutedInternally);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("revert") {
|
||||
let spec = load_spec(&matches, spec_factory)?;
|
||||
revert_chain::<F>(matches, spec)?;
|
||||
return Ok(Action::ExecutedInternally);
|
||||
}
|
||||
|
||||
let spec = load_spec(&matches, spec_factory)?;
|
||||
let mut config = service::Configuration::default_with_spec(spec);
|
||||
|
||||
config.impl_name = impl_name;
|
||||
config.impl_commit = version.commit;
|
||||
config.impl_version = version.version;
|
||||
|
||||
config.name = match matches.value_of("name") {
|
||||
None => Generator::with_naming(Name::Numbered).next().unwrap(),
|
||||
Some(name) => name.into(),
|
||||
};
|
||||
match is_node_name_valid(&config.name) {
|
||||
Ok(_) => (),
|
||||
Err(msg) => return Err(error::ErrorKind::Input(
|
||||
format!("Invalid node name '{}'. Reason: {}. If unsure, use none.", config.name, msg)).into())
|
||||
}
|
||||
|
||||
let base_path = base_path(&matches);
|
||||
|
||||
config.keystore_path = matches.value_of("keystore")
|
||||
.map(|x| Path::new(x).to_owned())
|
||||
.unwrap_or_else(|| keystore_path(&base_path, config.chain_spec.id()))
|
||||
.to_string_lossy()
|
||||
.into();
|
||||
|
||||
config.database_path = db_path(&base_path, config.chain_spec.id()).to_string_lossy().into();
|
||||
|
||||
config.pruning = match matches.value_of("pruning") {
|
||||
Some("archive") => PruningMode::ArchiveAll,
|
||||
None => PruningMode::default(),
|
||||
Some(s) => PruningMode::keep_blocks(s.parse()
|
||||
.map_err(|_| error::ErrorKind::Input("Invalid pruning mode specified".to_owned()))?),
|
||||
};
|
||||
|
||||
let role =
|
||||
if matches.is_present("light") {
|
||||
config.execution_strategy = service::ExecutionStrategy::NativeWhenPossible;
|
||||
service::Roles::LIGHT
|
||||
} else if matches.is_present("validator") || matches.is_present("dev") {
|
||||
config.execution_strategy = service::ExecutionStrategy::Both;
|
||||
service::Roles::AUTHORITY
|
||||
} else {
|
||||
config.execution_strategy = service::ExecutionStrategy::NativeWhenPossible;
|
||||
service::Roles::FULL
|
||||
};
|
||||
|
||||
if let Some(v) = matches.value_of("min-heap-pages") {
|
||||
config.min_heap_pages = v.parse().map_err(|_| "Invalid --min-heap-pages argument")?;
|
||||
}
|
||||
if let Some(v) = matches.value_of("max-heap-pages") {
|
||||
config.max_heap_pages = v.parse().map_err(|_| "Invalid --max-heap-pages argument")?;
|
||||
}
|
||||
|
||||
if let Some(s) = matches.value_of("execution") {
|
||||
config.execution_strategy = match s {
|
||||
"both" => service::ExecutionStrategy::Both,
|
||||
"native" => service::ExecutionStrategy::NativeWhenPossible,
|
||||
"wasm" => service::ExecutionStrategy::AlwaysWasm,
|
||||
_ => return Err(error::ErrorKind::Input("Invalid execution mode specified".to_owned()).into()),
|
||||
};
|
||||
}
|
||||
|
||||
config.roles = role;
|
||||
{
|
||||
config.network.boot_nodes.extend(matches
|
||||
.values_of("bootnodes")
|
||||
.map_or(Default::default(), |v| v.map(|n| n.to_owned()).collect::<Vec<_>>()));
|
||||
config.network.config_path = Some(network_path(&base_path, config.chain_spec.id()).to_string_lossy().into());
|
||||
config.network.net_config_path = config.network.config_path.clone();
|
||||
|
||||
let port = match matches.value_of("port") {
|
||||
Some(port) => port.parse().map_err(|_| "Invalid p2p port value specified.")?,
|
||||
None => 30333,
|
||||
};
|
||||
|
||||
config.network.listen_address = Some(SocketAddr::new("0.0.0.0".parse().unwrap(), port));
|
||||
config.network.public_address = None;
|
||||
config.network.client_version = config.client_id();
|
||||
config.network.use_secret = match matches.value_of("node-key").map(|s| s.parse()) {
|
||||
Some(Ok(secret)) => Some(secret),
|
||||
Some(Err(err)) => return Err(format!("Error parsing node key: {}", err).into()),
|
||||
None => None,
|
||||
};
|
||||
}
|
||||
|
||||
config.keys = matches.values_of("key").unwrap_or_default().map(str::to_owned).collect();
|
||||
if matches.is_present("dev") {
|
||||
config.keys.push("Alice".into());
|
||||
}
|
||||
|
||||
config.rpc_http = Some(parse_address("127.0.0.1:9933", "rpc-port", &matches)?);
|
||||
config.rpc_ws = Some(parse_address("127.0.0.1:9944", "ws-port", &matches)?);
|
||||
|
||||
// Override telemetry
|
||||
if matches.is_present("no-telemetry") {
|
||||
config.telemetry_url = None;
|
||||
} else if let Some(url) = matches.value_of("telemetry-url") {
|
||||
config.telemetry_url = Some(url.to_owned());
|
||||
}
|
||||
|
||||
Ok(Action::RunService(config))
|
||||
}
|
||||
|
||||
fn build_spec<F>(matches: &clap::ArgMatches, spec: ChainSpec<FactoryGenesis<F>>) -> error::Result<()>
|
||||
where F: ServiceFactory,
|
||||
{
|
||||
info!("Building chain spec");
|
||||
let raw = matches.is_present("raw");
|
||||
let json = service::chain_ops::build_spec::<FactoryGenesis<F>>(spec, raw)?;
|
||||
print!("{}", json);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn export_blocks<F, E>(matches: &clap::ArgMatches, spec: ChainSpec<FactoryGenesis<F>>, exit: E) -> error::Result<()>
|
||||
where F: ServiceFactory, E: Future<Item=(),Error=()> + Send + 'static,
|
||||
{
|
||||
let base_path = base_path(matches);
|
||||
let mut config = service::Configuration::default_with_spec(spec);
|
||||
config.database_path = db_path(&base_path, config.chain_spec.id()).to_string_lossy().into();
|
||||
info!("DB path: {}", config.database_path);
|
||||
let from: u64 = match matches.value_of("from") {
|
||||
Some(v) => v.parse().map_err(|_| "Invalid --from argument")?,
|
||||
None => 1,
|
||||
};
|
||||
|
||||
let to: Option<u64> = match matches.value_of("to") {
|
||||
Some(v) => Some(v.parse().map_err(|_| "Invalid --to argument")?),
|
||||
None => None,
|
||||
};
|
||||
let json = matches.is_present("json");
|
||||
|
||||
let file: Box<Write> = match matches.value_of("OUTPUT") {
|
||||
Some(filename) => Box::new(File::create(filename)?),
|
||||
None => Box::new(stdout()),
|
||||
};
|
||||
|
||||
Ok(service::chain_ops::export_blocks::<F, _, _>(config, exit, file, As::sa(from), to.map(As::sa), json)?)
|
||||
}
|
||||
|
||||
fn import_blocks<F, E>(matches: &clap::ArgMatches, spec: ChainSpec<FactoryGenesis<F>>, exit: E) -> error::Result<()>
|
||||
where F: ServiceFactory, E: Future<Item=(),Error=()> + Send + 'static,
|
||||
{
|
||||
let base_path = base_path(matches);
|
||||
let mut config = service::Configuration::default_with_spec(spec);
|
||||
config.database_path = db_path(&base_path, config.chain_spec.id()).to_string_lossy().into();
|
||||
|
||||
if let Some(v) = matches.value_of("min-heap-pages") {
|
||||
config.min_heap_pages = v.parse().map_err(|_| "Invalid --min-heap-pages argument")?;
|
||||
}
|
||||
if let Some(v) = matches.value_of("max-heap-pages") {
|
||||
config.max_heap_pages = v.parse().map_err(|_| "Invalid --max-heap-pages argument")?;
|
||||
}
|
||||
|
||||
if let Some(s) = matches.value_of("execution") {
|
||||
config.execution_strategy = match s {
|
||||
"both" => service::ExecutionStrategy::Both,
|
||||
"native" => service::ExecutionStrategy::NativeWhenPossible,
|
||||
"wasm" => service::ExecutionStrategy::AlwaysWasm,
|
||||
_ => return Err(error::ErrorKind::Input("Invalid execution mode specified".to_owned()).into()),
|
||||
};
|
||||
}
|
||||
|
||||
let file: Box<Read> = match matches.value_of("INPUT") {
|
||||
Some(filename) => Box::new(File::open(filename)?),
|
||||
None => Box::new(stdin()),
|
||||
};
|
||||
|
||||
Ok(service::chain_ops::import_blocks::<F, _, _>(config, exit, file)?)
|
||||
}
|
||||
|
||||
fn revert_chain<F>(matches: &clap::ArgMatches, spec: ChainSpec<FactoryGenesis<F>>) -> error::Result<()>
|
||||
where F: ServiceFactory,
|
||||
{
|
||||
let base_path = base_path(matches);
|
||||
let mut config = service::Configuration::default_with_spec(spec);
|
||||
config.database_path = db_path(&base_path, config.chain_spec.id()).to_string_lossy().into();
|
||||
|
||||
let blocks = match matches.value_of("NUM") {
|
||||
Some(v) => v.parse().map_err(|_| "Invalid block count specified")?,
|
||||
None => 256,
|
||||
};
|
||||
|
||||
Ok(service::chain_ops::revert_chain::<F>(config, As::sa(blocks))?)
|
||||
}
|
||||
|
||||
fn parse_address(default: &str, port_param: &str, matches: &clap::ArgMatches) -> Result<SocketAddr, String> {
|
||||
let mut address: SocketAddr = default.parse().ok().ok_or(format!("Invalid address specified for --{}.", port_param))?;
|
||||
if let Some(port) = matches.value_of(port_param) {
|
||||
let port: u16 = port.parse().ok().ok_or(format!("Invalid port for --{} specified.", port_param))?;
|
||||
address.set_port(port);
|
||||
}
|
||||
|
||||
Ok(address)
|
||||
}
|
||||
|
||||
fn keystore_path(base_path: &Path, chain_id: &str) -> PathBuf {
|
||||
let mut path = base_path.to_owned();
|
||||
path.push("chains");
|
||||
path.push(chain_id);
|
||||
path.push("keystore");
|
||||
path
|
||||
}
|
||||
|
||||
fn db_path(base_path: &Path, chain_id: &str) -> PathBuf {
|
||||
let mut path = base_path.to_owned();
|
||||
path.push("chains");
|
||||
path.push(chain_id);
|
||||
path.push("db");
|
||||
path
|
||||
}
|
||||
|
||||
fn network_path(base_path: &Path, chain_id: &str) -> PathBuf {
|
||||
let mut path = base_path.to_owned();
|
||||
path.push("chains");
|
||||
path.push(chain_id);
|
||||
path.push("network");
|
||||
path
|
||||
}
|
||||
|
||||
fn default_base_path() -> PathBuf {
|
||||
use app_dirs::{AppInfo, AppDataType};
|
||||
|
||||
let app_info = AppInfo {
|
||||
name: "Polkadot",
|
||||
author: "Parity Technologies",
|
||||
};
|
||||
|
||||
app_dirs::get_app_root(
|
||||
AppDataType::UserData,
|
||||
&app_info,
|
||||
).expect("app directories exist on all supported platforms; qed")
|
||||
}
|
||||
|
||||
fn init_logger(pattern: &str) {
|
||||
use ansi_term::Colour;
|
||||
|
||||
let mut builder = env_logger::LogBuilder::new();
|
||||
// Disable info logging by default for some modules:
|
||||
builder.filter(Some("ws"), log::LogLevelFilter::Warn);
|
||||
builder.filter(Some("hyper"), log::LogLevelFilter::Warn);
|
||||
// Enable info for others.
|
||||
builder.filter(None, log::LogLevelFilter::Info);
|
||||
|
||||
if let Ok(lvl) = std::env::var("RUST_LOG") {
|
||||
builder.parse(&lvl);
|
||||
}
|
||||
|
||||
builder.parse(pattern);
|
||||
let isatty = atty::is(atty::Stream::Stderr);
|
||||
let enable_color = isatty;
|
||||
|
||||
let format = move |record: &log::LogRecord| {
|
||||
let timestamp = time::strftime("%Y-%m-%d %H:%M:%S", &time::now()).expect("Error formatting log timestamp");
|
||||
|
||||
let mut output = if log::max_log_level() <= log::LogLevelFilter::Info {
|
||||
format!("{} {}", Colour::Black.bold().paint(timestamp), record.args())
|
||||
} else {
|
||||
let name = ::std::thread::current().name().map_or_else(Default::default, |x| format!("{}", Colour::Blue.bold().paint(x)));
|
||||
format!("{} {} {} {} {}", Colour::Black.bold().paint(timestamp), name, record.level(), record.target(), record.args())
|
||||
};
|
||||
|
||||
if !enable_color {
|
||||
output = kill_color(output.as_ref());
|
||||
}
|
||||
|
||||
if !isatty && record.level() <= log::LogLevel::Info && atty::is(atty::Stream::Stdout) {
|
||||
// duplicate INFO/WARN output to console
|
||||
println!("{}", output);
|
||||
}
|
||||
output
|
||||
};
|
||||
builder.format(format);
|
||||
|
||||
builder.init().expect("Logger initialized only once.");
|
||||
}
|
||||
|
||||
fn kill_color(s: &str) -> String {
|
||||
lazy_static! {
|
||||
static ref RE: Regex = Regex::new("\x1b\\[[^m]+m").expect("Error initializing color regex");
|
||||
}
|
||||
RE.replace_all(s, "").to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn tests_node_name_good() {
|
||||
assert!(is_node_name_valid("short name").is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tests_node_name_bad() {
|
||||
assert!(is_node_name_valid("long names are not very cool for the ui").is_err());
|
||||
assert!(is_node_name_valid("Dots.not.Ok").is_err());
|
||||
assert!(is_node_name_valid("http://visit.me").is_err());
|
||||
assert!(is_node_name_valid("https://visit.me").is_err());
|
||||
assert!(is_node_name_valid("www.visit.me").is_err());
|
||||
assert!(is_node_name_valid("email@domain").is_err());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// Copyright 2017 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/>.
|
||||
|
||||
//! Custom panic hook with bug report link
|
||||
|
||||
use backtrace::Backtrace;
|
||||
use std::io::{self, Write};
|
||||
use std::panic::{self, PanicInfo};
|
||||
use std::thread;
|
||||
|
||||
/// Set the panic hook
|
||||
pub fn set() {
|
||||
panic::set_hook(Box::new(panic_hook));
|
||||
}
|
||||
|
||||
static ABOUT_PANIC: &str = "
|
||||
This is a bug. Please report it at:
|
||||
|
||||
https://github.com/paritytech/polkadot/issues/new
|
||||
";
|
||||
|
||||
fn panic_hook(info: &PanicInfo) {
|
||||
let location = info.location();
|
||||
let file = location.as_ref().map(|l| l.file()).unwrap_or("<unknown>");
|
||||
let line = location.as_ref().map(|l| l.line()).unwrap_or(0);
|
||||
|
||||
let msg = match info.payload().downcast_ref::<&'static str>() {
|
||||
Some(s) => *s,
|
||||
None => match info.payload().downcast_ref::<String>() {
|
||||
Some(s) => &s[..],
|
||||
None => "Box<Any>",
|
||||
}
|
||||
};
|
||||
|
||||
let thread = thread::current();
|
||||
let name = thread.name().unwrap_or("<unnamed>");
|
||||
|
||||
let backtrace = Backtrace::new();
|
||||
|
||||
let mut stderr = io::stderr();
|
||||
|
||||
let _ = writeln!(stderr, "");
|
||||
let _ = writeln!(stderr, "====================");
|
||||
let _ = writeln!(stderr, "");
|
||||
let _ = writeln!(stderr, "{:?}", backtrace);
|
||||
let _ = writeln!(stderr, "");
|
||||
let _ = writeln!(
|
||||
stderr,
|
||||
"Thread '{}' panicked at '{}', {}:{}",
|
||||
name, msg, file, line
|
||||
);
|
||||
|
||||
let _ = writeln!(stderr, "{}", ABOUT_PANIC);
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ use substrate_runtime_primitives::traits::Block as BlockT;
|
||||
|
||||
type Metadata = apis::metadata::Metadata;
|
||||
type RpcHandler = pubsub::PubSubHandler<Metadata>;
|
||||
pub type HttpServer = http::Server;
|
||||
pub type WsServer = ws::Server;
|
||||
|
||||
/// Construct rpc `IoHandler`
|
||||
pub fn rpc_handler<Block: BlockT, S, C, A, Y>(
|
||||
|
||||
@@ -15,6 +15,7 @@ exit-future = "0.1"
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
serde_derive = "1.0"
|
||||
target_info = "0.1"
|
||||
substrate-keystore = { path = "../../substrate/keystore" }
|
||||
substrate-runtime-io = { path = "../../substrate/runtime-io" }
|
||||
substrate-runtime-primitives = { path = "../../substrate/runtime/primitives" }
|
||||
@@ -22,6 +23,9 @@ substrate-primitives = { path = "../../substrate/primitives" }
|
||||
substrate-network = { path = "../../substrate/network" }
|
||||
substrate-client = { path = "../../substrate/client" }
|
||||
substrate-client-db = { path = "../../substrate/client/db" }
|
||||
substrate-codec = { path = "../../substrate/codec" }
|
||||
substrate-executor = { path = "../../substrate/executor" }
|
||||
substrate-extrinsic-pool = { path = "../../substrate/extrinsic-pool" }
|
||||
substrate-rpc = { path = "../../substrate/rpc" }
|
||||
substrate-rpc-servers = { path = "../../substrate/rpc-servers" }
|
||||
substrate-telemetry = { path = "../../substrate/telemetry" }
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
// Copyright 2017 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/>.
|
||||
|
||||
//! Chain utilities.
|
||||
|
||||
use std::{self, io::{Read, Write}};
|
||||
use futures::Future;
|
||||
use serde_json;
|
||||
|
||||
use client::BlockOrigin;
|
||||
use runtime_primitives::generic::{SignedBlock, BlockId};
|
||||
use runtime_primitives::traits::{As};
|
||||
use components::{ServiceFactory, FactoryFullConfiguration, FactoryBlockNumber, RuntimeGenesis};
|
||||
use new_client;
|
||||
use codec::{Decode, Encode};
|
||||
use error;
|
||||
use chain_spec::ChainSpec;
|
||||
|
||||
/// Export a range of blocks to a binary stream.
|
||||
pub fn export_blocks<F, E, W>(config: FactoryFullConfiguration<F>, exit: E, mut output: W, from: FactoryBlockNumber<F>, to: Option<FactoryBlockNumber<F>>, json: bool) -> error::Result<()>
|
||||
where F: ServiceFactory, E: Future<Item=(),Error=()> + Send + 'static, W: Write,
|
||||
{
|
||||
let client = new_client::<F>(config)?;
|
||||
let mut block = from;
|
||||
|
||||
let last = match to {
|
||||
Some(v) if v == As::sa(0) => As::sa(1),
|
||||
Some(v) => v,
|
||||
None => client.info()?.chain.best_number,
|
||||
};
|
||||
|
||||
if last < block {
|
||||
return Err("Invalid block range specified".into());
|
||||
}
|
||||
|
||||
let (exit_send, exit_recv) = std::sync::mpsc::channel();
|
||||
::std::thread::spawn(move || {
|
||||
let _ = exit.wait();
|
||||
let _ = exit_send.send(());
|
||||
});
|
||||
info!("Exporting blocks from #{} to #{}", block, last);
|
||||
if !json {
|
||||
output.write(&(last - block + As::sa(1)).encode())?;
|
||||
}
|
||||
|
||||
loop {
|
||||
if exit_recv.try_recv().is_ok() {
|
||||
break;
|
||||
}
|
||||
match client.block(&BlockId::number(block))? {
|
||||
Some(block) => {
|
||||
if json {
|
||||
serde_json::to_writer(&mut output, &block).map_err(|e| format!("Eror writing JSON: {}", e))?;
|
||||
} else {
|
||||
output.write(&block.encode())?;
|
||||
}
|
||||
},
|
||||
None => break,
|
||||
}
|
||||
if block.as_() % 10000 == 0 {
|
||||
info!("#{}", block);
|
||||
}
|
||||
if block == last {
|
||||
break;
|
||||
}
|
||||
block += As::sa(1);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Import blocks from a binary stream.
|
||||
pub fn import_blocks<F, E, R>(config: FactoryFullConfiguration<F>, exit: E, mut input: R) -> error::Result<()>
|
||||
where F: ServiceFactory, E: Future<Item=(),Error=()> + Send + 'static, R: Read,
|
||||
{
|
||||
let client = new_client::<F>(config)?;
|
||||
|
||||
let (exit_send, exit_recv) = std::sync::mpsc::channel();
|
||||
::std::thread::spawn(move || {
|
||||
let _ = exit.wait();
|
||||
let _ = exit_send.send(());
|
||||
});
|
||||
|
||||
let count: u32 = Decode::decode(&mut input).ok_or("Error reading file")?;
|
||||
info!("Importing {} blocks", count);
|
||||
let mut block = 0;
|
||||
for _ in 0 .. count {
|
||||
if exit_recv.try_recv().is_ok() {
|
||||
break;
|
||||
}
|
||||
match SignedBlock::decode(&mut input) {
|
||||
Some(block) => {
|
||||
let header = client.check_justification(block.block.header, block.justification.into())?;
|
||||
client.import_block(BlockOrigin::File, header, Some(block.block.extrinsics))?;
|
||||
},
|
||||
None => {
|
||||
warn!("Error reading block data.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
block += 1;
|
||||
if block % 1000 == 0 {
|
||||
info!("#{}", block);
|
||||
}
|
||||
}
|
||||
info!("Imported {} blocks. Best: #{}", block, client.info()?.chain.best_number);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Revert the chain.
|
||||
pub fn revert_chain<F>(config: FactoryFullConfiguration<F>, blocks: FactoryBlockNumber<F>) -> error::Result<()>
|
||||
where F: ServiceFactory,
|
||||
{
|
||||
let client = new_client::<F>(config)?;
|
||||
let reverted = client.revert(blocks)?;
|
||||
let info = client.info()?.chain;
|
||||
info!("Reverted {} blocks. Best: #{} ({})", reverted, info.best_number, info.best_hash);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Build a chain spec json
|
||||
pub fn build_spec<G>(spec: ChainSpec<G>, raw: bool) -> error::Result<String>
|
||||
where G: RuntimeGenesis,
|
||||
{
|
||||
Ok(spec.to_json(raw)?)
|
||||
}
|
||||
@@ -75,6 +75,7 @@ struct ChainSpecFile {
|
||||
pub name: String,
|
||||
pub id: String,
|
||||
pub boot_nodes: Vec<String>,
|
||||
pub telemetry_url: Option<String>,
|
||||
}
|
||||
|
||||
/// A configuration of a chain. Can be used to build a genesis block.
|
||||
@@ -96,6 +97,10 @@ impl<G: RuntimeGenesis> ChainSpec<G> {
|
||||
&self.spec.id
|
||||
}
|
||||
|
||||
pub fn telemetry_url(&self) -> Option<&str> {
|
||||
self.spec.telemetry_url.as_ref().map(String::as_str)
|
||||
}
|
||||
|
||||
/// Parse json content into a `ChainSpec`
|
||||
pub fn from_embedded(json: &'static [u8]) -> Result<Self, String> {
|
||||
let spec = json::from_slice(json).map_err(|e| format!("Error parsing spec file: {}", e))?;
|
||||
@@ -116,11 +121,19 @@ impl<G: RuntimeGenesis> ChainSpec<G> {
|
||||
}
|
||||
|
||||
/// Create hardcoded spec.
|
||||
pub fn from_genesis(name: &str, id: &str, constructor: fn() -> G, boot_nodes: Vec<String>) -> Self {
|
||||
pub fn from_genesis(
|
||||
name: &str,
|
||||
id: &str,
|
||||
constructor: fn() -> G,
|
||||
boot_nodes: Vec<String>,
|
||||
telemetry_url: Option<&str>
|
||||
) -> Self
|
||||
{
|
||||
let spec = ChainSpecFile {
|
||||
name: name.to_owned(),
|
||||
id: id.to_owned(),
|
||||
boot_nodes: boot_nodes,
|
||||
telemetry_url: telemetry_url.map(str::to_owned),
|
||||
};
|
||||
ChainSpec {
|
||||
spec,
|
||||
|
||||
@@ -26,7 +26,7 @@ use error;
|
||||
use network::{self, OnDemand};
|
||||
use substrate_executor::{NativeExecutor, NativeExecutionDispatch};
|
||||
use extrinsic_pool::{txpool::Options as ExtrinsicPoolOptions, api::ExtrinsicPool as ExtrinsicPoolApi};
|
||||
use runtime_primitives::{traits::Block as BlockT, generic::BlockId, BuildStorage};
|
||||
use runtime_primitives::{traits::Block as BlockT, traits::Header as HeaderT, generic::BlockId, BuildStorage};
|
||||
use config::Configuration;
|
||||
|
||||
// Type aliases.
|
||||
@@ -80,6 +80,9 @@ pub type FactoryGenesis<F> = <F as ServiceFactory>::Genesis;
|
||||
/// `Block` type for a factory.
|
||||
pub type FactoryBlock<F> = <F as ServiceFactory>::Block;
|
||||
|
||||
/// `Number` type for a factory.
|
||||
pub type FactoryBlockNumber<F> = <<FactoryBlock<F> as BlockT>::Header as HeaderT>::Number;
|
||||
|
||||
/// Full `Configuration` type for a factory.
|
||||
pub type FactoryFullConfiguration<F> = Configuration<<F as ServiceFactory>::Configuration, FactoryGenesis<F>>;
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
//! Service configuration.
|
||||
|
||||
use std::net::SocketAddr;
|
||||
use extrinsic_pool;
|
||||
use chain_spec::ChainSpec;
|
||||
pub use client::ExecutionStrategy;
|
||||
@@ -24,9 +25,16 @@ pub use network::NetworkConfiguration;
|
||||
pub use client_db::PruningMode;
|
||||
use runtime_primitives::BuildStorage;
|
||||
use serde::{Serialize, de::DeserializeOwned};
|
||||
use target_info::Target;
|
||||
|
||||
/// Service configuration.
|
||||
pub struct Configuration<C, G: Serialize + DeserializeOwned + BuildStorage> {
|
||||
/// Implementation name
|
||||
pub impl_name: &'static str,
|
||||
/// Implementation version
|
||||
pub impl_version: &'static str,
|
||||
/// Git commit if any.
|
||||
pub impl_commit: &'static str,
|
||||
/// Node roles.
|
||||
pub roles: Roles,
|
||||
/// Extrinsic pool configuration.
|
||||
@@ -55,12 +63,21 @@ pub struct Configuration<C, G: Serialize + DeserializeOwned + BuildStorage> {
|
||||
pub min_heap_pages: usize,
|
||||
/// Maximum number of heap pages to allocate for Wasm execution.
|
||||
pub max_heap_pages: usize,
|
||||
/// RPC over HTTP binding address. `None` if disabled.
|
||||
pub rpc_http: Option<SocketAddr>,
|
||||
/// RPC over Websockets binding address. `None` if disabled.
|
||||
pub rpc_ws: Option<SocketAddr>,
|
||||
/// Telemetry service URL. `None` if disabled.
|
||||
pub telemetry_url: Option<String>,
|
||||
}
|
||||
|
||||
impl<C: Default, G: Serialize + DeserializeOwned + BuildStorage> Configuration<C, G> {
|
||||
/// Create default config for given chain spec.
|
||||
pub fn default_with_spec(chain_spec: ChainSpec<G>) -> Self {
|
||||
let mut configuration = Configuration {
|
||||
impl_name: "parity-substrate",
|
||||
impl_version: "0.0.0",
|
||||
impl_commit: "",
|
||||
chain_spec,
|
||||
name: Default::default(),
|
||||
roles: Roles::FULL,
|
||||
@@ -75,8 +92,30 @@ impl<C: Default, G: Serialize + DeserializeOwned + BuildStorage> Configuration<C
|
||||
execution_strategy: ExecutionStrategy::Both,
|
||||
min_heap_pages: 8,
|
||||
max_heap_pages: 1024,
|
||||
rpc_http: None,
|
||||
rpc_ws: None,
|
||||
telemetry_url: None,
|
||||
};
|
||||
configuration.network.boot_nodes = configuration.chain_spec.boot_nodes().to_vec();
|
||||
configuration.telemetry_url = configuration.chain_spec.telemetry_url().map(str::to_owned);
|
||||
configuration
|
||||
}
|
||||
|
||||
/// Returns platform info
|
||||
pub fn platform() -> String {
|
||||
let env = Target::env();
|
||||
let env_dash = if env.is_empty() { "" } else { "-" };
|
||||
format!("{}-{}{}{}", Target::arch(), Target::os(), env_dash, env)
|
||||
}
|
||||
|
||||
/// Returns full version string.
|
||||
pub fn full_version(&self) -> String {
|
||||
let commit_dash = if self.impl_commit.is_empty() { "" } else { "-" };
|
||||
format!("{}{}{}-{}", self.impl_version, commit_dash, self.impl_commit, Self::platform())
|
||||
}
|
||||
|
||||
/// Implementation id and version.
|
||||
pub fn client_id(&self) -> String {
|
||||
format!("{}/v{}", self.impl_name, self.full_version())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,9 @@ use network;
|
||||
use keystore;
|
||||
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
Io(::std::io::Error) #[doc="IO error"];
|
||||
}
|
||||
links {
|
||||
Client(client::error::Error, client::error::ErrorKind) #[doc="Client error"];
|
||||
Network(network::error::Error, network::error::ErrorKind) #[doc="Network error"];
|
||||
|
||||
@@ -30,11 +30,15 @@ extern crate substrate_network as network;
|
||||
extern crate substrate_executor;
|
||||
extern crate substrate_client as client;
|
||||
extern crate substrate_client_db as client_db;
|
||||
extern crate substrate_codec as codec;
|
||||
extern crate substrate_extrinsic_pool as extrinsic_pool;
|
||||
extern crate substrate_rpc;
|
||||
extern crate substrate_rpc_servers as rpc;
|
||||
extern crate target_info;
|
||||
extern crate tokio;
|
||||
|
||||
#[macro_use]
|
||||
extern crate substrate_telemetry;
|
||||
extern crate substrate_telemetry as tel;
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
#[macro_use]
|
||||
@@ -48,7 +52,10 @@ mod components;
|
||||
mod error;
|
||||
mod config;
|
||||
mod chain_spec;
|
||||
pub mod chain_ops;
|
||||
|
||||
use std::io;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use futures::prelude::*;
|
||||
use keystore::Store as Keystore;
|
||||
@@ -64,11 +71,13 @@ pub use config::{Configuration, Roles, PruningMode};
|
||||
pub use chain_spec::ChainSpec;
|
||||
pub use extrinsic_pool::txpool::{Options as ExtrinsicPoolOptions};
|
||||
pub use extrinsic_pool::api::{ExtrinsicPool as ExtrinsicPoolApi};
|
||||
pub use client::ExecutionStrategy;
|
||||
|
||||
pub use components::{ServiceFactory, FullBackend, FullExecutor, LightBackend,
|
||||
LightExecutor, ExtrinsicPool, Components, PoolApi, ComponentClient,
|
||||
ComponentBlock, FullClient, LightClient, FullComponents, LightComponents,
|
||||
CodeExecutor, NetworkService, FactoryChainSpec, FactoryBlock, FactoryFullConfiguration, RuntimeGenesis,
|
||||
CodeExecutor, NetworkService, FactoryChainSpec, FactoryBlock,
|
||||
FactoryFullConfiguration, RuntimeGenesis, FactoryGenesis,
|
||||
};
|
||||
|
||||
/// Substrate service.
|
||||
@@ -78,6 +87,9 @@ pub struct Service<Components: components::Components> {
|
||||
extrinsic_pool: Arc<Components::ExtrinsicPool>,
|
||||
keystore: Keystore,
|
||||
signal: Option<Signal>,
|
||||
_rpc_http: Option<rpc::HttpServer>,
|
||||
_rpc_ws: Option<rpc::WsServer>,
|
||||
_telemetry: Option<tel::Telemetry>,
|
||||
}
|
||||
|
||||
/// Creates bare client without any networking.
|
||||
@@ -121,6 +133,7 @@ impl<Components> Service<Components>
|
||||
let (client, on_demand) = Components::build_client(&config, executor)?;
|
||||
let best_header = client.best_block_header()?;
|
||||
|
||||
let version = config.full_version();
|
||||
info!("Best block: #{}", best_header.number());
|
||||
telemetry!("node.start"; "height" => best_header.number().as_(), "best" => ?best_header.hash());
|
||||
|
||||
@@ -178,12 +191,63 @@ impl<Components> Service<Components>
|
||||
task_executor.spawn(events);
|
||||
}
|
||||
|
||||
// RPC
|
||||
let rpc_config = RpcConfig {
|
||||
chain_name: config.chain_spec.name().to_string(),
|
||||
impl_name: config.impl_name,
|
||||
impl_version: config.impl_version,
|
||||
};
|
||||
|
||||
let (rpc_http, rpc_ws) = {
|
||||
let handler = || {
|
||||
let client = client.clone();
|
||||
let chain = rpc::apis::chain::Chain::new(client.clone(), task_executor.clone());
|
||||
let author = rpc::apis::author::Author::new(client.clone(), extrinsic_pool.api(), task_executor.clone());
|
||||
rpc::rpc_handler::<ComponentBlock<Components>, _, _, _, _>(
|
||||
client,
|
||||
chain,
|
||||
author,
|
||||
rpc_config.clone(),
|
||||
)
|
||||
};
|
||||
(
|
||||
maybe_start_server(config.rpc_http, |address| rpc::start_http(address, handler()))?,
|
||||
maybe_start_server(config.rpc_ws, |address| rpc::start_ws(address, handler()))?,
|
||||
)
|
||||
};
|
||||
|
||||
// Telemetry
|
||||
let telemetry = match config.telemetry_url {
|
||||
Some(url) => {
|
||||
let name = config.name.clone();
|
||||
let impl_name = config.impl_name.to_owned();
|
||||
let version = version.clone();
|
||||
let chain_name = config.chain_spec.name().to_owned();
|
||||
Some(tel::init_telemetry(tel::TelemetryConfig {
|
||||
url: url,
|
||||
on_connect: Box::new(move || {
|
||||
telemetry!("system.connected";
|
||||
"name" => name.clone(),
|
||||
"implementation" => impl_name.clone(),
|
||||
"version" => version.clone(),
|
||||
"config" => "",
|
||||
"chain" => chain_name.clone(),
|
||||
);
|
||||
}),
|
||||
}))
|
||||
},
|
||||
None => None,
|
||||
};
|
||||
|
||||
Ok(Service {
|
||||
client: client,
|
||||
network: network,
|
||||
extrinsic_pool: extrinsic_pool,
|
||||
signal: Some(signal),
|
||||
keystore: keystore,
|
||||
_rpc_http: rpc_http,
|
||||
_rpc_ws: rpc_ws,
|
||||
_telemetry: telemetry,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -219,3 +283,42 @@ impl<Components> Drop for Service<Components> where Components: components::Comp
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn maybe_start_server<T, F>(address: Option<SocketAddr>, start: F) -> Result<Option<T>, io::Error> where
|
||||
F: Fn(&SocketAddr) -> Result<T, io::Error>,
|
||||
{
|
||||
Ok(match address {
|
||||
Some(mut address) => Some(start(&address)
|
||||
.or_else(|e| match e.kind() {
|
||||
io::ErrorKind::AddrInUse |
|
||||
io::ErrorKind::PermissionDenied => {
|
||||
warn!("Unable to bind server to {}. Trying random port.", address);
|
||||
address.set_port(0);
|
||||
start(&address)
|
||||
},
|
||||
_ => Err(e),
|
||||
})?),
|
||||
None => None,
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct RpcConfig {
|
||||
chain_name: String,
|
||||
impl_name: &'static str,
|
||||
impl_version: &'static str,
|
||||
}
|
||||
|
||||
impl substrate_rpc::system::SystemApi for RpcConfig {
|
||||
fn system_name(&self) -> substrate_rpc::system::error::Result<String> {
|
||||
Ok(self.impl_name.into())
|
||||
}
|
||||
|
||||
fn system_version(&self) -> substrate_rpc::system::error::Result<String> {
|
||||
Ok(self.impl_version.into())
|
||||
}
|
||||
|
||||
fn system_chain(&self) -> substrate_rpc::system::error::Result<String> {
|
||||
Ok(self.chain_name.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +44,9 @@ pub struct TelemetryConfig {
|
||||
pub on_connect: Box<Fn() + Send + 'static>,
|
||||
}
|
||||
|
||||
/// Telemetry service guard.
|
||||
pub type Telemetry = slog_scope::GlobalLoggerGuard;
|
||||
|
||||
/// Size of the channel for passing messages to telemetry thread.
|
||||
const CHANNEL_SIZE: usize = 262144;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user