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

lightningnetwork / lnd / 11170835610

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

push

github

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

multi: bump btcd version.

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

26110 existing lines in 428 files now uncovered.

97359 of 197934 relevant lines covered (49.19%)

1.04 hits per line

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

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/go-errors/errors"
13
        "github.com/lightningnetwork/lnd/tlv"
14
)
15

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

166
        default:
×
167
                return "<unknown>"
×
168
        }
169
}
170

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

351
        // height is the block height when the htlc was received.
352
        height uint32
353
}
354

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

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

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

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

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

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

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

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

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

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

421
        return nil
×
422
}
423

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

622
        return &FailInvalidBlinding{OnionSHA256: sha256.Sum256(onion)}
×
623
}
624

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

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

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

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

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

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

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

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

696
        return fmt.Sprintf("TemporaryChannelFailure(update=%v)",
×
697
                spew.Sdump(f.Update))
×
698
}
699

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

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

717
        return nil
×
718
}
719

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

733
        if err := WriteElement(w, uint16(len(payload))); err != nil {
×
734
                return err
×
735
        }
×
736

737
        _, err := w.Write(payload)
×
738
        return err
×
739
}
740

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

750
        // Update is used to update information about state of the channel
751
        // which caused the failure.
752
        Update ChannelUpdate
753
}
754

755
// NewAmountBelowMinimum creates new instance of the FailAmountBelowMinimum.
756
func NewAmountBelowMinimum(htlcMsat MilliSatoshi,
757
        update ChannelUpdate) *FailAmountBelowMinimum {
×
758

×
759
        return &FailAmountBelowMinimum{
×
760
                HtlcMsat: htlcMsat,
×
761
                Update:   update,
×
762
        }
×
763
}
×
764

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

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

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

788
        var length uint16
×
789
        if err := ReadElement(r, &length); err != nil {
×
790
                return err
×
791
        }
×
792

793
        f.Update = ChannelUpdate{}
×
794
        return parseChannelUpdateCompatabilityMode(
×
795
                bufio.NewReader(r), &f.Update, pver,
×
796
        )
×
797
}
798

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

807
        return writeOnionErrorChanUpdate(w, &f.Update, pver)
×
808
}
809

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

819
        // Update is used to update information about state of the channel
820
        // which caused the failure.
821
        Update ChannelUpdate
822
}
823

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

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

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

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

UNCOV
856
        var length uint16
×
UNCOV
857
        if err := ReadElement(r, &length); err != nil {
×
858
                return err
×
859
        }
×
860

UNCOV
861
        f.Update = ChannelUpdate{}
×
UNCOV
862
        return parseChannelUpdateCompatabilityMode(
×
UNCOV
863
                bufio.NewReader(r), &f.Update, pver,
×
UNCOV
864
        )
×
865
}
866

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

UNCOV
875
        return writeOnionErrorChanUpdate(w, &f.Update, pver)
×
876
}
877

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

889
        // Update is used to update information about state of the channel
890
        // which caused the failure.
891
        Update ChannelUpdate
892
}
893

894
// NewIncorrectCltvExpiry creates new instance of the FailIncorrectCltvExpiry.
895
func NewIncorrectCltvExpiry(cltvExpiry uint32,
896
        update ChannelUpdate) *FailIncorrectCltvExpiry {
×
897

×
898
        return &FailIncorrectCltvExpiry{
×
899
                CltvExpiry: cltvExpiry,
×
900
                Update:     update,
×
901
        }
×
902
}
×
903

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

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

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

924
        var length uint16
×
925
        if err := ReadElement(r, &length); err != nil {
×
926
                return err
×
927
        }
×
928

929
        f.Update = ChannelUpdate{}
×
930
        return parseChannelUpdateCompatabilityMode(
×
931
                bufio.NewReader(r), &f.Update, pver,
×
932
        )
×
933
}
934

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

943
        return writeOnionErrorChanUpdate(w, &f.Update, pver)
×
944
}
945

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

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

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

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

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

986
        f.Update = ChannelUpdate{}
×
987
        return parseChannelUpdateCompatabilityMode(
×
988
                bufio.NewReader(r), &f.Update, pver,
×
989
        )
×
990
}
991

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

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

1009
        // Update is used to update information about state of the channel
1010
        // which caused the failure.
1011
        Update ChannelUpdate
1012
}
1013

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

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

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

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

1045
        var length uint16
×
1046
        if err := ReadElement(r, &length); err != nil {
×
1047
                return err
×
1048
        }
×
1049

1050
        f.Update = ChannelUpdate{}
×
1051
        return parseChannelUpdateCompatabilityMode(
×
1052
                bufio.NewReader(r), &f.Update, pver,
×
1053
        )
×
1054
}
1055

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

1064
        return writeOnionErrorChanUpdate(w, &f.Update, pver)
×
1065
}
1066

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1185
        // Offset is the byte offset within the payload where the failure
1186
        // occurred.
1187
        Offset uint16
1188
}
1189

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

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

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

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

×
1224
        return ReadElements(r, &f.Offset)
×
1225
}
1226

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

1236
        return WriteElements(w, f.Offset)
×
1237
}
1238

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

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

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

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

1278
        dataReader := bytes.NewReader(failureData)
×
1279

×
1280
        return DecodeFailureMessage(dataReader, pver)
×
1281
}
1282

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

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

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

UNCOV
1311
        return failure, nil
×
1312
}
1313

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

×
1319
        err := EncodeFailureMessage(&failureMessageBuffer, failure, pver)
×
1320
        if err != nil {
×
1321
                return err
×
1322
        }
×
1323

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

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

×
1336
        return WriteElements(w,
×
1337
                uint16(len(failureMessage)),
×
1338
                failureMessage,
×
1339
                uint16(len(pad)),
×
1340
                pad,
×
1341
        )
×
1342
}
1343

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

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

UNCOV
1367
        return nil
×
1368
}
1369

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

1377
        case CodeTemporaryNodeFailure:
×
1378
                return &FailTemporaryNodeFailure{}, nil
×
1379

1380
        case CodePermanentNodeFailure:
×
1381
                return &FailPermanentNodeFailure{}, nil
×
1382

1383
        case CodeRequiredNodeFeatureMissing:
×
1384
                return &FailRequiredNodeFeatureMissing{}, nil
×
1385

1386
        case CodePermanentChannelFailure:
×
1387
                return &FailPermanentChannelFailure{}, nil
×
1388

1389
        case CodeRequiredChannelFeatureMissing:
×
1390
                return &FailRequiredChannelFeatureMissing{}, nil
×
1391

1392
        case CodeUnknownNextPeer:
×
1393
                return &FailUnknownNextPeer{}, nil
×
1394

1395
        case CodeIncorrectOrUnknownPaymentDetails:
×
1396
                return &FailIncorrectDetails{}, nil
×
1397

1398
        case CodeIncorrectPaymentAmount:
×
1399
                return &FailIncorrectPaymentAmount{}, nil
×
1400

1401
        case CodeFinalExpiryTooSoon:
×
1402
                return &FailFinalExpiryTooSoon{}, nil
×
1403

1404
        case CodeInvalidOnionVersion:
×
1405
                return &FailInvalidOnionVersion{}, nil
×
1406

1407
        case CodeInvalidOnionHmac:
×
1408
                return &FailInvalidOnionHmac{}, nil
×
1409

1410
        case CodeInvalidOnionKey:
×
1411
                return &FailInvalidOnionKey{}, nil
×
1412

1413
        case CodeTemporaryChannelFailure:
×
1414
                return &FailTemporaryChannelFailure{}, nil
×
1415

1416
        case CodeAmountBelowMinimum:
×
1417
                return &FailAmountBelowMinimum{}, nil
×
1418

UNCOV
1419
        case CodeFeeInsufficient:
×
UNCOV
1420
                return &FailFeeInsufficient{}, nil
×
1421

1422
        case CodeIncorrectCltvExpiry:
×
1423
                return &FailIncorrectCltvExpiry{}, nil
×
1424

1425
        case CodeExpiryTooSoon:
×
1426
                return &FailExpiryTooSoon{}, nil
×
1427

1428
        case CodeChannelDisabled:
×
1429
                return &FailChannelDisabled{}, nil
×
1430

1431
        case CodeFinalIncorrectCltvExpiry:
×
1432
                return &FailFinalIncorrectCltvExpiry{}, nil
×
1433

1434
        case CodeFinalIncorrectHtlcAmount:
×
1435
                return &FailFinalIncorrectHtlcAmount{}, nil
×
1436

1437
        case CodeExpiryTooFar:
×
1438
                return &FailExpiryTooFar{}, nil
×
1439

1440
        case CodeInvalidOnionPayload:
×
1441
                return &InvalidOnionPayload{}, nil
×
1442

1443
        case CodeMPPTimeout:
×
1444
                return &FailMPPTimeout{}, nil
×
1445

1446
        case CodeInvalidBlinding:
×
1447
                return &FailInvalidBlinding{}, nil
×
1448

1449
        default:
×
1450
                return nil, errors.Errorf("unknown error code: %v", code)
×
1451
        }
1452
}
1453

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

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

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

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

UNCOV
1480
        return nil
×
1481
}
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