Skip to content

Commit 1fd2696

Browse files
committed
Move the pub wait methods from ChannelManager to Future
Rather than having three ways to await a `ChannelManager` being persistable, this moves to just exposing the awaitable `Future` and having sleep functions on that.
1 parent 285af51 commit 1fd2696

File tree

3 files changed

+39
-58
lines changed

3 files changed

+39
-58
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ impl BackgroundProcessor {
597597
define_run_body!(persister, chain_monitor, chain_monitor.process_pending_events(&event_handler),
598598
channel_manager, channel_manager.process_pending_events(&event_handler),
599599
gossip_sync, peer_manager, logger, scorer, stop_thread.load(Ordering::Acquire),
600-
channel_manager.await_persistable_update_timeout(Duration::from_millis(100)),
600+
channel_manager.get_persistable_update_future().wait_timeout(Duration::from_millis(100)),
601601
|_| Instant::now(), |time: &Instant, dur| time.elapsed().as_secs() > dur)
602602
});
603603
Self { stop_thread: stop_thread_clone, thread_handle: Some(handle) }

lightning/src/ln/channelmanager.rs

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6113,34 +6113,11 @@ where
61136113
}
61146114
}
61156115

6116-
/// Blocks until ChannelManager needs to be persisted or a timeout is reached. It returns a bool
6117-
/// indicating whether persistence is necessary. Only one listener on
6118-
/// [`await_persistable_update`], [`await_persistable_update_timeout`], or a future returned by
6119-
/// [`get_persistable_update_future`] is guaranteed to be woken up.
6116+
/// Gets a [`Future`] that completes when this [`ChannelManager`] needs to be persisted.
61206117
///
6121-
/// Note that this method is not available with the `no-std` feature.
6118+
/// Note that callbacks registered on the [`Future`] MUST NOT call back into this
6119+
/// [`ChannelManager`] and should instead register actions to be taken later.
61226120
///
6123-
/// [`await_persistable_update`]: Self::await_persistable_update
6124-
/// [`await_persistable_update_timeout`]: Self::await_persistable_update_timeout
6125-
/// [`get_persistable_update_future`]: Self::get_persistable_update_future
6126-
#[cfg(any(test, feature = "std"))]
6127-
pub fn await_persistable_update_timeout(&self, max_wait: Duration) -> bool {
6128-
self.persistence_notifier.wait_timeout(max_wait)
6129-
}
6130-
6131-
/// Blocks until ChannelManager needs to be persisted. Only one listener on
6132-
/// [`await_persistable_update`], `await_persistable_update_timeout`, or a future returned by
6133-
/// [`get_persistable_update_future`] is guaranteed to be woken up.
6134-
///
6135-
/// [`await_persistable_update`]: Self::await_persistable_update
6136-
/// [`get_persistable_update_future`]: Self::get_persistable_update_future
6137-
pub fn await_persistable_update(&self) {
6138-
self.persistence_notifier.wait()
6139-
}
6140-
6141-
/// Gets a [`Future`] that completes when a persistable update is available. Note that
6142-
/// callbacks registered on the [`Future`] MUST NOT call back into this [`ChannelManager`] and
6143-
/// should instead register actions to be taken later.
61446121
pub fn get_persistable_update_future(&self) -> Future {
61456122
self.persistence_notifier.get_future()
61466123
}
@@ -7899,9 +7876,9 @@ mod tests {
78997876

79007877
// All nodes start with a persistable update pending as `create_network` connects each node
79017878
// with all other nodes to make most tests simpler.
7902-
assert!(nodes[0].node.await_persistable_update_timeout(Duration::from_millis(1)));
7903-
assert!(nodes[1].node.await_persistable_update_timeout(Duration::from_millis(1)));
7904-
assert!(nodes[2].node.await_persistable_update_timeout(Duration::from_millis(1)));
7879+
assert!(nodes[0].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7880+
assert!(nodes[1].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7881+
assert!(nodes[2].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
79057882

79067883
let mut chan = create_announced_chan_between_nodes(&nodes, 0, 1);
79077884

@@ -7915,28 +7892,28 @@ mod tests {
79157892
&nodes[0].node.get_our_node_id()).pop().unwrap();
79167893

79177894
// The first two nodes (which opened a channel) should now require fresh persistence
7918-
assert!(nodes[0].node.await_persistable_update_timeout(Duration::from_millis(1)));
7919-
assert!(nodes[1].node.await_persistable_update_timeout(Duration::from_millis(1)));
7895+
assert!(nodes[0].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7896+
assert!(nodes[1].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
79207897
// ... but the last node should not.
7921-
assert!(!nodes[2].node.await_persistable_update_timeout(Duration::from_millis(1)));
7898+
assert!(!nodes[2].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
79227899
// After persisting the first two nodes they should no longer need fresh persistence.
7923-
assert!(!nodes[0].node.await_persistable_update_timeout(Duration::from_millis(1)));
7924-
assert!(!nodes[1].node.await_persistable_update_timeout(Duration::from_millis(1)));
7900+
assert!(!nodes[0].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7901+
assert!(!nodes[1].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
79257902

79267903
// Node 3, unrelated to the only channel, shouldn't care if it receives a channel_update
79277904
// about the channel.
79287905
nodes[2].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &chan.0);
79297906
nodes[2].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &chan.1);
7930-
assert!(!nodes[2].node.await_persistable_update_timeout(Duration::from_millis(1)));
7907+
assert!(!nodes[2].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
79317908

79327909
// The nodes which are a party to the channel should also ignore messages from unrelated
79337910
// parties.
79347911
nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan.0);
79357912
nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan.1);
79367913
nodes[1].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan.0);
79377914
nodes[1].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan.1);
7938-
assert!(!nodes[0].node.await_persistable_update_timeout(Duration::from_millis(1)));
7939-
assert!(!nodes[1].node.await_persistable_update_timeout(Duration::from_millis(1)));
7915+
assert!(!nodes[0].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7916+
assert!(!nodes[1].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
79407917

79417918
// At this point the channel info given by peers should still be the same.
79427919
assert_eq!(nodes[0].node.list_channels()[0], node_a_chan_info);
@@ -7953,17 +7930,17 @@ mod tests {
79537930
// persisted and that its channel info remains the same.
79547931
nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &as_update);
79557932
nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &bs_update);
7956-
assert!(!nodes[0].node.await_persistable_update_timeout(Duration::from_millis(1)));
7957-
assert!(!nodes[1].node.await_persistable_update_timeout(Duration::from_millis(1)));
7933+
assert!(!nodes[0].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7934+
assert!(!nodes[1].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
79587935
assert_eq!(nodes[0].node.list_channels()[0], node_a_chan_info);
79597936
assert_eq!(nodes[1].node.list_channels()[0], node_b_chan_info);
79607937

79617938
// Finally, deliver the other peers' message, ensuring each node needs to be persisted and
79627939
// the channel info has updated.
79637940
nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &bs_update);
79647941
nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &as_update);
7965-
assert!(nodes[0].node.await_persistable_update_timeout(Duration::from_millis(1)));
7966-
assert!(nodes[1].node.await_persistable_update_timeout(Duration::from_millis(1)));
7942+
assert!(nodes[0].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7943+
assert!(nodes[1].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
79677944
assert_ne!(nodes[0].node.list_channels()[0], node_a_chan_info);
79687945
assert_ne!(nodes[1].node.list_channels()[0], node_b_chan_info);
79697946
}

lightning/src/util/wakers.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ impl Notifier {
3939
}
4040
}
4141

42-
pub(crate) fn wait(&self) {
43-
Sleeper::from_single_future(self.get_future()).wait();
44-
}
45-
46-
#[cfg(any(test, feature = "std"))]
47-
pub(crate) fn wait_timeout(&self, max_wait: Duration) -> bool {
48-
Sleeper::from_single_future(self.get_future()).wait_timeout(max_wait)
49-
}
50-
5142
/// Wake waiters, tracking that wake needs to occur even if there are currently no waiters.
5243
pub(crate) fn notify(&self) {
5344
let mut lock = self.notify_pending.lock().unwrap();
@@ -167,6 +158,19 @@ impl Future {
167158
pub fn register_callback_fn<F: 'static + FutureCallback>(&self, callback: F) {
168159
self.register_callback(Box::new(callback));
169160
}
161+
162+
/// Waits until this [`Future`] completes.
163+
pub fn wait(self) {
164+
Sleeper::from_single_future(self).wait();
165+
}
166+
167+
/// Waits until this [`Future`] completes or the given amount of time has elapsed.
168+
///
169+
/// Returns true if the [`Future`] completed, false if the time elapsed.
170+
#[cfg(any(test, feature = "std"))]
171+
pub fn wait_timeout(self, max_wait: Duration) -> bool {
172+
Sleeper::from_single_future(self).wait_timeout(max_wait)
173+
}
170174
}
171175

172176
use core::task::Waker;
@@ -369,12 +373,12 @@ mod tests {
369373
});
370374

371375
// Check that we can block indefinitely until updates are available.
372-
let _ = persistence_notifier.wait();
376+
let _ = persistence_notifier.get_future().wait();
373377

374378
// Check that the Notifier will return after the given duration if updates are
375379
// available.
376380
loop {
377-
if persistence_notifier.wait_timeout(Duration::from_millis(100)) {
381+
if persistence_notifier.get_future().wait_timeout(Duration::from_millis(100)) {
378382
break
379383
}
380384
}
@@ -384,7 +388,7 @@ mod tests {
384388
// Check that the Notifier will return after the given duration even if no updates
385389
// are available.
386390
loop {
387-
if !persistence_notifier.wait_timeout(Duration::from_millis(100)) {
391+
if !persistence_notifier.get_future().wait_timeout(Duration::from_millis(100)) {
388392
break
389393
}
390394
}
@@ -482,8 +486,8 @@ mod tests {
482486

483487
// If we get a future and don't touch it we're definitely still notify-required.
484488
notifier.get_future();
485-
assert!(notifier.wait_timeout(Duration::from_millis(1)));
486-
assert!(!notifier.wait_timeout(Duration::from_millis(1)));
489+
assert!(notifier.get_future().wait_timeout(Duration::from_millis(1)));
490+
assert!(!notifier.get_future().wait_timeout(Duration::from_millis(1)));
487491

488492
// Even if we poll'd once but didn't observe a `Ready`, we should be notify-required.
489493
let mut future = notifier.get_future();
@@ -492,7 +496,7 @@ mod tests {
492496

493497
notifier.notify();
494498
assert!(woken.load(Ordering::SeqCst));
495-
assert!(notifier.wait_timeout(Duration::from_millis(1)));
499+
assert!(notifier.get_future().wait_timeout(Duration::from_millis(1)));
496500

497501
// However, once we do poll `Ready` it should wipe the notify-required flag.
498502
let mut future = notifier.get_future();
@@ -502,7 +506,7 @@ mod tests {
502506
notifier.notify();
503507
assert!(woken.load(Ordering::SeqCst));
504508
assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
505-
assert!(!notifier.wait_timeout(Duration::from_millis(1)));
509+
assert!(!notifier.get_future().wait_timeout(Duration::from_millis(1)));
506510
}
507511

508512
#[test]

0 commit comments

Comments
 (0)