commit 3b39a252ed1c44a4f3586e045699095b4c13e002
parent 8928134bd4c424fce4a2679e0a63b8fcd3a8b1be
Author: Antoine A <>
Date: Wed, 23 Feb 2022 12:11:24 +0100
Fix broken pipe when waiting for new block on real bitcoin blockchain
Diffstat:
4 files changed, 34 insertions(+), 29 deletions(-)
diff --git a/btc-wire/src/bin/btc-wire-utils.rs b/btc-wire/src/bin/btc-wire-utils.rs
@@ -113,7 +113,7 @@ fn main() {
}
_ => {
// Wait for next network block
- rpc.wait_for_new_block(0).ok();
+ rpc.wait_for_new_block().ok();
}
}
}
diff --git a/btc-wire/src/loops/watcher.rs b/btc-wire/src/loops/watcher.rs
@@ -25,7 +25,7 @@ pub fn watcher(mut rpc: AutoRpcCommon, mut db: AutoReconnectDb) {
let db = db.client();
let result: LoopResult<()> = (|| loop {
db.execute("NOTIFY new_block", &[])?;
- rpc.wait_for_new_block(0)?;
+ rpc.wait_for_new_block()?;
})();
if let Err(e) = result {
error!("watcher: {}", e);
diff --git a/btc-wire/src/rpc.rs b/btc-wire/src/rpc.rs
@@ -373,8 +373,14 @@ impl Rpc {
/* ----- Watcher ----- */
/// Block until a new block is mined
- pub fn wait_for_new_block(&mut self, timeout: u64) -> Result<Nothing> {
- self.call("waitfornewblock", &[timeout])
+ pub fn wait_for_new_block(&mut self) -> Result<()> {
+ let init_height = self.get_blockchain_info()?.blocks;
+ loop {
+ let info: BlockInfo = self.call("waitfornewblock", &[10])?;
+ if info.blocks != init_height {
+ return Ok(());
+ }
+ }
}
/// List new and removed transaction since a block
@@ -399,6 +405,11 @@ pub struct BlockchainInfo {
pub best_block_hash: BlockHash,
}
+#[derive(Clone, Debug, serde::Deserialize)]
+struct BlockInfo {
+ pub blocks: u64,
+}
+
#[derive(Debug, serde::Deserialize)]
pub struct BumpResult {
pub txid: Txid,
diff --git a/instrumentation/src/main.rs b/instrumentation/src/main.rs
@@ -16,7 +16,7 @@
use std::{fmt::Display, io::Write, path::PathBuf};
-use bitcoin::{Amount, BlockHash, Network, SignedAmount};
+use bitcoin::{Amount, Network, SignedAmount};
use btc_wire::{
config::BitcoinConfig,
config_bounce_fee,
@@ -46,24 +46,6 @@ fn print_now(disp: impl Display) {
std::io::stdout().flush().unwrap();
}
-fn wait_while_pending(rpc: &mut Rpc, wallet: &str, since: &mut BlockHash) {
- print_now(format_args!("Skip pending {}:", wallet));
- let mut pending = true;
- while pending {
- pending = false;
- rpc.wait_for_new_block(0).unwrap();
- print_now(".");
- let sync = rpc.list_since_block(Some(&since), 1).unwrap();
- for tx in sync.transactions {
- if tx.confirmations == 0 {
- pending = true;
- }
- }
- *since = sync.lastblock;
- }
- println!("");
-}
-
/// Depolymerizer instrumentation test
#[derive(clap::Parser, Debug)]
struct Args {
@@ -118,7 +100,7 @@ pub fn main() {
println!("Client need a minimum of {} BTC to run this test, send coins to this address: {}", min_fund.as_btc(), client_addr);
print_now("Waiting for fund:");
while client_rpc.get_balance().unwrap() < min_fund {
- client_rpc.wait_for_new_block(0).unwrap();
+ client_rpc.wait_for_new_block().unwrap();
print_now(".");
}
}
@@ -138,9 +120,6 @@ pub fn main() {
};
let mut wire_rpc = Rpc::wallet(&btc_config, WIRE).unwrap();
let wire_addr = wire_rpc.gen_addr().unwrap();
- // Skip pending
- wait_while_pending(&mut client_rpc, CLIENT, &mut since);
- wait_while_pending(&mut wire_rpc, WIRE, &mut since);
// Load balances
let client_balance = client_rpc.get_balance().unwrap();
let wire_balance = wire_rpc.get_balance().unwrap();
@@ -185,7 +164,7 @@ pub fn main() {
let mut bounced_id = None;
while pending || bounced_id.is_none() {
pending = false;
- rpc.wait_for_new_block(0).unwrap();
+ rpc.wait_for_new_block().unwrap();
print_now(".");
let sync = client_rpc.list_since_block(Some(&since), 1).unwrap();
for tx in sync.transactions {
@@ -257,7 +236,22 @@ pub fn main() {
credit_account: btc_payto_url(&client_addr),
})
.unwrap();
- wait_while_pending(&mut wire_rpc, WIRE, &mut since);
+
+ print_now("Wait for mining:");
+ let mut pending = true;
+ while pending {
+ pending = false;
+ rpc.wait_for_new_block().unwrap();
+ print_now(".");
+ let sync = rpc.list_since_block(Some(&since), 1).unwrap();
+ for tx in sync.transactions {
+ if tx.confirmations == 0 {
+ pending = true;
+ }
+ }
+ since = sync.lastblock;
+ }
+ println!("");
// Check balances change
let last_client_balance = client_rpc.get_balance().unwrap();