Skip to content

Commit 9957835

Browse files
committed
Only request gossip mesgs from peers that support our known encodings
1 parent 3e12446 commit 9957835

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

lightning/src/routing/network_graph.rs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use chain;
2424
use chain::Access;
2525
use ln::features::{ChannelFeatures, NodeFeatures};
2626
use ln::msgs::{DecodeError, ErrorAction, Init, LightningError, RoutingMessageHandler, NetAddress, MAX_VALUE_MSAT};
27-
use ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, OptionalField};
27+
use ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, OptionalField, SUPPORTED_GOSSIP_ENCODINGS};
2828
use ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds, ReplyShortChannelIdsEnd};
2929
use ln::msgs;
3030
use util::ser::{Writeable, Readable, Writer};
@@ -251,12 +251,18 @@ impl<C: Deref , L: Deref > RoutingMessageHandler for NetGraphMsgHandler<C, L> wh
251251
/// when the final reply_scids_end message is received, though we are not
252252
/// tracking this directly.
253253
fn sync_routing_table(&self, their_node_id: &PublicKey, init_msg: &Init) {
254-
255254
// We will only perform a sync with peers that support gossip_queries.
256255
if !init_msg.features.supports_gossip_queries() {
257256
return ();
258257
}
259258

259+
// We will only perform a sync with peers that support our encodings (if they specify their
260+
// encodings)
261+
if !init_msg.gossip_compression_encodings.is_empty() &&
262+
!init_msg.gossip_compression_encodings.iter().any(|e| SUPPORTED_GOSSIP_ENCODINGS.contains(e)) {
263+
return ();
264+
}
265+
260266
// Check if we need to perform a full synchronization with this peer
261267
if !self.should_request_full_sync(their_node_id) {
262268
return ();
@@ -1067,7 +1073,8 @@ mod tests {
10671073
use routing::network_graph::{NetGraphMsgHandler, NetworkGraph, MAX_EXCESS_BYTES_FOR_RELAY};
10681074
use ln::msgs::{Init, OptionalField, RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
10691075
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate, HTLCFailChannelUpdate,
1070-
ReplyChannelRange, ReplyShortChannelIdsEnd, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
1076+
ReplyChannelRange, ReplyShortChannelIdsEnd, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT,
1077+
SUPPORTED_GOSSIP_ENCODINGS, GossipEncodingType};
10711078
use util::test_utils;
10721079
use util::logger::Logger;
10731080
use util::ser::{Readable, Writeable};
@@ -2011,13 +2018,39 @@ mod tests {
20112018

20122019
// It should ignore if gossip_queries feature is not enabled
20132020
{
2014-
let init_msg = Init { features: InitFeatures::known().clear_gossip_queries(), gossip_compression_encodings: Vec::new() };
2021+
let init_msg = Init { features: InitFeatures::known().clear_gossip_queries(), gossip_compression_encodings: SUPPORTED_GOSSIP_ENCODINGS.iter().map(|e| *e).collect() };
2022+
net_graph_msg_handler.sync_routing_table(&node_id_1, &init_msg);
2023+
let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2024+
assert_eq!(events.len(), 0);
2025+
}
2026+
2027+
// It should ignore if available encodings do not match ours
2028+
{
2029+
let init_msg = Init { features: InitFeatures::known().clear_gossip_queries(), gossip_compression_encodings: vec![GossipEncodingType::Zlib] };
20152030
net_graph_msg_handler.sync_routing_table(&node_id_1, &init_msg);
20162031
let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
20172032
assert_eq!(events.len(), 0);
20182033
}
20192034

20202035
// It should send a query_channel_message with the correct information
2036+
{
2037+
let init_msg = Init { features: InitFeatures::known(), gossip_compression_encodings: SUPPORTED_GOSSIP_ENCODINGS.iter().map(|e| *e).collect() };
2038+
net_graph_msg_handler.sync_routing_table(&node_id_1, &init_msg);
2039+
let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2040+
assert_eq!(events.len(), 1);
2041+
match &events[0] {
2042+
MessageSendEvent::SendChannelRangeQuery{ node_id, msg } => {
2043+
assert_eq!(node_id, &node_id_1);
2044+
assert_eq!(msg.chain_hash, chain_hash);
2045+
assert_eq!(msg.first_blocknum, first_blocknum);
2046+
assert_eq!(msg.number_of_blocks, number_of_blocks);
2047+
},
2048+
_ => panic!("Expected MessageSendEvent::SendChannelRangeQuery")
2049+
};
2050+
}
2051+
2052+
// It should send a query_channel_message with the correct information if the peer was old
2053+
// and didn't send its supported encodings
20212054
{
20222055
let init_msg = Init { features: InitFeatures::known(), gossip_compression_encodings: Vec::new() };
20232056
net_graph_msg_handler.sync_routing_table(&node_id_1, &init_msg);
@@ -2039,7 +2072,7 @@ mod tests {
20392072
// which should_request_full_sync will return false
20402073
{
20412074
let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2042-
let init_msg = Init { features: InitFeatures::known(), gossip_compression_encodings: Vec::new() };
2075+
let init_msg = Init { features: InitFeatures::known(), gossip_compression_encodings: SUPPORTED_GOSSIP_ENCODINGS.iter().map(|e| *e).collect() };
20432076
for n in 1..7 {
20442077
let node_privkey = &SecretKey::from_slice(&[n; 32]).unwrap();
20452078
let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);

0 commit comments

Comments
 (0)