diff --git a/lightning/src/chain/chaininterface.rs b/lightning/src/chain/chaininterface.rs index 84281df1d7b..d4849431ffb 100644 --- a/lightning/src/chain/chaininterface.rs +++ b/lightning/src/chain/chaininterface.rs @@ -15,7 +15,7 @@ use core::{cmp, ops::Deref}; -use crate::prelude::*; +use crate::{ln::types::ChannelId, prelude::*, sync::{Arc, Mutex}, util::logger::Logger}; use bitcoin::transaction::Transaction; @@ -45,6 +45,40 @@ 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. + pub fn release_transactions(&self, broadcaster: Arc, logger: &L) where L::Target: Logger { + let transactions = self.transactions.lock().unwrap(); + log_info!(logger, "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().unwrap(); + 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)]