Retrieve blockchain state before opening the DB

This would make sure that bitcoind is up and running.
This commit is contained in:
Roman Zeyde 2018-05-26 17:14:15 +03:00
parent 50941e7bea
commit 67ece2dd49
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
2 changed files with 17 additions and 1 deletions

View File

@ -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,

View File

@ -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<BlockchainInfo> {
let info: Value = self.request("getblockchaininfo", json!([]))?;
Ok(from_value(info).chain_err(|| "invalid blockchain info")?)
}
pub fn getbestblockhash(&self) -> Result<Sha256dHash> {
parse_hash(&self.request("getbestblockhash", json!([]))?).chain_err(|| "invalid blockhash")
}