Skip to content

Commit

Permalink
Switch to submitpackage for broadcasting transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlueMatt committed Oct 14, 2024
1 parent d65b8a2 commit efba2f8
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions src/bitcoind_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,32 +314,29 @@ impl FeeEstimator for BitcoindClient {

impl BroadcasterInterface for BitcoindClient {
fn broadcast_transactions(&self, txs: &[&Transaction]) {
// TODO: Rather than calling `sendrawtransaction` in a a loop, we should probably use
// `submitpackage` once it becomes available.
for tx in txs {
let bitcoind_rpc_client = Arc::clone(&self.bitcoind_rpc_client);
let tx_serialized = encode::serialize_hex(tx);
let tx_json = serde_json::json!(tx_serialized);
let logger = Arc::clone(&self.logger);
self.handle.spawn(async move {
// This may error due to RL calling `broadcast_transactions` with the same transaction
// multiple times, but the error is safe to ignore.
match bitcoind_rpc_client
.call_method::<Txid>("sendrawtransaction", &vec![tx_json])
.await
{
Ok(_) => {}
Err(e) => {
let err_str = e.get_ref().unwrap().to_string();
log_error!(logger,
"Warning, failed to broadcast a transaction, this is likely okay but may indicate an error: {}\nTransaction: {}",
err_str,
tx_serialized);
print!("Warning, failed to broadcast a transaction, this is likely okay but may indicate an error: {}\n> ", err_str);
}
// We assume `submitpackage` is available (Bitcoin Core 25, at least).
let txn = txs.iter().map(|tx| encode::serialize_hex(tx)).collect::<Vec<_>>();
let bitcoind_rpc_client = Arc::clone(&self.bitcoind_rpc_client);
let tx_json = serde_json::json!(txn);
let logger = Arc::clone(&self.logger);
self.handle.spawn(async move {
// This may error due to RL calling `broadcast_transactions` with the same transaction
// multiple times, but the error is safe to ignore.
match bitcoind_rpc_client
.call_method::<Txid>("submitpackage", &vec![tx_json])
.await
{
Ok(_) => {}
Err(e) => {
let err_str = e.get_ref().unwrap().to_string();
log_error!(logger,
"Warning, failed to broadcast a transaction, this is likely okay but may indicate an error: {}\nTransactions: {:?}",
err_str,
txn);
print!("Warning, failed to broadcast a transaction, this is likely okay but may indicate an error: {}\n> ", err_str);
}
});
}
}
});
}
}

Expand Down

0 comments on commit efba2f8

Please sign in to comment.