• 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

54.43
/channeldb/payments.go
1
package channeldb
2

3
import (
4
        "bytes"
5
        "encoding/binary"
6
        "errors"
7
        "fmt"
8
        "io"
9
        "sort"
10
        "time"
11

12
        "github.com/btcsuite/btcd/btcec/v2"
13
        "github.com/btcsuite/btcd/wire"
14
        "github.com/lightningnetwork/lnd/kvdb"
15
        "github.com/lightningnetwork/lnd/lntypes"
16
        "github.com/lightningnetwork/lnd/lnwire"
17
        "github.com/lightningnetwork/lnd/record"
18
        "github.com/lightningnetwork/lnd/routing/route"
19
        "github.com/lightningnetwork/lnd/tlv"
20
)
21

22
var (
23
        // paymentsRootBucket is the name of the top-level bucket within the
24
        // database that stores all data related to payments. Within this
25
        // bucket, each payment hash its own sub-bucket keyed by its payment
26
        // hash.
27
        //
28
        // Bucket hierarchy:
29
        //
30
        // root-bucket
31
        //      |
32
        //      |-- <paymenthash>
33
        //      |        |--sequence-key: <sequence number>
34
        //      |        |--creation-info-key: <creation info>
35
        //      |        |--fail-info-key: <(optional) fail info>
36
        //      |        |
37
        //      |        |--payment-htlcs-bucket (shard-bucket)
38
        //      |        |        |
39
        //      |        |        |-- ai<htlc attempt ID>: <htlc attempt info>
40
        //      |        |        |-- si<htlc attempt ID>: <(optional) settle info>
41
        //      |        |        |-- fi<htlc attempt ID>: <(optional) fail info>
42
        //      |        |        |
43
        //      |        |       ...
44
        //      |        |
45
        //      |        |
46
        //      |        |--duplicate-bucket (only for old, completed payments)
47
        //      |                 |
48
        //      |                 |-- <seq-num>
49
        //      |                 |       |--sequence-key: <sequence number>
50
        //      |                 |       |--creation-info-key: <creation info>
51
        //      |                 |       |--ai: <attempt info>
52
        //      |                 |       |--si: <settle info>
53
        //      |                 |       |--fi: <fail info>
54
        //      |                 |
55
        //      |                 |-- <seq-num>
56
        //      |                 |       |
57
        //      |                ...     ...
58
        //      |
59
        //      |-- <paymenthash>
60
        //      |        |
61
        //      |       ...
62
        //     ...
63
        //
64
        paymentsRootBucket = []byte("payments-root-bucket")
65

66
        // paymentSequenceKey is a key used in the payment's sub-bucket to
67
        // store the sequence number of the payment.
68
        paymentSequenceKey = []byte("payment-sequence-key")
69

70
        // paymentCreationInfoKey is a key used in the payment's sub-bucket to
71
        // store the creation info of the payment.
72
        paymentCreationInfoKey = []byte("payment-creation-info")
73

74
        // paymentHtlcsBucket is a bucket where we'll store the information
75
        // about the HTLCs that were attempted for a payment.
76
        paymentHtlcsBucket = []byte("payment-htlcs-bucket")
77

78
        // htlcAttemptInfoKey is the key used as the prefix of an HTLC attempt
79
        // to store the info about the attempt that was done for the HTLC in
80
        // question. The HTLC attempt ID is concatenated at the end.
81
        htlcAttemptInfoKey = []byte("ai")
82

83
        // htlcSettleInfoKey is the key used as the prefix of an HTLC attempt
84
        // settle info, if any. The HTLC attempt ID is concatenated at the end.
85
        htlcSettleInfoKey = []byte("si")
86

87
        // htlcFailInfoKey is the key used as the prefix of an HTLC attempt
88
        // failure information, if any.The  HTLC attempt ID is concatenated at
89
        // the end.
90
        htlcFailInfoKey = []byte("fi")
91

92
        // paymentFailInfoKey is a key used in the payment's sub-bucket to
93
        // store information about the reason a payment failed.
94
        paymentFailInfoKey = []byte("payment-fail-info")
95

96
        // paymentsIndexBucket is the name of the top-level bucket within the
97
        // database that stores an index of payment sequence numbers to its
98
        // payment hash.
99
        // payments-sequence-index-bucket
100
        //         |--<sequence-number>: <payment hash>
101
        //         |--...
102
        //         |--<sequence-number>: <payment hash>
103
        paymentsIndexBucket = []byte("payments-index-bucket")
104
)
105

106
var (
107
        // ErrNoSequenceNumber is returned if we look up a payment which does
108
        // not have a sequence number.
109
        ErrNoSequenceNumber = errors.New("sequence number not found")
110

111
        // ErrDuplicateNotFound is returned when we lookup a payment by its
112
        // index and cannot find a payment with a matching sequence number.
113
        ErrDuplicateNotFound = errors.New("duplicate payment not found")
114

115
        // ErrNoDuplicateBucket is returned when we expect to find duplicates
116
        // when looking up a payment from its index, but the payment does not
117
        // have any.
118
        ErrNoDuplicateBucket = errors.New("expected duplicate bucket")
119

120
        // ErrNoDuplicateNestedBucket is returned if we do not find duplicate
121
        // payments in their own sub-bucket.
122
        ErrNoDuplicateNestedBucket = errors.New("nested duplicate bucket not " +
123
                "found")
124
)
125

126
// FailureReason encodes the reason a payment ultimately failed.
127
type FailureReason byte
128

129
const (
130
        // FailureReasonTimeout indicates that the payment did timeout before a
131
        // successful payment attempt was made.
132
        FailureReasonTimeout FailureReason = 0
133

134
        // FailureReasonNoRoute indicates no successful route to the
135
        // destination was found during path finding.
136
        FailureReasonNoRoute FailureReason = 1
137

138
        // FailureReasonError indicates that an unexpected error happened during
139
        // payment.
140
        FailureReasonError FailureReason = 2
141

142
        // FailureReasonPaymentDetails indicates that either the hash is unknown
143
        // or the final cltv delta or amount is incorrect.
144
        FailureReasonPaymentDetails FailureReason = 3
145

146
        // FailureReasonInsufficientBalance indicates that we didn't have enough
147
        // balance to complete the payment.
148
        FailureReasonInsufficientBalance FailureReason = 4
149

150
        // FailureReasonCanceled indicates that the payment was canceled by the
151
        // user.
152
        FailureReasonCanceled FailureReason = 5
153

154
        // TODO(joostjager): Add failure reasons for:
155
        // LocalLiquidityInsufficient, RemoteCapacityInsufficient.
156
)
157

158
// Error returns a human-readable error string for the FailureReason.
159
func (r FailureReason) Error() string {
4✔
160
        return r.String()
4✔
161
}
4✔
162

163
// String returns a human-readable FailureReason.
164
func (r FailureReason) String() string {
4✔
165
        switch r {
4✔
UNCOV
166
        case FailureReasonTimeout:
×
UNCOV
167
                return "timeout"
×
168
        case FailureReasonNoRoute:
4✔
169
                return "no_route"
4✔
UNCOV
170
        case FailureReasonError:
×
UNCOV
171
                return "error"
×
172
        case FailureReasonPaymentDetails:
4✔
173
                return "incorrect_payment_details"
4✔
174
        case FailureReasonInsufficientBalance:
4✔
175
                return "insufficient_balance"
4✔
UNCOV
176
        case FailureReasonCanceled:
×
UNCOV
177
                return "canceled"
×
178
        }
179

180
        return "unknown"
×
181
}
182

183
// PaymentCreationInfo is the information necessary to have ready when
184
// initiating a payment, moving it into state InFlight.
185
type PaymentCreationInfo struct {
186
        // PaymentIdentifier is the hash this payment is paying to in case of
187
        // non-AMP payments, and the SetID for AMP payments.
188
        PaymentIdentifier lntypes.Hash
189

190
        // Value is the amount we are paying.
191
        Value lnwire.MilliSatoshi
192

193
        // CreationTime is the time when this payment was initiated.
194
        CreationTime time.Time
195

196
        // PaymentRequest is the full payment request, if any.
197
        PaymentRequest []byte
198

199
        // FirstHopCustomRecords are the TLV records that are to be sent to the
200
        // first hop of this payment. These records will be transmitted via the
201
        // wire message only and therefore do not affect the onion payload size.
202
        FirstHopCustomRecords lnwire.CustomRecords
203
}
204

205
// htlcBucketKey creates a composite key from prefix and id where the result is
206
// simply the two concatenated.
207
func htlcBucketKey(prefix, id []byte) []byte {
4✔
208
        key := make([]byte, len(prefix)+len(id))
4✔
209
        copy(key, prefix)
4✔
210
        copy(key[len(prefix):], id)
4✔
211
        return key
4✔
212
}
4✔
213

214
// FetchPayments returns all sent payments found in the DB.
215
//
216
// nolint: dupl
UNCOV
217
func (d *DB) FetchPayments() ([]*MPPayment, error) {
×
UNCOV
218
        var payments []*MPPayment
×
UNCOV
219

×
UNCOV
220
        err := kvdb.View(d, func(tx kvdb.RTx) error {
×
UNCOV
221
                paymentsBucket := tx.ReadBucket(paymentsRootBucket)
×
UNCOV
222
                if paymentsBucket == nil {
×
223
                        return nil
×
224
                }
×
225

UNCOV
226
                return paymentsBucket.ForEach(func(k, v []byte) error {
×
UNCOV
227
                        bucket := paymentsBucket.NestedReadBucket(k)
×
UNCOV
228
                        if bucket == nil {
×
229
                                // We only expect sub-buckets to be found in
×
230
                                // this top-level bucket.
×
231
                                return fmt.Errorf("non bucket element in " +
×
232
                                        "payments bucket")
×
233
                        }
×
234

UNCOV
235
                        p, err := fetchPayment(bucket)
×
UNCOV
236
                        if err != nil {
×
237
                                return err
×
238
                        }
×
239

UNCOV
240
                        payments = append(payments, p)
×
UNCOV
241

×
UNCOV
242
                        // For older versions of lnd, duplicate payments to a
×
UNCOV
243
                        // payment has was possible. These will be found in a
×
UNCOV
244
                        // sub-bucket indexed by their sequence number if
×
UNCOV
245
                        // available.
×
UNCOV
246
                        duplicatePayments, err := fetchDuplicatePayments(bucket)
×
UNCOV
247
                        if err != nil {
×
248
                                return err
×
249
                        }
×
250

UNCOV
251
                        payments = append(payments, duplicatePayments...)
×
UNCOV
252
                        return nil
×
253
                })
UNCOV
254
        }, func() {
×
UNCOV
255
                payments = nil
×
UNCOV
256
        })
×
UNCOV
257
        if err != nil {
×
258
                return nil, err
×
259
        }
×
260

261
        // Before returning, sort the payments by their sequence number.
UNCOV
262
        sort.Slice(payments, func(i, j int) bool {
×
UNCOV
263
                return payments[i].SequenceNum < payments[j].SequenceNum
×
UNCOV
264
        })
×
265

UNCOV
266
        return payments, nil
×
267
}
268

269
func fetchCreationInfo(bucket kvdb.RBucket) (*PaymentCreationInfo, error) {
4✔
270
        b := bucket.Get(paymentCreationInfoKey)
4✔
271
        if b == nil {
4✔
272
                return nil, fmt.Errorf("creation info not found")
×
273
        }
×
274

275
        r := bytes.NewReader(b)
4✔
276
        return deserializePaymentCreationInfo(r)
4✔
277
}
278

279
func fetchPayment(bucket kvdb.RBucket) (*MPPayment, error) {
4✔
280
        seqBytes := bucket.Get(paymentSequenceKey)
4✔
281
        if seqBytes == nil {
4✔
282
                return nil, fmt.Errorf("sequence number not found")
×
283
        }
×
284

285
        sequenceNum := binary.BigEndian.Uint64(seqBytes)
4✔
286

4✔
287
        // Get the PaymentCreationInfo.
4✔
288
        creationInfo, err := fetchCreationInfo(bucket)
4✔
289
        if err != nil {
4✔
290
                return nil, err
×
291
        }
×
292

293
        var htlcs []HTLCAttempt
4✔
294
        htlcsBucket := bucket.NestedReadBucket(paymentHtlcsBucket)
4✔
295
        if htlcsBucket != nil {
8✔
296
                // Get the payment attempts. This can be empty.
4✔
297
                htlcs, err = fetchHtlcAttempts(htlcsBucket)
4✔
298
                if err != nil {
4✔
299
                        return nil, err
×
300
                }
×
301
        }
302

303
        // Get failure reason if available.
304
        var failureReason *FailureReason
4✔
305
        b := bucket.Get(paymentFailInfoKey)
4✔
306
        if b != nil {
8✔
307
                reason := FailureReason(b[0])
4✔
308
                failureReason = &reason
4✔
309
        }
4✔
310

311
        // Create a new payment.
312
        payment := &MPPayment{
4✔
313
                SequenceNum:   sequenceNum,
4✔
314
                Info:          creationInfo,
4✔
315
                HTLCs:         htlcs,
4✔
316
                FailureReason: failureReason,
4✔
317
        }
4✔
318

4✔
319
        // Set its state and status.
4✔
320
        if err := payment.setState(); err != nil {
4✔
321
                return nil, err
×
322
        }
×
323

324
        return payment, nil
4✔
325
}
326

327
// fetchHtlcAttempts retrieves all htlc attempts made for the payment found in
328
// the given bucket.
329
func fetchHtlcAttempts(bucket kvdb.RBucket) ([]HTLCAttempt, error) {
4✔
330
        htlcsMap := make(map[uint64]*HTLCAttempt)
4✔
331

4✔
332
        attemptInfoCount := 0
4✔
333
        err := bucket.ForEach(func(k, v []byte) error {
8✔
334
                aid := byteOrder.Uint64(k[len(k)-8:])
4✔
335

4✔
336
                if _, ok := htlcsMap[aid]; !ok {
8✔
337
                        htlcsMap[aid] = &HTLCAttempt{}
4✔
338
                }
4✔
339

340
                var err error
4✔
341
                switch {
4✔
342
                case bytes.HasPrefix(k, htlcAttemptInfoKey):
4✔
343
                        attemptInfo, err := readHtlcAttemptInfo(v)
4✔
344
                        if err != nil {
4✔
345
                                return err
×
346
                        }
×
347

348
                        attemptInfo.AttemptID = aid
4✔
349
                        htlcsMap[aid].HTLCAttemptInfo = *attemptInfo
4✔
350
                        attemptInfoCount++
4✔
351

352
                case bytes.HasPrefix(k, htlcSettleInfoKey):
4✔
353
                        htlcsMap[aid].Settle, err = readHtlcSettleInfo(v)
4✔
354
                        if err != nil {
4✔
355
                                return err
×
356
                        }
×
357

358
                case bytes.HasPrefix(k, htlcFailInfoKey):
4✔
359
                        htlcsMap[aid].Failure, err = readHtlcFailInfo(v)
4✔
360
                        if err != nil {
4✔
361
                                return err
×
362
                        }
×
363

364
                default:
×
365
                        return fmt.Errorf("unknown htlc attempt key")
×
366
                }
367

368
                return nil
4✔
369
        })
370
        if err != nil {
4✔
371
                return nil, err
×
372
        }
×
373

374
        // Sanity check that all htlcs have an attempt info.
375
        if attemptInfoCount != len(htlcsMap) {
4✔
376
                return nil, errNoAttemptInfo
×
377
        }
×
378

379
        keys := make([]uint64, len(htlcsMap))
4✔
380
        i := 0
4✔
381
        for k := range htlcsMap {
8✔
382
                keys[i] = k
4✔
383
                i++
4✔
384
        }
4✔
385

386
        // Sort HTLC attempts by their attempt ID. This is needed because in the
387
        // DB we store the attempts with keys prefixed by their status which
388
        // changes order (groups them together by status).
389
        sort.Slice(keys, func(i, j int) bool {
8✔
390
                return keys[i] < keys[j]
4✔
391
        })
4✔
392

393
        htlcs := make([]HTLCAttempt, len(htlcsMap))
4✔
394
        for i, key := range keys {
8✔
395
                htlcs[i] = *htlcsMap[key]
4✔
396
        }
4✔
397

398
        return htlcs, nil
4✔
399
}
400

401
// readHtlcAttemptInfo reads the payment attempt info for this htlc.
402
func readHtlcAttemptInfo(b []byte) (*HTLCAttemptInfo, error) {
4✔
403
        r := bytes.NewReader(b)
4✔
404
        return deserializeHTLCAttemptInfo(r)
4✔
405
}
4✔
406

407
// readHtlcSettleInfo reads the settle info for the htlc. If the htlc isn't
408
// settled, nil is returned.
409
func readHtlcSettleInfo(b []byte) (*HTLCSettleInfo, error) {
4✔
410
        r := bytes.NewReader(b)
4✔
411
        return deserializeHTLCSettleInfo(r)
4✔
412
}
4✔
413

414
// readHtlcFailInfo reads the failure info for the htlc. If the htlc hasn't
415
// failed, nil is returned.
416
func readHtlcFailInfo(b []byte) (*HTLCFailInfo, error) {
4✔
417
        r := bytes.NewReader(b)
4✔
418
        return deserializeHTLCFailInfo(r)
4✔
419
}
4✔
420

421
// fetchFailedHtlcKeys retrieves the bucket keys of all failed HTLCs of a
422
// payment bucket.
UNCOV
423
func fetchFailedHtlcKeys(bucket kvdb.RBucket) ([][]byte, error) {
×
UNCOV
424
        htlcsBucket := bucket.NestedReadBucket(paymentHtlcsBucket)
×
UNCOV
425

×
UNCOV
426
        var htlcs []HTLCAttempt
×
UNCOV
427
        var err error
×
UNCOV
428
        if htlcsBucket != nil {
×
UNCOV
429
                htlcs, err = fetchHtlcAttempts(htlcsBucket)
×
UNCOV
430
                if err != nil {
×
431
                        return nil, err
×
432
                }
×
433
        }
434

435
        // Now iterate though them and save the bucket keys for the failed
436
        // HTLCs.
UNCOV
437
        var htlcKeys [][]byte
×
UNCOV
438
        for _, h := range htlcs {
×
UNCOV
439
                if h.Failure == nil {
×
UNCOV
440
                        continue
×
441
                }
442

UNCOV
443
                htlcKeyBytes := make([]byte, 8)
×
UNCOV
444
                binary.BigEndian.PutUint64(htlcKeyBytes, h.AttemptID)
×
UNCOV
445

×
UNCOV
446
                htlcKeys = append(htlcKeys, htlcKeyBytes)
×
447
        }
448

UNCOV
449
        return htlcKeys, nil
×
450
}
451

452
// PaymentsQuery represents a query to the payments database starting or ending
453
// at a certain offset index. The number of retrieved records can be limited.
454
type PaymentsQuery struct {
455
        // IndexOffset determines the starting point of the payments query and
456
        // is always exclusive. In normal order, the query starts at the next
457
        // higher (available) index compared to IndexOffset. In reversed order,
458
        // the query ends at the next lower (available) index compared to the
459
        // IndexOffset. In the case of a zero index_offset, the query will start
460
        // with the oldest payment when paginating forwards, or will end with
461
        // the most recent payment when paginating backwards.
462
        IndexOffset uint64
463

464
        // MaxPayments is the maximal number of payments returned in the
465
        // payments query.
466
        MaxPayments uint64
467

468
        // Reversed gives a meaning to the IndexOffset. If reversed is set to
469
        // true, the query will fetch payments with indices lower than the
470
        // IndexOffset, otherwise, it will return payments with indices greater
471
        // than the IndexOffset.
472
        Reversed bool
473

474
        // If IncludeIncomplete is true, then return payments that have not yet
475
        // fully completed. This means that pending payments, as well as failed
476
        // payments will show up if this field is set to true.
477
        IncludeIncomplete bool
478

479
        // CountTotal indicates that all payments currently present in the
480
        // payment index (complete and incomplete) should be counted.
481
        CountTotal bool
482

483
        // CreationDateStart, expressed in Unix seconds, if set, filters out
484
        // all payments with a creation date greater than or equal to it.
485
        CreationDateStart int64
486

487
        // CreationDateEnd, expressed in Unix seconds, if set, filters out all
488
        // payments with a creation date less than or equal to it.
489
        CreationDateEnd int64
490
}
491

492
// PaymentsResponse contains the result of a query to the payments database.
493
// It includes the set of payments that match the query and integers which
494
// represent the index of the first and last item returned in the series of
495
// payments. These integers allow callers to resume their query in the event
496
// that the query's response exceeds the max number of returnable events.
497
type PaymentsResponse struct {
498
        // Payments is the set of payments returned from the database for the
499
        // PaymentsQuery.
500
        Payments []*MPPayment
501

502
        // FirstIndexOffset is the index of the first element in the set of
503
        // returned MPPayments. Callers can use this to resume their query
504
        // in the event that the slice has too many events to fit into a single
505
        // response. The offset can be used to continue reverse pagination.
506
        FirstIndexOffset uint64
507

508
        // LastIndexOffset is the index of the last element in the set of
509
        // returned MPPayments. Callers can use this to resume their query
510
        // in the event that the slice has too many events to fit into a single
511
        // response. The offset can be used to continue forward pagination.
512
        LastIndexOffset uint64
513

514
        // TotalCount represents the total number of payments that are currently
515
        // stored in the payment database. This will only be set if the
516
        // CountTotal field in the query was set to true.
517
        TotalCount uint64
518
}
519

520
// QueryPayments is a query to the payments database which is restricted
521
// to a subset of payments by the payments query, containing an offset
522
// index and a maximum number of returned payments.
523
func (d *DB) QueryPayments(query PaymentsQuery) (PaymentsResponse, error) {
4✔
524
        var resp PaymentsResponse
4✔
525

4✔
526
        if err := kvdb.View(d, func(tx kvdb.RTx) error {
8✔
527
                // Get the root payments bucket.
4✔
528
                paymentsBucket := tx.ReadBucket(paymentsRootBucket)
4✔
529
                if paymentsBucket == nil {
8✔
530
                        return nil
4✔
531
                }
4✔
532

533
                // Get the index bucket which maps sequence number -> payment
534
                // hash and duplicate bool. If we have a payments bucket, we
535
                // should have an indexes bucket as well.
536
                indexes := tx.ReadBucket(paymentsIndexBucket)
4✔
537
                if indexes == nil {
4✔
538
                        return fmt.Errorf("index bucket does not exist")
×
539
                }
×
540

541
                // accumulatePayments gets payments with the sequence number
542
                // and hash provided and adds them to our list of payments if
543
                // they meet the criteria of our query. It returns the number
544
                // of payments that were added.
545
                accumulatePayments := func(sequenceKey, hash []byte) (bool,
4✔
546
                        error) {
8✔
547

4✔
548
                        r := bytes.NewReader(hash)
4✔
549
                        paymentHash, err := deserializePaymentIndex(r)
4✔
550
                        if err != nil {
4✔
551
                                return false, err
×
552
                        }
×
553

554
                        payment, err := fetchPaymentWithSequenceNumber(
4✔
555
                                tx, paymentHash, sequenceKey,
4✔
556
                        )
4✔
557
                        if err != nil {
4✔
558
                                return false, err
×
559
                        }
×
560

561
                        // To keep compatibility with the old API, we only
562
                        // return non-succeeded payments if requested.
563
                        if payment.Status != StatusSucceeded &&
4✔
564
                                !query.IncludeIncomplete {
4✔
UNCOV
565

×
UNCOV
566
                                return false, err
×
UNCOV
567
                        }
×
568

569
                        // Get the creation time in Unix seconds, this always
570
                        // rounds down the nanoseconds to full seconds.
571
                        createTime := payment.Info.CreationTime.Unix()
4✔
572

4✔
573
                        // Skip any payments that were created before the
4✔
574
                        // specified time.
4✔
575
                        if createTime < query.CreationDateStart {
8✔
576
                                return false, nil
4✔
577
                        }
4✔
578

579
                        // Skip any payments that were created after the
580
                        // specified time.
581
                        if query.CreationDateEnd != 0 &&
4✔
582
                                createTime > query.CreationDateEnd {
8✔
583

4✔
584
                                return false, nil
4✔
585
                        }
4✔
586

587
                        // At this point, we've exhausted the offset, so we'll
588
                        // begin collecting invoices found within the range.
589
                        resp.Payments = append(resp.Payments, payment)
4✔
590
                        return true, nil
4✔
591
                }
592

593
                // Create a paginator which reads from our sequence index bucket
594
                // with the parameters provided by the payments query.
595
                paginator := newPaginator(
4✔
596
                        indexes.ReadCursor(), query.Reversed, query.IndexOffset,
4✔
597
                        query.MaxPayments,
4✔
598
                )
4✔
599

4✔
600
                // Run a paginated query, adding payments to our response.
4✔
601
                if err := paginator.query(accumulatePayments); err != nil {
4✔
602
                        return err
×
603
                }
×
604

605
                // Counting the total number of payments is expensive, since we
606
                // literally have to traverse the cursor linearly, which can
607
                // take quite a while. So it's an optional query parameter.
608
                if query.CountTotal {
4✔
609
                        var (
×
610
                                totalPayments uint64
×
611
                                err           error
×
612
                        )
×
613
                        countFn := func(_, _ []byte) error {
×
614
                                totalPayments++
×
615

×
616
                                return nil
×
617
                        }
×
618

619
                        // In non-boltdb database backends, there's a faster
620
                        // ForAll query that allows for batch fetching items.
621
                        if fastBucket, ok := indexes.(kvdb.ExtendedRBucket); ok {
×
622
                                err = fastBucket.ForAll(countFn)
×
623
                        } else {
×
624
                                err = indexes.ForEach(countFn)
×
625
                        }
×
626
                        if err != nil {
×
627
                                return fmt.Errorf("error counting payments: %w",
×
628
                                        err)
×
629
                        }
×
630

631
                        resp.TotalCount = totalPayments
×
632
                }
633

634
                return nil
4✔
635
        }, func() {
4✔
636
                resp = PaymentsResponse{}
4✔
637
        }); err != nil {
4✔
638
                return resp, err
×
639
        }
×
640

641
        // Need to swap the payments slice order if reversed order.
642
        if query.Reversed {
4✔
UNCOV
643
                for l, r := 0, len(resp.Payments)-1; l < r; l, r = l+1, r-1 {
×
UNCOV
644
                        resp.Payments[l], resp.Payments[r] =
×
UNCOV
645
                                resp.Payments[r], resp.Payments[l]
×
UNCOV
646
                }
×
647
        }
648

649
        // Set the first and last index of the returned payments so that the
650
        // caller can resume from this point later on.
651
        if len(resp.Payments) > 0 {
8✔
652
                resp.FirstIndexOffset = resp.Payments[0].SequenceNum
4✔
653
                resp.LastIndexOffset =
4✔
654
                        resp.Payments[len(resp.Payments)-1].SequenceNum
4✔
655
        }
4✔
656

657
        return resp, nil
4✔
658
}
659

660
// fetchPaymentWithSequenceNumber get the payment which matches the payment hash
661
// *and* sequence number provided from the database. This is required because
662
// we previously had more than one payment per hash, so we have multiple indexes
663
// pointing to a single payment; we want to retrieve the correct one.
664
func fetchPaymentWithSequenceNumber(tx kvdb.RTx, paymentHash lntypes.Hash,
665
        sequenceNumber []byte) (*MPPayment, error) {
4✔
666

4✔
667
        // We can now lookup the payment keyed by its hash in
4✔
668
        // the payments root bucket.
4✔
669
        bucket, err := fetchPaymentBucket(tx, paymentHash)
4✔
670
        if err != nil {
4✔
671
                return nil, err
×
672
        }
×
673

674
        // A single payment hash can have multiple payments associated with it.
675
        // We lookup our sequence number first, to determine whether this is
676
        // the payment we are actually looking for.
677
        seqBytes := bucket.Get(paymentSequenceKey)
4✔
678
        if seqBytes == nil {
4✔
679
                return nil, ErrNoSequenceNumber
×
680
        }
×
681

682
        // If this top level payment has the sequence number we are looking for,
683
        // return it.
684
        if bytes.Equal(seqBytes, sequenceNumber) {
8✔
685
                return fetchPayment(bucket)
4✔
686
        }
4✔
687

688
        // If we were not looking for the top level payment, we are looking for
689
        // one of our duplicate payments. We need to iterate through the seq
690
        // numbers in this bucket to find the correct payments. If we do not
691
        // find a duplicate payments bucket here, something is wrong.
UNCOV
692
        dup := bucket.NestedReadBucket(duplicatePaymentsBucket)
×
UNCOV
693
        if dup == nil {
×
UNCOV
694
                return nil, ErrNoDuplicateBucket
×
UNCOV
695
        }
×
696

UNCOV
697
        var duplicatePayment *MPPayment
×
UNCOV
698
        err = dup.ForEach(func(k, v []byte) error {
×
UNCOV
699
                subBucket := dup.NestedReadBucket(k)
×
UNCOV
700
                if subBucket == nil {
×
701
                        // We one bucket for each duplicate to be found.
×
702
                        return ErrNoDuplicateNestedBucket
×
703
                }
×
704

UNCOV
705
                seqBytes := subBucket.Get(duplicatePaymentSequenceKey)
×
UNCOV
706
                if seqBytes == nil {
×
707
                        return err
×
708
                }
×
709

710
                // If this duplicate payment is not the sequence number we are
711
                // looking for, we can continue.
UNCOV
712
                if !bytes.Equal(seqBytes, sequenceNumber) {
×
UNCOV
713
                        return nil
×
UNCOV
714
                }
×
715

UNCOV
716
                duplicatePayment, err = fetchDuplicatePayment(subBucket)
×
UNCOV
717
                if err != nil {
×
718
                        return err
×
719
                }
×
720

UNCOV
721
                return nil
×
722
        })
UNCOV
723
        if err != nil {
×
724
                return nil, err
×
725
        }
×
726

727
        // If none of the duplicate payments matched our sequence number, we
728
        // failed to find the payment with this sequence number; something is
729
        // wrong.
UNCOV
730
        if duplicatePayment == nil {
×
UNCOV
731
                return nil, ErrDuplicateNotFound
×
UNCOV
732
        }
×
733

UNCOV
734
        return duplicatePayment, nil
×
735
}
736

737
// DeletePayment deletes a payment from the DB given its payment hash. If
738
// failedHtlcsOnly is set, only failed HTLC attempts of the payment will be
739
// deleted.
740
func (d *DB) DeletePayment(paymentHash lntypes.Hash,
UNCOV
741
        failedHtlcsOnly bool) error {
×
UNCOV
742

×
UNCOV
743
        return kvdb.Update(d, func(tx kvdb.RwTx) error {
×
UNCOV
744
                payments := tx.ReadWriteBucket(paymentsRootBucket)
×
UNCOV
745
                if payments == nil {
×
746
                        return nil
×
747
                }
×
748

UNCOV
749
                bucket := payments.NestedReadWriteBucket(paymentHash[:])
×
UNCOV
750
                if bucket == nil {
×
UNCOV
751
                        return fmt.Errorf("non bucket element in payments " +
×
UNCOV
752
                                "bucket")
×
UNCOV
753
                }
×
754

755
                // If the status is InFlight, we cannot safely delete
756
                // the payment information, so we return early.
UNCOV
757
                paymentStatus, err := fetchPaymentStatus(bucket)
×
UNCOV
758
                if err != nil {
×
759
                        return err
×
760
                }
×
761

762
                // If the payment has inflight HTLCs, we cannot safely delete
763
                // the payment information, so we return an error.
UNCOV
764
                if err := paymentStatus.removable(); err != nil {
×
UNCOV
765
                        return fmt.Errorf("payment '%v' has inflight HTLCs"+
×
UNCOV
766
                                "and therefore cannot be deleted: %w",
×
UNCOV
767
                                paymentHash.String(), err)
×
UNCOV
768
                }
×
769

770
                // Delete the failed HTLC attempts we found.
UNCOV
771
                if failedHtlcsOnly {
×
UNCOV
772
                        toDelete, err := fetchFailedHtlcKeys(bucket)
×
UNCOV
773
                        if err != nil {
×
774
                                return err
×
775
                        }
×
776

UNCOV
777
                        htlcsBucket := bucket.NestedReadWriteBucket(
×
UNCOV
778
                                paymentHtlcsBucket,
×
UNCOV
779
                        )
×
UNCOV
780

×
UNCOV
781
                        for _, htlcID := range toDelete {
×
UNCOV
782
                                err = htlcsBucket.Delete(
×
UNCOV
783
                                        htlcBucketKey(htlcAttemptInfoKey, htlcID),
×
UNCOV
784
                                )
×
UNCOV
785
                                if err != nil {
×
786
                                        return err
×
787
                                }
×
788

UNCOV
789
                                err = htlcsBucket.Delete(
×
UNCOV
790
                                        htlcBucketKey(htlcFailInfoKey, htlcID),
×
UNCOV
791
                                )
×
UNCOV
792
                                if err != nil {
×
793
                                        return err
×
794
                                }
×
795

UNCOV
796
                                err = htlcsBucket.Delete(
×
UNCOV
797
                                        htlcBucketKey(htlcSettleInfoKey, htlcID),
×
UNCOV
798
                                )
×
UNCOV
799
                                if err != nil {
×
800
                                        return err
×
801
                                }
×
802
                        }
803

UNCOV
804
                        return nil
×
805
                }
806

UNCOV
807
                seqNrs, err := fetchSequenceNumbers(bucket)
×
UNCOV
808
                if err != nil {
×
809
                        return err
×
810
                }
×
811

UNCOV
812
                if err := payments.DeleteNestedBucket(paymentHash[:]); err != nil {
×
813
                        return err
×
814
                }
×
815

UNCOV
816
                indexBucket := tx.ReadWriteBucket(paymentsIndexBucket)
×
UNCOV
817
                for _, k := range seqNrs {
×
UNCOV
818
                        if err := indexBucket.Delete(k); err != nil {
×
819
                                return err
×
820
                        }
×
821
                }
822

UNCOV
823
                return nil
×
UNCOV
824
        }, func() {})
×
825
}
826

827
// DeletePayments deletes all completed and failed payments from the DB. If
828
// failedOnly is set, only failed payments will be considered for deletion. If
829
// failedHtlcsOnly is set, the payment itself won't be deleted, only failed HTLC
830
// attempts. The method returns the number of deleted payments, which is always
831
// 0 if failedHtlcsOnly is set.
832
func (d *DB) DeletePayments(failedOnly, failedHtlcsOnly bool) (int, error) {
4✔
833
        var numPayments int
4✔
834
        err := kvdb.Update(d, func(tx kvdb.RwTx) error {
8✔
835
                payments := tx.ReadWriteBucket(paymentsRootBucket)
4✔
836
                if payments == nil {
8✔
837
                        return nil
4✔
838
                }
4✔
839

840
                var (
4✔
841
                        // deleteBuckets is the set of payment buckets we need
4✔
842
                        // to delete.
4✔
843
                        deleteBuckets [][]byte
4✔
844

4✔
845
                        // deleteIndexes is the set of indexes pointing to these
4✔
846
                        // payments that need to be deleted.
4✔
847
                        deleteIndexes [][]byte
4✔
848

4✔
849
                        // deleteHtlcs maps a payment hash to the HTLC IDs we
4✔
850
                        // want to delete for that payment.
4✔
851
                        deleteHtlcs = make(map[lntypes.Hash][][]byte)
4✔
852
                )
4✔
853
                err := payments.ForEach(func(k, _ []byte) error {
8✔
854
                        bucket := payments.NestedReadBucket(k)
4✔
855
                        if bucket == nil {
4✔
856
                                // We only expect sub-buckets to be found in
×
857
                                // this top-level bucket.
×
858
                                return fmt.Errorf("non bucket element in " +
×
859
                                        "payments bucket")
×
860
                        }
×
861

862
                        // If the status is InFlight, we cannot safely delete
863
                        // the payment information, so we return early.
864
                        paymentStatus, err := fetchPaymentStatus(bucket)
4✔
865
                        if err != nil {
4✔
866
                                return err
×
867
                        }
×
868

869
                        // If the payment has inflight HTLCs, we cannot safely
870
                        // delete the payment information, so we return an nil
871
                        // to skip it.
872
                        if err := paymentStatus.removable(); err != nil {
4✔
UNCOV
873
                                return nil
×
UNCOV
874
                        }
×
875

876
                        // If we requested to only delete failed payments, we
877
                        // can return if this one is not.
878
                        if failedOnly && paymentStatus != StatusFailed {
4✔
UNCOV
879
                                return nil
×
UNCOV
880
                        }
×
881

882
                        // If we are only deleting failed HTLCs, fetch them.
883
                        if failedHtlcsOnly {
4✔
UNCOV
884
                                toDelete, err := fetchFailedHtlcKeys(bucket)
×
UNCOV
885
                                if err != nil {
×
886
                                        return err
×
887
                                }
×
888

UNCOV
889
                                hash, err := lntypes.MakeHash(k)
×
UNCOV
890
                                if err != nil {
×
891
                                        return err
×
892
                                }
×
893

UNCOV
894
                                deleteHtlcs[hash] = toDelete
×
UNCOV
895

×
UNCOV
896
                                // We return, we are only deleting attempts.
×
UNCOV
897
                                return nil
×
898
                        }
899

900
                        // Add the bucket to the set of buckets we can delete.
901
                        deleteBuckets = append(deleteBuckets, k)
4✔
902

4✔
903
                        // Get all the sequence number associated with the
4✔
904
                        // payment, including duplicates.
4✔
905
                        seqNrs, err := fetchSequenceNumbers(bucket)
4✔
906
                        if err != nil {
4✔
907
                                return err
×
908
                        }
×
909

910
                        deleteIndexes = append(deleteIndexes, seqNrs...)
4✔
911
                        numPayments++
4✔
912
                        return nil
4✔
913
                })
914
                if err != nil {
4✔
915
                        return err
×
916
                }
×
917

918
                // Delete the failed HTLC attempts we found.
919
                for hash, htlcIDs := range deleteHtlcs {
4✔
UNCOV
920
                        bucket := payments.NestedReadWriteBucket(hash[:])
×
UNCOV
921
                        htlcsBucket := bucket.NestedReadWriteBucket(
×
UNCOV
922
                                paymentHtlcsBucket,
×
UNCOV
923
                        )
×
UNCOV
924

×
UNCOV
925
                        for _, aid := range htlcIDs {
×
UNCOV
926
                                if err := htlcsBucket.Delete(
×
UNCOV
927
                                        htlcBucketKey(htlcAttemptInfoKey, aid),
×
UNCOV
928
                                ); err != nil {
×
929
                                        return err
×
930
                                }
×
931

UNCOV
932
                                if err := htlcsBucket.Delete(
×
UNCOV
933
                                        htlcBucketKey(htlcFailInfoKey, aid),
×
UNCOV
934
                                ); err != nil {
×
935
                                        return err
×
936
                                }
×
937

UNCOV
938
                                if err := htlcsBucket.Delete(
×
UNCOV
939
                                        htlcBucketKey(htlcSettleInfoKey, aid),
×
UNCOV
940
                                ); err != nil {
×
941
                                        return err
×
942
                                }
×
943
                        }
944
                }
945

946
                for _, k := range deleteBuckets {
8✔
947
                        if err := payments.DeleteNestedBucket(k); err != nil {
4✔
948
                                return err
×
949
                        }
×
950
                }
951

952
                // Get our index bucket and delete all indexes pointing to the
953
                // payments we are deleting.
954
                indexBucket := tx.ReadWriteBucket(paymentsIndexBucket)
4✔
955
                for _, k := range deleteIndexes {
8✔
956
                        if err := indexBucket.Delete(k); err != nil {
4✔
957
                                return err
×
958
                        }
×
959
                }
960

961
                return nil
4✔
962
        }, func() {
4✔
963
                numPayments = 0
4✔
964
        })
4✔
965
        if err != nil {
4✔
966
                return 0, err
×
967
        }
×
968

969
        return numPayments, nil
4✔
970
}
971

972
// fetchSequenceNumbers fetches all the sequence numbers associated with a
973
// payment, including those belonging to any duplicate payments.
974
func fetchSequenceNumbers(paymentBucket kvdb.RBucket) ([][]byte, error) {
4✔
975
        seqNum := paymentBucket.Get(paymentSequenceKey)
4✔
976
        if seqNum == nil {
4✔
977
                return nil, errors.New("expected sequence number")
×
978
        }
×
979

980
        sequenceNumbers := [][]byte{seqNum}
4✔
981

4✔
982
        // Get the duplicate payments bucket, if it has no duplicates, just
4✔
983
        // return early with the payment sequence number.
4✔
984
        duplicates := paymentBucket.NestedReadBucket(duplicatePaymentsBucket)
4✔
985
        if duplicates == nil {
8✔
986
                return sequenceNumbers, nil
4✔
987
        }
4✔
988

989
        // If we do have duplicated, they are keyed by sequence number, so we
990
        // iterate through the duplicates bucket and add them to our set of
991
        // sequence numbers.
UNCOV
992
        if err := duplicates.ForEach(func(k, v []byte) error {
×
UNCOV
993
                sequenceNumbers = append(sequenceNumbers, k)
×
UNCOV
994
                return nil
×
UNCOV
995
        }); err != nil {
×
996
                return nil, err
×
997
        }
×
998

UNCOV
999
        return sequenceNumbers, nil
×
1000
}
1001

1002
// nolint: dupl
1003
func serializePaymentCreationInfo(w io.Writer, c *PaymentCreationInfo) error {
4✔
1004
        var scratch [8]byte
4✔
1005

4✔
1006
        if _, err := w.Write(c.PaymentIdentifier[:]); err != nil {
4✔
1007
                return err
×
1008
        }
×
1009

1010
        byteOrder.PutUint64(scratch[:], uint64(c.Value))
4✔
1011
        if _, err := w.Write(scratch[:]); err != nil {
4✔
1012
                return err
×
1013
        }
×
1014

1015
        if err := serializeTime(w, c.CreationTime); err != nil {
4✔
1016
                return err
×
1017
        }
×
1018

1019
        byteOrder.PutUint32(scratch[:4], uint32(len(c.PaymentRequest)))
4✔
1020
        if _, err := w.Write(scratch[:4]); err != nil {
4✔
1021
                return err
×
1022
        }
×
1023

1024
        if _, err := w.Write(c.PaymentRequest[:]); err != nil {
4✔
1025
                return err
×
1026
        }
×
1027

1028
        // Any remaining bytes are TLV encoded records. Currently, these are
1029
        // only the custom records provided by the user to be sent to the first
1030
        // hop. But this can easily be extended with further records by merging
1031
        // the records into a single TLV stream.
1032
        err := c.FirstHopCustomRecords.SerializeTo(w)
4✔
1033
        if err != nil {
4✔
1034
                return err
×
1035
        }
×
1036

1037
        return nil
4✔
1038
}
1039

1040
func deserializePaymentCreationInfo(r io.Reader) (*PaymentCreationInfo,
1041
        error) {
4✔
1042

4✔
1043
        var scratch [8]byte
4✔
1044

4✔
1045
        c := &PaymentCreationInfo{}
4✔
1046

4✔
1047
        if _, err := io.ReadFull(r, c.PaymentIdentifier[:]); err != nil {
4✔
1048
                return nil, err
×
1049
        }
×
1050

1051
        if _, err := io.ReadFull(r, scratch[:]); err != nil {
4✔
1052
                return nil, err
×
1053
        }
×
1054
        c.Value = lnwire.MilliSatoshi(byteOrder.Uint64(scratch[:]))
4✔
1055

4✔
1056
        creationTime, err := deserializeTime(r)
4✔
1057
        if err != nil {
4✔
1058
                return nil, err
×
1059
        }
×
1060
        c.CreationTime = creationTime
4✔
1061

4✔
1062
        if _, err := io.ReadFull(r, scratch[:4]); err != nil {
4✔
1063
                return nil, err
×
1064
        }
×
1065

1066
        reqLen := uint32(byteOrder.Uint32(scratch[:4]))
4✔
1067
        payReq := make([]byte, reqLen)
4✔
1068
        if reqLen > 0 {
8✔
1069
                if _, err := io.ReadFull(r, payReq); err != nil {
4✔
1070
                        return nil, err
×
1071
                }
×
1072
        }
1073
        c.PaymentRequest = payReq
4✔
1074

4✔
1075
        // Any remaining bytes are TLV encoded records. Currently, these are
4✔
1076
        // only the custom records provided by the user to be sent to the first
4✔
1077
        // hop. But this can easily be extended with further records by merging
4✔
1078
        // the records into a single TLV stream.
4✔
1079
        c.FirstHopCustomRecords, err = lnwire.ParseCustomRecordsFrom(r)
4✔
1080
        if err != nil {
4✔
1081
                return nil, err
×
1082
        }
×
1083

1084
        return c, nil
4✔
1085
}
1086

1087
func serializeHTLCAttemptInfo(w io.Writer, a *HTLCAttemptInfo) error {
4✔
1088
        if err := WriteElements(w, a.sessionKey); err != nil {
4✔
1089
                return err
×
1090
        }
×
1091

1092
        if err := SerializeRoute(w, a.Route); err != nil {
4✔
1093
                return err
×
1094
        }
×
1095

1096
        if err := serializeTime(w, a.AttemptTime); err != nil {
4✔
1097
                return err
×
1098
        }
×
1099

1100
        // If the hash is nil we can just return.
1101
        if a.Hash == nil {
4✔
UNCOV
1102
                return nil
×
UNCOV
1103
        }
×
1104

1105
        if _, err := w.Write(a.Hash[:]); err != nil {
4✔
1106
                return err
×
1107
        }
×
1108

1109
        // Merge the fixed/known records together with the custom records to
1110
        // serialize them as a single blob. We can't do this in SerializeRoute
1111
        // because we're in the middle of the byte stream there. We can only do
1112
        // TLV serialization at the end of the stream, since EOF is allowed for
1113
        // a stream if no more data is expected.
1114
        producers := []tlv.RecordProducer{
4✔
1115
                &a.Route.FirstHopAmount,
4✔
1116
        }
4✔
1117
        tlvData, err := lnwire.MergeAndEncode(
4✔
1118
                producers, nil, a.Route.FirstHopWireCustomRecords,
4✔
1119
        )
4✔
1120
        if err != nil {
4✔
1121
                return err
×
1122
        }
×
1123

1124
        if _, err := w.Write(tlvData); err != nil {
4✔
1125
                return err
×
1126
        }
×
1127

1128
        return nil
4✔
1129
}
1130

1131
func deserializeHTLCAttemptInfo(r io.Reader) (*HTLCAttemptInfo, error) {
4✔
1132
        a := &HTLCAttemptInfo{}
4✔
1133
        err := ReadElements(r, &a.sessionKey)
4✔
1134
        if err != nil {
4✔
1135
                return nil, err
×
1136
        }
×
1137

1138
        a.Route, err = DeserializeRoute(r)
4✔
1139
        if err != nil {
4✔
1140
                return nil, err
×
1141
        }
×
1142

1143
        a.AttemptTime, err = deserializeTime(r)
4✔
1144
        if err != nil {
4✔
1145
                return nil, err
×
1146
        }
×
1147

1148
        hash := lntypes.Hash{}
4✔
1149
        _, err = io.ReadFull(r, hash[:])
4✔
1150

4✔
1151
        switch {
4✔
1152
        // Older payment attempts wouldn't have the hash set, in which case we
1153
        // can just return.
UNCOV
1154
        case err == io.EOF, err == io.ErrUnexpectedEOF:
×
UNCOV
1155
                return a, nil
×
1156

1157
        case err != nil:
×
1158
                return nil, err
×
1159

1160
        default:
4✔
1161
        }
1162

1163
        a.Hash = &hash
4✔
1164

4✔
1165
        // Read any remaining data (if any) and parse it into the known records
4✔
1166
        // and custom records.
4✔
1167
        extraData, err := io.ReadAll(r)
4✔
1168
        if err != nil {
4✔
1169
                return nil, err
×
1170
        }
×
1171

1172
        customRecords, _, _, err := lnwire.ParseAndExtractCustomRecords(
4✔
1173
                extraData, &a.Route.FirstHopAmount,
4✔
1174
        )
4✔
1175
        if err != nil {
4✔
1176
                return nil, err
×
1177
        }
×
1178

1179
        a.Route.FirstHopWireCustomRecords = customRecords
4✔
1180

4✔
1181
        return a, nil
4✔
1182
}
1183

1184
func serializeHop(w io.Writer, h *route.Hop) error {
4✔
1185
        if err := WriteElements(w,
4✔
1186
                h.PubKeyBytes[:],
4✔
1187
                h.ChannelID,
4✔
1188
                h.OutgoingTimeLock,
4✔
1189
                h.AmtToForward,
4✔
1190
        ); err != nil {
4✔
1191
                return err
×
1192
        }
×
1193

1194
        if err := binary.Write(w, byteOrder, h.LegacyPayload); err != nil {
4✔
1195
                return err
×
1196
        }
×
1197

1198
        // For legacy payloads, we don't need to write any TLV records, so
1199
        // we'll write a zero indicating the our serialized TLV map has no
1200
        // records.
1201
        if h.LegacyPayload {
4✔
UNCOV
1202
                return WriteElements(w, uint32(0))
×
UNCOV
1203
        }
×
1204

1205
        // Gather all non-primitive TLV records so that they can be serialized
1206
        // as a single blob.
1207
        //
1208
        // TODO(conner): add migration to unify all fields in a single TLV
1209
        // blobs. The split approach will cause headaches down the road as more
1210
        // fields are added, which we can avoid by having a single TLV stream
1211
        // for all payload fields.
1212
        var records []tlv.Record
4✔
1213
        if h.MPP != nil {
8✔
1214
                records = append(records, h.MPP.Record())
4✔
1215
        }
4✔
1216

1217
        // Add blinding point and encrypted data if present.
1218
        if h.EncryptedData != nil {
8✔
1219
                records = append(records, record.NewEncryptedDataRecord(
4✔
1220
                        &h.EncryptedData,
4✔
1221
                ))
4✔
1222
        }
4✔
1223

1224
        if h.BlindingPoint != nil {
8✔
1225
                records = append(records, record.NewBlindingPointRecord(
4✔
1226
                        &h.BlindingPoint,
4✔
1227
                ))
4✔
1228
        }
4✔
1229

1230
        if h.AMP != nil {
8✔
1231
                records = append(records, h.AMP.Record())
4✔
1232
        }
4✔
1233

1234
        if h.Metadata != nil {
4✔
UNCOV
1235
                records = append(records, record.NewMetadataRecord(&h.Metadata))
×
UNCOV
1236
        }
×
1237

1238
        if h.TotalAmtMsat != 0 {
8✔
1239
                totalMsatInt := uint64(h.TotalAmtMsat)
4✔
1240
                records = append(
4✔
1241
                        records, record.NewTotalAmtMsatBlinded(&totalMsatInt),
4✔
1242
                )
4✔
1243
        }
4✔
1244

1245
        // Final sanity check to absolutely rule out custom records that are not
1246
        // custom and write into the standard range.
1247
        if err := h.CustomRecords.Validate(); err != nil {
4✔
1248
                return err
×
1249
        }
×
1250

1251
        // Convert custom records to tlv and add to the record list.
1252
        // MapToRecords sorts the list, so adding it here will keep the list
1253
        // canonical.
1254
        tlvRecords := tlv.MapToRecords(h.CustomRecords)
4✔
1255
        records = append(records, tlvRecords...)
4✔
1256

4✔
1257
        // Otherwise, we'll transform our slice of records into a map of the
4✔
1258
        // raw bytes, then serialize them in-line with a length (number of
4✔
1259
        // elements) prefix.
4✔
1260
        mapRecords, err := tlv.RecordsToMap(records)
4✔
1261
        if err != nil {
4✔
1262
                return err
×
1263
        }
×
1264

1265
        numRecords := uint32(len(mapRecords))
4✔
1266
        if err := WriteElements(w, numRecords); err != nil {
4✔
1267
                return err
×
1268
        }
×
1269

1270
        for recordType, rawBytes := range mapRecords {
8✔
1271
                if err := WriteElements(w, recordType); err != nil {
4✔
1272
                        return err
×
1273
                }
×
1274

1275
                if err := wire.WriteVarBytes(w, 0, rawBytes); err != nil {
4✔
1276
                        return err
×
1277
                }
×
1278
        }
1279

1280
        return nil
4✔
1281
}
1282

1283
// maxOnionPayloadSize is the largest Sphinx payload possible, so we don't need
1284
// to read/write a TLV stream larger than this.
1285
const maxOnionPayloadSize = 1300
1286

1287
func deserializeHop(r io.Reader) (*route.Hop, error) {
4✔
1288
        h := &route.Hop{}
4✔
1289

4✔
1290
        var pub []byte
4✔
1291
        if err := ReadElements(r, &pub); err != nil {
4✔
1292
                return nil, err
×
1293
        }
×
1294
        copy(h.PubKeyBytes[:], pub)
4✔
1295

4✔
1296
        if err := ReadElements(r,
4✔
1297
                &h.ChannelID, &h.OutgoingTimeLock, &h.AmtToForward,
4✔
1298
        ); err != nil {
4✔
1299
                return nil, err
×
1300
        }
×
1301

1302
        // TODO(roasbeef): change field to allow LegacyPayload false to be the
1303
        // legacy default?
1304
        err := binary.Read(r, byteOrder, &h.LegacyPayload)
4✔
1305
        if err != nil {
4✔
1306
                return nil, err
×
1307
        }
×
1308

1309
        var numElements uint32
4✔
1310
        if err := ReadElements(r, &numElements); err != nil {
4✔
1311
                return nil, err
×
1312
        }
×
1313

1314
        // If there're no elements, then we can return early.
1315
        if numElements == 0 {
8✔
1316
                return h, nil
4✔
1317
        }
4✔
1318

1319
        tlvMap := make(map[uint64][]byte)
4✔
1320
        for i := uint32(0); i < numElements; i++ {
8✔
1321
                var tlvType uint64
4✔
1322
                if err := ReadElements(r, &tlvType); err != nil {
4✔
1323
                        return nil, err
×
1324
                }
×
1325

1326
                rawRecordBytes, err := wire.ReadVarBytes(
4✔
1327
                        r, 0, maxOnionPayloadSize, "tlv",
4✔
1328
                )
4✔
1329
                if err != nil {
4✔
1330
                        return nil, err
×
1331
                }
×
1332

1333
                tlvMap[tlvType] = rawRecordBytes
4✔
1334
        }
1335

1336
        // If the MPP type is present, remove it from the generic TLV map and
1337
        // parse it back into a proper MPP struct.
1338
        //
1339
        // TODO(conner): add migration to unify all fields in a single TLV
1340
        // blobs. The split approach will cause headaches down the road as more
1341
        // fields are added, which we can avoid by having a single TLV stream
1342
        // for all payload fields.
1343
        mppType := uint64(record.MPPOnionType)
4✔
1344
        if mppBytes, ok := tlvMap[mppType]; ok {
8✔
1345
                delete(tlvMap, mppType)
4✔
1346

4✔
1347
                var (
4✔
1348
                        mpp    = &record.MPP{}
4✔
1349
                        mppRec = mpp.Record()
4✔
1350
                        r      = bytes.NewReader(mppBytes)
4✔
1351
                )
4✔
1352
                err := mppRec.Decode(r, uint64(len(mppBytes)))
4✔
1353
                if err != nil {
4✔
1354
                        return nil, err
×
1355
                }
×
1356
                h.MPP = mpp
4✔
1357
        }
1358

1359
        // If encrypted data or blinding key are present, remove them from
1360
        // the TLV map and parse into proper types.
1361
        encryptedDataType := uint64(record.EncryptedDataOnionType)
4✔
1362
        if data, ok := tlvMap[encryptedDataType]; ok {
8✔
1363
                delete(tlvMap, encryptedDataType)
4✔
1364
                h.EncryptedData = data
4✔
1365
        }
4✔
1366

1367
        blindingType := uint64(record.BlindingPointOnionType)
4✔
1368
        if blindingPoint, ok := tlvMap[blindingType]; ok {
8✔
1369
                delete(tlvMap, blindingType)
4✔
1370

4✔
1371
                h.BlindingPoint, err = btcec.ParsePubKey(blindingPoint)
4✔
1372
                if err != nil {
4✔
1373
                        return nil, fmt.Errorf("invalid blinding point: %w",
×
1374
                                err)
×
1375
                }
×
1376
        }
1377

1378
        ampType := uint64(record.AMPOnionType)
4✔
1379
        if ampBytes, ok := tlvMap[ampType]; ok {
8✔
1380
                delete(tlvMap, ampType)
4✔
1381

4✔
1382
                var (
4✔
1383
                        amp    = &record.AMP{}
4✔
1384
                        ampRec = amp.Record()
4✔
1385
                        r      = bytes.NewReader(ampBytes)
4✔
1386
                )
4✔
1387
                err := ampRec.Decode(r, uint64(len(ampBytes)))
4✔
1388
                if err != nil {
4✔
1389
                        return nil, err
×
1390
                }
×
1391
                h.AMP = amp
4✔
1392
        }
1393

1394
        // If the metadata type is present, remove it from the tlv map and
1395
        // populate directly on the hop.
1396
        metadataType := uint64(record.MetadataOnionType)
4✔
1397
        if metadata, ok := tlvMap[metadataType]; ok {
4✔
UNCOV
1398
                delete(tlvMap, metadataType)
×
UNCOV
1399

×
UNCOV
1400
                h.Metadata = metadata
×
UNCOV
1401
        }
×
1402

1403
        totalAmtMsatType := uint64(record.TotalAmtMsatBlindedType)
4✔
1404
        if totalAmtMsat, ok := tlvMap[totalAmtMsatType]; ok {
8✔
1405
                delete(tlvMap, totalAmtMsatType)
4✔
1406

4✔
1407
                var (
4✔
1408
                        totalAmtMsatInt uint64
4✔
1409
                        buf             [8]byte
4✔
1410
                )
4✔
1411
                if err := tlv.DTUint64(
4✔
1412
                        bytes.NewReader(totalAmtMsat),
4✔
1413
                        &totalAmtMsatInt,
4✔
1414
                        &buf,
4✔
1415
                        uint64(len(totalAmtMsat)),
4✔
1416
                ); err != nil {
4✔
1417
                        return nil, err
×
1418
                }
×
1419

1420
                h.TotalAmtMsat = lnwire.MilliSatoshi(totalAmtMsatInt)
4✔
1421
        }
1422

1423
        h.CustomRecords = tlvMap
4✔
1424

4✔
1425
        return h, nil
4✔
1426
}
1427

1428
// SerializeRoute serializes a route.
1429
func SerializeRoute(w io.Writer, r route.Route) error {
4✔
1430
        if err := WriteElements(w,
4✔
1431
                r.TotalTimeLock, r.TotalAmount, r.SourcePubKey[:],
4✔
1432
        ); err != nil {
4✔
1433
                return err
×
1434
        }
×
1435

1436
        if err := WriteElements(w, uint32(len(r.Hops))); err != nil {
4✔
1437
                return err
×
1438
        }
×
1439

1440
        for _, h := range r.Hops {
8✔
1441
                if err := serializeHop(w, h); err != nil {
4✔
1442
                        return err
×
1443
                }
×
1444
        }
1445

1446
        // Any new/extra TLV data is encoded in serializeHTLCAttemptInfo!
1447

1448
        return nil
4✔
1449
}
1450

1451
// DeserializeRoute deserializes a route.
1452
func DeserializeRoute(r io.Reader) (route.Route, error) {
4✔
1453
        rt := route.Route{}
4✔
1454
        if err := ReadElements(r,
4✔
1455
                &rt.TotalTimeLock, &rt.TotalAmount,
4✔
1456
        ); err != nil {
4✔
1457
                return rt, err
×
1458
        }
×
1459

1460
        var pub []byte
4✔
1461
        if err := ReadElements(r, &pub); err != nil {
4✔
1462
                return rt, err
×
1463
        }
×
1464
        copy(rt.SourcePubKey[:], pub)
4✔
1465

4✔
1466
        var numHops uint32
4✔
1467
        if err := ReadElements(r, &numHops); err != nil {
4✔
1468
                return rt, err
×
1469
        }
×
1470

1471
        var hops []*route.Hop
4✔
1472
        for i := uint32(0); i < numHops; i++ {
8✔
1473
                hop, err := deserializeHop(r)
4✔
1474
                if err != nil {
4✔
1475
                        return rt, err
×
1476
                }
×
1477
                hops = append(hops, hop)
4✔
1478
        }
1479
        rt.Hops = hops
4✔
1480

4✔
1481
        // Any new/extra TLV data is decoded in deserializeHTLCAttemptInfo!
4✔
1482

4✔
1483
        return rt, nil
4✔
1484
}
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