Skip to content

Commit

Permalink
Remove unwrap calls from main
Browse files Browse the repository at this point in the history
  • Loading branch information
coa-telos authored and poplexity committed Sep 17, 2024
1 parent 5daae00 commit 39f393e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
40 changes: 19 additions & 21 deletions client/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
extern crate alloc;

use std::fs;

use crate::config::{AppConfig, CliArgs};
use crate::main_utils::{parse_log_level, run_client};
use clap::Parser;
use eyre::{Context, Result};
use reth_primitives::revm_primitives::bitvec::macros::internal::funty::Fundamental;
use tokio_retry::strategy::FixedInterval;
use tokio_retry::Retry;
use tokio_retry::{strategy::FixedInterval, Retry};
use tracing::info;
use tracing_subscriber::fmt;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt};

mod auth;
mod client;
Expand All @@ -20,27 +20,25 @@ mod json_rpc;
mod main_utils;

#[tokio::main]
async fn main() {
async fn main() -> Result<()> {
let args = CliArgs::parse();
let config_contents = std::fs::read_to_string(&args.config).unwrap();
let config: AppConfig = toml::from_str(&config_contents).unwrap();
let log_level_filter = parse_log_level(&config.log_level).unwrap();
let config_contents = fs::read_to_string(&args.config)?;
let config: AppConfig = toml::from_str(&config_contents)?;
let log_level_filter = parse_log_level(&config.log_level)?;

tracing_subscriber::registry()
.with(fmt::layer())
.with(log_level_filter)
.init();

let retry_strategy = FixedInterval::from_millis(config.retry_interval.unwrap_or(8000u64))
.take(config.max_retry.unwrap_or(8u8).as_usize());

let result = Retry::spawn(retry_strategy, || run_client(args.clone(), config.clone())).await;
match result {
Ok(_) => {
info!("Consensus client Finished!");
}
Err(e) => {
info!("Stopping consensus client, run failed! Error: {:?}", e);
}
}
let retry_interval = config.retry_interval.unwrap_or(8000u64);
let max_retries = config.max_retry.unwrap_or(8u8).as_usize();
let retry_strategy = FixedInterval::from_millis(retry_interval).take(max_retries);

Retry::spawn(retry_strategy, || run_client(args.clone(), config.clone()))
.await
.wrap_err("Stopping consensus client, run failed!")?;

info!("Consensus client Finished!");
Ok(())
}
5 changes: 3 additions & 2 deletions client/src/main_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::client::Error::{CannotStartConsensusClient, TranslatorShutdown};
use crate::client::{ConsensusClient, Error, Shutdown};
use crate::config::{AppConfig, CliArgs};
use crate::data::Block;
use eyre::eyre;
use telos_translator_rs::block::TelosEVMBlock;
use telos_translator_rs::translator::Translator;
use tokio::sync::mpsc;
Expand Down Expand Up @@ -87,14 +88,14 @@ pub async fn build_consensus_client(
Ok((client, lib))
}

pub fn parse_log_level(s: &str) -> Result<LevelFilter, String> {
pub fn parse_log_level(s: &str) -> eyre::Result<LevelFilter> {
match s.to_lowercase().as_str() {
"off" => Ok(LevelFilter::OFF),
"error" => Ok(LevelFilter::ERROR),
"warn" => Ok(LevelFilter::WARN),
"info" => Ok(LevelFilter::INFO),
"debug" => Ok(LevelFilter::DEBUG),
"trace" => Ok(LevelFilter::TRACE),
_ => Err(format!("Unknown log level: {}", s)),
_ => Err(eyre!("Unknown log level: {s}")),
}
}

0 comments on commit 39f393e

Please sign in to comment.