pub struct TestBroadcaster {}
impl BroadcasterInterface for TestBroadcaster {
- fn broadcast_transaction(&self, _tx: &Transaction) { }
+ fn broadcast_transactions(&self, _txs: &[&Transaction]) { }
}
pub struct VecWriter(pub Vec<u8>);
txn_broadcasted: Mutex<Vec<Transaction>>,
}
impl BroadcasterInterface for TestBroadcaster {
- fn broadcast_transaction(&self, tx: &Transaction) {
- self.txn_broadcasted.lock().unwrap().push(tx.clone());
+ fn broadcast_transactions(&self, txs: &[&Transaction]) {
+ let owned_txs: Vec<Transaction> = txs.iter().map(|tx| (*tx).clone()).collect();
+ self.txn_broadcasted.lock().unwrap().extend(owned_txs);
}
}
/// An interface to send a transaction to the Bitcoin network.
pub trait BroadcasterInterface {
- /// Sends a transaction out to (hopefully) be mined.
- fn broadcast_transaction(&self, tx: &Transaction);
+ /// Sends a list of transactions out to (hopefully) be mined.
+ /// This only needs to handle the actual broadcasting of transactions, LDK will automatically
+ /// rebroadcast transactions that haven't made it into a block.
+ ///
+ /// In some cases LDK may attempt to broadcast a transaction which double-spends another
+ /// and this isn't a bug and can be safely ignored.
+ ///
+ /// If more than one transaction is given, these transactions should be considered to be a
+ /// package and broadcast together. Some of the transactions may or may not depend on each other,
+ /// be sure to manage both cases correctly.
+ ///
+ /// Bitcoin transaction packages are defined in BIP 331 and here:
+ /// https://github.com/bitcoin/bitcoin/blob/master/doc/policy/packages.md
+ fn broadcast_transactions(&self, txs: &[&Transaction]);
}
/// An enum that represents the speed at which we want a transaction to confirm used for feerate
where B::Target: BroadcasterInterface,
L::Target: Logger,
{
- for tx in self.get_latest_holder_commitment_txn(logger).iter() {
+ let commit_txs = self.get_latest_holder_commitment_txn(logger);
+ let mut txs = vec![];
+ for tx in commit_txs.iter() {
log_info!(logger, "Broadcasting local {}", log_tx!(tx));
- broadcaster.broadcast_transaction(tx);
+ txs.push(tx);
}
+ broadcaster.broadcast_transactions(&txs);
self.pending_monitor_events.push(MonitorEvent::CommitmentTxConfirmed(self.funding_info.0));
}
OnchainClaim::Tx(tx) => {
let log_start = if bumped_feerate { "Broadcasting RBF-bumped" } else { "Rebroadcasting" };
log_info!(logger, "{} onchain {}", log_start, log_tx!(tx));
- broadcaster.broadcast_transaction(&tx);
+ broadcaster.broadcast_transactions(&[&tx]);
},
#[cfg(anchors)]
OnchainClaim::Event(event) => {
let package_id = match claim {
OnchainClaim::Tx(tx) => {
log_info!(logger, "Broadcasting onchain {}", log_tx!(tx));
- broadcaster.broadcast_transaction(&tx);
+ broadcaster.broadcast_transactions(&[&tx]);
tx.txid().into_inner()
},
#[cfg(anchors)]
match bump_claim {
OnchainClaim::Tx(bump_tx) => {
log_info!(logger, "Broadcasting RBF-bumped onchain {}", log_tx!(bump_tx));
- broadcaster.broadcast_transaction(&bump_tx);
+ broadcaster.broadcast_transactions(&[&bump_tx]);
},
#[cfg(anchors)]
OnchainClaim::Event(claim_event) => {
match bump_claim {
OnchainClaim::Tx(bump_tx) => {
log_info!(logger, "Broadcasting onchain {}", log_tx!(bump_tx));
- broadcaster.broadcast_transaction(&bump_tx);
+ broadcaster.broadcast_transactions(&[&bump_tx]);
},
#[cfg(anchors)]
OnchainClaim::Event(claim_event) => {
if let Some(tx) = funding_broadcastable {
log_info!(self.logger, "Broadcasting funding transaction with txid {}", tx.txid());
- self.tx_broadcaster.broadcast_transaction(&tx);
+ self.tx_broadcaster.broadcast_transactions(&[&tx]);
}
{
};
if let Some(broadcast_tx) = tx {
log_info!(self.logger, "Broadcasting {}", log_tx!(broadcast_tx));
- self.tx_broadcaster.broadcast_transaction(&broadcast_tx);
+ self.tx_broadcaster.broadcast_transactions(&[&broadcast_tx]);
}
if let Some(chan) = chan_option {
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
self.issue_channel_close_events(chan, ClosureReason::CooperativeClosure);
log_info!(self.logger, "Broadcasting {}", log_tx!(tx));
- self.tx_broadcaster.broadcast_transaction(&tx);
+ self.tx_broadcaster.broadcast_transactions(&[&tx]);
update_maps_on_chan_removal!(self, chan);
false
} else { true }
}
impl chaininterface::BroadcasterInterface for TestBroadcaster {
- fn broadcast_transaction(&self, tx: &Transaction) {
- let lock_time = tx.lock_time.0;
- assert!(lock_time < 1_500_000_000);
- if bitcoin::LockTime::from(tx.lock_time).is_block_height() && lock_time > self.blocks.lock().unwrap().last().unwrap().1 {
- for inp in tx.input.iter() {
- if inp.sequence != Sequence::MAX {
- panic!("We should never broadcast a transaction before its locktime ({})!", tx.lock_time);
+ fn broadcast_transactions(&self, txs: &[&Transaction]) {
+ for tx in txs {
+ let lock_time = tx.lock_time.0;
+ assert!(lock_time < 1_500_000_000);
+ if bitcoin::LockTime::from(tx.lock_time).is_block_height() && lock_time > self.blocks.lock().unwrap().last().unwrap().1 {
+ for inp in tx.input.iter() {
+ if inp.sequence != Sequence::MAX {
+ panic!("We should never broadcast a transaction before its locktime ({})!", tx.lock_time);
+ }
}
}
}
- self.txn_broadcasted.lock().unwrap().push(tx.clone());
+ let owned_txs: Vec<Transaction> = txs.iter().map(|tx| (*tx).clone()).collect();
+ self.txn_broadcasted.lock().unwrap().extend(owned_txs);
}
}