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

lightningnetwork / lnd / 25455608158

06 May 2026 07:10PM UTC coverage: 62.23% (+0.002%) from 62.228%
25455608158

Pull #10686

github

web-flow
Merge c5cd7ffca into 6fd5b7bb2
Pull Request #10686: chainreg: use getnetworkinfo to count outbound peers (bitcoind backend)

7 of 37 new or added lines in 1 file covered. (18.92%)

31329 existing lines in 480 files now uncovered.

143794 of 231070 relevant lines covered (62.23%)

19066.44 hits per line

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

62.5
/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 {
644✔
27
        var flags rejectFlags
644✔
28
        if exists {
948✔
29
                flags |= rejectFlagExists
304✔
30
        }
304✔
31
        if isZombie {
850✔
UNCOV
32
                flags |= rejectFlagZombie
206✔
UNCOV
33
        }
206✔
34

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

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

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

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

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