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 24, 2024
1 parent 10f9123 commit 8f3f61e
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion lightning/src/chain/chaininterface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<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.
pub fn release_transactions<L: Deref>(&self, broadcaster: Arc<dyn BroadcasterInterface>, 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::<Vec<&Transaction>>())
}
}

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

0 comments on commit 8f3f61e

Please sign in to comment.