Skip to content

Commit 145e1ab

Browse files
committed
Trigger full sync only on old GossipTimestampFilters
Previously, upon receipt of a GossipTimestampFilter message, we would immediately start unloading the entire network graph on our unsuspecting peer. This commit modifies our behavior to only do so if the timestamp of the filter message is at least six hour old. Otherwise, we only send updated sync data as it comes in.
1 parent 8da30df commit 145e1ab

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

lightning/src/ln/peer_handler.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,18 @@ impl fmt::Display for PeerHandleError {
509509
}
510510
}
511511

512+
/// Internal struct for keeping track of the gossip syncing progress with a given peer
512513
enum InitSyncTracker{
514+
/// Only sync ad-hoc gossip as it comes in, do not send historical gossip.
515+
/// Upon receipt of a GossipTimestampFilter message, this is the default initial state if the
516+
/// contained timestamp is less than 6 hours old.
513517
NoSyncRequested,
518+
/// Send historical gossip starting at the given channel id, which gets incremented as the
519+
/// gossiping progresses.
520+
/// Upon receipt of a GossipTimestampFilter message, this is the default initial state if the
521+
/// contained timestamp is at least 6 hours old, and the initial channel id is set to 0.
514522
ChannelsSyncing(u64),
523+
/// Once the channel announcements and updates finish syncing, the node announcements are synced.
515524
NodesSyncing(NodeId),
516525
}
517526

@@ -1727,7 +1736,24 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
17271736
if peer_lock.their_features.as_ref().unwrap().supports_gossip_queries() &&
17281737
!peer_lock.sent_gossip_timestamp_filter {
17291738
peer_lock.sent_gossip_timestamp_filter = true;
1730-
peer_lock.sync_status = InitSyncTracker::ChannelsSyncing(0);
1739+
1740+
#[allow(unused_mut)]
1741+
let mut should_do_full_sync = true;
1742+
#[cfg(feature = "std")]
1743+
{
1744+
// Forward ad-hoc gossip if the timestamp range is less than six hours ago.
1745+
// Otherwise, do a full sync.
1746+
use std::time::{SystemTime, UNIX_EPOCH};
1747+
let full_sync_threshold = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time must be > 1970").as_secs() - 6 * 3600;
1748+
if (_msg.first_timestamp as u64) > full_sync_threshold {
1749+
should_do_full_sync = false;
1750+
}
1751+
}
1752+
if should_do_full_sync {
1753+
peer_lock.sync_status = InitSyncTracker::ChannelsSyncing(0);
1754+
} else {
1755+
peer_lock.sync_status = InitSyncTracker::NoSyncRequested;
1756+
}
17311757
}
17321758
return Ok(None);
17331759
}

0 commit comments

Comments
 (0)