Skip to content

Commit 0af66d8

Browse files
committed
discovery: start sending block heights in range query
1 parent 49fe20c commit 0af66d8

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
@@ -627,8 +627,12 @@ func (g *GossipSyncer) channelGraphSyncer() {
627627
if g.localUpdateHorizon == nil &&
628628
syncType.IsActiveSync() {
629629

630+
startBlock := g.cfg.bestHeight()
631+
blockRange := uint32(math.MaxUint32)
632+
630633
err := g.sendGossipTimestampRange(
631634
time.Now(), math.MaxUint32,
635+
&startBlock, &blockRange,
632636
)
633637
if err != nil {
634638
log.Errorf("Unable to send update "+
@@ -695,12 +699,23 @@ func (g *GossipSyncer) replyHandler() {
695699
// sendGossipTimestampRange constructs and sets a GossipTimestampRange for the
696700
// syncer and sends it to the remote peer.
697701
func (g *GossipSyncer) sendGossipTimestampRange(firstTimestamp time.Time,
698-
timestampRange uint32) error {
702+
timestampRange uint32, firstBlock, blockRange *uint32) error {
699703

700704
endTimestamp := firstTimestamp.Add(
701705
time.Duration(timestampRange) * time.Second,
702706
)
703707

708+
if firstBlock != nil && blockRange != nil {
709+
log.Infof("GossipSyncer(%x): applying "+
710+
"gossipFilter(start-time=%v, end-time=%v, "+
711+
"start-block=%v, block-range=%v)", g.cfg.peerPub[:],
712+
firstTimestamp, endTimestamp, *firstBlock, *blockRange)
713+
} else {
714+
log.Infof("GossipSyncer(%x): applying "+
715+
"gossipFilter(start-time=%v, end-time=%v",
716+
g.cfg.peerPub[:], firstTimestamp, endTimestamp)
717+
}
718+
704719
log.Infof("GossipSyncer(%x): applying gossipFilter(start=%v, end=%v)",
705720
g.cfg.peerPub[:], firstTimestamp, endTimestamp)
706721

@@ -710,11 +725,28 @@ func (g *GossipSyncer) sendGossipTimestampRange(firstTimestamp time.Time,
710725
TimestampRange: timestampRange,
711726
}
712727

728+
if firstBlock != nil {
729+
first := tlv.ZeroRecordT[tlv.TlvType2, uint32]()
730+
first.Val = *firstBlock
731+
732+
localUpdateHorizon.FirstBlockHeight = tlv.SomeRecordT(first)
733+
}
734+
735+
if blockRange != nil {
736+
bRange := tlv.ZeroRecordT[tlv.TlvType4, uint32]()
737+
bRange.Val = *blockRange
738+
739+
localUpdateHorizon.BlockRange = tlv.SomeRecordT(bRange)
740+
}
741+
713742
if err := g.cfg.sendToPeer(localUpdateHorizon); err != nil {
714743
return err
715744
}
716745

717-
if firstTimestamp == zeroTimestamp && timestampRange == 0 {
746+
noTimeStamps := firstTimestamp == zeroTimestamp && timestampRange == 0
747+
noBlockHeights := firstBlock == nil && blockRange == nil
748+
749+
if noTimeStamps && noBlockHeights {
718750
g.localUpdateHorizon = nil
719751
} else {
720752
g.localUpdateHorizon = localUpdateHorizon
@@ -1624,6 +1656,8 @@ func (g *GossipSyncer) handleSyncTransition(req *syncTransitionReq) error {
16241656
var (
16251657
firstTimestamp time.Time
16261658
timestampRange uint32
1659+
firstBlock *uint32
1660+
blockRange *uint32
16271661
)
16281662

16291663
switch req.newSyncType {
@@ -1632,6 +1666,10 @@ func (g *GossipSyncer) handleSyncTransition(req *syncTransitionReq) error {
16321666
case ActiveSync, PinnedSync:
16331667
firstTimestamp = time.Now()
16341668
timestampRange = math.MaxUint32
1669+
bestHeight := g.cfg.bestHeight()
1670+
firstBlock = &bestHeight
1671+
heightRange := uint32(math.MaxUint32)
1672+
blockRange = &heightRange
16351673

16361674
// If a PassiveSync transition has been requested, then we should no
16371675
// longer receive any new updates from the remote peer. We can do this
@@ -1646,7 +1684,9 @@ func (g *GossipSyncer) handleSyncTransition(req *syncTransitionReq) error {
16461684
req.newSyncType)
16471685
}
16481686

1649-
err := g.sendGossipTimestampRange(firstTimestamp, timestampRange)
1687+
err := g.sendGossipTimestampRange(
1688+
firstTimestamp, timestampRange, firstBlock, blockRange,
1689+
)
16501690
if err != nil {
16511691
return fmt.Errorf("unable to send local update horizon: %v", err)
16521692
}

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

@@ -2177,12 +2178,26 @@ func TestGossipSyncerSyncTransitions(t *testing.T) {
21772178
// send out a message that indicates we want
21782179
// all the updates from here on.
21792180
firstTimestamp := uint32(time.Now().Unix())
2180-
assertMsgSent(
2181-
t, mChan, &lnwire.GossipTimestampRange{
2182-
FirstTimestamp: firstTimestamp,
2183-
TimestampRange: math.MaxUint32,
2184-
},
2185-
)
2181+
firstBlock := tlv.ZeroRecordT[
2182+
tlv.TlvType2, uint32,
2183+
]()
2184+
firstBlock.Val = uint32(latestKnownHeight)
2185+
2186+
blockRange := tlv.ZeroRecordT[
2187+
tlv.TlvType4, uint32,
2188+
]()
2189+
blockRange.Val = uint32(math.MaxUint32)
2190+
2191+
assertMsgSent(t, mChan, &lnwire.GossipTimestampRange{ //nolint:lll
2192+
FirstTimestamp: firstTimestamp,
2193+
TimestampRange: math.MaxUint32,
2194+
FirstBlockHeight: tlv.SomeRecordT(
2195+
firstBlock,
2196+
),
2197+
BlockRange: tlv.SomeRecordT(
2198+
blockRange,
2199+
),
2200+
})
21862201

21872202
// When transitioning from active to passive, we
21882203
// should expect to see a new local update
@@ -2217,10 +2232,26 @@ func TestGossipSyncerSyncTransitions(t *testing.T) {
22172232
// horizon sent to the remote peer indicating
22182233
// that it would like to receive any future
22192234
// updates.
2235+
firstBlock := tlv.ZeroRecordT[
2236+
tlv.TlvType2, uint32,
2237+
]()
2238+
firstBlock.Val = uint32(latestKnownHeight)
2239+
2240+
blockRange := tlv.ZeroRecordT[
2241+
tlv.TlvType4, uint32,
2242+
]()
2243+
blockRange.Val = uint32(math.MaxUint32)
2244+
22202245
firstTimestamp := uint32(time.Now().Unix())
22212246
assertMsgSent(t, msgChan, &lnwire.GossipTimestampRange{
22222247
FirstTimestamp: firstTimestamp,
22232248
TimestampRange: math.MaxUint32,
2249+
FirstBlockHeight: tlv.SomeRecordT(
2250+
firstBlock,
2251+
),
2252+
BlockRange: tlv.SomeRecordT(
2253+
blockRange,
2254+
),
22242255
})
22252256

22262257
syncState := g.syncState()

0 commit comments

Comments
 (0)