diff --git a/binaries/cuprated/src/commands.rs b/binaries/cuprated/src/commands.rs index 38d0b3c5..85da57a8 100644 --- a/binaries/cuprated/src/commands.rs +++ b/binaries/cuprated/src/commands.rs @@ -1,10 +1,21 @@ use std::{io, thread::sleep, time::Duration}; use clap::{builder::TypedValueParser, Parser, ValueEnum}; - use tokio::sync::mpsc; +use tower::{Service, ServiceExt}; use tracing::level_filters::LevelFilter; +use cuprate_consensus_context::{ + BlockChainContextRequest, BlockChainContextResponse, BlockChainContextService, +}; +use cuprate_helper::time::secs_to_hms; + +use crate::{ + constants::PANIC_CRITICAL_SERVICE_ERROR, + logging::{self, CupratedTracingFilter}, + statics, +}; + const PARSER_TEMPLATE: &str = "{all-args}"; /// A command received from [`io::stdin`]. @@ -66,3 +77,50 @@ pub fn command_listener(incoming_commands: mpsc::Sender) -> ! { line.clear(); } } + +/// The [`Command`] handler loop. +pub async fn io_loop( + mut incoming_commands: mpsc::Receiver, + mut context_service: BlockChainContextService, +) -> ! { + while let Some(command) = incoming_commands.recv().await { + match command { + Command::SetLog { + level, + output_target, + } => { + let modify_output = |filter: &mut CupratedTracingFilter| { + if let Some(level) = level { + filter.level = level; + } + println!("NEW LOG FILTER: {filter}"); + }; + + match output_target { + OutputTarget::File => logging::modify_file_output(modify_output), + OutputTarget::Stdout => logging::modify_stdout_output(modify_output), + } + } + Command::Status => { + let BlockChainContextResponse::Context(blockchain_context) = context_service + .ready() + .await + .expect(PANIC_CRITICAL_SERVICE_ERROR) + .call(BlockChainContextRequest::Context) + .await + .expect(PANIC_CRITICAL_SERVICE_ERROR) + else { + unreachable!(); + }; + let context = blockchain_context.unchecked_blockchain_context(); + + let uptime = statics::START_INSTANT.elapsed().unwrap_or_default(); + let (hours, minutes, second) = secs_to_hms(uptime.as_secs()); + + println!("STATUS:\n uptime: {hours}h {minutes}m {second}s,\n height: {},\n top_hash: {}", context.chain_height, hex::encode(context.top_hash)); + } + } + } + + unreachable!() +} diff --git a/binaries/cuprated/src/main.rs b/binaries/cuprated/src/main.rs index 100e8f3d..fbb73f88 100644 --- a/binaries/cuprated/src/main.rs +++ b/binaries/cuprated/src/main.rs @@ -28,10 +28,7 @@ use cuprate_consensus_context::{ use cuprate_helper::time::secs_to_hms; use crate::{ - commands::{Command, OutputTarget}, - config::Config, - constants::PANIC_CRITICAL_SERVICE_ERROR, - logging::CupratedTracingFilter, + config::Config, constants::PANIC_CRITICAL_SERVICE_ERROR, logging::CupratedTracingFilter, }; mod blockchain; @@ -131,7 +128,7 @@ fn main() { std::thread::spawn(|| commands::command_listener(command_tx)); // Wait on the io_loop, spawned on a separate task as this improves performance. - tokio::spawn(io_loop(command_rx, context_svc)) + tokio::spawn(commands::io_loop(command_rx, context_svc)) .await .unwrap(); }); @@ -155,50 +152,3 @@ fn init_global_rayon_pool(config: &Config) { .build_global() .unwrap(); } - -/// The [`Command`] handler loop. -async fn io_loop( - mut incoming_commands: mpsc::Receiver, - mut context_service: BlockChainContextService, -) -> ! { - while let Some(command) = incoming_commands.recv().await { - match command { - Command::SetLog { - level, - output_target, - } => { - let modify_output = |filter: &mut CupratedTracingFilter| { - if let Some(level) = level { - filter.level = level; - } - println!("NEW LOG FILTER: {filter}"); - }; - - match output_target { - OutputTarget::File => logging::modify_file_output(modify_output), - OutputTarget::Stdout => logging::modify_stdout_output(modify_output), - } - } - Command::Status => { - let BlockChainContextResponse::Context(blockchain_context) = context_service - .ready() - .await - .expect(PANIC_CRITICAL_SERVICE_ERROR) - .call(BlockChainContextRequest::Context) - .await - .expect(PANIC_CRITICAL_SERVICE_ERROR) - else { - unreachable!(); - }; - let context = blockchain_context.unchecked_blockchain_context(); - - let uptime = statics::START_INSTANT.elapsed().unwrap_or_default(); - let (hours, minutes, second) = secs_to_hms(uptime.as_secs()); - - println!("STATUS:\n uptime: {hours}h {minutes}m {second}s,\n height: {},\n top_hash: {}", context.chain_height, hex::encode(context.top_hash)); - } - } - } - - unreachable!() -}