Skip to content

Send a gossip_timestamp_filter on connect to enable gossip sync #1368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5893,6 +5893,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
&events::MessageSendEvent::SendChannelRangeQuery { .. } => false,
&events::MessageSendEvent::SendShortIdsQuery { .. } => false,
&events::MessageSendEvent::SendReplyChannelRange { .. } => false,
&events::MessageSendEvent::SendGossipTimestampFilter { .. } => false,
}
});
}
Expand Down
3 changes: 3 additions & 0 deletions lightning/src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,9 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
msg.sync_complete);
self.enqueue_message(get_peer_for_forwarding!(node_id), msg);
}
MessageSendEvent::SendGossipTimestampFilter { ref node_id, ref msg } => {
self.enqueue_message(get_peer_for_forwarding!(node_id), msg);
}
}
}

Expand Down
36 changes: 31 additions & 5 deletions lightning/src/routing/network_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use chain;
use chain::Access;
use ln::features::{ChannelFeatures, NodeFeatures};
use ln::msgs::{DecodeError, ErrorAction, Init, LightningError, RoutingMessageHandler, NetAddress, MAX_VALUE_MSAT};
use ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, OptionalField};
use ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, OptionalField, GossipTimestampFilter};
use ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds, ReplyShortChannelIdsEnd};
use ln::msgs;
use util::ser::{Writeable, Readable, Writer};
Expand Down Expand Up @@ -401,6 +401,22 @@ where C::Target: chain::Access, L::Target: Logger
return ();
}

// Send a gossip_timestamp_filter to enable gossip message receipt. Note that we have to
// use a "all timestamps" filter as sending the current timestamp would result in missing
// gossip messages that are simply sent late. We could calculate the intended filter time
// by looking at the current time and subtracting two weeks (before which we'll reject
// messages), but there's not a lot of reason to bother - our peers should be discarding
// the same messages.
let mut pending_events = self.pending_events.lock().unwrap();
pending_events.push(MessageSendEvent::SendGossipTimestampFilter {
node_id: their_node_id.clone(),
msg: GossipTimestampFilter {
chain_hash: self.network_graph.genesis_hash,
first_timestamp: 0,
timestamp_range: u32::max_value(),
},
});

// Check if we need to perform a full synchronization with this peer
if !self.should_request_full_sync(&their_node_id) {
return ();
Expand All @@ -409,7 +425,6 @@ where C::Target: chain::Access, L::Target: Logger
let first_blocknum = 0;
let number_of_blocks = 0xffffffff;
log_debug!(self.logger, "Sending query_channel_range peer={}, first_blocknum={}, number_of_blocks={}", log_pubkey!(their_node_id), first_blocknum, number_of_blocks);
let mut pending_events = self.pending_events.lock().unwrap();
pending_events.push(MessageSendEvent::SendChannelRangeQuery {
node_id: their_node_id.clone(),
msg: QueryChannelRange {
Expand Down Expand Up @@ -2280,8 +2295,17 @@ mod tests {
let init_msg = Init { features: InitFeatures::known() };
net_graph_msg_handler.peer_connected(&node_id_1, &init_msg);
let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
assert_eq!(events.len(), 1);
assert_eq!(events.len(), 2);
match &events[0] {
MessageSendEvent::SendGossipTimestampFilter{ node_id, msg } => {
assert_eq!(node_id, &node_id_1);
assert_eq!(msg.chain_hash, chain_hash);
assert_eq!(msg.first_timestamp, 0);
assert_eq!(msg.timestamp_range, u32::max_value());
},
_ => panic!("Expected MessageSendEvent::SendChannelRangeQuery")
};
match &events[1] {
MessageSendEvent::SendChannelRangeQuery{ node_id, msg } => {
assert_eq!(node_id, &node_id_1);
assert_eq!(msg.chain_hash, chain_hash);
Expand All @@ -2305,9 +2329,11 @@ mod tests {
net_graph_msg_handler.peer_connected(&node_id, &init_msg);
let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
if n <= 5 {
assert_eq!(events.len(), 1);
assert_eq!(events.len(), 2);
} else {
assert_eq!(events.len(), 0);
// Even after the we stop sending the explicit query, we should still send a
// gossip_timestamp_filter on each new connection.
assert_eq!(events.len(), 1);
}

}
Expand Down
10 changes: 9 additions & 1 deletion lightning/src/util/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,15 @@ pub enum MessageSendEvent {
node_id: PublicKey,
/// The reply_channel_range which should be sent.
msg: msgs::ReplyChannelRange,
}
},
/// Sends a timestamp filter for inbound gossip. This should be sent on each new connection to
/// enable receiving gossip messages from the peer.
SendGossipTimestampFilter {
/// The node_id of this message recipient
node_id: PublicKey,
/// The gossip_timestamp_filter which should be sent.
msg: msgs::GossipTimestampFilter,
},
}

/// A trait indicating an object may generate message send events
Expand Down