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

lightningnetwork / lnd / 12301186252

12 Dec 2024 05:01PM UTC coverage: 48.92% (-9.7%) from 58.642%
12301186252

push

github

web-flow
Merge pull request #9309 from yyforyongyu/fix-unit-test

chainntnfs: fix `TestHistoricalConfDetailsTxIndex`

99121 of 202617 relevant lines covered (48.92%)

1.54 hits per line

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

89.39
/graph/validation_barrier.go
1
package graph
2

3
import (
4
        "fmt"
5
        "sync"
6

7
        "github.com/lightningnetwork/lnd/graph/db/models"
8
        "github.com/lightningnetwork/lnd/lnwire"
9
        "github.com/lightningnetwork/lnd/routing/route"
10
)
11

12
// validationSignals contains two signals which allows the ValidationBarrier to
13
// communicate back to the caller whether a dependent should be processed or not
14
// based on whether its parent was successfully validated. Only one of these
15
// signals is to be used at a time.
16
type validationSignals struct {
17
        // allow is the signal used to allow a dependent to be processed.
18
        allow chan struct{}
19

20
        // deny is the signal used to prevent a dependent from being processed.
21
        deny chan struct{}
22
}
23

24
// ValidationBarrier is a barrier used to ensure proper validation order while
25
// concurrently validating new announcements for channel edges, and the
26
// attributes of channel edges.  It uses this set of maps (protected by this
27
// mutex) to track validation dependencies. For a given channel our
28
// dependencies look like this: chanAnn <- chanUp <- nodeAnn. That is we must
29
// validate the item on the left of the arrow before that on the right.
30
type ValidationBarrier struct {
31
        // validationSemaphore is a channel of structs which is used as a
32
        // semaphore. Initially we'll fill this with a buffered channel of the
33
        // size of the number of active requests. Each new job will consume
34
        // from this channel, then restore the value upon completion.
35
        validationSemaphore chan struct{}
36

37
        // chanAnnFinSignal is map that keep track of all the pending
38
        // ChannelAnnouncement like validation job going on. Once the job has
39
        // been completed, the channel will be closed unblocking any
40
        // dependants.
41
        chanAnnFinSignal map[lnwire.ShortChannelID]*validationSignals
42

43
        // chanEdgeDependencies tracks any channel edge updates which should
44
        // wait until the completion of the ChannelAnnouncement before
45
        // proceeding. This is a dependency, as we can't validate the update
46
        // before we validate the announcement which creates the channel
47
        // itself.
48
        chanEdgeDependencies map[lnwire.ShortChannelID]*validationSignals
49

50
        // nodeAnnDependencies tracks any pending NodeAnnouncement validation
51
        // jobs which should wait until the completion of the
52
        // ChannelAnnouncement before proceeding.
53
        nodeAnnDependencies map[route.Vertex]*validationSignals
54

55
        quit chan struct{}
56
        sync.Mutex
57
}
58

59
// NewValidationBarrier creates a new instance of a validation barrier given
60
// the total number of active requests, and a quit channel which will be used
61
// to know when to kill pending, but unfilled jobs.
62
func NewValidationBarrier(numActiveReqs int,
63
        quitChan chan struct{}) *ValidationBarrier {
3✔
64

3✔
65
        v := &ValidationBarrier{
3✔
66
                chanAnnFinSignal:     make(map[lnwire.ShortChannelID]*validationSignals),
3✔
67
                chanEdgeDependencies: make(map[lnwire.ShortChannelID]*validationSignals),
3✔
68
                nodeAnnDependencies:  make(map[route.Vertex]*validationSignals),
3✔
69
                quit:                 quitChan,
3✔
70
        }
3✔
71

3✔
72
        // We'll first initialize a set of semaphores to limit our concurrency
3✔
73
        // when validating incoming requests in parallel.
3✔
74
        v.validationSemaphore = make(chan struct{}, numActiveReqs)
3✔
75
        for i := 0; i < numActiveReqs; i++ {
6✔
76
                v.validationSemaphore <- struct{}{}
3✔
77
        }
3✔
78

79
        return v
3✔
80
}
81

82
// InitJobDependencies will wait for a new job slot to become open, and then
83
// sets up any dependent signals/trigger for the new job
84
func (v *ValidationBarrier) InitJobDependencies(job interface{}) {
3✔
85
        // We'll wait for either a new slot to become open, or for the quit
3✔
86
        // channel to be closed.
3✔
87
        select {
3✔
88
        case <-v.validationSemaphore:
3✔
89
        case <-v.quit:
×
90
        }
91

92
        v.Lock()
3✔
93
        defer v.Unlock()
3✔
94

3✔
95
        // Once a slot is open, we'll examine the message of the job, to see if
3✔
96
        // there need to be any dependent barriers set up.
3✔
97
        switch msg := job.(type) {
3✔
98

99
        // If this is a channel announcement, then we'll need to set up den
100
        // tenancies, as we'll need to verify this before we verify any
101
        // ChannelUpdates for the same channel, or NodeAnnouncements of nodes
102
        // that are involved in this channel. This goes for both the wire
103
        // type,s and also the types that we use within the database.
104
        case *lnwire.ChannelAnnouncement1:
3✔
105

3✔
106
                // We ensure that we only create a new announcement signal iff,
3✔
107
                // one doesn't already exist, as there may be duplicate
3✔
108
                // announcements.  We'll close this signal once the
3✔
109
                // ChannelAnnouncement has been validated. This will result in
3✔
110
                // all the dependent jobs being unlocked so they can finish
3✔
111
                // execution themselves.
3✔
112
                if _, ok := v.chanAnnFinSignal[msg.ShortChannelID]; !ok {
6✔
113
                        // We'll create the channel that we close after we
3✔
114
                        // validate this announcement. All dependants will
3✔
115
                        // point to this same channel, so they'll be unblocked
3✔
116
                        // at the same time.
3✔
117
                        signals := &validationSignals{
3✔
118
                                allow: make(chan struct{}),
3✔
119
                                deny:  make(chan struct{}),
3✔
120
                        }
3✔
121

3✔
122
                        v.chanAnnFinSignal[msg.ShortChannelID] = signals
3✔
123
                        v.chanEdgeDependencies[msg.ShortChannelID] = signals
3✔
124

3✔
125
                        v.nodeAnnDependencies[route.Vertex(msg.NodeID1)] = signals
3✔
126
                        v.nodeAnnDependencies[route.Vertex(msg.NodeID2)] = signals
3✔
127
                }
3✔
128
        case *models.ChannelEdgeInfo:
3✔
129

3✔
130
                shortID := lnwire.NewShortChanIDFromInt(msg.ChannelID)
3✔
131
                if _, ok := v.chanAnnFinSignal[shortID]; !ok {
6✔
132
                        signals := &validationSignals{
3✔
133
                                allow: make(chan struct{}),
3✔
134
                                deny:  make(chan struct{}),
3✔
135
                        }
3✔
136

3✔
137
                        v.chanAnnFinSignal[shortID] = signals
3✔
138
                        v.chanEdgeDependencies[shortID] = signals
3✔
139

3✔
140
                        v.nodeAnnDependencies[route.Vertex(msg.NodeKey1Bytes)] = signals
3✔
141
                        v.nodeAnnDependencies[route.Vertex(msg.NodeKey2Bytes)] = signals
3✔
142
                }
3✔
143

144
        // These other types don't have any dependants, so no further
145
        // initialization needs to be done beyond just occupying a job slot.
146
        case *models.ChannelEdgePolicy:
3✔
147
                return
3✔
148
        case *lnwire.ChannelUpdate1:
3✔
149
                return
3✔
150
        case *lnwire.NodeAnnouncement:
3✔
151
                // TODO(roasbeef): node ann needs to wait on existing channel updates
3✔
152
                return
3✔
153
        case *models.LightningNode:
3✔
154
                return
3✔
155
        case *lnwire.AnnounceSignatures1:
×
156
                // TODO(roasbeef): need to wait on chan ann?
×
157
                return
×
158
        }
159
}
160

161
// CompleteJob returns a free slot to the set of available job slots. This
162
// should be called once a job has been fully completed. Otherwise, slots may
163
// not be returned to the internal scheduling, causing a deadlock when a new
164
// overflow job is attempted.
165
func (v *ValidationBarrier) CompleteJob() {
3✔
166
        select {
3✔
167
        case v.validationSemaphore <- struct{}{}:
3✔
168
        case <-v.quit:
×
169
        }
170
}
171

172
// WaitForDependants will block until any jobs that this job dependants on have
173
// finished executing. This allows us a graceful way to schedule goroutines
174
// based on any pending uncompleted dependent jobs. If this job doesn't have an
175
// active dependent, then this function will return immediately.
176
func (v *ValidationBarrier) WaitForDependants(job interface{}) error {
3✔
177

3✔
178
        var (
3✔
179
                signals *validationSignals
3✔
180
                ok      bool
3✔
181
                jobDesc string
3✔
182
        )
3✔
183

3✔
184
        // Acquire a lock to read ValidationBarrier.
3✔
185
        v.Lock()
3✔
186

3✔
187
        switch msg := job.(type) {
3✔
188
        // Any ChannelUpdate or NodeAnnouncement jobs will need to wait on the
189
        // completion of any active ChannelAnnouncement jobs related to them.
190
        case *models.ChannelEdgePolicy:
3✔
191
                shortID := lnwire.NewShortChanIDFromInt(msg.ChannelID)
3✔
192
                signals, ok = v.chanEdgeDependencies[shortID]
3✔
193

3✔
194
                jobDesc = fmt.Sprintf("job=lnwire.ChannelEdgePolicy, scid=%v",
3✔
195
                        msg.ChannelID)
3✔
196

197
        case *models.LightningNode:
3✔
198
                vertex := route.Vertex(msg.PubKeyBytes)
3✔
199
                signals, ok = v.nodeAnnDependencies[vertex]
3✔
200

3✔
201
                jobDesc = fmt.Sprintf("job=channeldb.LightningNode, pub=%s",
3✔
202
                        vertex)
3✔
203

204
        case *lnwire.ChannelUpdate1:
3✔
205
                signals, ok = v.chanEdgeDependencies[msg.ShortChannelID]
3✔
206

3✔
207
                jobDesc = fmt.Sprintf("job=lnwire.ChannelUpdate, scid=%v",
3✔
208
                        msg.ShortChannelID.ToUint64())
3✔
209

210
        case *lnwire.NodeAnnouncement:
3✔
211
                vertex := route.Vertex(msg.NodeID)
3✔
212
                signals, ok = v.nodeAnnDependencies[vertex]
3✔
213
                jobDesc = fmt.Sprintf("job=lnwire.NodeAnnouncement, pub=%s",
3✔
214
                        vertex)
3✔
215

216
        // Other types of jobs can be executed immediately, so we'll just
217
        // return directly.
218
        case *lnwire.AnnounceSignatures1:
×
219
                // TODO(roasbeef): need to wait on chan ann?
220
        case *models.ChannelEdgeInfo:
3✔
221
        case *lnwire.ChannelAnnouncement1:
3✔
222
        }
223

224
        // Release the lock once the above read is finished.
225
        v.Unlock()
3✔
226

3✔
227
        // If it's not ok, it means either the job is not a dependent type, or
3✔
228
        // it doesn't have a dependency signal. Either way, we can return
3✔
229
        // early.
3✔
230
        if !ok {
6✔
231
                return nil
3✔
232
        }
3✔
233

234
        log.Debugf("Waiting for dependent on %s", jobDesc)
3✔
235

3✔
236
        // If we do have an active job, then we'll wait until either the signal
3✔
237
        // is closed, or the set of jobs exits.
3✔
238
        select {
3✔
239
        case <-v.quit:
×
240
                return NewErrf(ErrVBarrierShuttingDown,
×
241
                        "validation barrier shutting down")
×
242

243
        case <-signals.deny:
×
244
                log.Debugf("Signal deny for %s", jobDesc)
×
245
                return NewErrf(ErrParentValidationFailed,
×
246
                        "parent validation failed")
×
247

248
        case <-signals.allow:
3✔
249
                log.Tracef("Signal allow for %s", jobDesc)
3✔
250
                return nil
3✔
251
        }
252
}
253

254
// SignalDependants will allow/deny any jobs that are dependent on this job that
255
// they can continue execution. If the job doesn't have any dependants, then
256
// this function sill exit immediately.
257
func (v *ValidationBarrier) SignalDependants(job interface{}, allow bool) {
3✔
258
        v.Lock()
3✔
259
        defer v.Unlock()
3✔
260

3✔
261
        switch msg := job.(type) {
3✔
262

263
        // If we've just finished executing a ChannelAnnouncement, then we'll
264
        // close out the signal, and remove the signal from the map of active
265
        // ones. This will allow/deny any dependent jobs to continue execution.
266
        case *models.ChannelEdgeInfo:
3✔
267
                shortID := lnwire.NewShortChanIDFromInt(msg.ChannelID)
3✔
268
                finSignals, ok := v.chanAnnFinSignal[shortID]
3✔
269
                if ok {
6✔
270
                        if allow {
6✔
271
                                close(finSignals.allow)
3✔
272
                        } else {
3✔
273
                                close(finSignals.deny)
×
274
                        }
×
275
                        delete(v.chanAnnFinSignal, shortID)
3✔
276
                }
277
        case *lnwire.ChannelAnnouncement1:
3✔
278
                finSignals, ok := v.chanAnnFinSignal[msg.ShortChannelID]
3✔
279
                if ok {
6✔
280
                        if allow {
6✔
281
                                close(finSignals.allow)
3✔
282
                        } else {
3✔
283
                                close(finSignals.deny)
×
284
                        }
×
285
                        delete(v.chanAnnFinSignal, msg.ShortChannelID)
3✔
286
                }
287

288
                delete(v.chanEdgeDependencies, msg.ShortChannelID)
3✔
289

290
        // For all other job types, we'll delete the tracking entries from the
291
        // map, as if we reach this point, then all dependants have already
292
        // finished executing and we can proceed.
293
        case *models.LightningNode:
3✔
294
                delete(v.nodeAnnDependencies, route.Vertex(msg.PubKeyBytes))
3✔
295
        case *lnwire.NodeAnnouncement:
3✔
296
                delete(v.nodeAnnDependencies, route.Vertex(msg.NodeID))
3✔
297
        case *lnwire.ChannelUpdate1:
3✔
298
                delete(v.chanEdgeDependencies, msg.ShortChannelID)
3✔
299
        case *models.ChannelEdgePolicy:
3✔
300
                shortID := lnwire.NewShortChanIDFromInt(msg.ChannelID)
3✔
301
                delete(v.chanEdgeDependencies, shortID)
3✔
302

303
        case *lnwire.AnnounceSignatures1:
×
304
                return
×
305
        }
306
}
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

© 2025 Coveralls, Inc