• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 21999477495

13 Feb 2026 07:11PM UTC coverage: 55.074% (-10.0%) from 65.12%
21999477495

Pull #10581

github

web-flow
Merge b1b1dee16 into f9e382560
Pull Request #10581: discovery: enforce strict validation of peer gossip messages

21 of 44 new or added lines in 1 file covered. (47.73%)

30052 existing lines in 466 files now uncovered.

100589 of 182643 relevant lines covered (55.07%)

1.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.25
/graph/db/reject_cache.go
1
package graphdb
2

3
import (
4
        "time"
5

6
        "github.com/lightningnetwork/lnd/lnwire"
7
)
8

9
// rejectFlags is a compact representation of various metadata stored by the
10
// reject cache about a particular channel.
11
type rejectFlags uint8
12

13
const (
14
        // rejectFlagExists is a flag indicating whether the channel exists,
15
        // i.e. the channel is open and has a recent channel update. If this
16
        // flag is not set, the channel is either a zombie or unknown.
17
        rejectFlagExists rejectFlags = 1 << iota
18

19
        // rejectFlagZombie is a flag indicating whether the channel is a
20
        // zombie, i.e. the channel is open but has no recent channel updates.
21
        rejectFlagZombie
22
)
23

24
// packRejectFlags computes the rejectFlags corresponding to the passed boolean
25
// values indicating whether the edge exists or is a zombie.
26
func packRejectFlags(exists, isZombie bool) rejectFlags {
3✔
27
        var flags rejectFlags
3✔
28
        if exists {
6✔
29
                flags |= rejectFlagExists
3✔
30
        }
3✔
31
        if isZombie {
3✔
UNCOV
32
                flags |= rejectFlagZombie
×
UNCOV
33
        }
×
34

35
        return flags
3✔
36
}
37

38
// unpack returns the booleans packed into the rejectFlags. The first indicates
39
// if the edge exists in our graph, the second indicates if the edge is a
40
// zombie.
41
func (f rejectFlags) unpack() (bool, bool) {
3✔
42
        return f&rejectFlagExists == rejectFlagExists,
3✔
43
                f&rejectFlagZombie == rejectFlagZombie
3✔
44
}
3✔
45

46
// rejectCacheEntry caches frequently accessed information about a channel,
47
// including the timestamps of its latest edge policies and whether or not the
48
// channel exists in the graph.
49
type rejectCacheEntry struct {
50
        // upd{1,2}Time are Unix timestamps for v1 policies.
51
        upd1Time int64
52
        upd2Time int64
53

54
        // upd{1,2}BlockHeight are the last known block heights for v2
55
        // policies.
56
        upd1BlockHeight uint32
57
        upd2BlockHeight uint32
58

59
        flags rejectFlags
60
}
61

62
func newRejectCacheEntryV1(upd1, upd2 time.Time, exists,
63
        isZombie bool) rejectCacheEntry {
×
64

×
65
        return rejectCacheEntry{
×
66
                upd1Time: upd1.Unix(),
×
67
                upd2Time: upd2.Unix(),
×
68
                flags:    packRejectFlags(exists, isZombie),
×
69
        }
×
70
}
×
71

72
func newRejectCacheEntryV2(upd1, upd2 uint32, exists,
73
        isZombie bool) rejectCacheEntry {
×
74

×
75
        return rejectCacheEntry{
×
76
                upd1BlockHeight: upd1,
×
77
                upd2BlockHeight: upd2,
×
78
                flags:           packRejectFlags(exists, isZombie),
×
79
        }
×
80
}
×
81

82
func updateRejectCacheEntryV1(entry *rejectCacheEntry, isUpdate1 bool,
83
        lastUpdate time.Time) {
×
84

×
85
        if isUpdate1 {
×
86
                entry.upd1Time = lastUpdate.Unix()
×
87
        } else {
×
88
                entry.upd2Time = lastUpdate.Unix()
×
89
        }
×
90
}
91

92
func updateRejectCacheEntryV2(entry *rejectCacheEntry, isUpdate1 bool,
93
        blockHeight uint32) {
×
94

×
95
        if isUpdate1 {
×
96
                entry.upd1BlockHeight = blockHeight
×
97
        } else {
×
98
                entry.upd2BlockHeight = blockHeight
×
99
        }
×
100
}
101

102
// rejectCacheKey uniquely identifies a channel entry in the reject cache by
103
// gossip version and channel ID.
104
type rejectCacheKey struct {
105
        version lnwire.GossipVersion
106
        chanID  uint64
107
}
108

109
// rejectCache is an in-memory cache used to improve the performance of
110
// HasChannelEdge. It caches information about the whether or channel exists, as
111
// well as the most recent timestamps for each policy (if they exists).
112
type rejectCache struct {
113
        n     int
114
        edges map[rejectCacheKey]rejectCacheEntry
115
}
116

117
// newRejectCache creates a new rejectCache with maximum capacity of n entries.
118
func newRejectCache(n int) *rejectCache {
3✔
119
        return &rejectCache{
3✔
120
                n:     n,
3✔
121
                edges: make(map[rejectCacheKey]rejectCacheEntry, n),
3✔
122
        }
3✔
123
}
3✔
124

125
// get returns the entry from the cache for chanid, if it exists.
126
func (c *rejectCache) get(version lnwire.GossipVersion, chanid uint64) (
127
        rejectCacheEntry, bool) {
3✔
128

3✔
129
        entry, ok := c.edges[rejectCacheKey{
3✔
130
                version: version,
3✔
131
                chanID:  chanid,
3✔
132
        }]
3✔
133
        return entry, ok
3✔
134
}
3✔
135

136
// insert adds the entry to the reject cache. If an entry for chanid already
137
// exists, it will be replaced with the new entry. If the entry doesn't exists,
138
// it will be inserted to the cache, performing a random eviction if the cache
139
// is at capacity.
140
func (c *rejectCache) insert(version lnwire.GossipVersion, chanid uint64,
141
        entry rejectCacheEntry) {
3✔
142

3✔
143
        key := rejectCacheKey{
3✔
144
                version: version,
3✔
145
                chanID:  chanid,
3✔
146
        }
3✔
147

3✔
148
        // If entry exists, replace it.
3✔
149
        if _, ok := c.edges[key]; ok {
6✔
150
                c.edges[key] = entry
3✔
151
                return
3✔
152
        }
3✔
153

154
        // Otherwise, evict an entry at random and insert.
155
        if len(c.edges) == c.n {
3✔
UNCOV
156
                for id := range c.edges {
×
UNCOV
157
                        delete(c.edges, id)
×
UNCOV
158
                        break
×
159
                }
160
        }
161
        c.edges[key] = entry
3✔
162
}
163

164
// remove deletes an entry for chanid from the cache, if it exists.
165
func (c *rejectCache) remove(version lnwire.GossipVersion, chanid uint64) {
3✔
166
        delete(c.edges, rejectCacheKey{
3✔
167
                version: version,
3✔
168
                chanID:  chanid,
3✔
169
        })
3✔
170
}
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc