Skip to content

Commit 6e8d8e1

Browse files
committed
discovery: start sending block heights in range query
1 parent 332cad3 commit 6e8d8e1

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

discovery/syncer.go

+43-3
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,12 @@ func (g *GossipSyncer) channelGraphSyncer() {
622622
if g.localUpdateHorizon == nil &&
623623
syncType.IsActiveSync() {
624624

625+
startBlock := g.cfg.bestHeight()
626+
blockRange := uint32(math.MaxUint32)
627+
625628
err := g.sendGossipTimestampRange(
626629
time.Now(), math.MaxUint32,
630+
&startBlock, &blockRange,
627631
)
628632
if err != nil {
629633
log.Errorf("Unable to send update "+
@@ -690,12 +694,23 @@ func (g *GossipSyncer) replyHandler() {
690694
// sendGossipTimestampRange constructs and sets a GossipTimestampRange for the
691695
// syncer and sends it to the remote peer.
692696
func (g *GossipSyncer) sendGossipTimestampRange(firstTimestamp time.Time,
693-
timestampRange uint32) error {
697+
timestampRange uint32, firstBlock, blockRange *uint32) error {
694698

695699
endTimestamp := firstTimestamp.Add(
696700
time.Duration(timestampRange) * time.Second,
697701
)
698702

703+
if firstBlock != nil && blockRange != nil {
704+
log.Infof("GossipSyncer(%x): applying "+
705+
"gossipFilter(start-time=%v, end-time=%v, "+
706+
"start-block=%v, block-range=%v)", g.cfg.peerPub[:],
707+
firstTimestamp, endTimestamp, *firstBlock, *blockRange)
708+
} else {
709+
log.Infof("GossipSyncer(%x): applying "+
710+
"gossipFilter(start-time=%v, end-time=%v",
711+
g.cfg.peerPub[:], firstTimestamp, endTimestamp)
712+
}
713+
699714
log.Infof("GossipSyncer(%x): applying gossipFilter(start=%v, end=%v)",
700715
g.cfg.peerPub[:], firstTimestamp, endTimestamp)
701716

@@ -705,11 +720,28 @@ func (g *GossipSyncer) sendGossipTimestampRange(firstTimestamp time.Time,
705720
TimestampRange: timestampRange,
706721
}
707722

723+
if firstBlock != nil {
724+
first := tlv.ZeroRecordT[tlv.TlvType2, uint32]()
725+
first.Val = *firstBlock
726+
727+
localUpdateHorizon.FirstBlockHeight = tlv.SomeRecordT(first)
728+
}
729+
730+
if blockRange != nil {
731+
bRange := tlv.ZeroRecordT[tlv.TlvType4, uint32]()
732+
bRange.Val = *blockRange
733+
734+
localUpdateHorizon.BlockRange = tlv.SomeRecordT(bRange)
735+
}
736+
708737
if err := g.cfg.sendToPeer(localUpdateHorizon); err != nil {
709738
return err
710739
}
711740

712-
if firstTimestamp == zeroTimestamp && timestampRange == 0 {
741+
noTimeStamps := firstTimestamp == zeroTimestamp && timestampRange == 0
742+
noBlockHeights := firstBlock == nil && blockRange == nil
743+
744+
if noTimeStamps && noBlockHeights {
713745
g.localUpdateHorizon = nil
714746
} else {
715747
g.localUpdateHorizon = localUpdateHorizon
@@ -1661,6 +1693,8 @@ func (g *GossipSyncer) handleSyncTransition(req *syncTransitionReq) error {
16611693
var (
16621694
firstTimestamp time.Time
16631695
timestampRange uint32
1696+
firstBlock *uint32
1697+
blockRange *uint32
16641698
)
16651699

16661700
switch req.newSyncType {
@@ -1669,6 +1703,10 @@ func (g *GossipSyncer) handleSyncTransition(req *syncTransitionReq) error {
16691703
case ActiveSync, PinnedSync:
16701704
firstTimestamp = time.Now()
16711705
timestampRange = math.MaxUint32
1706+
bestHeight := g.cfg.bestHeight()
1707+
firstBlock = &bestHeight
1708+
heightRange := uint32(math.MaxUint32)
1709+
blockRange = &heightRange
16721710

16731711
// If a PassiveSync transition has been requested, then we should no
16741712
// longer receive any new updates from the remote peer. We can do this
@@ -1683,7 +1721,9 @@ func (g *GossipSyncer) handleSyncTransition(req *syncTransitionReq) error {
16831721
req.newSyncType)
16841722
}
16851723

1686-
err := g.sendGossipTimestampRange(firstTimestamp, timestampRange)
1724+
err := g.sendGossipTimestampRange(
1725+
firstTimestamp, timestampRange, firstBlock, blockRange,
1726+
)
16871727
if err != nil {
16881728
return fmt.Errorf("unable to send local update horizon: %w",
16891729
err)

discovery/syncer_test.go

+37-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/davecgh/go-spew/spew"
1616
"github.com/lightningnetwork/lnd/channeldb"
1717
"github.com/lightningnetwork/lnd/lnwire"
18+
"github.com/lightningnetwork/lnd/tlv"
1819
"github.com/stretchr/testify/require"
1920
)
2021

@@ -2258,12 +2259,26 @@ func TestGossipSyncerSyncTransitions(t *testing.T) {
22582259
// send out a message that indicates we want
22592260
// all the updates from here on.
22602261
firstTimestamp := uint32(time.Now().Unix())
2261-
assertMsgSent(
2262-
t, mChan, &lnwire.GossipTimestampRange{
2263-
FirstTimestamp: firstTimestamp,
2264-
TimestampRange: math.MaxUint32,
2265-
},
2266-
)
2262+
firstBlock := tlv.ZeroRecordT[
2263+
tlv.TlvType2, uint32,
2264+
]()
2265+
firstBlock.Val = uint32(latestKnownHeight)
2266+
2267+
blockRange := tlv.ZeroRecordT[
2268+
tlv.TlvType4, uint32,
2269+
]()
2270+
blockRange.Val = uint32(math.MaxUint32)
2271+
2272+
assertMsgSent(t, mChan, &lnwire.GossipTimestampRange{ //nolint:lll
2273+
FirstTimestamp: firstTimestamp,
2274+
TimestampRange: math.MaxUint32,
2275+
FirstBlockHeight: tlv.SomeRecordT(
2276+
firstBlock,
2277+
),
2278+
BlockRange: tlv.SomeRecordT(
2279+
blockRange,
2280+
),
2281+
})
22672282

22682283
// When transitioning from active to passive, we
22692284
// should expect to see a new local update
@@ -2298,10 +2313,26 @@ func TestGossipSyncerSyncTransitions(t *testing.T) {
22982313
// horizon sent to the remote peer indicating
22992314
// that it would like to receive any future
23002315
// updates.
2316+
firstBlock := tlv.ZeroRecordT[
2317+
tlv.TlvType2, uint32,
2318+
]()
2319+
firstBlock.Val = uint32(latestKnownHeight)
2320+
2321+
blockRange := tlv.ZeroRecordT[
2322+
tlv.TlvType4, uint32,
2323+
]()
2324+
blockRange.Val = uint32(math.MaxUint32)
2325+
23012326
firstTimestamp := uint32(time.Now().Unix())
23022327
assertMsgSent(t, msgChan, &lnwire.GossipTimestampRange{
23032328
FirstTimestamp: firstTimestamp,
23042329
TimestampRange: math.MaxUint32,
2330+
FirstBlockHeight: tlv.SomeRecordT(
2331+
firstBlock,
2332+
),
2333+
BlockRange: tlv.SomeRecordT(
2334+
blockRange,
2335+
),
23052336
})
23062337

23072338
syncState := g.syncState()

0 commit comments

Comments
 (0)