Skip to content

Commit 3d171d3

Browse files
committed
Add the ability to broadcast gossip msgs via the event pipeline
When we process gossip messages asynchronously we may find that we want to forward a gossip message to a peer after we've returned from the existing `handle_*` method. In order to do so, we need to be able to send arbitrary loose gossip messages back to the `PeerManager` via `MessageSendEvent`. This commit modifies `MessageSendEvent` in order to support this.
1 parent 39688f5 commit 3d171d3

File tree

6 files changed

+35
-14
lines changed

6 files changed

+35
-14
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4884,7 +4884,7 @@ where
48844884
), chan),
48854885
// Note that announcement_signatures fails if the channel cannot be announced,
48864886
// so get_channel_update_for_broadcast will never fail by the time we get here.
4887-
update_msg: self.get_channel_update_for_broadcast(chan.get()).unwrap(),
4887+
update_msg: Some(self.get_channel_update_for_broadcast(chan.get()).unwrap()),
48884888
});
48894889
},
48904890
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
@@ -5792,7 +5792,7 @@ where
57925792
msg: announcement,
57935793
// Note that announcement_signatures fails if the channel cannot be announced,
57945794
// so get_channel_update_for_broadcast will never fail by the time we get here.
5795-
update_msg: self.get_channel_update_for_broadcast(channel).unwrap(),
5795+
update_msg: Some(self.get_channel_update_for_broadcast(channel).unwrap()),
57965796
});
57975797
}
57985798
}
@@ -6108,6 +6108,7 @@ where
61086108
&events::MessageSendEvent::SendChannelAnnouncement { .. } => false,
61096109
&events::MessageSendEvent::BroadcastChannelAnnouncement { .. } => true,
61106110
&events::MessageSendEvent::BroadcastChannelUpdate { .. } => true,
6111+
&events::MessageSendEvent::BroadcastNodeAnnouncement { .. } => true,
61116112
&events::MessageSendEvent::SendChannelUpdate { .. } => false,
61126113
&events::MessageSendEvent::HandleError { .. } => false,
61136114
&events::MessageSendEvent::SendChannelRangeQuery { .. } => false,

lightning/src/ln/functional_test_utils.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,9 @@ pub fn remove_first_msg_event_to_node(msg_node_id: &PublicKey, msg_events: &Vec<
621621
MessageSendEvent::BroadcastChannelUpdate { .. } => {
622622
false
623623
},
624+
MessageSendEvent::BroadcastNodeAnnouncement { .. } => {
625+
false
626+
},
624627
MessageSendEvent::SendChannelUpdate { node_id, .. } => {
625628
node_id == msg_node_id
626629
},
@@ -1010,7 +1013,7 @@ pub fn create_chan_between_nodes_with_value_b<'a, 'b, 'c>(node_a: &Node<'a, 'b,
10101013
assert_eq!(events_7.len(), 1);
10111014
let (announcement, bs_update) = match events_7[0] {
10121015
MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
1013-
(msg, update_msg)
1016+
(msg, update_msg.clone().unwrap())
10141017
},
10151018
_ => panic!("Unexpected event"),
10161019
};
@@ -1021,6 +1024,7 @@ pub fn create_chan_between_nodes_with_value_b<'a, 'b, 'c>(node_a: &Node<'a, 'b,
10211024
let as_update = match events_8[0] {
10221025
MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
10231026
assert!(*announcement == *msg);
1027+
let update_msg = update_msg.clone().unwrap();
10241028
assert_eq!(update_msg.contents.short_channel_id, announcement.contents.short_channel_id);
10251029
assert_eq!(update_msg.contents.short_channel_id, bs_update.contents.short_channel_id);
10261030
update_msg
@@ -1031,7 +1035,7 @@ pub fn create_chan_between_nodes_with_value_b<'a, 'b, 'c>(node_a: &Node<'a, 'b,
10311035
*node_a.network_chan_count.borrow_mut() += 1;
10321036

10331037
expect_channel_ready_event(&node_b, &node_a.node.get_our_node_id());
1034-
((*announcement).clone(), (*as_update).clone(), (*bs_update).clone())
1038+
((*announcement).clone(), as_update, bs_update)
10351039
}
10361040

10371041
pub fn create_announced_chan_between_nodes<'a, 'b, 'c, 'd>(nodes: &'a Vec<Node<'b, 'c, 'd>>, a: usize, b: usize) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {

lightning/src/ln/peer_handler.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,10 +1707,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
17071707
self.forward_broadcast_msg(peers, &wire::Message::ChannelAnnouncement(msg), None),
17081708
_ => {},
17091709
}
1710-
match self.message_handler.route_handler.handle_channel_update(&update_msg) {
1711-
Ok(_) | Err(LightningError { action: msgs::ErrorAction::IgnoreDuplicateGossip, .. }) =>
1712-
self.forward_broadcast_msg(peers, &wire::Message::ChannelUpdate(update_msg), None),
1713-
_ => {},
1710+
if let Some(msg) = update_msg {
1711+
match self.message_handler.route_handler.handle_channel_update(&msg) {
1712+
Ok(_) | Err(LightningError { action: msgs::ErrorAction::IgnoreDuplicateGossip, .. }) =>
1713+
self.forward_broadcast_msg(peers, &wire::Message::ChannelUpdate(msg), None),
1714+
_ => {},
1715+
}
17141716
}
17151717
},
17161718
MessageSendEvent::BroadcastChannelUpdate { msg } => {
@@ -1721,6 +1723,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
17211723
_ => {},
17221724
}
17231725
},
1726+
MessageSendEvent::BroadcastNodeAnnouncement { msg } => {
1727+
log_debug!(self.logger, "Handling BroadcastNodeAnnouncement event in peer_handler for short node {}", msg.contents.node_id);
1728+
match self.message_handler.route_handler.handle_node_announcement(&msg) {
1729+
Ok(_) | Err(LightningError { action: msgs::ErrorAction::IgnoreDuplicateGossip, .. }) =>
1730+
self.forward_broadcast_msg(peers, &wire::Message::NodeAnnouncement(msg), None),
1731+
_ => {},
1732+
}
1733+
},
17241734
MessageSendEvent::SendChannelUpdate { ref node_id, ref msg } => {
17251735
log_trace!(self.logger, "Handling SendChannelUpdate event in peer_handler for node {} for channel {}",
17261736
log_pubkey!(node_id), msg.contents.short_channel_id);

lightning/src/ln/priv_short_conf_tests.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,14 @@ fn do_test_1_conf_open(connect_style: ConnectStyle) {
184184
msg.clone()
185185
} else { panic!("Unexpected event"); };
186186
let (bs_announcement, bs_update) = if let MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } = bs_announce_events[1] {
187-
(msg.clone(), update_msg.clone())
187+
(msg.clone(), update_msg.clone().unwrap())
188188
} else { panic!("Unexpected event"); };
189189

190190
nodes[0].node.handle_announcement_signatures(&nodes[1].node.get_our_node_id(), &bs_announcement_sigs);
191191
let as_announce_events = nodes[0].node.get_and_clear_pending_msg_events();
192192
assert_eq!(as_announce_events.len(), 1);
193193
let (announcement, as_update) = if let MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } = as_announce_events[0] {
194-
(msg.clone(), update_msg.clone())
194+
(msg.clone(), update_msg.clone().unwrap())
195195
} else { panic!("Unexpected event"); };
196196
assert_eq!(announcement, bs_announcement);
197197

@@ -757,7 +757,7 @@ fn test_public_0conf_channel() {
757757
match bs_announcement[0] {
758758
MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
759759
announcement = msg.clone();
760-
bs_update = update_msg.clone();
760+
bs_update = update_msg.clone().unwrap();
761761
},
762762
_ => panic!("Unexpected event"),
763763
};
@@ -767,6 +767,7 @@ fn test_public_0conf_channel() {
767767
match as_announcement[0] {
768768
MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
769769
assert!(announcement == *msg);
770+
let update_msg = update_msg.as_ref().unwrap();
770771
assert_eq!(update_msg.contents.short_channel_id, scid);
771772
assert_eq!(update_msg.contents.short_channel_id, announcement.contents.short_channel_id);
772773
assert_eq!(update_msg.contents.short_channel_id, bs_update.contents.short_channel_id);

lightning/src/ln/reload_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ fn test_funding_peer_disconnect() {
140140
assert_eq!(events_7.len(), 1);
141141
let (chan_announcement, as_update) = match events_7[0] {
142142
MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
143-
(msg.clone(), update_msg.clone())
143+
(msg.clone(), update_msg.clone().unwrap())
144144
},
145145
_ => panic!("Unexpected event {:?}", events_7[0]),
146146
};
@@ -153,7 +153,7 @@ fn test_funding_peer_disconnect() {
153153
let bs_update = match events_8[0] {
154154
MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
155155
assert_eq!(*msg, chan_announcement);
156-
update_msg.clone()
156+
update_msg.clone().unwrap()
157157
},
158158
_ => panic!("Unexpected event {:?}", events_8[0]),
159159
};

lightning/src/util/events.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1611,13 +1611,18 @@ pub enum MessageSendEvent {
16111611
/// The channel_announcement which should be sent.
16121612
msg: msgs::ChannelAnnouncement,
16131613
/// The followup channel_update which should be sent.
1614-
update_msg: msgs::ChannelUpdate,
1614+
update_msg: Option<msgs::ChannelUpdate>,
16151615
},
16161616
/// Used to indicate that a channel_update should be broadcast to all peers.
16171617
BroadcastChannelUpdate {
16181618
/// The channel_update which should be sent.
16191619
msg: msgs::ChannelUpdate,
16201620
},
1621+
/// Used to indicate that a node_announcement should be broadcast to all peers.
1622+
BroadcastNodeAnnouncement {
1623+
/// The channel_update which should be sent.
1624+
msg: msgs::NodeAnnouncement,
1625+
},
16211626
/// Used to indicate that a channel_update should be sent to a single peer.
16221627
/// In contrast to [`Self::BroadcastChannelUpdate`], this is used when the channel is a
16231628
/// private channel and we shouldn't be informing all of our peers of channel parameters.

0 commit comments

Comments
 (0)