From 62671a26a8a2805ff8ef2c3cff34e3bca9592618 Mon Sep 17 00:00:00 2001 From: olegkubrakov Date: Thu, 5 Dec 2024 17:06:47 -0800 Subject: [PATCH] Vec Broadcaster Implementation of `BroacasterInterface` that does not broadcast transactions immediately, but stores them internally to broadcast later with a call to `release_transactions`. --- lightning/src/chain/chaininterface.rs | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lightning/src/chain/chaininterface.rs b/lightning/src/chain/chaininterface.rs index 84281df1d7b..36438c1c6fe 100644 --- a/lightning/src/chain/chaininterface.rs +++ b/lightning/src/chain/chaininterface.rs @@ -45,6 +45,45 @@ pub trait BroadcasterInterface { fn broadcast_transactions(&self, txs: &[&Transaction]); } +/// Transaction broadcaster that does not broadcast transactions, but accumulates +/// them in a Vec instead. This could be used to delay broadcasts until the system +/// is ready. +pub struct VecBroadcaster { + channel_id: ChannelId, + transactions: Mutex>, +} + +impl VecBroadcaster { + /// Create a new broadcaster for a channel + pub fn new(channel_id: ChannelId) -> Self { + Self { + channel_id, + transactions: Mutex::new(Vec::new()), + } + } + + /// Used to actually broadcast stored transactions to the network. + #[instrument(skip_all, fields(channel = %self.channel_id))] + pub fn release_transactions(&self, broadcaster: Arc) { + let transactions = self.transactions.lock(); + info!( + "Releasing transactions for channel_id={}, len={}", + self.channel_id, + transactions.len() + ); + broadcaster.broadcast_transactions(&transactions.iter().collect::>()) + } +} + +impl BroadcasterInterface for VecBroadcaster { + fn broadcast_transactions(&self, txs: &[&Transaction]) { + let mut tx_storage = self.transactions.lock(); + for tx in txs { + tx_storage.push((*tx).to_owned()) + } + } +} + /// An enum that represents the priority at which we want a transaction to confirm used for feerate /// estimation. #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]