github.com/decred/dcrlnd@v0.7.6/lnwire/onion_error.go (about) 1 package lnwire 2 3 import ( 4 "bufio" 5 "crypto/sha256" 6 "encoding/binary" 7 "fmt" 8 "io" 9 10 "bytes" 11 12 "github.com/davecgh/go-spew/spew" 13 "github.com/decred/dcrlnd/tlv" 14 "github.com/go-errors/errors" 15 ) 16 17 // FailureMessage represents the onion failure object identified by its unique 18 // failure code. 19 type FailureMessage interface { 20 // Code returns a failure code describing the exact nature of the 21 // error. 22 Code() FailCode 23 24 // Error returns a human readable string describing the error. With 25 // this method, the FailureMessage interface meets the built-in error 26 // interface. 27 Error() string 28 } 29 30 // FailureMessageLength is the size of the failure message plus the size of 31 // padding. The FailureMessage message should always be EXACTLY this size. 32 const FailureMessageLength = 256 33 34 const ( 35 // FlagBadOnion error flag describes an unparsable, encrypted by 36 // previous node. 37 FlagBadOnion FailCode = 0x8000 38 39 // FlagPerm error flag indicates a permanent failure. 40 FlagPerm FailCode = 0x4000 41 42 // FlagNode error flag indicates a node failure. 43 FlagNode FailCode = 0x2000 44 45 // FlagUpdate error flag indicates a new channel update is enclosed 46 // within the error. 47 FlagUpdate FailCode = 0x1000 48 ) 49 50 // FailCode specifies the precise reason that an upstream HTLC was canceled. 51 // Each UpdateFailHTLC message carries a FailCode which is to be passed 52 // backwards, encrypted at each step back to the source of the HTLC within the 53 // route. 54 type FailCode uint16 55 56 // The currently defined onion failure types within this current version of the 57 // Lightning protocol. 58 const ( 59 CodeNone FailCode = 0 60 CodeInvalidRealm = FlagBadOnion | 1 61 CodeTemporaryNodeFailure = FlagNode | 2 62 CodePermanentNodeFailure = FlagPerm | FlagNode | 2 63 CodeRequiredNodeFeatureMissing = FlagPerm | FlagNode | 3 64 CodeInvalidOnionVersion = FlagBadOnion | FlagPerm | 4 65 CodeInvalidOnionHmac = FlagBadOnion | FlagPerm | 5 66 CodeInvalidOnionKey = FlagBadOnion | FlagPerm | 6 67 CodeTemporaryChannelFailure = FlagUpdate | 7 68 CodePermanentChannelFailure = FlagPerm | 8 69 CodeRequiredChannelFeatureMissing = FlagPerm | 9 70 CodeUnknownNextPeer = FlagPerm | 10 71 CodeAmountBelowMinimum = FlagUpdate | 11 72 CodeFeeInsufficient = FlagUpdate | 12 73 CodeIncorrectCltvExpiry = FlagUpdate | 13 74 CodeExpiryTooSoon = FlagUpdate | 14 75 CodeChannelDisabled = FlagUpdate | 20 76 CodeIncorrectOrUnknownPaymentDetails = FlagPerm | 15 77 CodeIncorrectPaymentAmount = FlagPerm | 16 78 CodeFinalExpiryTooSoon FailCode = 17 79 CodeFinalIncorrectCltvExpiry FailCode = 18 80 CodeFinalIncorrectHtlcAmount FailCode = 19 81 CodeExpiryTooFar FailCode = 21 82 CodeInvalidOnionPayload = FlagPerm | 22 83 CodeMPPTimeout FailCode = 23 84 ) 85 86 // String returns the string representation of the failure code. 87 func (c FailCode) String() string { 88 switch c { 89 case CodeInvalidRealm: 90 return "InvalidRealm" 91 92 case CodeTemporaryNodeFailure: 93 return "TemporaryNodeFailure" 94 95 case CodePermanentNodeFailure: 96 return "PermanentNodeFailure" 97 98 case CodeRequiredNodeFeatureMissing: 99 return "RequiredNodeFeatureMissing" 100 101 case CodeInvalidOnionVersion: 102 return "InvalidOnionVersion" 103 104 case CodeInvalidOnionHmac: 105 return "InvalidOnionHmac" 106 107 case CodeInvalidOnionKey: 108 return "InvalidOnionKey" 109 110 case CodeTemporaryChannelFailure: 111 return "TemporaryChannelFailure" 112 113 case CodePermanentChannelFailure: 114 return "PermanentChannelFailure" 115 116 case CodeRequiredChannelFeatureMissing: 117 return "RequiredChannelFeatureMissing" 118 119 case CodeUnknownNextPeer: 120 return "UnknownNextPeer" 121 122 case CodeAmountBelowMinimum: 123 return "AmountBelowMinimum" 124 125 case CodeFeeInsufficient: 126 return "FeeInsufficient" 127 128 case CodeIncorrectCltvExpiry: 129 return "IncorrectCltvExpiry" 130 131 case CodeIncorrectPaymentAmount: 132 return "IncorrectPaymentAmount" 133 134 case CodeExpiryTooSoon: 135 return "ExpiryTooSoon" 136 137 case CodeChannelDisabled: 138 return "ChannelDisabled" 139 140 case CodeIncorrectOrUnknownPaymentDetails: 141 return "IncorrectOrUnknownPaymentDetails" 142 143 case CodeFinalExpiryTooSoon: 144 return "FinalExpiryTooSoon" 145 146 case CodeFinalIncorrectCltvExpiry: 147 return "FinalIncorrectCltvExpiry" 148 149 case CodeFinalIncorrectHtlcAmount: 150 return "FinalIncorrectHtlcAmount" 151 152 case CodeExpiryTooFar: 153 return "ExpiryTooFar" 154 155 case CodeInvalidOnionPayload: 156 return "InvalidOnionPayload" 157 158 case CodeMPPTimeout: 159 return "MPPTimeout" 160 161 default: 162 return "<unknown>" 163 } 164 } 165 166 // FailInvalidRealm is returned if the realm byte is unknown. 167 // 168 // NOTE: May be returned by any node in the payment route. 169 type FailInvalidRealm struct{} 170 171 // Returns a human readable string describing the target FailureMessage. 172 // 173 // NOTE: Implements the error interface. 174 func (f *FailInvalidRealm) Error() string { 175 return f.Code().String() 176 } 177 178 // Code returns the failure unique code. 179 // 180 // NOTE: Part of the FailureMessage interface. 181 func (f *FailInvalidRealm) Code() FailCode { 182 return CodeInvalidRealm 183 } 184 185 // FailTemporaryNodeFailure is returned if an otherwise unspecified transient 186 // error occurs for the entire node. 187 // 188 // NOTE: May be returned by any node in the payment route. 189 type FailTemporaryNodeFailure struct{} 190 191 // Code returns the failure unique code. 192 // NOTE: Part of the FailureMessage interface. 193 func (f *FailTemporaryNodeFailure) Code() FailCode { 194 return CodeTemporaryNodeFailure 195 } 196 197 // Returns a human readable string describing the target FailureMessage. 198 // 199 // NOTE: Implements the error interface. 200 func (f *FailTemporaryNodeFailure) Error() string { 201 return f.Code().String() 202 } 203 204 // FailPermanentNodeFailure is returned if an otherwise unspecified permanent 205 // error occurs for the entire node. 206 // 207 // NOTE: May be returned by any node in the payment route. 208 type FailPermanentNodeFailure struct{} 209 210 // Code returns the failure unique code. 211 // 212 // NOTE: Part of the FailureMessage interface. 213 func (f *FailPermanentNodeFailure) Code() FailCode { 214 return CodePermanentNodeFailure 215 } 216 217 // Returns a human readable string describing the target FailureMessage. 218 // 219 // NOTE: Implements the error interface. 220 func (f *FailPermanentNodeFailure) Error() string { 221 return f.Code().String() 222 } 223 224 // FailRequiredNodeFeatureMissing is returned if a node has requirement 225 // advertised in its node_announcement features which were not present in the 226 // onion. 227 // 228 // NOTE: May be returned by any node in the payment route. 229 type FailRequiredNodeFeatureMissing struct{} 230 231 // Code returns the failure unique code. 232 // 233 // NOTE: Part of the FailureMessage interface. 234 func (f *FailRequiredNodeFeatureMissing) Code() FailCode { 235 return CodeRequiredNodeFeatureMissing 236 } 237 238 // Returns a human readable string describing the target FailureMessage. 239 // 240 // NOTE: Implements the error interface. 241 func (f *FailRequiredNodeFeatureMissing) Error() string { 242 return f.Code().String() 243 } 244 245 // FailPermanentChannelFailure is return if an otherwise unspecified permanent 246 // error occurs for the outgoing channel (eg. channel (recently). 247 // 248 // NOTE: May be returned by any node in the payment route. 249 type FailPermanentChannelFailure struct{} 250 251 // Code returns the failure unique code. 252 // 253 // NOTE: Part of the FailureMessage interface. 254 func (f *FailPermanentChannelFailure) Code() FailCode { 255 return CodePermanentChannelFailure 256 } 257 258 // Returns a human readable string describing the target FailureMessage. 259 // 260 // NOTE: Implements the error interface. 261 func (f *FailPermanentChannelFailure) Error() string { 262 return f.Code().String() 263 } 264 265 // FailRequiredChannelFeatureMissing is returned if the outgoing channel has a 266 // requirement advertised in its channel announcement features which were not 267 // present in the onion. 268 // 269 // NOTE: May only be returned by intermediate nodes. 270 type FailRequiredChannelFeatureMissing struct{} 271 272 // Code returns the failure unique code. 273 // 274 // NOTE: Part of the FailureMessage interface. 275 func (f *FailRequiredChannelFeatureMissing) Code() FailCode { 276 return CodeRequiredChannelFeatureMissing 277 } 278 279 // Returns a human readable string describing the target FailureMessage. 280 // 281 // NOTE: Implements the error interface. 282 func (f *FailRequiredChannelFeatureMissing) Error() string { 283 return f.Code().String() 284 } 285 286 // FailUnknownNextPeer is returned if the next peer specified by the onion is 287 // not known. 288 // 289 // NOTE: May only be returned by intermediate nodes. 290 type FailUnknownNextPeer struct{} 291 292 // Code returns the failure unique code. 293 // 294 // NOTE: Part of the FailureMessage interface. 295 func (f *FailUnknownNextPeer) Code() FailCode { 296 return CodeUnknownNextPeer 297 } 298 299 // Returns a human readable string describing the target FailureMessage. 300 // 301 // NOTE: Implements the error interface. 302 func (f *FailUnknownNextPeer) Error() string { 303 return f.Code().String() 304 } 305 306 // FailIncorrectPaymentAmount is returned if the amount paid is less than the 307 // amount expected, the final node MUST fail the HTLC. If the amount paid is 308 // more than twice the amount expected, the final node SHOULD fail the HTLC. 309 // This allows the sender to reduce information leakage by altering the amount, 310 // without allowing accidental gross overpayment. 311 // 312 // NOTE: May only be returned by the final node in the path. 313 type FailIncorrectPaymentAmount struct{} 314 315 // Code returns the failure unique code. 316 // 317 // NOTE: Part of the FailureMessage interface. 318 func (f *FailIncorrectPaymentAmount) Code() FailCode { 319 return CodeIncorrectPaymentAmount 320 } 321 322 // Returns a human readable string describing the target FailureMessage. 323 // 324 // NOTE: Implements the error interface. 325 func (f *FailIncorrectPaymentAmount) Error() string { 326 return f.Code().String() 327 } 328 329 // FailIncorrectDetails is returned for two reasons: 330 // 331 // 1) if the payment hash has already been paid, the final node MAY treat the 332 // payment hash as unknown, or may succeed in accepting the HTLC. If the 333 // payment hash is unknown, the final node MUST fail the HTLC. 334 // 335 // 2) if the amount paid is less than the amount expected, the final node MUST 336 // fail the HTLC. If the amount paid is more than twice the amount expected, 337 // the final node SHOULD fail the HTLC. This allows the sender to reduce 338 // information leakage by altering the amount, without allowing accidental 339 // gross overpayment. 340 // 341 // NOTE: May only be returned by the final node in the path. 342 type FailIncorrectDetails struct { 343 // amount is the value of the extended HTLC. 344 amount MilliAtom 345 346 // height is the block height when the htlc was received. 347 height uint32 348 } 349 350 // NewFailIncorrectDetails makes a new instance of the FailIncorrectDetails 351 // error bound to the specified HTLC amount and acceptance height. 352 func NewFailIncorrectDetails(amt MilliAtom, 353 height uint32) *FailIncorrectDetails { 354 355 return &FailIncorrectDetails{ 356 amount: amt, 357 height: height, 358 } 359 } 360 361 // Amount is the value of the extended HTLC. 362 func (f *FailIncorrectDetails) Amount() MilliAtom { 363 return f.amount 364 } 365 366 // Height is the block height when the htlc was received. 367 func (f *FailIncorrectDetails) Height() uint32 { 368 return f.height 369 } 370 371 // Code returns the failure unique code. 372 // 373 // NOTE: Part of the FailureMessage interface. 374 func (f *FailIncorrectDetails) Code() FailCode { 375 return CodeIncorrectOrUnknownPaymentDetails 376 } 377 378 // Returns a human readable string describing the target FailureMessage. 379 // 380 // NOTE: Implements the error interface. 381 func (f *FailIncorrectDetails) Error() string { 382 return fmt.Sprintf( 383 "%v(amt=%v, height=%v)", CodeIncorrectOrUnknownPaymentDetails, 384 f.amount, f.height, 385 ) 386 } 387 388 // Decode decodes the failure from bytes stream. 389 // 390 // NOTE: Part of the Serializable interface. 391 func (f *FailIncorrectDetails) Decode(r io.Reader, pver uint32) error { 392 err := ReadElement(r, &f.amount) 393 switch { 394 // This is an optional tack on that was added later in the protocol. As 395 // a result, older nodes may not include this value. We'll account for 396 // this by checking for io.EOF here which means that no bytes were read 397 // at all. 398 case err == io.EOF: 399 return nil 400 401 case err != nil: 402 return err 403 } 404 405 // At a later stage, the height field was also tacked on. We need to 406 // check for io.EOF here as well. 407 err = ReadElement(r, &f.height) 408 switch { 409 case err == io.EOF: 410 return nil 411 412 case err != nil: 413 return err 414 } 415 416 return nil 417 } 418 419 // Encode writes the failure in bytes stream. 420 // 421 // NOTE: Part of the Serializable interface. 422 func (f *FailIncorrectDetails) Encode(w *bytes.Buffer, pver uint32) error { 423 if err := WriteMilliAtom(w, f.amount); err != nil { 424 return err 425 } 426 427 return WriteUint32(w, f.height) 428 } 429 430 // FailFinalExpiryTooSoon is returned if the cltv_expiry is too low, the final 431 // node MUST fail the HTLC. 432 // 433 // NOTE: May only be returned by the final node in the path. 434 type FailFinalExpiryTooSoon struct{} 435 436 // Code returns the failure unique code. 437 // 438 // NOTE: Part of the FailureMessage interface. 439 func (f *FailFinalExpiryTooSoon) Code() FailCode { 440 return CodeFinalExpiryTooSoon 441 } 442 443 // Returns a human readable string describing the target FailureMessage. 444 // 445 // NOTE: Implements the error interface. 446 func (f *FailFinalExpiryTooSoon) Error() string { 447 return f.Code().String() 448 } 449 450 // NewFinalExpiryTooSoon creates new instance of the FailFinalExpiryTooSoon. 451 func NewFinalExpiryTooSoon() *FailFinalExpiryTooSoon { 452 return &FailFinalExpiryTooSoon{} 453 } 454 455 // FailInvalidOnionVersion is returned if the onion version byte is unknown. 456 // 457 // NOTE: May be returned only by intermediate nodes. 458 type FailInvalidOnionVersion struct { 459 // OnionSHA256 hash of the onion blob which haven't been proceeded. 460 OnionSHA256 [sha256.Size]byte 461 } 462 463 // Returns a human readable string describing the target FailureMessage. 464 // 465 // NOTE: Implements the error interface. 466 func (f *FailInvalidOnionVersion) Error() string { 467 return fmt.Sprintf("InvalidOnionVersion(onion_sha=%x)", f.OnionSHA256[:]) 468 } 469 470 // NewInvalidOnionVersion creates new instance of the FailInvalidOnionVersion. 471 func NewInvalidOnionVersion(onion []byte) *FailInvalidOnionVersion { 472 return &FailInvalidOnionVersion{OnionSHA256: sha256.Sum256(onion)} 473 } 474 475 // Code returns the failure unique code. 476 // 477 // NOTE: Part of the FailureMessage interface. 478 func (f *FailInvalidOnionVersion) Code() FailCode { 479 return CodeInvalidOnionVersion 480 } 481 482 // Decode decodes the failure from bytes stream. 483 // 484 // NOTE: Part of the Serializable interface. 485 func (f *FailInvalidOnionVersion) Decode(r io.Reader, pver uint32) error { 486 return ReadElement(r, f.OnionSHA256[:]) 487 } 488 489 // Encode writes the failure in bytes stream. 490 // 491 // NOTE: Part of the Serializable interface. 492 func (f *FailInvalidOnionVersion) Encode(w *bytes.Buffer, pver uint32) error { 493 return WriteBytes(w, f.OnionSHA256[:]) 494 } 495 496 // FailInvalidOnionHmac is return if the onion HMAC is incorrect. 497 // 498 // NOTE: May only be returned by intermediate nodes. 499 type FailInvalidOnionHmac struct { 500 // OnionSHA256 hash of the onion blob which haven't been proceeded. 501 OnionSHA256 [sha256.Size]byte 502 } 503 504 // NewInvalidOnionHmac creates new instance of the FailInvalidOnionHmac. 505 func NewInvalidOnionHmac(onion []byte) *FailInvalidOnionHmac { 506 return &FailInvalidOnionHmac{OnionSHA256: sha256.Sum256(onion)} 507 } 508 509 // Code returns the failure unique code. 510 // 511 // NOTE: Part of the FailureMessage interface. 512 func (f *FailInvalidOnionHmac) Code() FailCode { 513 return CodeInvalidOnionHmac 514 } 515 516 // Decode decodes the failure from bytes stream. 517 // 518 // NOTE: Part of the Serializable interface. 519 func (f *FailInvalidOnionHmac) Decode(r io.Reader, pver uint32) error { 520 return ReadElement(r, f.OnionSHA256[:]) 521 } 522 523 // Encode writes the failure in bytes stream. 524 // 525 // NOTE: Part of the Serializable interface. 526 func (f *FailInvalidOnionHmac) Encode(w *bytes.Buffer, pver uint32) error { 527 return WriteBytes(w, f.OnionSHA256[:]) 528 } 529 530 // Returns a human readable string describing the target FailureMessage. 531 // 532 // NOTE: Implements the error interface. 533 func (f *FailInvalidOnionHmac) Error() string { 534 return fmt.Sprintf("InvalidOnionHMAC(onion_sha=%x)", f.OnionSHA256[:]) 535 } 536 537 // FailInvalidOnionKey is return if the ephemeral key in the onion is 538 // unparsable. 539 // 540 // NOTE: May only be returned by intermediate nodes. 541 type FailInvalidOnionKey struct { 542 // OnionSHA256 hash of the onion blob which haven't been proceeded. 543 OnionSHA256 [sha256.Size]byte 544 } 545 546 // NewInvalidOnionKey creates new instance of the FailInvalidOnionKey. 547 func NewInvalidOnionKey(onion []byte) *FailInvalidOnionKey { 548 return &FailInvalidOnionKey{OnionSHA256: sha256.Sum256(onion)} 549 } 550 551 // Code returns the failure unique code. 552 // 553 // NOTE: Part of the FailureMessage interface. 554 func (f *FailInvalidOnionKey) Code() FailCode { 555 return CodeInvalidOnionKey 556 } 557 558 // Decode decodes the failure from bytes stream. 559 // 560 // NOTE: Part of the Serializable interface. 561 func (f *FailInvalidOnionKey) Decode(r io.Reader, pver uint32) error { 562 return ReadElement(r, f.OnionSHA256[:]) 563 } 564 565 // Encode writes the failure in bytes stream. 566 // 567 // NOTE: Part of the Serializable interface. 568 func (f *FailInvalidOnionKey) Encode(w *bytes.Buffer, pver uint32) error { 569 return WriteBytes(w, f.OnionSHA256[:]) 570 } 571 572 // Returns a human readable string describing the target FailureMessage. 573 // 574 // NOTE: Implements the error interface. 575 func (f *FailInvalidOnionKey) Error() string { 576 return fmt.Sprintf("InvalidOnionKey(onion_sha=%x)", f.OnionSHA256[:]) 577 } 578 579 // parseChannelUpdateCompatabilityMode will attempt to parse a channel updated 580 // encoded into an onion error payload in two ways. First, we'll try the 581 // compatibility oriented version wherein we'll _skip_ the length prefixing on 582 // the channel update message. Older versions of c-lighting do this so we'll 583 // attempt to parse these messages in order to retain compatibility. If we're 584 // unable to pull out a fully valid version, then we'll fall back to the 585 // regular parsing mechanism which includes the length prefix an NO type byte. 586 func parseChannelUpdateCompatabilityMode(r *bufio.Reader, 587 chanUpdate *ChannelUpdate, pver uint32) error { 588 589 // We'll peek out two bytes from the buffer without advancing the 590 // buffer so we can decide how to parse the remainder of it. 591 maybeTypeBytes, err := r.Peek(2) 592 if err != nil { 593 return err 594 } 595 596 // Some nodes well prefix an additional set of bytes in front of their 597 // channel updates. These bytes will _almost_ always be 258 or the type 598 // of the ChannelUpdate message. 599 typeInt := binary.BigEndian.Uint16(maybeTypeBytes) 600 if typeInt == MsgChannelUpdate { 601 // At this point it's likely the case that this is a channel 602 // update message with its type prefixed, so we'll snip off the 603 // first two bytes and parse it as normal. 604 var throwAwayTypeBytes [2]byte 605 _, err := r.Read(throwAwayTypeBytes[:]) 606 if err != nil { 607 return err 608 } 609 } 610 611 // At this pint, we've either decided to keep the entire thing, or snip 612 // off the first two bytes. In either case, we can just read it as 613 // normal. 614 return chanUpdate.Decode(r, pver) 615 } 616 617 // FailTemporaryChannelFailure is if an otherwise unspecified transient error 618 // occurs for the outgoing channel (eg. channel capacity reached, too many 619 // in-flight htlcs) 620 // 621 // NOTE: May only be returned by intermediate nodes. 622 type FailTemporaryChannelFailure struct { 623 // Update is used to update information about state of the channel 624 // which caused the failure. 625 // 626 // NOTE: This field is optional. 627 Update *ChannelUpdate 628 } 629 630 // NewTemporaryChannelFailure creates new instance of the FailTemporaryChannelFailure. 631 func NewTemporaryChannelFailure(update *ChannelUpdate) *FailTemporaryChannelFailure { 632 return &FailTemporaryChannelFailure{Update: update} 633 } 634 635 // Code returns the failure unique code. 636 // 637 // NOTE: Part of the FailureMessage interface. 638 func (f *FailTemporaryChannelFailure) Code() FailCode { 639 return CodeTemporaryChannelFailure 640 } 641 642 // Returns a human readable string describing the target FailureMessage. 643 // 644 // NOTE: Implements the error interface. 645 func (f *FailTemporaryChannelFailure) Error() string { 646 if f.Update == nil { 647 return f.Code().String() 648 } 649 650 return fmt.Sprintf("TemporaryChannelFailure(update=%v)", 651 spew.Sdump(f.Update)) 652 } 653 654 // Decode decodes the failure from bytes stream. 655 // 656 // NOTE: Part of the Serializable interface. 657 func (f *FailTemporaryChannelFailure) Decode(r io.Reader, pver uint32) error { 658 var length uint16 659 err := ReadElement(r, &length) 660 if err != nil { 661 return err 662 } 663 664 if length != 0 { 665 f.Update = &ChannelUpdate{} 666 return parseChannelUpdateCompatabilityMode( 667 bufio.NewReader(r), f.Update, pver, 668 ) 669 } 670 671 return nil 672 } 673 674 // Encode writes the failure in bytes stream. 675 // 676 // NOTE: Part of the Serializable interface. 677 func (f *FailTemporaryChannelFailure) Encode(w *bytes.Buffer, 678 pver uint32) error { 679 680 var payload []byte 681 if f.Update != nil { 682 var bw bytes.Buffer 683 if err := f.Update.Encode(&bw, pver); err != nil { 684 return err 685 } 686 payload = bw.Bytes() 687 } 688 689 if err := WriteUint16(w, uint16(len(payload))); err != nil { 690 return err 691 } 692 693 _, err := w.Write(payload) 694 return err 695 } 696 697 // FailAmountBelowMinimum is returned if the HTLC does not reach the current 698 // minimum amount, we tell them the amount of the incoming HTLC and the current 699 // channel setting for the outgoing channel. 700 // 701 // NOTE: May only be returned by the intermediate nodes in the path. 702 type FailAmountBelowMinimum struct { 703 // HtlcMAtoms is the wrong amount of the incoming HTLC. 704 HtlcMAtoms MilliAtom 705 706 // Update is used to update information about state of the channel 707 // which caused the failure. 708 Update ChannelUpdate 709 } 710 711 // NewAmountBelowMinimum creates new instance of the FailAmountBelowMinimum. 712 func NewAmountBelowMinimum(htlcMAtoms MilliAtom, 713 update ChannelUpdate) *FailAmountBelowMinimum { 714 715 return &FailAmountBelowMinimum{ 716 HtlcMAtoms: htlcMAtoms, 717 Update: update, 718 } 719 } 720 721 // Code returns the failure unique code. 722 // 723 // NOTE: Part of the FailureMessage interface. 724 func (f *FailAmountBelowMinimum) Code() FailCode { 725 return CodeAmountBelowMinimum 726 } 727 728 // Returns a human readable string describing the target FailureMessage. 729 // 730 // NOTE: Implements the error interface. 731 func (f *FailAmountBelowMinimum) Error() string { 732 return fmt.Sprintf("AmountBelowMinimum(amt=%v, update=%v", f.HtlcMAtoms, 733 spew.Sdump(f.Update)) 734 } 735 736 // Decode decodes the failure from bytes stream. 737 // 738 // NOTE: Part of the Serializable interface. 739 func (f *FailAmountBelowMinimum) Decode(r io.Reader, pver uint32) error { 740 if err := ReadElement(r, &f.HtlcMAtoms); err != nil { 741 return err 742 } 743 744 var length uint16 745 if err := ReadElement(r, &length); err != nil { 746 return err 747 } 748 749 f.Update = ChannelUpdate{} 750 return parseChannelUpdateCompatabilityMode( 751 bufio.NewReader(r), &f.Update, pver, 752 ) 753 } 754 755 // Encode writes the failure in bytes stream. 756 // 757 // NOTE: Part of the Serializable interface. 758 func (f *FailAmountBelowMinimum) Encode(w *bytes.Buffer, pver uint32) error { 759 if err := WriteMilliAtom(w, f.HtlcMAtoms); err != nil { 760 return err 761 } 762 763 return writeOnionErrorChanUpdate(w, &f.Update, pver) 764 } 765 766 // FailFeeInsufficient is returned if the HTLC does not pay sufficient fee, we 767 // tell them the amount of the incoming HTLC and the current channel setting 768 // for the outgoing channel. 769 // 770 // NOTE: May only be returned by intermediate nodes. 771 type FailFeeInsufficient struct { 772 // HtlcMAtoms is the wrong amount of the incoming HTLC. 773 HtlcMAtoms MilliAtom 774 775 // Update is used to update information about state of the channel 776 // which caused the failure. 777 Update ChannelUpdate 778 } 779 780 // NewFeeInsufficient creates new instance of the FailFeeInsufficient. 781 func NewFeeInsufficient(htlcMAtoms MilliAtom, 782 update ChannelUpdate) *FailFeeInsufficient { 783 return &FailFeeInsufficient{ 784 HtlcMAtoms: htlcMAtoms, 785 Update: update, 786 } 787 } 788 789 // Code returns the failure unique code. 790 // 791 // NOTE: Part of the FailureMessage interface. 792 func (f *FailFeeInsufficient) Code() FailCode { 793 return CodeFeeInsufficient 794 } 795 796 // Returns a human readable string describing the target FailureMessage. 797 // 798 // NOTE: Implements the error interface. 799 func (f *FailFeeInsufficient) Error() string { 800 return fmt.Sprintf("FeeInsufficient(htlc_amt==%v, update=%v", f.HtlcMAtoms, 801 spew.Sdump(f.Update)) 802 } 803 804 // Decode decodes the failure from bytes stream. 805 // 806 // NOTE: Part of the Serializable interface. 807 func (f *FailFeeInsufficient) Decode(r io.Reader, pver uint32) error { 808 if err := ReadElement(r, &f.HtlcMAtoms); err != nil { 809 return err 810 } 811 812 var length uint16 813 if err := ReadElement(r, &length); err != nil { 814 return err 815 } 816 817 f.Update = ChannelUpdate{} 818 return parseChannelUpdateCompatabilityMode( 819 bufio.NewReader(r), &f.Update, pver, 820 ) 821 } 822 823 // Encode writes the failure in bytes stream. 824 // 825 // NOTE: Part of the Serializable interface. 826 func (f *FailFeeInsufficient) Encode(w *bytes.Buffer, pver uint32) error { 827 if err := WriteMilliAtom(w, f.HtlcMAtoms); err != nil { 828 return err 829 } 830 831 return writeOnionErrorChanUpdate(w, &f.Update, pver) 832 } 833 834 // FailIncorrectCltvExpiry is returned if outgoing cltv value does not match 835 // the update add htlc's cltv expiry minus cltv expiry delta for the outgoing 836 // channel, we tell them the cltv expiry and the current channel setting for 837 // the outgoing channel. 838 // 839 // NOTE: May only be returned by intermediate nodes. 840 type FailIncorrectCltvExpiry struct { 841 // CltvExpiry is the wrong absolute timeout in blocks, after which 842 // outgoing HTLC expires. 843 CltvExpiry uint32 844 845 // Update is used to update information about state of the channel 846 // which caused the failure. 847 Update ChannelUpdate 848 } 849 850 // NewIncorrectCltvExpiry creates new instance of the FailIncorrectCltvExpiry. 851 func NewIncorrectCltvExpiry(cltvExpiry uint32, 852 update ChannelUpdate) *FailIncorrectCltvExpiry { 853 854 return &FailIncorrectCltvExpiry{ 855 CltvExpiry: cltvExpiry, 856 Update: update, 857 } 858 } 859 860 // Code returns the failure unique code. 861 // 862 // NOTE: Part of the FailureMessage interface. 863 func (f *FailIncorrectCltvExpiry) Code() FailCode { 864 return CodeIncorrectCltvExpiry 865 } 866 867 func (f *FailIncorrectCltvExpiry) Error() string { 868 return fmt.Sprintf("IncorrectCltvExpiry(expiry=%v, update=%v", 869 f.CltvExpiry, spew.Sdump(f.Update)) 870 } 871 872 // Decode decodes the failure from bytes stream. 873 // 874 // NOTE: Part of the Serializable interface. 875 func (f *FailIncorrectCltvExpiry) Decode(r io.Reader, pver uint32) error { 876 if err := ReadElement(r, &f.CltvExpiry); err != nil { 877 return err 878 } 879 880 var length uint16 881 if err := ReadElement(r, &length); err != nil { 882 return err 883 } 884 885 f.Update = ChannelUpdate{} 886 return parseChannelUpdateCompatabilityMode( 887 bufio.NewReader(r), &f.Update, pver, 888 ) 889 } 890 891 // Encode writes the failure in bytes stream. 892 // 893 // NOTE: Part of the Serializable interface. 894 func (f *FailIncorrectCltvExpiry) Encode(w *bytes.Buffer, pver uint32) error { 895 if err := WriteUint32(w, f.CltvExpiry); err != nil { 896 return err 897 } 898 899 return writeOnionErrorChanUpdate(w, &f.Update, pver) 900 } 901 902 // FailExpiryTooSoon is returned if the ctlv-expiry is too near, we tell them 903 // the current channel setting for the outgoing channel. 904 // 905 // NOTE: May only be returned by intermediate nodes. 906 type FailExpiryTooSoon struct { 907 // Update is used to update information about state of the channel 908 // which caused the failure. 909 Update ChannelUpdate 910 } 911 912 // NewExpiryTooSoon creates new instance of the FailExpiryTooSoon. 913 func NewExpiryTooSoon(update ChannelUpdate) *FailExpiryTooSoon { 914 return &FailExpiryTooSoon{ 915 Update: update, 916 } 917 } 918 919 // Code returns the failure unique code. 920 // 921 // NOTE: Part of the FailureMessage interface. 922 func (f *FailExpiryTooSoon) Code() FailCode { 923 return CodeExpiryTooSoon 924 } 925 926 // Returns a human readable string describing the target FailureMessage. 927 // 928 // NOTE: Implements the error interface. 929 func (f *FailExpiryTooSoon) Error() string { 930 return fmt.Sprintf("ExpiryTooSoon(update=%v", spew.Sdump(f.Update)) 931 } 932 933 // Decode decodes the failure from l stream. 934 // 935 // NOTE: Part of the Serializable interface. 936 func (f *FailExpiryTooSoon) Decode(r io.Reader, pver uint32) error { 937 var length uint16 938 if err := ReadElement(r, &length); err != nil { 939 return err 940 } 941 942 f.Update = ChannelUpdate{} 943 return parseChannelUpdateCompatabilityMode( 944 bufio.NewReader(r), &f.Update, pver, 945 ) 946 } 947 948 // Encode writes the failure in bytes stream. 949 // 950 // NOTE: Part of the Serializable interface. 951 func (f *FailExpiryTooSoon) Encode(w *bytes.Buffer, pver uint32) error { 952 return writeOnionErrorChanUpdate(w, &f.Update, pver) 953 } 954 955 // FailChannelDisabled is returned if the channel is disabled, we tell them the 956 // current channel setting for the outgoing channel. 957 // 958 // NOTE: May only be returned by intermediate nodes. 959 type FailChannelDisabled struct { 960 // Flags least-significant bit must be set to 0 if the creating node 961 // corresponds to the first node in the previously sent channel 962 // announcement and 1 otherwise. 963 Flags uint16 964 965 // Update is used to update information about state of the channel 966 // which caused the failure. 967 Update ChannelUpdate 968 } 969 970 // NewChannelDisabled creates new instance of the FailChannelDisabled. 971 func NewChannelDisabled(flags uint16, update ChannelUpdate) *FailChannelDisabled { 972 return &FailChannelDisabled{ 973 Flags: flags, 974 Update: update, 975 } 976 } 977 978 // Code returns the failure unique code. 979 // 980 // NOTE: Part of the FailureMessage interface. 981 func (f *FailChannelDisabled) Code() FailCode { 982 return CodeChannelDisabled 983 } 984 985 // Returns a human readable string describing the target FailureMessage. 986 // 987 // NOTE: Implements the error interface. 988 func (f *FailChannelDisabled) Error() string { 989 return fmt.Sprintf("ChannelDisabled(flags=%v, update=%v", f.Flags, 990 spew.Sdump(f.Update)) 991 } 992 993 // Decode decodes the failure from bytes stream. 994 // 995 // NOTE: Part of the Serializable interface. 996 func (f *FailChannelDisabled) Decode(r io.Reader, pver uint32) error { 997 if err := ReadElement(r, &f.Flags); err != nil { 998 return err 999 } 1000 1001 var length uint16 1002 if err := ReadElement(r, &length); err != nil { 1003 return err 1004 } 1005 1006 f.Update = ChannelUpdate{} 1007 return parseChannelUpdateCompatabilityMode( 1008 bufio.NewReader(r), &f.Update, pver, 1009 ) 1010 } 1011 1012 // Encode writes the failure in bytes stream. 1013 // 1014 // NOTE: Part of the Serializable interface. 1015 func (f *FailChannelDisabled) Encode(w *bytes.Buffer, pver uint32) error { 1016 if err := WriteUint16(w, f.Flags); err != nil { 1017 return err 1018 } 1019 1020 return writeOnionErrorChanUpdate(w, &f.Update, pver) 1021 } 1022 1023 // FailFinalIncorrectCltvExpiry is returned if the outgoing_cltv_value does not 1024 // match the ctlv_expiry of the HTLC at the final hop. 1025 // 1026 // NOTE: might be returned by final node only. 1027 type FailFinalIncorrectCltvExpiry struct { 1028 // CltvExpiry is the wrong absolute timeout in blocks, after which 1029 // outgoing HTLC expires. 1030 CltvExpiry uint32 1031 } 1032 1033 // Returns a human readable string describing the target FailureMessage. 1034 // 1035 // NOTE: Implements the error interface. 1036 func (f *FailFinalIncorrectCltvExpiry) Error() string { 1037 return fmt.Sprintf("FinalIncorrectCltvExpiry(expiry=%v)", f.CltvExpiry) 1038 } 1039 1040 // NewFinalIncorrectCltvExpiry creates new instance of the 1041 // FailFinalIncorrectCltvExpiry. 1042 func NewFinalIncorrectCltvExpiry(cltvExpiry uint32) *FailFinalIncorrectCltvExpiry { 1043 return &FailFinalIncorrectCltvExpiry{ 1044 CltvExpiry: cltvExpiry, 1045 } 1046 } 1047 1048 // Code returns the failure unique code. 1049 // 1050 // NOTE: Part of the FailureMessage interface. 1051 func (f *FailFinalIncorrectCltvExpiry) Code() FailCode { 1052 return CodeFinalIncorrectCltvExpiry 1053 } 1054 1055 // Decode decodes the failure from bytes stream. 1056 // 1057 // NOTE: Part of the Serializable interface. 1058 func (f *FailFinalIncorrectCltvExpiry) Decode(r io.Reader, pver uint32) error { 1059 return ReadElement(r, &f.CltvExpiry) 1060 } 1061 1062 // Encode writes the failure in bytes stream. 1063 // 1064 // NOTE: Part of the Serializable interface. 1065 func (f *FailFinalIncorrectCltvExpiry) Encode(w *bytes.Buffer, 1066 pver uint32) error { 1067 1068 return WriteUint32(w, f.CltvExpiry) 1069 } 1070 1071 // FailFinalIncorrectHtlcAmount is returned if the amt_to_forward is higher 1072 // than incoming_htlc_amt of the HTLC at the final hop. 1073 // 1074 // NOTE: May only be returned by the final node. 1075 type FailFinalIncorrectHtlcAmount struct { 1076 // IncomingHTLCAmount is the wrong forwarded htlc amount. 1077 IncomingHTLCAmount MilliAtom 1078 } 1079 1080 // Returns a human readable string describing the target FailureMessage. 1081 // 1082 // NOTE: Implements the error interface. 1083 func (f *FailFinalIncorrectHtlcAmount) Error() string { 1084 return fmt.Sprintf("FinalIncorrectHtlcAmount(amt=%v)", 1085 f.IncomingHTLCAmount) 1086 } 1087 1088 // NewFinalIncorrectHtlcAmount creates new instance of the 1089 // FailFinalIncorrectHtlcAmount. 1090 func NewFinalIncorrectHtlcAmount(amount MilliAtom) *FailFinalIncorrectHtlcAmount { 1091 return &FailFinalIncorrectHtlcAmount{ 1092 IncomingHTLCAmount: amount, 1093 } 1094 } 1095 1096 // Code returns the failure unique code. 1097 // 1098 // NOTE: Part of the FailureMessage interface. 1099 func (f *FailFinalIncorrectHtlcAmount) Code() FailCode { 1100 return CodeFinalIncorrectHtlcAmount 1101 } 1102 1103 // Decode decodes the failure from bytes stream. 1104 // 1105 // NOTE: Part of the Serializable interface. 1106 func (f *FailFinalIncorrectHtlcAmount) Decode(r io.Reader, pver uint32) error { 1107 return ReadElement(r, &f.IncomingHTLCAmount) 1108 } 1109 1110 // Encode writes the failure in bytes stream. 1111 // 1112 // NOTE: Part of the Serializable interface. 1113 func (f *FailFinalIncorrectHtlcAmount) Encode(w *bytes.Buffer, 1114 pver uint32) error { 1115 1116 return WriteMilliAtom(w, f.IncomingHTLCAmount) 1117 } 1118 1119 // FailExpiryTooFar is returned if the CLTV expiry in the HTLC is too far in the 1120 // future. 1121 // 1122 // NOTE: May be returned by any node in the payment route. 1123 type FailExpiryTooFar struct{} 1124 1125 // Code returns the failure unique code. 1126 // 1127 // NOTE: Part of the FailureMessage interface. 1128 func (f *FailExpiryTooFar) Code() FailCode { 1129 return CodeExpiryTooFar 1130 } 1131 1132 // Returns a human readable string describing the target FailureMessage. 1133 // 1134 // NOTE: Implements the error interface. 1135 func (f *FailExpiryTooFar) Error() string { 1136 return f.Code().String() 1137 } 1138 1139 // InvalidOnionPayload is returned if the hop could not process the TLV payload 1140 // enclosed in the onion. 1141 type InvalidOnionPayload struct { 1142 // Type is the TLV type that caused the specific failure. 1143 Type uint64 1144 1145 // Offset is the byte offset within the payload where the failure 1146 // occurred. 1147 Offset uint16 1148 } 1149 1150 // NewInvalidOnionPayload initializes a new InvalidOnionPayload failure. 1151 func NewInvalidOnionPayload(typ uint64, offset uint16) *InvalidOnionPayload { 1152 return &InvalidOnionPayload{ 1153 Type: typ, 1154 Offset: offset, 1155 } 1156 } 1157 1158 // Code returns the failure unique code. 1159 // 1160 // NOTE: Part of the FailureMessage interface. 1161 func (f *InvalidOnionPayload) Code() FailCode { 1162 return CodeInvalidOnionPayload 1163 } 1164 1165 // Returns a human readable string describing the target FailureMessage. 1166 // 1167 // NOTE: Implements the error interface. 1168 func (f *InvalidOnionPayload) Error() string { 1169 return fmt.Sprintf("%v(type=%v, offset=%d)", 1170 f.Code(), f.Type, f.Offset) 1171 } 1172 1173 // Decode decodes the failure from bytes stream. 1174 // 1175 // NOTE: Part of the Serializable interface. 1176 func (f *InvalidOnionPayload) Decode(r io.Reader, pver uint32) error { 1177 var buf [8]byte 1178 typ, err := tlv.ReadVarInt(r, &buf) 1179 if err != nil { 1180 return err 1181 } 1182 f.Type = typ 1183 1184 return ReadElements(r, &f.Offset) 1185 } 1186 1187 // Encode writes the failure in bytes stream. 1188 // 1189 // NOTE: Part of the Serializable interface. 1190 func (f *InvalidOnionPayload) Encode(w *bytes.Buffer, pver uint32) error { 1191 var buf [8]byte 1192 if err := tlv.WriteVarInt(w, f.Type, &buf); err != nil { 1193 return err 1194 } 1195 1196 return WriteUint16(w, f.Offset) 1197 } 1198 1199 // FailMPPTimeout is returned if the complete amount for a multi part payment 1200 // was not received within a reasonable time. 1201 // 1202 // NOTE: May only be returned by the final node in the path. 1203 type FailMPPTimeout struct{} 1204 1205 // Code returns the failure unique code. 1206 // 1207 // NOTE: Part of the FailureMessage interface. 1208 func (f *FailMPPTimeout) Code() FailCode { 1209 return CodeMPPTimeout 1210 } 1211 1212 // Returns a human readable string describing the target FailureMessage. 1213 // 1214 // NOTE: Implements the error interface. 1215 func (f *FailMPPTimeout) Error() string { 1216 return f.Code().String() 1217 } 1218 1219 // DecodeFailure decodes, validates, and parses the lnwire onion failure, for 1220 // the provided protocol version. 1221 func DecodeFailure(r io.Reader, pver uint32) (FailureMessage, error) { 1222 // First, we'll parse out the encapsulated failure message itself. This 1223 // is a 2 byte length followed by the payload itself. 1224 var failureLength uint16 1225 if err := ReadElement(r, &failureLength); err != nil { 1226 return nil, fmt.Errorf("unable to read error len: %v", err) 1227 } 1228 if failureLength > FailureMessageLength { 1229 return nil, fmt.Errorf("failure message is too "+ 1230 "long: %v", failureLength) 1231 } 1232 failureData := make([]byte, failureLength) 1233 if _, err := io.ReadFull(r, failureData); err != nil { 1234 return nil, fmt.Errorf("unable to full read payload of "+ 1235 "%v: %v", failureLength, err) 1236 } 1237 1238 dataReader := bytes.NewReader(failureData) 1239 1240 return DecodeFailureMessage(dataReader, pver) 1241 } 1242 1243 // DecodeFailureMessage decodes just the failure message, ignoring any padding 1244 // that may be present at the end. 1245 func DecodeFailureMessage(r io.Reader, pver uint32) (FailureMessage, error) { 1246 // Once we have the failure data, we can obtain the failure code from 1247 // the first two bytes of the buffer. 1248 var codeBytes [2]byte 1249 if _, err := io.ReadFull(r, codeBytes[:]); err != nil { 1250 return nil, fmt.Errorf("unable to read failure code: %v", err) 1251 } 1252 failCode := FailCode(binary.BigEndian.Uint16(codeBytes[:])) 1253 1254 // Create the empty failure by given code and populate the failure with 1255 // additional data if needed. 1256 failure, err := makeEmptyOnionError(failCode) 1257 if err != nil { 1258 return nil, fmt.Errorf("unable to make empty error: %v", err) 1259 } 1260 1261 // Finally, if this failure has a payload, then we'll read that now as 1262 // well. 1263 switch f := failure.(type) { 1264 case Serializable: 1265 if err := f.Decode(r, pver); err != nil { 1266 return nil, fmt.Errorf("unable to decode error "+ 1267 "update (type=%T): %v", failure, err) 1268 } 1269 } 1270 1271 return failure, nil 1272 } 1273 1274 // EncodeFailure encodes, including the necessary onion failure header 1275 // information. 1276 func EncodeFailure(w *bytes.Buffer, failure FailureMessage, pver uint32) error { 1277 var failureMessageBuffer bytes.Buffer 1278 1279 err := EncodeFailureMessage(&failureMessageBuffer, failure, pver) 1280 if err != nil { 1281 return err 1282 } 1283 1284 // The combined size of this message must be below the max allowed 1285 // failure message length. 1286 failureMessage := failureMessageBuffer.Bytes() 1287 if len(failureMessage) > FailureMessageLength { 1288 return fmt.Errorf("failure message exceed max "+ 1289 "available size: %v", len(failureMessage)) 1290 } 1291 1292 // Finally, we'll add some padding in order to ensure that all failure 1293 // messages are fixed size. 1294 pad := make([]byte, FailureMessageLength-len(failureMessage)) 1295 1296 if err := WriteUint16(w, uint16(len(failureMessage))); err != nil { 1297 return err 1298 } 1299 1300 if err := WriteBytes(w, failureMessage); err != nil { 1301 return err 1302 } 1303 if err := WriteUint16(w, uint16(len(pad))); err != nil { 1304 return err 1305 } 1306 1307 return WriteBytes(w, pad) 1308 } 1309 1310 // EncodeFailureMessage encodes just the failure message without adding a length 1311 // and padding the message for the onion protocol. 1312 func EncodeFailureMessage(w *bytes.Buffer, 1313 failure FailureMessage, pver uint32) error { 1314 1315 // First, we'll write out the error code itself into the failure 1316 // buffer. 1317 var codeBytes [2]byte 1318 code := uint16(failure.Code()) 1319 binary.BigEndian.PutUint16(codeBytes[:], code) 1320 _, err := w.Write(codeBytes[:]) 1321 if err != nil { 1322 return err 1323 } 1324 1325 // Next, some message have an additional message payload, if this is 1326 // one of those types, then we'll also encode the error payload as 1327 // well. 1328 switch failure := failure.(type) { 1329 case Serializable: 1330 if err := failure.Encode(w, pver); err != nil { 1331 return err 1332 } 1333 } 1334 1335 return nil 1336 } 1337 1338 // makeEmptyOnionError creates a new empty onion error of the proper concrete 1339 // type based on the passed failure code. 1340 func makeEmptyOnionError(code FailCode) (FailureMessage, error) { 1341 switch code { 1342 case CodeInvalidRealm: 1343 return &FailInvalidRealm{}, nil 1344 1345 case CodeTemporaryNodeFailure: 1346 return &FailTemporaryNodeFailure{}, nil 1347 1348 case CodePermanentNodeFailure: 1349 return &FailPermanentNodeFailure{}, nil 1350 1351 case CodeRequiredNodeFeatureMissing: 1352 return &FailRequiredNodeFeatureMissing{}, nil 1353 1354 case CodePermanentChannelFailure: 1355 return &FailPermanentChannelFailure{}, nil 1356 1357 case CodeRequiredChannelFeatureMissing: 1358 return &FailRequiredChannelFeatureMissing{}, nil 1359 1360 case CodeUnknownNextPeer: 1361 return &FailUnknownNextPeer{}, nil 1362 1363 case CodeIncorrectOrUnknownPaymentDetails: 1364 return &FailIncorrectDetails{}, nil 1365 1366 case CodeIncorrectPaymentAmount: 1367 return &FailIncorrectPaymentAmount{}, nil 1368 1369 case CodeFinalExpiryTooSoon: 1370 return &FailFinalExpiryTooSoon{}, nil 1371 1372 case CodeInvalidOnionVersion: 1373 return &FailInvalidOnionVersion{}, nil 1374 1375 case CodeInvalidOnionHmac: 1376 return &FailInvalidOnionHmac{}, nil 1377 1378 case CodeInvalidOnionKey: 1379 return &FailInvalidOnionKey{}, nil 1380 1381 case CodeTemporaryChannelFailure: 1382 return &FailTemporaryChannelFailure{}, nil 1383 1384 case CodeAmountBelowMinimum: 1385 return &FailAmountBelowMinimum{}, nil 1386 1387 case CodeFeeInsufficient: 1388 return &FailFeeInsufficient{}, nil 1389 1390 case CodeIncorrectCltvExpiry: 1391 return &FailIncorrectCltvExpiry{}, nil 1392 1393 case CodeExpiryTooSoon: 1394 return &FailExpiryTooSoon{}, nil 1395 1396 case CodeChannelDisabled: 1397 return &FailChannelDisabled{}, nil 1398 1399 case CodeFinalIncorrectCltvExpiry: 1400 return &FailFinalIncorrectCltvExpiry{}, nil 1401 1402 case CodeFinalIncorrectHtlcAmount: 1403 return &FailFinalIncorrectHtlcAmount{}, nil 1404 1405 case CodeExpiryTooFar: 1406 return &FailExpiryTooFar{}, nil 1407 1408 case CodeInvalidOnionPayload: 1409 return &InvalidOnionPayload{}, nil 1410 1411 case CodeMPPTimeout: 1412 return &FailMPPTimeout{}, nil 1413 1414 default: 1415 return nil, errors.Errorf("unknown error code: %v", code) 1416 } 1417 } 1418 1419 // writeOnionErrorChanUpdate writes out a ChannelUpdate using the onion error 1420 // format. The format is that we first write out the true serialized length of 1421 // the channel update, followed by the serialized channel update itself. 1422 func writeOnionErrorChanUpdate(w *bytes.Buffer, chanUpdate *ChannelUpdate, 1423 pver uint32) error { 1424 1425 // First, we encode the channel update in a temporary buffer in order 1426 // to get the exact serialized size. 1427 var b bytes.Buffer 1428 if err := chanUpdate.Encode(&b, pver); err != nil { 1429 return err 1430 } 1431 1432 // Now that we know the size, we can write the length out in the main 1433 // writer. 1434 updateLen := b.Len() 1435 if err := WriteUint16(w, uint16(updateLen)); err != nil { 1436 return err 1437 } 1438 1439 // With the length written, we'll then write out the serialized channel 1440 // update. 1441 if _, err := w.Write(b.Bytes()); err != nil { 1442 return err 1443 } 1444 1445 return nil 1446 }