Skip to content

Commit

Permalink
multi: add incoming htlc amount to interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero authored and ffranr committed May 15, 2024
1 parent 8025296 commit 03dceca
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 181 deletions.
13 changes: 12 additions & 1 deletion htlcswitch/interceptable_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ type FwdResolution struct {
// FwdActionSettle.
Preimage lntypes.Preimage

// IncomingAmountMsat is the amount that is to be used for validating if
// Action is FwdActionResumeModified.
IncomingAmountMsat fn.Option[lnwire.MilliSatoshi]

// OutgoingAmountMsat is the amount that is to be used for forwarding if
// Action is FwdActionResumeModified.
OutgoingAmountMsat fn.Option[lnwire.MilliSatoshi]
Expand Down Expand Up @@ -391,7 +395,8 @@ func (s *InterceptableSwitch) resolve(res *FwdResolution) error {

case FwdActionResumeModified:
return intercepted.ResumeModified(
res.OutgoingAmountMsat, res.CustomRecords,
res.IncomingAmountMsat, res.OutgoingAmountMsat,
res.CustomRecords,
)

case FwdActionSettle:
Expand Down Expand Up @@ -638,9 +643,15 @@ func (f *interceptedForward) Resume() error {

// ResumeModified resumes the default behavior with field modifications.
func (f *interceptedForward) ResumeModified(
incomingAmountMsat fn.Option[lnwire.MilliSatoshi],
outgoingAmountMsat fn.Option[lnwire.MilliSatoshi],
customRecords fn.Option[record.CustomSet]) error {

// Set the incoming amount, if it is provided, on the packet.
incomingAmountMsat.WhenSome(func(amount lnwire.MilliSatoshi) {
f.packet.incomingAmount = amount
})

// Modify the wire message contained in the packet.
switch htlc := f.packet.htlc.(type) {
case *lnwire.UpdateAddHTLC:
Expand Down
3 changes: 2 additions & 1 deletion htlcswitch/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ type InterceptedForward interface {

// ResumeModified notifies the intention to resume an existing hold
// forward with modified fields.
ResumeModified(outgoingAmountMsat fn.Option[lnwire.MilliSatoshi],
ResumeModified(incomingAmountMsat,
outgoingAmountMsat fn.Option[lnwire.MilliSatoshi],
customRecords fn.Option[record.CustomSet]) error

// Settle notifies the intention to settle an existing hold
Expand Down
2 changes: 1 addition & 1 deletion intercepted_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (f *interceptedForward) Resume() error {

// ResumeModified notifies the intention to resume an existing hold forward with
// a modified htlc.
func (f *interceptedForward) ResumeModified(_ fn.Option[lnwire.MilliSatoshi],
func (f *interceptedForward) ResumeModified(_, _ fn.Option[lnwire.MilliSatoshi],
_ fn.Option[record.CustomSet]) error {

return ErrCannotResume
Expand Down
8 changes: 8 additions & 0 deletions lnrpc/routerrpc/forward_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ func (r *forwardInterceptor) resolveFromClient(

case ResolveHoldForwardAction_RESUME_MODIFIED:
// Modify HTLC and resume forward.
incomingAmtMsat := fn.None[lnwire.MilliSatoshi]()
if in.IncomingAmountMsat > 0 {
incomingAmtMsat = fn.Some[lnwire.MilliSatoshi](
lnwire.MilliSatoshi(in.IncomingAmountMsat),
)
}

outgoingAmtMsat := fn.None[lnwire.MilliSatoshi]()
if in.OutgoingAmountMsat > 0 {
outgoingAmtMsat = fn.Some[lnwire.MilliSatoshi](
Expand All @@ -148,6 +155,7 @@ func (r *forwardInterceptor) resolveFromClient(
return r.htlcSwitch.Resolve(&htlcswitch.FwdResolution{
Key: circuitKey,
Action: htlcswitch.FwdActionResumeModified,
IncomingAmountMsat: incomingAmtMsat,
OutgoingAmountMsat: outgoingAmtMsat,
CustomRecords: customRecords,
})
Expand Down
365 changes: 189 additions & 176 deletions lnrpc/routerrpc/router.pb.go

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions lnrpc/routerrpc/router.proto
Original file line number Diff line number Diff line change
Expand Up @@ -993,13 +993,17 @@ message ForwardHtlcInterceptResponse {
// default value for this field.
lnrpc.Failure.FailureCode failure_code = 5;

// incoming_amount_msat is used to set the p2p message incoming amount field
// for validating an incoming HTLC.
uint64 incoming_amount_msat = 6;

// outgoing_amount_msat is used to set the p2p message outgoing amount field
// for resuming a HTLC.
uint64 outgoing_amount_msat = 6;
uint64 outgoing_amount_msat = 7;

// custom_records is used to set the p2p message custom records field for
// resuming a HTLC.
map<uint64, bytes> custom_records = 7;
map<uint64, bytes> custom_records = 8;
}

enum ResolveHoldForwardAction {
Expand Down
5 changes: 5 additions & 0 deletions lnrpc/routerrpc/router.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,11 @@
"$ref": "#/definitions/FailureFailureCode",
"description": "Return the specified failure code in case the resolve action is Fail. The\nmessage data fields are populated automatically.\n\nIf a non-zero failure_code is specified, failure_message must not be set.\n\nFor backwards-compatibility reasons, TEMPORARY_CHANNEL_FAILURE is the\ndefault value for this field."
},
"incoming_amount_msat": {
"type": "string",
"format": "uint64",
"description": "incoming_amount_msat is used to set the p2p message incoming amount field\nfor validating an incoming HTLC."
},
"outgoing_amount_msat": {
"type": "string",
"format": "uint64",
Expand Down

0 comments on commit 03dceca

Please sign in to comment.