From 92652b26a29fb8e43561b1dba986a0646bd8c429 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Fri, 8 Dec 2023 15:36:45 +0000 Subject: [PATCH] add cli args to RPC scanning binary --- Cargo.toml | 2 - consensus/Cargo.toml | 4 +- consensus/src/bin/scan_chain.rs | 103 ++++++++++++++++++++++---------- consensus/src/rpc.rs | 6 +- 4 files changed, 76 insertions(+), 39 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 757ed53..4f4e99a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,14 +15,12 @@ members = [ ] [profile.release] -panic = 'abort' lto = true # Build with LTO strip = "none" # Keep panic stack traces codegen-units = 1 # Optimize for binary speed over compile times opt-level = 3 [profile.dev] -panic = 'abort' lto = false strip = "none" # Not much slower compile times than opt-level 0, but much faster code. diff --git a/consensus/Cargo.toml b/consensus/Cargo.toml index 935b70b..13d3043 100644 --- a/consensus/Cargo.toml +++ b/consensus/Cargo.toml @@ -22,7 +22,8 @@ binaries = [ "dep:monero-epee-bin-serde", "dep:monero-wire", "dep:bincode", - "dep:dirs" + "dep:dirs", + "dep:clap" ] [dependencies] @@ -56,6 +57,7 @@ serde = {version = "1", optional = true, features = ["derive"]} tracing-subscriber = {version = "0.3", optional = true} bincode = {version = "2.0.0-rc.3", optional = true} dirs = {version="5.0", optional = true} +clap = { version = "4.4.8", optional = true, features = ["derive"] } # here to help cargo to pick a version - remove me syn = "2.0.37" diff --git a/consensus/src/bin/scan_chain.rs b/consensus/src/bin/scan_chain.rs index 68319da..c74f11c 100644 --- a/consensus/src/bin/scan_chain.rs +++ b/consensus/src/bin/scan_chain.rs @@ -2,6 +2,7 @@ use std::{ops::Range, path::PathBuf, sync::Arc}; +use clap::Parser; use futures::{ channel::{mpsc, oneshot}, SinkExt, StreamExt, @@ -230,48 +231,84 @@ where Ok(()) } +#[derive(Parser)] +struct Args { + /// The log level, valid values: + /// "off", "error", "warn", "info", "debug", "trace", or a number 0-5. + #[arg(short, long, default_value = "info")] + log_level: LevelFilter, + /// The network we should scan, valid values: + /// "mainnet", "testnet", "stagenet". + #[arg(short, long, default_value = "mainnet")] + network: String, + /// A list of RPC nodes we should use. + /// Example: http://xmr-node.cakewallet.com:18081 + #[arg(long)] + rpc_nodes: Vec, + /// Stops the scanner from including the default list of nodes, this is not + /// recommended unless you have sufficient self defined nodes with `rpc_nodes` + #[arg(long)] + dont_use_default_nodes: bool, + /// The directory/ folder to save the scanning cache in. + /// This will default to your user cache directory. + #[arg(long)] + cache_dir: Option, +} + #[tokio::main] async fn main() { - // TODO: take this in as config options: - // - nodes to connect to - // - block batch size (not header) - // - network - // - tracing level + let args = Args::parse(); + + if args.dont_use_default_nodes & args.rpc_nodes.is_empty() { + panic!("Can't run scanner with no RPC nodes, see `--help` ") + } tracing_subscriber::fmt() - .with_max_level(LevelFilter::INFO) + .with_max_level(args.log_level) .init(); - let network = Network::Mainnet; + let network = match args.network.as_str() { + "mainnet" => Network::Mainnet, + _ => panic!("Invalid network, scanner currently only supports mainnet"), + }; - let mut file_for_cache = dirs::cache_dir().unwrap(); + let mut file_for_cache = match args.cache_dir { + Some(dir) => dir, + None => dirs::cache_dir().unwrap(), + }; file_for_cache.push("cuprate_rpc_scanning_cache.bin"); - let urls = vec![ - "http://xmr-node.cakewallet.com:18081".to_string(), - "https://node.sethforprivacy.com".to_string(), - "http://nodex.monerujo.io:18081".to_string(), - "http://nodes.hashvault.pro:18081".to_string(), - "http://node.c3pool.com:18081".to_string(), - "http://node.trocador.app:18089".to_string(), - "http://xmr.lukas.services:18089".to_string(), - "http://xmr-node-eu.cakewallet.com:18081".to_string(), - "http://38.105.209.54:18089".to_string(), - "http://68.118.241.70:18089".to_string(), - "http://145.239.97.211:18089".to_string(), - // - "http://xmr-node.cakewallet.com:18081".to_string(), - "https://node.sethforprivacy.com".to_string(), - "http://nodex.monerujo.io:18081".to_string(), - "http://nodes.hashvault.pro:18081".to_string(), - "http://node.c3pool.com:18081".to_string(), - "http://node.trocador.app:18089".to_string(), - "http://xmr.lukas.services:18089".to_string(), - "http://xmr-node-eu.cakewallet.com:18081".to_string(), - "http://38.105.209.54:18089".to_string(), - "http://68.118.241.70:18089".to_string(), - "http://145.239.97.211:18089".to_string(), - ]; + let mut urls = if args.dont_use_default_nodes { + vec![] + } else { + vec![ + "http://xmr-node.cakewallet.com:18081".to_string(), + "https://node.sethforprivacy.com".to_string(), + "http://nodex.monerujo.io:18081".to_string(), + "http://nodes.hashvault.pro:18081".to_string(), + "http://node.c3pool.com:18081".to_string(), + "http://node.trocador.app:18089".to_string(), + "http://xmr.lukas.services:18089".to_string(), + "http://xmr-node-eu.cakewallet.com:18081".to_string(), + "http://38.105.209.54:18089".to_string(), + "http://68.118.241.70:18089".to_string(), + "http://145.239.97.211:18089".to_string(), + // + "http://xmr-node.cakewallet.com:18081".to_string(), + "https://node.sethforprivacy.com".to_string(), + "http://nodex.monerujo.io:18081".to_string(), + "http://nodes.hashvault.pro:18081".to_string(), + "http://node.c3pool.com:18081".to_string(), + "http://node.trocador.app:18089".to_string(), + "http://xmr.lukas.services:18089".to_string(), + "http://xmr-node-eu.cakewallet.com:18081".to_string(), + "http://38.105.209.54:18089".to_string(), + "http://68.118.241.70:18089".to_string(), + "http://145.239.97.211:18089".to_string(), + ] + }; + + urls.extend(args.rpc_nodes.into_iter()); let rpc_config = RpcConfig::new(MAX_BLOCKS_IN_RANGE, MAX_BLOCKS_HEADERS_IN_RANGE); let rpc_config = Arc::new(std::sync::RwLock::new(rpc_config)); diff --git a/consensus/src/rpc.rs b/consensus/src/rpc.rs index bb2a025..af9db58 100644 --- a/consensus/src/rpc.rs +++ b/consensus/src/rpc.rs @@ -130,7 +130,7 @@ where fn call(&mut self, req: DatabaseRequest) -> Self::Future { let this = self.rpcs.clone(); let config_mutex = self.config.clone(); - let config = config_mutex.read().unwrap(); + let config = config_mutex.clone(); let cache = self.cache.clone(); @@ -166,7 +166,7 @@ where DatabaseRequest::BlockBatchInRange, DatabaseResponse::BlockBatchInRange, resp_to_ret, - config.max_blocks_per_node, + config.read().unwrap().max_blocks_per_node, ) .boxed() } @@ -183,7 +183,7 @@ where DatabaseRequest::BlockExtendedHeaderInRange, DatabaseResponse::BlockExtendedHeaderInRange, resp_to_ret, - config.max_block_headers_per_node, + config.read().unwrap().max_block_headers_per_node, ) .boxed() }