Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging to file (experimental) #15

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
RPC_URL_WS=ws://127.0.0.1:8545
DB_URL=mongodb://localhost:27017
LOG_TO_FILE=false
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
target
*.json
arbData
logs

47 changes: 47 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
4 changes: 2 additions & 2 deletions src/commands/scan.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 10 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}

Expand All @@ -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),
}
}
}
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ pub mod config;
pub mod data;
pub mod error;
pub mod event_history;
pub mod hindsight;
pub mod interfaces;
pub mod logs;
pub mod processor;
pub mod sim;
pub mod util;

Expand Down
24 changes: 24 additions & 0 deletions src/logs.rs
Original file line number Diff line number Diff line change
@@ -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<tracing_appender::non_blocking::WorkerGuard>,
}

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),
}
}
}
16 changes: 15 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use hindsight::{
config::Config,
data::arbs::ArbFilterParams,
debug, info,
logs::FileLogger,
};
use revm::primitives::bitvec::macros::internal::funty::Fundamental;

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/hindsight.rs → src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
let client = get_ws_client(Some(rpc_url_ws)).await?;
Ok(Self { client })
Expand Down Expand Up @@ -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!({
Expand Down