Skip to content

Commit 5d2291b

Browse files
committed
discovery: start sending block heights in range query
1 parent 5531356 commit 5d2291b

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

discovery/syncer.go

+38-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/btcsuite/btcd/chaincfg/chainhash"
14+
"github.com/lightningnetwork/lnd/fn"
1415
"github.com/lightningnetwork/lnd/lnpeer"
1516
"github.com/lightningnetwork/lnd/lnwire"
1617
"golang.org/x/time/rate"
@@ -604,8 +605,12 @@ func (g *GossipSyncer) channelGraphSyncer() {
604605
if g.localUpdateHorizon == nil &&
605606
syncType.IsActiveSync() {
606607

608+
startBlock := g.cfg.bestHeight()
609+
blockRange := uint32(math.MaxUint32)
610+
607611
err := g.sendGossipTimestampRange(
608612
time.Now(), math.MaxUint32,
613+
&startBlock, &blockRange,
609614
)
610615
if err != nil {
611616
log.Errorf("Unable to send update "+
@@ -672,12 +677,23 @@ func (g *GossipSyncer) replyHandler() {
672677
// sendGossipTimestampRange constructs and sets a GossipTimestampRange for the
673678
// syncer and sends it to the remote peer.
674679
func (g *GossipSyncer) sendGossipTimestampRange(firstTimestamp time.Time,
675-
timestampRange uint32) error {
680+
timestampRange uint32, firstBlock, blockRange *uint32) error {
676681

677682
endTimestamp := firstTimestamp.Add(
678683
time.Duration(timestampRange) * time.Second,
679684
)
680685

686+
if firstBlock != nil && blockRange != nil {
687+
log.Infof("GossipSyncer(%x): applying "+
688+
"gossipFilter(start-time=%v, end-time=%v, "+
689+
"start-block=%v, block-range=%v)", g.cfg.peerPub[:],
690+
firstTimestamp, endTimestamp, *firstBlock, *blockRange)
691+
} else {
692+
log.Infof("GossipSyncer(%x): applying "+
693+
"gossipFilter(start-time=%v, end-time=%v",
694+
g.cfg.peerPub[:], firstTimestamp, endTimestamp)
695+
}
696+
681697
log.Infof("GossipSyncer(%x): applying gossipFilter(start=%v, end=%v)",
682698
g.cfg.peerPub[:], firstTimestamp, endTimestamp)
683699

@@ -687,11 +703,22 @@ func (g *GossipSyncer) sendGossipTimestampRange(firstTimestamp time.Time,
687703
TimestampRange: timestampRange,
688704
}
689705

706+
if firstBlock != nil {
707+
localUpdateHorizon.FirstBlockHeight = fn.Some(*firstBlock)
708+
}
709+
710+
if blockRange != nil {
711+
localUpdateHorizon.BlockRange = fn.Some(*blockRange)
712+
}
713+
690714
if err := g.cfg.sendToPeer(localUpdateHorizon); err != nil {
691715
return err
692716
}
693717

694-
if firstTimestamp == zeroTimestamp && timestampRange == 0 {
718+
noTimeStamps := firstTimestamp == zeroTimestamp && timestampRange == 0
719+
noBlockHeights := firstBlock == nil && blockRange == nil
720+
721+
if noTimeStamps && noBlockHeights {
695722
g.localUpdateHorizon = nil
696723
} else {
697724
g.localUpdateHorizon = localUpdateHorizon
@@ -1511,6 +1538,8 @@ func (g *GossipSyncer) handleSyncTransition(req *syncTransitionReq) error {
15111538
var (
15121539
firstTimestamp time.Time
15131540
timestampRange uint32
1541+
firstBlock *uint32
1542+
blockRange *uint32
15141543
)
15151544

15161545
switch req.newSyncType {
@@ -1519,6 +1548,10 @@ func (g *GossipSyncer) handleSyncTransition(req *syncTransitionReq) error {
15191548
case ActiveSync, PinnedSync:
15201549
firstTimestamp = time.Now()
15211550
timestampRange = math.MaxUint32
1551+
bestHeight := g.cfg.bestHeight()
1552+
firstBlock = &bestHeight
1553+
heightRange := uint32(math.MaxUint32)
1554+
blockRange = &heightRange
15221555

15231556
// If a PassiveSync transition has been requested, then we should no
15241557
// longer receive any new updates from the remote peer. We can do this
@@ -1533,7 +1566,9 @@ func (g *GossipSyncer) handleSyncTransition(req *syncTransitionReq) error {
15331566
req.newSyncType)
15341567
}
15351568

1536-
err := g.sendGossipTimestampRange(firstTimestamp, timestampRange)
1569+
err := g.sendGossipTimestampRange(
1570+
firstTimestamp, timestampRange, firstBlock, blockRange,
1571+
)
15371572
if err != nil {
15381573
return fmt.Errorf("unable to send local update horizon: %v", err)
15391574
}

discovery/syncer_test.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/btcsuite/btcd/chaincfg/chainhash"
1515
"github.com/davecgh/go-spew/spew"
1616
"github.com/lightningnetwork/lnd/channeldb"
17+
"github.com/lightningnetwork/lnd/fn"
1718
"github.com/lightningnetwork/lnd/lnwire"
1819
"github.com/stretchr/testify/require"
1920
)
@@ -2153,12 +2154,14 @@ func TestGossipSyncerSyncTransitions(t *testing.T) {
21532154
// send out a message that indicates we want
21542155
// all the updates from here on.
21552156
firstTimestamp := uint32(time.Now().Unix())
2156-
assertMsgSent(
2157-
t, mChan, &lnwire.GossipTimestampRange{
2158-
FirstTimestamp: firstTimestamp,
2159-
TimestampRange: math.MaxUint32,
2160-
},
2161-
)
2157+
firstBlock := uint32(latestKnownHeight)
2158+
blockRange := uint32(math.MaxUint32)
2159+
assertMsgSent(t, mChan, &lnwire.GossipTimestampRange{
2160+
FirstTimestamp: firstTimestamp,
2161+
TimestampRange: math.MaxUint32,
2162+
FirstBlockHeight: fn.Some(firstBlock),
2163+
BlockRange: fn.Some(blockRange),
2164+
})
21622165

21632166
// When transitioning from active to passive, we
21642167
// should expect to see a new local update
@@ -2193,10 +2196,14 @@ func TestGossipSyncerSyncTransitions(t *testing.T) {
21932196
// horizon sent to the remote peer indicating
21942197
// that it would like to receive any future
21952198
// updates.
2199+
firstBlock := uint32(latestKnownHeight)
2200+
blockRange := uint32(math.MaxUint32)
21962201
firstTimestamp := uint32(time.Now().Unix())
21972202
assertMsgSent(t, msgChan, &lnwire.GossipTimestampRange{
2198-
FirstTimestamp: firstTimestamp,
2199-
TimestampRange: math.MaxUint32,
2203+
FirstTimestamp: firstTimestamp,
2204+
TimestampRange: math.MaxUint32,
2205+
FirstBlockHeight: fn.Some(firstBlock),
2206+
BlockRange: fn.Some(blockRange),
22002207
})
22012208

22022209
syncState := g.syncState()

0 commit comments

Comments
 (0)