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

lightningnetwork / lnd / 23682929266

28 Mar 2026 10:06AM UTC coverage: 52.276% (-9.9%) from 62.223%
23682929266

Pull #10684

github

web-flow
Merge 3e33be4e8 into f297c4782
Pull Request #10684: sqldb: add network-mismatch safeguard for native-SQL backends

0 of 8 new or added lines in 1 file covered. (0.0%)

31019 existing lines in 474 files now uncovered.

102028 of 195171 relevant lines covered (52.28%)

2.17 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 {
4✔
27
        var flags rejectFlags
4✔
28
        if exists {
8✔
29
                flags |= rejectFlagExists
4✔
30
        }
4✔
31
        if isZombie {
4✔
UNCOV
32
                flags |= rejectFlagZombie
×
UNCOV
33
        }
×
34

35
        return flags
4✔
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) {
4✔
42
        return f&rejectFlagExists == rejectFlagExists,
4✔
43
                f&rejectFlagZombie == rejectFlagZombie
4✔
44
}
4✔
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 {
4✔
119
        return &rejectCache{
4✔
120
                n:     n,
4✔
121
                edges: make(map[rejectCacheKey]rejectCacheEntry, n),
4✔
122
        }
4✔
123
}
4✔
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) {
4✔
128

4✔
129
        entry, ok := c.edges[rejectCacheKey{
4✔
130
                version: version,
4✔
131
                chanID:  chanid,
4✔
132
        }]
4✔
133
        return entry, ok
4✔
134
}
4✔
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) {
4✔
142

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

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

154
        // Otherwise, evict an entry at random and insert.
155
        if len(c.edges) == c.n {
4✔
UNCOV
156
                for id := range c.edges {
×
UNCOV
157
                        delete(c.edges, id)
×
UNCOV
158
                        break
×
159
                }
160
        }
161
        c.edges[key] = entry
4✔
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) {
4✔
166
        delete(c.edges, rejectCacheKey{
4✔
167
                version: version,
4✔
168
                chanID:  chanid,
4✔
169
        })
4✔
170
}
4✔
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