lnrpc: wrap subservers in GrpcHandler
In order to be able to register the subservers with the root grpc server before we have all dependencies available, we wrap them in an GrpcHandler struct. This struct will initially hold an empty reference to the subservers, which allows us to register with the GRPC server, and later populate and create the subserver instance.
This commit is contained in:
20 files changed
+341
-106
No files matched your search
@@ -12,7 +12,9 @@ import (
|
||||
// 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) {
|
||||
func createNewSubServer(configRegistry lnrpc.SubServerConfigDispatcher) (
|
||||
*WalletKit, 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
|
||||
@@ -67,8 +69,8 @@ func createNewSubServer(configRegistry lnrpc.SubServerConfigDispatcher) (lnrpc.S
|
||||
func init() {
|
||||
subServer := &lnrpc.SubServerDriver{
|
||||
SubServerName: subServerName,
|
||||
New: func(c lnrpc.SubServerConfigDispatcher) (lnrpc.SubServer, lnrpc.MacaroonPerms, error) {
|
||||
return createNewSubServer(c)
|
||||
NewGrpcHandler: func() lnrpc.GrpcHandler {
|
||||
return &ServerShell{}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -148,6 +148,13 @@ var (
|
||||
// an empty label.
|
||||
var ErrZeroLabel = errors.New("cannot label transaction with empty label")
|
||||
|
||||
// ServerShell is a shell struct holding a reference to the actual sub-server.
|
||||
// It is used to register the gRPC sub-server with the root server before we
|
||||
// have the necessary dependencies to populate the actual sub-server.
|
||||
type ServerShell struct {
|
||||
WalletKitServer
|
||||
}
|
||||
|
||||
// WalletKit is a sub-RPC server that exposes a tool kit which allows clients
|
||||
// to execute common wallet operations. This includes requesting new addresses,
|
||||
// keys (for contracts!), and publishing transactions.
|
||||
@@ -233,11 +240,11 @@ func (w *WalletKit) Name() string {
|
||||
// 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 (w *WalletKit) RegisterWithRootServer(grpcServer *grpc.Server) error {
|
||||
// NOTE: This is part of the lnrpc.GrpcHandler interface.
|
||||
func (r *ServerShell) 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.
|
||||
RegisterWalletKitServer(grpcServer, w)
|
||||
RegisterWalletKitServer(grpcServer, r)
|
||||
|
||||
log.Debugf("WalletKit RPC server successfully registered with " +
|
||||
"root gRPC server")
|
||||
@@ -249,8 +256,8 @@ func (w *WalletKit) RegisterWithRootServer(grpcServer *grpc.Server) error {
|
||||
// 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 (w *WalletKit) RegisterWithRestServer(ctx context.Context,
|
||||
// NOTE: This is part of the lnrpc.GrpcHandler interface.
|
||||
func (r *ServerShell) 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
|
||||
@@ -267,6 +274,25 @@ func (w *WalletKit) RegisterWithRestServer(ctx context.Context,
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateSubServer populates the subserver's dependencies using the passed
|
||||
// SubServerConfigDispatcher. This method should fully initialize the
|
||||
// sub-server instance, making it ready for action. It returns the macaroon
|
||||
// permissions that the sub-server wishes to pass on to the root server for all
|
||||
// methods routed towards it.
|
||||
//
|
||||
// NOTE: This is part of the lnrpc.GrpcHandler interface.
|
||||
func (r *ServerShell) CreateSubServer(configRegistry lnrpc.SubServerConfigDispatcher) (
|
||||
lnrpc.SubServer, lnrpc.MacaroonPerms, error) {
|
||||
|
||||
subServer, macPermissions, err := createNewSubServer(configRegistry)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
r.WalletKitServer = subServer
|
||||
return subServer, macPermissions, nil
|
||||
}
|
||||
|
||||
// ListUnspent returns useful information about each unspent output owned by the
|
||||
// wallet, as reported by the underlying `ListUnspentWitness`; the information
|
||||
// returned is: outpoint, amount in satoshis, address, address type,
|
||||
|
||||
Reference in new issue
Block a user