mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-01-18 21:35:24 +01:00
htlcswitch: add resume modified HTLC action to switch
Introduce `ResumeModified` action to resume standard behavior of a p2p message with optional modifications as specified by the client during interception.
This commit is contained in:
parent
8d1059f41c
commit
abca4b8234
@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/go-errors/errors"
|
"github.com/go-errors/errors"
|
||||||
"github.com/lightningnetwork/lnd/chainntnfs"
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
||||||
"github.com/lightningnetwork/lnd/channeldb/models"
|
"github.com/lightningnetwork/lnd/channeldb/models"
|
||||||
|
"github.com/lightningnetwork/lnd/fn"
|
||||||
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
||||||
"github.com/lightningnetwork/lnd/lntypes"
|
"github.com/lightningnetwork/lnd/lntypes"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
@ -109,6 +110,10 @@ const (
|
|||||||
|
|
||||||
// FwdActionFail fails the intercepted packet back to the sender.
|
// FwdActionFail fails the intercepted packet back to the sender.
|
||||||
FwdActionFail
|
FwdActionFail
|
||||||
|
|
||||||
|
// FwdActionResumeModified forwards the intercepted packet to the switch
|
||||||
|
// with modifications.
|
||||||
|
FwdActionResumeModified
|
||||||
)
|
)
|
||||||
|
|
||||||
// FwdResolution defines the action to be taken on an intercepted packet.
|
// FwdResolution defines the action to be taken on an intercepted packet.
|
||||||
@ -123,6 +128,14 @@ type FwdResolution struct {
|
|||||||
// FwdActionSettle.
|
// FwdActionSettle.
|
||||||
Preimage lntypes.Preimage
|
Preimage lntypes.Preimage
|
||||||
|
|
||||||
|
// OutAmountMsat is the amount that is to be used for forwarding if
|
||||||
|
// Action is FwdActionResumeModified.
|
||||||
|
OutAmountMsat fn.Option[lnwire.MilliSatoshi]
|
||||||
|
|
||||||
|
// OutWireCustomRecords is the custom records that are to be used for
|
||||||
|
// forwarding if Action is FwdActionResumeModified.
|
||||||
|
OutWireCustomRecords fn.Option[lnwire.CustomRecords]
|
||||||
|
|
||||||
// FailureMessage is the encrypted failure message that is to be passed
|
// FailureMessage is the encrypted failure message that is to be passed
|
||||||
// back to the sender if action is FwdActionFail.
|
// back to the sender if action is FwdActionFail.
|
||||||
FailureMessage []byte
|
FailureMessage []byte
|
||||||
@ -399,6 +412,11 @@ func (s *InterceptableSwitch) resolve(res *FwdResolution) error {
|
|||||||
case FwdActionResume:
|
case FwdActionResume:
|
||||||
return intercepted.Resume()
|
return intercepted.Resume()
|
||||||
|
|
||||||
|
case FwdActionResumeModified:
|
||||||
|
return intercepted.ResumeModified(
|
||||||
|
res.OutAmountMsat, res.OutWireCustomRecords,
|
||||||
|
)
|
||||||
|
|
||||||
case FwdActionSettle:
|
case FwdActionSettle:
|
||||||
return intercepted.Settle(res.Preimage)
|
return intercepted.Settle(res.Preimage)
|
||||||
|
|
||||||
@ -641,6 +659,64 @@ func (f *interceptedForward) Resume() error {
|
|||||||
return f.htlcSwitch.ForwardPackets(nil, f.packet)
|
return f.htlcSwitch.ForwardPackets(nil, f.packet)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResumeModified resumes the default behavior with field modifications. The
|
||||||
|
// input amount (if provided) specifies that the value of the inbound HTLC
|
||||||
|
// should be interpreted differently from the on-chain amount during further
|
||||||
|
// validation. The presence of an output amount and/or custom records indicates
|
||||||
|
// that those values should be modified on the outgoing HTLC.
|
||||||
|
func (f *interceptedForward) ResumeModified(
|
||||||
|
outAmountMsat fn.Option[lnwire.MilliSatoshi],
|
||||||
|
outWireCustomRecords fn.Option[lnwire.CustomRecords]) error {
|
||||||
|
|
||||||
|
// Convert the optional custom records to the correct type and validate
|
||||||
|
// them.
|
||||||
|
validatedRecords, err := fn.MapOptionZ(
|
||||||
|
outWireCustomRecords,
|
||||||
|
func(cr lnwire.CustomRecords) fn.Result[lnwire.CustomRecords] {
|
||||||
|
if len(cr) == 0 {
|
||||||
|
return fn.Ok[lnwire.CustomRecords](nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Type cast and validate custom records.
|
||||||
|
err := cr.Validate()
|
||||||
|
if err != nil {
|
||||||
|
return fn.Err[lnwire.CustomRecords](
|
||||||
|
fmt.Errorf("failed to validate "+
|
||||||
|
"custom records: %w", err),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fn.Ok(cr)
|
||||||
|
},
|
||||||
|
).Unpack()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to encode custom records: %w",
|
||||||
|
err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Modify the wire message contained in the packet.
|
||||||
|
switch htlc := f.packet.htlc.(type) {
|
||||||
|
case *lnwire.UpdateAddHTLC:
|
||||||
|
outAmountMsat.WhenSome(func(amount lnwire.MilliSatoshi) {
|
||||||
|
f.packet.amount = amount
|
||||||
|
htlc.Amount = amount
|
||||||
|
})
|
||||||
|
|
||||||
|
if len(validatedRecords) > 0 {
|
||||||
|
htlc.CustomRecords = validatedRecords
|
||||||
|
}
|
||||||
|
|
||||||
|
case *lnwire.UpdateFulfillHTLC:
|
||||||
|
if len(validatedRecords) > 0 {
|
||||||
|
htlc.CustomRecords = validatedRecords
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Forward to the switch. A link quit channel isn't needed, because we
|
||||||
|
// are on a different thread now.
|
||||||
|
return f.htlcSwitch.ForwardPackets(nil, f.packet)
|
||||||
|
}
|
||||||
|
|
||||||
// Fail notifies the intention to Fail an existing hold forward with an
|
// Fail notifies the intention to Fail an existing hold forward with an
|
||||||
// encrypted failure reason.
|
// encrypted failure reason.
|
||||||
func (f *interceptedForward) Fail(reason []byte) error {
|
func (f *interceptedForward) Fail(reason []byte) error {
|
||||||
|
@ -383,6 +383,11 @@ type InterceptedForward interface {
|
|||||||
// this htlc which usually means forward it.
|
// this htlc which usually means forward it.
|
||||||
Resume() error
|
Resume() error
|
||||||
|
|
||||||
|
// ResumeModified notifies the intention to resume an existing hold
|
||||||
|
// forward with modified fields.
|
||||||
|
ResumeModified(outgoingAmountMsat fn.Option[lnwire.MilliSatoshi],
|
||||||
|
customRecords fn.Option[lnwire.CustomRecords]) error
|
||||||
|
|
||||||
// Settle notifies the intention to settle an existing hold
|
// Settle notifies the intention to settle an existing hold
|
||||||
// forward with a given preimage.
|
// forward with a given preimage.
|
||||||
Settle(lntypes.Preimage) error
|
Settle(lntypes.Preimage) error
|
||||||
|
@ -3,6 +3,7 @@ package lnd
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"github.com/lightningnetwork/lnd/fn"
|
||||||
"github.com/lightningnetwork/lnd/htlcswitch"
|
"github.com/lightningnetwork/lnd/htlcswitch"
|
||||||
"github.com/lightningnetwork/lnd/lntypes"
|
"github.com/lightningnetwork/lnd/lntypes"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
@ -51,6 +52,14 @@ func (f *interceptedForward) Resume() error {
|
|||||||
return ErrCannotResume
|
return ErrCannotResume
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResumeModified notifies the intention to resume an existing hold forward with
|
||||||
|
// a modified htlc.
|
||||||
|
func (f *interceptedForward) ResumeModified(_ fn.Option[lnwire.MilliSatoshi],
|
||||||
|
_ fn.Option[lnwire.CustomRecords]) error {
|
||||||
|
|
||||||
|
return ErrCannotResume
|
||||||
|
}
|
||||||
|
|
||||||
// Fail notifies the intention to fail an existing hold forward with an
|
// Fail notifies the intention to fail an existing hold forward with an
|
||||||
// encrypted failure reason.
|
// encrypted failure reason.
|
||||||
func (f *interceptedForward) Fail(_ []byte) error {
|
func (f *interceptedForward) Fail(_ []byte) error {
|
||||||
|
@ -176,7 +176,7 @@ func TestInvoices(t *testing.T) {
|
|||||||
test: testQueryInvoices,
|
test: testQueryInvoices,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "CustomRecords",
|
name: "OutWireCustomRecords",
|
||||||
test: testCustomRecords,
|
test: testCustomRecords,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user