extract common relay initialization func (#392)

This commit is contained in:
Svyatoslav Nikolsky
2020-10-05 10:01:19 +03:00
committed by Bastian Köcher
parent fe2d786f13
commit 52b11a58c9
6 changed files with 77 additions and 46 deletions
+3 -40
View File
@@ -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<EthereumConnectionParams, String> {
let mut params = EthereumConnectionParams::default();
if let Some(eth_host) = matches.value_of("eth-host") {
+9 -5
View File
@@ -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" }
+4 -1
View File
@@ -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<relay_millau_client::Millau>;
@@ -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);
}
}
+3
View File
@@ -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
+57
View File
@@ -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 <http://www.gnu.org/licenses/>.
//! 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();
}
+1
View File
@@ -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).