Skip to content

Commit d4ad57b

Browse files
authored
Merge pull request #622 from valentinewallace/chanmgr-logger-arc-to-deref
ChannelManager+Router++ Logger Arc --> Deref
2 parents 59fe300 + 222f0cb commit d4ad57b

20 files changed

+741
-725
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use bitcoin::hash_types::{BlockHash, WPubkeyHash};
2222

2323
use lightning::chain::chaininterface;
2424
use lightning::chain::transaction::OutPoint;
25-
use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil};
25+
use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil,ChainWatchInterface};
2626
use lightning::chain::keysinterface::{KeysInterface, InMemoryChannelKeys};
2727
use lightning::ln::channelmonitor;
2828
use lightning::ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, HTLCUpdate};
@@ -75,7 +75,7 @@ impl Writer for VecWriter {
7575

7676
struct TestChannelMonitor {
7777
pub logger: Arc<dyn Logger>,
78-
pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>>>,
78+
pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<dyn ChainWatchInterface>>>,
7979
pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
8080
// If we reload a node with an old copy of ChannelMonitors, the ChannelManager deserialization
8181
// logic will automatically force-close our channels for us (as we don't have an up-to-date
@@ -115,8 +115,8 @@ impl channelmonitor::ManyChannelMonitor<EnforcingChannelKeys> for TestChannelMon
115115
hash_map::Entry::Vacant(_) => panic!("Didn't have monitor on update call"),
116116
};
117117
let mut deserialized_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::
118-
read(&mut Cursor::new(&map_entry.get().1), Arc::clone(&self.logger)).unwrap().1;
119-
deserialized_monitor.update_monitor(update.clone(), &&TestBroadcaster {}).unwrap();
118+
read(&mut Cursor::new(&map_entry.get().1)).unwrap().1;
119+
deserialized_monitor.update_monitor(update.clone(), &&TestBroadcaster {}, &self.logger).unwrap();
120120
let mut ser = VecWriter(Vec::new());
121121
deserialized_monitor.write_for_disk(&mut ser).unwrap();
122122
map_entry.insert((update.update_id, ser.0));
@@ -187,7 +187,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
187187
macro_rules! make_node {
188188
($node_id: expr) => { {
189189
let logger: Arc<dyn Logger> = Arc::new(test_logger::TestLogger::new($node_id.to_string(), out.clone()));
190-
let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger)));
190+
let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin));
191191
let monitor = Arc::new(TestChannelMonitor::new(watch.clone(), broadcast.clone(), logger.clone(), fee_est.clone()));
192192

193193
let keys_manager = Arc::new(KeyProvider { node_id: $node_id, session_id: atomic::AtomicU8::new(0), channel_id: atomic::AtomicU8::new(0) });
@@ -203,7 +203,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
203203
macro_rules! reload_node {
204204
($ser: expr, $node_id: expr, $old_monitors: expr) => { {
205205
let logger: Arc<dyn Logger> = Arc::new(test_logger::TestLogger::new($node_id.to_string(), out.clone()));
206-
let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger)));
206+
let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin));
207207
let monitor = Arc::new(TestChannelMonitor::new(watch.clone(), broadcast.clone(), logger.clone(), fee_est.clone()));
208208

209209
let keys_manager = Arc::new(KeyProvider { node_id: $node_id, session_id: atomic::AtomicU8::new(0), channel_id: atomic::AtomicU8::new(0) });
@@ -215,7 +215,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
215215
let mut monitors = HashMap::new();
216216
let mut old_monitors = $old_monitors.latest_monitors.lock().unwrap();
217217
for (outpoint, (update_id, monitor_ser)) in old_monitors.drain() {
218-
monitors.insert(outpoint, <(BlockHash, ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&monitor_ser), Arc::clone(&logger)).expect("Failed to read monitor").1);
218+
monitors.insert(outpoint, <(BlockHash, ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&monitor_ser)).expect("Failed to read monitor").1);
219219
monitor.latest_monitors.lock().unwrap().insert(outpoint, (update_id, monitor_ser));
220220
}
221221
let mut monitor_refs = HashMap::new();
@@ -233,7 +233,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
233233
channel_monitors: &mut monitor_refs,
234234
};
235235

236-
(<(BlockHash, ChannelManager<EnforcingChannelKeys, Arc<TestChannelMonitor>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>>)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor)
236+
(<(BlockHash, ChannelManager<EnforcingChannelKeys, Arc<TestChannelMonitor>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>, Arc<dyn Logger>>)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor)
237237
} }
238238
}
239239

fuzz/src/chanmon_deser.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ use bitcoin::hash_types::BlockHash;
55

66
use lightning::util::enforcing_trait_impls::EnforcingChannelKeys;
77
use lightning::ln::channelmonitor;
8-
use lightning::util::ser::{ReadableArgs, Writer};
8+
use lightning::util::ser::{Readable, Writer};
99

1010
use utils::test_logger;
1111

1212
use std::io::Cursor;
13-
use std::sync::Arc;
1413

1514
struct VecWriter(Vec<u8>);
1615
impl Writer for VecWriter {
@@ -24,12 +23,11 @@ impl Writer for VecWriter {
2423
}
2524

2625
#[inline]
27-
pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
28-
let logger = Arc::new(test_logger::TestLogger::new("".to_owned(), out));
29-
if let Ok((latest_block_hash, monitor)) = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(data), logger.clone()) {
26+
pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
27+
if let Ok((latest_block_hash, monitor)) = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(data)) {
3028
let mut w = VecWriter(Vec::new());
3129
monitor.write_for_disk(&mut w).unwrap();
32-
let deserialized_copy = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&w.0), logger.clone()).unwrap();
30+
let deserialized_copy = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&w.0)).unwrap();
3331
assert!(latest_block_hash == deserialized_copy.0);
3432
assert!(monitor == deserialized_copy.1);
3533
}

fuzz/src/full_stack.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ impl<'a> std::hash::Hash for Peer<'a> {
135135
}
136136

137137
struct MoneyLossDetector<'a> {
138-
manager: Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>>>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>>>,
139-
monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>>>,
140-
handler: PeerManager<Peer<'a>, Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>>>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>>>>,
138+
manager: Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<ChainWatchInterfaceUtil>>>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>, Arc<dyn Logger>>>,
139+
monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<ChainWatchInterfaceUtil>>>,
140+
handler: PeerManager<Peer<'a>, Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<ChainWatchInterfaceUtil>>>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>, Arc<dyn Logger>>>, Arc<dyn Logger>>,
141141

142142
peers: &'a RefCell<[bool; 256]>,
143143
funding_txn: Vec<Transaction>,
@@ -149,9 +149,9 @@ struct MoneyLossDetector<'a> {
149149
}
150150
impl<'a> MoneyLossDetector<'a> {
151151
pub fn new(peers: &'a RefCell<[bool; 256]>,
152-
manager: Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>>>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>>>,
153-
monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>>>,
154-
handler: PeerManager<Peer<'a>, Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>>>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>>>>) -> Self {
152+
manager: Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<ChainWatchInterfaceUtil>>>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>, Arc<dyn Logger>>>,
153+
monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<ChainWatchInterfaceUtil>>>,
154+
handler: PeerManager<Peer<'a>, Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<ChainWatchInterfaceUtil>>>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>, Arc<dyn Logger>>>, Arc<dyn Logger>>) -> Self {
155155
MoneyLossDetector {
156156
manager,
157157
monitor,
@@ -323,7 +323,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
323323
Err(_) => return,
324324
};
325325

326-
let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger)));
326+
let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin));
327327
let broadcast = Arc::new(TestBroadcaster{});
328328
let monitor = Arc::new(channelmonitor::SimpleManyChannelMonitor::new(watch.clone(), broadcast.clone(), Arc::clone(&logger), fee_est.clone()));
329329

lightning-net-tokio/src/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
//! // Define concrete types for our high-level objects:
2525
//! type TxBroadcaster = dyn lightning::chain::chaininterface::BroadcasterInterface;
2626
//! type FeeEstimator = dyn lightning::chain::chaininterface::FeeEstimator;
27-
//! type ChannelMonitor = lightning::ln::channelmonitor::SimpleManyChannelMonitor<lightning::chain::transaction::OutPoint, lightning::chain::keysinterface::InMemoryChannelKeys, Arc<TxBroadcaster>, Arc<FeeEstimator>>;
28-
//! type ChannelManager = lightning::ln::channelmanager::SimpleArcChannelManager<ChannelMonitor, TxBroadcaster, FeeEstimator>;
29-
//! type PeerManager = lightning::ln::peer_handler::SimpleArcPeerManager<lightning_net_tokio::SocketDescriptor, ChannelMonitor, TxBroadcaster, FeeEstimator>;
27+
//! type Logger = dyn lightning::util::logger::Logger;
28+
//! type ChainWatchInterface = dyn lightning::chain::chaininterface::ChainWatchInterface;
29+
//! type ChannelMonitor = lightning::ln::channelmonitor::SimpleManyChannelMonitor<lightning::chain::transaction::OutPoint, lightning::chain::keysinterface::InMemoryChannelKeys, Arc<TxBroadcaster>, Arc<FeeEstimator>, Arc<Logger>, Arc<ChainWatchInterface>>;
30+
//! type ChannelManager = lightning::ln::channelmanager::SimpleArcChannelManager<ChannelMonitor, TxBroadcaster, FeeEstimator, Logger>;
31+
//! type PeerManager = lightning::ln::peer_handler::SimpleArcPeerManager<lightning_net_tokio::SocketDescriptor, ChannelMonitor, TxBroadcaster, FeeEstimator, Logger>;
3032
//!
3133
//! // Connect to node with pubkey their_node_id at addr:
3234
//! async fn connect_to_node(peer_manager: PeerManager, channel_monitor: Arc<ChannelMonitor>, channel_manager: ChannelManager, their_node_id: PublicKey, addr: SocketAddr) {
@@ -69,6 +71,7 @@ use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt};
6971
use lightning::ln::peer_handler;
7072
use lightning::ln::peer_handler::SocketDescriptor as LnSocketTrait;
7173
use lightning::ln::msgs::ChannelMessageHandler;
74+
use lightning::util::logger::Logger;
7275

7376
use std::{task, thread};
7477
use std::net::SocketAddr;
@@ -121,7 +124,7 @@ impl Connection {
121124
_ => panic!()
122125
}
123126
}
124-
async fn schedule_read<CMH: ChannelMessageHandler + 'static>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>>>, us: Arc<Mutex<Self>>, mut reader: io::ReadHalf<TcpStream>, mut read_wake_receiver: mpsc::Receiver<()>, mut write_avail_receiver: mpsc::Receiver<()>) {
127+
async fn schedule_read<CMH: ChannelMessageHandler + 'static, L: Logger + 'static + ?Sized>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>, Arc<L>>>, us: Arc<Mutex<Self>>, mut reader: io::ReadHalf<TcpStream>, mut read_wake_receiver: mpsc::Receiver<()>, mut write_avail_receiver: mpsc::Receiver<()>) {
125128
let peer_manager_ref = peer_manager.clone();
126129
// 8KB is nice and big but also should never cause any issues with stack overflowing.
127130
let mut buf = [0; 8192];
@@ -231,7 +234,7 @@ impl Connection {
231234
/// not need to poll the provided future in order to make progress.
232235
///
233236
/// See the module-level documentation for how to handle the event_notify mpsc::Sender.
234-
pub fn setup_inbound<CMH: ChannelMessageHandler + 'static>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>>>, event_notify: mpsc::Sender<()>, stream: TcpStream) -> impl std::future::Future<Output=()> {
237+
pub fn setup_inbound<CMH: ChannelMessageHandler + 'static, L: Logger + 'static + ?Sized>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>, Arc<L>>>, event_notify: mpsc::Sender<()>, stream: TcpStream) -> impl std::future::Future<Output=()> {
235238
let (reader, write_receiver, read_receiver, us) = Connection::new(event_notify, stream);
236239
#[cfg(debug_assertions)]
237240
let last_us = Arc::clone(&us);
@@ -270,7 +273,7 @@ pub fn setup_inbound<CMH: ChannelMessageHandler + 'static>(peer_manager: Arc<pee
270273
/// not need to poll the provided future in order to make progress.
271274
///
272275
/// See the module-level documentation for how to handle the event_notify mpsc::Sender.
273-
pub fn setup_outbound<CMH: ChannelMessageHandler + 'static>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>>>, event_notify: mpsc::Sender<()>, their_node_id: PublicKey, stream: TcpStream) -> impl std::future::Future<Output=()> {
276+
pub fn setup_outbound<CMH: ChannelMessageHandler + 'static, L: Logger + 'static + ?Sized>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>, Arc<L>>>, event_notify: mpsc::Sender<()>, their_node_id: PublicKey, stream: TcpStream) -> impl std::future::Future<Output=()> {
274277
let (reader, mut write_receiver, read_receiver, us) = Connection::new(event_notify, stream);
275278
#[cfg(debug_assertions)]
276279
let last_us = Arc::clone(&us);
@@ -339,7 +342,7 @@ pub fn setup_outbound<CMH: ChannelMessageHandler + 'static>(peer_manager: Arc<pe
339342
/// make progress.
340343
///
341344
/// See the module-level documentation for how to handle the event_notify mpsc::Sender.
342-
pub async fn connect_outbound<CMH: ChannelMessageHandler + 'static>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>>>, event_notify: mpsc::Sender<()>, their_node_id: PublicKey, addr: SocketAddr) -> Option<impl std::future::Future<Output=()>> {
345+
pub async fn connect_outbound<CMH: ChannelMessageHandler + 'static, L: Logger + 'static + ?Sized>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>, Arc<L>>>, event_notify: mpsc::Sender<()>, their_node_id: PublicKey, addr: SocketAddr) -> Option<impl std::future::Future<Output=()>> {
343346
if let Ok(Ok(stream)) = time::timeout(Duration::from_secs(10), TcpStream::connect(&addr)).await {
344347
Some(setup_outbound(peer_manager, event_notify, their_node_id, stream))
345348
} else { None }

0 commit comments

Comments
 (0)