From f90dd1fa53c7de358d621983812d2478af8296ef Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Wed, 20 Oct 2021 16:50:59 +1100 Subject: [PATCH] Convert tracing::error into anyhow::Error context This prevents us from reporting an error when the transaction was actually on-chain. --- daemon/src/wallet.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/daemon/src/wallet.rs b/daemon/src/wallet.rs index e658a21..2691411 100644 --- a/daemon/src/wallet.rs +++ b/daemon/src/wallet.rs @@ -96,18 +96,10 @@ impl Wallet { pub async fn try_broadcast_transaction(&self, tx: Transaction) -> Result { let wallet = self.wallet.lock().await; - // TODO: Optimize this match to be a map_err / more readable in general let txid = tx.txid(); let result = wallet.broadcast(tx.clone()); - if result.is_err() { - tracing::error!( - "Broadcasting transaction failed. Raw transaction: {}", - serialize_hex(&tx) - ); - } - if let Err(&bdk::Error::Electrum(electrum_client::Error::Protocol(ref value))) = result.as_ref() { @@ -116,11 +108,21 @@ impl Wallet { })?; if error_code == i64::from(RpcErrorCode::RpcVerifyAlreadyInChain) { + tracing::trace!( + %txid, "Attempted to broadcast transaction that was already on-chain", + ); + return Ok(txid); } } - let txid = result?; + let txid = result.with_context(|| { + format!( + "Broadcasting transaction failed. Txid: {}. Raw transaction: {}", + txid, + serialize_hex(&tx) + ) + })?; Ok(txid) }