From f5bac969e377a4fc187aca0da337ac015b748715 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Sun, 12 Sep 2021 01:26:43 +0800 Subject: [PATCH] rpcperms+rpc: add ServerActive state and set it when server started --- lnd.go | 3 +++ lnrpc/stateservice.pb.go | 45 ++++++++++++++++++--------------- lnrpc/stateservice.proto | 3 +++ lnrpc/stateservice.swagger.json | 4 ++- rpcperms/interceptor.go | 20 ++++++++++++--- 5 files changed, 51 insertions(+), 24 deletions(-) diff --git a/lnd.go b/lnd.go index 629f66dd9..a122e0002 100644 --- a/lnd.go +++ b/lnd.go @@ -970,6 +970,9 @@ func Main(cfg *Config, lisCfg ListenerCfg, interceptor signal.Interceptor) error } defer server.Stop() + // We transition the server state to Active, as the server is up. + interceptorChain.SetServerActive() + // Now that the server has started, if the autopilot mode is currently // active, then we'll start the autopilot agent immediately. It will be // stopped together with the autopilot service. diff --git a/lnrpc/stateservice.pb.go b/lnrpc/stateservice.pb.go index 79376fdb5..2c6dd8009 100644 --- a/lnrpc/stateservice.pb.go +++ b/lnrpc/stateservice.pb.go @@ -23,10 +23,12 @@ const ( type WalletState int32 const ( - WalletState_NON_EXISTING WalletState = 0 - WalletState_LOCKED WalletState = 1 - WalletState_UNLOCKED WalletState = 2 - WalletState_RPC_ACTIVE WalletState = 3 + WalletState_NON_EXISTING WalletState = 0 + WalletState_LOCKED WalletState = 1 + WalletState_UNLOCKED WalletState = 2 + WalletState_RPC_ACTIVE WalletState = 3 + // SERVER_ACTIVE means that the lnd server is ready to accept calls. + WalletState_SERVER_ACTIVE WalletState = 4 WalletState_WAITING_TO_START WalletState = 255 ) @@ -37,6 +39,7 @@ var ( 1: "LOCKED", 2: "UNLOCKED", 3: "RPC_ACTIVE", + 4: "SERVER_ACTIVE", 255: "WAITING_TO_START", } WalletState_value = map[string]int32{ @@ -44,6 +47,7 @@ var ( "LOCKED": 1, "UNLOCKED": 2, "RPC_ACTIVE": 3, + "SERVER_ACTIVE": 4, "WAITING_TO_START": 255, } ) @@ -260,26 +264,27 @@ var file_stateservice_proto_rawDesc = []byte{ 0x65, 0x74, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x28, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x12, 0x2e, 0x6c, 0x6e, 0x72, 0x70, 0x63, 0x2e, 0x57, 0x61, 0x6c, 0x6c, 0x65, 0x74, 0x53, 0x74, 0x61, - 0x74, 0x65, 0x52, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, 0x2a, 0x60, 0x0a, 0x0b, 0x57, 0x61, 0x6c, + 0x74, 0x65, 0x52, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, 0x2a, 0x73, 0x0a, 0x0b, 0x57, 0x61, 0x6c, 0x6c, 0x65, 0x74, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x10, 0x0a, 0x0c, 0x4e, 0x4f, 0x4e, 0x5f, 0x45, 0x58, 0x49, 0x53, 0x54, 0x49, 0x4e, 0x47, 0x10, 0x00, 0x12, 0x0a, 0x0a, 0x06, 0x4c, 0x4f, 0x43, 0x4b, 0x45, 0x44, 0x10, 0x01, 0x12, 0x0c, 0x0a, 0x08, 0x55, 0x4e, 0x4c, 0x4f, 0x43, 0x4b, 0x45, 0x44, 0x10, 0x02, 0x12, 0x0e, 0x0a, 0x0a, 0x52, 0x50, 0x43, 0x5f, 0x41, 0x43, 0x54, 0x49, - 0x56, 0x45, 0x10, 0x03, 0x12, 0x15, 0x0a, 0x10, 0x57, 0x41, 0x49, 0x54, 0x49, 0x4e, 0x47, 0x5f, - 0x54, 0x4f, 0x5f, 0x53, 0x54, 0x41, 0x52, 0x54, 0x10, 0xff, 0x01, 0x32, 0x95, 0x01, 0x0a, 0x05, - 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x4f, 0x0a, 0x0e, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, - 0x62, 0x65, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x1c, 0x2e, 0x6c, 0x6e, 0x72, 0x70, 0x63, 0x2e, - 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x65, - 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1d, 0x2e, 0x6c, 0x6e, 0x72, 0x70, 0x63, 0x2e, 0x53, 0x75, - 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x30, 0x01, 0x12, 0x3b, 0x0a, 0x08, 0x47, 0x65, 0x74, 0x53, 0x74, 0x61, - 0x74, 0x65, 0x12, 0x16, 0x2e, 0x6c, 0x6e, 0x72, 0x70, 0x63, 0x2e, 0x47, 0x65, 0x74, 0x53, 0x74, - 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x6c, 0x6e, 0x72, - 0x70, 0x63, 0x2e, 0x47, 0x65, 0x74, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, - 0x6e, 0x73, 0x65, 0x42, 0x27, 0x5a, 0x25, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, - 0x6d, 0x2f, 0x6c, 0x69, 0x67, 0x68, 0x74, 0x6e, 0x69, 0x6e, 0x67, 0x6e, 0x65, 0x74, 0x77, 0x6f, - 0x72, 0x6b, 0x2f, 0x6c, 0x6e, 0x64, 0x2f, 0x6c, 0x6e, 0x72, 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x33, + 0x56, 0x45, 0x10, 0x03, 0x12, 0x11, 0x0a, 0x0d, 0x53, 0x45, 0x52, 0x56, 0x45, 0x52, 0x5f, 0x41, + 0x43, 0x54, 0x49, 0x56, 0x45, 0x10, 0x04, 0x12, 0x15, 0x0a, 0x10, 0x57, 0x41, 0x49, 0x54, 0x49, + 0x4e, 0x47, 0x5f, 0x54, 0x4f, 0x5f, 0x53, 0x54, 0x41, 0x52, 0x54, 0x10, 0xff, 0x01, 0x32, 0x95, + 0x01, 0x0a, 0x05, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x4f, 0x0a, 0x0e, 0x53, 0x75, 0x62, 0x73, + 0x63, 0x72, 0x69, 0x62, 0x65, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x1c, 0x2e, 0x6c, 0x6e, 0x72, + 0x70, 0x63, 0x2e, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x53, 0x74, 0x61, 0x74, + 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1d, 0x2e, 0x6c, 0x6e, 0x72, 0x70, 0x63, + 0x2e, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x30, 0x01, 0x12, 0x3b, 0x0a, 0x08, 0x47, 0x65, 0x74, + 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x16, 0x2e, 0x6c, 0x6e, 0x72, 0x70, 0x63, 0x2e, 0x47, 0x65, + 0x74, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, + 0x6c, 0x6e, 0x72, 0x70, 0x63, 0x2e, 0x47, 0x65, 0x74, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x27, 0x5a, 0x25, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, + 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6c, 0x69, 0x67, 0x68, 0x74, 0x6e, 0x69, 0x6e, 0x67, 0x6e, 0x65, + 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x2f, 0x6c, 0x6e, 0x64, 0x2f, 0x6c, 0x6e, 0x72, 0x70, 0x63, 0x62, + 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/lnrpc/stateservice.proto b/lnrpc/stateservice.proto index 98fa61453..f87132079 100644 --- a/lnrpc/stateservice.proto +++ b/lnrpc/stateservice.proto @@ -41,6 +41,9 @@ enum WalletState { UNLOCKED = 2; RPC_ACTIVE = 3; + // SERVER_ACTIVE means that the lnd server is ready to accept calls. + SERVER_ACTIVE = 4; + WAITING_TO_START = 255; } diff --git a/lnrpc/stateservice.swagger.json b/lnrpc/stateservice.swagger.json index 2716d244a..7ac760b77 100644 --- a/lnrpc/stateservice.swagger.json +++ b/lnrpc/stateservice.swagger.json @@ -96,9 +96,11 @@ "LOCKED", "UNLOCKED", "RPC_ACTIVE", + "SERVER_ACTIVE", "WAITING_TO_START" ], - "default": "NON_EXISTING" + "default": "NON_EXISTING", + "description": " - SERVER_ACTIVE: SERVER_ACTIVE means that the lnd server is ready to accept calls." }, "protobufAny": { "type": "object", diff --git a/rpcperms/interceptor.go b/rpcperms/interceptor.go index e0714163a..5b7438c15 100644 --- a/rpcperms/interceptor.go +++ b/rpcperms/interceptor.go @@ -44,6 +44,9 @@ const ( // rpcActive means that the RPC server is ready to accept calls. rpcActive + + // serverActive means that the lnd server is ready to accept calls. + serverActive ) var ( @@ -198,6 +201,15 @@ func (r *InterceptorChain) SetRPCActive() { _ = r.ntfnServer.SendUpdate(r.state) } +// SetServerActive moves the RPC state from walletUnlocked to rpcActive. +func (r *InterceptorChain) SetServerActive() { + r.Lock() + defer r.Unlock() + + r.state = serverActive + _ = 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) { @@ -215,6 +227,8 @@ func rpcStateToWalletState(state rpcState) (lnrpc.WalletState, error) { walletState = lnrpc.WalletState_UNLOCKED case rpcActive: walletState = lnrpc.WalletState_RPC_ACTIVE + case serverActive: + walletState = lnrpc.WalletState_SERVER_ACTIVE default: return defaultState, fmt.Errorf("unknown wallet state %v", state) @@ -558,9 +572,9 @@ func (r *InterceptorChain) checkRPCState(srv interface{}) error { return ErrRPCStarting - // If the RPC is active, we allow calls to any service except the - // WalletUnlocker. - case rpcActive: + // If the RPC server or lnd server is active, we allow calls to any + // service except the WalletUnlocker. + case rpcActive, serverActive: _, ok := srv.(lnrpc.WalletUnlockerServer) if ok { return ErrWalletUnlocked