github.com/decred/dcrlnd@v0.7.6/channeldb/migration/lnwire21/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 io.Writer, pver uint32) error { 423 return WriteElements(w, f.amount, f.height) 424 } 425 426 // FailFinalExpiryTooSoon is returned if the cltv_expiry is too low, the final 427 // node MUST fail the HTLC. 428 // 429 // NOTE: May only be returned by the final node in the path. 430 type FailFinalExpiryTooSoon struct{} 431 432 // Code returns the failure unique code. 433 // 434 // NOTE: Part of the FailureMessage interface. 435 func (f *FailFinalExpiryTooSoon) Code() FailCode { 436 return CodeFinalExpiryTooSoon 437 } 438 439 // Returns a human readable string describing the target FailureMessage. 440 // 441 // NOTE: Implements the error interface. 442 func (f *FailFinalExpiryTooSoon) Error() string { 443 return f.Code().String() 444 } 445 446 // NewFinalExpiryTooSoon creates new instance of the FailFinalExpiryTooSoon. 447 func NewFinalExpiryTooSoon() *FailFinalExpiryTooSoon { 448 return &FailFinalExpiryTooSoon{} 449 } 450 451 // FailInvalidOnionVersion is returned if the onion version byte is unknown. 452 // 453 // NOTE: May be returned only by intermediate nodes. 454 type FailInvalidOnionVersion struct { 455 // OnionSHA256 hash of the onion blob which haven't been proceeded. 456 OnionSHA256 [sha256.Size]byte 457 } 458 459 // Returns a human readable string describing the target FailureMessage. 460 // 461 // NOTE: Implements the error interface. 462 func (f *FailInvalidOnionVersion) Error() string { 463 return fmt.Sprintf("InvalidOnionVersion(onion_sha=%x)", f.OnionSHA256[:]) 464 } 465 466 // NewInvalidOnionVersion creates new instance of the FailInvalidOnionVersion. 467 func NewInvalidOnionVersion(onion []byte) *FailInvalidOnionVersion { 468 return &FailInvalidOnionVersion{OnionSHA256: sha256.Sum256(onion)} 469 } 470 471 // Code returns the failure unique code. 472 // 473 // NOTE: Part of the FailureMessage interface. 474 func (f *FailInvalidOnionVersion) Code() FailCode { 475 return CodeInvalidOnionVersion 476 } 477 478 // Decode decodes the failure from bytes stream. 479 // 480 // NOTE: Part of the Serializable interface. 481 func (f *FailInvalidOnionVersion) Decode(r io.Reader, pver uint32) error { 482 return ReadElement(r, f.OnionSHA256[:]) 483 } 484 485 // Encode writes the failure in bytes stream. 486 // 487 // NOTE: Part of the Serializable interface. 488 func (f *FailInvalidOnionVersion) Encode(w io.Writer, pver uint32) error { 489 return WriteElement(w, f.OnionSHA256[:]) 490 } 491 492 // FailInvalidOnionHmac is return if the onion HMAC is incorrect. 493 // 494 // NOTE: May only be returned by intermediate nodes. 495 type FailInvalidOnionHmac struct { 496 // OnionSHA256 hash of the onion blob which haven't been proceeded. 497 OnionSHA256 [sha256.Size]byte 498 } 499 500 // NewInvalidOnionHmac creates new instance of the FailInvalidOnionHmac. 501 func NewInvalidOnionHmac(onion []byte) *FailInvalidOnionHmac { 502 return &FailInvalidOnionHmac{OnionSHA256: sha256.Sum256(onion)} 503 } 504 505 // Code returns the failure unique code. 506 // 507 // NOTE: Part of the FailureMessage interface. 508 func (f *FailInvalidOnionHmac) Code() FailCode { 509 return CodeInvalidOnionHmac 510 } 511 512 // Decode decodes the failure from bytes stream. 513 // 514 // NOTE: Part of the Serializable interface. 515 func (f *FailInvalidOnionHmac) Decode(r io.Reader, pver uint32) error { 516 return ReadElement(r, f.OnionSHA256[:]) 517 } 518 519 // Encode writes the failure in bytes stream. 520 // 521 // NOTE: Part of the Serializable interface. 522 func (f *FailInvalidOnionHmac) Encode(w io.Writer, pver uint32) error { 523 return WriteElement(w, f.OnionSHA256[:]) 524 } 525 526 // Returns a human readable string describing the target FailureMessage. 527 // 528 // NOTE: Implements the error interface. 529 func (f *FailInvalidOnionHmac) Error() string { 530 return fmt.Sprintf("InvalidOnionHMAC(onion_sha=%x)", f.OnionSHA256[:]) 531 } 532 533 // FailInvalidOnionKey is return if the ephemeral key in the onion is 534 // unparsable. 535 // 536 // NOTE: May only be returned by intermediate nodes. 537 type FailInvalidOnionKey struct { 538 // OnionSHA256 hash of the onion blob which haven't been proceeded. 539 OnionSHA256 [sha256.Size]byte 540 } 541 542 // NewInvalidOnionKey creates new instance of the FailInvalidOnionKey. 543 func NewInvalidOnionKey(onion []byte) *FailInvalidOnionKey { 544 return &FailInvalidOnionKey{OnionSHA256: sha256.Sum256(onion)} 545 } 546 547 // Code returns the failure unique code. 548 // 549 // NOTE: Part of the FailureMessage interface. 550 func (f *FailInvalidOnionKey) Code() FailCode { 551 return CodeInvalidOnionKey 552 } 553 554 // Decode decodes the failure from bytes stream. 555 // 556 // NOTE: Part of the Serializable interface. 557 func (f *FailInvalidOnionKey) Decode(r io.Reader, pver uint32) error { 558 return ReadElement(r, f.OnionSHA256[:]) 559 } 560 561 // Encode writes the failure in bytes stream. 562 // 563 // NOTE: Part of the Serializable interface. 564 func (f *FailInvalidOnionKey) Encode(w io.Writer, pver uint32) error { 565 return WriteElement(w, f.OnionSHA256[:]) 566 } 567 568 // Returns a human readable string describing the target FailureMessage. 569 // 570 // NOTE: Implements the error interface. 571 func (f *FailInvalidOnionKey) Error() string { 572 return fmt.Sprintf("InvalidOnionKey(onion_sha=%x)", f.OnionSHA256[:]) 573 } 574 575 // parseChannelUpdateCompatabilityMode will attempt to parse a channel updated 576 // encoded into an onion error payload in two ways. First, we'll try the 577 // compatibility oriented version wherein we'll _skip_ the length prefixing on 578 // the channel update message. Older versions of c-lighting do this so we'll 579 // attempt to parse these messages in order to retain compatibility. If we're 580 // unable to pull out a fully valid version, then we'll fall back to the 581 // regular parsing mechanism which includes the length prefix an NO type byte. 582 func parseChannelUpdateCompatabilityMode(r *bufio.Reader, 583 chanUpdate *ChannelUpdate, pver uint32) error { 584 585 // We'll peek out two bytes from the buffer without advancing the 586 // buffer so we can decide how to parse the remainder of it. 587 maybeTypeBytes, err := r.Peek(2) 588 if err != nil { 589 return err 590 } 591 592 // Some nodes well prefix an additional set of bytes in front of their 593 // channel updates. These bytes will _almost_ always be 258 or the type 594 // of the ChannelUpdate message. 595 typeInt := binary.BigEndian.Uint16(maybeTypeBytes) 596 if typeInt == MsgChannelUpdate { 597 // At this point it's likely the case that this is a channel 598 // update message with its type prefixed, so we'll snip off the 599 // first two bytes and parse it as normal. 600 var throwAwayTypeBytes [2]byte 601 _, err := r.Read(throwAwayTypeBytes[:]) 602 if err != nil { 603 return err 604 } 605 } 606 607 // At this pint, we've either decided to keep the entire thing, or snip 608 // off the first two bytes. In either case, we can just read it as 609 // normal. 610 return chanUpdate.Decode(r, pver) 611 } 612 613 // FailTemporaryChannelFailure is if an otherwise unspecified transient error 614 // occurs for the outgoing channel (eg. channel capacity reached, too many 615 // in-flight htlcs) 616 // 617 // NOTE: May only be returned by intermediate nodes. 618 type FailTemporaryChannelFailure struct { 619 // Update is used to update information about state of the channel 620 // which caused the failure. 621 // 622 // NOTE: This field is optional. 623 Update *ChannelUpdate 624 } 625 626 // NewTemporaryChannelFailure creates new instance of the FailTemporaryChannelFailure. 627 func NewTemporaryChannelFailure(update *ChannelUpdate) *FailTemporaryChannelFailure { 628 return &FailTemporaryChannelFailure{Update: update} 629 } 630 631 // Code returns the failure unique code. 632 // 633 // NOTE: Part of the FailureMessage interface. 634 func (f *FailTemporaryChannelFailure) Code() FailCode { 635 return CodeTemporaryChannelFailure 636 } 637 638 // Returns a human readable string describing the target FailureMessage. 639 // 640 // NOTE: Implements the error interface. 641 func (f *FailTemporaryChannelFailure) Error() string { 642 if f.Update == nil { 643 return f.Code().String() 644 } 645 646 return fmt.Sprintf("TemporaryChannelFailure(update=%v)", 647 spew.Sdump(f.Update)) 648 } 649 650 // Decode decodes the failure from bytes stream. 651 // 652 // NOTE: Part of the Serializable interface. 653 func (f *FailTemporaryChannelFailure) Decode(r io.Reader, pver uint32) error { 654 var length uint16 655 err := ReadElement(r, &length) 656 if err != nil { 657 return err 658 } 659 660 if length != 0 { 661 f.Update = &ChannelUpdate{} 662 return parseChannelUpdateCompatabilityMode( 663 bufio.NewReader(r), f.Update, pver, 664 ) 665 } 666 667 return nil 668 } 669 670 // Encode writes the failure in bytes stream. 671 // 672 // NOTE: Part of the Serializable interface. 673 func (f *FailTemporaryChannelFailure) Encode(w io.Writer, pver uint32) error { 674 var payload []byte 675 if f.Update != nil { 676 var bw bytes.Buffer 677 if err := f.Update.Encode(&bw, pver); err != nil { 678 return err 679 } 680 payload = bw.Bytes() 681 } 682 683 if err := WriteElement(w, uint16(len(payload))); err != nil { 684 return err 685 } 686 687 _, err := w.Write(payload) 688 return err 689 } 690 691 // FailAmountBelowMinimum is returned if the HTLC does not reach the current 692 // minimum amount, we tell them the amount of the incoming HTLC and the current 693 // channel setting for the outgoing channel. 694 // 695 // NOTE: May only be returned by the intermediate nodes in the path. 696 type FailAmountBelowMinimum struct { 697 // HtlcMAtoms is the wrong amount of the incoming HTLC. 698 HtlcMAtoms MilliAtom 699 700 // Update is used to update information about state of the channel 701 // which caused the failure. 702 Update ChannelUpdate 703 } 704 705 // NewAmountBelowMinimum creates new instance of the FailAmountBelowMinimum. 706 func NewAmountBelowMinimum(htlcMAtoms MilliAtom, 707 update ChannelUpdate) *FailAmountBelowMinimum { 708 709 return &FailAmountBelowMinimum{ 710 HtlcMAtoms: htlcMAtoms, 711 Update: update, 712 } 713 } 714 715 // Code returns the failure unique code. 716 // 717 // NOTE: Part of the FailureMessage interface. 718 func (f *FailAmountBelowMinimum) Code() FailCode { 719 return CodeAmountBelowMinimum 720 } 721 722 // Returns a human readable string describing the target FailureMessage. 723 // 724 // NOTE: Implements the error interface. 725 func (f *FailAmountBelowMinimum) Error() string { 726 return fmt.Sprintf("AmountBelowMinimum(amt=%v, update=%v", f.HtlcMAtoms, 727 spew.Sdump(f.Update)) 728 } 729 730 // Decode decodes the failure from bytes stream. 731 // 732 // NOTE: Part of the Serializable interface. 733 func (f *FailAmountBelowMinimum) Decode(r io.Reader, pver uint32) error { 734 if err := ReadElement(r, &f.HtlcMAtoms); err != nil { 735 return err 736 } 737 738 var length uint16 739 if err := ReadElement(r, &length); err != nil { 740 return err 741 } 742 743 f.Update = ChannelUpdate{} 744 return parseChannelUpdateCompatabilityMode( 745 bufio.NewReader(r), &f.Update, pver, 746 ) 747 } 748 749 // Encode writes the failure in bytes stream. 750 // 751 // NOTE: Part of the Serializable interface. 752 func (f *FailAmountBelowMinimum) Encode(w io.Writer, pver uint32) error { 753 if err := WriteElement(w, f.HtlcMAtoms); err != nil { 754 return err 755 } 756 757 return writeOnionErrorChanUpdate(w, &f.Update, pver) 758 } 759 760 // FailFeeInsufficient is returned if the HTLC does not pay sufficient fee, we 761 // tell them the amount of the incoming HTLC and the current channel setting 762 // for the outgoing channel. 763 // 764 // NOTE: May only be returned by intermediate nodes. 765 type FailFeeInsufficient struct { 766 // HtlcMAtoms is the wrong amount of the incoming HTLC. 767 HtlcMAtoms MilliAtom 768 769 // Update is used to update information about state of the channel 770 // which caused the failure. 771 Update ChannelUpdate 772 } 773 774 // NewFeeInsufficient creates new instance of the FailFeeInsufficient. 775 func NewFeeInsufficient(htlcMAtoms MilliAtom, 776 update ChannelUpdate) *FailFeeInsufficient { 777 return &FailFeeInsufficient{ 778 HtlcMAtoms: htlcMAtoms, 779 Update: update, 780 } 781 } 782 783 // Code returns the failure unique code. 784 // 785 // NOTE: Part of the FailureMessage interface. 786 func (f *FailFeeInsufficient) Code() FailCode { 787 return CodeFeeInsufficient 788 } 789 790 // Returns a human readable string describing the target FailureMessage. 791 // 792 // NOTE: Implements the error interface. 793 func (f *FailFeeInsufficient) Error() string { 794 return fmt.Sprintf("FeeInsufficient(htlc_amt==%v, update=%v", f.HtlcMAtoms, 795 spew.Sdump(f.Update)) 796 } 797 798 // Decode decodes the failure from bytes stream. 799 // 800 // NOTE: Part of the Serializable interface. 801 func (f *FailFeeInsufficient) Decode(r io.Reader, pver uint32) error { 802 if err := ReadElement(r, &f.HtlcMAtoms); err != nil { 803 return err 804 } 805 806 var length uint16 807 if err := ReadElement(r, &length); err != nil { 808 return err 809 } 810 811 f.Update = ChannelUpdate{} 812 return parseChannelUpdateCompatabilityMode( 813 bufio.NewReader(r), &f.Update, pver, 814 ) 815 } 816 817 // Encode writes the failure in bytes stream. 818 // 819 // NOTE: Part of the Serializable interface. 820 func (f *FailFeeInsufficient) Encode(w io.Writer, pver uint32) error { 821 if err := WriteElement(w, f.HtlcMAtoms); err != nil { 822 return err 823 } 824 825 return writeOnionErrorChanUpdate(w, &f.Update, pver) 826 } 827 828 // FailIncorrectCltvExpiry is returned if outgoing cltv value does not match 829 // the update add htlc's cltv expiry minus cltv expiry delta for the outgoing 830 // channel, we tell them the cltv expiry and the current channel setting for 831 // the outgoing channel. 832 // 833 // NOTE: May only be returned by intermediate nodes. 834 type FailIncorrectCltvExpiry struct { 835 // CltvExpiry is the wrong absolute timeout in blocks, after which 836 // outgoing HTLC expires. 837 CltvExpiry uint32 838 839 // Update is used to update information about state of the channel 840 // which caused the failure. 841 Update ChannelUpdate 842 } 843 844 // NewIncorrectCltvExpiry creates new instance of the FailIncorrectCltvExpiry. 845 func NewIncorrectCltvExpiry(cltvExpiry uint32, 846 update ChannelUpdate) *FailIncorrectCltvExpiry { 847 848 return &FailIncorrectCltvExpiry{ 849 CltvExpiry: cltvExpiry, 850 Update: update, 851 } 852 } 853 854 // Code returns the failure unique code. 855 // 856 // NOTE: Part of the FailureMessage interface. 857 func (f *FailIncorrectCltvExpiry) Code() FailCode { 858 return CodeIncorrectCltvExpiry 859 } 860 861 func (f *FailIncorrectCltvExpiry) Error() string { 862 return fmt.Sprintf("IncorrectCltvExpiry(expiry=%v, update=%v", 863 f.CltvExpiry, spew.Sdump(f.Update)) 864 } 865 866 // Decode decodes the failure from bytes stream. 867 // 868 // NOTE: Part of the Serializable interface. 869 func (f *FailIncorrectCltvExpiry) Decode(r io.Reader, pver uint32) error { 870 if err := ReadElement(r, &f.CltvExpiry); err != nil { 871 return err 872 } 873 874 var length uint16 875 if err := ReadElement(r, &length); err != nil { 876 return err 877 } 878 879 f.Update = ChannelUpdate{} 880 return parseChannelUpdateCompatabilityMode( 881 bufio.NewReader(r), &f.Update, pver, 882 ) 883 } 884 885 // Encode writes the failure in bytes stream. 886 // 887 // NOTE: Part of the Serializable interface. 888 func (f *FailIncorrectCltvExpiry) Encode(w io.Writer, pver uint32) error { 889 if err := WriteElement(w, f.CltvExpiry); err != nil { 890 return err 891 } 892 893 return writeOnionErrorChanUpdate(w, &f.Update, pver) 894 } 895 896 // FailExpiryTooSoon is returned if the ctlv-expiry is too near, we tell them 897 // the current channel setting for the outgoing channel. 898 // 899 // NOTE: May only be returned by intermediate nodes. 900 type FailExpiryTooSoon struct { 901 // Update is used to update information about state of the channel 902 // which caused the failure. 903 Update ChannelUpdate 904 } 905 906 // NewExpiryTooSoon creates new instance of the FailExpiryTooSoon. 907 func NewExpiryTooSoon(update ChannelUpdate) *FailExpiryTooSoon { 908 return &FailExpiryTooSoon{ 909 Update: update, 910 } 911 } 912 913 // Code returns the failure unique code. 914 // 915 // NOTE: Part of the FailureMessage interface. 916 func (f *FailExpiryTooSoon) Code() FailCode { 917 return CodeExpiryTooSoon 918 } 919 920 // Returns a human readable string describing the target FailureMessage. 921 // 922 // NOTE: Implements the error interface. 923 func (f *FailExpiryTooSoon) Error() string { 924 return fmt.Sprintf("ExpiryTooSoon(update=%v", spew.Sdump(f.Update)) 925 } 926 927 // Decode decodes the failure from l stream. 928 // 929 // NOTE: Part of the Serializable interface. 930 func (f *FailExpiryTooSoon) Decode(r io.Reader, pver uint32) error { 931 var length uint16 932 if err := ReadElement(r, &length); err != nil { 933 return err 934 } 935 936 f.Update = ChannelUpdate{} 937 return parseChannelUpdateCompatabilityMode( 938 bufio.NewReader(r), &f.Update, pver, 939 ) 940 } 941 942 // Encode writes the failure in bytes stream. 943 // 944 // NOTE: Part of the Serializable interface. 945 func (f *FailExpiryTooSoon) Encode(w io.Writer, pver uint32) error { 946 return writeOnionErrorChanUpdate(w, &f.Update, pver) 947 } 948 949 // FailChannelDisabled is returned if the channel is disabled, we tell them the 950 // current channel setting for the outgoing channel. 951 // 952 // NOTE: May only be returned by intermediate nodes. 953 type FailChannelDisabled struct { 954 // Flags least-significant bit must be set to 0 if the creating node 955 // corresponds to the first node in the previously sent channel 956 // announcement and 1 otherwise. 957 Flags uint16 958 959 // Update is used to update information about state of the channel 960 // which caused the failure. 961 Update ChannelUpdate 962 } 963 964 // NewChannelDisabled creates new instance of the FailChannelDisabled. 965 func NewChannelDisabled(flags uint16, update ChannelUpdate) *FailChannelDisabled { 966 return &FailChannelDisabled{ 967 Flags: flags, 968 Update: update, 969 } 970 } 971 972 // Code returns the failure unique code. 973 // 974 // NOTE: Part of the FailureMessage interface. 975 func (f *FailChannelDisabled) Code() FailCode { 976 return CodeChannelDisabled 977 } 978 979 // Returns a human readable string describing the target FailureMessage. 980 // 981 // NOTE: Implements the error interface. 982 func (f *FailChannelDisabled) Error() string { 983 return fmt.Sprintf("ChannelDisabled(flags=%v, update=%v", f.Flags, 984 spew.Sdump(f.Update)) 985 } 986 987 // Decode decodes the failure from bytes stream. 988 // 989 // NOTE: Part of the Serializable interface. 990 func (f *FailChannelDisabled) Decode(r io.Reader, pver uint32) error { 991 if err := ReadElement(r, &f.Flags); err != nil { 992 return err 993 } 994 995 var length uint16 996 if err := ReadElement(r, &length); err != nil { 997 return err 998 } 999 1000 f.Update = ChannelUpdate{} 1001 return parseChannelUpdateCompatabilityMode( 1002 bufio.NewReader(r), &f.Update, pver, 1003 ) 1004 } 1005 1006 // Encode writes the failure in bytes stream. 1007 // 1008 // NOTE: Part of the Serializable interface. 1009 func (f *FailChannelDisabled) Encode(w io.Writer, pver uint32) error { 1010 if err := WriteElement(w, f.Flags); err != nil { 1011 return err 1012 } 1013 1014 return writeOnionErrorChanUpdate(w, &f.Update, pver) 1015 } 1016 1017 // FailFinalIncorrectCltvExpiry is returned if the outgoing_cltv_value does not 1018 // match the ctlv_expiry of the HTLC at the final hop. 1019 // 1020 // NOTE: might be returned by final node only. 1021 type FailFinalIncorrectCltvExpiry struct { 1022 // CltvExpiry is the wrong absolute timeout in blocks, after which 1023 // outgoing HTLC expires. 1024 CltvExpiry uint32 1025 } 1026 1027 // Returns a human readable string describing the target FailureMessage. 1028 // 1029 // NOTE: Implements the error interface. 1030 func (f *FailFinalIncorrectCltvExpiry) Error() string { 1031 return fmt.Sprintf("FinalIncorrectCltvExpiry(expiry=%v)", f.CltvExpiry) 1032 } 1033 1034 // NewFinalIncorrectCltvExpiry creates new instance of the 1035 // FailFinalIncorrectCltvExpiry. 1036 func NewFinalIncorrectCltvExpiry(cltvExpiry uint32) *FailFinalIncorrectCltvExpiry { 1037 return &FailFinalIncorrectCltvExpiry{ 1038 CltvExpiry: cltvExpiry, 1039 } 1040 } 1041 1042 // Code returns the failure unique code. 1043 // 1044 // NOTE: Part of the FailureMessage interface. 1045 func (f *FailFinalIncorrectCltvExpiry) Code() FailCode { 1046 return CodeFinalIncorrectCltvExpiry 1047 } 1048 1049 // Decode decodes the failure from bytes stream. 1050 // 1051 // NOTE: Part of the Serializable interface. 1052 func (f *FailFinalIncorrectCltvExpiry) Decode(r io.Reader, pver uint32) error { 1053 return ReadElement(r, &f.CltvExpiry) 1054 } 1055 1056 // Encode writes the failure in bytes stream. 1057 // 1058 // NOTE: Part of the Serializable interface. 1059 func (f *FailFinalIncorrectCltvExpiry) Encode(w io.Writer, pver uint32) error { 1060 return WriteElement(w, f.CltvExpiry) 1061 } 1062 1063 // FailFinalIncorrectHtlcAmount is returned if the amt_to_forward is higher 1064 // than incoming_htlc_amt of the HTLC at the final hop. 1065 // 1066 // NOTE: May only be returned by the final node. 1067 type FailFinalIncorrectHtlcAmount struct { 1068 // IncomingHTLCAmount is the wrong forwarded htlc amount. 1069 IncomingHTLCAmount MilliAtom 1070 } 1071 1072 // Returns a human readable string describing the target FailureMessage. 1073 // 1074 // NOTE: Implements the error interface. 1075 func (f *FailFinalIncorrectHtlcAmount) Error() string { 1076 return fmt.Sprintf("FinalIncorrectHtlcAmount(amt=%v)", 1077 f.IncomingHTLCAmount) 1078 } 1079 1080 // NewFinalIncorrectHtlcAmount creates new instance of the 1081 // FailFinalIncorrectHtlcAmount. 1082 func NewFinalIncorrectHtlcAmount(amount MilliAtom) *FailFinalIncorrectHtlcAmount { 1083 return &FailFinalIncorrectHtlcAmount{ 1084 IncomingHTLCAmount: amount, 1085 } 1086 } 1087 1088 // Code returns the failure unique code. 1089 // 1090 // NOTE: Part of the FailureMessage interface. 1091 func (f *FailFinalIncorrectHtlcAmount) Code() FailCode { 1092 return CodeFinalIncorrectHtlcAmount 1093 } 1094 1095 // Decode decodes the failure from bytes stream. 1096 // 1097 // NOTE: Part of the Serializable interface. 1098 func (f *FailFinalIncorrectHtlcAmount) Decode(r io.Reader, pver uint32) error { 1099 return ReadElement(r, &f.IncomingHTLCAmount) 1100 } 1101 1102 // Encode writes the failure in bytes stream. 1103 // 1104 // NOTE: Part of the Serializable interface. 1105 func (f *FailFinalIncorrectHtlcAmount) Encode(w io.Writer, pver uint32) error { 1106 return WriteElement(w, f.IncomingHTLCAmount) 1107 } 1108 1109 // FailExpiryTooFar is returned if the CLTV expiry in the HTLC is too far in the 1110 // future. 1111 // 1112 // NOTE: May be returned by any node in the payment route. 1113 type FailExpiryTooFar struct{} 1114 1115 // Code returns the failure unique code. 1116 // 1117 // NOTE: Part of the FailureMessage interface. 1118 func (f *FailExpiryTooFar) Code() FailCode { 1119 return CodeExpiryTooFar 1120 } 1121 1122 // Returns a human readable string describing the target FailureMessage. 1123 // 1124 // NOTE: Implements the error interface. 1125 func (f *FailExpiryTooFar) Error() string { 1126 return f.Code().String() 1127 } 1128 1129 // InvalidOnionPayload is returned if the hop could not process the TLV payload 1130 // enclosed in the onion. 1131 type InvalidOnionPayload struct { 1132 // Type is the TLV type that caused the specific failure. 1133 Type uint64 1134 1135 // Offset is the byte offset within the payload where the failure 1136 // occurred. 1137 Offset uint16 1138 } 1139 1140 // NewInvalidOnionPayload initializes a new InvalidOnionPayload failure. 1141 func NewInvalidOnionPayload(typ uint64, offset uint16) *InvalidOnionPayload { 1142 return &InvalidOnionPayload{ 1143 Type: typ, 1144 Offset: offset, 1145 } 1146 } 1147 1148 // Code returns the failure unique code. 1149 // 1150 // NOTE: Part of the FailureMessage interface. 1151 func (f *InvalidOnionPayload) Code() FailCode { 1152 return CodeInvalidOnionPayload 1153 } 1154 1155 // Returns a human readable string describing the target FailureMessage. 1156 // 1157 // NOTE: Implements the error interface. 1158 func (f *InvalidOnionPayload) Error() string { 1159 return fmt.Sprintf("%v(type=%v, offset=%d)", 1160 f.Code(), f.Type, f.Offset) 1161 } 1162 1163 // Decode decodes the failure from bytes stream. 1164 // 1165 // NOTE: Part of the Serializable interface. 1166 func (f *InvalidOnionPayload) Decode(r io.Reader, pver uint32) error { 1167 var buf [8]byte 1168 typ, err := tlv.ReadVarInt(r, &buf) 1169 if err != nil { 1170 return err 1171 } 1172 f.Type = typ 1173 1174 return ReadElements(r, &f.Offset) 1175 } 1176 1177 // Encode writes the failure in bytes stream. 1178 // 1179 // NOTE: Part of the Serializable interface. 1180 func (f *InvalidOnionPayload) Encode(w io.Writer, pver uint32) error { 1181 var buf [8]byte 1182 if err := tlv.WriteVarInt(w, f.Type, &buf); err != nil { 1183 return err 1184 } 1185 1186 return WriteElements(w, f.Offset) 1187 } 1188 1189 // FailMPPTimeout is returned if the complete amount for a multi part payment 1190 // was not received within a reasonable time. 1191 // 1192 // NOTE: May only be returned by the final node in the path. 1193 type FailMPPTimeout struct{} 1194 1195 // Code returns the failure unique code. 1196 // 1197 // NOTE: Part of the FailureMessage interface. 1198 func (f *FailMPPTimeout) Code() FailCode { 1199 return CodeMPPTimeout 1200 } 1201 1202 // Returns a human readable string describing the target FailureMessage. 1203 // 1204 // NOTE: Implements the error interface. 1205 func (f *FailMPPTimeout) Error() string { 1206 return f.Code().String() 1207 } 1208 1209 // DecodeFailure decodes, validates, and parses the lnwire onion failure, for 1210 // the provided protocol version. 1211 func DecodeFailure(r io.Reader, pver uint32) (FailureMessage, error) { 1212 // First, we'll parse out the encapsulated failure message itself. This 1213 // is a 2 byte length followed by the payload itself. 1214 var failureLength uint16 1215 if err := ReadElement(r, &failureLength); err != nil { 1216 return nil, fmt.Errorf("unable to read error len: %v", err) 1217 } 1218 if failureLength > FailureMessageLength { 1219 return nil, fmt.Errorf("failure message is too "+ 1220 "long: %v", failureLength) 1221 } 1222 failureData := make([]byte, failureLength) 1223 if _, err := io.ReadFull(r, failureData); err != nil { 1224 return nil, fmt.Errorf("unable to full read payload of "+ 1225 "%v: %v", failureLength, err) 1226 } 1227 1228 dataReader := bytes.NewReader(failureData) 1229 1230 return DecodeFailureMessage(dataReader, pver) 1231 } 1232 1233 // DecodeFailureMessage decodes just the failure message, ignoring any padding 1234 // that may be present at the end. 1235 func DecodeFailureMessage(r io.Reader, pver uint32) (FailureMessage, error) { 1236 // Once we have the failure data, we can obtain the failure code from 1237 // the first two bytes of the buffer. 1238 var codeBytes [2]byte 1239 if _, err := io.ReadFull(r, codeBytes[:]); err != nil { 1240 return nil, fmt.Errorf("unable to read failure code: %v", err) 1241 } 1242 failCode := FailCode(binary.BigEndian.Uint16(codeBytes[:])) 1243 1244 // Create the empty failure by given code and populate the failure with 1245 // additional data if needed. 1246 failure, err := makeEmptyOnionError(failCode) 1247 if err != nil { 1248 return nil, fmt.Errorf("unable to make empty error: %v", err) 1249 } 1250 1251 // Finally, if this failure has a payload, then we'll read that now as 1252 // well. 1253 switch f := failure.(type) { 1254 case Serializable: 1255 if err := f.Decode(r, pver); err != nil { 1256 return nil, fmt.Errorf("unable to decode error "+ 1257 "update (type=%T): %v", failure, err) 1258 } 1259 } 1260 1261 return failure, nil 1262 } 1263 1264 // EncodeFailure encodes, including the necessary onion failure header 1265 // information. 1266 func EncodeFailure(w io.Writer, failure FailureMessage, pver uint32) error { 1267 var failureMessageBuffer bytes.Buffer 1268 1269 err := EncodeFailureMessage(&failureMessageBuffer, failure, pver) 1270 if err != nil { 1271 return err 1272 } 1273 1274 // The combined size of this message must be below the max allowed 1275 // failure message length. 1276 failureMessage := failureMessageBuffer.Bytes() 1277 if len(failureMessage) > FailureMessageLength { 1278 return fmt.Errorf("failure message exceed max "+ 1279 "available size: %v", len(failureMessage)) 1280 } 1281 1282 // Finally, we'll add some padding in order to ensure that all failure 1283 // messages are fixed size. 1284 pad := make([]byte, FailureMessageLength-len(failureMessage)) 1285 1286 return WriteElements(w, 1287 uint16(len(failureMessage)), 1288 failureMessage, 1289 uint16(len(pad)), 1290 pad, 1291 ) 1292 } 1293 1294 // EncodeFailureMessage encodes just the failure message without adding a length 1295 // and padding the message for the onion protocol. 1296 func EncodeFailureMessage(w io.Writer, failure FailureMessage, pver uint32) error { 1297 // First, we'll write out the error code itself into the failure 1298 // buffer. 1299 var codeBytes [2]byte 1300 code := uint16(failure.Code()) 1301 binary.BigEndian.PutUint16(codeBytes[:], code) 1302 _, err := w.Write(codeBytes[:]) 1303 if err != nil { 1304 return err 1305 } 1306 1307 // Next, some message have an additional message payload, if this is 1308 // one of those types, then we'll also encode the error payload as 1309 // well. 1310 switch failure := failure.(type) { 1311 case Serializable: 1312 if err := failure.Encode(w, pver); err != nil { 1313 return err 1314 } 1315 } 1316 1317 return nil 1318 } 1319 1320 // makeEmptyOnionError creates a new empty onion error of the proper concrete 1321 // type based on the passed failure code. 1322 func makeEmptyOnionError(code FailCode) (FailureMessage, error) { 1323 switch code { 1324 case CodeInvalidRealm: 1325 return &FailInvalidRealm{}, nil 1326 1327 case CodeTemporaryNodeFailure: 1328 return &FailTemporaryNodeFailure{}, nil 1329 1330 case CodePermanentNodeFailure: 1331 return &FailPermanentNodeFailure{}, nil 1332 1333 case CodeRequiredNodeFeatureMissing: 1334 return &FailRequiredNodeFeatureMissing{}, nil 1335 1336 case CodePermanentChannelFailure: 1337 return &FailPermanentChannelFailure{}, nil 1338 1339 case CodeRequiredChannelFeatureMissing: 1340 return &FailRequiredChannelFeatureMissing{}, nil 1341 1342 case CodeUnknownNextPeer: 1343 return &FailUnknownNextPeer{}, nil 1344 1345 case CodeIncorrectOrUnknownPaymentDetails: 1346 return &FailIncorrectDetails{}, nil 1347 1348 case CodeIncorrectPaymentAmount: 1349 return &FailIncorrectPaymentAmount{}, nil 1350 1351 case CodeFinalExpiryTooSoon: 1352 return &FailFinalExpiryTooSoon{}, nil 1353 1354 case CodeInvalidOnionVersion: 1355 return &FailInvalidOnionVersion{}, nil 1356 1357 case CodeInvalidOnionHmac: 1358 return &FailInvalidOnionHmac{}, nil 1359 1360 case CodeInvalidOnionKey: 1361 return &FailInvalidOnionKey{}, nil 1362 1363 case CodeTemporaryChannelFailure: 1364 return &FailTemporaryChannelFailure{}, nil 1365 1366 case CodeAmountBelowMinimum: 1367 return &FailAmountBelowMinimum{}, nil 1368 1369 case CodeFeeInsufficient: 1370 return &FailFeeInsufficient{}, nil 1371 1372 case CodeIncorrectCltvExpiry: 1373 return &FailIncorrectCltvExpiry{}, nil 1374 1375 case CodeExpiryTooSoon: 1376 return &FailExpiryTooSoon{}, nil 1377 1378 case CodeChannelDisabled: 1379 return &FailChannelDisabled{}, nil 1380 1381 case CodeFinalIncorrectCltvExpiry: 1382 return &FailFinalIncorrectCltvExpiry{}, nil 1383 1384 case CodeFinalIncorrectHtlcAmount: 1385 return &FailFinalIncorrectHtlcAmount{}, nil 1386 1387 case CodeExpiryTooFar: 1388 return &FailExpiryTooFar{}, nil 1389 1390 case CodeInvalidOnionPayload: 1391 return &InvalidOnionPayload{}, nil 1392 1393 case CodeMPPTimeout: 1394 return &FailMPPTimeout{}, nil 1395 1396 default: 1397 return nil, errors.Errorf("unknown error code: %v", code) 1398 } 1399 } 1400 1401 // writeOnionErrorChanUpdate writes out a ChannelUpdate using the onion error 1402 // format. The format is that we first write out the true serialized length of 1403 // the channel update, followed by the serialized channel update itself. 1404 func writeOnionErrorChanUpdate(w io.Writer, chanUpdate *ChannelUpdate, 1405 pver uint32) error { 1406 1407 // First, we encode the channel update in a temporary buffer in order 1408 // to get the exact serialized size. 1409 var b bytes.Buffer 1410 if err := chanUpdate.Encode(&b, pver); err != nil { 1411 return err 1412 } 1413 1414 // Now that we know the size, we can write the length out in the main 1415 // writer. 1416 updateLen := b.Len() 1417 if err := WriteElement(w, uint16(updateLen)); err != nil { 1418 return err 1419 } 1420 1421 // With the length written, we'll then write out the serialized channel 1422 // update. 1423 if _, err := w.Write(b.Bytes()); err != nil { 1424 return err 1425 } 1426 1427 return nil 1428 }