Skip to content

Commit cc85b4d

Browse files
committed
Use helper functions to log broadcast errors
These will be useful when we add support for broadcasting packages in an upcoming commit.
1 parent a9040a9 commit cc85b4d

3 files changed

Lines changed: 82 additions & 101 deletions

File tree

src/chain/bitcoind.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -596,45 +596,35 @@ impl BitcoindChainSource {
596596
Ok(())
597597
}
598598

599+
fn log_broadcast_error(&self, e: impl core::fmt::Display, txids: &[Txid], txs: &[Transaction]) {
600+
log_error!(self.logger, "Failed to broadcast transaction(s) {:?}: {}", txids, e);
601+
log_trace!(self.logger, "Failed broadcast transaction bytes:");
602+
for tx in txs.iter() {
603+
log_trace!(self.logger, "{}", log_bytes!(tx.encode()));
604+
}
605+
}
606+
599607
pub(crate) async fn process_transaction_broadcast(&self, txs: SortedTransactions) {
600608
// While it's a bit unclear when we'd be able to lean on Bitcoin Core >v28
601609
// features, we should eventually switch to use `submitpackage` via the
602610
// `rust-bitcoind-json-rpc` crate rather than just broadcasting individual
603611
// transactions.
604-
for tx in txs.iter() {
612+
let txs = txs.into_inner();
613+
for tx in txs {
605614
let txid = tx.compute_txid();
606615
let timeout_fut = tokio::time::timeout(
607616
Duration::from_secs(DEFAULT_TX_BROADCAST_TIMEOUT_SECS),
608-
self.api_client.broadcast_transaction(tx),
617+
self.api_client.broadcast_transaction(&tx),
609618
);
610619
match timeout_fut.await {
611620
Ok(res) => match res {
612621
Ok(id) => {
613622
debug_assert_eq!(id, txid);
614623
log_trace!(self.logger, "Successfully broadcast transaction {}", txid);
615624
},
616-
Err(e) => {
617-
log_error!(self.logger, "Failed to broadcast transaction {}: {}", txid, e);
618-
log_trace!(
619-
self.logger,
620-
"Failed broadcast transaction bytes: {}",
621-
log_bytes!(tx.encode())
622-
);
623-
},
624-
},
625-
Err(e) => {
626-
log_error!(
627-
self.logger,
628-
"Failed to broadcast transaction due to timeout {}: {}",
629-
txid,
630-
e
631-
);
632-
log_trace!(
633-
self.logger,
634-
"Failed broadcast transaction bytes: {}",
635-
log_bytes!(tx.encode())
636-
);
625+
Err(e) => self.log_broadcast_error(e, &[txid], &[tx]),
637626
},
627+
Err(e) => self.log_broadcast_error(e, &[txid], &[tx]),
638628
}
639629
}
640630
}

src/chain/electrum.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -625,46 +625,40 @@ impl ElectrumRuntimeClient {
625625
})
626626
}
627627

628+
fn log_broadcast_error(&self, e: impl core::fmt::Display, txids: &[Txid], txs: &[Transaction]) {
629+
log_error!(self.logger, "Failed to broadcast transaction(s) {:?}: {}", txids, e);
630+
log_trace!(self.logger, "Failed broadcast transaction bytes:");
631+
for tx in txs {
632+
log_trace!(self.logger, "{}", log_bytes!(tx.encode()));
633+
}
634+
}
635+
628636
async fn broadcast(&self, tx: Transaction) {
629637
let electrum_client = Arc::clone(&self.electrum_client);
630638

631639
let txid = tx.compute_txid();
632-
let tx_bytes = tx.encode();
640+
let tx = Arc::new(tx);
633641

634-
let spawn_fut =
635-
self.runtime.spawn_blocking(move || electrum_client.transaction_broadcast(&tx));
642+
let spawn_fut = self.runtime.spawn_blocking({
643+
let tx = Arc::clone(&tx);
644+
move || electrum_client.transaction_broadcast(tx.as_ref())
645+
});
636646
let timeout_fut = tokio::time::timeout(
637647
Duration::from_secs(self.sync_config.timeouts_config.tx_broadcast_timeout_secs),
638648
spawn_fut,
639649
);
640650

641651
match timeout_fut.await {
642652
Ok(res) => match res {
643-
Ok(_) => {
653+
Ok(Ok(txid)) => {
644654
log_trace!(self.logger, "Successfully broadcast transaction {}", txid);
645655
},
646-
Err(e) => {
647-
log_error!(self.logger, "Failed to broadcast transaction {}: {}", txid, e);
648-
log_trace!(
649-
self.logger,
650-
"Failed broadcast transaction bytes: {}",
651-
log_bytes!(tx_bytes)
652-
);
656+
Ok(Err(e)) => {
657+
self.log_broadcast_error(e, &[txid], core::slice::from_ref(tx.as_ref()))
653658
},
659+
Err(e) => self.log_broadcast_error(e, &[txid], core::slice::from_ref(tx.as_ref())),
654660
},
655-
Err(e) => {
656-
log_error!(
657-
self.logger,
658-
"Failed to broadcast transaction due to timeout {}: {}",
659-
txid,
660-
e
661-
);
662-
log_trace!(
663-
self.logger,
664-
"Failed broadcast transaction bytes: {}",
665-
log_bytes!(tx_bytes)
666-
);
667-
},
661+
Err(e) => self.log_broadcast_error(e, &[txid], core::slice::from_ref(tx.as_ref())),
668662
}
669663
}
670664

src/chain/esplora.rs

Lines changed: 51 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,55 @@ impl EsploraChainSource {
411411
Ok(())
412412
}
413413

414+
fn log_http_error(&self, e: esplora_client::Error, txids: &[Txid], txs: &SortedTransactions) {
415+
match e {
416+
esplora_client::Error::HttpResponse { status, message } => {
417+
if status == 400 && txs.len() == 1 {
418+
// Log 400 at lesser level, as this often just means bitcoind already knows the
419+
// transaction.
420+
// FIXME: We can further differentiate here based on the error
421+
// message which will be available with rust-esplora-client 0.7 and
422+
// later.
423+
log_trace!(
424+
self.logger,
425+
"Failed to broadcast due to HTTP connection error: {}",
426+
message
427+
);
428+
log_trace!(self.logger, "Failed to broadcast transaction(s) {:?}", txids);
429+
} else {
430+
log_error!(
431+
self.logger,
432+
"Failed to broadcast due to HTTP connection error: {} - {}",
433+
status,
434+
message
435+
);
436+
log_error!(self.logger, "Failed to broadcast transaction(s) {:?}", txids);
437+
}
438+
log_trace!(self.logger, "Failed broadcast transaction(s) bytes:");
439+
for tx in txs.iter() {
440+
log_trace!(self.logger, "{}", log_bytes!(tx.encode()));
441+
}
442+
},
443+
_ => {
444+
log_error!(self.logger, "Failed to broadcast transaction(s) {:?}: {}", txids, e);
445+
log_trace!(self.logger, "Failed broadcast transaction(s) bytes:");
446+
for tx in txs.iter() {
447+
log_trace!(self.logger, "{}", log_bytes!(tx.encode()));
448+
}
449+
},
450+
}
451+
}
452+
453+
fn log_broadcast_error(
454+
&self, e: impl core::fmt::Display, txids: &[Txid], txs: &SortedTransactions,
455+
) {
456+
log_error!(self.logger, "Failed to broadcast transaction(s) {:?}: {}", txids, e);
457+
log_trace!(self.logger, "Failed broadcast transaction bytes:");
458+
for tx in txs.iter() {
459+
log_trace!(self.logger, "{}", log_bytes!(tx.encode()));
460+
}
461+
}
462+
414463
pub(crate) async fn process_transaction_broadcast(&self, txs: SortedTransactions) {
415464
for tx in txs.iter() {
416465
let txid = tx.compute_txid();
@@ -423,61 +472,9 @@ impl EsploraChainSource {
423472
Ok(()) => {
424473
log_trace!(self.logger, "Successfully broadcast transaction {}", txid);
425474
},
426-
Err(e) => match e {
427-
esplora_client::Error::HttpResponse { status, message } => {
428-
if status == 400 {
429-
// Log 400 at lesser level, as this often just means bitcoind already knows the
430-
// transaction.
431-
// FIXME: We can further differentiate here based on the error
432-
// message which will be available with rust-esplora-client 0.7 and
433-
// later.
434-
log_trace!(
435-
self.logger,
436-
"Failed to broadcast due to HTTP connection error: {}",
437-
message
438-
);
439-
} else {
440-
log_error!(
441-
self.logger,
442-
"Failed to broadcast due to HTTP connection error: {} - {}",
443-
status,
444-
message
445-
);
446-
}
447-
log_trace!(
448-
self.logger,
449-
"Failed broadcast transaction bytes: {}",
450-
log_bytes!(tx.encode())
451-
);
452-
},
453-
_ => {
454-
log_error!(
455-
self.logger,
456-
"Failed to broadcast transaction {}: {}",
457-
txid,
458-
e
459-
);
460-
log_trace!(
461-
self.logger,
462-
"Failed broadcast transaction bytes: {}",
463-
log_bytes!(tx.encode())
464-
);
465-
},
466-
},
467-
},
468-
Err(e) => {
469-
log_error!(
470-
self.logger,
471-
"Failed to broadcast transaction due to timeout {}: {}",
472-
txid,
473-
e
474-
);
475-
log_trace!(
476-
self.logger,
477-
"Failed broadcast transaction bytes: {}",
478-
log_bytes!(tx.encode())
479-
);
475+
Err(e) => self.log_http_error(e, &[txid], &txs),
480476
},
477+
Err(e) => self.log_broadcast_error(e, &[txid], &txs),
481478
}
482479
}
483480
}

0 commit comments

Comments
 (0)