Skip to content

Commit 498ae1b

Browse files
committed
f - Add GossipSync::None instead of using Option<GossipSync>
1 parent 9de98e3 commit 498ae1b

File tree

1 file changed

+29
-29
lines changed
  • lightning-background-processor/src

1 file changed

+29
-29
lines changed

lightning-background-processor/src/lib.rs

+29-29
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ use std::ops::Deref;
4040
/// [`ChannelManager`] persistence should be done in the background.
4141
/// * Calling [`ChannelManager::timer_tick_occurred`] and [`PeerManager::timer_tick_occurred`]
4242
/// at the appropriate intervals.
43-
/// * Calling [`NetworkGraph::remove_stale_channels`] (if a [`GossipSync`] is provided to
44-
/// [`BackgroundProcessor::start`]).
43+
/// * Calling [`NetworkGraph::remove_stale_channels`] (if a [`GossipSync`] with a [`NetworkGraph`]
44+
/// is provided to [`BackgroundProcessor::start`]).
4545
///
4646
/// It will also call [`PeerManager::process_events`] periodically though this shouldn't be relied
4747
/// upon as doing so may result in high latency.
@@ -102,6 +102,8 @@ where A::Target: chain::Access, L::Target: Logger {
102102
P2P(P),
103103
/// Rapid gossip sync from a trusted server.
104104
Rapid(R),
105+
/// No gossip sync.
106+
None,
105107
}
106108

107109
impl<
@@ -112,10 +114,11 @@ impl<
112114
L: Deref,
113115
> GossipSync<P, R, G, A, L>
114116
where A::Target: chain::Access, L::Target: Logger {
115-
fn network_graph(&self) -> &G {
117+
fn network_graph(&self) -> Option<&G> {
116118
match self {
117-
GossipSync::P2P(gossip_sync) => gossip_sync.network_graph(),
118-
GossipSync::Rapid(gossip_sync) => gossip_sync.network_graph(),
119+
GossipSync::P2P(gossip_sync) => Some(gossip_sync.network_graph()),
120+
GossipSync::Rapid(gossip_sync) => Some(gossip_sync.network_graph()),
121+
GossipSync::None => None,
119122
}
120123
}
121124

@@ -129,14 +132,15 @@ where A::Target: chain::Access, L::Target: Logger {
129132
None
130133
}
131134
},
135+
GossipSync::None => None,
132136
}
133137
}
134138
}
135139

136140
/// Decorates an [`EventHandler`] with common functionality provided by standard [`EventHandler`]s.
137141
struct DecoratingEventHandler<
142+
'a,
138143
E: EventHandler,
139-
GS: Deref<Target = GossipSync<PGS, RGS, G, A, L>>,
140144
PGS: Deref<Target = P2PGossipSync<G, A, L>>,
141145
RGS: Deref<Target = RapidGossipSync<G, L>>,
142146
G: Deref<Target = NetworkGraph<L>>,
@@ -145,22 +149,22 @@ struct DecoratingEventHandler<
145149
>
146150
where A::Target: chain::Access, L::Target: Logger {
147151
event_handler: E,
148-
gossip_sync: Option<GS>,
152+
gossip_sync: &'a GossipSync<PGS, RGS, G, A, L>,
149153
}
150154

151155
impl<
156+
'a,
152157
E: EventHandler,
153-
GS: Deref<Target = GossipSync<PGS, RGS, G, A, L>>,
154158
PGS: Deref<Target = P2PGossipSync<G, A, L>>,
155159
RGS: Deref<Target = RapidGossipSync<G, L>>,
156160
G: Deref<Target = NetworkGraph<L>>,
157161
A: Deref,
158162
L: Deref,
159-
> EventHandler for DecoratingEventHandler<E, GS, PGS, RGS, G, A, L>
163+
> EventHandler for DecoratingEventHandler<'a, E, PGS, RGS, G, A, L>
160164
where A::Target: chain::Access, L::Target: Logger {
161165
fn handle_event(&self, event: &Event) {
162-
if let Some(gossip_sync) = &self.gossip_sync {
163-
gossip_sync.network_graph().handle_event(event);
166+
if let Some(network_graph) = self.gossip_sync.network_graph() {
167+
network_graph.handle_event(event);
164168
}
165169
self.event_handler.handle_event(event);
166170
}
@@ -198,9 +202,9 @@ impl BackgroundProcessor {
198202
///
199203
/// # Rapid Gossip Sync
200204
///
201-
/// If rapid gossip sync is meant to run at startup, pass an optional [`RapidGossipSync`]
202-
/// to `gossip_sync` to indicate that the [`BackgroundProcessor`] should not prune the
203-
/// [`NetworkGraph`] instance until the [`RapidGossipSync`] instance completes its first sync.
205+
/// If rapid gossip sync is meant to run at startup, pass a [`RapidGossipSync`] to `gossip_sync`
206+
/// to indicate that the [`BackgroundProcessor`] should not prune the [`NetworkGraph`] instance
207+
/// until the [`RapidGossipSync`] instance completes its first sync.
204208
///
205209
/// [top-level documentation]: BackgroundProcessor
206210
/// [`join`]: Self::join
@@ -238,8 +242,7 @@ impl BackgroundProcessor {
238242
SC: WriteableScore<'a>,
239243
>(
240244
persister: PS, event_handler: EH, chain_monitor: M, channel_manager: CM,
241-
gossip_sync: Option<GossipSync<PGS, RGS, G, CA, L>>, peer_manager: PM, logger: L,
242-
scorer: Option<S>,
245+
gossip_sync: GossipSync<PGS, RGS, G, CA, L>, peer_manager: PM, logger: L, scorer: Option<S>,
243246
) -> Self
244247
where
245248
CA::Target: 'static + chain::Access,
@@ -260,7 +263,7 @@ impl BackgroundProcessor {
260263
let handle = thread::spawn(move || -> Result<(), std::io::Error> {
261264
let event_handler = DecoratingEventHandler {
262265
event_handler,
263-
gossip_sync: gossip_sync.as_ref(),
266+
gossip_sync: &gossip_sync,
264267
};
265268

266269
log_trace!(logger, "Calling ChannelManager's timer_tick_occurred on startup");
@@ -340,10 +343,7 @@ impl BackgroundProcessor {
340343
if last_prune_call.elapsed().as_secs() > if have_pruned { NETWORK_PRUNE_TIMER } else { FIRST_NETWORK_PRUNE_TIMER } {
341344
// The network graph must not be pruned while rapid sync completion is pending
342345
log_trace!(logger, "Assessing prunability of network graph");
343-
let graph_to_prune = gossip_sync
344-
.as_ref()
345-
.and_then(|gossip_sync| gossip_sync.prunable_network_graph());
346-
if let Some(network_graph) = graph_to_prune {
346+
if let Some(network_graph) = gossip_sync.prunable_network_graph() {
347347
network_graph.remove_stale_channels();
348348

349349
if let Err(e) = persister.persist_graph(network_graph) {
@@ -379,8 +379,8 @@ impl BackgroundProcessor {
379379
}
380380

381381
// Persist NetworkGraph on exit
382-
if let Some(ref gossip_sync) = gossip_sync {
383-
persister.persist_graph(gossip_sync.network_graph())?;
382+
if let Some(network_graph) = gossip_sync.network_graph() {
383+
persister.persist_graph(network_graph)?;
384384
}
385385

386386
Ok(())
@@ -500,16 +500,16 @@ mod tests {
500500
}
501501

502502
impl Node {
503-
fn p2p_gossip_sync(&self) -> Option<GossipSync<PGS, RGS, Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>> {
504-
Some(GossipSync::P2P(self.p2p_gossip_sync.clone()))
503+
fn p2p_gossip_sync(&self) -> GossipSync<PGS, RGS, Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>> {
504+
GossipSync::P2P(self.p2p_gossip_sync.clone())
505505
}
506506

507-
fn rapid_gossip_sync(&self) -> Option<GossipSync<PGS, RGS, Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>> {
508-
Some(GossipSync::Rapid(self.rapid_gossip_sync.clone()))
507+
fn rapid_gossip_sync(&self) -> GossipSync<PGS, RGS, Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>> {
508+
GossipSync::Rapid(self.rapid_gossip_sync.clone())
509509
}
510510

511-
fn no_gossip_sync(&self) -> Option<GossipSync<PGS, RGS, Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>> {
512-
None
511+
fn no_gossip_sync(&self) -> GossipSync<PGS, RGS, Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>> {
512+
GossipSync::None
513513
}
514514
}
515515

0 commit comments

Comments
 (0)