From 67df687f08a7c9674f38b42f5496d41b22448751 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Wed, 20 Sep 2023 10:44:11 +0200 Subject: [PATCH] discovery: start sending timestamps if requested --- discovery/chan_series.go | 13 ++++++++----- discovery/syncer.go | 26 +++++++++++++++++++++++++- discovery/syncer_test.go | 3 ++- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/discovery/chan_series.go b/discovery/chan_series.go index 647d741a5..d0dc091d7 100644 --- a/discovery/chan_series.go +++ b/discovery/chan_series.go @@ -43,8 +43,8 @@ type ChannelGraphTimeSeries interface { // between the start height and the end height. The channel IDs are // grouped by their common block height. We'll use this to to a remote // peer's QueryChannelRange message. - FilterChannelRange(chain chainhash.Hash, - startHeight, endHeight uint32) ([]channeldb.BlockChannelRange, error) + FilterChannelRange(chain chainhash.Hash, startHeight, endHeight uint32, + withTimestamps bool) ([]channeldb.BlockChannelRange, error) // FetchChanAnns returns a full set of channel announcements as well as // their updates that match the set of specified short channel ID's. @@ -226,10 +226,13 @@ func (c *ChanSeries) FilterKnownChanIDs(chain chainhash.Hash, // message. // // NOTE: This is part of the ChannelGraphTimeSeries interface. -func (c *ChanSeries) FilterChannelRange(chain chainhash.Hash, - startHeight, endHeight uint32) ([]channeldb.BlockChannelRange, error) { +func (c *ChanSeries) FilterChannelRange(_ chainhash.Hash, startHeight, + endHeight uint32, withTimestamps bool) ([]channeldb.BlockChannelRange, + error) { - return c.graph.FilterChannelRange(startHeight, endHeight, false) + return c.graph.FilterChannelRange( + startHeight, endHeight, withTimestamps, + ) } // FetchChanAnns returns a full set of channel announcements as well as their diff --git a/discovery/syncer.go b/discovery/syncer.go index 35368ddf6..6b0234dd7 100644 --- a/discovery/syncer.go +++ b/discovery/syncer.go @@ -1027,12 +1027,18 @@ func (g *GossipSyncer) replyChanRangeQuery(query *lnwire.QueryChannelRange) erro "num_blocks=%v", g.cfg.peerPub[:], query.FirstBlockHeight, query.NumBlocks) + // Check if the query asked for timestamps. We will only serve + // timestamps if this has not been disabled with + // noTimestampQueryOption. + withTimestamps := query.WithTimestamps() && + !g.cfg.noTimestampQueryOption + // Next, we'll consult the time series to obtain the set of known // channel ID's that match their query. startBlock := query.FirstBlockHeight endBlock := query.LastBlockHeight() channelRanges, err := g.cfg.channelSeries.FilterChannelRange( - query.ChainHash, startBlock, endBlock, + query.ChainHash, startBlock, endBlock, withTimestamps, ) if err != nil { return err @@ -1058,9 +1064,26 @@ func (g *GossipSyncer) replyChanRangeQuery(query *lnwire.QueryChannelRange) erro complete = 1 } + var timestamps lnwire.Timestamps + if withTimestamps { + timestamps = make(lnwire.Timestamps, len(channelChunk)) + } + scids := make([]lnwire.ShortChannelID, len(channelChunk)) for i, info := range channelChunk { scids[i] = info.ShortChannelID + + if !withTimestamps { + continue + } + + timestamps[i].Timestamp1 = uint32( + info.Node1UpdateTimestamp.Unix(), + ) + + timestamps[i].Timestamp2 = uint32( + info.Node2UpdateTimestamp.Unix(), + ) } return g.cfg.sendToPeerSync(&lnwire.ReplyChannelRange{ @@ -1070,6 +1093,7 @@ func (g *GossipSyncer) replyChanRangeQuery(query *lnwire.QueryChannelRange) erro Complete: complete, EncodingType: g.cfg.encodingType, ShortChanIDs: scids, + Timestamps: timestamps, }) } diff --git a/discovery/syncer_test.go b/discovery/syncer_test.go index 23fa2eb4e..3c7835107 100644 --- a/discovery/syncer_test.go +++ b/discovery/syncer_test.go @@ -98,7 +98,8 @@ func (m *mockChannelGraphTimeSeries) FilterKnownChanIDs(chain chainhash.Hash, return <-m.filterResp, nil } func (m *mockChannelGraphTimeSeries) FilterChannelRange(chain chainhash.Hash, - startHeight, endHeight uint32) ([]channeldb.BlockChannelRange, error) { + startHeight, endHeight uint32, withTimestamps bool) ( + []channeldb.BlockChannelRange, error) { m.filterRangeReqs <- filterRangeReq{startHeight, endHeight} reply := <-m.filterRangeResp