Skip to content

Commit 420b25d

Browse files
committed
Submit packages via esplora
1 parent 38b6264 commit 420b25d

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,6 @@ check-cfg = [
148148
"cfg(cln_test)",
149149
"cfg(lnd_test)",
150150
]
151+
152+
[patch.crates-io]
153+
esplora-client = { git = 'https://github.com/acidbunny21/rust-esplora-client.git', branch = 'submit-tx-pkg-clients' }

src/chain/esplora.rs

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,17 @@ impl EsploraChainSource {
365365
}
366366

367367
pub(crate) async fn process_broadcast_package(&self, package: Vec<Transaction>) {
368-
for tx in &package {
368+
if package.len() == 1 {
369+
let tx = &package[0];
369370
let txid = tx.compute_txid();
370371
let timeout_fut = tokio::time::timeout(
371372
Duration::from_secs(TX_BROADCAST_TIMEOUT_SECS),
372373
self.esplora_client.broadcast(tx),
373374
);
374375
match timeout_fut.await {
375376
Ok(res) => match res {
376-
Ok(()) => {
377+
Ok(id) => {
378+
debug_assert_eq!(id, txid);
377379
log_trace!(self.logger, "Successfully broadcast transaction {}", txid);
378380
},
379381
Err(e) => match e {
@@ -432,6 +434,78 @@ impl EsploraChainSource {
432434
);
433435
},
434436
}
437+
} else if package.len() > 1 {
438+
let txids: Vec<_> = package.iter().map(|tx| tx.compute_txid()).collect();
439+
let timeout_fut = tokio::time::timeout(
440+
Duration::from_secs(TX_BROADCAST_TIMEOUT_SECS),
441+
self.esplora_client.submit_package(&package, None, None),
442+
);
443+
match timeout_fut.await {
444+
Ok(res) => match res {
445+
Ok(result) => {
446+
// TODO: sometimes, we get 0 ids back...
447+
let _ids: Vec<_> =
448+
result.tx_results.values().map(|value| value.txid).collect();
449+
log_trace!(
450+
self.logger,
451+
"Package broadcast message {}, txids: {:?}",
452+
result.package_msg,
453+
txids,
454+
);
455+
},
456+
Err(e) => match e {
457+
esplora_client::Error::HttpResponse { status, message } => {
458+
if status == 400 {
459+
// Log 400 at lesser level, as this often just means bitcoind already knows the
460+
// transaction.
461+
// FIXME: We can further differentiate here based on the error
462+
// message which will be available with rust-esplora-client 0.7 and
463+
// later.
464+
log_trace!(
465+
self.logger,
466+
"Failed to broadcast due to HTTP connection error: {}",
467+
message
468+
);
469+
} else {
470+
log_error!(
471+
self.logger,
472+
"Failed to broadcast due to HTTP connection error: {} - {}",
473+
status,
474+
message
475+
);
476+
}
477+
log_trace!(self.logger, "Failed broadcast package bytes:");
478+
for tx in package {
479+
log_trace!(self.logger, "{}", log_bytes!(tx.encode()));
480+
}
481+
},
482+
_ => {
483+
log_error!(
484+
self.logger,
485+
"Failed to broadcast package {:?}: {}",
486+
txids,
487+
e
488+
);
489+
log_trace!(self.logger, "Failed broadcast package bytes:");
490+
for tx in package {
491+
log_trace!(self.logger, "{}", log_bytes!(tx.encode()));
492+
}
493+
},
494+
},
495+
},
496+
Err(e) => {
497+
log_error!(
498+
self.logger,
499+
"Failed to broadcast package due to timeout {:?}: {}",
500+
txids,
501+
e
502+
);
503+
log_trace!(self.logger, "Failed broadcast transaction bytes:");
504+
for tx in package {
505+
log_trace!(self.logger, "{}", log_bytes!(tx.encode()));
506+
}
507+
},
508+
}
435509
}
436510
}
437511
}

0 commit comments

Comments
 (0)