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

lightningnetwork / lnd / 11216766535

07 Oct 2024 01:37PM UTC coverage: 57.817% (-1.0%) from 58.817%
11216766535

Pull #9148

github

ProofOfKeags
lnwire: remove kickoff feerate from propose/commit
Pull Request #9148: DynComms [2/n]: lnwire: add authenticated wire messages for Dyn*

571 of 879 new or added lines in 16 files covered. (64.96%)

23253 existing lines in 251 files now uncovered.

99022 of 171268 relevant lines covered (57.82%)

38420.67 hits per line

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

10.03
/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

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

UNCOV
600
// Decode decodes the failure from bytes stream.
×
UNCOV
601
//
×
UNCOV
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

UNCOV
607
// Encode writes the failure in bytes stream.
×
UNCOV
608
//
×
UNCOV
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

UNCOV
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
        }
×
UNCOV
621

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

×
625
// parseChannelUpdateCompatabilityMode will attempt to parse a channel updated
UNCOV
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,
633
        chanUpdate *ChannelUpdate, pver uint32) error {
634

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

1✔
642
        // Some nodes well prefix an additional set of bytes in front of their
1✔
UNCOV
643
        // channel updates. These bytes will _almost_ always be 258 or the type
×
UNCOV
644
        // of the ChannelUpdate message.
×
645
        typeInt := binary.BigEndian.Uint16(maybeTypeBytes)
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.
1✔
650
                var throwAwayTypeBytes [2]byte
1✔
651
                _, err := r.Read(throwAwayTypeBytes[:])
×
652
                if err != nil {
×
653
                        return err
×
654
                }
×
UNCOV
655
        }
×
UNCOV
656

×
UNCOV
657
        // At this pint, we've either decided to keep the entire thing, or snip
×
UNCOV
658
        // off the first two bytes. In either case, we can just read it as
×
659
        // normal.
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
1✔
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

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

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

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

UNCOV
700
// Decode decodes the failure from bytes stream.
×
UNCOV
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
        }
×
UNCOV
709

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

×
717
        return nil
×
UNCOV
718
}
×
UNCOV
719

×
720
// Encode writes the failure in bytes stream.
UNCOV
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()
×
UNCOV
731
        }
×
UNCOV
732

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

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

UNCOV
741
// FailAmountBelowMinimum is returned if the HTLC does not reach the current
×
UNCOV
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
}
×
UNCOV
764

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

UNCOV
772
// Returns a human readable string describing the target FailureMessage.
×
UNCOV
773
//
×
UNCOV
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
}
UNCOV
779

×
UNCOV
780
// Decode decodes the failure from bytes stream.
×
UNCOV
781
//
×
UNCOV
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
        }
UNCOV
787

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

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

×
UNCOV
799
// Encode writes the failure in bytes stream.
×
UNCOV
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
        }
UNCOV
806

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

×
810
// FailFeeInsufficient is returned if the HTLC does not pay sufficient fee, we
UNCOV
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
}
×
UNCOV
832

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

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

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

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

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

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

3✔
875
        return writeOnionErrorChanUpdate(w, &f.Update, pver)
3✔
UNCOV
876
}
×
UNCOV
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
3✔
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
}
×
UNCOV
903

×
UNCOV
904
// Code returns the failure unique code.
×
UNCOV
905
//
×
UNCOV
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
}
UNCOV
915

×
UNCOV
916
// Decode decodes the failure from bytes stream.
×
UNCOV
917
//
×
UNCOV
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
        }
UNCOV
923

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

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

×
UNCOV
935
// Encode writes the failure in bytes stream.
×
UNCOV
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
        }
UNCOV
942

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

×
946
// FailExpiryTooSoon is returned if the ctlv-expiry is too near, we tell them
UNCOV
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
}
×
UNCOV
962

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

UNCOV
970
// Returns a human readable string describing the target FailureMessage.
×
UNCOV
971
//
×
UNCOV
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

UNCOV
977
// Decode decodes the failure from l stream.
×
UNCOV
978
//
×
UNCOV
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
        }
×
UNCOV
985

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

×
UNCOV
992
// Encode writes the failure in bytes stream.
×
UNCOV
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

UNCOV
999
// FailChannelDisabled is returned if the channel is disabled, we tell them the
×
UNCOV
1000
// current channel setting for the outgoing channel.
×
UNCOV
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
}
×
UNCOV
1021

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

UNCOV
1029
// Returns a human readable string describing the target FailureMessage.
×
UNCOV
1030
//
×
UNCOV
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
}
UNCOV
1036

×
UNCOV
1037
// Decode decodes the failure from bytes stream.
×
UNCOV
1038
//
×
UNCOV
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
        }
UNCOV
1044

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

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

×
UNCOV
1056
// Encode writes the failure in bytes stream.
×
UNCOV
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
        }
UNCOV
1063

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

×
1067
// FailFinalIncorrectCltvExpiry is returned if the outgoing_cltv_value does not
UNCOV
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

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

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

UNCOV
1099
// Decode decodes the failure from bytes stream.
×
UNCOV
1100
//
×
UNCOV
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

UNCOV
1106
// Encode writes the failure in bytes stream.
×
UNCOV
1107
//
×
UNCOV
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

UNCOV
1113
// FailFinalIncorrectHtlcAmount is returned if the amt_to_forward is higher
×
UNCOV
1114
// than incoming_htlc_amt of the HTLC at the final hop.
×
UNCOV
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
}
UNCOV
1129

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

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

UNCOV
1145
// Decode decodes the failure from bytes stream.
×
UNCOV
1146
//
×
UNCOV
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

UNCOV
1152
// Encode writes the failure in bytes stream.
×
UNCOV
1153
//
×
UNCOV
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

UNCOV
1159
// FailExpiryTooFar is returned if the CLTV expiry in the HTLC is too far in the
×
UNCOV
1160
// future.
×
UNCOV
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

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

UNCOV
1179
// InvalidOnionPayload is returned if the hop could not process the TLV payload
×
UNCOV
1180
// enclosed in the onion.
×
UNCOV
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
}
×
UNCOV
1197

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

UNCOV
1205
// Returns a human readable string describing the target FailureMessage.
×
UNCOV
1206
//
×
UNCOV
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
}
UNCOV
1212

×
UNCOV
1213
// Decode decodes the failure from bytes stream.
×
UNCOV
1214
//
×
UNCOV
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)
×
UNCOV
1225
}
×
UNCOV
1226

×
UNCOV
1227
// Encode writes the failure in bytes stream.
×
UNCOV
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
        }
×
UNCOV
1235

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

×
1239
// FailMPPTimeout is returned if the complete amount for a multi part payment
UNCOV
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

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

UNCOV
1259
// DecodeFailure decodes, validates, and parses the lnwire onion failure, for
×
UNCOV
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
        }
×
UNCOV
1277

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

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

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

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

1✔
1301
        // Finally, if this failure has a payload, then we'll read that now as
1✔
UNCOV
1302
        // well.
×
UNCOV
1303
        switch f := failure.(type) {
×
1304
        case Serializable:
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)
1✔
1308
                }
1✔
1309
        }
1✔
UNCOV
1310

×
UNCOV
1311
        return failure, nil
×
UNCOV
1312
}
×
1313

1314
// EncodeFailure encodes, including the necessary onion failure header
1315
// information.
1✔
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
        }
×
UNCOV
1323

×
UNCOV
1324
        // The combined size of this message must be below the max allowed
×
UNCOV
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
        }
×
UNCOV
1331

×
UNCOV
1332
        // Finally, we'll add some padding in order to ensure that all failure
×
UNCOV
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
        )
×
UNCOV
1342
}
×
UNCOV
1343

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

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

3✔
UNCOV
1367
        return nil
×
UNCOV
1368
}
×
1369

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

×
UNCOV
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,
1458
        pver uint32) error {
1459

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

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

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

1480
        return nil
3✔
UNCOV
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