2018-10-23 07:45:44 +03:00
|
|
|
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"
|
2020-03-13 10:30:16 +03:00
|
|
|
"sync/atomic"
|
2021-01-19 11:57:15 +03:00
|
|
|
"time"
|
2018-10-23 07:45:44 +03:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcutil"
|
2020-04-02 16:12:28 +03:00
|
|
|
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
2019-03-22 12:21:25 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
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"
|
2020-07-23 19:36:42 +03:00
|
|
|
"github.com/lightningnetwork/lnd/macaroons"
|
2018-10-23 07:45:44 +03:00
|
|
|
"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 (
|
2020-03-13 10:30:16 +03:00
|
|
|
errServerShuttingDown = errors.New("routerrpc server shutting down")
|
|
|
|
|
2020-05-19 12:57:36 +03:00
|
|
|
// ErrInterceptorAlreadyExists is an error returned when the a new stream
|
|
|
|
// is opened and there is already one active interceptor.
|
|
|
|
// The user must disconnect prior to open another stream.
|
|
|
|
ErrInterceptorAlreadyExists = errors.New("interceptor already exists")
|
|
|
|
|
2018-10-23 07:45:44 +03:00
|
|
|
// 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{
|
2020-04-13 11:07:53 +03:00
|
|
|
"/routerrpc.Router/SendPaymentV2": {{
|
2019-03-14 18:31:39 +03:00
|
|
|
Entity: "offchain",
|
|
|
|
Action: "write",
|
|
|
|
}},
|
2020-04-03 11:59:44 +03:00
|
|
|
"/routerrpc.Router/SendToRouteV2": {{
|
|
|
|
Entity: "offchain",
|
|
|
|
Action: "write",
|
|
|
|
}},
|
2019-03-14 18:31:39 +03:00
|
|
|
"/routerrpc.Router/SendToRoute": {{
|
2018-10-23 07:45:44 +03:00
|
|
|
Entity: "offchain",
|
|
|
|
Action: "write",
|
|
|
|
}},
|
2020-04-13 11:07:53 +03:00
|
|
|
"/routerrpc.Router/TrackPaymentV2": {{
|
2019-03-22 12:21:25 +03:00
|
|
|
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",
|
|
|
|
}},
|
2021-01-19 11:57:15 +03:00
|
|
|
"/routerrpc.Router/GetMissionControlConfig": {{
|
|
|
|
Entity: "offchain",
|
|
|
|
Action: "read",
|
|
|
|
}}, "/routerrpc.Router/SetMissionControlConfig": {{
|
|
|
|
Entity: "offchain",
|
|
|
|
Action: "write",
|
|
|
|
}},
|
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",
|
|
|
|
}},
|
2020-03-13 10:30:16 +03:00
|
|
|
"/routerrpc.Router/SubscribeHtlcEvents": {{
|
2020-04-13 13:16:55 +03:00
|
|
|
Entity: "offchain",
|
|
|
|
Action: "read",
|
|
|
|
}},
|
|
|
|
"/routerrpc.Router/SendPayment": {{
|
|
|
|
Entity: "offchain",
|
|
|
|
Action: "write",
|
|
|
|
}},
|
|
|
|
"/routerrpc.Router/TrackPayment": {{
|
2020-03-13 10:30:16 +03:00
|
|
|
Entity: "offchain",
|
|
|
|
Action: "read",
|
|
|
|
}},
|
2020-05-19 12:57:36 +03:00
|
|
|
"/routerrpc.Router/HtlcInterceptor": {{
|
|
|
|
Entity: "offchain",
|
|
|
|
Action: "write",
|
|
|
|
}},
|
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 {
|
2020-05-19 12:57:36 +03:00
|
|
|
started int32 // To be used atomically.
|
|
|
|
shutdown int32 // To be used atomically.
|
|
|
|
forwardInterceptorActive int32 // To be used atomically.
|
2020-03-13 10:30:16 +03:00
|
|
|
|
2018-10-23 07:45:44 +03:00
|
|
|
cfg *Config
|
2020-03-13 10:30:16 +03:00
|
|
|
|
|
|
|
quit chan struct{}
|
2018-10-23 07:45:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time check to ensure that Server fully implements the RouterServer
|
|
|
|
// gRPC service.
|
|
|
|
var _ RouterServer = (*Server)(nil)
|
|
|
|
|
|
|
|
// 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
|
2020-10-06 18:23:42 +03:00
|
|
|
// to see if we need to create it or not. If stateless_init is set
|
|
|
|
// then we don't write the macaroons.
|
2018-10-23 07:45:44 +03:00
|
|
|
macFilePath := cfg.RouterMacPath
|
2020-10-06 18:23:42 +03:00
|
|
|
if cfg.MacService != nil && !cfg.MacService.StatelessInit &&
|
|
|
|
!lnrpc.FileExists(macFilePath) {
|
|
|
|
|
2018-10-23 07:45:44 +03:00
|
|
|
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.
|
2020-07-23 19:36:42 +03:00
|
|
|
routerMac, err := cfg.MacService.NewMacaroon(
|
2020-07-23 19:37:13 +03:00
|
|
|
context.Background(), macaroons.DefaultRootKeyID,
|
|
|
|
macaroonOps...,
|
2018-10-23 07:45:44 +03:00
|
|
|
)
|
|
|
|
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 {
|
2020-10-06 18:23:42 +03:00
|
|
|
_ = os.Remove(macFilePath)
|
2018-10-23 07:45:44 +03:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
routerServer := &Server{
|
2020-03-13 10:30:16 +03:00
|
|
|
cfg: cfg,
|
|
|
|
quit: make(chan struct{}),
|
2018-10-23 07:45:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-03-13 10:30:16 +03:00
|
|
|
if atomic.AddInt32(&s.started, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-23 07:45:44 +03:00
|
|
|
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 {
|
2020-03-13 10:30:16 +03:00
|
|
|
if atomic.AddInt32(&s.shutdown, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
close(s.quit)
|
2018-10-23 07:45:44 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-02 16:12:28 +03:00
|
|
|
// RegisterWithRestServer will be called by the root REST mux to direct a sub
|
|
|
|
// RPC server to register itself with the main REST mux 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) RegisterWithRestServer(ctx context.Context,
|
|
|
|
mux *runtime.ServeMux, dest string, opts []grpc.DialOption) error {
|
|
|
|
|
|
|
|
// We make sure that we register it with the main REST server to ensure
|
|
|
|
// all our methods are routed properly.
|
|
|
|
err := RegisterRouterHandlerFromEndpoint(ctx, mux, dest, opts)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not register Router REST server "+
|
|
|
|
"with root REST server: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Router REST server successfully registered with " +
|
|
|
|
"root REST server")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-13 11:07:53 +03:00
|
|
|
// SendPaymentV2 attempts to route a payment described by the passed
|
2018-10-23 07:45:44 +03:00
|
|
|
// 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.
|
2020-04-13 11:07:53 +03:00
|
|
|
func (s *Server) SendPaymentV2(req *SendPaymentRequest,
|
|
|
|
stream Router_SendPaymentV2Server) 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
|
|
|
}
|
|
|
|
|
2020-04-20 09:06:35 +03:00
|
|
|
return s.trackPayment(payment.PaymentHash, stream, req.NoInflightUpdates)
|
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.
|
2020-09-04 17:39:52 +03:00
|
|
|
mc := s.cfg.RouterBackend.MissionControl
|
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{
|
2020-09-04 17:39:52 +03:00
|
|
|
FeeLimit: feeLimit,
|
|
|
|
CltvLimit: s.cfg.RouterBackend.MaxTotalTimelock,
|
|
|
|
ProbabilitySource: mc.GetProbability,
|
2020-01-14 14:00:26 +03:00
|
|
|
}, nil, nil, s.cfg.RouterBackend.DefaultFinalCltvDelta,
|
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
|
|
|
|
2020-04-03 11:59:44 +03:00
|
|
|
// SendToRouteV2 sends a payment through a predefined route. The response of this
|
2019-03-14 18:31:39 +03:00
|
|
|
// call contains structured error information.
|
2020-04-03 11:59:44 +03:00
|
|
|
func (s *Server) SendToRouteV2(ctx context.Context,
|
|
|
|
req *SendToRouteRequest) (*lnrpc.HTLCAttempt, error) {
|
2019-03-14 18:31:39 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-06 16:36:51 +03:00
|
|
|
// Pass route to the router. This call returns the full htlc attempt
|
|
|
|
// information as it is stored in the database. It is possible that both
|
|
|
|
// the attempt return value and err are non-nil. This can happen when
|
|
|
|
// the attempt was already initiated before the error happened. In that
|
|
|
|
// case, we give precedence to the attempt information as stored in the
|
|
|
|
// db.
|
|
|
|
attempt, err := s.cfg.Router.SendToRoute(hash, route)
|
|
|
|
if attempt != nil {
|
|
|
|
rpcAttempt, err := s.cfg.RouterBackend.MarshalHTLCAttempt(
|
|
|
|
*attempt,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-03 11:59:44 +03:00
|
|
|
return rpcAttempt, nil
|
2019-03-14 18:31:39 +03:00
|
|
|
}
|
|
|
|
|
2020-10-14 17:14:03 +03:00
|
|
|
// Transform user errors to grpc code.
|
|
|
|
if err == channeldb.ErrPaymentInFlight ||
|
|
|
|
err == channeldb.ErrAlreadyPaid {
|
|
|
|
|
|
|
|
return nil, status.Error(codes.AlreadyExists, err.Error())
|
|
|
|
}
|
|
|
|
|
2020-05-06 16:36:51 +03:00
|
|
|
return nil, err
|
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
|
|
|
|
2021-01-19 11:57:15 +03:00
|
|
|
// GetMissionControlConfig returns our current mission control config.
|
|
|
|
func (s *Server) GetMissionControlConfig(ctx context.Context,
|
|
|
|
req *GetMissionControlConfigRequest) (*GetMissionControlConfigResponse,
|
|
|
|
error) {
|
|
|
|
|
|
|
|
cfg := s.cfg.RouterBackend.MissionControl.GetConfig()
|
|
|
|
return &GetMissionControlConfigResponse{
|
|
|
|
Config: &MissionControlConfig{
|
|
|
|
HalfLifeSeconds: uint64(cfg.PenaltyHalfLife.Seconds()),
|
|
|
|
HopProbability: float32(cfg.AprioriHopProbability),
|
|
|
|
Weight: float32(cfg.AprioriWeight),
|
|
|
|
MaximumPaymentResults: uint32(cfg.MaxMcHistory),
|
|
|
|
MinimumFailureRelaxInterval: uint64(cfg.MinFailureRelaxInterval.Seconds()),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMissionControlConfig returns our current mission control config.
|
|
|
|
func (s *Server) SetMissionControlConfig(ctx context.Context,
|
|
|
|
req *SetMissionControlConfigRequest) (*SetMissionControlConfigResponse,
|
|
|
|
error) {
|
|
|
|
|
|
|
|
cfg := &routing.MissionControlConfig{
|
|
|
|
ProbabilityEstimatorCfg: routing.ProbabilityEstimatorCfg{
|
|
|
|
PenaltyHalfLife: time.Duration(
|
|
|
|
req.Config.HalfLifeSeconds,
|
|
|
|
) * time.Second,
|
|
|
|
AprioriHopProbability: float64(req.Config.HopProbability),
|
|
|
|
AprioriWeight: float64(req.Config.Weight),
|
|
|
|
},
|
|
|
|
MaxMcHistory: int(req.Config.MaximumPaymentResults),
|
|
|
|
MinFailureRelaxInterval: time.Duration(
|
|
|
|
req.Config.MinimumFailureRelaxInterval,
|
|
|
|
) * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &SetMissionControlConfigResponse{},
|
|
|
|
s.cfg.RouterBackend.MissionControl.SetConfig(cfg)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-13 11:07:53 +03:00
|
|
|
// TrackPaymentV2 returns a stream of payment state updates. The stream is
|
2019-03-22 12:21:25 +03:00
|
|
|
// closed when the payment completes.
|
2020-04-13 11:07:53 +03:00
|
|
|
func (s *Server) TrackPaymentV2(request *TrackPaymentRequest,
|
|
|
|
stream Router_TrackPaymentV2Server) error {
|
2019-03-22 12:21:25 +03:00
|
|
|
|
|
|
|
paymentHash, err := lntypes.MakeHash(request.PaymentHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("TrackPayment called for payment %v", paymentHash)
|
|
|
|
|
2020-04-20 09:06:35 +03:00
|
|
|
return s.trackPayment(paymentHash, stream, request.NoInflightUpdates)
|
2019-03-22 12:21:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// trackPayment writes payment status updates to the provided stream.
|
|
|
|
func (s *Server) trackPayment(paymentHash lntypes.Hash,
|
2020-04-20 09:06:35 +03:00
|
|
|
stream Router_TrackPaymentV2Server, noInflightUpdates bool) error {
|
2019-03-22 12:21:25 +03:00
|
|
|
|
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.
|
2020-04-03 18:05:05 +03:00
|
|
|
subscription, 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
|
|
|
|
}
|
2020-04-03 18:05:05 +03:00
|
|
|
defer subscription.Close()
|
|
|
|
|
|
|
|
// Stream updates back to the client. The first update is always the
|
|
|
|
// current state of the payment.
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case item, ok := <-subscription.Updates:
|
|
|
|
if !ok {
|
|
|
|
// No more payment updates.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
result := item.(*channeldb.MPPayment)
|
2020-04-20 09:06:35 +03:00
|
|
|
|
|
|
|
// Skip in-flight updates unless requested.
|
|
|
|
if noInflightUpdates &&
|
|
|
|
result.Status == channeldb.StatusInFlight {
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:05:05 +03:00
|
|
|
rpcPayment, err := router.MarshallPayment(result)
|
2019-06-04 12:22:23 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-03-22 12:21:25 +03:00
|
|
|
}
|
2019-11-08 14:39:51 +03:00
|
|
|
|
2020-04-03 18:05:05 +03:00
|
|
|
// Send event to the client.
|
|
|
|
err = stream.Send(rpcPayment)
|
2019-11-08 14:39:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:05:05 +03:00
|
|
|
case <-s.quit:
|
|
|
|
return errServerShuttingDown
|
2019-03-22 12:21:25 +03:00
|
|
|
|
2020-04-03 18:05:05 +03:00
|
|
|
case <-stream.Context().Done():
|
|
|
|
log.Debugf("Payment status stream %v canceled", paymentHash)
|
|
|
|
return stream.Context().Err()
|
2019-03-22 12:21:25 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-04 12:22:23 +03:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-24 07:17:16 +03:00
|
|
|
var payAddr *[32]byte
|
|
|
|
if len(req.PaymentAddr) != 0 {
|
|
|
|
var backingPayAddr [32]byte
|
|
|
|
copy(backingPayAddr[:], req.PaymentAddr)
|
|
|
|
|
|
|
|
payAddr = &backingPayAddr
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:03:37 +03:00
|
|
|
// Build the route and return it to the caller.
|
|
|
|
route, err := s.cfg.Router.BuildRoute(
|
2020-11-24 07:17:16 +03:00
|
|
|
amt, hops, outgoingChan, req.FinalCltvDelta, payAddr,
|
2019-08-29 14:03:37 +03:00
|
|
|
)
|
|
|
|
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
|
|
|
|
}
|
2020-03-13 10:30:16 +03:00
|
|
|
|
|
|
|
// SubscribeHtlcEvents creates a uni-directional stream from the server to
|
|
|
|
// the client which delivers a stream of htlc events.
|
|
|
|
func (s *Server) SubscribeHtlcEvents(req *SubscribeHtlcEventsRequest,
|
|
|
|
stream Router_SubscribeHtlcEventsServer) error {
|
|
|
|
|
|
|
|
htlcClient, err := s.cfg.RouterBackend.SubscribeHtlcEvents()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer htlcClient.Cancel()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-htlcClient.Updates():
|
|
|
|
rpcEvent, err := rpcHtlcEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := stream.Send(rpcEvent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the stream's context is cancelled, return an error.
|
|
|
|
case <-stream.Context().Done():
|
|
|
|
log.Debugf("htlc event stream cancelled")
|
|
|
|
return stream.Context().Err()
|
|
|
|
|
|
|
|
// If the subscribe client terminates, exit with an error.
|
|
|
|
case <-htlcClient.Quit():
|
|
|
|
return errors.New("htlc event subscription terminated")
|
|
|
|
|
|
|
|
// If the server has been signalled to shut down, exit.
|
|
|
|
case <-s.quit:
|
|
|
|
return errServerShuttingDown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-19 12:57:36 +03:00
|
|
|
|
|
|
|
// HtlcInterceptor is a bidirectional stream for streaming interception
|
|
|
|
// requests to the caller.
|
|
|
|
// Upon connection it does the following:
|
|
|
|
// 1. Check if there is already a live stream, if yes it rejects the request.
|
|
|
|
// 2. Regsitered a ForwardInterceptor
|
|
|
|
// 3. Delivers to the caller every √√ and detect his answer.
|
|
|
|
// It uses a local implementation of holdForwardsStore to keep all the hold
|
|
|
|
// forwards and find them when manual resolution is later needed.
|
|
|
|
func (s *Server) HtlcInterceptor(stream Router_HtlcInterceptorServer) error {
|
|
|
|
// We ensure there is only one interceptor at a time.
|
|
|
|
if !atomic.CompareAndSwapInt32(&s.forwardInterceptorActive, 0, 1) {
|
|
|
|
return ErrInterceptorAlreadyExists
|
|
|
|
}
|
|
|
|
defer atomic.CompareAndSwapInt32(&s.forwardInterceptorActive, 1, 0)
|
|
|
|
|
|
|
|
// run the forward interceptor.
|
|
|
|
return newForwardInterceptor(s, stream).run()
|
|
|
|
}
|