Skip to content

Commit 5b57e12

Browse files
committed
Apply network graph updates through NetworkUpdate's instead of Event's
1 parent 10096ea commit 5b57e12

File tree

2 files changed

+44
-102
lines changed

2 files changed

+44
-102
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

+39-101
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;
@@ -212,9 +212,6 @@ impl_writeable_tlv_based_enum_upgradable!(NetworkUpdate,
212212
/// This network graph is then used for routing payments.
213213
/// Provides interface to help with initial routing sync by
214214
/// serving historical announcements.
215-
///
216-
/// Serves as an [`EventHandler`] for applying updates from [`Event::PaymentPathFailed`] to the
217-
/// [`NetworkGraph`].
218215
pub struct P2PGossipSync<G: Deref<Target=NetworkGraph<L>>, C: Deref, L: Deref>
219216
where C::Target: chain::Access, L::Target: Logger
220217
{
@@ -274,32 +271,29 @@ where C::Target: chain::Access, L::Target: Logger
274271
}
275272
}
276273

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-
}
274+
impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
275+
/// Handles any network updates originating from [`Event`]s.
276+
pub fn handle_network_update(&self, network_update: &NetworkUpdate) {
277+
match network_update {
278+
NetworkUpdate::ChannelUpdateMessage { ref msg } => {
279+
let short_channel_id = msg.contents.short_channel_id;
280+
let is_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
281+
let status = if is_enabled { "enabled" } else { "disabled" };
282+
log_debug!(self.logger, "Updating channel with channel_update from a payment failure. Channel {} is {}.", short_channel_id, status);
283+
let _ = self.update_channel(msg);
284+
},
285+
NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } => {
286+
let action = if *is_permanent { "Removing" } else { "Disabling" };
287+
log_debug!(self.logger, "{} channel graph entry for {} due to a payment failure.", action, short_channel_id);
288+
self.channel_failed(*short_channel_id, *is_permanent);
289+
},
290+
NetworkUpdate::NodeFailure { ref node_id, is_permanent } => {
291+
if *is_permanent {
292+
log_debug!(self.logger,
293+
"Removed node graph entry for {} due to a payment failure.", log_pubkey!(node_id));
294+
self.node_failed_permanent(node_id);
295+
};
296+
},
303297
}
304298
}
305299
}
@@ -1972,15 +1966,14 @@ mod tests {
19721966
use crate::chain;
19731967
use crate::ln::channelmanager;
19741968
use crate::ln::chan_utils::make_funding_redeemscript;
1975-
use crate::ln::PaymentHash;
19761969
use crate::ln::features::InitFeatures;
19771970
use crate::routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate, NodeAlias, MAX_EXCESS_BYTES_FOR_RELAY, NodeId, RoutingFees, ChannelUpdateInfo, ChannelInfo, NodeAnnouncementInfo, NodeInfo};
19781971
use crate::ln::msgs::{RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
19791972
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate,
19801973
ReplyChannelRange, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
19811974
use crate::util::test_utils;
19821975
use crate::util::ser::{ReadableArgs, Writeable};
1983-
use crate::util::events::{Event, EventHandler, MessageSendEvent, MessageSendEventsProvider};
1976+
use crate::util::events::{MessageSendEvent, MessageSendEventsProvider};
19841977
use crate::util::scid_utils::scid_from_parts;
19851978

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

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,
2420+
network_graph.handle_network_update(&NetworkUpdate::ChannelUpdateMessage {
2421+
msg: valid_channel_update,
24402422
});
24412423

24422424
assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_some());
@@ -2451,20 +2433,9 @@ mod tests {
24512433
}
24522434
};
24532435

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,
2436+
network_graph.handle_network_update(&NetworkUpdate::ChannelFailure {
2437+
short_channel_id,
2438+
is_permanent: false,
24682439
});
24692440

24702441
match network_graph.read_only().channels().get(&short_channel_id) {
@@ -2476,20 +2447,9 @@ mod tests {
24762447
}
24772448

24782449
// 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,
2450+
network_graph.handle_network_update(&NetworkUpdate::ChannelFailure {
2451+
short_channel_id,
2452+
is_permanent: true,
24932453
});
24942454

24952455
assert_eq!(network_graph.read_only().channels().len(), 0);
@@ -2508,40 +2468,18 @@ mod tests {
25082468
assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
25092469

25102470
// 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,
2471+
network_graph.handle_network_update(&NetworkUpdate::NodeFailure {
2472+
node_id: node_2_id,
2473+
is_permanent: false,
25252474
});
25262475

25272476
assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
25282477
assert!(network_graph.read_only().nodes().get(&NodeId::from_pubkey(&node_2_id)).is_some());
25292478

25302479
// 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,
2480+
network_graph.handle_network_update(&NetworkUpdate::NodeFailure {
2481+
node_id: node_2_id,
2482+
is_permanent: true,
25452483
});
25462484

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

0 commit comments

Comments
 (0)