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

lightningnetwork / lnd / 16466354971

23 Jul 2025 09:05AM UTC coverage: 57.54% (-9.7%) from 67.201%
16466354971

Pull #9455

github

web-flow
Merge f914ae23c into 90e211684
Pull Request #9455: discovery+lnwire: add support for DNS host name in NodeAnnouncement msg

151 of 291 new or added lines in 7 files covered. (51.89%)

28441 existing lines in 456 files now uncovered.

98864 of 171817 relevant lines covered (57.54%)

1.79 hits per line

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

0.0
/channeldb/migration/lnwire21/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/lightningnetwork/lnd/tlv"
13
)
14

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

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

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

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

37
        // FlagPerm error flag indicates a permanent failure.
38
        FlagPerm FailCode = 0x4000
39

40
        // FlagNode error flag indicates a node failure.
41
        FlagNode FailCode = 0x2000
42

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

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

54
// The currently defined onion failure types within this current version of the
55
// Lightning protocol.
56
//
57
//nolint:ll
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
85
)
86

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

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

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

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

102
        case CodeInvalidOnionVersion:
×
103
                return "InvalidOnionVersion"
×
104

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

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

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

114
        case CodePermanentChannelFailure:
×
115
                return "PermanentChannelFailure"
×
116

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

120
        case CodeUnknownNextPeer:
×
121
                return "UnknownNextPeer"
×
122

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

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

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

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

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

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

141
        case CodeIncorrectOrUnknownPaymentDetails:
×
142
                return "IncorrectOrUnknownPaymentDetails"
×
143

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

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

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

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

156
        case CodeInvalidOnionPayload:
×
157
                return "InvalidOnionPayload"
×
158

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

162
        case CodeInvalidBlinding:
×
163
                return "InvalidBlinding"
×
164

165
        default:
×
166
                return "<unknown>"
×
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.
185
func (f *FailInvalidRealm) Code() FailCode {
×
186
        return CodeInvalidRealm
×
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.
197
func (f *FailTemporaryNodeFailure) Code() FailCode {
×
198
        return CodeTemporaryNodeFailure
×
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.
217
func (f *FailPermanentNodeFailure) Code() FailCode {
×
218
        return CodePermanentNodeFailure
×
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.
238
func (f *FailRequiredNodeFeatureMissing) Code() FailCode {
×
239
        return CodeRequiredNodeFeatureMissing
×
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 {
×
259
        return CodePermanentChannelFailure
×
260
}
×
261

262
// Returns a human readable string describing the target FailureMessage.
263
//
264
// NOTE: Implements the error interface.
265
func (f *FailPermanentChannelFailure) Error() string {
×
266
        return f.Code().String()
×
267
}
×
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.
279
func (f *FailRequiredChannelFeatureMissing) Code() FailCode {
×
280
        return CodeRequiredChannelFeatureMissing
×
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 {
×
300
        return CodeUnknownNextPeer
×
301
}
×
302

303
// Returns a human readable string describing the target FailureMessage.
304
//
305
// NOTE: Implements the error interface.
306
func (f *FailUnknownNextPeer) Error() string {
×
307
        return f.Code().String()
×
308
}
×
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.
322
func (f *FailIncorrectPaymentAmount) Code() FailCode {
×
323
        return CodeIncorrectPaymentAmount
×
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

354
// NewFailIncorrectDetails makes a new instance of the FailIncorrectDetails
355
// error bound to the specified HTLC amount and acceptance height.
356
func NewFailIncorrectDetails(amt MilliSatoshi,
357
        height uint32) *FailIncorrectDetails {
×
358

×
359
        return &FailIncorrectDetails{
×
360
                amount: amt,
×
361
                height: height,
×
362
        }
×
363
}
×
364

365
// Amount is the value of the extended HTLC.
366
func (f *FailIncorrectDetails) Amount() MilliSatoshi {
×
367
        return f.amount
×
368
}
×
369

370
// Height is the block height when the htlc was received.
371
func (f *FailIncorrectDetails) Height() uint32 {
×
372
        return f.height
×
373
}
×
374

375
// Code returns the failure unique code.
376
//
377
// NOTE: Part of the FailureMessage interface.
378
func (f *FailIncorrectDetails) Code() FailCode {
×
379
        return CodeIncorrectOrUnknownPaymentDetails
×
380
}
×
381

382
// Returns a human readable string describing the target FailureMessage.
383
//
384
// NOTE: Implements the error interface.
385
func (f *FailIncorrectDetails) Error() string {
×
386
        return fmt.Sprintf(
×
387
                "%v(amt=%v, height=%v)", CodeIncorrectOrUnknownPaymentDetails,
×
388
                f.amount, f.height,
×
389
        )
×
390
}
×
391

392
// Decode decodes the failure from bytes stream.
393
//
394
// NOTE: Part of the Serializable interface.
395
func (f *FailIncorrectDetails) Decode(r io.Reader, pver uint32) error {
×
396
        err := ReadElement(r, &f.amount)
×
397
        switch {
×
398
        // This is an optional tack on that was added later in the protocol. As
399
        // a result, older nodes may not include this value. We'll account for
400
        // this by checking for io.EOF here which means that no bytes were read
401
        // at all.
402
        case err == io.EOF:
×
403
                return nil
×
404

405
        case err != nil:
×
406
                return err
×
407
        }
408

409
        // At a later stage, the height field was also tacked on. We need to
410
        // check for io.EOF here as well.
411
        err = ReadElement(r, &f.height)
×
412
        switch {
×
413
        case err == io.EOF:
×
414
                return nil
×
415

416
        case err != nil:
×
417
                return err
×
418
        }
419

420
        return nil
×
421
}
422

423
// Encode writes the failure in bytes stream.
424
//
425
// NOTE: Part of the Serializable interface.
426
func (f *FailIncorrectDetails) Encode(w io.Writer, pver uint32) error {
×
427
        return WriteElements(w, f.amount, f.height)
×
428
}
×
429

430
// FailFinalExpiryTooSoon is returned if the cltv_expiry is too low, the final
431
// node MUST fail the HTLC.
432
//
433
// NOTE: May only be returned by the final node in the path.
434
type FailFinalExpiryTooSoon struct{}
435

436
// Code returns the failure unique code.
437
//
438
// NOTE: Part of the FailureMessage interface.
439
func (f *FailFinalExpiryTooSoon) Code() FailCode {
×
440
        return CodeFinalExpiryTooSoon
×
441
}
×
442

443
// Returns a human readable string describing the target FailureMessage.
444
//
445
// NOTE: Implements the error interface.
446
func (f *FailFinalExpiryTooSoon) Error() string {
×
447
        return f.Code().String()
×
448
}
×
449

450
// NewFinalExpiryTooSoon creates new instance of the FailFinalExpiryTooSoon.
451
func NewFinalExpiryTooSoon() *FailFinalExpiryTooSoon {
×
452
        return &FailFinalExpiryTooSoon{}
×
453
}
×
454

455
// FailInvalidOnionVersion is returned if the onion version byte is unknown.
456
//
457
// NOTE: May be returned only by intermediate nodes.
458
type FailInvalidOnionVersion struct {
459
        // OnionSHA256 hash of the onion blob which haven't been proceeded.
460
        OnionSHA256 [sha256.Size]byte
461
}
462

463
// Returns a human readable string describing the target FailureMessage.
464
//
465
// NOTE: Implements the error interface.
466
func (f *FailInvalidOnionVersion) Error() string {
×
467
        return fmt.Sprintf("InvalidOnionVersion(onion_sha=%x)", f.OnionSHA256[:])
×
468
}
×
469

470
// NewInvalidOnionVersion creates new instance of the FailInvalidOnionVersion.
471
func NewInvalidOnionVersion(onion []byte) *FailInvalidOnionVersion {
×
472
        return &FailInvalidOnionVersion{OnionSHA256: sha256.Sum256(onion)}
×
473
}
×
474

475
// Code returns the failure unique code.
476
//
477
// NOTE: Part of the FailureMessage interface.
478
func (f *FailInvalidOnionVersion) Code() FailCode {
×
479
        return CodeInvalidOnionVersion
×
480
}
×
481

482
// Decode decodes the failure from bytes stream.
483
//
484
// NOTE: Part of the Serializable interface.
485
func (f *FailInvalidOnionVersion) Decode(r io.Reader, pver uint32) error {
×
486
        return ReadElement(r, f.OnionSHA256[:])
×
487
}
×
488

489
// Encode writes the failure in bytes stream.
490
//
491
// NOTE: Part of the Serializable interface.
492
func (f *FailInvalidOnionVersion) Encode(w io.Writer, pver uint32) error {
×
493
        return WriteElement(w, f.OnionSHA256[:])
×
494
}
×
495

496
// FailInvalidOnionHmac is return if the onion HMAC is incorrect.
497
//
498
// NOTE: May only be returned by intermediate nodes.
499
type FailInvalidOnionHmac struct {
500
        // OnionSHA256 hash of the onion blob which haven't been proceeded.
501
        OnionSHA256 [sha256.Size]byte
502
}
503

504
// NewInvalidOnionHmac creates new instance of the FailInvalidOnionHmac.
505
func NewInvalidOnionHmac(onion []byte) *FailInvalidOnionHmac {
×
506
        return &FailInvalidOnionHmac{OnionSHA256: sha256.Sum256(onion)}
×
507
}
×
508

509
// Code returns the failure unique code.
510
//
511
// NOTE: Part of the FailureMessage interface.
512
func (f *FailInvalidOnionHmac) Code() FailCode {
×
513
        return CodeInvalidOnionHmac
×
514
}
×
515

516
// Decode decodes the failure from bytes stream.
517
//
518
// NOTE: Part of the Serializable interface.
519
func (f *FailInvalidOnionHmac) Decode(r io.Reader, pver uint32) error {
×
520
        return ReadElement(r, f.OnionSHA256[:])
×
521
}
×
522

523
// Encode writes the failure in bytes stream.
524
//
525
// NOTE: Part of the Serializable interface.
526
func (f *FailInvalidOnionHmac) Encode(w io.Writer, pver uint32) error {
×
527
        return WriteElement(w, f.OnionSHA256[:])
×
528
}
×
529

530
// Returns a human readable string describing the target FailureMessage.
531
//
532
// NOTE: Implements the error interface.
533
func (f *FailInvalidOnionHmac) Error() string {
×
534
        return fmt.Sprintf("InvalidOnionHMAC(onion_sha=%x)", f.OnionSHA256[:])
×
535
}
×
536

537
// FailInvalidOnionKey is return if the ephemeral key in the onion is
538
// unparsable.
539
//
540
// NOTE: May only be returned by intermediate nodes.
541
type FailInvalidOnionKey struct {
542
        // OnionSHA256 hash of the onion blob which haven't been proceeded.
543
        OnionSHA256 [sha256.Size]byte
544
}
545

546
// NewInvalidOnionKey creates new instance of the FailInvalidOnionKey.
547
func NewInvalidOnionKey(onion []byte) *FailInvalidOnionKey {
×
548
        return &FailInvalidOnionKey{OnionSHA256: sha256.Sum256(onion)}
×
549
}
×
550

551
// Code returns the failure unique code.
552
//
553
// NOTE: Part of the FailureMessage interface.
554
func (f *FailInvalidOnionKey) Code() FailCode {
×
555
        return CodeInvalidOnionKey
×
556
}
×
557

558
// Decode decodes the failure from bytes stream.
559
//
560
// NOTE: Part of the Serializable interface.
561
func (f *FailInvalidOnionKey) Decode(r io.Reader, pver uint32) error {
×
562
        return ReadElement(r, f.OnionSHA256[:])
×
563
}
×
564

565
// Encode writes the failure in bytes stream.
566
//
567
// NOTE: Part of the Serializable interface.
568
func (f *FailInvalidOnionKey) Encode(w io.Writer, pver uint32) error {
×
569
        return WriteElement(w, f.OnionSHA256[:])
×
570
}
×
571

572
// Returns a human readable string describing the target FailureMessage.
573
//
574
// NOTE: Implements the error interface.
575
func (f *FailInvalidOnionKey) Error() string {
×
576
        return fmt.Sprintf("InvalidOnionKey(onion_sha=%x)", f.OnionSHA256[:])
×
577
}
×
578

579
// FailInvalidBlinding is returned if there has been a route blinding related
580
// error.
581
type FailInvalidBlinding struct {
582
        OnionSHA256 [sha256.Size]byte
583
}
584

585
// A compile-time check to ensure that FailInvalidBlinding implements the
586
// Serializable interface.
587
var _ Serializable = (*FailInvalidBlinding)(nil)
588

589
// Code returns the failure unique code.
590
//
591
// NOTE: Part of the FailureMessage interface.
592
func (f *FailInvalidBlinding) Code() FailCode {
×
593
        return CodeInvalidBlinding
×
594
}
×
595

596
// Returns a human readable string describing the target FailureMessage.
597
//
598
// NOTE: Implements the error interface.
599
func (f *FailInvalidBlinding) Error() string {
×
600
        return f.Code().String()
×
601
}
×
602

603
// Decode decodes the failure from bytes stream.
604
//
605
// NOTE: Part of the Serializable interface.
606
func (f *FailInvalidBlinding) Decode(r io.Reader, _ uint32) error {
×
607
        return ReadElement(r, f.OnionSHA256[:])
×
608
}
×
609

610
// Encode writes the failure in bytes stream.
611
//
612
// NOTE: Part of the Serializable interface.
613
func (f *FailInvalidBlinding) Encode(w io.Writer, _ uint32) error {
×
614
        return WriteElement(w, f.OnionSHA256[:])
×
615
}
×
616

617
// NewInvalidBlinding creates new instance of FailInvalidBlinding.
618
func NewInvalidBlinding(onion []byte) *FailInvalidBlinding {
×
619
        // The spec allows empty onion hashes for invalid blinding, so we only
×
620
        // include our onion hash if it's provided.
×
621
        if onion == nil {
×
622
                return &FailInvalidBlinding{}
×
623
        }
×
624

625
        return &FailInvalidBlinding{OnionSHA256: sha256.Sum256(onion)}
×
626
}
627

628
// parseChannelUpdateCompatabilityMode will attempt to parse a channel updated
629
// encoded into an onion error payload in two ways. First, we'll try the
630
// compatibility oriented version wherein we'll _skip_ the length prefixing on
631
// the channel update message. Older versions of c-lighting do this so we'll
632
// attempt to parse these messages in order to retain compatibility. If we're
633
// unable to pull out a fully valid version, then we'll fall back to the
634
// regular parsing mechanism which includes the length prefix an NO type byte.
635
func parseChannelUpdateCompatabilityMode(r *bufio.Reader,
UNCOV
636
        chanUpdate *ChannelUpdate, pver uint32) error {
×
UNCOV
637

×
UNCOV
638
        // We'll peek out two bytes from the buffer without advancing the
×
UNCOV
639
        // buffer so we can decide how to parse the remainder of it.
×
UNCOV
640
        maybeTypeBytes, err := r.Peek(2)
×
UNCOV
641
        if err != nil {
×
642
                return err
×
643
        }
×
644

645
        // Some nodes well prefix an additional set of bytes in front of their
646
        // channel updates. These bytes will _almost_ always be 258 or the type
647
        // of the ChannelUpdate message.
UNCOV
648
        typeInt := binary.BigEndian.Uint16(maybeTypeBytes)
×
UNCOV
649
        if typeInt == MsgChannelUpdate {
×
650
                // At this point it's likely the case that this is a channel
×
651
                // update message with its type prefixed, so we'll snip off the
×
652
                // first two bytes and parse it as normal.
×
653
                var throwAwayTypeBytes [2]byte
×
654
                _, err := r.Read(throwAwayTypeBytes[:])
×
655
                if err != nil {
×
656
                        return err
×
657
                }
×
658
        }
659

660
        // At this pint, we've either decided to keep the entire thing, or snip
661
        // off the first two bytes. In either case, we can just read it as
662
        // normal.
UNCOV
663
        return chanUpdate.Decode(r, pver)
×
664
}
665

666
// FailTemporaryChannelFailure is if an otherwise unspecified transient error
667
// occurs for the outgoing channel (eg. channel capacity reached, too many
668
// in-flight htlcs)
669
//
670
// NOTE: May only be returned by intermediate nodes.
671
type FailTemporaryChannelFailure struct {
672
        // Update is used to update information about state of the channel
673
        // which caused the failure.
674
        //
675
        // NOTE: This field is optional.
676
        Update *ChannelUpdate
677
}
678

679
// NewTemporaryChannelFailure creates new instance of the FailTemporaryChannelFailure.
680
func NewTemporaryChannelFailure(update *ChannelUpdate) *FailTemporaryChannelFailure {
×
681
        return &FailTemporaryChannelFailure{Update: update}
×
682
}
×
683

684
// Code returns the failure unique code.
685
//
686
// NOTE: Part of the FailureMessage interface.
687
func (f *FailTemporaryChannelFailure) Code() FailCode {
×
688
        return CodeTemporaryChannelFailure
×
689
}
×
690

691
// Returns a human readable string describing the target FailureMessage.
692
//
693
// NOTE: Implements the error interface.
694
func (f *FailTemporaryChannelFailure) Error() string {
×
695
        if f.Update == nil {
×
696
                return f.Code().String()
×
697
        }
×
698

699
        return fmt.Sprintf("TemporaryChannelFailure(update=%v)",
×
700
                spew.Sdump(f.Update))
×
701
}
702

703
// Decode decodes the failure from bytes stream.
704
//
705
// NOTE: Part of the Serializable interface.
706
func (f *FailTemporaryChannelFailure) Decode(r io.Reader, pver uint32) error {
×
707
        var length uint16
×
708
        err := ReadElement(r, &length)
×
709
        if err != nil {
×
710
                return err
×
711
        }
×
712

713
        if length != 0 {
×
714
                f.Update = &ChannelUpdate{}
×
715
                return parseChannelUpdateCompatabilityMode(
×
716
                        bufio.NewReader(r), f.Update, pver,
×
717
                )
×
718
        }
×
719

720
        return nil
×
721
}
722

723
// Encode writes the failure in bytes stream.
724
//
725
// NOTE: Part of the Serializable interface.
726
func (f *FailTemporaryChannelFailure) Encode(w io.Writer, pver uint32) error {
×
727
        var payload []byte
×
728
        if f.Update != nil {
×
729
                var bw bytes.Buffer
×
730
                if err := f.Update.Encode(&bw, pver); err != nil {
×
731
                        return err
×
732
                }
×
733
                payload = bw.Bytes()
×
734
        }
735

736
        if err := WriteElement(w, uint16(len(payload))); err != nil {
×
737
                return err
×
738
        }
×
739

740
        _, err := w.Write(payload)
×
741
        return err
×
742
}
743

744
// FailAmountBelowMinimum is returned if the HTLC does not reach the current
745
// minimum amount, we tell them the amount of the incoming HTLC and the current
746
// channel setting for the outgoing channel.
747
//
748
// NOTE: May only be returned by the intermediate nodes in the path.
749
type FailAmountBelowMinimum struct {
750
        // HtlcMsat is the wrong amount of the incoming HTLC.
751
        HtlcMsat MilliSatoshi
752

753
        // Update is used to update information about state of the channel
754
        // which caused the failure.
755
        Update ChannelUpdate
756
}
757

758
// NewAmountBelowMinimum creates new instance of the FailAmountBelowMinimum.
759
func NewAmountBelowMinimum(htlcMsat MilliSatoshi,
760
        update ChannelUpdate) *FailAmountBelowMinimum {
×
761

×
762
        return &FailAmountBelowMinimum{
×
763
                HtlcMsat: htlcMsat,
×
764
                Update:   update,
×
765
        }
×
766
}
×
767

768
// Code returns the failure unique code.
769
//
770
// NOTE: Part of the FailureMessage interface.
771
func (f *FailAmountBelowMinimum) Code() FailCode {
×
772
        return CodeAmountBelowMinimum
×
773
}
×
774

775
// Returns a human readable string describing the target FailureMessage.
776
//
777
// NOTE: Implements the error interface.
778
func (f *FailAmountBelowMinimum) Error() string {
×
779
        return fmt.Sprintf("AmountBelowMinimum(amt=%v, update=%v", f.HtlcMsat,
×
780
                spew.Sdump(f.Update))
×
781
}
×
782

783
// Decode decodes the failure from bytes stream.
784
//
785
// NOTE: Part of the Serializable interface.
786
func (f *FailAmountBelowMinimum) Decode(r io.Reader, pver uint32) error {
×
787
        if err := ReadElement(r, &f.HtlcMsat); err != nil {
×
788
                return err
×
789
        }
×
790

791
        var length uint16
×
792
        if err := ReadElement(r, &length); err != nil {
×
793
                return err
×
794
        }
×
795

796
        f.Update = ChannelUpdate{}
×
797
        return parseChannelUpdateCompatabilityMode(
×
798
                bufio.NewReader(r), &f.Update, pver,
×
799
        )
×
800
}
801

802
// Encode writes the failure in bytes stream.
803
//
804
// NOTE: Part of the Serializable interface.
805
func (f *FailAmountBelowMinimum) Encode(w io.Writer, pver uint32) error {
×
806
        if err := WriteElement(w, f.HtlcMsat); err != nil {
×
807
                return err
×
808
        }
×
809

810
        return writeOnionErrorChanUpdate(w, &f.Update, pver)
×
811
}
812

813
// FailFeeInsufficient is returned if the HTLC does not pay sufficient fee, we
814
// tell them the amount of the incoming HTLC and the current channel setting
815
// for the outgoing channel.
816
//
817
// NOTE: May only be returned by intermediate nodes.
818
type FailFeeInsufficient struct {
819
        // HtlcMsat is the wrong amount of the incoming HTLC.
820
        HtlcMsat MilliSatoshi
821

822
        // Update is used to update information about state of the channel
823
        // which caused the failure.
824
        Update ChannelUpdate
825
}
826

827
// NewFeeInsufficient creates new instance of the FailFeeInsufficient.
828
func NewFeeInsufficient(htlcMsat MilliSatoshi,
829
        update ChannelUpdate) *FailFeeInsufficient {
×
830
        return &FailFeeInsufficient{
×
831
                HtlcMsat: htlcMsat,
×
832
                Update:   update,
×
833
        }
×
834
}
×
835

836
// Code returns the failure unique code.
837
//
838
// NOTE: Part of the FailureMessage interface.
UNCOV
839
func (f *FailFeeInsufficient) Code() FailCode {
×
UNCOV
840
        return CodeFeeInsufficient
×
UNCOV
841
}
×
842

843
// Returns a human readable string describing the target FailureMessage.
844
//
845
// NOTE: Implements the error interface.
846
func (f *FailFeeInsufficient) Error() string {
×
847
        return fmt.Sprintf("FeeInsufficient(htlc_amt==%v, update=%v", f.HtlcMsat,
×
848
                spew.Sdump(f.Update))
×
849
}
×
850

851
// Decode decodes the failure from bytes stream.
852
//
853
// NOTE: Part of the Serializable interface.
UNCOV
854
func (f *FailFeeInsufficient) Decode(r io.Reader, pver uint32) error {
×
UNCOV
855
        if err := ReadElement(r, &f.HtlcMsat); err != nil {
×
856
                return err
×
857
        }
×
858

UNCOV
859
        var length uint16
×
UNCOV
860
        if err := ReadElement(r, &length); err != nil {
×
861
                return err
×
862
        }
×
863

UNCOV
864
        f.Update = ChannelUpdate{}
×
UNCOV
865
        return parseChannelUpdateCompatabilityMode(
×
UNCOV
866
                bufio.NewReader(r), &f.Update, pver,
×
UNCOV
867
        )
×
868
}
869

870
// Encode writes the failure in bytes stream.
871
//
872
// NOTE: Part of the Serializable interface.
UNCOV
873
func (f *FailFeeInsufficient) Encode(w io.Writer, pver uint32) error {
×
UNCOV
874
        if err := WriteElement(w, f.HtlcMsat); err != nil {
×
875
                return err
×
876
        }
×
877

UNCOV
878
        return writeOnionErrorChanUpdate(w, &f.Update, pver)
×
879
}
880

881
// FailIncorrectCltvExpiry is returned if outgoing cltv value does not match
882
// the update add htlc's cltv expiry minus cltv expiry delta for the outgoing
883
// channel, we tell them the cltv expiry and the current channel setting for
884
// the outgoing channel.
885
//
886
// NOTE: May only be returned by intermediate nodes.
887
type FailIncorrectCltvExpiry struct {
888
        // CltvExpiry is the wrong absolute timeout in blocks, after which
889
        // outgoing HTLC expires.
890
        CltvExpiry uint32
891

892
        // Update is used to update information about state of the channel
893
        // which caused the failure.
894
        Update ChannelUpdate
895
}
896

897
// NewIncorrectCltvExpiry creates new instance of the FailIncorrectCltvExpiry.
898
func NewIncorrectCltvExpiry(cltvExpiry uint32,
899
        update ChannelUpdate) *FailIncorrectCltvExpiry {
×
900

×
901
        return &FailIncorrectCltvExpiry{
×
902
                CltvExpiry: cltvExpiry,
×
903
                Update:     update,
×
904
        }
×
905
}
×
906

907
// Code returns the failure unique code.
908
//
909
// NOTE: Part of the FailureMessage interface.
910
func (f *FailIncorrectCltvExpiry) Code() FailCode {
×
911
        return CodeIncorrectCltvExpiry
×
912
}
×
913

914
func (f *FailIncorrectCltvExpiry) Error() string {
×
915
        return fmt.Sprintf("IncorrectCltvExpiry(expiry=%v, update=%v",
×
916
                f.CltvExpiry, spew.Sdump(f.Update))
×
917
}
×
918

919
// Decode decodes the failure from bytes stream.
920
//
921
// NOTE: Part of the Serializable interface.
922
func (f *FailIncorrectCltvExpiry) Decode(r io.Reader, pver uint32) error {
×
923
        if err := ReadElement(r, &f.CltvExpiry); err != nil {
×
924
                return err
×
925
        }
×
926

927
        var length uint16
×
928
        if err := ReadElement(r, &length); err != nil {
×
929
                return err
×
930
        }
×
931

932
        f.Update = ChannelUpdate{}
×
933
        return parseChannelUpdateCompatabilityMode(
×
934
                bufio.NewReader(r), &f.Update, pver,
×
935
        )
×
936
}
937

938
// Encode writes the failure in bytes stream.
939
//
940
// NOTE: Part of the Serializable interface.
941
func (f *FailIncorrectCltvExpiry) Encode(w io.Writer, pver uint32) error {
×
942
        if err := WriteElement(w, f.CltvExpiry); err != nil {
×
943
                return err
×
944
        }
×
945

946
        return writeOnionErrorChanUpdate(w, &f.Update, pver)
×
947
}
948

949
// FailExpiryTooSoon is returned if the ctlv-expiry is too near, we tell them
950
// the current channel setting for the outgoing channel.
951
//
952
// NOTE: May only be returned by intermediate nodes.
953
type FailExpiryTooSoon struct {
954
        // Update is used to update information about state of the channel
955
        // which caused the failure.
956
        Update ChannelUpdate
957
}
958

959
// NewExpiryTooSoon creates new instance of the FailExpiryTooSoon.
960
func NewExpiryTooSoon(update ChannelUpdate) *FailExpiryTooSoon {
×
961
        return &FailExpiryTooSoon{
×
962
                Update: update,
×
963
        }
×
964
}
×
965

966
// Code returns the failure unique code.
967
//
968
// NOTE: Part of the FailureMessage interface.
969
func (f *FailExpiryTooSoon) Code() FailCode {
×
970
        return CodeExpiryTooSoon
×
971
}
×
972

973
// Returns a human readable string describing the target FailureMessage.
974
//
975
// NOTE: Implements the error interface.
976
func (f *FailExpiryTooSoon) Error() string {
×
977
        return fmt.Sprintf("ExpiryTooSoon(update=%v", spew.Sdump(f.Update))
×
978
}
×
979

980
// Decode decodes the failure from l stream.
981
//
982
// NOTE: Part of the Serializable interface.
983
func (f *FailExpiryTooSoon) Decode(r io.Reader, pver uint32) error {
×
984
        var length uint16
×
985
        if err := ReadElement(r, &length); err != nil {
×
986
                return err
×
987
        }
×
988

989
        f.Update = ChannelUpdate{}
×
990
        return parseChannelUpdateCompatabilityMode(
×
991
                bufio.NewReader(r), &f.Update, pver,
×
992
        )
×
993
}
994

995
// Encode writes the failure in bytes stream.
996
//
997
// NOTE: Part of the Serializable interface.
998
func (f *FailExpiryTooSoon) Encode(w io.Writer, pver uint32) error {
×
999
        return writeOnionErrorChanUpdate(w, &f.Update, pver)
×
1000
}
×
1001

1002
// FailChannelDisabled is returned if the channel is disabled, we tell them the
1003
// current channel setting for the outgoing channel.
1004
//
1005
// NOTE: May only be returned by intermediate nodes.
1006
type FailChannelDisabled struct {
1007
        // Flags least-significant bit must be set to 0 if the creating node
1008
        // corresponds to the first node in the previously sent channel
1009
        // announcement and 1 otherwise.
1010
        Flags uint16
1011

1012
        // Update is used to update information about state of the channel
1013
        // which caused the failure.
1014
        Update ChannelUpdate
1015
}
1016

1017
// NewChannelDisabled creates new instance of the FailChannelDisabled.
1018
func NewChannelDisabled(flags uint16, update ChannelUpdate) *FailChannelDisabled {
×
1019
        return &FailChannelDisabled{
×
1020
                Flags:  flags,
×
1021
                Update: update,
×
1022
        }
×
1023
}
×
1024

1025
// Code returns the failure unique code.
1026
//
1027
// NOTE: Part of the FailureMessage interface.
1028
func (f *FailChannelDisabled) Code() FailCode {
×
1029
        return CodeChannelDisabled
×
1030
}
×
1031

1032
// Returns a human readable string describing the target FailureMessage.
1033
//
1034
// NOTE: Implements the error interface.
1035
func (f *FailChannelDisabled) Error() string {
×
1036
        return fmt.Sprintf("ChannelDisabled(flags=%v, update=%v", f.Flags,
×
1037
                spew.Sdump(f.Update))
×
1038
}
×
1039

1040
// Decode decodes the failure from bytes stream.
1041
//
1042
// NOTE: Part of the Serializable interface.
1043
func (f *FailChannelDisabled) Decode(r io.Reader, pver uint32) error {
×
1044
        if err := ReadElement(r, &f.Flags); err != nil {
×
1045
                return err
×
1046
        }
×
1047

1048
        var length uint16
×
1049
        if err := ReadElement(r, &length); err != nil {
×
1050
                return err
×
1051
        }
×
1052

1053
        f.Update = ChannelUpdate{}
×
1054
        return parseChannelUpdateCompatabilityMode(
×
1055
                bufio.NewReader(r), &f.Update, pver,
×
1056
        )
×
1057
}
1058

1059
// Encode writes the failure in bytes stream.
1060
//
1061
// NOTE: Part of the Serializable interface.
1062
func (f *FailChannelDisabled) Encode(w io.Writer, pver uint32) error {
×
1063
        if err := WriteElement(w, f.Flags); err != nil {
×
1064
                return err
×
1065
        }
×
1066

1067
        return writeOnionErrorChanUpdate(w, &f.Update, pver)
×
1068
}
1069

1070
// FailFinalIncorrectCltvExpiry is returned if the outgoing_cltv_value does not
1071
// match the ctlv_expiry of the HTLC at the final hop.
1072
//
1073
// NOTE: might be returned by final node only.
1074
type FailFinalIncorrectCltvExpiry struct {
1075
        // CltvExpiry is the wrong absolute timeout in blocks, after which
1076
        // outgoing HTLC expires.
1077
        CltvExpiry uint32
1078
}
1079

1080
// Returns a human readable string describing the target FailureMessage.
1081
//
1082
// NOTE: Implements the error interface.
1083
func (f *FailFinalIncorrectCltvExpiry) Error() string {
×
1084
        return fmt.Sprintf("FinalIncorrectCltvExpiry(expiry=%v)", f.CltvExpiry)
×
1085
}
×
1086

1087
// NewFinalIncorrectCltvExpiry creates new instance of the
1088
// FailFinalIncorrectCltvExpiry.
1089
func NewFinalIncorrectCltvExpiry(cltvExpiry uint32) *FailFinalIncorrectCltvExpiry {
×
1090
        return &FailFinalIncorrectCltvExpiry{
×
1091
                CltvExpiry: cltvExpiry,
×
1092
        }
×
1093
}
×
1094

1095
// Code returns the failure unique code.
1096
//
1097
// NOTE: Part of the FailureMessage interface.
1098
func (f *FailFinalIncorrectCltvExpiry) Code() FailCode {
×
1099
        return CodeFinalIncorrectCltvExpiry
×
1100
}
×
1101

1102
// Decode decodes the failure from bytes stream.
1103
//
1104
// NOTE: Part of the Serializable interface.
1105
func (f *FailFinalIncorrectCltvExpiry) Decode(r io.Reader, pver uint32) error {
×
1106
        return ReadElement(r, &f.CltvExpiry)
×
1107
}
×
1108

1109
// Encode writes the failure in bytes stream.
1110
//
1111
// NOTE: Part of the Serializable interface.
1112
func (f *FailFinalIncorrectCltvExpiry) Encode(w io.Writer, pver uint32) error {
×
1113
        return WriteElement(w, f.CltvExpiry)
×
1114
}
×
1115

1116
// FailFinalIncorrectHtlcAmount is returned if the amt_to_forward is higher
1117
// than incoming_htlc_amt of the HTLC at the final hop.
1118
//
1119
// NOTE: May only be returned by the final node.
1120
type FailFinalIncorrectHtlcAmount struct {
1121
        // IncomingHTLCAmount is the wrong forwarded htlc amount.
1122
        IncomingHTLCAmount MilliSatoshi
1123
}
1124

1125
// Returns a human readable string describing the target FailureMessage.
1126
//
1127
// NOTE: Implements the error interface.
1128
func (f *FailFinalIncorrectHtlcAmount) Error() string {
×
1129
        return fmt.Sprintf("FinalIncorrectHtlcAmount(amt=%v)",
×
1130
                f.IncomingHTLCAmount)
×
1131
}
×
1132

1133
// NewFinalIncorrectHtlcAmount creates new instance of the
1134
// FailFinalIncorrectHtlcAmount.
1135
func NewFinalIncorrectHtlcAmount(amount MilliSatoshi) *FailFinalIncorrectHtlcAmount {
×
1136
        return &FailFinalIncorrectHtlcAmount{
×
1137
                IncomingHTLCAmount: amount,
×
1138
        }
×
1139
}
×
1140

1141
// Code returns the failure unique code.
1142
//
1143
// NOTE: Part of the FailureMessage interface.
1144
func (f *FailFinalIncorrectHtlcAmount) Code() FailCode {
×
1145
        return CodeFinalIncorrectHtlcAmount
×
1146
}
×
1147

1148
// Decode decodes the failure from bytes stream.
1149
//
1150
// NOTE: Part of the Serializable interface.
1151
func (f *FailFinalIncorrectHtlcAmount) Decode(r io.Reader, pver uint32) error {
×
1152
        return ReadElement(r, &f.IncomingHTLCAmount)
×
1153
}
×
1154

1155
// Encode writes the failure in bytes stream.
1156
//
1157
// NOTE: Part of the Serializable interface.
1158
func (f *FailFinalIncorrectHtlcAmount) Encode(w io.Writer, pver uint32) error {
×
1159
        return WriteElement(w, f.IncomingHTLCAmount)
×
1160
}
×
1161

1162
// FailExpiryTooFar is returned if the CLTV expiry in the HTLC is too far in the
1163
// future.
1164
//
1165
// NOTE: May be returned by any node in the payment route.
1166
type FailExpiryTooFar struct{}
1167

1168
// Code returns the failure unique code.
1169
//
1170
// NOTE: Part of the FailureMessage interface.
1171
func (f *FailExpiryTooFar) Code() FailCode {
×
1172
        return CodeExpiryTooFar
×
1173
}
×
1174

1175
// Returns a human readable string describing the target FailureMessage.
1176
//
1177
// NOTE: Implements the error interface.
1178
func (f *FailExpiryTooFar) Error() string {
×
1179
        return f.Code().String()
×
1180
}
×
1181

1182
// InvalidOnionPayload is returned if the hop could not process the TLV payload
1183
// enclosed in the onion.
1184
type InvalidOnionPayload struct {
1185
        // Type is the TLV type that caused the specific failure.
1186
        Type uint64
1187

1188
        // Offset is the byte offset within the payload where the failure
1189
        // occurred.
1190
        Offset uint16
1191
}
1192

1193
// NewInvalidOnionPayload initializes a new InvalidOnionPayload failure.
1194
func NewInvalidOnionPayload(typ uint64, offset uint16) *InvalidOnionPayload {
×
1195
        return &InvalidOnionPayload{
×
1196
                Type:   typ,
×
1197
                Offset: offset,
×
1198
        }
×
1199
}
×
1200

1201
// Code returns the failure unique code.
1202
//
1203
// NOTE: Part of the FailureMessage interface.
1204
func (f *InvalidOnionPayload) Code() FailCode {
×
1205
        return CodeInvalidOnionPayload
×
1206
}
×
1207

1208
// Returns a human readable string describing the target FailureMessage.
1209
//
1210
// NOTE: Implements the error interface.
1211
func (f *InvalidOnionPayload) Error() string {
×
1212
        return fmt.Sprintf("%v(type=%v, offset=%d)",
×
1213
                f.Code(), f.Type, f.Offset)
×
1214
}
×
1215

1216
// Decode decodes the failure from bytes stream.
1217
//
1218
// NOTE: Part of the Serializable interface.
1219
func (f *InvalidOnionPayload) Decode(r io.Reader, pver uint32) error {
×
1220
        var buf [8]byte
×
1221
        typ, err := tlv.ReadVarInt(r, &buf)
×
1222
        if err != nil {
×
1223
                return err
×
1224
        }
×
1225
        f.Type = typ
×
1226

×
1227
        return ReadElements(r, &f.Offset)
×
1228
}
1229

1230
// Encode writes the failure in bytes stream.
1231
//
1232
// NOTE: Part of the Serializable interface.
1233
func (f *InvalidOnionPayload) Encode(w io.Writer, pver uint32) error {
×
1234
        var buf [8]byte
×
1235
        if err := tlv.WriteVarInt(w, f.Type, &buf); err != nil {
×
1236
                return err
×
1237
        }
×
1238

1239
        return WriteElements(w, f.Offset)
×
1240
}
1241

1242
// FailMPPTimeout is returned if the complete amount for a multi part payment
1243
// was not received within a reasonable time.
1244
//
1245
// NOTE: May only be returned by the final node in the path.
1246
type FailMPPTimeout struct{}
1247

1248
// Code returns the failure unique code.
1249
//
1250
// NOTE: Part of the FailureMessage interface.
1251
func (f *FailMPPTimeout) Code() FailCode {
×
1252
        return CodeMPPTimeout
×
1253
}
×
1254

1255
// Returns a human readable string describing the target FailureMessage.
1256
//
1257
// NOTE: Implements the error interface.
1258
func (f *FailMPPTimeout) Error() string {
×
1259
        return f.Code().String()
×
1260
}
×
1261

1262
// DecodeFailure decodes, validates, and parses the lnwire onion failure, for
1263
// the provided protocol version.
1264
func DecodeFailure(r io.Reader, pver uint32) (FailureMessage, error) {
×
1265
        // First, we'll parse out the encapsulated failure message itself. This
×
1266
        // is a 2 byte length followed by the payload itself.
×
1267
        var failureLength uint16
×
1268
        if err := ReadElement(r, &failureLength); err != nil {
×
1269
                return nil, fmt.Errorf("unable to read error len: %w", err)
×
1270
        }
×
1271
        if failureLength > FailureMessageLength {
×
1272
                return nil, fmt.Errorf("failure message is too "+
×
1273
                        "long: %v", failureLength)
×
1274
        }
×
1275
        failureData := make([]byte, failureLength)
×
1276
        if _, err := io.ReadFull(r, failureData); err != nil {
×
1277
                return nil, fmt.Errorf("unable to full read payload of "+
×
1278
                        "%v: %v", failureLength, err)
×
1279
        }
×
1280

1281
        dataReader := bytes.NewReader(failureData)
×
1282

×
1283
        return DecodeFailureMessage(dataReader, pver)
×
1284
}
1285

1286
// DecodeFailureMessage decodes just the failure message, ignoring any padding
1287
// that may be present at the end.
UNCOV
1288
func DecodeFailureMessage(r io.Reader, pver uint32) (FailureMessage, error) {
×
UNCOV
1289
        // Once we have the failure data, we can obtain the failure code from
×
UNCOV
1290
        // the first two bytes of the buffer.
×
UNCOV
1291
        var codeBytes [2]byte
×
UNCOV
1292
        if _, err := io.ReadFull(r, codeBytes[:]); err != nil {
×
1293
                return nil, fmt.Errorf("unable to read failure code: %w", err)
×
1294
        }
×
UNCOV
1295
        failCode := FailCode(binary.BigEndian.Uint16(codeBytes[:]))
×
UNCOV
1296

×
UNCOV
1297
        // Create the empty failure by given code and populate the failure with
×
UNCOV
1298
        // additional data if needed.
×
UNCOV
1299
        failure, err := makeEmptyOnionError(failCode)
×
UNCOV
1300
        if err != nil {
×
1301
                return nil, fmt.Errorf("unable to make empty error: %w", err)
×
1302
        }
×
1303

1304
        // Finally, if this failure has a payload, then we'll read that now as
1305
        // well.
UNCOV
1306
        switch f := failure.(type) {
×
UNCOV
1307
        case Serializable:
×
UNCOV
1308
                if err := f.Decode(r, pver); err != nil {
×
1309
                        return nil, fmt.Errorf("unable to decode error "+
×
1310
                                "update (type=%T): %v", failure, err)
×
1311
                }
×
1312
        }
1313

UNCOV
1314
        return failure, nil
×
1315
}
1316

1317
// EncodeFailure encodes, including the necessary onion failure header
1318
// information.
1319
func EncodeFailure(w io.Writer, failure FailureMessage, pver uint32) error {
×
1320
        var failureMessageBuffer bytes.Buffer
×
1321

×
1322
        err := EncodeFailureMessage(&failureMessageBuffer, failure, pver)
×
1323
        if err != nil {
×
1324
                return err
×
1325
        }
×
1326

1327
        // The combined size of this message must be below the max allowed
1328
        // failure message length.
1329
        failureMessage := failureMessageBuffer.Bytes()
×
1330
        if len(failureMessage) > FailureMessageLength {
×
1331
                return fmt.Errorf("failure message exceed max "+
×
1332
                        "available size: %v", len(failureMessage))
×
1333
        }
×
1334

1335
        // Finally, we'll add some padding in order to ensure that all failure
1336
        // messages are fixed size.
1337
        pad := make([]byte, FailureMessageLength-len(failureMessage))
×
1338

×
1339
        return WriteElements(w,
×
1340
                uint16(len(failureMessage)),
×
1341
                failureMessage,
×
1342
                uint16(len(pad)),
×
1343
                pad,
×
1344
        )
×
1345
}
1346

1347
// EncodeFailureMessage encodes just the failure message without adding a length
1348
// and padding the message for the onion protocol.
UNCOV
1349
func EncodeFailureMessage(w io.Writer, failure FailureMessage, pver uint32) error {
×
UNCOV
1350
        // First, we'll write out the error code itself into the failure
×
UNCOV
1351
        // buffer.
×
UNCOV
1352
        var codeBytes [2]byte
×
UNCOV
1353
        code := uint16(failure.Code())
×
UNCOV
1354
        binary.BigEndian.PutUint16(codeBytes[:], code)
×
UNCOV
1355
        _, err := w.Write(codeBytes[:])
×
UNCOV
1356
        if err != nil {
×
1357
                return err
×
1358
        }
×
1359

1360
        // Next, some message have an additional message payload, if this is
1361
        // one of those types, then we'll also encode the error payload as
1362
        // well.
UNCOV
1363
        switch failure := failure.(type) {
×
UNCOV
1364
        case Serializable:
×
UNCOV
1365
                if err := failure.Encode(w, pver); err != nil {
×
1366
                        return err
×
1367
                }
×
1368
        }
1369

UNCOV
1370
        return nil
×
1371
}
1372

1373
// makeEmptyOnionError creates a new empty onion error  of the proper concrete
1374
// type based on the passed failure code.
UNCOV
1375
func makeEmptyOnionError(code FailCode) (FailureMessage, error) {
×
UNCOV
1376
        switch code {
×
1377
        case CodeInvalidRealm:
×
1378
                return &FailInvalidRealm{}, nil
×
1379

1380
        case CodeTemporaryNodeFailure:
×
1381
                return &FailTemporaryNodeFailure{}, nil
×
1382

1383
        case CodePermanentNodeFailure:
×
1384
                return &FailPermanentNodeFailure{}, nil
×
1385

1386
        case CodeRequiredNodeFeatureMissing:
×
1387
                return &FailRequiredNodeFeatureMissing{}, nil
×
1388

1389
        case CodePermanentChannelFailure:
×
1390
                return &FailPermanentChannelFailure{}, nil
×
1391

1392
        case CodeRequiredChannelFeatureMissing:
×
1393
                return &FailRequiredChannelFeatureMissing{}, nil
×
1394

1395
        case CodeUnknownNextPeer:
×
1396
                return &FailUnknownNextPeer{}, nil
×
1397

1398
        case CodeIncorrectOrUnknownPaymentDetails:
×
1399
                return &FailIncorrectDetails{}, nil
×
1400

1401
        case CodeIncorrectPaymentAmount:
×
1402
                return &FailIncorrectPaymentAmount{}, nil
×
1403

1404
        case CodeFinalExpiryTooSoon:
×
1405
                return &FailFinalExpiryTooSoon{}, nil
×
1406

1407
        case CodeInvalidOnionVersion:
×
1408
                return &FailInvalidOnionVersion{}, nil
×
1409

1410
        case CodeInvalidOnionHmac:
×
1411
                return &FailInvalidOnionHmac{}, nil
×
1412

1413
        case CodeInvalidOnionKey:
×
1414
                return &FailInvalidOnionKey{}, nil
×
1415

1416
        case CodeTemporaryChannelFailure:
×
1417
                return &FailTemporaryChannelFailure{}, nil
×
1418

1419
        case CodeAmountBelowMinimum:
×
1420
                return &FailAmountBelowMinimum{}, nil
×
1421

UNCOV
1422
        case CodeFeeInsufficient:
×
UNCOV
1423
                return &FailFeeInsufficient{}, nil
×
1424

1425
        case CodeIncorrectCltvExpiry:
×
1426
                return &FailIncorrectCltvExpiry{}, nil
×
1427

1428
        case CodeExpiryTooSoon:
×
1429
                return &FailExpiryTooSoon{}, nil
×
1430

1431
        case CodeChannelDisabled:
×
1432
                return &FailChannelDisabled{}, nil
×
1433

1434
        case CodeFinalIncorrectCltvExpiry:
×
1435
                return &FailFinalIncorrectCltvExpiry{}, nil
×
1436

1437
        case CodeFinalIncorrectHtlcAmount:
×
1438
                return &FailFinalIncorrectHtlcAmount{}, nil
×
1439

1440
        case CodeExpiryTooFar:
×
1441
                return &FailExpiryTooFar{}, nil
×
1442

1443
        case CodeInvalidOnionPayload:
×
1444
                return &InvalidOnionPayload{}, nil
×
1445

1446
        case CodeMPPTimeout:
×
1447
                return &FailMPPTimeout{}, nil
×
1448

1449
        case CodeInvalidBlinding:
×
1450
                return &FailInvalidBlinding{}, nil
×
1451

1452
        default:
×
1453
                return nil, fmt.Errorf("unknown error code: %v", code)
×
1454
        }
1455
}
1456

1457
// writeOnionErrorChanUpdate writes out a ChannelUpdate using the onion error
1458
// format. The format is that we first write out the true serialized length of
1459
// the channel update, followed by the serialized channel update itself.
1460
func writeOnionErrorChanUpdate(w io.Writer, chanUpdate *ChannelUpdate,
UNCOV
1461
        pver uint32) error {
×
UNCOV
1462

×
UNCOV
1463
        // First, we encode the channel update in a temporary buffer in order
×
UNCOV
1464
        // to get the exact serialized size.
×
UNCOV
1465
        var b bytes.Buffer
×
UNCOV
1466
        if err := chanUpdate.Encode(&b, pver); err != nil {
×
1467
                return err
×
1468
        }
×
1469

1470
        // Now that we know the size, we can write the length out in the main
1471
        // writer.
UNCOV
1472
        updateLen := b.Len()
×
UNCOV
1473
        if err := WriteElement(w, uint16(updateLen)); err != nil {
×
1474
                return err
×
1475
        }
×
1476

1477
        // With the length written, we'll then write out the serialized channel
1478
        // update.
UNCOV
1479
        if _, err := w.Write(b.Bytes()); err != nil {
×
1480
                return err
×
1481
        }
×
1482

UNCOV
1483
        return nil
×
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