Skip to content

Commit b0d0f3d

Browse files
committed
Apply network graph updates through NetworkUpdate's instead of Event's
1 parent 15b79f8 commit b0d0f3d

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
use crate::util::string::PrintableString;
3535

@@ -213,9 +213,6 @@ impl_writeable_tlv_based_enum_upgradable!(NetworkUpdate,
213213
/// This network graph is then used for routing payments.
214214
/// Provides interface to help with initial routing sync by
215215
/// serving historical announcements.
216-
///
217-
/// Serves as an [`EventHandler`] for applying updates from [`Event::PaymentPathFailed`] to the
218-
/// [`NetworkGraph`].
219216
pub struct P2PGossipSync<G: Deref<Target=NetworkGraph<L>>, C: Deref, L: Deref>
220217
where C::Target: chain::Access, L::Target: Logger
221218
{
@@ -275,32 +272,31 @@ where C::Target: chain::Access, L::Target: Logger
275272
}
276273
}
277274

278-
impl<L: Deref> EventHandler for NetworkGraph<L> where L::Target: Logger {
279-
fn handle_event(&self, event: &Event) {
280-
if let Event::PaymentPathFailed { network_update, .. } = event {
281-
if let Some(network_update) = network_update {
282-
match *network_update {
283-
NetworkUpdate::ChannelUpdateMessage { ref msg } => {
284-
let short_channel_id = msg.contents.short_channel_id;
285-
let is_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
286-
let status = if is_enabled { "enabled" } else { "disabled" };
287-
log_debug!(self.logger, "Updating channel with channel_update from a payment failure. Channel {} is {}.", short_channel_id, status);
288-
let _ = self.update_channel(msg);
289-
},
290-
NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } => {
291-
let action = if is_permanent { "Removing" } else { "Disabling" };
292-
log_debug!(self.logger, "{} channel graph entry for {} due to a payment failure.", action, short_channel_id);
293-
self.channel_failed(short_channel_id, is_permanent);
294-
},
295-
NetworkUpdate::NodeFailure { ref node_id, is_permanent } => {
296-
if is_permanent {
297-
log_debug!(self.logger,
298-
"Removed node graph entry for {} due to a payment failure.", log_pubkey!(node_id));
299-
self.node_failed_permanent(node_id);
300-
};
301-
},
302-
}
303-
}
275+
impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
276+
/// Handles any network updates originating from [`Event`]s.
277+
///
278+
/// [`Event`]: crate::util::events::Event
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+
},
304300
}
305301
}
306302
}
@@ -1931,15 +1927,14 @@ mod tests {
19311927
use crate::chain;
19321928
use crate::ln::channelmanager;
19331929
use crate::ln::chan_utils::make_funding_redeemscript;
1934-
use crate::ln::PaymentHash;
19351930
use crate::ln::features::InitFeatures;
19361931
use crate::routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate, NodeAlias, MAX_EXCESS_BYTES_FOR_RELAY, NodeId, RoutingFees, ChannelUpdateInfo, ChannelInfo, NodeAnnouncementInfo, NodeInfo};
19371932
use crate::ln::msgs::{RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
19381933
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate,
19391934
ReplyChannelRange, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
19401935
use crate::util::test_utils;
19411936
use crate::util::ser::{ReadableArgs, Writeable};
1942-
use crate::util::events::{Event, EventHandler, MessageSendEvent, MessageSendEventsProvider};
1937+
use crate::util::events::{MessageSendEvent, MessageSendEventsProvider};
19431938
use crate::util::scid_utils::scid_from_parts;
19441939

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

2386-
network_graph.handle_event(&Event::PaymentPathFailed {
2387-
payment_id: None,
2388-
payment_hash: PaymentHash([0; 32]),
2389-
payment_failed_permanently: false,
2390-
all_paths_failed: true,
2391-
path: vec![],
2392-
network_update: Some(NetworkUpdate::ChannelUpdateMessage {
2393-
msg: valid_channel_update,
2394-
}),
2395-
short_channel_id: None,
2396-
retry: None,
2397-
error_code: None,
2398-
error_data: None,
2381+
network_graph.handle_network_update(&NetworkUpdate::ChannelUpdateMessage {
2382+
msg: valid_channel_update,
23992383
});
24002384

24012385
assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_some());
@@ -2410,20 +2394,9 @@ mod tests {
24102394
}
24112395
};
24122396

2413-
network_graph.handle_event(&Event::PaymentPathFailed {
2414-
payment_id: None,
2415-
payment_hash: PaymentHash([0; 32]),
2416-
payment_failed_permanently: false,
2417-
all_paths_failed: true,
2418-
path: vec![],
2419-
network_update: Some(NetworkUpdate::ChannelFailure {
2420-
short_channel_id,
2421-
is_permanent: false,
2422-
}),
2423-
short_channel_id: None,
2424-
retry: None,
2425-
error_code: None,
2426-
error_data: None,
2397+
network_graph.handle_network_update(&NetworkUpdate::ChannelFailure {
2398+
short_channel_id,
2399+
is_permanent: false,
24272400
});
24282401

24292402
match network_graph.read_only().channels().get(&short_channel_id) {
@@ -2435,20 +2408,9 @@ mod tests {
24352408
}
24362409

24372410
// Permanent closing deletes a channel
2438-
network_graph.handle_event(&Event::PaymentPathFailed {
2439-
payment_id: None,
2440-
payment_hash: PaymentHash([0; 32]),
2441-
payment_failed_permanently: false,
2442-
all_paths_failed: true,
2443-
path: vec![],
2444-
network_update: Some(NetworkUpdate::ChannelFailure {
2445-
short_channel_id,
2446-
is_permanent: true,
2447-
}),
2448-
short_channel_id: None,
2449-
retry: None,
2450-
error_code: None,
2451-
error_data: None,
2411+
network_graph.handle_network_update(&NetworkUpdate::ChannelFailure {
2412+
short_channel_id,
2413+
is_permanent: true,
24522414
});
24532415

24542416
assert_eq!(network_graph.read_only().channels().len(), 0);
@@ -2467,40 +2429,18 @@ mod tests {
24672429
assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
24682430

24692431
// Non-permanent node failure does not delete any nodes or channels
2470-
network_graph.handle_event(&Event::PaymentPathFailed {
2471-
payment_id: None,
2472-
payment_hash: PaymentHash([0; 32]),
2473-
payment_failed_permanently: false,
2474-
all_paths_failed: true,
2475-
path: vec![],
2476-
network_update: Some(NetworkUpdate::NodeFailure {
2477-
node_id: node_2_id,
2478-
is_permanent: false,
2479-
}),
2480-
short_channel_id: None,
2481-
retry: None,
2482-
error_code: None,
2483-
error_data: None,
2432+
network_graph.handle_network_update(&NetworkUpdate::NodeFailure {
2433+
node_id: node_2_id,
2434+
is_permanent: false,
24842435
});
24852436

24862437
assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
24872438
assert!(network_graph.read_only().nodes().get(&NodeId::from_pubkey(&node_2_id)).is_some());
24882439

24892440
// Permanent node failure deletes node and its channels
2490-
network_graph.handle_event(&Event::PaymentPathFailed {
2491-
payment_id: None,
2492-
payment_hash: PaymentHash([0; 32]),
2493-
payment_failed_permanently: false,
2494-
all_paths_failed: true,
2495-
path: vec![],
2496-
network_update: Some(NetworkUpdate::NodeFailure {
2497-
node_id: node_2_id,
2498-
is_permanent: true,
2499-
}),
2500-
short_channel_id: None,
2501-
retry: None,
2502-
error_code: None,
2503-
error_data: None,
2441+
network_graph.handle_network_update(&NetworkUpdate::NodeFailure {
2442+
node_id: node_2_id,
2443+
is_permanent: true,
25042444
});
25052445

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

0 commit comments

Comments
 (0)