Skip to content

Commit 3cca221

Browse files
committed
Send a gossip_timestamp_filter on connect to enable gossip sync
On connection, if our peer supports gossip queries, and we never send a `gossip_timestamp_filter`, our peer is supposed to never send us gossip outside of explicit queries. Thus, we'll end up always having stale gossip information after the first few connections we make to peers. The solution is to send a dummy `gossip_timestamp_filter` immediately after connecting to peers.
1 parent b1fb7fd commit 3cca221

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5893,6 +5893,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
58935893
&events::MessageSendEvent::SendChannelRangeQuery { .. } => false,
58945894
&events::MessageSendEvent::SendShortIdsQuery { .. } => false,
58955895
&events::MessageSendEvent::SendReplyChannelRange { .. } => false,
5896+
&events::MessageSendEvent::SendGossipTimestampFilter { .. } => false,
58965897
}
58975898
});
58985899
}

lightning/src/ln/peer_handler.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,9 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
14771477
msg.sync_complete);
14781478
self.enqueue_message(get_peer_for_forwarding!(node_id), msg);
14791479
}
1480+
MessageSendEvent::SendGossipTimestampFilter { ref node_id, ref msg } => {
1481+
self.enqueue_message(get_peer_for_forwarding!(node_id), msg);
1482+
}
14801483
}
14811484
}
14821485

lightning/src/routing/network_graph.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use chain;
2525
use chain::Access;
2626
use ln::features::{ChannelFeatures, NodeFeatures};
2727
use ln::msgs::{DecodeError, ErrorAction, Init, LightningError, RoutingMessageHandler, NetAddress, MAX_VALUE_MSAT};
28-
use ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, OptionalField};
28+
use ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, OptionalField, GossipTimestampFilter};
2929
use ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds, ReplyShortChannelIdsEnd};
3030
use ln::msgs;
3131
use util::ser::{Writeable, Readable, Writer};
@@ -401,6 +401,22 @@ where C::Target: chain::Access, L::Target: Logger
401401
return ();
402402
}
403403

404+
// Send a gossip_timestamp_filter to enable gossip message receipt. Note that we have to
405+
// use a "all timestamps" filter as sending the current timestamp would result in missing
406+
// gossip messages that are simply sent late. We could calculate the intended filter time
407+
// by looking at the current time and subtracting two weeks (before which we'll reject
408+
// messages), but there's not a lot of reason to bother - our peers should be discarding
409+
// the same messages.
410+
let mut pending_events = self.pending_events.lock().unwrap();
411+
pending_events.push(MessageSendEvent::SendGossipTimestampFilter {
412+
node_id: their_node_id.clone(),
413+
msg: GossipTimestampFilter {
414+
chain_hash: self.network_graph.genesis_hash,
415+
first_timestamp: 0,
416+
timestamp_range: u32::max_value(),
417+
},
418+
});
419+
404420
// Check if we need to perform a full synchronization with this peer
405421
if !self.should_request_full_sync(&their_node_id) {
406422
return ();
@@ -409,7 +425,6 @@ where C::Target: chain::Access, L::Target: Logger
409425
let first_blocknum = 0;
410426
let number_of_blocks = 0xffffffff;
411427
log_debug!(self.logger, "Sending query_channel_range peer={}, first_blocknum={}, number_of_blocks={}", log_pubkey!(their_node_id), first_blocknum, number_of_blocks);
412-
let mut pending_events = self.pending_events.lock().unwrap();
413428
pending_events.push(MessageSendEvent::SendChannelRangeQuery {
414429
node_id: their_node_id.clone(),
415430
msg: QueryChannelRange {
@@ -2280,8 +2295,17 @@ mod tests {
22802295
let init_msg = Init { features: InitFeatures::known() };
22812296
net_graph_msg_handler.peer_connected(&node_id_1, &init_msg);
22822297
let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2283-
assert_eq!(events.len(), 1);
2298+
assert_eq!(events.len(), 2);
22842299
match &events[0] {
2300+
MessageSendEvent::SendGossipTimestampFilter{ node_id, msg } => {
2301+
assert_eq!(node_id, &node_id_1);
2302+
assert_eq!(msg.chain_hash, chain_hash);
2303+
assert_eq!(msg.first_timestamp, 0);
2304+
assert_eq!(msg.timestamp_range, u32::max_value());
2305+
},
2306+
_ => panic!("Expected MessageSendEvent::SendChannelRangeQuery")
2307+
};
2308+
match &events[1] {
22852309
MessageSendEvent::SendChannelRangeQuery{ node_id, msg } => {
22862310
assert_eq!(node_id, &node_id_1);
22872311
assert_eq!(msg.chain_hash, chain_hash);
@@ -2305,9 +2329,11 @@ mod tests {
23052329
net_graph_msg_handler.peer_connected(&node_id, &init_msg);
23062330
let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
23072331
if n <= 5 {
2308-
assert_eq!(events.len(), 1);
2332+
assert_eq!(events.len(), 2);
23092333
} else {
2310-
assert_eq!(events.len(), 0);
2334+
// Even after the we stop sending the explicit query, we should still send a
2335+
// gossip_timestamp_filter on each new connection.
2336+
assert_eq!(events.len(), 1);
23112337
}
23122338

23132339
}

lightning/src/util/events.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,15 @@ pub enum MessageSendEvent {
909909
node_id: PublicKey,
910910
/// The reply_channel_range which should be sent.
911911
msg: msgs::ReplyChannelRange,
912-
}
912+
},
913+
/// Sends a timestamp filter for inbound gossip. This should be sent on each new connection to
914+
/// enable receiving gossip messages from the peer.
915+
SendGossipTimestampFilter {
916+
/// The node_id of this message recipient
917+
node_id: PublicKey,
918+
/// The gossip_timestamp_filter which should be sent.
919+
msg: msgs::GossipTimestampFilter,
920+
},
913921
}
914922

915923
/// A trait indicating an object may generate message send events

0 commit comments

Comments
 (0)