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

lightningnetwork / lnd / 12012751795

25 Nov 2024 02:40PM UTC coverage: 49.835% (-9.2%) from 59.013%
12012751795

Pull #9303

github

yyforyongyu
lnwallet: add debug logs
Pull Request #9303: htlcswitch+routing: handle nil pointer dereference properly

20 of 23 new or added lines in 4 files covered. (86.96%)

25467 existing lines in 425 files now uncovered.

99835 of 200331 relevant lines covered (49.84%)

2.07 hits per line

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

89.94
/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 {
4✔
65

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

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

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

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

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

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

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

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

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

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

4✔
141
                        v.nodeAnnDependencies[route.Vertex(msg.NodeKey1Bytes)] = signals
4✔
142
                        v.nodeAnnDependencies[route.Vertex(msg.NodeKey2Bytes)] = signals
4✔
143
                }
4✔
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:
4✔
148
                return
4✔
149
        case *lnwire.ChannelUpdate1:
4✔
150
                return
4✔
151
        case *lnwire.NodeAnnouncement:
4✔
152
                // TODO(roasbeef): node ann needs to wait on existing channel updates
4✔
153
                return
4✔
154
        case *channeldb.LightningNode:
4✔
155
                return
4✔
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() {
4✔
167
        select {
4✔
168
        case v.validationSemaphore <- struct{}{}:
4✔
169
        case <-v.quit:
1✔
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 {
4✔
178

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

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

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

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

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

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

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

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

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

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

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

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

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

4✔
262
        switch msg := job.(type) {
4✔
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:
4✔
268
                shortID := lnwire.NewShortChanIDFromInt(msg.ChannelID)
4✔
269
                finSignals, ok := v.chanAnnFinSignal[shortID]
4✔
270
                if ok {
8✔
271
                        if allow {
8✔
272
                                close(finSignals.allow)
4✔
273
                        } else {
4✔
UNCOV
274
                                close(finSignals.deny)
×
UNCOV
275
                        }
×
276
                        delete(v.chanAnnFinSignal, shortID)
4✔
277
                }
278
        case *lnwire.ChannelAnnouncement1:
4✔
279
                finSignals, ok := v.chanAnnFinSignal[msg.ShortChannelID]
4✔
280
                if ok {
8✔
281
                        if allow {
8✔
282
                                close(finSignals.allow)
4✔
283
                        } else {
4✔
UNCOV
284
                                close(finSignals.deny)
×
UNCOV
285
                        }
×
286
                        delete(v.chanAnnFinSignal, msg.ShortChannelID)
4✔
287
                }
288

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