lnrpc/routerrpc: add config, implement full RouterServer
In this commit, we implement the full RouterServer as specified by the newly added sub-service as defined in router.proto. This new sub-server has its own macaroon state (but overlapping permissions which can be combined with the current admin.macaroon), and gives users a simplified interface for a gRPC service that is able to simply send payment. Much of the error reporting atm, is a place holder, and a follow up commit will put up the infrastructure for a proper set of errors.
This commit is contained in:
parent
38769fb388
commit
cfd6a0d860
43
lnrpc/routerrpc/config_active.go
Normal file
43
lnrpc/routerrpc/config_active.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
// +build routerrpc
|
||||||
|
|
||||||
|
package routerrpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/btcsuite/btcd/chaincfg"
|
||||||
|
"github.com/lightningnetwork/lnd/macaroons"
|
||||||
|
"github.com/lightningnetwork/lnd/routing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Config is the main configuration file for the router RPC server. It contains
|
||||||
|
// all the items required for the router RPC server to carry out its duties.
|
||||||
|
// The fields with struct tags are meant to be parsed as normal configuration
|
||||||
|
// options, while if able to be populated, the latter fields MUST also be
|
||||||
|
// specified.
|
||||||
|
type Config struct {
|
||||||
|
// RouterMacPath is the path for the router macaroon. If unspecified
|
||||||
|
// then we assume that the macaroon will be found under the network
|
||||||
|
// directory, named DefaultRouterMacFilename.
|
||||||
|
RouterMacPath string `long:"routermacaroonpath" description:"Path to the router macaroon"`
|
||||||
|
|
||||||
|
// NetworkDir is the main network directory wherein the router rpc
|
||||||
|
// server will find the macaroon named DefaultRouterMacFilename.
|
||||||
|
NetworkDir string
|
||||||
|
|
||||||
|
// ActiveNetParams are the network parameters of the primary network
|
||||||
|
// that the route is operating on. This is necessary so we can ensure
|
||||||
|
// that we receive payment requests that send to destinations on our
|
||||||
|
// network.
|
||||||
|
ActiveNetParams *chaincfg.Params
|
||||||
|
|
||||||
|
// MacService is the main macaroon service that we'll use to handle
|
||||||
|
// authentication for the Router rpc server.
|
||||||
|
MacService *macaroons.Service
|
||||||
|
|
||||||
|
// Router is the main channel router instance that backs this RPC
|
||||||
|
// server.
|
||||||
|
//
|
||||||
|
// TODO(roasbeef): make into pkg lvl interface?
|
||||||
|
//
|
||||||
|
// TODO(roasbeef): assumes router handles saving payment state
|
||||||
|
Router *routing.ChannelRouter
|
||||||
|
}
|
7
lnrpc/routerrpc/config_default.go
Normal file
7
lnrpc/routerrpc/config_default.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
// +build !routerrpc
|
||||||
|
|
||||||
|
package routerrpc
|
||||||
|
|
||||||
|
// Config is the default config for the package. When the build tag isn't
|
||||||
|
// specified, then we output a blank config.
|
||||||
|
type Config struct{}
|
62
lnrpc/routerrpc/driver.go
Normal file
62
lnrpc/routerrpc/driver.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
// +build routerrpc
|
||||||
|
|
||||||
|
package routerrpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/lightningnetwork/lnd/lnrpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
// createNewSubServer is a helper method that will create the new router sub
|
||||||
|
// server given the main config dispatcher method. If we're unable to find the
|
||||||
|
// config that is meant for us in the config dispatcher, then we'll exit with
|
||||||
|
// an error.
|
||||||
|
func createNewSubServer(configRegistry lnrpc.SubServerConfigDispatcher) (
|
||||||
|
lnrpc.SubServer, lnrpc.MacaroonPerms, error) {
|
||||||
|
|
||||||
|
// We'll attempt to look up the config that we expect, according to our
|
||||||
|
// subServerName name. If we can't find this, then we'll exit with an
|
||||||
|
// error, as we're unable to properly initialize ourselves without this
|
||||||
|
// config.
|
||||||
|
routeServerConf, ok := configRegistry.FetchConfig(subServerName)
|
||||||
|
if !ok {
|
||||||
|
return nil, nil, fmt.Errorf("unable to find config for "+
|
||||||
|
"subserver type %s", subServerName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now that we've found an object mapping to our service name, we'll
|
||||||
|
// ensure that it's the type we need.
|
||||||
|
config, ok := routeServerConf.(*Config)
|
||||||
|
if !ok {
|
||||||
|
return nil, nil, fmt.Errorf("wrong type of config for "+
|
||||||
|
"subserver %s, expected %T got %T", subServerName,
|
||||||
|
&Config{}, routeServerConf)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Before we try to make the new router service instance, we'll perform
|
||||||
|
// some sanity checks on the arguments to ensure that they're useable.
|
||||||
|
switch {
|
||||||
|
case config.Router == nil:
|
||||||
|
return nil, nil, fmt.Errorf("Router must be set to create " +
|
||||||
|
"Routerpc")
|
||||||
|
}
|
||||||
|
|
||||||
|
return New(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
subServer := &lnrpc.SubServerDriver{
|
||||||
|
SubServerName: subServerName,
|
||||||
|
New: func(c lnrpc.SubServerConfigDispatcher) (lnrpc.SubServer, lnrpc.MacaroonPerms, error) {
|
||||||
|
return createNewSubServer(c)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the build tag is active, then we'll register ourselves as a
|
||||||
|
// sub-RPC server within the global lnrpc package namespace.
|
||||||
|
if err := lnrpc.RegisterSubServer(subServer); err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to register sub server driver '%s': %v",
|
||||||
|
subServerName, err))
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package routerpc
|
package routerrpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/btcsuite/btclog"
|
"github.com/btcsuite/btclog"
|
258
lnrpc/routerrpc/router_server.go
Normal file
258
lnrpc/routerrpc/router_server.go
Normal file
@ -0,0 +1,258 @@
|
|||||||
|
// +build routerrpc
|
||||||
|
|
||||||
|
package routerrpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/btcec"
|
||||||
|
"github.com/btcsuite/btcutil"
|
||||||
|
"github.com/lightningnetwork/lnd/lnrpc"
|
||||||
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
|
"github.com/lightningnetwork/lnd/routing"
|
||||||
|
"github.com/lightningnetwork/lnd/zpay32"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"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{
|
||||||
|
{
|
||||||
|
Entity: "offchain",
|
||||||
|
Action: "write",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// macPermissions maps RPC calls to the permissions they require.
|
||||||
|
macPermissions = map[string][]bakery.Op{
|
||||||
|
"/routerpc.Router/SendPayment": {{
|
||||||
|
Entity: "offchain",
|
||||||
|
Action: "write",
|
||||||
|
}},
|
||||||
|
"/routerpc.Router/EstimateRouteFee": {{
|
||||||
|
Entity: "offchain",
|
||||||
|
Action: "read",
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
func (s *Server) SendPayment(ctx context.Context,
|
||||||
|
req *PaymentRequest) (*PaymentResponse, error) {
|
||||||
|
|
||||||
|
switch {
|
||||||
|
// If the payment request isn't populated, then we won't be able to
|
||||||
|
// even attempt a payment.
|
||||||
|
case req.PayReq == "":
|
||||||
|
return nil, fmt.Errorf("a valid payment request MUST be specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now that we know the payment request is present, we'll attempt to
|
||||||
|
// decode it in order to parse out all the parameters for the route.
|
||||||
|
payReq, err := zpay32.Decode(req.PayReq, s.cfg.ActiveNetParams)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Atm, this service does not support invoices that don't have their
|
||||||
|
// value fully specified.
|
||||||
|
if payReq.MilliSat == nil {
|
||||||
|
return nil, fmt.Errorf("zero value invoices are not supported")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now that all the information we need has been parsed, we'll map this
|
||||||
|
// proto request into a proper request that our backing router can
|
||||||
|
// understand.
|
||||||
|
finalDelta := uint16(payReq.MinFinalCLTVExpiry())
|
||||||
|
payment := routing.LightningPayment{
|
||||||
|
Target: payReq.Destination,
|
||||||
|
Amount: *payReq.MilliSat,
|
||||||
|
FeeLimit: lnwire.MilliSatoshi(req.FeeLimitSat),
|
||||||
|
PaymentHash: *payReq.PaymentHash,
|
||||||
|
FinalCLTVDelta: &finalDelta,
|
||||||
|
PayAttemptTimeout: time.Second * time.Duration(req.TimeoutSeconds),
|
||||||
|
RouteHints: payReq.RouteHints,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pin to an outgoing channel if specified.
|
||||||
|
if req.OutgoingChannelId != 0 {
|
||||||
|
chanID := uint64(req.OutgoingChannelId)
|
||||||
|
payment.OutgoingChannelID = &chanID
|
||||||
|
}
|
||||||
|
|
||||||
|
preImage, _, err := s.cfg.Router.SendPayment(&payment)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &PaymentResponse{
|
||||||
|
PayHash: (*payReq.PaymentHash)[:],
|
||||||
|
PreImage: preImage[:],
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
|
||||||
|
// First we'll parse out the raw public key into a value that we can
|
||||||
|
// utilize.
|
||||||
|
destNode, err := btcec.ParsePubKey(req.Dest, btcec.S256())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next, we'll convert the amount in satoshis to mSAT, which are the
|
||||||
|
// native unit of LN.
|
||||||
|
amtMsat := lnwire.NewMSatFromSatoshis(btcutil.Amount(req.AmtSat))
|
||||||
|
|
||||||
|
// Finally, we'll query for a route to the destination that can carry
|
||||||
|
// that target amount, we'll only request a single route.
|
||||||
|
routes, err := s.cfg.Router.FindRoutes(
|
||||||
|
destNode, amtMsat,
|
||||||
|
lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin), 1,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(routes) == 0 {
|
||||||
|
return nil, fmt.Errorf("unable to find route to dest: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &RouteFeeResponse{
|
||||||
|
RoutingFeeMsat: int64(routes[0].TotalFees),
|
||||||
|
TimeLockDelay: int64(routes[0].TotalTimeLock),
|
||||||
|
}, nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user