intercepted_forward.go 2.8 KB

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