Skip to content

Commit 3920157

Browse files
authored
Merge pull request #1368 from TheBlueMatt/2022-03-fix-post-start-sync
Send a gossip_timestamp_filter on connect to enable gossip sync
2 parents 996d3d8 + 3cca221 commit 3920157

File tree

7 files changed

+53
-16
lines changed

7 files changed

+53
-16
lines changed

lightning-net-tokio/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ mod tests {
496496
fn handle_channel_update(&self, _msg: &ChannelUpdate) -> Result<bool, LightningError> { Ok(false) }
497497
fn get_next_channel_announcements(&self, _starting_point: u64, _batch_amount: u8) -> Vec<(ChannelAnnouncement, Option<ChannelUpdate>, Option<ChannelUpdate>)> { Vec::new() }
498498
fn get_next_node_announcements(&self, _starting_point: Option<&PublicKey>, _batch_amount: u8) -> Vec<NodeAnnouncement> { Vec::new() }
499-
fn sync_routing_table(&self, _their_node_id: &PublicKey, _init_msg: &Init) { }
499+
fn peer_connected(&self, _their_node_id: &PublicKey, _init_msg: &Init) { }
500500
fn handle_reply_channel_range(&self, _their_node_id: &PublicKey, _msg: ReplyChannelRange) -> Result<(), LightningError> { Ok(()) }
501501
fn handle_reply_short_channel_ids_end(&self, _their_node_id: &PublicKey, _msg: ReplyShortChannelIdsEnd) -> Result<(), LightningError> { Ok(()) }
502502
fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: QueryChannelRange) -> Result<(), LightningError> { Ok(()) }

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5899,6 +5899,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
58995899
&events::MessageSendEvent::SendChannelRangeQuery { .. } => false,
59005900
&events::MessageSendEvent::SendShortIdsQuery { .. } => false,
59015901
&events::MessageSendEvent::SendReplyChannelRange { .. } => false,
5902+
&events::MessageSendEvent::SendGossipTimestampFilter { .. } => false,
59025903
}
59035904
});
59045905
}

lightning/src/ln/msgs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ pub trait RoutingMessageHandler : MessageSendEventsProvider {
886886
/// Called when a connection is established with a peer. This can be used to
887887
/// perform routing table synchronization using a strategy defined by the
888888
/// implementor.
889-
fn sync_routing_table(&self, their_node_id: &PublicKey, init: &Init);
889+
fn peer_connected(&self, their_node_id: &PublicKey, init: &Init);
890890
/// Handles the reply of a query we initiated to learn about channels
891891
/// for a given range of blocks. We can expect to receive one or more
892892
/// replies to a single query.

lightning/src/ln/peer_handler.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl RoutingMessageHandler for IgnoringMessageHandler {
6969
fn get_next_channel_announcements(&self, _starting_point: u64, _batch_amount: u8) ->
7070
Vec<(msgs::ChannelAnnouncement, Option<msgs::ChannelUpdate>, Option<msgs::ChannelUpdate>)> { Vec::new() }
7171
fn get_next_node_announcements(&self, _starting_point: Option<&PublicKey>, _batch_amount: u8) -> Vec<msgs::NodeAnnouncement> { Vec::new() }
72-
fn sync_routing_table(&self, _their_node_id: &PublicKey, _init: &msgs::Init) {}
72+
fn peer_connected(&self, _their_node_id: &PublicKey, _init: &msgs::Init) {}
7373
fn handle_reply_channel_range(&self, _their_node_id: &PublicKey, _msg: msgs::ReplyChannelRange) -> Result<(), LightningError> { Ok(()) }
7474
fn handle_reply_short_channel_ids_end(&self, _their_node_id: &PublicKey, _msg: msgs::ReplyShortChannelIdsEnd) -> Result<(), LightningError> { Ok(()) }
7575
fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: msgs::QueryChannelRange) -> Result<(), LightningError> { Ok(()) }
@@ -1018,7 +1018,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
10181018
return Err(PeerHandleError{ no_connection_possible: true }.into());
10191019
}
10201020

1021-
self.message_handler.route_handler.sync_routing_table(&peer.their_node_id.unwrap(), &msg);
1021+
self.message_handler.route_handler.peer_connected(&peer.their_node_id.unwrap(), &msg);
10221022

10231023
self.message_handler.chan_handler.peer_connected(&peer.their_node_id.unwrap(), &msg);
10241024
peer.their_features = Some(msg.features);
@@ -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: 35 additions & 10 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};
@@ -395,13 +395,28 @@ where C::Target: chain::Access, L::Target: Logger
395395
/// to request gossip messages for each channel. The sync is considered complete
396396
/// when the final reply_scids_end message is received, though we are not
397397
/// tracking this directly.
398-
fn sync_routing_table(&self, their_node_id: &PublicKey, init_msg: &Init) {
399-
398+
fn peer_connected(&self, their_node_id: &PublicKey, init_msg: &Init) {
400399
// We will only perform a sync with peers that support gossip_queries.
401400
if !init_msg.features.supports_gossip_queries() {
402401
return ();
403402
}
404403

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+
405420
// Check if we need to perform a full synchronization with this peer
406421
if !self.should_request_full_sync(&their_node_id) {
407422
return ();
@@ -410,7 +425,6 @@ where C::Target: chain::Access, L::Target: Logger
410425
let first_blocknum = 0;
411426
let number_of_blocks = 0xffffffff;
412427
log_debug!(self.logger, "Sending query_channel_range peer={}, first_blocknum={}, number_of_blocks={}", log_pubkey!(their_node_id), first_blocknum, number_of_blocks);
413-
let mut pending_events = self.pending_events.lock().unwrap();
414428
pending_events.push(MessageSendEvent::SendChannelRangeQuery {
415429
node_id: their_node_id.clone(),
416430
msg: QueryChannelRange {
@@ -2271,18 +2285,27 @@ mod tests {
22712285
// It should ignore if gossip_queries feature is not enabled
22722286
{
22732287
let init_msg = Init { features: InitFeatures::known().clear_gossip_queries() };
2274-
net_graph_msg_handler.sync_routing_table(&node_id_1, &init_msg);
2288+
net_graph_msg_handler.peer_connected(&node_id_1, &init_msg);
22752289
let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
22762290
assert_eq!(events.len(), 0);
22772291
}
22782292

22792293
// It should send a query_channel_message with the correct information
22802294
{
22812295
let init_msg = Init { features: InitFeatures::known() };
2282-
net_graph_msg_handler.sync_routing_table(&node_id_1, &init_msg);
2296+
net_graph_msg_handler.peer_connected(&node_id_1, &init_msg);
22832297
let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2284-
assert_eq!(events.len(), 1);
2298+
assert_eq!(events.len(), 2);
22852299
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] {
22862309
MessageSendEvent::SendChannelRangeQuery{ node_id, msg } => {
22872310
assert_eq!(node_id, &node_id_1);
22882311
assert_eq!(msg.chain_hash, chain_hash);
@@ -2303,12 +2326,14 @@ mod tests {
23032326
for n in 1..7 {
23042327
let node_privkey = &SecretKey::from_slice(&[n; 32]).unwrap();
23052328
let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
2306-
net_graph_msg_handler.sync_routing_table(&node_id, &init_msg);
2329+
net_graph_msg_handler.peer_connected(&node_id, &init_msg);
23072330
let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
23082331
if n <= 5 {
2309-
assert_eq!(events.len(), 1);
2332+
assert_eq!(events.len(), 2);
23102333
} else {
2311-
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);
23122337
}
23132338

23142339
}

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

lightning/src/util/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
384384
Vec::new()
385385
}
386386

387-
fn sync_routing_table(&self, _their_node_id: &PublicKey, _init_msg: &msgs::Init) {}
387+
fn peer_connected(&self, _their_node_id: &PublicKey, _init_msg: &msgs::Init) {}
388388

389389
fn handle_reply_channel_range(&self, _their_node_id: &PublicKey, _msg: msgs::ReplyChannelRange) -> Result<(), msgs::LightningError> {
390390
Ok(())

0 commit comments

Comments
 (0)