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

lightningnetwork / lnd / 15561477203

10 Jun 2025 01:54PM UTC coverage: 58.351% (-10.1%) from 68.487%
15561477203

Pull #9356

github

web-flow
Merge 6440b25db into c6d6d4c0b
Pull Request #9356: lnrpc: add incoming/outgoing channel ids filter to forwarding history request

33 of 36 new or added lines in 2 files covered. (91.67%)

28366 existing lines in 455 files now uncovered.

97715 of 167461 relevant lines covered (58.35%)

1.81 hits per line

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

46.69
/lnwire/onion_error.go
1
package lnwire
2

3
import (
4
        "bufio"
5
        "bytes"
6
        "crypto/sha256"
7
        "encoding/binary"
8
        "fmt"
9
        "io"
10

11
        "github.com/davecgh/go-spew/spew"
12
        "github.com/go-errors/errors"
13
        "github.com/lightningnetwork/lnd/fn/v2"
14
        "github.com/lightningnetwork/lnd/tlv"
15
)
16

17
// FailureMessage represents the onion failure object identified by its unique
18
// failure code.
19
type FailureMessage interface {
20
        // Code returns a failure code describing the exact nature of the
21
        // error.
22
        Code() FailCode
23

24
        // Error returns a human readable string describing the error. With
25
        // this method, the FailureMessage interface meets the built-in error
26
        // interface.
27
        Error() string
28
}
29

30
// FailureMessageLength is the size of the failure message plus the size of
31
// padding. The FailureMessage message should always be EXACTLY this size.
32
const FailureMessageLength = 256
33

34
const (
35
        // FlagBadOnion error flag describes an unparsable, encrypted by
36
        // previous node.
37
        FlagBadOnion FailCode = 0x8000
38

39
        // FlagPerm error flag indicates a permanent failure.
40
        FlagPerm FailCode = 0x4000
41

42
        // FlagNode error flag indicates a node failure.
43
        FlagNode FailCode = 0x2000
44

45
        // FlagUpdate error flag indicates a new channel update is enclosed
46
        // within the error.
47
        FlagUpdate FailCode = 0x1000
48
)
49

50
// FailCode specifies the precise reason that an upstream HTLC was canceled.
51
// Each UpdateFailHTLC message carries a FailCode which is to be passed
52
// backwards, encrypted at each step back to the source of the HTLC within the
53
// route.
54
type FailCode uint16
55

56
// The currently defined onion failure types within this current version of the
57
// Lightning protocol.
58
const (
59
        CodeNone                             FailCode = 0
60
        CodeInvalidRealm                              = FlagBadOnion | 1
61
        CodeTemporaryNodeFailure                      = FlagNode | 2
62
        CodePermanentNodeFailure                      = FlagPerm | FlagNode | 2
63
        CodeRequiredNodeFeatureMissing                = FlagPerm | FlagNode | 3
64
        CodeInvalidOnionVersion                       = FlagBadOnion | FlagPerm | 4
65
        CodeInvalidOnionHmac                          = FlagBadOnion | FlagPerm | 5
66
        CodeInvalidOnionKey                           = FlagBadOnion | FlagPerm | 6
67
        CodeTemporaryChannelFailure                   = FlagUpdate | 7
68
        CodePermanentChannelFailure                   = FlagPerm | 8
69
        CodeRequiredChannelFeatureMissing             = FlagPerm | 9
70
        CodeUnknownNextPeer                           = FlagPerm | 10
71
        CodeAmountBelowMinimum                        = FlagUpdate | 11
72
        CodeFeeInsufficient                           = FlagUpdate | 12
73
        CodeIncorrectCltvExpiry                       = FlagUpdate | 13
74
        CodeExpiryTooSoon                             = FlagUpdate | 14
75
        CodeChannelDisabled                           = FlagUpdate | 20
76
        CodeIncorrectOrUnknownPaymentDetails          = FlagPerm | 15
77
        CodeIncorrectPaymentAmount                    = FlagPerm | 16
78
        CodeFinalExpiryTooSoon               FailCode = 17
79
        CodeFinalIncorrectCltvExpiry         FailCode = 18
80
        CodeFinalIncorrectHtlcAmount         FailCode = 19
81
        CodeExpiryTooFar                     FailCode = 21
82
        CodeInvalidOnionPayload                       = FlagPerm | 22
83
        CodeMPPTimeout                       FailCode = 23
84
        CodeInvalidBlinding                           = FlagBadOnion | FlagPerm | 24 //nolint:ll
85
)
86

87
// String returns the string representation of the failure code.
88
func (c FailCode) String() string {
3✔
89
        switch c {
3✔
UNCOV
90
        case CodeInvalidRealm:
×
UNCOV
91
                return "InvalidRealm"
×
92

UNCOV
93
        case CodeTemporaryNodeFailure:
×
UNCOV
94
                return "TemporaryNodeFailure"
×
95

UNCOV
96
        case CodePermanentNodeFailure:
×
UNCOV
97
                return "PermanentNodeFailure"
×
98

UNCOV
99
        case CodeRequiredNodeFeatureMissing:
×
UNCOV
100
                return "RequiredNodeFeatureMissing"
×
101

102
        case CodeInvalidOnionVersion:
3✔
103
                return "InvalidOnionVersion"
3✔
104

UNCOV
105
        case CodeInvalidOnionHmac:
×
UNCOV
106
                return "InvalidOnionHmac"
×
107

UNCOV
108
        case CodeInvalidOnionKey:
×
UNCOV
109
                return "InvalidOnionKey"
×
110

UNCOV
111
        case CodeTemporaryChannelFailure:
×
UNCOV
112
                return "TemporaryChannelFailure"
×
113

114
        case CodePermanentChannelFailure:
3✔
115
                return "PermanentChannelFailure"
3✔
116

UNCOV
117
        case CodeRequiredChannelFeatureMissing:
×
UNCOV
118
                return "RequiredChannelFeatureMissing"
×
119

120
        case CodeUnknownNextPeer:
3✔
121
                return "UnknownNextPeer"
3✔
122

UNCOV
123
        case CodeAmountBelowMinimum:
×
UNCOV
124
                return "AmountBelowMinimum"
×
125

UNCOV
126
        case CodeFeeInsufficient:
×
UNCOV
127
                return "FeeInsufficient"
×
128

UNCOV
129
        case CodeIncorrectCltvExpiry:
×
UNCOV
130
                return "IncorrectCltvExpiry"
×
131

UNCOV
132
        case CodeIncorrectPaymentAmount:
×
UNCOV
133
                return "IncorrectPaymentAmount"
×
134

UNCOV
135
        case CodeExpiryTooSoon:
×
UNCOV
136
                return "ExpiryTooSoon"
×
137

UNCOV
138
        case CodeChannelDisabled:
×
UNCOV
139
                return "ChannelDisabled"
×
140

141
        case CodeIncorrectOrUnknownPaymentDetails:
3✔
142
                return "IncorrectOrUnknownPaymentDetails"
3✔
143

UNCOV
144
        case CodeFinalExpiryTooSoon:
×
UNCOV
145
                return "FinalExpiryTooSoon"
×
146

UNCOV
147
        case CodeFinalIncorrectCltvExpiry:
×
UNCOV
148
                return "FinalIncorrectCltvExpiry"
×
149

UNCOV
150
        case CodeFinalIncorrectHtlcAmount:
×
UNCOV
151
                return "FinalIncorrectHtlcAmount"
×
152

153
        case CodeExpiryTooFar:
×
154
                return "ExpiryTooFar"
×
155

156
        case CodeInvalidOnionPayload:
3✔
157
                return "InvalidOnionPayload"
3✔
158

UNCOV
159
        case CodeMPPTimeout:
×
UNCOV
160
                return "MPPTimeout"
×
161

162
        case CodeInvalidBlinding:
3✔
163
                return "InvalidBlinding"
3✔
164

165
        default:
3✔
166
                return "<unknown>"
3✔
167
        }
168
}
169

170
// FailInvalidRealm is returned if the realm byte is unknown.
171
//
172
// NOTE: May be returned by any node in the payment route.
173
type FailInvalidRealm struct{}
174

175
// Returns a human readable string describing the target FailureMessage.
176
//
177
// NOTE: Implements the error interface.
178
func (f *FailInvalidRealm) Error() string {
×
179
        return f.Code().String()
×
180
}
×
181

182
// Code returns the failure unique code.
183
//
184
// NOTE: Part of the FailureMessage interface.
UNCOV
185
func (f *FailInvalidRealm) Code() FailCode {
×
UNCOV
186
        return CodeInvalidRealm
×
UNCOV
187
}
×
188

189
// FailTemporaryNodeFailure is returned if an otherwise unspecified transient
190
// error occurs for the entire node.
191
//
192
// NOTE: May be returned by any node in the payment route.
193
type FailTemporaryNodeFailure struct{}
194

195
// Code returns the failure unique code.
196
// NOTE: Part of the FailureMessage interface.
UNCOV
197
func (f *FailTemporaryNodeFailure) Code() FailCode {
×
UNCOV
198
        return CodeTemporaryNodeFailure
×
UNCOV
199
}
×
200

201
// Returns a human readable string describing the target FailureMessage.
202
//
203
// NOTE: Implements the error interface.
204
func (f *FailTemporaryNodeFailure) Error() string {
×
205
        return f.Code().String()
×
206
}
×
207

208
// FailPermanentNodeFailure is returned if an otherwise unspecified permanent
209
// error occurs for the entire node.
210
//
211
// NOTE: May be returned by any node in the payment route.
212
type FailPermanentNodeFailure struct{}
213

214
// Code returns the failure unique code.
215
//
216
// NOTE: Part of the FailureMessage interface.
UNCOV
217
func (f *FailPermanentNodeFailure) Code() FailCode {
×
UNCOV
218
        return CodePermanentNodeFailure
×
UNCOV
219
}
×
220

221
// Returns a human readable string describing the target FailureMessage.
222
//
223
// NOTE: Implements the error interface.
224
func (f *FailPermanentNodeFailure) Error() string {
×
225
        return f.Code().String()
×
226
}
×
227

228
// FailRequiredNodeFeatureMissing is returned if a node has requirement
229
// advertised in its node_announcement features which were not present in the
230
// onion.
231
//
232
// NOTE: May be returned by any node in the payment route.
233
type FailRequiredNodeFeatureMissing struct{}
234

235
// Code returns the failure unique code.
236
//
237
// NOTE: Part of the FailureMessage interface.
UNCOV
238
func (f *FailRequiredNodeFeatureMissing) Code() FailCode {
×
UNCOV
239
        return CodeRequiredNodeFeatureMissing
×
UNCOV
240
}
×
241

242
// Returns a human readable string describing the target FailureMessage.
243
//
244
// NOTE: Implements the error interface.
245
func (f *FailRequiredNodeFeatureMissing) Error() string {
×
246
        return f.Code().String()
×
247
}
×
248

249
// FailPermanentChannelFailure is return if an otherwise unspecified permanent
250
// error occurs for the outgoing channel (eg. channel (recently).
251
//
252
// NOTE: May be returned by any node in the payment route.
253
type FailPermanentChannelFailure struct{}
254

255
// Code returns the failure unique code.
256
//
257
// NOTE: Part of the FailureMessage interface.
258
func (f *FailPermanentChannelFailure) Code() FailCode {
3✔
259
        return CodePermanentChannelFailure
3✔
260
}
3✔
261

262
// Returns a human readable string describing the target FailureMessage.
263
//
264
// NOTE: Implements the error interface.
265
func (f *FailPermanentChannelFailure) Error() string {
3✔
266
        return f.Code().String()
3✔
267
}
3✔
268

269
// FailRequiredChannelFeatureMissing is returned if the outgoing channel has a
270
// requirement advertised in its channel announcement features which were not
271
// present in the onion.
272
//
273
// NOTE: May only be returned by intermediate nodes.
274
type FailRequiredChannelFeatureMissing struct{}
275

276
// Code returns the failure unique code.
277
//
278
// NOTE: Part of the FailureMessage interface.
UNCOV
279
func (f *FailRequiredChannelFeatureMissing) Code() FailCode {
×
UNCOV
280
        return CodeRequiredChannelFeatureMissing
×
UNCOV
281
}
×
282

283
// Returns a human readable string describing the target FailureMessage.
284
//
285
// NOTE: Implements the error interface.
286
func (f *FailRequiredChannelFeatureMissing) Error() string {
×
287
        return f.Code().String()
×
288
}
×
289

290
// FailUnknownNextPeer is returned if the next peer specified by the onion is
291
// not known.
292
//
293
// NOTE: May only be returned by intermediate nodes.
294
type FailUnknownNextPeer struct{}
295

296
// Code returns the failure unique code.
297
//
298
// NOTE: Part of the FailureMessage interface.
299
func (f *FailUnknownNextPeer) Code() FailCode {
3✔
300
        return CodeUnknownNextPeer
3✔
301
}
3✔
302

303
// Returns a human readable string describing the target FailureMessage.
304
//
305
// NOTE: Implements the error interface.
306
func (f *FailUnknownNextPeer) Error() string {
3✔
307
        return f.Code().String()
3✔
308
}
3✔
309

310
// FailIncorrectPaymentAmount is returned if the amount paid is less than the
311
// amount expected, the final node MUST fail the HTLC. If the amount paid is
312
// more than twice the amount expected, the final node SHOULD fail the HTLC.
313
// This allows the sender to reduce information leakage by altering the amount,
314
// without allowing accidental gross overpayment.
315
//
316
// NOTE: May only be returned by the final node in the path.
317
type FailIncorrectPaymentAmount struct{}
318

319
// Code returns the failure unique code.
320
//
321
// NOTE: Part of the FailureMessage interface.
UNCOV
322
func (f *FailIncorrectPaymentAmount) Code() FailCode {
×
UNCOV
323
        return CodeIncorrectPaymentAmount
×
UNCOV
324
}
×
325

326
// Returns a human readable string describing the target FailureMessage.
327
//
328
// NOTE: Implements the error interface.
329
func (f *FailIncorrectPaymentAmount) Error() string {
×
330
        return f.Code().String()
×
331
}
×
332

333
// FailIncorrectDetails is returned for two reasons:
334
//
335
// 1) if the payment hash has already been paid, the final node MAY treat the
336
// payment hash as unknown, or may succeed in accepting the HTLC. If the
337
// payment hash is unknown, the final node MUST fail the HTLC.
338
//
339
// 2) if the amount paid is less than the amount expected, the final node MUST
340
// fail the HTLC. If the amount paid is more than twice the amount expected,
341
// the final node SHOULD fail the HTLC. This allows the sender to reduce
342
// information leakage by altering the amount, without allowing accidental
343
// gross overpayment.
344
//
345
// NOTE: May only be returned by the final node in the path.
346
type FailIncorrectDetails struct {
347
        // amount is the value of the extended HTLC.
348
        amount MilliSatoshi
349

350
        // height is the block height when the htlc was received.
351
        height uint32
352

353
        // extraOpaqueData contains additional failure message tlv data.
354
        extraOpaqueData ExtraOpaqueData
355
}
356

357
// NewFailIncorrectDetails makes a new instance of the FailIncorrectDetails
358
// error bound to the specified HTLC amount and acceptance height.
359
func NewFailIncorrectDetails(amt MilliSatoshi,
360
        height uint32) *FailIncorrectDetails {
3✔
361

3✔
362
        return &FailIncorrectDetails{
3✔
363
                amount:          amt,
3✔
364
                height:          height,
3✔
365
                extraOpaqueData: []byte{},
3✔
366
        }
3✔
367
}
3✔
368

369
// Amount is the value of the extended HTLC.
370
func (f *FailIncorrectDetails) Amount() MilliSatoshi {
×
371
        return f.amount
×
372
}
×
373

374
// Height is the block height when the htlc was received.
375
func (f *FailIncorrectDetails) Height() uint32 {
3✔
376
        return f.height
3✔
377
}
3✔
378

379
// ExtraOpaqueData returns additional failure message tlv data.
UNCOV
380
func (f *FailIncorrectDetails) ExtraOpaqueData() ExtraOpaqueData {
×
UNCOV
381
        return f.extraOpaqueData
×
UNCOV
382
}
×
383

384
// Code returns the failure unique code.
385
//
386
// NOTE: Part of the FailureMessage interface.
387
func (f *FailIncorrectDetails) Code() FailCode {
3✔
388
        return CodeIncorrectOrUnknownPaymentDetails
3✔
389
}
3✔
390

391
// Returns a human readable string describing the target FailureMessage.
392
//
393
// NOTE: Implements the error interface.
394
func (f *FailIncorrectDetails) Error() string {
3✔
395
        return fmt.Sprintf(
3✔
396
                "%v(amt=%v, height=%v)", CodeIncorrectOrUnknownPaymentDetails,
3✔
397
                f.amount, f.height,
3✔
398
        )
3✔
399
}
3✔
400

401
// Decode decodes the failure from bytes stream.
402
//
403
// NOTE: Part of the Serializable interface.
404
func (f *FailIncorrectDetails) Decode(r io.Reader, pver uint32) error {
3✔
405
        err := ReadElement(r, &f.amount)
3✔
406
        switch {
3✔
407
        // This is an optional tack on that was added later in the protocol. As
408
        // a result, older nodes may not include this value. We'll account for
409
        // this by checking for io.EOF here which means that no bytes were read
410
        // at all.
UNCOV
411
        case err == io.EOF:
×
UNCOV
412
                return nil
×
413

UNCOV
414
        case err != nil:
×
UNCOV
415
                return err
×
416
        }
417

418
        // At a later stage, the height field was also tacked on. We need to
419
        // check for io.EOF here as well.
420
        err = ReadElement(r, &f.height)
3✔
421
        switch {
3✔
UNCOV
422
        case err == io.EOF:
×
UNCOV
423
                return nil
×
424

UNCOV
425
        case err != nil:
×
UNCOV
426
                return err
×
427
        }
428

429
        return f.extraOpaqueData.Decode(r)
3✔
430
}
431

432
// Encode writes the failure in bytes stream.
433
//
434
// NOTE: Part of the Serializable interface.
435
func (f *FailIncorrectDetails) Encode(w *bytes.Buffer, pver uint32) error {
3✔
436
        if err := WriteMilliSatoshi(w, f.amount); err != nil {
3✔
437
                return err
×
438
        }
×
439

440
        if err := WriteUint32(w, f.height); err != nil {
3✔
441
                return err
×
442
        }
×
443

444
        return f.extraOpaqueData.Encode(w)
3✔
445
}
446

447
// FailFinalExpiryTooSoon is returned if the cltv_expiry is too low, the final
448
// node MUST fail the HTLC.
449
//
450
// NOTE: May only be returned by the final node in the path.
451
type FailFinalExpiryTooSoon struct{}
452

453
// Code returns the failure unique code.
454
//
455
// NOTE: Part of the FailureMessage interface.
UNCOV
456
func (f *FailFinalExpiryTooSoon) Code() FailCode {
×
UNCOV
457
        return CodeFinalExpiryTooSoon
×
UNCOV
458
}
×
459

460
// Returns a human readable string describing the target FailureMessage.
461
//
462
// NOTE: Implements the error interface.
463
func (f *FailFinalExpiryTooSoon) Error() string {
×
464
        return f.Code().String()
×
465
}
×
466

467
// NewFinalExpiryTooSoon creates new instance of the FailFinalExpiryTooSoon.
468
func NewFinalExpiryTooSoon() *FailFinalExpiryTooSoon {
×
469
        return &FailFinalExpiryTooSoon{}
×
470
}
×
471

472
// FailInvalidOnionVersion is returned if the onion version byte is unknown.
473
//
474
// NOTE: May be returned only by intermediate nodes.
475
type FailInvalidOnionVersion struct {
476
        // OnionSHA256 hash of the onion blob which haven't been proceeded.
477
        OnionSHA256 [sha256.Size]byte
478
}
479

480
// Returns a human readable string describing the target FailureMessage.
481
//
482
// NOTE: Implements the error interface.
483
func (f *FailInvalidOnionVersion) Error() string {
3✔
484
        return fmt.Sprintf("InvalidOnionVersion(onion_sha=%x)", f.OnionSHA256[:])
3✔
485
}
3✔
486

487
// NewInvalidOnionVersion creates new instance of the FailInvalidOnionVersion.
UNCOV
488
func NewInvalidOnionVersion(onion []byte) *FailInvalidOnionVersion {
×
UNCOV
489
        return &FailInvalidOnionVersion{OnionSHA256: sha256.Sum256(onion)}
×
UNCOV
490
}
×
491

492
// Code returns the failure unique code.
493
//
494
// NOTE: Part of the FailureMessage interface.
495
func (f *FailInvalidOnionVersion) Code() FailCode {
3✔
496
        return CodeInvalidOnionVersion
3✔
497
}
3✔
498

499
// Decode decodes the failure from bytes stream.
500
//
501
// NOTE: Part of the Serializable interface.
502
func (f *FailInvalidOnionVersion) Decode(r io.Reader, pver uint32) error {
3✔
503
        return ReadElement(r, f.OnionSHA256[:])
3✔
504
}
3✔
505

506
// Encode writes the failure in bytes stream.
507
//
508
// NOTE: Part of the Serializable interface.
509
func (f *FailInvalidOnionVersion) Encode(w *bytes.Buffer, pver uint32) error {
3✔
510
        return WriteBytes(w, f.OnionSHA256[:])
3✔
511
}
3✔
512

513
// FailInvalidOnionHmac is return if the onion HMAC is incorrect.
514
//
515
// NOTE: May only be returned by intermediate nodes.
516
type FailInvalidOnionHmac struct {
517
        // OnionSHA256 hash of the onion blob which haven't been proceeded.
518
        OnionSHA256 [sha256.Size]byte
519
}
520

521
// NewInvalidOnionHmac creates new instance of the FailInvalidOnionHmac.
UNCOV
522
func NewInvalidOnionHmac(onion []byte) *FailInvalidOnionHmac {
×
UNCOV
523
        return &FailInvalidOnionHmac{OnionSHA256: sha256.Sum256(onion)}
×
UNCOV
524
}
×
525

526
// Code returns the failure unique code.
527
//
528
// NOTE: Part of the FailureMessage interface.
UNCOV
529
func (f *FailInvalidOnionHmac) Code() FailCode {
×
UNCOV
530
        return CodeInvalidOnionHmac
×
UNCOV
531
}
×
532

533
// Decode decodes the failure from bytes stream.
534
//
535
// NOTE: Part of the Serializable interface.
UNCOV
536
func (f *FailInvalidOnionHmac) Decode(r io.Reader, pver uint32) error {
×
UNCOV
537
        return ReadElement(r, f.OnionSHA256[:])
×
UNCOV
538
}
×
539

540
// Encode writes the failure in bytes stream.
541
//
542
// NOTE: Part of the Serializable interface.
UNCOV
543
func (f *FailInvalidOnionHmac) Encode(w *bytes.Buffer, pver uint32) error {
×
UNCOV
544
        return WriteBytes(w, f.OnionSHA256[:])
×
UNCOV
545
}
×
546

547
// Returns a human readable string describing the target FailureMessage.
548
//
549
// NOTE: Implements the error interface.
550
func (f *FailInvalidOnionHmac) Error() string {
×
551
        return fmt.Sprintf("InvalidOnionHMAC(onion_sha=%x)", f.OnionSHA256[:])
×
552
}
×
553

554
// FailInvalidOnionKey is return if the ephemeral key in the onion is
555
// unparsable.
556
//
557
// NOTE: May only be returned by intermediate nodes.
558
type FailInvalidOnionKey struct {
559
        // OnionSHA256 hash of the onion blob which haven't been proceeded.
560
        OnionSHA256 [sha256.Size]byte
561
}
562

563
// NewInvalidOnionKey creates new instance of the FailInvalidOnionKey.
UNCOV
564
func NewInvalidOnionKey(onion []byte) *FailInvalidOnionKey {
×
UNCOV
565
        return &FailInvalidOnionKey{OnionSHA256: sha256.Sum256(onion)}
×
UNCOV
566
}
×
567

568
// Code returns the failure unique code.
569
//
570
// NOTE: Part of the FailureMessage interface.
UNCOV
571
func (f *FailInvalidOnionKey) Code() FailCode {
×
UNCOV
572
        return CodeInvalidOnionKey
×
UNCOV
573
}
×
574

575
// Decode decodes the failure from bytes stream.
576
//
577
// NOTE: Part of the Serializable interface.
UNCOV
578
func (f *FailInvalidOnionKey) Decode(r io.Reader, pver uint32) error {
×
UNCOV
579
        return ReadElement(r, f.OnionSHA256[:])
×
UNCOV
580
}
×
581

582
// Encode writes the failure in bytes stream.
583
//
584
// NOTE: Part of the Serializable interface.
UNCOV
585
func (f *FailInvalidOnionKey) Encode(w *bytes.Buffer, pver uint32) error {
×
UNCOV
586
        return WriteBytes(w, f.OnionSHA256[:])
×
UNCOV
587
}
×
588

589
// Returns a human readable string describing the target FailureMessage.
590
//
591
// NOTE: Implements the error interface.
592
func (f *FailInvalidOnionKey) Error() string {
×
593
        return fmt.Sprintf("InvalidOnionKey(onion_sha=%x)", f.OnionSHA256[:])
×
594
}
×
595

596
// parseChannelUpdateCompatibilityMode will attempt to parse a channel updated
597
// encoded into an onion error payload in two ways. First, we'll try the
598
// compatibility oriented version wherein we'll _skip_ the length prefixing on
599
// the channel update message. Older versions of c-lighting do this so we'll
600
// attempt to parse these messages in order to retain compatibility. If we're
601
// unable to pull out a fully valid version, then we'll fall back to the
602
// regular parsing mechanism which includes the length prefix an NO type byte.
603
func parseChannelUpdateCompatibilityMode(reader io.Reader, length uint16,
604
        chanUpdate *ChannelUpdate1, pver uint32) error {
3✔
605

3✔
606
        // Instantiate a LimitReader because there may be additional data
3✔
607
        // present after the channel update. Without limiting the stream, the
3✔
608
        // additional data would be interpreted as channel update tlv data.
3✔
609
        limitReader := io.LimitReader(reader, int64(length))
3✔
610

3✔
611
        r := bufio.NewReader(limitReader)
3✔
612

3✔
613
        // We'll peek out two bytes from the buffer without advancing the
3✔
614
        // buffer so we can decide how to parse the remainder of it.
3✔
615
        maybeTypeBytes, err := r.Peek(2)
3✔
616
        if err != nil {
3✔
UNCOV
617
                return err
×
UNCOV
618
        }
×
619

620
        // Some nodes well prefix an additional set of bytes in front of their
621
        // channel updates. These bytes will _almost_ always be 258 or the type
622
        // of the ChannelUpdate message.
623
        typeInt := binary.BigEndian.Uint16(maybeTypeBytes)
3✔
624
        if typeInt == MsgChannelUpdate {
6✔
625
                // At this point it's likely the case that this is a channel
3✔
626
                // update message with its type prefixed, so we'll snip off the
3✔
627
                // first two bytes and parse it as normal.
3✔
628
                var throwAwayTypeBytes [2]byte
3✔
629
                _, err := r.Read(throwAwayTypeBytes[:])
3✔
630
                if err != nil {
3✔
631
                        return err
×
632
                }
×
633
        }
634

635
        // At this pint, we've either decided to keep the entire thing, or snip
636
        // off the first two bytes. In either case, we can just read it as
637
        // normal.
638
        return chanUpdate.Decode(r, pver)
3✔
639
}
640

641
// FailTemporaryChannelFailure is if an otherwise unspecified transient error
642
// occurs for the outgoing channel (eg. channel capacity reached, too many
643
// in-flight htlcs)
644
//
645
// NOTE: May only be returned by intermediate nodes.
646
type FailTemporaryChannelFailure struct {
647
        // Update is used to update information about state of the channel
648
        // which caused the failure.
649
        //
650
        // NOTE: This field is optional.
651
        Update *ChannelUpdate1
652
}
653

654
// NewTemporaryChannelFailure creates new instance of the FailTemporaryChannelFailure.
655
func NewTemporaryChannelFailure(
656
        update *ChannelUpdate1) *FailTemporaryChannelFailure {
3✔
657

3✔
658
        return &FailTemporaryChannelFailure{Update: update}
3✔
659
}
3✔
660

661
// Code returns the failure unique code.
662
//
663
// NOTE: Part of the FailureMessage interface.
664
func (f *FailTemporaryChannelFailure) Code() FailCode {
3✔
665
        return CodeTemporaryChannelFailure
3✔
666
}
3✔
667

668
// Returns a human readable string describing the target FailureMessage.
669
//
670
// NOTE: Implements the error interface.
671
func (f *FailTemporaryChannelFailure) Error() string {
3✔
672
        if f.Update == nil {
3✔
UNCOV
673
                return f.Code().String()
×
UNCOV
674
        }
×
675

676
        return fmt.Sprintf("TemporaryChannelFailure(update=%v)",
3✔
677
                spew.Sdump(f.Update))
3✔
678
}
679

680
// Decode decodes the failure from bytes stream.
681
//
682
// NOTE: Part of the Serializable interface.
683
func (f *FailTemporaryChannelFailure) Decode(r io.Reader, pver uint32) error {
3✔
684
        var length uint16
3✔
685
        err := ReadElement(r, &length)
3✔
686
        if err != nil {
3✔
UNCOV
687
                return err
×
UNCOV
688
        }
×
689

690
        if length != 0 {
6✔
691
                f.Update = &ChannelUpdate1{}
3✔
692

3✔
693
                return parseChannelUpdateCompatibilityMode(
3✔
694
                        r, length, f.Update, pver,
3✔
695
                )
3✔
696
        }
3✔
697

UNCOV
698
        return nil
×
699
}
700

701
// Encode writes the failure in bytes stream.
702
//
703
// NOTE: Part of the Serializable interface.
704
func (f *FailTemporaryChannelFailure) Encode(w *bytes.Buffer,
705
        pver uint32) error {
3✔
706

3✔
707
        if f.Update != nil {
6✔
708
                return writeOnionErrorChanUpdate(w, f.Update, pver)
3✔
709
        }
3✔
710

711
        // Write zero length to indicate no channel_update is present.
UNCOV
712
        return WriteUint16(w, 0)
×
713
}
714

715
// FailAmountBelowMinimum is returned if the HTLC does not reach the current
716
// minimum amount, we tell them the amount of the incoming HTLC and the current
717
// channel setting for the outgoing channel.
718
//
719
// NOTE: May only be returned by the intermediate nodes in the path.
720
type FailAmountBelowMinimum struct {
721
        // HtlcMsat is the wrong amount of the incoming HTLC.
722
        HtlcMsat MilliSatoshi
723

724
        // Update is used to update information about state of the channel
725
        // which caused the failure.
726
        Update ChannelUpdate1
727
}
728

729
// NewAmountBelowMinimum creates new instance of the FailAmountBelowMinimum.
730
func NewAmountBelowMinimum(htlcMsat MilliSatoshi,
731
        update ChannelUpdate1) *FailAmountBelowMinimum {
3✔
732

3✔
733
        return &FailAmountBelowMinimum{
3✔
734
                HtlcMsat: htlcMsat,
3✔
735
                Update:   update,
3✔
736
        }
3✔
737
}
3✔
738

739
// Code returns the failure unique code.
740
//
741
// NOTE: Part of the FailureMessage interface.
742
func (f *FailAmountBelowMinimum) Code() FailCode {
3✔
743
        return CodeAmountBelowMinimum
3✔
744
}
3✔
745

746
// Returns a human readable string describing the target FailureMessage.
747
//
748
// NOTE: Implements the error interface.
749
func (f *FailAmountBelowMinimum) Error() string {
3✔
750
        return fmt.Sprintf("AmountBelowMinimum(amt=%v, update=%v", f.HtlcMsat,
3✔
751
                spew.Sdump(f.Update))
3✔
752
}
3✔
753

754
// Decode decodes the failure from bytes stream.
755
//
756
// NOTE: Part of the Serializable interface.
757
func (f *FailAmountBelowMinimum) Decode(r io.Reader, pver uint32) error {
3✔
758
        if err := ReadElement(r, &f.HtlcMsat); err != nil {
3✔
UNCOV
759
                return err
×
UNCOV
760
        }
×
761

762
        var length uint16
3✔
763
        if err := ReadElement(r, &length); err != nil {
3✔
UNCOV
764
                return err
×
UNCOV
765
        }
×
766

767
        f.Update = ChannelUpdate1{}
3✔
768

3✔
769
        return parseChannelUpdateCompatibilityMode(
3✔
770
                r, length, &f.Update, pver,
3✔
771
        )
3✔
772
}
773

774
// Encode writes the failure in bytes stream.
775
//
776
// NOTE: Part of the Serializable interface.
777
func (f *FailAmountBelowMinimum) Encode(w *bytes.Buffer, pver uint32) error {
3✔
778
        if err := WriteMilliSatoshi(w, f.HtlcMsat); err != nil {
3✔
779
                return err
×
780
        }
×
781

782
        return writeOnionErrorChanUpdate(w, &f.Update, pver)
3✔
783
}
784

785
// FailFeeInsufficient is returned if the HTLC does not pay sufficient fee, we
786
// tell them the amount of the incoming HTLC and the current channel setting
787
// for the outgoing channel.
788
//
789
// NOTE: May only be returned by intermediate nodes.
790
type FailFeeInsufficient struct {
791
        // HtlcMsat is the wrong amount of the incoming HTLC.
792
        HtlcMsat MilliSatoshi
793

794
        // Update is used to update information about state of the channel
795
        // which caused the failure.
796
        Update ChannelUpdate1
797
}
798

799
// NewFeeInsufficient creates new instance of the FailFeeInsufficient.
800
func NewFeeInsufficient(htlcMsat MilliSatoshi,
801
        update ChannelUpdate1) *FailFeeInsufficient {
3✔
802
        return &FailFeeInsufficient{
3✔
803
                HtlcMsat: htlcMsat,
3✔
804
                Update:   update,
3✔
805
        }
3✔
806
}
3✔
807

808
// Code returns the failure unique code.
809
//
810
// NOTE: Part of the FailureMessage interface.
811
func (f *FailFeeInsufficient) Code() FailCode {
3✔
812
        return CodeFeeInsufficient
3✔
813
}
3✔
814

815
// Returns a human readable string describing the target FailureMessage.
816
//
817
// NOTE: Implements the error interface.
818
func (f *FailFeeInsufficient) Error() string {
3✔
819
        return fmt.Sprintf("FeeInsufficient(htlc_amt==%v, update=%v", f.HtlcMsat,
3✔
820
                spew.Sdump(f.Update))
3✔
821
}
3✔
822

823
// Decode decodes the failure from bytes stream.
824
//
825
// NOTE: Part of the Serializable interface.
826
func (f *FailFeeInsufficient) Decode(r io.Reader, pver uint32) error {
3✔
827
        if err := ReadElement(r, &f.HtlcMsat); err != nil {
3✔
UNCOV
828
                return err
×
UNCOV
829
        }
×
830

831
        var length uint16
3✔
832
        if err := ReadElement(r, &length); err != nil {
3✔
UNCOV
833
                return err
×
UNCOV
834
        }
×
835

836
        f.Update = ChannelUpdate1{}
3✔
837

3✔
838
        return parseChannelUpdateCompatibilityMode(
3✔
839
                r, length, &f.Update, pver,
3✔
840
        )
3✔
841
}
842

843
// Encode writes the failure in bytes stream.
844
//
845
// NOTE: Part of the Serializable interface.
846
func (f *FailFeeInsufficient) Encode(w *bytes.Buffer, pver uint32) error {
3✔
847
        if err := WriteMilliSatoshi(w, f.HtlcMsat); err != nil {
3✔
848
                return err
×
849
        }
×
850

851
        return writeOnionErrorChanUpdate(w, &f.Update, pver)
3✔
852
}
853

854
// FailIncorrectCltvExpiry is returned if outgoing cltv value does not match
855
// the update add htlc's cltv expiry minus cltv expiry delta for the outgoing
856
// channel, we tell them the cltv expiry and the current channel setting for
857
// the outgoing channel.
858
//
859
// NOTE: May only be returned by intermediate nodes.
860
type FailIncorrectCltvExpiry struct {
861
        // CltvExpiry is the wrong absolute timeout in blocks, after which
862
        // outgoing HTLC expires.
863
        CltvExpiry uint32
864

865
        // Update is used to update information about state of the channel
866
        // which caused the failure.
867
        Update ChannelUpdate1
868
}
869

870
// NewIncorrectCltvExpiry creates new instance of the FailIncorrectCltvExpiry.
871
func NewIncorrectCltvExpiry(cltvExpiry uint32,
UNCOV
872
        update ChannelUpdate1) *FailIncorrectCltvExpiry {
×
UNCOV
873

×
UNCOV
874
        return &FailIncorrectCltvExpiry{
×
UNCOV
875
                CltvExpiry: cltvExpiry,
×
UNCOV
876
                Update:     update,
×
UNCOV
877
        }
×
UNCOV
878
}
×
879

880
// Code returns the failure unique code.
881
//
882
// NOTE: Part of the FailureMessage interface.
UNCOV
883
func (f *FailIncorrectCltvExpiry) Code() FailCode {
×
UNCOV
884
        return CodeIncorrectCltvExpiry
×
UNCOV
885
}
×
886

UNCOV
887
func (f *FailIncorrectCltvExpiry) Error() string {
×
UNCOV
888
        return fmt.Sprintf("IncorrectCltvExpiry(expiry=%v, update=%v",
×
UNCOV
889
                f.CltvExpiry, spew.Sdump(f.Update))
×
UNCOV
890
}
×
891

892
// Decode decodes the failure from bytes stream.
893
//
894
// NOTE: Part of the Serializable interface.
UNCOV
895
func (f *FailIncorrectCltvExpiry) Decode(r io.Reader, pver uint32) error {
×
UNCOV
896
        if err := ReadElement(r, &f.CltvExpiry); err != nil {
×
UNCOV
897
                return err
×
UNCOV
898
        }
×
899

UNCOV
900
        var length uint16
×
UNCOV
901
        if err := ReadElement(r, &length); err != nil {
×
UNCOV
902
                return err
×
UNCOV
903
        }
×
904

UNCOV
905
        f.Update = ChannelUpdate1{}
×
UNCOV
906

×
UNCOV
907
        return parseChannelUpdateCompatibilityMode(
×
UNCOV
908
                r, length, &f.Update, pver,
×
UNCOV
909
        )
×
910
}
911

912
// Encode writes the failure in bytes stream.
913
//
914
// NOTE: Part of the Serializable interface.
UNCOV
915
func (f *FailIncorrectCltvExpiry) Encode(w *bytes.Buffer, pver uint32) error {
×
UNCOV
916
        if err := WriteUint32(w, f.CltvExpiry); err != nil {
×
917
                return err
×
918
        }
×
919

UNCOV
920
        return writeOnionErrorChanUpdate(w, &f.Update, pver)
×
921
}
922

923
// FailExpiryTooSoon is returned if the ctlv-expiry is too near, we tell them
924
// the current channel setting for the outgoing channel.
925
//
926
// NOTE: May only be returned by intermediate nodes.
927
type FailExpiryTooSoon struct {
928
        // Update is used to update information about state of the channel
929
        // which caused the failure.
930
        Update ChannelUpdate1
931
}
932

933
// NewExpiryTooSoon creates new instance of the FailExpiryTooSoon.
UNCOV
934
func NewExpiryTooSoon(update ChannelUpdate1) *FailExpiryTooSoon {
×
UNCOV
935
        return &FailExpiryTooSoon{
×
UNCOV
936
                Update: update,
×
UNCOV
937
        }
×
UNCOV
938
}
×
939

940
// Code returns the failure unique code.
941
//
942
// NOTE: Part of the FailureMessage interface.
UNCOV
943
func (f *FailExpiryTooSoon) Code() FailCode {
×
UNCOV
944
        return CodeExpiryTooSoon
×
UNCOV
945
}
×
946

947
// Returns a human readable string describing the target FailureMessage.
948
//
949
// NOTE: Implements the error interface.
UNCOV
950
func (f *FailExpiryTooSoon) Error() string {
×
UNCOV
951
        return fmt.Sprintf("ExpiryTooSoon(update=%v", spew.Sdump(f.Update))
×
UNCOV
952
}
×
953

954
// Decode decodes the failure from l stream.
955
//
956
// NOTE: Part of the Serializable interface.
UNCOV
957
func (f *FailExpiryTooSoon) Decode(r io.Reader, pver uint32) error {
×
UNCOV
958
        var length uint16
×
UNCOV
959
        if err := ReadElement(r, &length); err != nil {
×
UNCOV
960
                return err
×
UNCOV
961
        }
×
962

UNCOV
963
        f.Update = ChannelUpdate1{}
×
UNCOV
964

×
UNCOV
965
        return parseChannelUpdateCompatibilityMode(
×
UNCOV
966
                r, length, &f.Update, pver,
×
UNCOV
967
        )
×
968
}
969

970
// Encode writes the failure in bytes stream.
971
//
972
// NOTE: Part of the Serializable interface.
UNCOV
973
func (f *FailExpiryTooSoon) Encode(w *bytes.Buffer, pver uint32) error {
×
UNCOV
974
        return writeOnionErrorChanUpdate(w, &f.Update, pver)
×
UNCOV
975
}
×
976

977
// FailChannelDisabled is returned if the channel is disabled, we tell them the
978
// current channel setting for the outgoing channel.
979
//
980
// NOTE: May only be returned by intermediate nodes.
981
type FailChannelDisabled struct {
982
        // Flags least-significant bit must be set to 0 if the creating node
983
        // corresponds to the first node in the previously sent channel
984
        // announcement and 1 otherwise.
985
        Flags uint16
986

987
        // Update is used to update information about state of the channel
988
        // which caused the failure.
989
        Update ChannelUpdate1
990
}
991

992
// NewChannelDisabled creates new instance of the FailChannelDisabled.
993
func NewChannelDisabled(flags uint16,
UNCOV
994
        update ChannelUpdate1) *FailChannelDisabled {
×
UNCOV
995

×
UNCOV
996
        return &FailChannelDisabled{
×
UNCOV
997
                Flags:  flags,
×
UNCOV
998
                Update: update,
×
UNCOV
999
        }
×
UNCOV
1000
}
×
1001

1002
// Code returns the failure unique code.
1003
//
1004
// NOTE: Part of the FailureMessage interface.
1005
func (f *FailChannelDisabled) Code() FailCode {
3✔
1006
        return CodeChannelDisabled
3✔
1007
}
3✔
1008

1009
// Returns a human readable string describing the target FailureMessage.
1010
//
1011
// NOTE: Implements the error interface.
1012
func (f *FailChannelDisabled) Error() string {
3✔
1013
        return fmt.Sprintf("ChannelDisabled(flags=%v, update=%v", f.Flags,
3✔
1014
                spew.Sdump(f.Update))
3✔
1015
}
3✔
1016

1017
// Decode decodes the failure from bytes stream.
1018
//
1019
// NOTE: Part of the Serializable interface.
1020
func (f *FailChannelDisabled) Decode(r io.Reader, pver uint32) error {
3✔
1021
        if err := ReadElement(r, &f.Flags); err != nil {
3✔
UNCOV
1022
                return err
×
UNCOV
1023
        }
×
1024

1025
        var length uint16
3✔
1026
        if err := ReadElement(r, &length); err != nil {
3✔
UNCOV
1027
                return err
×
UNCOV
1028
        }
×
1029

1030
        f.Update = ChannelUpdate1{}
3✔
1031

3✔
1032
        return parseChannelUpdateCompatibilityMode(
3✔
1033
                r, length, &f.Update, pver,
3✔
1034
        )
3✔
1035
}
1036

1037
// Encode writes the failure in bytes stream.
1038
//
1039
// NOTE: Part of the Serializable interface.
1040
func (f *FailChannelDisabled) Encode(w *bytes.Buffer, pver uint32) error {
3✔
1041
        if err := WriteUint16(w, f.Flags); err != nil {
3✔
1042
                return err
×
1043
        }
×
1044

1045
        return writeOnionErrorChanUpdate(w, &f.Update, pver)
3✔
1046
}
1047

1048
// FailFinalIncorrectCltvExpiry is returned if the outgoing_cltv_value does not
1049
// match the ctlv_expiry of the HTLC at the final hop.
1050
//
1051
// NOTE: might be returned by final node only.
1052
type FailFinalIncorrectCltvExpiry struct {
1053
        // CltvExpiry is the wrong absolute timeout in blocks, after which
1054
        // outgoing HTLC expires.
1055
        CltvExpiry uint32
1056
}
1057

1058
// Returns a human readable string describing the target FailureMessage.
1059
//
1060
// NOTE: Implements the error interface.
UNCOV
1061
func (f *FailFinalIncorrectCltvExpiry) Error() string {
×
UNCOV
1062
        return fmt.Sprintf("FinalIncorrectCltvExpiry(expiry=%v)", f.CltvExpiry)
×
UNCOV
1063
}
×
1064

1065
// NewFinalIncorrectCltvExpiry creates new instance of the
1066
// FailFinalIncorrectCltvExpiry.
UNCOV
1067
func NewFinalIncorrectCltvExpiry(cltvExpiry uint32) *FailFinalIncorrectCltvExpiry {
×
UNCOV
1068
        return &FailFinalIncorrectCltvExpiry{
×
UNCOV
1069
                CltvExpiry: cltvExpiry,
×
UNCOV
1070
        }
×
UNCOV
1071
}
×
1072

1073
// Code returns the failure unique code.
1074
//
1075
// NOTE: Part of the FailureMessage interface.
UNCOV
1076
func (f *FailFinalIncorrectCltvExpiry) Code() FailCode {
×
UNCOV
1077
        return CodeFinalIncorrectCltvExpiry
×
UNCOV
1078
}
×
1079

1080
// Decode decodes the failure from bytes stream.
1081
//
1082
// NOTE: Part of the Serializable interface.
UNCOV
1083
func (f *FailFinalIncorrectCltvExpiry) Decode(r io.Reader, pver uint32) error {
×
UNCOV
1084
        return ReadElement(r, &f.CltvExpiry)
×
UNCOV
1085
}
×
1086

1087
// Encode writes the failure in bytes stream.
1088
//
1089
// NOTE: Part of the Serializable interface.
1090
func (f *FailFinalIncorrectCltvExpiry) Encode(w *bytes.Buffer,
UNCOV
1091
        pver uint32) error {
×
UNCOV
1092

×
UNCOV
1093
        return WriteUint32(w, f.CltvExpiry)
×
UNCOV
1094
}
×
1095

1096
// FailFinalIncorrectHtlcAmount is returned if the amt_to_forward is higher
1097
// than incoming_htlc_amt of the HTLC at the final hop.
1098
//
1099
// NOTE: May only be returned by the final node.
1100
type FailFinalIncorrectHtlcAmount struct {
1101
        // IncomingHTLCAmount is the wrong forwarded htlc amount.
1102
        IncomingHTLCAmount MilliSatoshi
1103
}
1104

1105
// Returns a human readable string describing the target FailureMessage.
1106
//
1107
// NOTE: Implements the error interface.
1108
func (f *FailFinalIncorrectHtlcAmount) Error() string {
×
1109
        return fmt.Sprintf("FinalIncorrectHtlcAmount(amt=%v)",
×
1110
                f.IncomingHTLCAmount)
×
1111
}
×
1112

1113
// NewFinalIncorrectHtlcAmount creates new instance of the
1114
// FailFinalIncorrectHtlcAmount.
UNCOV
1115
func NewFinalIncorrectHtlcAmount(amount MilliSatoshi) *FailFinalIncorrectHtlcAmount {
×
UNCOV
1116
        return &FailFinalIncorrectHtlcAmount{
×
UNCOV
1117
                IncomingHTLCAmount: amount,
×
UNCOV
1118
        }
×
UNCOV
1119
}
×
1120

1121
// Code returns the failure unique code.
1122
//
1123
// NOTE: Part of the FailureMessage interface.
UNCOV
1124
func (f *FailFinalIncorrectHtlcAmount) Code() FailCode {
×
UNCOV
1125
        return CodeFinalIncorrectHtlcAmount
×
UNCOV
1126
}
×
1127

1128
// Decode decodes the failure from bytes stream.
1129
//
1130
// NOTE: Part of the Serializable interface.
UNCOV
1131
func (f *FailFinalIncorrectHtlcAmount) Decode(r io.Reader, pver uint32) error {
×
UNCOV
1132
        return ReadElement(r, &f.IncomingHTLCAmount)
×
UNCOV
1133
}
×
1134

1135
// Encode writes the failure in bytes stream.
1136
//
1137
// NOTE: Part of the Serializable interface.
1138
func (f *FailFinalIncorrectHtlcAmount) Encode(w *bytes.Buffer,
UNCOV
1139
        pver uint32) error {
×
UNCOV
1140

×
UNCOV
1141
        return WriteMilliSatoshi(w, f.IncomingHTLCAmount)
×
UNCOV
1142
}
×
1143

1144
// FailExpiryTooFar is returned if the CLTV expiry in the HTLC is too far in the
1145
// future.
1146
//
1147
// NOTE: May be returned by any node in the payment route.
1148
type FailExpiryTooFar struct{}
1149

1150
// Code returns the failure unique code.
1151
//
1152
// NOTE: Part of the FailureMessage interface.
1153
func (f *FailExpiryTooFar) Code() FailCode {
×
1154
        return CodeExpiryTooFar
×
1155
}
×
1156

1157
// Returns a human readable string describing the target FailureMessage.
1158
//
1159
// NOTE: Implements the error interface.
1160
func (f *FailExpiryTooFar) Error() string {
×
1161
        return f.Code().String()
×
1162
}
×
1163

1164
// InvalidOnionPayload is returned if the hop could not process the TLV payload
1165
// enclosed in the onion.
1166
type InvalidOnionPayload struct {
1167
        // Type is the TLV type that caused the specific failure.
1168
        Type uint64
1169

1170
        // Offset is the byte offset within the payload where the failure
1171
        // occurred.
1172
        Offset uint16
1173
}
1174

1175
// NewInvalidOnionPayload initializes a new InvalidOnionPayload failure.
1176
func NewInvalidOnionPayload(typ uint64, offset uint16) *InvalidOnionPayload {
3✔
1177
        return &InvalidOnionPayload{
3✔
1178
                Type:   typ,
3✔
1179
                Offset: offset,
3✔
1180
        }
3✔
1181
}
3✔
1182

1183
// Code returns the failure unique code.
1184
//
1185
// NOTE: Part of the FailureMessage interface.
1186
func (f *InvalidOnionPayload) Code() FailCode {
3✔
1187
        return CodeInvalidOnionPayload
3✔
1188
}
3✔
1189

1190
// Returns a human readable string describing the target FailureMessage.
1191
//
1192
// NOTE: Implements the error interface.
1193
func (f *InvalidOnionPayload) Error() string {
3✔
1194
        return fmt.Sprintf("%v(type=%v, offset=%d)",
3✔
1195
                f.Code(), f.Type, f.Offset)
3✔
1196
}
3✔
1197

1198
// Decode decodes the failure from bytes stream.
1199
//
1200
// NOTE: Part of the Serializable interface.
UNCOV
1201
func (f *InvalidOnionPayload) Decode(r io.Reader, pver uint32) error {
×
UNCOV
1202
        var buf [8]byte
×
UNCOV
1203
        typ, err := tlv.ReadVarInt(r, &buf)
×
UNCOV
1204
        if err != nil {
×
UNCOV
1205
                return err
×
UNCOV
1206
        }
×
UNCOV
1207
        f.Type = typ
×
UNCOV
1208

×
UNCOV
1209
        return ReadElements(r, &f.Offset)
×
1210
}
1211

1212
// Encode writes the failure in bytes stream.
1213
//
1214
// NOTE: Part of the Serializable interface.
1215
func (f *InvalidOnionPayload) Encode(w *bytes.Buffer, pver uint32) error {
3✔
1216
        var buf [8]byte
3✔
1217
        if err := tlv.WriteVarInt(w, f.Type, &buf); err != nil {
3✔
1218
                return err
×
1219
        }
×
1220

1221
        return WriteUint16(w, f.Offset)
3✔
1222
}
1223

1224
// FailMPPTimeout is returned if the complete amount for a multi part payment
1225
// was not received within a reasonable time.
1226
//
1227
// NOTE: May only be returned by the final node in the path.
1228
type FailMPPTimeout struct{}
1229

1230
// Code returns the failure unique code.
1231
//
1232
// NOTE: Part of the FailureMessage interface.
UNCOV
1233
func (f *FailMPPTimeout) Code() FailCode {
×
UNCOV
1234
        return CodeMPPTimeout
×
UNCOV
1235
}
×
1236

1237
// Returns a human readable string describing the target FailureMessage.
1238
//
1239
// NOTE: Implements the error interface.
1240
func (f *FailMPPTimeout) Error() string {
×
1241
        return f.Code().String()
×
1242
}
×
1243

1244
// FailInvalidBlinding is returned if there has been a route blinding related
1245
// error.
1246
type FailInvalidBlinding struct {
1247
        OnionSHA256 [sha256.Size]byte
1248
}
1249

1250
// Code returns the failure unique code.
1251
//
1252
// NOTE: Part of the FailureMessage interface.
1253
func (f *FailInvalidBlinding) Code() FailCode {
3✔
1254
        return CodeInvalidBlinding
3✔
1255
}
3✔
1256

1257
// Returns a human readable string describing the target FailureMessage.
1258
//
1259
// NOTE: Implements the error interface.
1260
func (f *FailInvalidBlinding) Error() string {
3✔
1261
        return f.Code().String()
3✔
1262
}
3✔
1263

1264
// Decode decodes the failure from bytes stream.
1265
//
1266
// NOTE: Part of the Serializable interface.
1267
func (f *FailInvalidBlinding) Decode(r io.Reader, _ uint32) error {
3✔
1268
        return ReadElement(r, f.OnionSHA256[:])
3✔
1269
}
3✔
1270

1271
// Encode writes the failure in bytes stream.
1272
//
1273
// NOTE: Part of the Serializable interface.
1274
func (f *FailInvalidBlinding) Encode(w *bytes.Buffer, _ uint32) error {
3✔
1275
        return WriteBytes(w, f.OnionSHA256[:])
3✔
1276
}
3✔
1277

1278
// NewInvalidBlinding creates new instance of FailInvalidBlinding.
1279
func NewInvalidBlinding(
1280
        onion fn.Option[[OnionPacketSize]byte]) *FailInvalidBlinding {
3✔
1281
        // The spec allows empty onion hashes for invalid blinding, so we only
3✔
1282
        // include our onion hash if it's provided.
3✔
1283
        if onion.IsNone() {
6✔
1284
                return &FailInvalidBlinding{}
3✔
1285
        }
3✔
1286

1287
        shaSum := fn.MapOptionZ(onion, func(o [OnionPacketSize]byte) [32]byte {
6✔
1288
                return sha256.Sum256(o[:])
3✔
1289
        })
3✔
1290

1291
        return &FailInvalidBlinding{OnionSHA256: shaSum}
3✔
1292
}
1293

1294
// DecodeFailure decodes, validates, and parses the lnwire onion failure, for
1295
// the provided protocol version.
1296
func DecodeFailure(r io.Reader, pver uint32) (FailureMessage, error) {
3✔
1297
        // First, we'll parse out the encapsulated failure message itself. This
3✔
1298
        // is a 2 byte length followed by the payload itself.
3✔
1299
        var failureLength uint16
3✔
1300
        if err := ReadElement(r, &failureLength); err != nil {
3✔
UNCOV
1301
                return nil, fmt.Errorf("unable to read failure len: %w", err)
×
UNCOV
1302
        }
×
1303

1304
        failureData := make([]byte, failureLength)
3✔
1305
        if _, err := io.ReadFull(r, failureData); err != nil {
3✔
1306
                return nil, fmt.Errorf("unable to full read payload of "+
×
1307
                        "%v: %w", failureLength, err)
×
1308
        }
×
1309

1310
        // Read the padding.
1311
        var padLength uint16
3✔
1312
        if err := ReadElement(r, &padLength); err != nil {
3✔
1313
                return nil, fmt.Errorf("unable to read pad len: %w", err)
×
1314
        }
×
1315

1316
        if _, err := io.CopyN(io.Discard, r, int64(padLength)); err != nil {
3✔
1317
                return nil, fmt.Errorf("unable to read padding %w", err)
×
1318
        }
×
1319

1320
        // Verify that we are at the end of the stream now.
1321
        scratch := make([]byte, 1)
3✔
1322
        _, err := r.Read(scratch)
3✔
1323
        if err != io.EOF {
3✔
1324
                return nil, fmt.Errorf("unexpected failure bytes")
×
1325
        }
×
1326

1327
        // Check the total length. Convert to 32 bits to prevent overflow.
1328
        totalLength := uint32(padLength) + uint32(failureLength)
3✔
1329
        if totalLength < FailureMessageLength {
3✔
1330
                return nil, fmt.Errorf("failure message too short: "+
×
1331
                        "msg=%v, pad=%v, total=%v",
×
1332
                        failureLength, padLength, totalLength)
×
1333
        }
×
1334

1335
        // Decode the failure message.
1336
        dataReader := bytes.NewReader(failureData)
3✔
1337

3✔
1338
        return DecodeFailureMessage(dataReader, pver)
3✔
1339
}
1340

1341
// DecodeFailureMessage decodes just the failure message, ignoring any padding
1342
// that may be present at the end.
1343
func DecodeFailureMessage(r io.Reader, pver uint32) (FailureMessage, error) {
3✔
1344
        // Once we have the failure data, we can obtain the failure code from
3✔
1345
        // the first two bytes of the buffer.
3✔
1346
        var codeBytes [2]byte
3✔
1347
        if _, err := io.ReadFull(r, codeBytes[:]); err != nil {
3✔
1348
                return nil, fmt.Errorf("unable to read failure code: %w", err)
×
1349
        }
×
1350
        failCode := FailCode(binary.BigEndian.Uint16(codeBytes[:]))
3✔
1351

3✔
1352
        // Create the empty failure by given code and populate the failure with
3✔
1353
        // additional data if needed.
3✔
1354
        failure, err := makeEmptyOnionError(failCode)
3✔
1355
        if err != nil {
3✔
1356
                return nil, fmt.Errorf("unable to make empty error: %w", err)
×
1357
        }
×
1358

1359
        // Finally, if this failure has a payload, then we'll read that now as
1360
        // well.
1361
        switch f := failure.(type) {
3✔
1362
        case Serializable:
3✔
1363
                if err := f.Decode(r, pver); err != nil {
3✔
UNCOV
1364
                        return nil, fmt.Errorf("unable to decode error "+
×
UNCOV
1365
                                "update (type=%T): %v", failure, err)
×
UNCOV
1366
                }
×
1367
        }
1368

1369
        return failure, nil
3✔
1370
}
1371

1372
// EncodeFailure encodes, including the necessary onion failure header
1373
// information.
1374
func EncodeFailure(w *bytes.Buffer, failure FailureMessage, pver uint32) error {
3✔
1375
        var failureMessageBuffer bytes.Buffer
3✔
1376

3✔
1377
        err := EncodeFailureMessage(&failureMessageBuffer, failure, pver)
3✔
1378
        if err != nil {
3✔
1379
                return err
×
1380
        }
×
1381

1382
        // The combined size of this message must be below the max allowed
1383
        // failure message length.
1384
        failureMessage := failureMessageBuffer.Bytes()
3✔
1385
        if len(failureMessage) > FailureMessageLength {
3✔
UNCOV
1386
                return fmt.Errorf("failure message exceed max "+
×
UNCOV
1387
                        "available size: %v", len(failureMessage))
×
UNCOV
1388
        }
×
1389

1390
        // Finally, we'll add some padding in order to ensure that all failure
1391
        // messages are fixed size.
1392
        pad := make([]byte, FailureMessageLength-len(failureMessage))
3✔
1393

3✔
1394
        if err := WriteUint16(w, uint16(len(failureMessage))); err != nil {
3✔
1395
                return err
×
1396
        }
×
1397

1398
        if err := WriteBytes(w, failureMessage); err != nil {
3✔
1399
                return err
×
1400
        }
×
1401
        if err := WriteUint16(w, uint16(len(pad))); err != nil {
3✔
1402
                return err
×
1403
        }
×
1404

1405
        return WriteBytes(w, pad)
3✔
1406
}
1407

1408
// EncodeFailureMessage encodes just the failure message without adding a length
1409
// and padding the message for the onion protocol.
1410
func EncodeFailureMessage(w *bytes.Buffer,
1411
        failure FailureMessage, pver uint32) error {
3✔
1412

3✔
1413
        // First, we'll write out the error code itself into the failure
3✔
1414
        // buffer.
3✔
1415
        var codeBytes [2]byte
3✔
1416
        code := uint16(failure.Code())
3✔
1417
        binary.BigEndian.PutUint16(codeBytes[:], code)
3✔
1418
        _, err := w.Write(codeBytes[:])
3✔
1419
        if err != nil {
3✔
1420
                return err
×
1421
        }
×
1422

1423
        // Next, some message have an additional message payload, if this is
1424
        // one of those types, then we'll also encode the error payload as
1425
        // well.
1426
        switch failure := failure.(type) {
3✔
1427
        case Serializable:
3✔
1428
                if err := failure.Encode(w, pver); err != nil {
3✔
1429
                        return err
×
1430
                }
×
1431
        }
1432

1433
        return nil
3✔
1434
}
1435

1436
// makeEmptyOnionError creates a new empty onion error  of the proper concrete
1437
// type based on the passed failure code.
1438
func makeEmptyOnionError(code FailCode) (FailureMessage, error) {
3✔
1439
        switch code {
3✔
UNCOV
1440
        case CodeInvalidRealm:
×
UNCOV
1441
                return &FailInvalidRealm{}, nil
×
1442

UNCOV
1443
        case CodeTemporaryNodeFailure:
×
UNCOV
1444
                return &FailTemporaryNodeFailure{}, nil
×
1445

UNCOV
1446
        case CodePermanentNodeFailure:
×
UNCOV
1447
                return &FailPermanentNodeFailure{}, nil
×
1448

UNCOV
1449
        case CodeRequiredNodeFeatureMissing:
×
UNCOV
1450
                return &FailRequiredNodeFeatureMissing{}, nil
×
1451

1452
        case CodePermanentChannelFailure:
3✔
1453
                return &FailPermanentChannelFailure{}, nil
3✔
1454

UNCOV
1455
        case CodeRequiredChannelFeatureMissing:
×
UNCOV
1456
                return &FailRequiredChannelFeatureMissing{}, nil
×
1457

1458
        case CodeUnknownNextPeer:
3✔
1459
                return &FailUnknownNextPeer{}, nil
3✔
1460

1461
        case CodeIncorrectOrUnknownPaymentDetails:
3✔
1462
                return &FailIncorrectDetails{}, nil
3✔
1463

UNCOV
1464
        case CodeIncorrectPaymentAmount:
×
UNCOV
1465
                return &FailIncorrectPaymentAmount{}, nil
×
1466

UNCOV
1467
        case CodeFinalExpiryTooSoon:
×
UNCOV
1468
                return &FailFinalExpiryTooSoon{}, nil
×
1469

1470
        case CodeInvalidOnionVersion:
3✔
1471
                return &FailInvalidOnionVersion{}, nil
3✔
1472

UNCOV
1473
        case CodeInvalidOnionHmac:
×
UNCOV
1474
                return &FailInvalidOnionHmac{}, nil
×
1475

UNCOV
1476
        case CodeInvalidOnionKey:
×
UNCOV
1477
                return &FailInvalidOnionKey{}, nil
×
1478

1479
        case CodeTemporaryChannelFailure:
3✔
1480
                return &FailTemporaryChannelFailure{}, nil
3✔
1481

1482
        case CodeAmountBelowMinimum:
3✔
1483
                return &FailAmountBelowMinimum{}, nil
3✔
1484

1485
        case CodeFeeInsufficient:
3✔
1486
                return &FailFeeInsufficient{}, nil
3✔
1487

UNCOV
1488
        case CodeIncorrectCltvExpiry:
×
UNCOV
1489
                return &FailIncorrectCltvExpiry{}, nil
×
1490

UNCOV
1491
        case CodeExpiryTooSoon:
×
UNCOV
1492
                return &FailExpiryTooSoon{}, nil
×
1493

1494
        case CodeChannelDisabled:
3✔
1495
                return &FailChannelDisabled{}, nil
3✔
1496

UNCOV
1497
        case CodeFinalIncorrectCltvExpiry:
×
UNCOV
1498
                return &FailFinalIncorrectCltvExpiry{}, nil
×
1499

UNCOV
1500
        case CodeFinalIncorrectHtlcAmount:
×
UNCOV
1501
                return &FailFinalIncorrectHtlcAmount{}, nil
×
1502

1503
        case CodeExpiryTooFar:
×
1504
                return &FailExpiryTooFar{}, nil
×
1505

UNCOV
1506
        case CodeInvalidOnionPayload:
×
UNCOV
1507
                return &InvalidOnionPayload{}, nil
×
1508

UNCOV
1509
        case CodeMPPTimeout:
×
UNCOV
1510
                return &FailMPPTimeout{}, nil
×
1511

1512
        case CodeInvalidBlinding:
3✔
1513
                return &FailInvalidBlinding{}, nil
3✔
1514

1515
        default:
×
1516
                return nil, errors.Errorf("unknown error code: %v", code)
×
1517
        }
1518
}
1519

1520
// writeOnionErrorChanUpdate writes out a ChannelUpdate using the onion error
1521
// format. The format is that we first write out the true serialized length of
1522
// the channel update, followed by the serialized channel update itself.
1523
func writeOnionErrorChanUpdate(w *bytes.Buffer, chanUpdate *ChannelUpdate1,
1524
        pver uint32) error {
3✔
1525

3✔
1526
        // First, we encode the channel update in a temporary buffer in order
3✔
1527
        // to get the exact serialized size.
3✔
1528
        var b bytes.Buffer
3✔
1529
        updateLen, err := WriteMessage(&b, chanUpdate, pver)
3✔
1530
        if err != nil {
3✔
1531
                return err
×
1532
        }
×
1533

1534
        // Now that we know the size, we can write the length out in the main
1535
        // writer.
1536
        if err := WriteUint16(w, uint16(updateLen)); err != nil {
3✔
1537
                return err
×
1538
        }
×
1539

1540
        // With the length written, we'll then write out the serialized channel
1541
        // update.
1542
        if _, err := w.Write(b.Bytes()); err != nil {
3✔
1543
                return err
×
1544
        }
×
1545

1546
        return nil
3✔
1547
}
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