Skip to content

Commit 7e1e0ac

Browse files
committed
Pass gossip_queries messages to handler via ownership
This change modifies gossip_queries methods in RoutingMessageHandler to move the message instead of passing a reference. This allows the message handler to be more efficient by not requiring a full copy of SCIDs passed in messages.
1 parent 14d4492 commit 7e1e0ac

File tree

5 files changed

+36
-47
lines changed

5 files changed

+36
-47
lines changed

lightning-net-tokio/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,10 @@ mod tests {
537537
fn get_next_node_announcements(&self, _starting_point: Option<&PublicKey>, _batch_amount: u8) -> Vec<NodeAnnouncement> { Vec::new() }
538538
fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool { false }
539539
fn sync_routing_table(&self, _their_node_id: &PublicKey) { }
540-
fn handle_reply_channel_range(&self, _their_node_id: &PublicKey, _msg: &ReplyChannelRange) -> Result<(), LightningError> { Ok(()) }
541-
fn handle_reply_short_channel_ids_end(&self, _their_node_id: &PublicKey, _msg: &ReplyShortChannelIdsEnd) -> Result<(), LightningError> { Ok(()) }
542-
fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: &QueryChannelRange) -> Result<(), LightningError> { Ok(()) }
543-
fn handle_query_short_channel_ids(&self, _their_node_id: &PublicKey, _msg: &QueryShortChannelIds) -> Result<(), LightningError> { Ok(()) }
540+
fn handle_reply_channel_range(&self, _their_node_id: &PublicKey, _msg: ReplyChannelRange) -> Result<(), LightningError> { Ok(()) }
541+
fn handle_reply_short_channel_ids_end(&self, _their_node_id: &PublicKey, _msg: ReplyShortChannelIdsEnd) -> Result<(), LightningError> { Ok(()) }
542+
fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: QueryChannelRange) -> Result<(), LightningError> { Ok(()) }
543+
fn handle_query_short_channel_ids(&self, _their_node_id: &PublicKey, _msg: QueryShortChannelIds) -> Result<(), LightningError> { Ok(()) }
544544
}
545545
impl ChannelMessageHandler for MsgHandler {
546546
fn handle_open_channel(&self, _their_node_id: &PublicKey, _their_features: InitFeatures, _msg: &OpenChannel) {}

lightning/src/ln/msgs.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -834,18 +834,22 @@ pub trait RoutingMessageHandler : Send + Sync + events::MessageSendEventsProvide
834834
/// Handles the reply of a query we initiated to learn about channels
835835
/// for a given range of blocks. We can expect to receive one or more
836836
/// replies to a single query.
837-
fn handle_reply_channel_range(&self, their_node_id: &PublicKey, msg: &ReplyChannelRange) -> Result<(), LightningError>;
837+
fn handle_reply_channel_range(&self, their_node_id: &PublicKey, msg: ReplyChannelRange) -> Result<(), LightningError>;
838838
/// Handles the reply of a query we initiated asking for routing gossip
839839
/// messages for a list of channels. We should receive this message when
840840
/// a node has completed its best effort to send us the pertaining routing
841841
/// gossip messages.
842-
fn handle_reply_short_channel_ids_end(&self, their_node_id: &PublicKey, msg: &ReplyShortChannelIdsEnd) -> Result<(), LightningError>;
842+
fn handle_reply_short_channel_ids_end(&self, their_node_id: &PublicKey, msg: ReplyShortChannelIdsEnd) -> Result<(), LightningError>;
843843
/// Handles when a peer asks us to send a list of short_channel_ids
844-
/// for the requested range of blocks.
845-
fn handle_query_channel_range(&self, their_node_id: &PublicKey, msg: &QueryChannelRange) -> Result<(), LightningError>;
844+
/// for the requested range of blocks. There are potential DoS vectors when
845+
/// handling inbound queries. Handling requests with first_blocknum very far
846+
/// away may trigger repeated disk I/O if the NetworkGraph is not fully in-memory.
847+
fn handle_query_channel_range(&self, their_node_id: &PublicKey, msg: QueryChannelRange) -> Result<(), LightningError>;
846848
/// Handles when a peer asks us to send routing gossip messages for a
847-
/// list of short_channel_ids.
848-
fn handle_query_short_channel_ids(&self, their_node_id: &PublicKey, msg: &QueryShortChannelIds) -> Result<(), LightningError>;
849+
/// list of short_channel_ids. There are potential DoS vectors when handling
850+
/// inbound queries. Handling requests with first_blocknum very far away may
851+
/// trigger repeated disk I/O if the NetworkGraph is not fully in-memory.
852+
fn handle_query_short_channel_ids(&self, their_node_id: &PublicKey, msg: QueryShortChannelIds) -> Result<(), LightningError>;
849853
}
850854

851855
mod fuzzy_internal_msgs {

lightning/src/ln/peer_handler.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -842,16 +842,16 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
842842
}
843843
},
844844
wire::Message::QueryShortChannelIds(msg) => {
845-
self.message_handler.route_handler.handle_query_short_channel_ids(&peer.their_node_id.unwrap(), &msg)?;
845+
self.message_handler.route_handler.handle_query_short_channel_ids(&peer.their_node_id.unwrap(), msg)?;
846846
},
847847
wire::Message::ReplyShortChannelIdsEnd(msg) => {
848-
self.message_handler.route_handler.handle_reply_short_channel_ids_end(&peer.their_node_id.unwrap(), &msg)?;
848+
self.message_handler.route_handler.handle_reply_short_channel_ids_end(&peer.their_node_id.unwrap(), msg)?;
849849
},
850850
wire::Message::QueryChannelRange(msg) => {
851-
self.message_handler.route_handler.handle_query_channel_range(&peer.their_node_id.unwrap(), &msg)?;
851+
self.message_handler.route_handler.handle_query_channel_range(&peer.their_node_id.unwrap(), msg)?;
852852
},
853853
wire::Message::ReplyChannelRange(msg) => {
854-
self.message_handler.route_handler.handle_reply_channel_range(&peer.their_node_id.unwrap(), &msg)?;
854+
self.message_handler.route_handler.handle_reply_channel_range(&peer.their_node_id.unwrap(), msg)?;
855855
},
856856
wire::Message::GossipTimestampFilter(_msg) => {
857857
// TODO: handle message

lightning/src/routing/network_graph.rs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,8 @@ impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for N
246246
/// stateless, it does not validate the sequencing of replies for multi-
247247
/// reply ranges. It does not validate whether the reply(ies) cover the
248248
/// queried range. It also does not filter SCIDs to only those in the
249-
/// original query range. In the event of a failure, we may have received
250-
/// some channel information. Before trying with another peer, the
251-
/// caller should update its set of SCIDs that need to be queried.
252-
fn handle_reply_channel_range(&self, their_node_id: &PublicKey, msg: &ReplyChannelRange) -> Result<(), LightningError> {
249+
/// original query range.
250+
fn handle_reply_channel_range(&self, their_node_id: &PublicKey, msg: ReplyChannelRange) -> Result<(), LightningError> {
253251
log_debug!(self.logger, "Handling reply_channel_range peer={}, first_blocknum={}, number_of_blocks={}, full_information={}, scids={}", log_pubkey!(their_node_id), msg.first_blocknum, msg.number_of_blocks, msg.full_information, msg.short_channel_ids.len(),);
254252

255253
// Validate that the remote node maintains up-to-date channel
@@ -263,20 +261,13 @@ impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for N
263261
});
264262
}
265263

266-
// Copy the SCIDs into a new vector to be sent in the SCID query
267-
let scid_size = msg.short_channel_ids.len();
268-
let mut short_channel_ids: Vec<u64> = Vec::with_capacity(scid_size);
269-
for scid in msg.short_channel_ids.iter() {
270-
short_channel_ids.push(scid.clone());
271-
}
272-
273-
log_debug!(self.logger, "Sending query_short_channel_ids peer={}, batch_size={}", log_pubkey!(their_node_id), scid_size);
264+
log_debug!(self.logger, "Sending query_short_channel_ids peer={}, batch_size={}", log_pubkey!(their_node_id), msg.short_channel_ids.len());
274265
let mut pending_events = self.pending_events.lock().unwrap();
275266
pending_events.push(events::MessageSendEvent::SendShortIdsQuery {
276267
node_id: their_node_id.clone(),
277268
msg: QueryShortChannelIds {
278-
chain_hash: msg.chain_hash.clone(),
279-
short_channel_ids,
269+
chain_hash: msg.chain_hash,
270+
short_channel_ids: msg.short_channel_ids,
280271
}
281272
});
282273

@@ -287,7 +278,7 @@ impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for N
287278
/// gossip messages. In the event of a failure, we may have received
288279
/// some channel information. Before trying with another peer, the
289280
/// caller should update its set of SCIDs that need to be queried.
290-
fn handle_reply_short_channel_ids_end(&self, their_node_id: &PublicKey, msg: &ReplyShortChannelIdsEnd) -> Result<(), LightningError> {
281+
fn handle_reply_short_channel_ids_end(&self, their_node_id: &PublicKey, msg: ReplyShortChannelIdsEnd) -> Result<(), LightningError> {
291282
log_debug!(self.logger, "Handling reply_short_channel_ids_end peer={}, full_information={}", log_pubkey!(their_node_id), msg.full_information);
292283

293284
// If the remote node does not have up-to-date information for the
@@ -303,21 +294,15 @@ impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for N
303294
Ok(())
304295
}
305296

306-
/// There are potential DoS vectors when handling inbound queries.
307-
/// Handling requests with first_blocknum very far away may trigger repeated
308-
/// disk I/O if the NetworkGraph is not fully in-memory.
309-
fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: &QueryChannelRange) -> Result<(), LightningError> {
297+
fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: QueryChannelRange) -> Result<(), LightningError> {
310298
// TODO
311299
Err(LightningError {
312300
err: String::from("Not implemented"),
313301
action: ErrorAction::IgnoreError,
314302
})
315303
}
316304

317-
/// There are potential DoS vectors when handling inbound queries.
318-
/// Handling requests with first_blocknum very far away may trigger repeated
319-
/// disk I/O if the NetworkGraph is not fully in-memory.
320-
fn handle_query_short_channel_ids(&self, _their_node_id: &PublicKey, _msg: &QueryShortChannelIds) -> Result<(), LightningError> {
305+
fn handle_query_short_channel_ids(&self, _their_node_id: &PublicKey, _msg: QueryShortChannelIds) -> Result<(), LightningError> {
321306
// TODO
322307
Err(LightningError {
323308
err: String::from("Not implemented"),
@@ -1982,7 +1967,7 @@ mod tests {
19821967
// matching the SCIDs in the reply
19831968
{
19841969
// Handle a single successful reply that encompasses the queried channel range
1985-
let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
1970+
let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, ReplyChannelRange {
19861971
chain_hash,
19871972
full_information: true,
19881973
first_blocknum: 0,
@@ -2023,7 +2008,7 @@ mod tests {
20232008
// full_information=false and short_channel_ids=[] as the signal.
20242009
{
20252010
// Handle the reply indicating the peer was unable to fulfill our request.
2026-
let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2011+
let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, ReplyChannelRange {
20272012
chain_hash,
20282013
full_information: false,
20292014
first_blocknum: 1000,
@@ -2045,7 +2030,7 @@ mod tests {
20452030

20462031
// Test receipt of a successful reply
20472032
{
2048-
let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, &ReplyShortChannelIdsEnd {
2033+
let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, ReplyShortChannelIdsEnd {
20492034
chain_hash,
20502035
full_information: true,
20512036
});
@@ -2055,7 +2040,7 @@ mod tests {
20552040
// Test receipt of a reply that indicates the peer does not maintain up-to-date information
20562041
// for the chain_hash requested in the query.
20572042
{
2058-
let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, &ReplyShortChannelIdsEnd {
2043+
let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, ReplyShortChannelIdsEnd {
20592044
chain_hash,
20602045
full_information: false,
20612046
});
@@ -2072,7 +2057,7 @@ mod tests {
20722057

20732058
let chain_hash = genesis_block(Network::Testnet).header.block_hash();
20742059

2075-
let result = net_graph_msg_handler.handle_query_channel_range(&node_id, &QueryChannelRange {
2060+
let result = net_graph_msg_handler.handle_query_channel_range(&node_id, QueryChannelRange {
20762061
chain_hash,
20772062
first_blocknum: 0,
20782063
number_of_blocks: 0xffff_ffff,
@@ -2088,7 +2073,7 @@ mod tests {
20882073

20892074
let chain_hash = genesis_block(Network::Testnet).header.block_hash();
20902075

2091-
let result = net_graph_msg_handler.handle_query_short_channel_ids(&node_id, &QueryShortChannelIds {
2076+
let result = net_graph_msg_handler.handle_query_short_channel_ids(&node_id, QueryShortChannelIds {
20922077
chain_hash,
20932078
short_channel_ids: vec![0x0003e8_000000_0000],
20942079
});

lightning/src/util/test_utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,19 +322,19 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
322322

323323
fn sync_routing_table(&self, _their_node_id: &PublicKey) {}
324324

325-
fn handle_reply_channel_range(&self, _their_node_id: &PublicKey, _msg: &msgs::ReplyChannelRange) -> Result<(), msgs::LightningError> {
325+
fn handle_reply_channel_range(&self, _their_node_id: &PublicKey, _msg: msgs::ReplyChannelRange) -> Result<(), msgs::LightningError> {
326326
Ok(())
327327
}
328328

329-
fn handle_reply_short_channel_ids_end(&self, _their_node_id: &PublicKey, _msg: &msgs::ReplyShortChannelIdsEnd) -> Result<(), msgs::LightningError> {
329+
fn handle_reply_short_channel_ids_end(&self, _their_node_id: &PublicKey, _msg: msgs::ReplyShortChannelIdsEnd) -> Result<(), msgs::LightningError> {
330330
Ok(())
331331
}
332332

333-
fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: &msgs::QueryChannelRange) -> Result<(), msgs::LightningError> {
333+
fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: msgs::QueryChannelRange) -> Result<(), msgs::LightningError> {
334334
Ok(())
335335
}
336336

337-
fn handle_query_short_channel_ids(&self, _their_node_id: &PublicKey, _msg: &msgs::QueryShortChannelIds) -> Result<(), msgs::LightningError> {
337+
fn handle_query_short_channel_ids(&self, _their_node_id: &PublicKey, _msg: msgs::QueryShortChannelIds) -> Result<(), msgs::LightningError> {
338338
Ok(())
339339
}
340340
}

0 commit comments

Comments
 (0)