Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Update SubstrateCli to return String #6550

Merged
merged 10 commits into from
Jul 2, 2020
24 changes: 10 additions & 14 deletions bin/node-template/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,30 @@ use crate::service;
use sc_cli::{SubstrateCli, RuntimeVersion, Role, ChainSpec};

impl SubstrateCli for Cli {
fn impl_name() -> &'static str {
"Substrate Node"
fn impl_name() -> String {
"Substrate Node".into()
}

fn impl_version() -> &'static str {
env!("SUBSTRATE_CLI_IMPL_VERSION")
fn impl_version() -> String {
env!("SUBSTRATE_CLI_IMPL_VERSION").into()
}

fn description() -> &'static str {
env!("CARGO_PKG_DESCRIPTION")
fn description() -> String {
env!("CARGO_PKG_DESCRIPTION").into()
}

fn author() -> &'static str {
env!("CARGO_PKG_AUTHORS")
fn author() -> String {
env!("CARGO_PKG_AUTHORS").into()
}

fn support_url() -> &'static str {
"support.anonymous.an"
fn support_url() -> String {
"support.anonymous.an".into()
}

fn copyright_start_year() -> i32 {
2017
}

fn executable_name() -> &'static str {
env!("CARGO_PKG_NAME")
}

fn load_spec(&self, id: &str) -> Result<Box<dyn sc_service::ChainSpec>, String> {
Ok(match id {
"dev" => Box::new(chain_spec::development_config()),
Expand Down
24 changes: 10 additions & 14 deletions bin/node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,30 @@ use node_runtime::{Block, RuntimeApi};
use sc_cli::{Result, SubstrateCli, RuntimeVersion, Role, ChainSpec};

impl SubstrateCli for Cli {
fn impl_name() -> &'static str {
"Substrate Node"
fn impl_name() -> String {
"Substrate Node".into()
}

fn impl_version() -> &'static str {
env!("SUBSTRATE_CLI_IMPL_VERSION")
fn impl_version() -> String {
env!("SUBSTRATE_CLI_IMPL_VERSION").into()
}

fn description() -> &'static str {
env!("CARGO_PKG_DESCRIPTION")
fn description() -> String {
env!("CARGO_PKG_DESCRIPTION").into()
}

fn author() -> &'static str {
env!("CARGO_PKG_AUTHORS")
fn author() -> String {
env!("CARGO_PKG_AUTHORS").into()
}

fn support_url() -> &'static str {
"https://github.com/paritytech/substrate/issues/new"
fn support_url() -> String {
"https://github.com/paritytech/substrate/issues/new".into()
}

fn copyright_start_year() -> i32 {
2017
}

fn executable_name() -> &'static str {
"substrate"
}

fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
Ok(match id {
"dev" => Box::new(chain_spec::development_config()),
Expand Down
4 changes: 2 additions & 2 deletions client/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ pub trait CliConfiguration: Sized {
let chain_spec = cli.load_spec(chain_id.as_str())?;
let base_path = self
.base_path()?
.unwrap_or_else(|| BasePath::from_project("", "", C::executable_name()));
.unwrap_or_else(|| BasePath::from_project("", "", &C::executable_name()));
let config_dir = base_path
.path()
.to_path_buf()
Expand Down Expand Up @@ -498,7 +498,7 @@ pub trait CliConfiguration: Sized {
fn init<C: SubstrateCli>(&self) -> Result<()> {
let logger_pattern = self.log_filters()?;

sp_panic_handler::set(C::support_url(), C::impl_version());
sp_panic_handler::set(&C::support_url(), &C::impl_version());

fdlimit::raise_fd_limit();
init_logger(&logger_pattern);
Expand Down
38 changes: 26 additions & 12 deletions client/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,33 @@ use structopt::{
/// its own implementation that will fill the necessary field based on the trait's functions.
pub trait SubstrateCli: Sized {
/// Implementation name.
fn impl_name() -> &'static str;
fn impl_name() -> String;

/// Implementation version.
///
/// By default this will look like this: 2.0.0-b950f731c-x86_64-linux-gnu where the hash is the
/// short commit hash of the commit of in the Git repository.
fn impl_version() -> &'static str;
fn impl_version() -> String;

/// Executable file name.
fn executable_name() -> &'static str;
///
/// Extracts the file name from `std::env::current_exe()`.
/// Resorts to the env var `CARGO_PKG_NAME` in case of Error.
fn executable_name() -> String {
std::env::current_exe().ok()
.and_then(|e| e.file_name().map(|s| s.to_os_string()))
.and_then(|w| w.into_string().ok())
.unwrap_or_else(|| env!("CARGO_PKG_NAME").into())
}

/// Executable file description.
fn description() -> &'static str;
fn description() -> String;

/// Executable file author.
fn author() -> &'static str;
fn author() -> String;

/// Support URL.
fn support_url() -> &'static str;
fn support_url() -> String;

/// Copyright starting year (x-current year)
fn copyright_start_year() -> i32;
Expand Down Expand Up @@ -119,10 +127,13 @@ pub trait SubstrateCli: Sized {
let mut full_version = Self::impl_version().to_string();
full_version.push_str("\n");

let name = Self::executable_name();
let author = Self::author();
let about = Self::description();
let app = app
.name(Self::executable_name())
.author(Self::author())
.about(Self::description())
.name(&*name)
.author(&*author)
.about(&*about)
pscott marked this conversation as resolved.
Show resolved Hide resolved
.version(full_version.as_str())
.settings(&[
AppSettings::GlobalVersion,
Expand Down Expand Up @@ -178,10 +189,13 @@ pub trait SubstrateCli: Sized {
let mut full_version = Self::impl_version().to_string();
full_version.push_str("\n");

let name = Self::executable_name();
let author = Self::author();
let about = Self::description();
let app = app
.name(Self::executable_name())
.author(Self::author())
.about(Self::description())
.name(&*name)
.author(&*author)
.about(&*about)
pscott marked this conversation as resolved.
Show resolved Hide resolved
.version(full_version.as_str());

let matches = app.get_matches_from_safe(iter)?;
Expand Down
10 changes: 5 additions & 5 deletions client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,8 +1247,8 @@ fn build_telemetry<TBl: BlockT>(
let is_authority = config.role.is_authority();
let network_id = network.local_peer_id().to_base58();
let name = config.network.node_name.clone();
let impl_name = config.impl_name.to_owned();
let version = config.impl_version;
let impl_name = config.impl_name.clone();
let impl_version = config.impl_version.clone();
let chain_name = config.chain_spec.name().to_owned();
let telemetry = sc_telemetry::init_telemetry(sc_telemetry::TelemetryConfig {
endpoints,
Expand All @@ -1265,7 +1265,7 @@ fn build_telemetry<TBl: BlockT>(
telemetry!(SUBSTRATE_INFO; "system.connected";
"name" => name.clone(),
"implementation" => impl_name.clone(),
"version" => version,
"version" => impl_version.clone(),
"config" => "",
"chain" => chain_name.clone(),
"genesis_hash" => ?genesis_hash,
Expand Down Expand Up @@ -1314,8 +1314,8 @@ fn gen_handler<TBl, TBackend, TExPool, TRpc, TCl>(

let system_info = sc_rpc::system::SystemInfo {
chain_name: config.chain_spec.name().into(),
impl_name: config.impl_name.into(),
impl_version: config.impl_version.into(),
impl_name: config.impl_name.clone(),
impl_version: config.impl_version.clone(),
properties: config.chain_spec.properties(),
chain_type: config.chain_spec.chain_type(),
};
Expand Down
4 changes: 2 additions & 2 deletions client/service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ use tempfile::TempDir;
#[derive(Debug)]
pub struct Configuration {
/// Implementation name
pub impl_name: &'static str,
pub impl_name: String,
/// Implementation version (see sc-cli to see an example of format)
pub impl_version: &'static str,
pub impl_version: String,
/// Node role.
pub role: Role,
/// How to spawn background tasks. Mandatory, otherwise creating a `Service` will error.
Expand Down
4 changes: 2 additions & 2 deletions client/service/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ fn node_config<G: RuntimeGenesis + 'static, E: ChainSpecExtension + Clone + 'sta
};

Configuration {
impl_name: "network-test-impl",
impl_version: "0.1",
impl_name: String::from("network-test-impl"),
impl_version: String::from("0.1"),
role,
task_executor,
transaction_pool: Default::default(),
Expand Down
7 changes: 4 additions & 3 deletions primitives/panic-handler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ enum OnPanic {
///
/// The `bug_url` parameter is an invitation for users to visit that URL to submit a bug report
/// in the case where a panic happens.
pub fn set(bug_url: &'static str, version: &str) {
pub fn set(bug_url: &str, version: &str) {
panic::set_hook(Box::new({
let version = version.to_string();
let bug_url = bug_url.to_string();
move |c| {
panic_hook(c, bug_url, &version)
panic_hook(c, &bug_url, &version)
}
}));
}
Expand Down Expand Up @@ -130,7 +131,7 @@ impl Drop for AbortGuard {
}

/// Function being called when a panic happens.
fn panic_hook(info: &PanicInfo, report_url: &'static str, version: &str) {
fn panic_hook(info: &PanicInfo, report_url: &str, version: &str) {
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);
Expand Down
4 changes: 2 additions & 2 deletions utils/browser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ where
disable_grandpa: Default::default(),
execution_strategies: Default::default(),
force_authoring: Default::default(),
impl_name: "parity-substrate",
impl_version: "0.0.0",
impl_name: String::from("parity-substrate"),
impl_version: String::from("0.0.0"),
offchain_worker: Default::default(),
prometheus_config: Default::default(),
pruning: Default::default(),
Expand Down