Skip to content

Commit 8686fea

Browse files
committed
fix: Handle Ok(false) from route_handler callbacks in event path
The broadcast event handling code did not catch Ok(false) returned from the route handler when deciding whether or not to broadcast messages. Instead, it only checked if the return value was an Error. Fix it up and enable the regression tests.
1 parent 6367162 commit 8686fea

File tree

1 file changed

+47
-23
lines changed

1 file changed

+47
-23
lines changed

lightning/src/ln/peers/handler.rs

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,23 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, TransportImpl
11361136
},
11371137
MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
11381138
log_trace!(self.logger, "Handling BroadcastChannelAnnouncement event in peer_handler for short channel id {}", msg.contents.short_channel_id);
1139-
if self.message_handler.route_handler.handle_channel_announcement(msg).is_ok() && self.message_handler.route_handler.handle_channel_update(update_msg).is_ok() {
1139+
let route_handler_wants_broadcast = match self.message_handler.route_handler.handle_channel_announcement(msg) {
1140+
Err(e) => {
1141+
log_trace!(self.logger, "Ignoring because handle_channel_announcement returned error: {:?}", e);
1142+
false
1143+
}
1144+
Ok(false) => false,
1145+
Ok(true) => {
1146+
match self.message_handler.route_handler.handle_channel_update(update_msg) {
1147+
Err(e) => {
1148+
log_trace!(self.logger, "Ignoring because handle_channel_update returned error: {:?}", e);
1149+
false
1150+
},
1151+
Ok(result) => result
1152+
}
1153+
}
1154+
};
1155+
if route_handler_wants_broadcast {
11401156
for (descriptor, peer) in peers.initialized_peers_mut() {
11411157
if !peer.should_forward_channel_announcement(msg.contents.short_channel_id) {
11421158
continue
@@ -1154,26 +1170,38 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, TransportImpl
11541170
},
11551171
MessageSendEvent::BroadcastNodeAnnouncement { ref msg } => {
11561172
log_trace!(self.logger, "Handling BroadcastNodeAnnouncement event in peer_handler");
1157-
if self.message_handler.route_handler.handle_node_announcement(msg).is_ok() {
1158-
for (descriptor, peer) in peers.initialized_peers_mut() {
1159-
if !peer.should_forward_node_announcement(msg.contents.node_id) {
1160-
continue
1173+
match self.message_handler.route_handler.handle_node_announcement(msg) {
1174+
Err(e) => {
1175+
log_trace!(self.logger, "Ignoring because handle_node_announcement returned error: {:?}", e);
1176+
},
1177+
Ok(true) => {
1178+
for (descriptor, peer) in peers.initialized_peers_mut() {
1179+
if !peer.should_forward_node_announcement(msg.contents.node_id) {
1180+
continue
1181+
}
1182+
peer.transport.enqueue_message(msg, &mut peer.pending_outbound_buffer, &*self.logger);
1183+
self.do_attempt_write_data(&mut (*descriptor).clone(), &mut peer.post_init_state, &mut peer.transport, &mut peer.pending_outbound_buffer);
11611184
}
1162-
peer.transport.enqueue_message(msg, &mut peer.pending_outbound_buffer, &*self.logger);
1163-
self.do_attempt_write_data(&mut (*descriptor).clone(), &mut peer.post_init_state, &mut peer.transport, &mut peer.pending_outbound_buffer);
1164-
}
1185+
},
1186+
Ok(false) => { }
11651187
}
11661188
},
11671189
MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
11681190
log_trace!(self.logger, "Handling BroadcastChannelUpdate event in peer_handler for short channel id {}", msg.contents.short_channel_id);
1169-
if self.message_handler.route_handler.handle_channel_update(msg).is_ok() {
1170-
for (descriptor, peer) in peers.initialized_peers_mut() {
1171-
if !peer.should_forward_channel_announcement(msg.contents.short_channel_id) {
1172-
continue
1191+
match self.message_handler.route_handler.handle_channel_update(msg) {
1192+
Err(e) => {
1193+
log_trace!(self.logger, "Ignoring because handle_channel_update returned error: {:?}", e);
1194+
},
1195+
Ok(true) => {
1196+
for (descriptor, peer) in peers.initialized_peers_mut() {
1197+
if !peer.should_forward_channel_announcement(msg.contents.short_channel_id) {
1198+
continue
1199+
}
1200+
peer.transport.enqueue_message(msg, &mut peer.pending_outbound_buffer, &*self.logger);
1201+
self.do_attempt_write_data(&mut (*descriptor).clone(), &mut peer.post_init_state, &mut peer.transport, &mut peer.pending_outbound_buffer);
11731202
}
1174-
peer.transport.enqueue_message(msg, &mut peer.pending_outbound_buffer, &*self.logger);
1175-
self.do_attempt_write_data(&mut (*descriptor).clone(), &mut peer.post_init_state, &mut peer.transport, &mut peer.pending_outbound_buffer);
1176-
}
1203+
},
1204+
Ok(false) => { }
11771205
}
11781206
},
11791207
MessageSendEvent::PaymentFailureNetworkUpdate { ref update } => {
@@ -2468,7 +2496,6 @@ mod unit_tests {
24682496
// Test that a post-Init connection:
24692497
// * process_events() sends nothing when it receives a BroadcastChannelAnnouncement if the
24702498
// route_handler.handle_channel_announcement returns false
2471-
// XXXBUG: Implementation does not check return value of handle_channel_announcement, only that it didn't error
24722499
#[test]
24732500
fn post_init_broadcast_channel_announcement_route_handler_handle_announcement_returns_false() {
24742501
let channel_handler = TestChannelMessageHandler::new();
@@ -2485,13 +2512,12 @@ mod unit_tests {
24852512
let peer_manager = new_peer_manager_post_init!(&test_ctx, &mut descriptor, &transport);
24862513

24872514
peer_manager.process_events();
2488-
// assert!(descriptor.get_recording().is_empty());
2515+
assert!(descriptor.get_recording().is_empty());
24892516
}
24902517

24912518
// Test that a post-Init connection:
24922519
// * process_events() sends nothing when it receives a BroadcastChannelAnnouncement if the
24932520
// route_handler.handle_channel_update returns false
2494-
// XXXBUG: Implementation does not check return value of handle_channel_update, only that it didn't error
24952521
#[test]
24962522
fn post_init_broadcast_channel_announcement_route_handle_update_returns_false() {
24972523
let channel_handler = TestChannelMessageHandler::new();
@@ -2508,7 +2534,7 @@ mod unit_tests {
25082534
let peer_manager = new_peer_manager_post_init!(&test_ctx, &mut descriptor, &transport);
25092535

25102536
peer_manager.process_events();
2511-
// assert!(descriptor.get_recording().is_empty());
2537+
assert!(descriptor.get_recording().is_empty());
25122538
}
25132539

25142540
// To reduce test expansion, the unconnected and connected transport tests are only run on one
@@ -2673,7 +2699,6 @@ mod unit_tests {
26732699
// Test that a post-Init connection:
26742700
// * process_events() sends nothing when it receives a BroadcastNodeAnnouncement if the
26752701
// route_handler.handle_node_announcement returns false
2676-
// XXXBUG: Implementation does not check return value of handle_node_announcement, only that it didn't error
26772702
#[test]
26782703
fn post_init_broadcast_node_announcement_route_handler_handle_announcement_returns_false() {
26792704
let channel_handler = TestChannelMessageHandler::new();
@@ -2689,7 +2714,7 @@ mod unit_tests {
26892714
let peer_manager = new_peer_manager_post_init!(&test_ctx, &mut descriptor, &transport);
26902715

26912716
peer_manager.process_events();
2692-
// assert!(descriptor.get_recording().is_empty());
2717+
assert!(descriptor.get_recording().is_empty());
26932718
}
26942719

26952720
// Test that a post-Init connection:
@@ -2737,7 +2762,6 @@ mod unit_tests {
27372762
// Test that a post-Init connection:
27382763
// * process_events() sends nothing when it receives a BroadcastChannelUpdate if the
27392764
// route_handler.handle_channel_update returns false
2740-
// XXXBUG: Implementation does not check return value of handle_node_announcement, only that it didn't error
27412765
#[test]
27422766
fn post_init_broadcast_channel_update_route_handler_handle_update_returns_false() {
27432767
let channel_handler = TestChannelMessageHandler::new();
@@ -2753,7 +2777,7 @@ mod unit_tests {
27532777
let peer_manager = new_peer_manager_post_init!(&test_ctx, &mut descriptor, &transport);
27542778

27552779
peer_manager.process_events();
2756-
// assert!(descriptor.get_recording().is_empty());
2780+
assert!(descriptor.get_recording().is_empty());
27572781
}
27582782

27592783
// Test that a post-Init connection:

0 commit comments

Comments
 (0)