Skip to content

Commit 97c6c70

Browse files
committed
discovery: Filter ChanUpdate2 messages
1 parent 96afa6e commit 97c6c70

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

discovery/syncer.go

+44-14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/lightningnetwork/lnd/channeldb"
1515
"github.com/lightningnetwork/lnd/lnpeer"
1616
"github.com/lightningnetwork/lnd/lnwire"
17+
"github.com/lightningnetwork/lnd/tlv"
1718
"golang.org/x/time/rate"
1819
)
1920

@@ -1396,14 +1397,25 @@ func (g *GossipSyncer) FilterGossipMsgs(msgs ...msgWithSenders) {
13961397
endTime := startTime.Add(
13971398
time.Duration(g.remoteUpdateHorizon.TimestampRange) * time.Second,
13981399
)
1400+
1401+
var (
1402+
startBlock tlv.RecordT[tlv.TlvType2, uint32]
1403+
endBlock tlv.RecordT[tlv.TlvType4, uint32]
1404+
)
1405+
startBlock = g.remoteUpdateHorizon.FirstBlockHeight.UnwrapOr(startBlock)
1406+
endBlock = g.remoteUpdateHorizon.BlockRange.UnwrapOr(endBlock)
13991407
g.Unlock()
14001408

1401-
passesFilter := func(timeStamp uint32) bool {
1409+
passesTimestampFilter := func(timeStamp uint32) bool {
14021410
t := time.Unix(int64(timeStamp), 0)
14031411
return t.Equal(startTime) ||
14041412
(t.After(startTime) && t.Before(endTime))
14051413
}
14061414

1415+
passesBlockHeightFilter := func(height uint32) bool {
1416+
return height >= startBlock.Val && height < endBlock.Val
1417+
}
1418+
14071419
msgsToSend := make([]lnwire.Message, 0, len(msgs))
14081420
for _, msg := range msgs {
14091421
// If the target peer is the peer that sent us this message,
@@ -1438,36 +1450,54 @@ func (g *GossipSyncer) FilterGossipMsgs(msgs ...msgWithSenders) {
14381450
}
14391451

14401452
for _, chanUpdate := range chanUpdates {
1441-
update, ok := chanUpdate.(*lnwire.ChannelUpdate1)
1442-
if !ok {
1443-
log.Errorf("expected "+
1444-
"*lnwire.ChannelUpdate1, "+
1445-
"got: %T", update)
1453+
switch update := chanUpdate.(type) {
1454+
case *lnwire.ChannelUpdate1:
1455+
if passesTimestampFilter(
1456+
update.Timestamp,
1457+
) {
14461458

1447-
continue
1448-
}
1459+
msgsToSend = append(
1460+
msgsToSend, msg,
1461+
)
14491462

1450-
if passesFilter(update.Timestamp) {
1451-
msgsToSend = append(msgsToSend, msg)
1452-
break
1463+
break
1464+
}
1465+
case *lnwire.ChannelUpdate2:
1466+
if passesBlockHeightFilter(
1467+
update.BlockHeight.Val,
1468+
) {
1469+
1470+
msgsToSend = append(
1471+
msgsToSend, msg,
1472+
)
1473+
1474+
break
1475+
}
14531476
}
14541477
}
14551478

14561479
if len(chanUpdates) == 0 {
14571480
msgsToSend = append(msgsToSend, msg)
14581481
}
14591482

1460-
// For each channel update, we'll only send if it the timestamp
1483+
// For each channel update 1, we'll only send if the timestamp
14611484
// is between our time range.
14621485
case *lnwire.ChannelUpdate1:
1463-
if passesFilter(msg.Timestamp) {
1486+
if passesTimestampFilter(msg.Timestamp) {
1487+
msgsToSend = append(msgsToSend, msg)
1488+
}
1489+
1490+
// For each channel update 2, we'll only send if the block
1491+
// height is between our block range.
1492+
case *lnwire.ChannelUpdate2:
1493+
if passesBlockHeightFilter(msg.BlockHeight.Val) {
14641494
msgsToSend = append(msgsToSend, msg)
14651495
}
14661496

14671497
// Similarly, we only send node announcements if the update
14681498
// timestamp ifs between our set gossip filter time range.
14691499
case *lnwire.NodeAnnouncement1:
1470-
if passesFilter(msg.Timestamp) {
1500+
if passesTimestampFilter(msg.Timestamp) {
14711501
msgsToSend = append(msgsToSend, msg)
14721502
}
14731503
}

0 commit comments

Comments
 (0)