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

lightningnetwork / lnd / 13035292482

29 Jan 2025 03:59PM UTC coverage: 49.3% (-9.5%) from 58.777%
13035292482

Pull #9456

github

mohamedawnallah
docs: update release-notes-0.19.0.md

In this commit, we warn users about the removal
of RPCs `SendToRoute`, `SendToRouteSync`, `SendPayment`,
and `SendPaymentSync` in the next release 0.20.
Pull Request #9456: lnrpc+docs: deprecate warning `SendToRoute`, `SendToRouteSync`, `SendPayment`, and `SendPaymentSync` in Release 0.19

100634 of 204126 relevant lines covered (49.3%)

1.54 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:ll
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
// A compile-time check to ensure that FailInvalidBlinding implements the
587
// Serializable interface.
588
var _ Serializable = (*FailInvalidBlinding)(nil)
589

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

721
        return nil
×
722
}
723

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1315
        return failure, nil
×
1316
}
1317

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

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

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

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

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

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

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

1371
        return nil
×
1372
}
1373

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1484
        return nil
×
1485
}
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