Merge pull request #3819 from joostjager/fix-custom-record-payment
multi: fix custom record payment
This commit is contained in:
23 files changed
+447
-204
No files matched your search
@@ -13,14 +13,12 @@ import (
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcutil"
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
||||
"github.com/lightningnetwork/lnd/lnrpc"
|
||||
"github.com/lightningnetwork/lnd/lntypes"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
"github.com/lightningnetwork/lnd/record"
|
||||
"github.com/lightningnetwork/lnd/routing"
|
||||
"github.com/lightningnetwork/lnd/routing/route"
|
||||
"github.com/lightningnetwork/lnd/tlv"
|
||||
"github.com/lightningnetwork/lnd/zpay32"
|
||||
)
|
||||
|
||||
@@ -46,7 +44,7 @@ type RouterBackend struct {
|
||||
// routes.
|
||||
FindRoute func(source, target route.Vertex,
|
||||
amt lnwire.MilliSatoshi, restrictions *routing.RestrictParams,
|
||||
destTlvRecords []tlv.Record,
|
||||
destCustomRecords record.CustomSet,
|
||||
finalExpiry ...uint16) (*route.Route, error)
|
||||
|
||||
MissionControl MissionControl
|
||||
@@ -226,14 +224,14 @@ func (r *RouterBackend) QueryRoutes(ctx context.Context,
|
||||
fromNode, toNode, amt,
|
||||
)
|
||||
},
|
||||
DestPayloadTLV: len(in.DestCustomRecords) != 0,
|
||||
CltvLimit: cltvLimit,
|
||||
DestCustomRecords: record.CustomSet(in.DestCustomRecords),
|
||||
CltvLimit: cltvLimit,
|
||||
}
|
||||
|
||||
// If we have any TLV records destined for the final hop, then we'll
|
||||
// attempt to decode them now into a form that the router can more
|
||||
// easily manipulate.
|
||||
destTlvRecords, err := UnmarshallCustomRecords(in.DestCustomRecords)
|
||||
err = ValidateCustomRecords(in.DestCustomRecords)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -243,7 +241,7 @@ func (r *RouterBackend) QueryRoutes(ctx context.Context,
|
||||
// the route.
|
||||
route, err := r.FindRoute(
|
||||
sourcePubKey, targetPubKey, amt, restrictions,
|
||||
destTlvRecords, finalCLTVDelta,
|
||||
in.DestCustomRecords, finalCLTVDelta,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -347,11 +345,6 @@ func (r *RouterBackend) MarshallRoute(route *route.Route) (*lnrpc.Route, error)
|
||||
}
|
||||
}
|
||||
|
||||
tlvMap, err := tlv.RecordsToMap(hop.TLVRecords)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp.Hops[i] = &lnrpc.Hop{
|
||||
ChanId: hop.ChannelID,
|
||||
ChanCapacity: int64(chanCapacity),
|
||||
@@ -363,7 +356,7 @@ func (r *RouterBackend) MarshallRoute(route *route.Route) (*lnrpc.Route, error)
|
||||
PubKey: hex.EncodeToString(
|
||||
hop.PubKeyBytes[:],
|
||||
),
|
||||
CustomRecords: tlvMap,
|
||||
CustomRecords: hop.CustomRecords,
|
||||
TlvPayload: !hop.LegacyPayload,
|
||||
MppRecord: mpp,
|
||||
}
|
||||
@@ -373,24 +366,16 @@ func (r *RouterBackend) MarshallRoute(route *route.Route) (*lnrpc.Route, error)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// UnmarshallCustomRecords unmarshall rpc custom records to tlv records.
|
||||
func UnmarshallCustomRecords(rpcRecords map[uint64][]byte) ([]tlv.Record,
|
||||
error) {
|
||||
|
||||
if len(rpcRecords) == 0 {
|
||||
return nil, nil
|
||||
// ValidateCustomRecords checks that all custom records are in the custom type
|
||||
// range.
|
||||
func ValidateCustomRecords(rpcRecords map[uint64][]byte) error {
|
||||
for key := range rpcRecords {
|
||||
if key < record.CustomTypeStart {
|
||||
return fmt.Errorf("no custom records with types "+
|
||||
"below %v allowed", record.CustomTypeStart)
|
||||
}
|
||||
}
|
||||
|
||||
tlvRecords := tlv.MapToRecords(rpcRecords)
|
||||
|
||||
// tlvRecords is sorted, so we only need to check that the first
|
||||
// element is within the custom range.
|
||||
if uint64(tlvRecords[0].Type()) < hop.CustomTypeStart {
|
||||
return nil, fmt.Errorf("no custom records with types "+
|
||||
"below %v allowed", hop.CustomTypeStart)
|
||||
}
|
||||
|
||||
return tlvRecords, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshallHopWithPubkey unmarshalls an rpc hop for which the pubkey has
|
||||
@@ -398,7 +383,7 @@ func UnmarshallCustomRecords(rpcRecords map[uint64][]byte) ([]tlv.Record,
|
||||
func UnmarshallHopWithPubkey(rpcHop *lnrpc.Hop, pubkey route.Vertex) (*route.Hop,
|
||||
error) {
|
||||
|
||||
tlvRecords, err := UnmarshallCustomRecords(rpcHop.CustomRecords)
|
||||
err := ValidateCustomRecords(rpcHop.CustomRecords)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -413,7 +398,7 @@ func UnmarshallHopWithPubkey(rpcHop *lnrpc.Hop, pubkey route.Vertex) (*route.Hop
|
||||
AmtToForward: lnwire.MilliSatoshi(rpcHop.AmtToForwardMsat),
|
||||
PubKeyBytes: pubkey,
|
||||
ChannelID: rpcHop.ChanId,
|
||||
TLVRecords: tlvRecords,
|
||||
CustomRecords: rpcHop.CustomRecords,
|
||||
LegacyPayload: !rpcHop.TlvPayload,
|
||||
MPP: mpp,
|
||||
}, nil
|
||||
@@ -541,12 +526,11 @@ func (r *RouterBackend) extractIntentFromSendRequest(
|
||||
return nil, errors.New("timeout_seconds must be specified")
|
||||
}
|
||||
|
||||
payIntent.FinalDestRecords, err = UnmarshallCustomRecords(
|
||||
rpcPayReq.DestCustomRecords,
|
||||
)
|
||||
err = ValidateCustomRecords(rpcPayReq.DestCustomRecords)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
payIntent.DestCustomRecords = rpcPayReq.DestCustomRecords
|
||||
|
||||
payIntent.PayAttemptTimeout = time.Second *
|
||||
time.Duration(rpcPayReq.TimeoutSeconds)
|
||||
|
||||
@@ -8,9 +8,9 @@ import (
|
||||
|
||||
"github.com/btcsuite/btcutil"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
"github.com/lightningnetwork/lnd/record"
|
||||
"github.com/lightningnetwork/lnd/routing"
|
||||
"github.com/lightningnetwork/lnd/routing/route"
|
||||
"github.com/lightningnetwork/lnd/tlv"
|
||||
|
||||
"github.com/lightningnetwork/lnd/lnrpc"
|
||||
)
|
||||
@@ -92,7 +92,7 @@ func testQueryRoutes(t *testing.T, useMissionControl bool, useMsat bool) {
|
||||
|
||||
findRoute := func(source, target route.Vertex,
|
||||
amt lnwire.MilliSatoshi, restrictions *routing.RestrictParams,
|
||||
_ []tlv.Record,
|
||||
_ record.CustomSet,
|
||||
finalExpiry ...uint16) (*route.Route, error) {
|
||||
|
||||
if int64(amt) != amtSat*1000 {
|
||||
|
||||
Reference in new issue
Block a user