Skip to content

Commit

Permalink
Vec Broadcaster
Browse files Browse the repository at this point in the history
Implementation of `BroacasterInterface` that does not broadcast transactions
immediately, but stores them internally to broadcast later with a call to
`release_transactions`.
  • Loading branch information
yellowred committed Dec 6, 2024
1 parent 10f9123 commit 62671a2
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lightning/src/chain/chaininterface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<Transaction>>,
}

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<dyn BroadcasterInterface>) {
let transactions = self.transactions.lock();
info!(
"Releasing transactions for channel_id={}, len={}",
self.channel_id,
transactions.len()
);
broadcaster.broadcast_transactions(&transactions.iter().collect::<Vec<&Transaction>>())
}
}

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)]
Expand Down

0 comments on commit 62671a2

Please sign in to comment.