multi: extend QueryRoutes RPC with CLTV limit field

This commit is contained in:
Wilmer Paulino
2019-10-11 15:47:15 -04:00
parent 0fc401de19
commit cbf7e4886a
5 changed files with 334 additions and 295 deletions

View File

@@ -5,6 +5,7 @@ import (
"encoding/hex"
"errors"
"fmt"
math "math"
"time"
"github.com/btcsuite/btcd/btcec"
@@ -169,6 +170,26 @@ func (r *RouterBackend) QueryRoutes(ctx context.Context,
ignoredPairs[pair] = struct{}{}
}
// Since QueryRoutes allows having a different source other than
// ourselves, we'll only apply our max time lock if we are the source.
maxTotalTimelock := r.MaxTotalTimelock
if sourcePubKey != r.SelfNode {
maxTotalTimelock = math.MaxUint32
}
cltvLimit, err := ValidateCLTVLimit(in.CltvLimit, maxTotalTimelock)
if err != nil {
return nil, err
}
// We need to subtract the final delta before passing it into path
// finding. The optimal path is independent of the final cltv delta and
// the path finding algorithm is unaware of this value.
finalCLTVDelta := uint16(zpay32.DefaultFinalCLTVDelta)
if in.FinalCltvDelta != 0 {
finalCLTVDelta = uint16(in.FinalCltvDelta)
}
cltvLimit -= uint32(finalCLTVDelta)
var destTLV map[uint64][]byte
restrictions := &routing.RestrictParams{
FeeLimit: feeLimit,
@@ -196,6 +217,7 @@ func (r *RouterBackend) QueryRoutes(ctx context.Context,
)
},
DestPayloadTLV: len(destTLV) != 0,
CltvLimit: cltvLimit,
}
// If we have any TLV records destined for the final hop, then we'll
@@ -209,24 +231,12 @@ func (r *RouterBackend) QueryRoutes(ctx context.Context,
// Query the channel router for a possible path to the destination that
// can carry `in.Amt` satoshis _including_ the total fee required on
// the route.
var (
route *route.Route
findErr error
route, err := r.FindRoute(
sourcePubKey, targetPubKey, amtMSat, restrictions,
destTlvRecords, finalCLTVDelta,
)
if in.FinalCltvDelta == 0 {
route, findErr = r.FindRoute(
sourcePubKey, targetPubKey, amtMSat, restrictions,
destTlvRecords,
)
} else {
route, findErr = r.FindRoute(
sourcePubKey, targetPubKey, amtMSat, restrictions,
destTlvRecords, uint16(in.FinalCltvDelta),
)
}
if findErr != nil {
return nil, findErr
if err != nil {
return nil, err
}
// For each valid route, we'll convert the result into the format