Skip to content

Commit 7c6dc99

Browse files
committed
Implement EventHandler for NetworkGraph
Instead of implementing EventHandler for P2PGossipSync, implement it on NetworkGraph. This allows RapidGossipSync to handle events, too, by delegating to its NetworkGraph.
1 parent 52007c1 commit 7c6dc99

File tree

3 files changed

+43
-41
lines changed

3 files changed

+43
-41
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ const FIRST_NETWORK_PRUNE_TIMER: u64 = 1;
9292

9393
/// Decorates an [`EventHandler`] with common functionality provided by standard [`EventHandler`]s.
9494
struct DecoratingEventHandler<
95+
'a,
9596
E: EventHandler,
9697
P: Deref<Target = P2PGossipSync<G, A, L>>,
9798
G: Deref<Target = NetworkGraph<L>>,
@@ -100,19 +101,22 @@ struct DecoratingEventHandler<
100101
>
101102
where A::Target: chain::Access, L::Target: Logger {
102103
event_handler: E,
104+
logger: &'a L,
103105
p2p_gossip_sync: Option<P>,
104106
}
105107

106108
impl<
109+
'a,
107110
E: EventHandler,
108111
P: Deref<Target = P2PGossipSync<G, A, L>>,
109112
G: Deref<Target = NetworkGraph<L>>,
110113
A: Deref,
111114
L: Deref,
112-
> EventHandler for DecoratingEventHandler<E, P, G, A, L>
115+
> EventHandler for DecoratingEventHandler<'a, E, P, G, A, L>
113116
where A::Target: chain::Access, L::Target: Logger {
114117
fn handle_event(&self, event: &Event) {
115-
if let Some(event_handler) = &self.p2p_gossip_sync {
118+
if let Some(gossip_sync) = &self.p2p_gossip_sync {
119+
let event_handler = (gossip_sync.network_graph(), self.logger);
116120
event_handler.handle_event(event);
117121
}
118122
self.event_handler.handle_event(event);
@@ -211,7 +215,11 @@ impl BackgroundProcessor {
211215
let stop_thread = Arc::new(AtomicBool::new(false));
212216
let stop_thread_clone = stop_thread.clone();
213217
let handle = thread::spawn(move || -> Result<(), std::io::Error> {
214-
let event_handler = DecoratingEventHandler { event_handler, p2p_gossip_sync: p2p_gossip_sync.as_ref().map(|t| t.deref()) };
218+
let event_handler = DecoratingEventHandler {
219+
event_handler,
220+
logger: &logger,
221+
p2p_gossip_sync: p2p_gossip_sync.as_ref().map(|t| t.deref()),
222+
};
215223

216224
log_trace!(logger, "Calling ChannelManager's timer_tick_occurred on startup");
217225
channel_manager.timer_tick_occurred();

lightning/src/routing/gossip.rs

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,6 @@ impl_writeable_tlv_based_enum_upgradable!(NetworkUpdate,
184184
},
185185
);
186186

187-
impl<G: Deref<Target=NetworkGraph<L>>, C: Deref, L: Deref> EventHandler for P2PGossipSync<G, C, L>
188-
where C::Target: chain::Access, L::Target: Logger {
189-
fn handle_event(&self, event: &Event) {
190-
if let Event::PaymentPathFailed { payment_hash: _, rejected_by_dest: _, network_update, .. } = event {
191-
if let Some(network_update) = network_update {
192-
self.handle_network_update(network_update);
193-
}
194-
}
195-
}
196-
}
197-
198187
/// Receives and validates network updates from peers,
199188
/// stores authentic and relevant data as a network graph.
200189
/// This network graph is then used for routing payments.
@@ -257,27 +246,34 @@ where C::Target: chain::Access, L::Target: Logger
257246
false
258247
}
259248
}
249+
}
260250

261-
/// Applies changes to the [`NetworkGraph`] from the given update.
262-
fn handle_network_update(&self, update: &NetworkUpdate) {
263-
match *update {
264-
NetworkUpdate::ChannelUpdateMessage { ref msg } => {
265-
let short_channel_id = msg.contents.short_channel_id;
266-
let is_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
267-
let status = if is_enabled { "enabled" } else { "disabled" };
268-
log_debug!(self.logger, "Updating channel with channel_update from a payment failure. Channel {} is {}.", short_channel_id, status);
269-
let _ = self.network_graph.update_channel(msg);
270-
},
271-
NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } => {
272-
let action = if is_permanent { "Removing" } else { "Disabling" };
273-
log_debug!(self.logger, "{} channel graph entry for {} due to a payment failure.", action, short_channel_id);
274-
self.network_graph.channel_failed(short_channel_id, is_permanent);
275-
},
276-
NetworkUpdate::NodeFailure { ref node_id, is_permanent } => {
277-
let action = if is_permanent { "Removing" } else { "Disabling" };
278-
log_debug!(self.logger, "{} node graph entry for {} due to a payment failure.", action, node_id);
279-
self.network_graph.node_failed(node_id, is_permanent);
280-
},
251+
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> EventHandler for (&G, &L)
252+
where L::Target: Logger {
253+
fn handle_event(&self, event: &Event) {
254+
if let Event::PaymentPathFailed { payment_hash: _, rejected_by_dest: _, network_update, .. } = event {
255+
if let Some(network_update) = network_update {
256+
let (network_graph, logger) = self;
257+
match *network_update {
258+
NetworkUpdate::ChannelUpdateMessage { ref msg } => {
259+
let short_channel_id = msg.contents.short_channel_id;
260+
let is_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
261+
let status = if is_enabled { "enabled" } else { "disabled" };
262+
log_debug!(logger, "Updating channel with channel_update from a payment failure. Channel {} is {}.", short_channel_id, status);
263+
let _ = network_graph.update_channel(msg);
264+
},
265+
NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } => {
266+
let action = if is_permanent { "Removing" } else { "Disabling" };
267+
log_debug!(logger, "{} channel graph entry for {} due to a payment failure.", action, short_channel_id);
268+
network_graph.channel_failed(short_channel_id, is_permanent);
269+
},
270+
NetworkUpdate::NodeFailure { ref node_id, is_permanent } => {
271+
let action = if is_permanent { "Removing" } else { "Disabling" };
272+
log_debug!(logger, "{} node graph entry for {} due to a payment failure.", action, node_id);
273+
network_graph.node_failed(node_id, is_permanent);
274+
},
275+
}
276+
}
281277
}
282278
}
283279
}
@@ -2055,11 +2051,10 @@ mod tests {
20552051
#[test]
20562052
fn handling_network_update() {
20572053
let logger = test_utils::TestLogger::new();
2058-
let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
20592054
let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
20602055
let network_graph = NetworkGraph::new(genesis_hash, &logger);
2061-
let gossip_sync = P2PGossipSync::new(&network_graph, Some(chain_source.clone()), &logger);
20622056
let secp_ctx = Secp256k1::new();
2057+
let event_handler = (&&network_graph, &&logger);
20632058

20642059
let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
20652060
let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
@@ -2081,7 +2076,7 @@ mod tests {
20812076
let valid_channel_update = get_signed_channel_update(|_| {}, node_1_privkey, &secp_ctx);
20822077
assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_none());
20832078

2084-
gossip_sync.handle_event(&Event::PaymentPathFailed {
2079+
event_handler.handle_event(&Event::PaymentPathFailed {
20852080
payment_id: None,
20862081
payment_hash: PaymentHash([0; 32]),
20872082
rejected_by_dest: false,
@@ -2108,7 +2103,7 @@ mod tests {
21082103
}
21092104
};
21102105

2111-
gossip_sync.handle_event(&Event::PaymentPathFailed {
2106+
event_handler.handle_event(&Event::PaymentPathFailed {
21122107
payment_id: None,
21132108
payment_hash: PaymentHash([0; 32]),
21142109
rejected_by_dest: false,
@@ -2133,7 +2128,7 @@ mod tests {
21332128
}
21342129

21352130
// Permanent closing deletes a channel
2136-
gossip_sync.handle_event(&Event::PaymentPathFailed {
2131+
event_handler.handle_event(&Event::PaymentPathFailed {
21372132
payment_id: None,
21382133
payment_hash: PaymentHash([0; 32]),
21392134
rejected_by_dest: false,

lightning/src/util/events.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,9 @@ pub enum Event {
337337
/// payment route.
338338
///
339339
/// Should be applied to the [`NetworkGraph`] so that routing decisions can take into
340-
/// account the update. [`P2PGossipSync`] is capable of doing this.
340+
/// account the update.
341341
///
342342
/// [`NetworkGraph`]: crate::routing::gossip::NetworkGraph
343-
/// [`P2PGossipSync`]: crate::routing::gossip::P2PGossipSync
344343
network_update: Option<NetworkUpdate>,
345344
/// For both single-path and multi-path payments, this is set if all paths of the payment have
346345
/// failed. This will be set to false if (1) this is an MPP payment and (2) other parts of the

0 commit comments

Comments
 (0)