From ef01f6c6d61e10119163370cda1097da3b24d27b Mon Sep 17 00:00:00 2001 From: zeroXbrock <2791467+zeroXbrock@users.noreply.github.com> Date: Tue, 8 Aug 2023 16:44:39 -0700 Subject: [PATCH 1/3] rename Hindsight module to Processor --- src/commands/scan.rs | 4 ++-- src/lib.rs | 2 +- src/main.rs | 1 + src/{hindsight.rs => processor.rs} | 6 +++--- 4 files changed, 7 insertions(+), 6 deletions(-) rename src/{hindsight.rs => processor.rs} (98%) diff --git a/src/commands/scan.rs b/src/commands/scan.rs index 29a8059..bccccae 100644 --- a/src/commands/scan.rs +++ b/src/commands/scan.rs @@ -1,8 +1,8 @@ use crate::config::Config; use crate::data::arbs::ArbDb; use crate::event_history::event_history_url; -use crate::hindsight::Hindsight; use crate::info; +use crate::processor::Processor; use crate::sim::processor::H256Map; use crate::util::{fetch_txs, filter_events_by_topic, get_ws_client}; use crate::Result; @@ -56,7 +56,7 @@ pub async fn run(params: ScanOptions, config: Config) -> Result<()> { ); let ws_client = get_ws_client(None).await?; let mevshare = EventClient::default(); - let hindsight = Hindsight::new(config.rpc_url_ws).await?; + let hindsight = Processor::new(config.rpc_url_ws).await?; let db = ArbDb::new(None).await?; let mut event_params: EventHistoryParams = params.clone().into(); diff --git a/src/lib.rs b/src/lib.rs index 22fe06f..e4c060c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,8 +3,8 @@ pub mod config; pub mod data; pub mod error; pub mod event_history; -pub mod hindsight; pub mod interfaces; +pub mod processor; pub mod sim; pub mod util; diff --git a/src/main.rs b/src/main.rs index 5001a7f..666d254 100644 --- a/src/main.rs +++ b/src/main.rs @@ -66,6 +66,7 @@ async fn main() -> anyhow::Result<()> { tracing_subscriber::fmt::init(); let config = Config::default(); let cli = Cli::parse(); + log4rs::init_file("log4rs.yml", Default::default()).expect("failed to load log4rs.yml"); match cli.command { Some(Commands::Scan { diff --git a/src/hindsight.rs b/src/processor.rs similarity index 98% rename from src/hindsight.rs rename to src/processor.rs index 3799c9f..f6118d1 100644 --- a/src/hindsight.rs +++ b/src/processor.rs @@ -10,11 +10,11 @@ use futures::future; use mev_share_sse::EventHistory; #[derive(Clone, Debug)] -pub struct Hindsight { +pub struct Processor { pub client: WsClient, } -impl Hindsight { +impl Processor { pub async fn new(rpc_url_ws: String) -> Result { let client = get_ws_client(Some(rpc_url_ws)).await?; Ok(Self { client }) @@ -81,7 +81,7 @@ mod tests { #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn it_processes_orderflow() -> Result<()> { let config = Config::default(); - let hindsight = Hindsight::new(config.rpc_url_ws).await?; + let hindsight = Processor::new(config.rpc_url_ws).await?; // data from an actual juicy event let juicy_event: EventHistory = serde_json::from_value(json!({ From 22c5b46ae5ef251aae712f29c2099deec8f073a9 Mon Sep 17 00:00:00 2001 From: zeroXbrock <2791467+zeroXbrock@users.noreply.github.com> Date: Tue, 8 Aug 2023 16:46:36 -0700 Subject: [PATCH 2/3] remove bad code --- src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 666d254..5001a7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -66,7 +66,6 @@ async fn main() -> anyhow::Result<()> { tracing_subscriber::fmt::init(); let config = Config::default(); let cli = Cli::parse(); - log4rs::init_file("log4rs.yml", Default::default()).expect("failed to load log4rs.yml"); match cli.command { Some(Commands::Scan { From e60e2f4ae2ecc701982be0cd095e4c5233db7b67 Mon Sep 17 00:00:00 2001 From: zeroXbrock <2791467+zeroXbrock@users.noreply.github.com> Date: Tue, 8 Aug 2023 19:26:12 -0700 Subject: [PATCH 3/3] optionally log logs to file --- .env.example | 1 + .gitignore | 1 + Cargo.lock | 47 +++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 ++- src/config.rs | 12 ++++++++++-- src/lib.rs | 1 + src/logs.rs | 24 ++++++++++++++++++++++++ src/main.rs | 16 +++++++++++++++- 8 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 src/logs.rs diff --git a/.env.example b/.env.example index 6657819..6d91e4c 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ RPC_URL_WS=ws://127.0.0.1:8545 DB_URL=mongodb://localhost:27017 +LOG_TO_FILE=false diff --git a/.gitignore b/.gitignore index 8d23afd..f1079d1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ target *.json arbData +logs diff --git a/Cargo.lock b/Cargo.lock index 7f6d7c8..92611de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1839,6 +1839,7 @@ dependencies = [ "serde_json", "tokio", "tracing", + "tracing-appender", "tracing-subscriber", "uniswap_v3_math", ] @@ -2321,6 +2322,15 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata", +] + [[package]] name = "matches" version = "0.1.10" @@ -3156,6 +3166,15 @@ dependencies = [ "regex-syntax 0.7.2", ] +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", +] + [[package]] name = "regex-syntax" version = "0.6.29" @@ -4361,6 +4380,17 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-appender" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09d48f71a791638519505cefafe162606f706c25592e4bde4d97600c0195312e" +dependencies = [ + "crossbeam-channel", + "time 0.3.22", + "tracing-subscriber", +] + [[package]] name = "tracing-attributes" version = "0.1.26" @@ -4403,18 +4433,35 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-serde" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc6b213177105856957181934e4920de57730fc69bf42c37ee5bb664d406d9e1" +dependencies = [ + "serde", + "tracing-core", +] + [[package]] name = "tracing-subscriber" version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" dependencies = [ + "matchers", "nu-ansi-term", + "once_cell", + "regex", + "serde", + "serde_json", "sharded-slab", "smallvec", "thread_local", + "tracing", "tracing-core", "tracing-log", + "tracing-serde", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index c54f4c4..8c0db00 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,5 +21,6 @@ serde = "1.0.164" serde_json = {version = "1.0.99", features = ["arbitrary_precision", "std", "preserve_order"]} tokio = {version = "1.29.1", features = ["macros", "net", "process", "rt", "rt-multi-thread", "sync", "time"]} tracing = "0.1.37" -tracing-subscriber = "0.3.17" +tracing-appender = "0.2.2" +tracing-subscriber = { version = "0.3.17", features = ["env-filter", "fmt", "json", "tracing-log", "tracing"] } uniswap_v3_math = {git = "https://github.com/0xKitsune/uniswap_v3_math.git"} diff --git a/src/config.rs b/src/config.rs index 107d9c3..4516ebb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,11 +5,16 @@ use std::env; pub struct Config { pub rpc_url_ws: String, pub db_url: String, + pub log_to_file: bool, } impl Config { - pub fn new(rpc_url_ws: String, db_url: String) -> Config { - Config { rpc_url_ws, db_url } + pub fn new(rpc_url_ws: String, db_url: String, log_to_file: bool) -> Config { + Config { + rpc_url_ws, + db_url, + log_to_file, + } } } @@ -23,6 +28,9 @@ impl Default for Config { Config { db_url: env::var("DB_URL").expect("DB_URL must be set"), rpc_url_ws: env::var("RPC_URL_WS").expect("RPC_URL_WS must be set"), + log_to_file: env::var("LOG_TO_FILE") + .map(|v| v.to_string() == "true") + .unwrap_or(false), } } } diff --git a/src/lib.rs b/src/lib.rs index e4c060c..d235a28 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ pub mod data; pub mod error; pub mod event_history; pub mod interfaces; +pub mod logs; pub mod processor; pub mod sim; pub mod util; diff --git a/src/logs.rs b/src/logs.rs new file mode 100644 index 0000000..0c82797 --- /dev/null +++ b/src/logs.rs @@ -0,0 +1,24 @@ +use tracing_appender::rolling::RollingFileAppender; + +fn get_file_appender() -> RollingFileAppender { + // tracing_appender::rolling::hourly("./logs", "trace.log") + tracing_appender::rolling::minutely("./logs", "trace.log") +} + +pub struct FileLogger { + _guard: Box, +} + +impl FileLogger { + pub fn new() -> Self { + let (non_blocking, guard) = tracing_appender::non_blocking(get_file_appender()); + tracing_subscriber::fmt() + .with_writer(std::io::stderr) + .with_writer(std::io::stdout) + .with_writer(non_blocking) + .init(); + Self { + _guard: Box::new(guard), + } + } +} diff --git a/src/main.rs b/src/main.rs index 5001a7f..51f4b3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use hindsight::{ config::Config, data::arbs::ArbFilterParams, debug, info, + logs::FileLogger, }; use revm::primitives::bitvec::macros::internal::funty::Fundamental; @@ -63,8 +64,21 @@ enum Commands { #[tokio::main] async fn main() -> anyhow::Result<()> { - tracing_subscriber::fmt::init(); + // + // let file_appender = get_file_appender(); + // let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender); + // tracing_subscriber::fmt().with_writer(non_blocking).init(); + + // let (non_blocking, _guard) = + // tracing_appender::non_blocking(tracing_appender::rolling::minutely("./logs", "trace.log")); let config = Config::default(); + let _f = if config.log_to_file { + println!("check `./logs/` for logs"); + Some(FileLogger::new()) + } else { + tracing_subscriber::fmt().init(); + None + }; let cli = Cli::parse(); match cli.command {