Skip to content

Commit d82fd8f

Browse files
committed
Apply network graph updates through NetworkUpdate's instead of Event's
1 parent 8f525c4 commit d82fd8f

File tree

2 files changed

+52
-102
lines changed

2 files changed

+52
-102
lines changed

lightning-background-processor/src/lib.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ where
192192
}
193193
}
194194

195+
fn handle_network_graph_update<L: Deref>(
196+
network_graph: &NetworkGraph<L>, event: &Event
197+
) where L::Target: Logger {
198+
if let Event::PaymentPathFailed { ref network_update, .. } = event {
199+
if let Some(network_update) = network_update {
200+
network_graph.handle_network_update(&network_update);
201+
}
202+
}
203+
}
204+
195205
/// Decorates an [`EventHandler`] with common functionality provided by standard [`EventHandler`]s.
196206
struct DecoratingEventHandler<
197207
'a,
@@ -219,7 +229,7 @@ impl<
219229
where A::Target: chain::Access, L::Target: Logger {
220230
fn handle_event(&self, event: &Event) {
221231
if let Some(network_graph) = self.gossip_sync.network_graph() {
222-
network_graph.handle_event(event);
232+
handle_network_graph_update(network_graph, &event)
223233
}
224234
self.event_handler.handle_event(event);
225235
}

lightning/src/routing/gossip.rs

+41-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,31 @@ 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+
///
277+
/// [`Event`]: crate::util::events::Event
278+
pub fn handle_network_update(&self, network_update: &NetworkUpdate) {
279+
match *network_update {
280+
NetworkUpdate::ChannelUpdateMessage { ref msg } => {
281+
let short_channel_id = msg.contents.short_channel_id;
282+
let is_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
283+
let status = if is_enabled { "enabled" } else { "disabled" };
284+
log_debug!(self.logger, "Updating channel with channel_update from a payment failure. Channel {} is {}.", short_channel_id, status);
285+
let _ = self.update_channel(msg);
286+
},
287+
NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } => {
288+
let action = if is_permanent { "Removing" } else { "Disabling" };
289+
log_debug!(self.logger, "{} channel graph entry for {} due to a payment failure.", action, short_channel_id);
290+
self.channel_failed(short_channel_id, is_permanent);
291+
},
292+
NetworkUpdate::NodeFailure { ref node_id, is_permanent } => {
293+
if is_permanent {
294+
log_debug!(self.logger,
295+
"Removed node graph entry for {} due to a payment failure.", log_pubkey!(node_id));
296+
self.node_failed_permanent(node_id);
297+
};
298+
},
303299
}
304300
}
305301
}
@@ -1936,15 +1932,14 @@ mod tests {
19361932
use crate::chain;
19371933
use crate::ln::channelmanager;
19381934
use crate::ln::chan_utils::make_funding_redeemscript;
1939-
use crate::ln::PaymentHash;
19401935
use crate::ln::features::InitFeatures;
19411936
use crate::routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate, NodeAlias, MAX_EXCESS_BYTES_FOR_RELAY, NodeId, RoutingFees, ChannelUpdateInfo, ChannelInfo, NodeAnnouncementInfo, NodeInfo};
19421937
use crate::ln::msgs::{RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
19431938
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate,
19441939
ReplyChannelRange, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
19451940
use crate::util::test_utils;
19461941
use crate::util::ser::{ReadableArgs, Writeable};
1947-
use crate::util::events::{Event, EventHandler, MessageSendEvent, MessageSendEventsProvider};
1942+
use crate::util::events::{MessageSendEvent, MessageSendEventsProvider};
19481943
use crate::util::scid_utils::scid_from_parts;
19491944

19501945
use crate::routing::gossip::REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS;
@@ -2388,19 +2383,8 @@ mod tests {
23882383
let valid_channel_update = get_signed_channel_update(|_| {}, node_1_privkey, &secp_ctx);
23892384
assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_none());
23902385

2391-
network_graph.handle_event(&Event::PaymentPathFailed {
2392-
payment_id: None,
2393-
payment_hash: PaymentHash([0; 32]),
2394-
payment_failed_permanently: false,
2395-
all_paths_failed: true,
2396-
path: vec![],
2397-
network_update: Some(NetworkUpdate::ChannelUpdateMessage {
2398-
msg: valid_channel_update,
2399-
}),
2400-
short_channel_id: None,
2401-
retry: None,
2402-
error_code: None,
2403-
error_data: None,
2386+
network_graph.handle_network_update(&NetworkUpdate::ChannelUpdateMessage {
2387+
msg: valid_channel_update,
24042388
});
24052389

24062390
assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_some());
@@ -2415,20 +2399,9 @@ mod tests {
24152399
}
24162400
};
24172401

2418-
network_graph.handle_event(&Event::PaymentPathFailed {
2419-
payment_id: None,
2420-
payment_hash: PaymentHash([0; 32]),
2421-
payment_failed_permanently: false,
2422-
all_paths_failed: true,
2423-
path: vec![],
2424-
network_update: Some(NetworkUpdate::ChannelFailure {
2425-
short_channel_id,
2426-
is_permanent: false,
2427-
}),
2428-
short_channel_id: None,
2429-
retry: None,
2430-
error_code: None,
2431-
error_data: None,
2402+
network_graph.handle_network_update(&NetworkUpdate::ChannelFailure {
2403+
short_channel_id,
2404+
is_permanent: false,
24322405
});
24332406

24342407
match network_graph.read_only().channels().get(&short_channel_id) {
@@ -2440,20 +2413,9 @@ mod tests {
24402413
}
24412414

24422415
// Permanent closing deletes a channel
2443-
network_graph.handle_event(&Event::PaymentPathFailed {
2444-
payment_id: None,
2445-
payment_hash: PaymentHash([0; 32]),
2446-
payment_failed_permanently: false,
2447-
all_paths_failed: true,
2448-
path: vec![],
2449-
network_update: Some(NetworkUpdate::ChannelFailure {
2450-
short_channel_id,
2451-
is_permanent: true,
2452-
}),
2453-
short_channel_id: None,
2454-
retry: None,
2455-
error_code: None,
2456-
error_data: None,
2416+
network_graph.handle_network_update(&NetworkUpdate::ChannelFailure {
2417+
short_channel_id,
2418+
is_permanent: true,
24572419
});
24582420

24592421
assert_eq!(network_graph.read_only().channels().len(), 0);
@@ -2472,40 +2434,18 @@ mod tests {
24722434
assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
24732435

24742436
// Non-permanent node failure does not delete any nodes or channels
2475-
network_graph.handle_event(&Event::PaymentPathFailed {
2476-
payment_id: None,
2477-
payment_hash: PaymentHash([0; 32]),
2478-
payment_failed_permanently: false,
2479-
all_paths_failed: true,
2480-
path: vec![],
2481-
network_update: Some(NetworkUpdate::NodeFailure {
2482-
node_id: node_2_id,
2483-
is_permanent: false,
2484-
}),
2485-
short_channel_id: None,
2486-
retry: None,
2487-
error_code: None,
2488-
error_data: None,
2437+
network_graph.handle_network_update(&NetworkUpdate::NodeFailure {
2438+
node_id: node_2_id,
2439+
is_permanent: false,
24892440
});
24902441

24912442
assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
24922443
assert!(network_graph.read_only().nodes().get(&NodeId::from_pubkey(&node_2_id)).is_some());
24932444

24942445
// Permanent node failure deletes node and its channels
2495-
network_graph.handle_event(&Event::PaymentPathFailed {
2496-
payment_id: None,
2497-
payment_hash: PaymentHash([0; 32]),
2498-
payment_failed_permanently: false,
2499-
all_paths_failed: true,
2500-
path: vec![],
2501-
network_update: Some(NetworkUpdate::NodeFailure {
2502-
node_id: node_2_id,
2503-
is_permanent: true,
2504-
}),
2505-
short_channel_id: None,
2506-
retry: None,
2507-
error_code: None,
2508-
error_data: None,
2446+
network_graph.handle_network_update(&NetworkUpdate::NodeFailure {
2447+
node_id: node_2_id,
2448+
is_permanent: true,
25092449
});
25102450

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

0 commit comments

Comments
 (0)