2018-10-23 07:45:44 +03:00
|
|
|
// +build routerrpc
|
|
|
|
|
|
|
|
package routerrpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-14 16:19:35 +03:00
|
|
|
"errors"
|
2018-10-23 07:45:44 +03:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcutil"
|
2019-03-22 12:21:25 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch"
|
2018-10-23 07:45:44 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2019-03-22 12:21:25 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2018-10-23 07:45:44 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/lightningnetwork/lnd/routing"
|
2019-04-05 18:36:11 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2019-03-22 12:21:25 +03:00
|
|
|
|
2018-10-23 07:45:44 +03:00
|
|
|
"google.golang.org/grpc"
|
2019-03-22 12:21:25 +03:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2018-10-23 07:45:44 +03:00
|
|
|
"gopkg.in/macaroon-bakery.v2/bakery"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// subServerName is the name of the sub rpc server. We'll use this name
|
|
|
|
// to register ourselves, and we also require that the main
|
|
|
|
// SubServerConfigDispatcher instance recognize as the name of our
|
|
|
|
subServerName = "RouterRPC"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// macaroonOps are the set of capabilities that our minted macaroon (if
|
|
|
|
// it doesn't already exist) will have.
|
|
|
|
macaroonOps = []bakery.Op{
|
2019-03-14 18:31:39 +03:00
|
|
|
{
|
|
|
|
Entity: "offchain",
|
|
|
|
Action: "read",
|
|
|
|
},
|
2018-10-23 07:45:44 +03:00
|
|
|
{
|
|
|
|
Entity: "offchain",
|
|
|
|
Action: "write",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// macPermissions maps RPC calls to the permissions they require.
|
|
|
|
macPermissions = map[string][]bakery.Op{
|
2019-03-14 18:31:39 +03:00
|
|
|
"/routerrpc.Router/SendPayment": {{
|
|
|
|
Entity: "offchain",
|
|
|
|
Action: "write",
|
|
|
|
}},
|
|
|
|
"/routerrpc.Router/SendToRoute": {{
|
2018-10-23 07:45:44 +03:00
|
|
|
Entity: "offchain",
|
|
|
|
Action: "write",
|
|
|
|
}},
|
2019-03-22 12:21:25 +03:00
|
|
|
"/routerrpc.Router/TrackPayment": {{
|
|
|
|
Entity: "offchain",
|
|
|
|
Action: "read",
|
|
|
|
}},
|
2019-03-14 18:31:39 +03:00
|
|
|
"/routerrpc.Router/EstimateRouteFee": {{
|
2018-10-23 07:45:44 +03:00
|
|
|
Entity: "offchain",
|
|
|
|
Action: "read",
|
|
|
|
}},
|
2019-05-10 11:38:31 +03:00
|
|
|
"/routerrpc.Router/QueryMissionControl": {{
|
|
|
|
Entity: "offchain",
|
|
|
|
Action: "read",
|
|
|
|
}},
|
2019-09-27 12:43:12 +03:00
|
|
|
"/routerrpc.Router/QueryProbability": {{
|
|
|
|
Entity: "offchain",
|
|
|
|
Action: "read",
|
|
|
|
}},
|
2019-05-10 19:00:15 +03:00
|
|
|
"/routerrpc.Router/ResetMissionControl": {{
|
|
|
|
Entity: "offchain",
|
|
|
|
Action: "write",
|
|
|
|
}},
|
2019-08-29 14:03:37 +03:00
|
|
|
"/routerrpc.Router/BuildRoute": {{
|
|
|
|
Entity: "offchain",
|
|
|
|
Action: "read",
|
|
|
|
}},
|
2018-10-23 07:45:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultRouterMacFilename is the default name of the router macaroon
|
|
|
|
// that we expect to find via a file handle within the main
|
|
|
|
// configuration file in this package.
|
|
|
|
DefaultRouterMacFilename = "router.macaroon"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server is a stand alone sub RPC server which exposes functionality that
|
|
|
|
// allows clients to route arbitrary payment through the Lightning Network.
|
|
|
|
type Server struct {
|
|
|
|
cfg *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time check to ensure that Server fully implements the RouterServer
|
|
|
|
// gRPC service.
|
|
|
|
var _ RouterServer = (*Server)(nil)
|
|
|
|
|
|
|
|
// fileExists reports whether the named file or directory exists.
|
|
|
|
func fileExists(name string) bool {
|
|
|
|
if _, err := os.Stat(name); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new instance of the RouterServer given a configuration struct
|
|
|
|
// that contains all external dependencies. If the target macaroon exists, and
|
|
|
|
// we're unable to create it, then an error will be returned. We also return
|
|
|
|
// the set of permissions that we require as a server. At the time of writing
|
|
|
|
// of this documentation, this is the same macaroon as as the admin macaroon.
|
|
|
|
func New(cfg *Config) (*Server, lnrpc.MacaroonPerms, error) {
|
|
|
|
// If the path of the router macaroon wasn't generated, then we'll
|
|
|
|
// assume that it's found at the default network directory.
|
|
|
|
if cfg.RouterMacPath == "" {
|
|
|
|
cfg.RouterMacPath = filepath.Join(
|
|
|
|
cfg.NetworkDir, DefaultRouterMacFilename,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we know the full path of the router macaroon, we can check
|
|
|
|
// to see if we need to create it or not.
|
|
|
|
macFilePath := cfg.RouterMacPath
|
|
|
|
if !fileExists(macFilePath) && cfg.MacService != nil {
|
|
|
|
log.Infof("Making macaroons for Router RPC Server at: %v",
|
|
|
|
macFilePath)
|
|
|
|
|
|
|
|
// At this point, we know that the router macaroon doesn't yet,
|
|
|
|
// exist, so we need to create it with the help of the main
|
|
|
|
// macaroon service.
|
|
|
|
routerMac, err := cfg.MacService.Oven.NewMacaroon(
|
|
|
|
context.Background(), bakery.LatestVersion, nil,
|
|
|
|
macaroonOps...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
routerMacBytes, err := routerMac.M().MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(macFilePath, routerMacBytes, 0644)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(macFilePath)
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
routerServer := &Server{
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
|
|
|
|
|
|
|
return routerServer, macPermissions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start launches any helper goroutines required for the rpcServer to function.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the lnrpc.SubServer interface.
|
|
|
|
func (s *Server) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop signals any active goroutines for a graceful closure.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the lnrpc.SubServer interface.
|
|
|
|
func (s *Server) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns a unique string representation of the sub-server. This can be
|
|
|
|
// used to identify the sub-server and also de-duplicate them.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the lnrpc.SubServer interface.
|
|
|
|
func (s *Server) Name() string {
|
|
|
|
return subServerName
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterWithRootServer will be called by the root gRPC server to direct a
|
|
|
|
// sub RPC server to register itself with the main gRPC root server. Until this
|
|
|
|
// is called, each sub-server won't be able to have requests routed towards it.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the lnrpc.SubServer interface.
|
|
|
|
func (s *Server) RegisterWithRootServer(grpcServer *grpc.Server) error {
|
|
|
|
// We make sure that we register it with the main gRPC server to ensure
|
|
|
|
// all our methods are routed properly.
|
|
|
|
RegisterRouterServer(grpcServer, s)
|
|
|
|
|
|
|
|
log.Debugf("Router RPC server successfully register with root gRPC " +
|
|
|
|
"server")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendPayment attempts to route a payment described by the passed
|
|
|
|
// PaymentRequest to the final destination. If we are unable to route the
|
|
|
|
// payment, or cannot find a route that satisfies the constraints in the
|
|
|
|
// PaymentRequest, then an error will be returned. Otherwise, the payment
|
|
|
|
// pre-image, along with the final route will be returned.
|
2019-03-22 12:21:25 +03:00
|
|
|
func (s *Server) SendPayment(req *SendPaymentRequest,
|
|
|
|
stream Router_SendPaymentServer) error {
|
2018-10-23 07:45:44 +03:00
|
|
|
|
2019-04-18 10:34:28 +03:00
|
|
|
payment, err := s.cfg.RouterBackend.extractIntentFromSendRequest(req)
|
2018-10-23 07:45:44 +03:00
|
|
|
if err != nil {
|
2019-03-22 12:21:25 +03:00
|
|
|
return err
|
2018-10-23 07:45:44 +03:00
|
|
|
}
|
|
|
|
|
2019-03-22 12:21:25 +03:00
|
|
|
err = s.cfg.Router.SendPaymentAsync(payment)
|
2018-10-23 07:45:44 +03:00
|
|
|
if err != nil {
|
2019-03-22 12:21:25 +03:00
|
|
|
// Transform user errors to grpc code.
|
|
|
|
if err == channeldb.ErrPaymentInFlight ||
|
|
|
|
err == channeldb.ErrAlreadyPaid {
|
|
|
|
|
|
|
|
log.Debugf("SendPayment async result for hash %x: %v",
|
|
|
|
payment.PaymentHash, err)
|
|
|
|
|
|
|
|
return status.Error(
|
|
|
|
codes.AlreadyExists, err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Errorf("SendPayment async error for hash %x: %v",
|
|
|
|
payment.PaymentHash, err)
|
|
|
|
|
|
|
|
return err
|
2018-10-23 07:45:44 +03:00
|
|
|
}
|
|
|
|
|
2019-03-22 12:21:25 +03:00
|
|
|
return s.trackPayment(payment.PaymentHash, stream)
|
2018-10-23 07:45:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// EstimateRouteFee allows callers to obtain a lower bound w.r.t how much it
|
|
|
|
// may cost to send an HTLC to the target end destination.
|
|
|
|
func (s *Server) EstimateRouteFee(ctx context.Context,
|
|
|
|
req *RouteFeeRequest) (*RouteFeeResponse, error) {
|
|
|
|
|
2019-03-14 16:19:35 +03:00
|
|
|
if len(req.Dest) != 33 {
|
|
|
|
return nil, errors.New("invalid length destination key")
|
2018-10-23 07:45:44 +03:00
|
|
|
}
|
2019-04-05 18:36:11 +03:00
|
|
|
var destNode route.Vertex
|
2019-03-14 16:19:35 +03:00
|
|
|
copy(destNode[:], req.Dest)
|
2018-10-23 07:45:44 +03:00
|
|
|
|
|
|
|
// Next, we'll convert the amount in satoshis to mSAT, which are the
|
|
|
|
// native unit of LN.
|
|
|
|
amtMsat := lnwire.NewMSatFromSatoshis(btcutil.Amount(req.AmtSat))
|
|
|
|
|
2019-03-14 16:19:35 +03:00
|
|
|
// Pick a fee limit
|
|
|
|
//
|
|
|
|
// TODO: Change this into behaviour that makes more sense.
|
|
|
|
feeLimit := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
|
|
|
|
|
2018-10-23 07:45:44 +03:00
|
|
|
// Finally, we'll query for a route to the destination that can carry
|
2019-10-11 22:46:10 +03:00
|
|
|
// that target amount, we'll only request a single route. Set a
|
|
|
|
// restriction for the default CLTV limit, otherwise we can find a route
|
|
|
|
// that exceeds it and is useless to us.
|
2019-05-07 18:01:01 +03:00
|
|
|
route, err := s.cfg.Router.FindRoute(
|
2019-03-14 16:19:35 +03:00
|
|
|
s.cfg.RouterBackend.SelfNode, destNode, amtMsat,
|
|
|
|
&routing.RestrictParams{
|
2019-10-11 22:46:10 +03:00
|
|
|
FeeLimit: feeLimit,
|
|
|
|
CltvLimit: s.cfg.RouterBackend.MaxTotalTimelock,
|
2019-07-31 07:44:02 +03:00
|
|
|
}, nil,
|
2018-10-23 07:45:44 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &RouteFeeResponse{
|
2019-05-15 15:37:58 +03:00
|
|
|
RoutingFeeMsat: int64(route.TotalFees()),
|
2019-05-07 18:01:01 +03:00
|
|
|
TimeLockDelay: int64(route.TotalTimeLock),
|
2018-10-23 07:45:44 +03:00
|
|
|
}, nil
|
|
|
|
}
|
2019-03-14 18:31:39 +03:00
|
|
|
|
|
|
|
// SendToRoute sends a payment through a predefined route. The response of this
|
|
|
|
// call contains structured error information.
|
|
|
|
func (s *Server) SendToRoute(ctx context.Context,
|
|
|
|
req *SendToRouteRequest) (*SendToRouteResponse, error) {
|
|
|
|
|
|
|
|
if req.Route == nil {
|
|
|
|
return nil, fmt.Errorf("unable to send, no routes provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
route, err := s.cfg.RouterBackend.UnmarshallRoute(req.Route)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := lntypes.MakeHash(req.PaymentHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
preimage, err := s.cfg.Router.SendToRoute(hash, route)
|
|
|
|
|
|
|
|
// In the success case, return the preimage.
|
|
|
|
if err == nil {
|
|
|
|
return &SendToRouteResponse{
|
|
|
|
Preimage: preimage[:],
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the failure case, marshall the failure message to the rpc format
|
|
|
|
// before returning it to the caller.
|
|
|
|
rpcErr, err := marshallError(err)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &SendToRouteResponse{
|
|
|
|
Failure: rpcErr,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshallError marshall an error as received from the switch to rpc structs
|
|
|
|
// suitable for returning to the caller of an rpc method.
|
|
|
|
//
|
|
|
|
// Because of difficulties with using protobuf oneof constructs in some
|
|
|
|
// languages, the decision was made here to use a single message format for all
|
|
|
|
// failure messages with some fields left empty depending on the failure type.
|
|
|
|
func marshallError(sendError error) (*Failure, error) {
|
|
|
|
response := &Failure{}
|
|
|
|
|
2019-06-19 12:12:10 +03:00
|
|
|
if sendError == htlcswitch.ErrUnreadableFailureMessage {
|
|
|
|
response.Code = Failure_UNREADABLE_FAILURE
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2020-01-14 16:07:42 +03:00
|
|
|
rtErr, ok := sendError.(htlcswitch.ClearTextError)
|
2019-03-14 18:31:39 +03:00
|
|
|
if !ok {
|
|
|
|
return nil, sendError
|
|
|
|
}
|
|
|
|
|
2020-01-14 16:07:42 +03:00
|
|
|
switch onionErr := rtErr.WireMessage().(type) {
|
2019-03-14 18:31:39 +03:00
|
|
|
|
2019-06-12 12:35:54 +03:00
|
|
|
case *lnwire.FailIncorrectDetails:
|
|
|
|
response.Code = Failure_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS
|
2019-06-12 13:18:58 +03:00
|
|
|
response.Height = onionErr.Height()
|
2019-03-14 18:31:39 +03:00
|
|
|
|
|
|
|
case *lnwire.FailIncorrectPaymentAmount:
|
|
|
|
response.Code = Failure_INCORRECT_PAYMENT_AMOUNT
|
|
|
|
|
|
|
|
case *lnwire.FailFinalIncorrectCltvExpiry:
|
|
|
|
response.Code = Failure_FINAL_INCORRECT_CLTV_EXPIRY
|
|
|
|
response.CltvExpiry = onionErr.CltvExpiry
|
|
|
|
|
|
|
|
case *lnwire.FailFinalIncorrectHtlcAmount:
|
|
|
|
response.Code = Failure_FINAL_INCORRECT_HTLC_AMOUNT
|
|
|
|
response.HtlcMsat = uint64(onionErr.IncomingHTLCAmount)
|
|
|
|
|
|
|
|
case *lnwire.FailFinalExpiryTooSoon:
|
|
|
|
response.Code = Failure_FINAL_EXPIRY_TOO_SOON
|
|
|
|
|
|
|
|
case *lnwire.FailInvalidRealm:
|
|
|
|
response.Code = Failure_INVALID_REALM
|
|
|
|
|
|
|
|
case *lnwire.FailExpiryTooSoon:
|
|
|
|
response.Code = Failure_EXPIRY_TOO_SOON
|
|
|
|
response.ChannelUpdate = marshallChannelUpdate(&onionErr.Update)
|
|
|
|
|
2019-10-10 21:25:34 +03:00
|
|
|
case *lnwire.FailExpiryTooFar:
|
|
|
|
response.Code = Failure_EXPIRY_TOO_FAR
|
|
|
|
|
2019-03-14 18:31:39 +03:00
|
|
|
case *lnwire.FailInvalidOnionVersion:
|
|
|
|
response.Code = Failure_INVALID_ONION_VERSION
|
|
|
|
response.OnionSha_256 = onionErr.OnionSHA256[:]
|
|
|
|
|
|
|
|
case *lnwire.FailInvalidOnionHmac:
|
|
|
|
response.Code = Failure_INVALID_ONION_HMAC
|
|
|
|
response.OnionSha_256 = onionErr.OnionSHA256[:]
|
|
|
|
|
|
|
|
case *lnwire.FailInvalidOnionKey:
|
|
|
|
response.Code = Failure_INVALID_ONION_KEY
|
|
|
|
response.OnionSha_256 = onionErr.OnionSHA256[:]
|
|
|
|
|
|
|
|
case *lnwire.FailAmountBelowMinimum:
|
|
|
|
response.Code = Failure_AMOUNT_BELOW_MINIMUM
|
|
|
|
response.ChannelUpdate = marshallChannelUpdate(&onionErr.Update)
|
|
|
|
response.HtlcMsat = uint64(onionErr.HtlcMsat)
|
|
|
|
|
|
|
|
case *lnwire.FailFeeInsufficient:
|
|
|
|
response.Code = Failure_FEE_INSUFFICIENT
|
|
|
|
response.ChannelUpdate = marshallChannelUpdate(&onionErr.Update)
|
|
|
|
response.HtlcMsat = uint64(onionErr.HtlcMsat)
|
|
|
|
|
|
|
|
case *lnwire.FailIncorrectCltvExpiry:
|
|
|
|
response.Code = Failure_INCORRECT_CLTV_EXPIRY
|
|
|
|
response.ChannelUpdate = marshallChannelUpdate(&onionErr.Update)
|
|
|
|
response.CltvExpiry = onionErr.CltvExpiry
|
|
|
|
|
|
|
|
case *lnwire.FailChannelDisabled:
|
|
|
|
response.Code = Failure_CHANNEL_DISABLED
|
|
|
|
response.ChannelUpdate = marshallChannelUpdate(&onionErr.Update)
|
|
|
|
response.Flags = uint32(onionErr.Flags)
|
|
|
|
|
|
|
|
case *lnwire.FailTemporaryChannelFailure:
|
|
|
|
response.Code = Failure_TEMPORARY_CHANNEL_FAILURE
|
|
|
|
response.ChannelUpdate = marshallChannelUpdate(onionErr.Update)
|
|
|
|
|
|
|
|
case *lnwire.FailRequiredNodeFeatureMissing:
|
|
|
|
response.Code = Failure_REQUIRED_NODE_FEATURE_MISSING
|
|
|
|
|
|
|
|
case *lnwire.FailRequiredChannelFeatureMissing:
|
|
|
|
response.Code = Failure_REQUIRED_CHANNEL_FEATURE_MISSING
|
|
|
|
|
|
|
|
case *lnwire.FailUnknownNextPeer:
|
|
|
|
response.Code = Failure_UNKNOWN_NEXT_PEER
|
|
|
|
|
|
|
|
case *lnwire.FailTemporaryNodeFailure:
|
|
|
|
response.Code = Failure_TEMPORARY_NODE_FAILURE
|
|
|
|
|
|
|
|
case *lnwire.FailPermanentNodeFailure:
|
|
|
|
response.Code = Failure_PERMANENT_NODE_FAILURE
|
|
|
|
|
|
|
|
case *lnwire.FailPermanentChannelFailure:
|
|
|
|
response.Code = Failure_PERMANENT_CHANNEL_FAILURE
|
2019-10-10 21:25:34 +03:00
|
|
|
|
2019-12-20 13:25:08 +03:00
|
|
|
case *lnwire.FailMPPTimeout:
|
|
|
|
response.Code = Failure_MPP_TIMEOUT
|
|
|
|
|
2019-10-10 21:25:34 +03:00
|
|
|
case nil:
|
2019-06-19 16:09:23 +03:00
|
|
|
response.Code = Failure_UNKNOWN_FAILURE
|
2019-10-10 21:25:34 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("cannot marshall failure %T", onionErr)
|
2019-03-14 18:31:39 +03:00
|
|
|
}
|
|
|
|
|
2020-01-14 16:07:42 +03:00
|
|
|
// If the ClearTextError received is a ForwardingError, the error
|
|
|
|
// originated from a node along the route, not locally on our outgoing
|
|
|
|
// link. We set failureSourceIdx to the index of the node where the
|
|
|
|
// failure occurred. If the error is not a ForwardingError, the failure
|
|
|
|
// occurred at our node, so we leave the index as 0 to indicate that
|
|
|
|
// we failed locally.
|
|
|
|
fErr, ok := rtErr.(*htlcswitch.ForwardingError)
|
|
|
|
if ok {
|
|
|
|
response.FailureSourceIndex = uint32(fErr.FailureSourceIdx)
|
|
|
|
}
|
2019-03-14 18:31:39 +03:00
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshallChannelUpdate marshalls a channel update as received over the wire to
|
|
|
|
// the router rpc format.
|
|
|
|
func marshallChannelUpdate(update *lnwire.ChannelUpdate) *ChannelUpdate {
|
|
|
|
if update == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ChannelUpdate{
|
|
|
|
Signature: update.Signature[:],
|
|
|
|
ChainHash: update.ChainHash[:],
|
|
|
|
ChanId: update.ShortChannelID.ToUint64(),
|
|
|
|
Timestamp: update.Timestamp,
|
2019-05-24 10:08:07 +03:00
|
|
|
MessageFlags: uint32(update.MessageFlags),
|
2019-03-14 18:31:39 +03:00
|
|
|
ChannelFlags: uint32(update.ChannelFlags),
|
|
|
|
TimeLockDelta: uint32(update.TimeLockDelta),
|
|
|
|
HtlcMinimumMsat: uint64(update.HtlcMinimumMsat),
|
|
|
|
BaseFee: update.BaseFee,
|
|
|
|
FeeRate: update.FeeRate,
|
2019-05-24 10:08:07 +03:00
|
|
|
HtlcMaximumMsat: uint64(update.HtlcMaximumMsat),
|
|
|
|
ExtraOpaqueData: update.ExtraOpaqueData,
|
2019-03-14 18:31:39 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-10 19:00:15 +03:00
|
|
|
|
|
|
|
// ResetMissionControl clears all mission control state and starts with a clean
|
|
|
|
// slate.
|
|
|
|
func (s *Server) ResetMissionControl(ctx context.Context,
|
|
|
|
req *ResetMissionControlRequest) (*ResetMissionControlResponse, error) {
|
|
|
|
|
2019-06-26 14:00:35 +03:00
|
|
|
err := s.cfg.RouterBackend.MissionControl.ResetHistory()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-10 19:00:15 +03:00
|
|
|
|
|
|
|
return &ResetMissionControlResponse{}, nil
|
|
|
|
}
|
2019-05-10 11:38:31 +03:00
|
|
|
|
|
|
|
// QueryMissionControl exposes the internal mission control state to callers. It
|
|
|
|
// is a development feature.
|
|
|
|
func (s *Server) QueryMissionControl(ctx context.Context,
|
|
|
|
req *QueryMissionControlRequest) (*QueryMissionControlResponse, error) {
|
|
|
|
|
|
|
|
snapshot := s.cfg.RouterBackend.MissionControl.GetHistorySnapshot()
|
|
|
|
|
2019-07-29 16:10:58 +03:00
|
|
|
rpcPairs := make([]*PairHistory, 0, len(snapshot.Pairs))
|
|
|
|
for _, p := range snapshot.Pairs {
|
|
|
|
// Prevent binding to loop variable.
|
|
|
|
pair := p
|
|
|
|
|
|
|
|
rpcPair := PairHistory{
|
2019-09-30 10:12:50 +03:00
|
|
|
NodeFrom: pair.Pair.From[:],
|
|
|
|
NodeTo: pair.Pair.To[:],
|
|
|
|
History: toRPCPairData(&pair.TimedPairResult),
|
2019-05-10 11:38:31 +03:00
|
|
|
}
|
2019-07-29 16:10:58 +03:00
|
|
|
|
|
|
|
rpcPairs = append(rpcPairs, &rpcPair)
|
2019-05-10 11:38:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
response := QueryMissionControlResponse{
|
2019-07-29 16:10:58 +03:00
|
|
|
Pairs: rpcPairs,
|
2019-05-10 11:38:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return &response, nil
|
|
|
|
}
|
2019-03-22 12:21:25 +03:00
|
|
|
|
2019-09-30 10:12:50 +03:00
|
|
|
// toRPCPairData marshalls mission control pair data to the rpc struct.
|
|
|
|
func toRPCPairData(data *routing.TimedPairResult) *PairData {
|
|
|
|
rpcData := PairData{
|
2019-09-26 18:04:02 +03:00
|
|
|
FailAmtSat: int64(data.FailAmt.ToSatoshis()),
|
|
|
|
FailAmtMsat: int64(data.FailAmt),
|
|
|
|
SuccessAmtSat: int64(data.SuccessAmt.ToSatoshis()),
|
|
|
|
SuccessAmtMsat: int64(data.SuccessAmt),
|
2019-09-30 10:12:50 +03:00
|
|
|
}
|
|
|
|
|
2019-09-26 18:04:02 +03:00
|
|
|
if !data.FailTime.IsZero() {
|
|
|
|
rpcData.FailTime = data.FailTime.Unix()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !data.SuccessTime.IsZero() {
|
|
|
|
rpcData.SuccessTime = data.SuccessTime.Unix()
|
2019-09-30 10:12:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return &rpcData
|
|
|
|
}
|
|
|
|
|
2019-09-27 12:43:12 +03:00
|
|
|
// QueryProbability returns the current success probability estimate for a
|
|
|
|
// given node pair and amount.
|
|
|
|
func (s *Server) QueryProbability(ctx context.Context,
|
|
|
|
req *QueryProbabilityRequest) (*QueryProbabilityResponse, error) {
|
|
|
|
|
|
|
|
fromNode, err := route.NewVertexFromBytes(req.FromNode)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
toNode, err := route.NewVertexFromBytes(req.ToNode)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
amt := lnwire.MilliSatoshi(req.AmtMsat)
|
|
|
|
|
|
|
|
mc := s.cfg.RouterBackend.MissionControl
|
|
|
|
prob := mc.GetProbability(fromNode, toNode, amt)
|
|
|
|
history := mc.GetPairHistorySnapshot(fromNode, toNode)
|
|
|
|
|
|
|
|
return &QueryProbabilityResponse{
|
|
|
|
Probability: prob,
|
|
|
|
History: toRPCPairData(&history),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-22 12:21:25 +03:00
|
|
|
// TrackPayment returns a stream of payment state updates. The stream is
|
|
|
|
// closed when the payment completes.
|
|
|
|
func (s *Server) TrackPayment(request *TrackPaymentRequest,
|
|
|
|
stream Router_TrackPaymentServer) error {
|
|
|
|
|
|
|
|
paymentHash, err := lntypes.MakeHash(request.PaymentHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("TrackPayment called for payment %v", paymentHash)
|
|
|
|
|
|
|
|
return s.trackPayment(paymentHash, stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
// trackPayment writes payment status updates to the provided stream.
|
|
|
|
func (s *Server) trackPayment(paymentHash lntypes.Hash,
|
|
|
|
stream Router_TrackPaymentServer) error {
|
|
|
|
|
2019-06-04 18:18:41 +03:00
|
|
|
router := s.cfg.RouterBackend
|
|
|
|
|
2019-03-22 12:21:25 +03:00
|
|
|
// Subscribe to the outcome of this payment.
|
2019-06-04 18:18:41 +03:00
|
|
|
inFlight, resultChan, err := router.Tower.SubscribePayment(
|
2019-03-22 12:21:25 +03:00
|
|
|
paymentHash,
|
|
|
|
)
|
|
|
|
switch {
|
|
|
|
case err == channeldb.ErrPaymentNotInitiated:
|
|
|
|
return status.Error(codes.NotFound, err.Error())
|
|
|
|
case err != nil:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it is in flight, send a state update to the client. Payment status
|
|
|
|
// update streams are expected to always send the current payment state
|
|
|
|
// immediately.
|
|
|
|
if inFlight {
|
|
|
|
err = stream.Send(&PaymentStatus{
|
|
|
|
State: PaymentState_IN_FLIGHT,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the outcome of the payment. For payments that have
|
|
|
|
// completed, the result should already be waiting on the channel.
|
|
|
|
select {
|
|
|
|
case result := <-resultChan:
|
|
|
|
// Marshall result to rpc type.
|
|
|
|
var status PaymentStatus
|
|
|
|
if result.Success {
|
|
|
|
log.Debugf("Payment %v successfully completed",
|
|
|
|
paymentHash)
|
|
|
|
|
|
|
|
status.State = PaymentState_SUCCEEDED
|
|
|
|
status.Preimage = result.Preimage[:]
|
|
|
|
} else {
|
2019-06-04 12:22:23 +03:00
|
|
|
state, err := marshallFailureReason(
|
|
|
|
result.FailureReason,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-03-22 12:21:25 +03:00
|
|
|
}
|
2019-06-04 12:22:23 +03:00
|
|
|
status.State = state
|
2019-11-08 14:39:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the last route from the given list of HTLCs. This
|
|
|
|
// will populate the legacy route field for backwards
|
|
|
|
// compatibility.
|
|
|
|
//
|
|
|
|
// NOTE: For now there will be at most one HTLC, this code
|
|
|
|
// should be revisted or the field removed when multiple HTLCs
|
|
|
|
// are permitted.
|
|
|
|
var legacyRoute *route.Route
|
|
|
|
for _, htlc := range result.HTLCs {
|
|
|
|
switch {
|
|
|
|
case htlc.Settle != nil:
|
|
|
|
legacyRoute = &htlc.Route
|
|
|
|
|
|
|
|
// Only display the route for failed payments if we got
|
|
|
|
// an incorrect payment details error, so that it can be
|
|
|
|
// used for probing or fee estimation.
|
|
|
|
case htlc.Failure != nil && result.FailureReason ==
|
|
|
|
channeldb.FailureReasonPaymentDetails:
|
|
|
|
|
|
|
|
legacyRoute = &htlc.Route
|
2019-03-22 12:21:25 +03:00
|
|
|
}
|
|
|
|
}
|
2019-11-08 14:39:51 +03:00
|
|
|
if legacyRoute != nil {
|
|
|
|
status.Route, err = router.MarshallRoute(legacyRoute)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal our list of HTLCs that have been tried for this
|
|
|
|
// payment.
|
|
|
|
htlcs := make([]*lnrpc.HTLCAttempt, 0, len(result.HTLCs))
|
|
|
|
for _, dbHtlc := range result.HTLCs {
|
|
|
|
htlc, err := router.MarshalHTLCAttempt(dbHtlc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
htlcs = append(htlcs, htlc)
|
|
|
|
}
|
|
|
|
status.Htlcs = htlcs
|
2019-03-22 12:21:25 +03:00
|
|
|
|
|
|
|
// Send event to the client.
|
|
|
|
err = stream.Send(&status)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-stream.Context().Done():
|
|
|
|
log.Debugf("Payment status stream %v canceled", paymentHash)
|
|
|
|
return stream.Context().Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-04 12:22:23 +03:00
|
|
|
|
|
|
|
// marshallFailureReason marshalls the failure reason to the corresponding rpc
|
|
|
|
// type.
|
|
|
|
func marshallFailureReason(reason channeldb.FailureReason) (
|
|
|
|
PaymentState, error) {
|
|
|
|
|
|
|
|
switch reason {
|
|
|
|
|
|
|
|
case channeldb.FailureReasonTimeout:
|
|
|
|
return PaymentState_FAILED_TIMEOUT, nil
|
|
|
|
|
|
|
|
case channeldb.FailureReasonNoRoute:
|
|
|
|
return PaymentState_FAILED_NO_ROUTE, nil
|
|
|
|
|
|
|
|
case channeldb.FailureReasonError:
|
|
|
|
return PaymentState_FAILED_ERROR, nil
|
|
|
|
|
2019-11-20 07:42:21 +03:00
|
|
|
case channeldb.FailureReasonPaymentDetails:
|
2019-06-04 12:22:23 +03:00
|
|
|
return PaymentState_FAILED_INCORRECT_PAYMENT_DETAILS, nil
|
2019-11-21 13:59:17 +03:00
|
|
|
|
|
|
|
case channeldb.FailureReasonInsufficientBalance:
|
|
|
|
return PaymentState_FAILED_INSUFFICIENT_BALANCE, nil
|
2019-06-04 12:22:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0, errors.New("unknown failure reason")
|
|
|
|
}
|
2019-08-29 14:03:37 +03:00
|
|
|
|
|
|
|
// BuildRoute builds a route from a list of hop addresses.
|
|
|
|
func (s *Server) BuildRoute(ctx context.Context,
|
|
|
|
req *BuildRouteRequest) (*BuildRouteResponse, error) {
|
|
|
|
|
|
|
|
// Unmarshall hop list.
|
|
|
|
hops := make([]route.Vertex, len(req.HopPubkeys))
|
|
|
|
for i, pubkeyBytes := range req.HopPubkeys {
|
|
|
|
pubkey, err := route.NewVertexFromBytes(pubkeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hops[i] = pubkey
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare BuildRoute call parameters from rpc request.
|
|
|
|
var amt *lnwire.MilliSatoshi
|
|
|
|
if req.AmtMsat != 0 {
|
|
|
|
rpcAmt := lnwire.MilliSatoshi(req.AmtMsat)
|
|
|
|
amt = &rpcAmt
|
|
|
|
}
|
|
|
|
|
|
|
|
var outgoingChan *uint64
|
|
|
|
if req.OutgoingChanId != 0 {
|
|
|
|
outgoingChan = &req.OutgoingChanId
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the route and return it to the caller.
|
|
|
|
route, err := s.cfg.Router.BuildRoute(
|
|
|
|
amt, hops, outgoingChan, req.FinalCltvDelta,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcRoute, err := s.cfg.RouterBackend.MarshallRoute(route)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
routeResp := &BuildRouteResponse{
|
|
|
|
Route: rpcRoute,
|
|
|
|
}
|
|
|
|
|
|
|
|
return routeResp, nil
|
|
|
|
}
|