Skip to content

Commit 62671a2

Browse files
committed
Vec Broadcaster
Implementation of `BroacasterInterface` that does not broadcast transactions immediately, but stores them internally to broadcast later with a call to `release_transactions`.
1 parent 10f9123 commit 62671a2

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

lightning/src/chain/chaininterface.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,45 @@ pub trait BroadcasterInterface {
4545
fn broadcast_transactions(&self, txs: &[&Transaction]);
4646
}
4747

48+
/// Transaction broadcaster that does not broadcast transactions, but accumulates
49+
/// them in a Vec instead. This could be used to delay broadcasts until the system
50+
/// is ready.
51+
pub struct VecBroadcaster {
52+
channel_id: ChannelId,
53+
transactions: Mutex<Vec<Transaction>>,
54+
}
55+
56+
impl VecBroadcaster {
57+
/// Create a new broadcaster for a channel
58+
pub fn new(channel_id: ChannelId) -> Self {
59+
Self {
60+
channel_id,
61+
transactions: Mutex::new(Vec::new()),
62+
}
63+
}
64+
65+
/// Used to actually broadcast stored transactions to the network.
66+
#[instrument(skip_all, fields(channel = %self.channel_id))]
67+
pub fn release_transactions(&self, broadcaster: Arc<dyn BroadcasterInterface>) {
68+
let transactions = self.transactions.lock();
69+
info!(
70+
"Releasing transactions for channel_id={}, len={}",
71+
self.channel_id,
72+
transactions.len()
73+
);
74+
broadcaster.broadcast_transactions(&transactions.iter().collect::<Vec<&Transaction>>())
75+
}
76+
}
77+
78+
impl BroadcasterInterface for VecBroadcaster {
79+
fn broadcast_transactions(&self, txs: &[&Transaction]) {
80+
let mut tx_storage = self.transactions.lock();
81+
for tx in txs {
82+
tx_storage.push((*tx).to_owned())
83+
}
84+
}
85+
}
86+
4887
/// An enum that represents the priority at which we want a transaction to confirm used for feerate
4988
/// estimation.
5089
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]

0 commit comments

Comments
 (0)