diff --git a/bridges/relays/ethereum/src/main.rs b/bridges/relays/ethereum/src/main.rs index 20f18c3310..fd46847c6d 100644 --- a/bridges/relays/ethereum/src/main.rs +++ b/bridges/relays/ethereum/src/main.rs @@ -35,7 +35,7 @@ use headers_relay::sync::TargetTransactionMode; use hex_literal::hex; use instances::{BridgeInstance, Kovan, RialtoPoA}; use parity_crypto::publickey::{KeyPair, Secret}; -use relay_utils::metrics::MetricsParams; +use relay_utils::{initialize::initialize_relay, metrics::MetricsParams}; use sp_core::crypto::Pair; use substrate_sync_loop::SubstrateSyncParams; @@ -43,10 +43,10 @@ use headers_relay::sync::HeadersSyncParams; use relay_ethereum_client::{ConnectionParams as EthereumConnectionParams, SigningParams as EthereumSigningParams}; use relay_rialto_client::SigningParams as RialtoSigningParams; use relay_substrate_client::ConnectionParams as SubstrateConnectionParams; -use std::{io::Write, sync::Arc}; +use std::sync::Arc; fn main() { - initialize(); + initialize_relay(); let yaml = clap::load_yaml!("cli.yml"); let matches = clap::App::from_yaml(yaml).get_matches(); @@ -118,43 +118,6 @@ fn main() { } } -fn initialize() { - let mut builder = env_logger::Builder::new(); - - let filters = match std::env::var("RUST_LOG") { - Ok(env_filters) => format!("bridge=info,{}", env_filters), - Err(_) => "bridge=info".into(), - }; - - builder.parse_filters(&filters); - builder.format(move |buf, record| { - writeln!(buf, "{}", { - let timestamp = time::OffsetDateTime::now_local().format("%Y-%m-%d %H:%M:%S %z"); - if cfg!(windows) { - format!("{} {} {} {}", timestamp, record.level(), record.target(), record.args()) - } else { - use ansi_term::Colour as Color; - let log_level = match record.level() { - log::Level::Error => Color::Fixed(9).bold().paint(record.level().to_string()), - log::Level::Warn => Color::Fixed(11).bold().paint(record.level().to_string()), - log::Level::Info => Color::Fixed(10).paint(record.level().to_string()), - log::Level::Debug => Color::Fixed(14).paint(record.level().to_string()), - log::Level::Trace => Color::Fixed(12).paint(record.level().to_string()), - }; - format!( - "{} {} {} {}", - Color::Fixed(8).bold().paint(timestamp), - log_level, - Color::Fixed(8).paint(record.target()), - record.args() - ) - } - }) - }); - - builder.init(); -} - fn ethereum_connection_params(matches: &clap::ArgMatches) -> Result { let mut params = EthereumConnectionParams::default(); if let Some(eth_host) = matches.value_of("eth-host") { diff --git a/bridges/relays/substrate/Cargo.toml b/bridges/relays/substrate/Cargo.toml index 3351a3fb43..bd52598ff5 100644 --- a/bridges/relays/substrate/Cargo.toml +++ b/bridges/relays/substrate/Cargo.toml @@ -8,15 +8,19 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0" [dependencies] async-std = "1.6.2" async-trait = "0.1.41" -bp-rialto = { path = "../../primitives/rialto" } codec = { package = "parity-scale-codec", version = "1.3.4" } futures = "0.3.5" -headers-relay = { path = "../headers-relay" } log = "0.4.11" -messages-relay = { path = "../messages-relay" } paste = "1.0" +sp-runtime = "2.0" +structopt = "0.3" + +# Bridge dependencies + +bp-rialto = { path = "../../primitives/rialto" } +headers-relay = { path = "../headers-relay" } +messages-relay = { path = "../messages-relay" } relay-millau-client = { path = "../millau-client" } relay-rialto-client = { path = "../rialto-client" } relay-substrate-client = { path = "../substrate-client" } -sp-runtime = "2.0" -structopt = "0.3" +relay-utils = { path = "../utils" } diff --git a/bridges/relays/substrate/src/main.rs b/bridges/relays/substrate/src/main.rs index a42d721759..09728ef694 100644 --- a/bridges/relays/substrate/src/main.rs +++ b/bridges/relays/substrate/src/main.rs @@ -20,6 +20,7 @@ use relay_rialto_client::SigningParams as RialtoSigningParams; use relay_substrate_client::ConnectionParams; +use relay_utils::initialize::initialize_relay; /// Millau node client. pub type MillauClient = relay_substrate_client::Client; @@ -30,6 +31,8 @@ mod cli; mod millau_headers_to_rialto; fn main() { + initialize_relay(); + let result = async_std::task::block_on(run_command(cli::parse_args())); if let Err(error) = result { log::error!(target: "bridge", "Failed to start relay: {}", error); @@ -58,7 +61,7 @@ async fn run_command(command: cli::Command) -> Result<(), String> { rialto_sign.rialto_signer_password.as_deref(), ) .map_err(|e| format!("Failed to parse rialto-signer: {:?}", e))?; - millau_headers_to_rialto::run(millau_client, rialto_client, rialto_sign) + millau_headers_to_rialto::run(millau_client, rialto_client, rialto_sign); } } diff --git a/bridges/relays/utils/Cargo.toml b/bridges/relays/utils/Cargo.toml index 04113bfdd1..eeb49369f9 100644 --- a/bridges/relays/utils/Cargo.toml +++ b/bridges/relays/utils/Cargo.toml @@ -6,11 +6,14 @@ edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" [dependencies] +ansi_term = "0.12" async-std = "1.6.5" backoff = "0.2" +env_logger = "0.7.0" futures = "0.3.5" log = "0.4.11" sysinfo = "0.15" +time = "0.2" # Substrate dependencies diff --git a/bridges/relays/utils/src/initialize.rs b/bridges/relays/utils/src/initialize.rs new file mode 100644 index 0000000000..c6c395d18e --- /dev/null +++ b/bridges/relays/utils/src/initialize.rs @@ -0,0 +1,57 @@ +// Copyright 2019-2020 Parity Technologies (UK) Ltd. +// This file is part of Parity Bridges Common. + +// Parity Bridges Common 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. + +// Parity Bridges Common 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 Parity Bridges Common. If not, see . + +//! Relayer initialization functions. + +use std::io::Write; + +/// Initialize relay environment. +pub fn initialize_relay() { + let mut builder = env_logger::Builder::new(); + + let filters = match std::env::var("RUST_LOG") { + Ok(env_filters) => format!("bridge=info,{}", env_filters), + Err(_) => "bridge=info".into(), + }; + + builder.parse_filters(&filters); + builder.format(move |buf, record| { + writeln!(buf, "{}", { + let timestamp = time::OffsetDateTime::now_local().format("%Y-%m-%d %H:%M:%S %z"); + if cfg!(windows) { + format!("{} {} {} {}", timestamp, record.level(), record.target(), record.args()) + } else { + use ansi_term::Colour as Color; + let log_level = match record.level() { + log::Level::Error => Color::Fixed(9).bold().paint(record.level().to_string()), + log::Level::Warn => Color::Fixed(11).bold().paint(record.level().to_string()), + log::Level::Info => Color::Fixed(10).paint(record.level().to_string()), + log::Level::Debug => Color::Fixed(14).paint(record.level().to_string()), + log::Level::Trace => Color::Fixed(12).paint(record.level().to_string()), + }; + format!( + "{} {} {} {}", + Color::Fixed(8).bold().paint(timestamp), + log_level, + Color::Fixed(8).paint(record.target()), + record.args() + ) + } + }) + }); + + builder.init(); +} diff --git a/bridges/relays/utils/src/lib.rs b/bridges/relays/utils/src/lib.rs index 0b4d682c1c..49f2ae9965 100644 --- a/bridges/relays/utils/src/lib.rs +++ b/bridges/relays/utils/src/lib.rs @@ -27,6 +27,7 @@ pub const MAX_BACKOFF_INTERVAL: Duration = Duration::from_secs(60); /// reconnection again. pub const CONNECTION_ERROR_DELAY: Duration = Duration::from_secs(10); +pub mod initialize; pub mod metrics; /// Macro that returns (client, Err(error)) tuple from function if result is Err(error).