intercepted_forward.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package lnd
  2. import (
  3. "errors"
  4. "github.com/lightningnetwork/lnd/htlcswitch"
  5. "github.com/lightningnetwork/lnd/lntypes"
  6. "github.com/lightningnetwork/lnd/lnwire"
  7. )
  8. var (
  9. // ErrCannotResume is returned when an intercepted forward cannot be
  10. // resumed. This is the case in the on-chain resolution flow.
  11. ErrCannotResume = errors.New("cannot resume in the on-chain flow")
  12. // ErrCannotFail is returned when an intercepted forward cannot be failed.
  13. // This is the case in the on-chain resolution flow.
  14. ErrCannotFail = errors.New("cannot fail in the on-chain flow")
  15. // ErrPreimageMismatch is returned when the preimage that is specified to
  16. // settle an htlc doesn't match the htlc hash.
  17. ErrPreimageMismatch = errors.New("preimage does not match hash")
  18. )
  19. // interceptedForward implements the on-chain behavior for the resolution of
  20. // a forwarded htlc.
  21. type interceptedForward struct {
  22. packet *htlcswitch.InterceptedPacket
  23. beacon *preimageBeacon
  24. }
  25. func newInterceptedForward(
  26. packet *htlcswitch.InterceptedPacket,
  27. beacon *preimageBeacon) *interceptedForward {
  28. return &interceptedForward{
  29. beacon: beacon,
  30. packet: packet,
  31. }
  32. }
  33. // Packet returns the intercepted htlc packet.
  34. func (f *interceptedForward) Packet() htlcswitch.InterceptedPacket {
  35. return *f.packet
  36. }
  37. // Resume notifies the intention to resume an existing hold forward. This
  38. // basically means the caller wants to resume with the default behavior for this
  39. // htlc which usually means forward it.
  40. func (f *interceptedForward) Resume() error {
  41. return ErrCannotResume
  42. }
  43. // Fail notifies the intention to fail an existing hold forward with an
  44. // encrypted failure reason.
  45. func (f *interceptedForward) Fail(_ []byte) error {
  46. // We can't actively fail an htlc. The best we could do is abandon the
  47. // resolver, but this wouldn't be a safe operation. There may be a race
  48. // with the preimage beacon supplying a preimage. Therefore we don't
  49. // attempt to fail and just return an error here.
  50. return ErrCannotFail
  51. }
  52. // FailWithCode notifies the intention to fail an existing hold forward with the
  53. // specified failure code.
  54. func (f *interceptedForward) FailWithCode(_ lnwire.FailCode) error {
  55. return ErrCannotFail
  56. }
  57. // Settle notifies the intention to settle an existing hold forward with a given
  58. // preimage.
  59. func (f *interceptedForward) Settle(preimage lntypes.Preimage) error {
  60. if !preimage.Matches(f.packet.Hash) {
  61. return ErrPreimageMismatch
  62. }
  63. // Add preimage to the preimage beacon. The onchain resolver will pick
  64. // up the preimage from the beacon.
  65. return f.beacon.AddPreimages(preimage)
  66. }