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

lightningnetwork / lnd / 12430728843

20 Dec 2024 11:36AM UTC coverage: 61.336% (+2.6%) from 58.716%
12430728843

Pull #8777

github

ziggie1984
channeldb: fix typo.
Pull Request #8777: multi: make reassignment of alias channel edge atomic

161 of 213 new or added lines in 7 files covered. (75.59%)

70 existing lines in 17 files now uncovered.

23369 of 38100 relevant lines covered (61.34%)

115813.77 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 {
54✔
64

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

40✔
236
        // If we do have an active job, then we'll wait until either the signal
40✔
237
        // is closed, or the set of jobs exits.
40✔
238
        select {
40✔
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:
34✔
249
                log.Tracef("Signal allow for %s", jobDesc)
34✔
250
                return nil
34✔
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) {
347✔
258
        v.Lock()
347✔
259
        defer v.Unlock()
347✔
260

347✔
261
        switch msg := job.(type) {
347✔
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:
21✔
267
                shortID := lnwire.NewShortChanIDFromInt(msg.ChannelID)
21✔
268
                finSignals, ok := v.chanAnnFinSignal[shortID]
21✔
269
                if ok {
42✔
270
                        if allow {
39✔
271
                                close(finSignals.allow)
18✔
272
                        } else {
21✔
273
                                close(finSignals.deny)
3✔
274
                        }
3✔
275
                        delete(v.chanAnnFinSignal, shortID)
21✔
276
                }
277
        case *lnwire.ChannelAnnouncement1:
238✔
278
                finSignals, ok := v.chanAnnFinSignal[msg.ShortChannelID]
238✔
279
                if ok {
476✔
280
                        if allow {
270✔
281
                                close(finSignals.allow)
32✔
282
                        } else {
238✔
283
                                close(finSignals.deny)
206✔
284
                        }
206✔
285
                        delete(v.chanAnnFinSignal, msg.ShortChannelID)
238✔
286
                }
287

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