Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vec Broadcaster #3448

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we'll still want to broadcast the individual packages, this might need to be a Vec<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))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we require the instrument statment here?

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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut tx_storage = self.transactions.lock();
let mut tx_storage = self.transactions.lock().unwrap();

for tx in txs {
tx_storage.push((*tx).to_owned())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might want to see if we can dedup the packages before pushing, i.e., possibly only keep the latest version of a transaction, for example if we'd bump over and over again.

}
}
}

/// 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
Loading