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

lightningnetwork / lnd / 11170835610

03 Oct 2024 10:41PM UTC coverage: 49.188% (-9.6%) from 58.738%
11170835610

push

github

web-flow
Merge pull request #9154 from ziggie1984/master

multi: bump btcd version.

3 of 6 new or added lines in 6 files covered. (50.0%)

26110 existing lines in 428 files now uncovered.

97359 of 197934 relevant lines covered (49.19%)

1.04 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/channeldb"
8
        "github.com/lightningnetwork/lnd/channeldb/models"
9
        "github.com/lightningnetwork/lnd/lnwire"
10
        "github.com/lightningnetwork/lnd/routing/route"
11
)
12

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

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

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

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

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

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

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

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

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

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

80
        return v
2✔
81
}
82

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

93
        v.Lock()
2✔
94
        defer v.Unlock()
2✔
95

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

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

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

2✔
123
                        v.chanAnnFinSignal[msg.ShortChannelID] = signals
2✔
124
                        v.chanEdgeDependencies[msg.ShortChannelID] = signals
2✔
125

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

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

2✔
138
                        v.chanAnnFinSignal[shortID] = signals
2✔
139
                        v.chanEdgeDependencies[shortID] = signals
2✔
140

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

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

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

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

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

2✔
185
        // Acquire a lock to read ValidationBarrier.
2✔
186
        v.Lock()
2✔
187

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2✔
262
        switch msg := job.(type) {
2✔
263

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

289
                delete(v.chanEdgeDependencies, msg.ShortChannelID)
2✔
290

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

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