forked from getAlby/ldk-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx_broadcaster.rs
More file actions
53 lines (45 loc) · 1.59 KB
/
tx_broadcaster.rs
File metadata and controls
53 lines (45 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// This file is Copyright its original authors, visible in version control history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
// accordance with one or both of these licenses.
use std::ops::Deref;
use bitcoin::Transaction;
use lightning::chain::chaininterface::BroadcasterInterface;
use tokio::sync::{mpsc, Mutex, MutexGuard};
use crate::logger::{log_error, LdkLogger};
const BCAST_PACKAGE_QUEUE_SIZE: usize = 50;
pub(crate) struct TransactionBroadcaster<L: Deref>
where
L::Target: LdkLogger,
{
queue_sender: mpsc::Sender<Vec<Transaction>>,
queue_receiver: Mutex<mpsc::Receiver<Vec<Transaction>>>,
logger: L,
}
impl<L: Deref> TransactionBroadcaster<L>
where
L::Target: LdkLogger,
{
pub(crate) fn new(logger: L) -> Self {
let (queue_sender, queue_receiver) = mpsc::channel(BCAST_PACKAGE_QUEUE_SIZE);
Self { queue_sender, queue_receiver: Mutex::new(queue_receiver), logger }
}
pub(crate) async fn get_broadcast_queue(
&self,
) -> MutexGuard<'_, mpsc::Receiver<Vec<Transaction>>> {
self.queue_receiver.lock().await
}
}
impl<L: Deref> BroadcasterInterface for TransactionBroadcaster<L>
where
L::Target: LdkLogger,
{
fn broadcast_transactions(&self, txs: &[&Transaction]) {
let package = txs.iter().map(|&t| t.clone()).collect::<Vec<Transaction>>();
self.queue_sender.try_send(package).unwrap_or_else(|e| {
log_error!(self.logger, "Failed to broadcast transactions: {}", e);
});
}
}