From 67ece2dd4921b94d60eee5ae8c3bef823dd7944a Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Sat, 26 May 2018 17:14:15 +0300 Subject: [PATCH] Retrieve blockchain state before opening the DB This would make sure that bitcoind is up and running. --- src/app.rs | 1 + src/daemon.rs | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/app.rs b/src/app.rs index 36ca296..93336e5 100644 --- a/src/app.rs +++ b/src/app.rs @@ -101,6 +101,7 @@ impl App { fn run_server(config: &Config) -> Result<()> { let index = index::Index::new(); let daemon = daemon::Daemon::new(config.network_type)?; + debug!("{:?}", daemon.getblockchaininfo()?); let store = store::DBStore::open( config.db_path, diff --git a/src/daemon.rs b/src/daemon.rs index b9b6ab3..aa3acad 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -5,7 +5,7 @@ use bitcoin::network::serialize::BitcoinHash; use bitcoin::network::serialize::{deserialize, serialize}; use bitcoin::util::hash::Sha256dHash; use hex; -use serde_json::{from_str, Value}; +use serde_json::{from_str, from_value, Value}; use std::env::home_dir; use std::fs; use std::io::{BufRead, BufReader, Write}; @@ -49,6 +49,16 @@ pub struct MempoolEntry { fee_per_vbyte: f32, } +#[derive(Serialize, Deserialize, Debug)] +pub struct BlockchainInfo { + chain: String, + blocks: usize, + headers: usize, + bestblockhash: String, + size_on_disk: usize, + pruned: bool, +} + impl MempoolEntry { fn new(fee: u64, vsize: u32) -> MempoolEntry { MempoolEntry { @@ -144,6 +154,11 @@ impl Daemon { // bitcoind JSONRPC API: + pub fn getblockchaininfo(&self) -> Result { + let info: Value = self.request("getblockchaininfo", json!([]))?; + Ok(from_value(info).chain_err(|| "invalid blockchain info")?) + } + pub fn getbestblockhash(&self) -> Result { parse_hash(&self.request("getbestblockhash", json!([]))?).chain_err(|| "invalid blockhash") }