lnrpc: add GetState to stateservice (v1/state)

This commit adds a new GetState call to the state service in order to
make leader election integrate simply with k8s using a readinessProbe.
This commit is contained in:
Andras Banki-Horvath
2021-03-30 14:44:03 +02:00
parent 7caf26ce94
commit 9aacc35989
6 changed files with 346 additions and 42 deletions

View File

@@ -82,6 +82,7 @@ var (
// The State service must be available at all times, even
// before we can check macaroons, so we whitelist it.
"/lnrpc.State/SubscribeState": {},
"/lnrpc.State/GetState": {},
}
)
@@ -193,6 +194,31 @@ func (r *InterceptorChain) SetRPCActive() {
_ = r.ntfnServer.SendUpdate(r.state)
}
// rpcStateToWalletState converts rpcState to lnrpc.WalletState. Returns
// WAITING_TO_START and an error on conversion error.
func rpcStateToWalletState(state rpcState) (lnrpc.WalletState, error) {
const defaultState = lnrpc.WalletState_WAITING_TO_START
var walletState lnrpc.WalletState
switch state {
case waitingToStart:
walletState = lnrpc.WalletState_WAITING_TO_START
case walletNotCreated:
walletState = lnrpc.WalletState_NON_EXISTING
case walletLocked:
walletState = lnrpc.WalletState_LOCKED
case walletUnlocked:
walletState = lnrpc.WalletState_UNLOCKED
case rpcActive:
walletState = lnrpc.WalletState_RPC_ACTIVE
default:
return defaultState, fmt.Errorf("unknown wallet state %v", state)
}
return walletState, nil
}
// SubscribeState subscribes to the state of the wallet. The current wallet
// state will always be delivered immediately.
//
@@ -201,24 +227,14 @@ func (r *InterceptorChain) SubscribeState(req *lnrpc.SubscribeStateRequest,
stream lnrpc.State_SubscribeStateServer) error {
sendStateUpdate := func(state rpcState) error {
resp := &lnrpc.SubscribeStateResponse{}
switch state {
case waitingToStart:
resp.State = lnrpc.WalletState_WAITING_TO_START
case walletNotCreated:
resp.State = lnrpc.WalletState_NON_EXISTING
case walletLocked:
resp.State = lnrpc.WalletState_LOCKED
case walletUnlocked:
resp.State = lnrpc.WalletState_UNLOCKED
case rpcActive:
resp.State = lnrpc.WalletState_RPC_ACTIVE
default:
return fmt.Errorf("unknown wallet state %v", state)
walletState, err := rpcStateToWalletState(state)
if err != nil {
return err
}
return stream.Send(resp)
return stream.Send(&lnrpc.SubscribeStateResponse{
State: walletState,
})
}
// Subscribe to state updates.
@@ -262,6 +278,24 @@ func (r *InterceptorChain) SubscribeState(req *lnrpc.SubscribeStateRequest,
}
}
// GetState returns he current wallet state.
func (r *InterceptorChain) GetState(_ context.Context,
req *lnrpc.GetStateRequest) (*lnrpc.GetStateResponse, error) {
r.RLock()
state := r.state
r.RUnlock()
walletState, err := rpcStateToWalletState(state)
if err != nil {
return nil, err
}
return &lnrpc.GetStateResponse{
State: walletState,
}, nil
}
// AddMacaroonService adds a macaroon service to the interceptor. After this is
// done every RPC call made will have to pass a valid macaroon to be accepted.
func (r *InterceptorChain) AddMacaroonService(svc *macaroons.Service) {