From 0171b8f63ccffb08ed4c0ff0801fcbe550e96032 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Thu, 21 Jun 2018 14:06:09 +0300 Subject: [PATCH] Allow listing bitcoind blk*.dat files --- Cargo.toml | 1 + src/daemon.rs | 24 ++++++++++++++++++++++-- src/lib.rs | 1 + 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 132081c..758eabc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,3 +23,4 @@ serde_json = "1.0" stderrlog = "0.4" time = "0.1" tiny_http = "0.6" +glob = "0.2" diff --git a/src/daemon.rs b/src/daemon.rs index edb327f..8ac3e82 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -4,6 +4,7 @@ use bitcoin::blockdata::transaction::Transaction; use bitcoin::network::serialize::BitcoinHash; use bitcoin::network::serialize::{deserialize, serialize}; use bitcoin::util::hash::Sha256dHash; +use glob; use hex; use serde_json::{from_str, from_value, Value}; use std::collections::HashSet; @@ -11,6 +12,7 @@ use std::env::home_dir; use std::fs; use std::io::{BufRead, BufReader, Lines, Write}; use std::net::{SocketAddr, TcpStream}; +use std::path::PathBuf; use std::str::FromStr; use std::sync::Mutex; @@ -25,16 +27,31 @@ pub enum Network { Testnet, } -fn read_cookie(network: Network) -> Result> { - let mut path = home_dir().unwrap(); +fn data_dir(network: Network) -> Result { + let mut path = home_dir().chain_err(|| "could not find home directory")?; path.push(".bitcoin"); if let Network::Testnet = network { path.push("testnet3"); } + Ok(path) +} + +fn read_cookie(network: Network) -> Result> { + let mut path = data_dir(network)?; path.push(".cookie"); fs::read(&path).chain_err(|| format!("failed to read cookie from {:?}", path)) } +pub fn list_blk_files(network: Network) -> Result> { + let mut path = data_dir(network)?; + path.push("blocks"); + path.push("blk*.dat"); + Ok(glob::glob(path.to_str().unwrap()) + .chain_err(|| "failed to list blk*.dat files")? + .map(|res| res.unwrap()) + .collect()) +} + fn parse_hash(value: &Value) -> Result { Ok( Sha256dHash::from_hex(value.as_str().chain_err(|| "non-string value")?) @@ -169,6 +186,7 @@ impl Connection { } pub struct Daemon { + network: Network, conn: Mutex, // monitoring @@ -183,6 +201,7 @@ impl Daemon { Network::Testnet => "127.0.0.1:18332", }; let daemon = Daemon { + network, conn: Mutex::new(Connection::new( SocketAddr::from_str(addr).unwrap(), base64::encode(&read_cookie(network)?), @@ -202,6 +221,7 @@ impl Daemon { pub fn reconnect(&self) -> Result { Ok(Daemon { + network: self.network, conn: Mutex::new(self.conn.lock().unwrap().reconnect()?), latency: self.latency.clone(), size: self.size.clone(), diff --git a/src/lib.rs b/src/lib.rs index b1de176..cee920d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ extern crate bincode; extern crate bitcoin; extern crate chan_signal; extern crate crypto; +extern crate glob; extern crate hex; extern crate prometheus; extern crate rocksdb;