Skip to content

Commit 4ea3415

Browse files
committed
Handle NetworkUpdate's instead of Event's
1 parent 68f98f4 commit 4ea3415

File tree

2 files changed

+46
-101
lines changed

2 files changed

+46
-101
lines changed

lightning-background-processor/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ impl<
219219
where A::Target: chain::Access, L::Target: Logger {
220220
fn handle_event(&self, event: Event) {
221221
if let Some(network_graph) = self.gossip_sync.network_graph() {
222-
network_graph.handle_event(event.clone());
222+
if let Event::PaymentPathFailed { ref network_update, .. } = event {
223+
if let Some(network_update) = network_update {
224+
network_graph.handle_network_update(&network_update);
225+
}
226+
}
223227
}
224228
self.event_handler.handle_event(event);
225229
}

lightning/src/routing/gossip.rs

+41-100
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds
2929
use crate::ln::msgs;
3030
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, MaybeReadable};
3131
use crate::util::logger::{Logger, Level};
32-
use crate::util::events::{Event, EventHandler, MessageSendEvent, MessageSendEventsProvider};
32+
use crate::util::events::{MessageSendEvent, MessageSendEventsProvider};
3333
use crate::util::scid_utils::{block_from_scid, scid_from_parts, MAX_SCID_BLOCK};
3434

3535
use crate::io;
@@ -213,8 +213,8 @@ impl_writeable_tlv_based_enum_upgradable!(NetworkUpdate,
213213
/// Provides interface to help with initial routing sync by
214214
/// serving historical announcements.
215215
///
216-
/// Serves as an [`EventHandler`] for applying updates from [`Event::PaymentPathFailed`] to the
217-
/// [`NetworkGraph`].
216+
/// Implements a referenced version of [`EventHandler`] for applying updates from
217+
/// [`Event::PaymentPathFailed`] to the [`NetworkGraph`].
218218
pub struct P2PGossipSync<G: Deref<Target=NetworkGraph<L>>, C: Deref, L: Deref>
219219
where C::Target: chain::Access, L::Target: Logger
220220
{
@@ -274,32 +274,29 @@ where C::Target: chain::Access, L::Target: Logger
274274
}
275275
}
276276

277-
impl<L: Deref> EventHandler for NetworkGraph<L> where L::Target: Logger {
278-
fn handle_event(&self, event: Event) {
279-
if let Event::PaymentPathFailed { network_update, .. } = event {
280-
if let Some(network_update) = network_update {
281-
match network_update {
282-
NetworkUpdate::ChannelUpdateMessage { ref msg } => {
283-
let short_channel_id = msg.contents.short_channel_id;
284-
let is_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
285-
let status = if is_enabled { "enabled" } else { "disabled" };
286-
log_debug!(self.logger, "Updating channel with channel_update from a payment failure. Channel {} is {}.", short_channel_id, status);
287-
let _ = self.update_channel(msg);
288-
},
289-
NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } => {
290-
let action = if is_permanent { "Removing" } else { "Disabling" };
291-
log_debug!(self.logger, "{} channel graph entry for {} due to a payment failure.", action, short_channel_id);
292-
self.channel_failed(short_channel_id, is_permanent);
293-
},
294-
NetworkUpdate::NodeFailure { ref node_id, is_permanent } => {
295-
if is_permanent {
296-
log_debug!(self.logger,
297-
"Removed node graph entry for {} due to a payment failure.", log_pubkey!(node_id));
298-
self.node_failed_permanent(node_id);
299-
};
300-
},
301-
}
302-
}
277+
impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
278+
/// Handles any network updates originating from [`Event`]s.
279+
pub fn handle_network_update(&self, network_update: &NetworkUpdate) {
280+
match network_update {
281+
NetworkUpdate::ChannelUpdateMessage { ref msg } => {
282+
let short_channel_id = msg.contents.short_channel_id;
283+
let is_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
284+
let status = if is_enabled { "enabled" } else { "disabled" };
285+
log_debug!(self.logger, "Updating channel with channel_update from a payment failure. Channel {} is {}.", short_channel_id, status);
286+
let _ = self.update_channel(msg);
287+
},
288+
NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } => {
289+
let action = if *is_permanent { "Removing" } else { "Disabling" };
290+
log_debug!(self.logger, "{} channel graph entry for {} due to a payment failure.", action, short_channel_id);
291+
self.channel_failed(*short_channel_id, *is_permanent);
292+
},
293+
NetworkUpdate::NodeFailure { ref node_id, is_permanent } => {
294+
if *is_permanent {
295+
log_debug!(self.logger,
296+
"Removed node graph entry for {} due to a payment failure.", log_pubkey!(node_id));
297+
self.node_failed_permanent(node_id);
298+
};
299+
},
303300
}
304301
}
305302
}
@@ -1972,15 +1969,14 @@ mod tests {
19721969
use crate::chain;
19731970
use crate::ln::channelmanager;
19741971
use crate::ln::chan_utils::make_funding_redeemscript;
1975-
use crate::ln::PaymentHash;
19761972
use crate::ln::features::InitFeatures;
19771973
use crate::routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate, NodeAlias, MAX_EXCESS_BYTES_FOR_RELAY, NodeId, RoutingFees, ChannelUpdateInfo, ChannelInfo, NodeAnnouncementInfo, NodeInfo};
19781974
use crate::ln::msgs::{RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
19791975
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate,
19801976
ReplyChannelRange, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
19811977
use crate::util::test_utils;
19821978
use crate::util::ser::{ReadableArgs, Writeable};
1983-
use crate::util::events::{Event, EventHandler, MessageSendEvent, MessageSendEventsProvider};
1979+
use crate::util::events::{MessageSendEvent, MessageSendEventsProvider};
19841980
use crate::util::scid_utils::scid_from_parts;
19851981

19861982
use crate::routing::gossip::REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS;
@@ -2424,19 +2420,8 @@ mod tests {
24242420
let valid_channel_update = get_signed_channel_update(|_| {}, node_1_privkey, &secp_ctx);
24252421
assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_none());
24262422

2427-
network_graph.handle_event(Event::PaymentPathFailed {
2428-
payment_id: None,
2429-
payment_hash: PaymentHash([0; 32]),
2430-
payment_failed_permanently: false,
2431-
all_paths_failed: true,
2432-
path: vec![],
2433-
network_update: Some(NetworkUpdate::ChannelUpdateMessage {
2434-
msg: valid_channel_update,
2435-
}),
2436-
short_channel_id: None,
2437-
retry: None,
2438-
error_code: None,
2439-
error_data: None,
2423+
network_graph.handle_network_update(&NetworkUpdate::ChannelUpdateMessage {
2424+
msg: valid_channel_update,
24402425
});
24412426

24422427
assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_some());
@@ -2451,20 +2436,9 @@ mod tests {
24512436
}
24522437
};
24532438

2454-
network_graph.handle_event(Event::PaymentPathFailed {
2455-
payment_id: None,
2456-
payment_hash: PaymentHash([0; 32]),
2457-
payment_failed_permanently: false,
2458-
all_paths_failed: true,
2459-
path: vec![],
2460-
network_update: Some(NetworkUpdate::ChannelFailure {
2461-
short_channel_id,
2462-
is_permanent: false,
2463-
}),
2464-
short_channel_id: None,
2465-
retry: None,
2466-
error_code: None,
2467-
error_data: None,
2439+
network_graph.handle_network_update(&NetworkUpdate::ChannelFailure {
2440+
short_channel_id,
2441+
is_permanent: false,
24682442
});
24692443

24702444
match network_graph.read_only().channels().get(&short_channel_id) {
@@ -2476,20 +2450,9 @@ mod tests {
24762450
}
24772451

24782452
// Permanent closing deletes a channel
2479-
network_graph.handle_event(Event::PaymentPathFailed {
2480-
payment_id: None,
2481-
payment_hash: PaymentHash([0; 32]),
2482-
payment_failed_permanently: false,
2483-
all_paths_failed: true,
2484-
path: vec![],
2485-
network_update: Some(NetworkUpdate::ChannelFailure {
2486-
short_channel_id,
2487-
is_permanent: true,
2488-
}),
2489-
short_channel_id: None,
2490-
retry: None,
2491-
error_code: None,
2492-
error_data: None,
2453+
network_graph.handle_network_update(&NetworkUpdate::ChannelFailure {
2454+
short_channel_id,
2455+
is_permanent: true,
24932456
});
24942457

24952458
assert_eq!(network_graph.read_only().channels().len(), 0);
@@ -2508,40 +2471,18 @@ mod tests {
25082471
assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
25092472

25102473
// Non-permanent node failure does not delete any nodes or channels
2511-
network_graph.handle_event(Event::PaymentPathFailed {
2512-
payment_id: None,
2513-
payment_hash: PaymentHash([0; 32]),
2514-
payment_failed_permanently: false,
2515-
all_paths_failed: true,
2516-
path: vec![],
2517-
network_update: Some(NetworkUpdate::NodeFailure {
2518-
node_id: node_2_id,
2519-
is_permanent: false,
2520-
}),
2521-
short_channel_id: None,
2522-
retry: None,
2523-
error_code: None,
2524-
error_data: None,
2474+
network_graph.handle_network_update(&NetworkUpdate::NodeFailure {
2475+
node_id: node_2_id,
2476+
is_permanent: false,
25252477
});
25262478

25272479
assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
25282480
assert!(network_graph.read_only().nodes().get(&NodeId::from_pubkey(&node_2_id)).is_some());
25292481

25302482
// Permanent node failure deletes node and its channels
2531-
network_graph.handle_event(Event::PaymentPathFailed {
2532-
payment_id: None,
2533-
payment_hash: PaymentHash([0; 32]),
2534-
payment_failed_permanently: false,
2535-
all_paths_failed: true,
2536-
path: vec![],
2537-
network_update: Some(NetworkUpdate::NodeFailure {
2538-
node_id: node_2_id,
2539-
is_permanent: true,
2540-
}),
2541-
short_channel_id: None,
2542-
retry: None,
2543-
error_code: None,
2544-
error_data: None,
2483+
network_graph.handle_network_update(&NetworkUpdate::NodeFailure {
2484+
node_id: node_2_id,
2485+
is_permanent: true,
25452486
});
25462487

25472488
assert_eq!(network_graph.read_only().nodes().len(), 0);

0 commit comments

Comments
 (0)