Skip to content

Commit

Permalink
htlcswitch: expose custom records on intercepted packet
Browse files Browse the repository at this point in the history
  • Loading branch information
joostjager committed Jun 27, 2020
1 parent 363142d commit 2903505
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 165 deletions.
1 change: 1 addition & 0 deletions htlcswitch/interceptable_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (f *interceptedForward) Packet() InterceptedPacket {
OutgoingAmount: f.htlc.Amount,
IncomingAmount: f.packet.incomingAmount,
IncomingExpiry: f.packet.incomingTimeout,
CustomRecords: f.packet.customRecords,
}
}

Expand Down
5 changes: 5 additions & 0 deletions htlcswitch/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record"
)

// InvoiceDatabase is an interface which represents the persistent subsystem
Expand Down Expand Up @@ -226,6 +227,10 @@ type InterceptedPacket struct {

// IncomingAmount is the amount of the accepted htlc.
IncomingAmount lnwire.MilliSatoshi

// CustomRecords are user-defined records in the custom type range that
// were included in the payload.
CustomRecords record.CustomSet
}

// InterceptedForward is passed to the ForwardInterceptor for every forwarded
Expand Down
2 changes: 2 additions & 0 deletions htlcswitch/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -2731,6 +2731,7 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg,
obfuscator: obfuscator,
incomingTimeout: pd.Timeout,
outgoingTimeout: fwdInfo.OutgoingCTLV,
customRecords: pld.CustomRecords(),
}
switchPackets = append(
switchPackets, updatePacket,
Expand Down Expand Up @@ -2794,6 +2795,7 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg,
obfuscator: obfuscator,
incomingTimeout: pd.Timeout,
outgoingTimeout: fwdInfo.OutgoingCTLV,
customRecords: pld.CustomRecords(),
}

fwdPkg.FwdFilter.Set(idx)
Expand Down
5 changes: 5 additions & 0 deletions htlcswitch/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record"
)

// htlcPacket is a wrapper around htlc lnwire update, which adds additional
Expand Down Expand Up @@ -91,6 +92,10 @@ type htlcPacket struct {
// will be extraced from the hop payload recevived by the incoming
// link.
outgoingTimeout uint32

// customRecords are user-defined records in the custom type range that
// were included in the payload.
customRecords record.CustomSet
}

// inKey returns the circuit key used to identify the incoming htlc.
Expand Down
1 change: 1 addition & 0 deletions lnrpc/routerrpc/forward_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func (r *forwardInterceptor) holdAndForwardToClient(
OutgoingExpiry: htlc.OutgoingExpiry,
IncomingAmountMsat: uint64(htlc.IncomingAmount),
IncomingExpiry: htlc.IncomingExpiry,
CustomRecords: htlc.CustomRecords,
}

return r.stream.Send(interceptionRequest)
Expand Down
342 changes: 177 additions & 165 deletions lnrpc/routerrpc/router.pb.go

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions lnrpc/routerrpc/router.proto
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,9 @@ message ForwardHtlcInterceptRequest {

// The outgoing htlc expiry.
uint32 outgoing_expiry = 4;

// Any custom records that were present in the payload.
map<uint64, bytes> custom_records = 8;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions lnrpc/routerrpc/router.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,14 @@
"type": "integer",
"format": "int64",
"description": "The outgoing htlc expiry."
},
"custom_records": {
"type": "object",
"additionalProperties": {
"type": "string",
"format": "byte"
},
"description": "Any custom records that were present in the payload."
}
}
},
Expand Down
14 changes: 14 additions & 0 deletions lntest/itest/lnd_forward_interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import (
"google.golang.org/grpc/status"
)

var (
customTestKey uint64 = 394829
customTestValue = []byte{1, 3, 5}
)

type interceptorTestCase struct {
amountMsat int64
invoice *lnrpc.Invoice
Expand Down Expand Up @@ -149,6 +154,10 @@ func testForwardInterceptor(net *lntest.NetworkHarness, t *harnessTest) {
request.IncomingAmountMsat,
)

value, ok := request.CustomRecords[customTestKey]
require.True(t.t, ok, "expected custom record")
require.Equal(t.t, customTestValue, value)

testCase := testCases[i]

// For held packets we ignore, keeping them in hold status.
Expand Down Expand Up @@ -369,6 +378,11 @@ func (c *interceptorTestContext) sendAliceToCarolPayment(ctx context.Context,
Route: route,
}

// Send a custom record to the forwarding node.
route.Hops[0].CustomRecords = map[uint64][]byte{
customTestKey: customTestValue,
}

// Send the payment.
return c.alice.RouterClient.SendToRouteV2(ctx, sendReq)
}
Expand Down

0 comments on commit 2903505

Please sign in to comment.