Skip to content

Commit

Permalink
Wait for bitcoind to warm up
Browse files Browse the repository at this point in the history
  • Loading branch information
shesek committed Jan 6, 2021
1 parent 9677082 commit dec6d46
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 31 deletions.
16 changes: 10 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ impl App {
let indexer = Arc::new(RwLock::new(Indexer::new(rpc.clone(), watcher)));
let query = Arc::new(Query::new((&config).into(), rpc.clone(), indexer.clone()));

if let Some(bitcoind_wallet) = &config.bitcoind_wallet {
load_wallet(&rpc, bitcoind_wallet)?;
}

wait_bitcoind(&rpc, progress_tx.clone())?;
init_bitcoind(&rpc, &config, progress_tx.clone())?;

if config.startup_banner {
println!("{}", banner::get_welcome_banner(&query, false)?);
Expand Down Expand Up @@ -227,10 +223,18 @@ fn load_wallet(rpc: &RpcClient, name: &str) -> Result<()> {
}

// wait for bitcoind to sync and finish rescanning
fn wait_bitcoind(rpc: &RpcClient, progress_tx: Option<mpsc::Sender<Progress>>) -> Result<()> {
fn init_bitcoind(
rpc: &RpcClient,
config: &Config,
progress_tx: Option<mpsc::Sender<Progress>>,
) -> Result<()> {
const INTERVAL: time::Duration = time::Duration::from_secs(7);

let bcinfo = rpc.wait_blockchain_sync(progress_tx.clone(), INTERVAL)?;

if let Some(bitcoind_wallet) = &config.bitcoind_wallet {
load_wallet(&rpc, bitcoind_wallet)?;
}
let walletinfo = rpc.wait_wallet_scan(progress_tx, None, INTERVAL)?;

let netinfo = rpc.get_network_info()?;
Expand Down
58 changes: 33 additions & 25 deletions src/util/bitcoincore_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,42 @@ pub trait RpcApiExt: RpcApi {
interval: time::Duration,
) -> RpcResult<json::GetBlockchainInfoResult> {
Ok(loop {
let info = self.get_blockchain_info()?;
match self.get_blockchain_info() {
Ok(info) => {
if info.blocks == info.headers
&& (!info.initial_block_download || info.chain == "regtest")
{
if let Some(ref progress_tx) = progress_tx {
let progress = Progress::Sync {
progress_n: 1.0,
tip: info.median_time,
};
progress_tx.send(progress).ok();
}
break info;
}

if info.blocks == info.headers
&& (!info.initial_block_download || info.chain == "regtest")
{
if let Some(ref progress_tx) = progress_tx {
let progress = Progress::Sync {
progress_n: 1.0,
tip: info.median_time,
};
progress_tx.send(progress).ok();
if let Some(ref progress_tx) = progress_tx {
let progress = Progress::Sync {
progress_n: info.verification_progress as f32,
tip: info.median_time,
};
if progress_tx.send(progress).is_err() {
break info;
}
} else {
info!(target: "bwt",
"waiting for bitcoind to sync [{}/{} blocks, progress={:.1}%]",
info.blocks, info.headers, info.verification_progress * 100.0
);
}
}
break info;
}

if let Some(ref progress_tx) = progress_tx {
let progress = Progress::Sync {
progress_n: info.verification_progress as f32,
tip: info.median_time,
};
if progress_tx.send(progress).is_err() {
break info;
Err(rpc::Error::JsonRpc(rpc::jsonrpc::Error::Rpc(ref e)))
if e.code == RPC_IN_WARMUP =>
{
info!("waiting for bitcoind to warm up: {}", e.message);
}
} else {
info!(target: "bwt",
"waiting for bitcoind to sync [{}/{} blocks, progress={:.1}%]",
info.blocks, info.headers, info.verification_progress * 100.0
);
Err(e) => return Err(e),
}
thread::sleep(interval);
})
Expand Down

0 comments on commit dec6d46

Please sign in to comment.