mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-05-01 01:50:00 -07:00
parser: setup clap
This commit is contained in:
@@ -3,7 +3,10 @@ use std::{process::Command, thread::sleep, time::Duration};
|
||||
use color_eyre::eyre::eyre;
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::utils::{log, log_output, retry};
|
||||
use crate::{
|
||||
utils::{log, log_output, retry},
|
||||
Config,
|
||||
};
|
||||
|
||||
struct BlockchainInfo {
|
||||
pub headers: u64,
|
||||
@@ -11,15 +14,13 @@ struct BlockchainInfo {
|
||||
}
|
||||
|
||||
pub struct BitcoinDaemon {
|
||||
path: String,
|
||||
other_args: Vec<String>,
|
||||
config: Config,
|
||||
}
|
||||
|
||||
impl BitcoinDaemon {
|
||||
pub fn new(bitcoin_dir_path: String, other_bitcoin_args: Vec<String>) -> Self {
|
||||
pub fn new(config: &Config) -> Self {
|
||||
Self {
|
||||
path: bitcoin_dir_path,
|
||||
other_args: other_bitcoin_args,
|
||||
config: config.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,13 +31,16 @@ impl BitcoinDaemon {
|
||||
|
||||
command
|
||||
.arg(self.datadir_arg())
|
||||
.arg("-blocksonly")
|
||||
.arg("-txindex=1")
|
||||
.arg("-daemon");
|
||||
|
||||
self.other_args.iter().for_each(|arg| {
|
||||
command.arg(arg);
|
||||
});
|
||||
if self.config.blocksonly.unwrap() {
|
||||
command.arg("-blocksonly");
|
||||
}
|
||||
|
||||
if let Some(ip) = self.config.rpcconnect.as_ref() {
|
||||
command.arg(format!("-rpcconnect={}", ip));
|
||||
}
|
||||
|
||||
// bitcoind -datadir=/Users/k/Developer/bitcoin -blocksonly -txindex=1 -daemon
|
||||
let output = command
|
||||
@@ -123,6 +127,10 @@ impl BitcoinDaemon {
|
||||
}
|
||||
|
||||
fn datadir_arg(&self) -> String {
|
||||
format!("-datadir={}", self.path)
|
||||
if self.config.datadir.is_none() {
|
||||
unreachable!();
|
||||
}
|
||||
|
||||
format!("-datadir={}", self.config.datadir.as_ref().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ pub use crate::{
|
||||
datasets::OHLC,
|
||||
io::{Binary, Json, Serialization},
|
||||
structs::{
|
||||
DateMap, HeightMap, SerializedDateMap, SerializedHeightMap, WNaiveDate,
|
||||
Config, DateMap, HeightMap, SerializedDateMap, SerializedHeightMap, WNaiveDate,
|
||||
HEIGHT_MAP_CHUNK_SIZE,
|
||||
},
|
||||
utils::log,
|
||||
|
||||
@@ -1,23 +1,29 @@
|
||||
use std::{env::args, path::Path};
|
||||
use std::{path::Path, thread::sleep, time::Duration};
|
||||
|
||||
use itertools::Itertools;
|
||||
use parser::{iter_blocks, log, BitcoinDB, BitcoinDaemon};
|
||||
use color_eyre::eyre::eyre;
|
||||
use parser::{iter_blocks, log, BitcoinDB, BitcoinDaemon, Config};
|
||||
|
||||
fn main() -> color_eyre::Result<()> {
|
||||
let mut args = args().collect_vec();
|
||||
let bitcoin_dir_path = args.get(1).unwrap().to_owned();
|
||||
args.drain(0..2);
|
||||
|
||||
color_eyre::install()?;
|
||||
|
||||
let deamon = BitcoinDaemon::new(bitcoin_dir_path.to_owned(), args);
|
||||
let config = Config::read();
|
||||
|
||||
if config.datadir.is_none() {
|
||||
return Err(eyre!(
|
||||
"You need to set the --datadir parameter at least once to run the parser"
|
||||
));
|
||||
}
|
||||
|
||||
config.write()?;
|
||||
|
||||
let daemon = BitcoinDaemon::new(&config);
|
||||
|
||||
loop {
|
||||
deamon.stop();
|
||||
daemon.stop();
|
||||
|
||||
// Scoped to free bitcoin's lock
|
||||
let block_count = {
|
||||
let bitcoin_db = BitcoinDB::new(Path::new(&bitcoin_dir_path), true)?;
|
||||
let bitcoin_db = BitcoinDB::new(Path::new(&config.datadir.as_ref().unwrap()), true)?;
|
||||
|
||||
// let block_count = 200_000;
|
||||
let block_count = bitcoin_db.get_block_count();
|
||||
@@ -29,12 +35,16 @@ fn main() -> color_eyre::Result<()> {
|
||||
block_count
|
||||
};
|
||||
|
||||
deamon.start();
|
||||
daemon.start();
|
||||
|
||||
if deamon.check_if_fully_synced() {
|
||||
deamon.wait_for_new_block(block_count - 1);
|
||||
if daemon.check_if_fully_synced() {
|
||||
daemon.wait_for_new_block(block_count - 1);
|
||||
} else {
|
||||
deamon.wait_sync();
|
||||
daemon.wait_sync();
|
||||
}
|
||||
|
||||
if let Some(delay) = config.delay {
|
||||
sleep(Duration::from_secs(delay))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
62
parser/src/structs/config.rs
Normal file
62
parser/src/structs/config.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use std::fs::{self, File};
|
||||
|
||||
use clap::Parser;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Parser, Debug, Clone, Default, Serialize, Deserialize)]
|
||||
#[command(version, about, long_about = None)]
|
||||
pub struct Config {
|
||||
/// bitcoind `-datadir=<dir>` argument
|
||||
#[arg(long, value_name = "DIR")]
|
||||
pub datadir: Option<String>,
|
||||
|
||||
/// bitcoind `-blocksonly` argument, default: true
|
||||
#[arg(long)]
|
||||
pub blocksonly: Option<bool>,
|
||||
|
||||
/// bitcoind `-rpcconnect=<ip>` argument
|
||||
#[arg(long, value_name = "IP")]
|
||||
pub rpcconnect: Option<String>,
|
||||
|
||||
/// Delay between runs, default: 0
|
||||
#[arg(long, value_name = "SECONDS")]
|
||||
pub delay: Option<u64>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
const PATH: &'static str = "config.toml";
|
||||
|
||||
pub fn read() -> Self {
|
||||
File::open(Self::PATH).unwrap();
|
||||
|
||||
let string = fs::read_to_string(Self::PATH).unwrap();
|
||||
|
||||
let config_args = Config::parse();
|
||||
|
||||
let mut config_saved: Config = toml::from_str(&string).unwrap_or_default();
|
||||
|
||||
if let Some(datadir) = config_args.datadir {
|
||||
config_saved.datadir = Some(datadir);
|
||||
}
|
||||
|
||||
if let Some(blocksonly) = config_args.blocksonly {
|
||||
config_saved.blocksonly = Some(blocksonly);
|
||||
} else {
|
||||
config_saved.blocksonly = Some(true);
|
||||
}
|
||||
|
||||
if let Some(rpcconnect) = config_args.rpcconnect {
|
||||
config_saved.rpcconnect = Some(rpcconnect);
|
||||
}
|
||||
|
||||
if let Some(delay) = config_args.delay {
|
||||
config_saved.delay = Some(delay);
|
||||
}
|
||||
|
||||
config_saved
|
||||
}
|
||||
|
||||
pub fn write(&self) -> std::io::Result<()> {
|
||||
fs::write(Self::PATH, toml::to_string(self).unwrap())
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ mod any_map;
|
||||
mod bi_map;
|
||||
mod block_data;
|
||||
mod block_path;
|
||||
mod config;
|
||||
mod counter;
|
||||
mod date_data;
|
||||
mod date_map;
|
||||
@@ -33,6 +34,7 @@ pub use any_map::*;
|
||||
pub use bi_map::*;
|
||||
pub use block_data::*;
|
||||
pub use block_path::*;
|
||||
pub use config::*;
|
||||
pub use counter::*;
|
||||
pub use date_data::*;
|
||||
pub use date_map::*;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::process::Output;
|
||||
use std::{fs::OpenOptions, io::Write, process::Output};
|
||||
|
||||
use chrono::Local;
|
||||
use color_eyre::owo_colors::OwoColorize;
|
||||
@@ -10,6 +10,16 @@ pub fn log(str: &str) {
|
||||
str.lines()
|
||||
.filter(|line| !line.is_empty())
|
||||
.for_each(|line| {
|
||||
let mut file = OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open("./parser.log")
|
||||
.unwrap();
|
||||
|
||||
if let Err(e) = writeln!(file, "{} {}", date_time, line) {
|
||||
eprintln!("Couldn't write to file: {}", e);
|
||||
}
|
||||
|
||||
println!("{} {}", date_time.bright_black(), line);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user