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

lightningnetwork / lnd / 12432520655

20 Dec 2024 01:44PM UTC coverage: 58.598% (-0.1%) from 58.716%
12432520655

push

github

web-flow
Merge pull request #9368 from lightningnetwork/yy-waiting-on-merge

Fix itest re new behaviors introduced by `blockbeat`

2 of 240 new or added lines in 10 files covered. (0.83%)

431 existing lines in 48 files now uncovered.

134984 of 230357 relevant lines covered (58.6%)

19224.22 hits per line

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

95.53
/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 {
52✔
64

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

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

79
        return v
52✔
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{}) {
373✔
85
        // We'll wait for either a new slot to become open, or for the quit
373✔
86
        // channel to be closed.
373✔
87
        select {
373✔
88
        case <-v.validationSemaphore:
373✔
89
        case <-v.quit:
×
90
        }
91

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

373✔
95
        // Once a slot is open, we'll examine the message of the job, to see if
373✔
96
        // there need to be any dependent barriers set up.
373✔
97
        switch msg := job.(type) {
373✔
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:
240✔
105

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

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

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

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

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

19✔
140
                        v.nodeAnnDependencies[route.Vertex(msg.NodeKey1Bytes)] = signals
19✔
141
                        v.nodeAnnDependencies[route.Vertex(msg.NodeKey2Bytes)] = signals
19✔
142
                }
19✔
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:
8✔
147
                return
8✔
148
        case *lnwire.ChannelUpdate1:
65✔
149
                return
65✔
150
        case *lnwire.NodeAnnouncement:
26✔
151
                // TODO(roasbeef): node ann needs to wait on existing channel updates
26✔
152
                return
26✔
153
        case *models.LightningNode:
9✔
154
                return
9✔
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() {
353✔
166
        select {
353✔
167
        case v.validationSemaphore <- struct{}{}:
353✔
UNCOV
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 {
349✔
177

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

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

349✔
187
        switch msg := job.(type) {
349✔
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:
8✔
191
                shortID := lnwire.NewShortChanIDFromInt(msg.ChannelID)
8✔
192
                signals, ok = v.chanEdgeDependencies[shortID]
8✔
193

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

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

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

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

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

210
        case *lnwire.NodeAnnouncement:
26✔
211
                vertex := route.Vertex(msg.NodeID)
26✔
212
                signals, ok = v.nodeAnnDependencies[vertex]
26✔
213
                jobDesc = fmt.Sprintf("job=lnwire.NodeAnnouncement, pub=%s",
26✔
214
                        vertex)
26✔
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:
19✔
221
        case *lnwire.ChannelAnnouncement1:
232✔
222
        }
223

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

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

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

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

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

248
        case <-signals.allow:
32✔
249
                log.Tracef("Signal allow for %s", jobDesc)
32✔
250
                return nil
32✔
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) {
345✔
258
        v.Lock()
345✔
259
        defer v.Unlock()
345✔
260

345✔
261
        switch msg := job.(type) {
345✔
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:
19✔
267
                shortID := lnwire.NewShortChanIDFromInt(msg.ChannelID)
19✔
268
                finSignals, ok := v.chanAnnFinSignal[shortID]
19✔
269
                if ok {
38✔
270
                        if allow {
35✔
271
                                close(finSignals.allow)
16✔
272
                        } else {
19✔
273
                                close(finSignals.deny)
3✔
274
                        }
3✔
275
                        delete(v.chanAnnFinSignal, shortID)
19✔
276
                }
277
        case *lnwire.ChannelAnnouncement1:
236✔
278
                finSignals, ok := v.chanAnnFinSignal[msg.ShortChannelID]
236✔
279
                if ok {
472✔
280
                        if allow {
266✔
281
                                close(finSignals.allow)
30✔
282
                        } else {
236✔
283
                                close(finSignals.deny)
206✔
284
                        }
206✔
285
                        delete(v.chanAnnFinSignal, msg.ShortChannelID)
236✔
286
                }
287

288
                delete(v.chanEdgeDependencies, msg.ShortChannelID)
236✔
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:
9✔
294
                delete(v.nodeAnnDependencies, route.Vertex(msg.PubKeyBytes))
9✔
295
        case *lnwire.NodeAnnouncement:
26✔
296
                delete(v.nodeAnnDependencies, route.Vertex(msg.NodeID))
26✔
297
        case *lnwire.ChannelUpdate1:
57✔
298
                delete(v.chanEdgeDependencies, msg.ShortChannelID)
57✔
299
        case *models.ChannelEdgePolicy:
8✔
300
                shortID := lnwire.NewShortChanIDFromInt(msg.ChannelID)
8✔
301
                delete(v.chanEdgeDependencies, shortID)
8✔
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