Skip to content

Commit

Permalink
Interceptor improvements
Browse files Browse the repository at this point in the history
* Add OnionBlob and OutgoingAmount parameters
* Add OutgoingRequestedChanId when resuming a forward htlc
  • Loading branch information
yaslama committed Nov 16, 2023
1 parent e5ed824 commit 5f0a699
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 169 deletions.
18 changes: 15 additions & 3 deletions htlcswitch/interceptable_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ type FwdResolution struct {
// FwdActionSettle.
Preimage lntypes.Preimage

OutgoingAmount lnwire.MilliSatoshi
OutgoingChanID lnwire.ShortChannelID
OnionBlob [lnwire.OnionPacketSize]byte

// FailureMessage is the encrypted failure message that is to be passed
// back to the sender if action is FwdActionFail.
FailureMessage []byte
Expand Down Expand Up @@ -356,7 +360,10 @@ func (s *InterceptableSwitch) setInterceptor(interceptor ForwardInterceptor) {
log.Infof("Interceptor disconnected, resolving held packets")

s.heldHtlcSet.popAll(func(fwd InterceptedForward) {
err := fwd.Resume()
err := fwd.Resume(
fwd.Packet().OutgoingAmount,
fwd.Packet().OutgoingChanID,
fwd.Packet().OnionBlob)
if err != nil {
log.Errorf("Failed to resume hold forward %v", err)
}
Expand All @@ -371,7 +378,7 @@ func (s *InterceptableSwitch) resolve(res *FwdResolution) error {

switch res.Action {
case FwdActionResume:
return intercepted.Resume()
return intercepted.Resume(res.OutgoingAmount, res.OutgoingChanID, res.OnionBlob)

case FwdActionSettle:
return intercepted.Settle(res.Preimage)
Expand Down Expand Up @@ -609,7 +616,12 @@ func (f *interceptedForward) Packet() InterceptedPacket {
}

// Resume resumes the default behavior as if the packet was not intercepted.
func (f *interceptedForward) Resume() error {
func (f *interceptedForward) Resume(outgoingAmount lnwire.MilliSatoshi, outgoingChanID lnwire.ShortChannelID, onionBlob [lnwire.OnionPacketSize]byte) error {
f.htlc.OnionBlob = onionBlob
f.htlc.Amount = outgoingAmount
f.packet.htlc = f.htlc
f.packet.amount = outgoingAmount
f.packet.outgoingChanID = outgoingChanID
// 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)
Expand Down
2 changes: 1 addition & 1 deletion htlcswitch/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ type InterceptedForward interface {
// Resume notifies the intention to resume an existing hold forward. This
// basically means the caller wants to resume with the default behavior for
// this htlc which usually means forward it.
Resume() error
Resume(lnwire.MilliSatoshi, lnwire.ShortChannelID, [lnwire.OnionPacketSize]byte) error

// Settle notifies the intention to settle an existing hold
// forward with a given preimage.
Expand Down
2 changes: 1 addition & 1 deletion intercepted_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (f *interceptedForward) Packet() htlcswitch.InterceptedPacket {
// Resume notifies the intention to resume an existing hold forward. This
// basically means the caller wants to resume with the default behavior for this
// htlc which usually means forward it.
func (f *interceptedForward) Resume() error {
func (f *interceptedForward) Resume(lnwire.MilliSatoshi, lnwire.ShortChannelID, [lnwire.OnionPacketSize]byte) error {
return ErrCannotResume
}

Expand Down
9 changes: 7 additions & 2 deletions lnrpc/routerrpc/forward_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,14 @@ func (r *forwardInterceptor) resolveFromClient(

switch in.Action {
case ResolveHoldForwardAction_RESUME:
var onionBlob [lnwire.OnionPacketSize]byte
copy(onionBlob[:], in.OnionBlob)
return r.htlcSwitch.Resolve(&htlcswitch.FwdResolution{
Key: circuitKey,
Action: htlcswitch.FwdActionResume,
Key: circuitKey,
Action: htlcswitch.FwdActionResume,
OutgoingAmount: lnwire.MilliSatoshi(in.OutgoingAmountMsat),
OutgoingChanID: lnwire.NewShortChanIDFromInt(in.OutgoingRequestedChanId),
OnionBlob: onionBlob,
})

case ResolveHoldForwardAction_FAIL:
Expand Down
361 changes: 199 additions & 162 deletions lnrpc/routerrpc/router.pb.go

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions lnrpc/routerrpc/router.proto
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,16 @@ message ForwardHtlcInterceptResponse {
// For backwards-compatibility reasons, TEMPORARY_CHANNEL_FAILURE is the
// default value for this field.
lnrpc.Failure.FailureCode failure_code = 5;

uint64 outgoing_amount_msat = 6;

// The requested outgoing channel id for this forwarded htlc. Because of
// non-strict forwarding, this isn't necessarily the channel over which the
// packet will be forwarded eventually. A different channel to the same peer
// may be selected as well.
uint64 outgoing_requested_chan_id = 7;

bytes onion_blob = 8;
}

enum ResolveHoldForwardAction {
Expand Down
13 changes: 13 additions & 0 deletions lnrpc/routerrpc/router.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,19 @@
"failure_code": {
"$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."
},
"outgoing_amount_msat": {
"type": "string",
"format": "uint64"
},
"outgoing_requested_chan_id": {
"type": "string",
"format": "uint64",
"description": "The requested outgoing channel id for this forwarded htlc. Because of\nnon-strict forwarding, this isn't necessarily the channel over which the\npacket will be forwarded eventually. A different channel to the same peer\nmay be selected as well."
},
"onion_blob": {
"type": "string",
"format": "byte"
}
},
"description": "*\nForwardHtlcInterceptResponse enables the caller to resolve a previously hold\nforward. The caller can choose either to:\n- `Resume`: Execute the default behavior (usually forward).\n- `Reject`: Fail the htlc backwards.\n- `Settle`: Settle this htlc with a given preimage."
Expand Down

0 comments on commit 5f0a699

Please sign in to comment.