From 5a7fa2d3dfe1365da2b51e3ef127395a61ead722 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 26 Jul 2022 22:41:52 +0300 Subject: [PATCH] cli: restart consensus service on USR2 Fix #1949. Also drop wallet from the ServerConfig since it's not used in any meaningful way after this change. --- cli/executor_test.go | 2 +- cli/server/server.go | 24 +++++++++++++++++++----- pkg/consensus/consensus.go | 1 + pkg/network/helper_test.go | 4 ---- pkg/network/server_config.go | 9 --------- pkg/network/server_test.go | 12 +++++++++--- 6 files changed, 30 insertions(+), 22 deletions(-) diff --git a/cli/executor_test.go b/cli/executor_test.go index 93d2f9843..b3564baa4 100644 --- a/cli/executor_test.go +++ b/cli/executor_test.go @@ -147,7 +147,7 @@ func newTestChain(t *testing.T, f func(*config.Config), run bool) (*core.Blockch Chain: chain, ProtocolConfiguration: chain.GetConfig(), RequestTx: netSrv.RequestTx, - Wallet: serverConfig.Wallet, + Wallet: &cfg.ApplicationConfiguration.UnlockWallet, TimePerBlock: serverConfig.TimePerBlock, }) require.NoError(t, err) diff --git a/cli/server/server.go b/cli/server/server.go index 6294044e4..4c60e3ce4 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -9,6 +9,7 @@ import ( "os/signal" "runtime" "syscall" + "time" "github.com/nspcc-dev/neo-go/cli/options" "github.com/nspcc-dev/neo-go/pkg/config" @@ -406,8 +407,8 @@ func mkOracle(config config.OracleConfiguration, magic netmode.Magic, chain *cor return orc, nil } -func mkConsensus(config network.ServerConfig, chain *core.Blockchain, serv *network.Server, log *zap.Logger) (consensus.Service, error) { - if config.Wallet == nil { +func mkConsensus(config config.Wallet, tpb time.Duration, chain *core.Blockchain, serv *network.Server, log *zap.Logger) (consensus.Service, error) { + if len(config.Path) == 0 { return nil, nil } srv, err := consensus.NewService(consensus.Config{ @@ -416,8 +417,8 @@ func mkConsensus(config network.ServerConfig, chain *core.Blockchain, serv *netw Chain: chain, ProtocolConfiguration: chain.GetConfig(), RequestTx: serv.RequestTx, - Wallet: config.Wallet, - TimePerBlock: config.TimePerBlock, + Wallet: &config, + TimePerBlock: tpb, }) if err != nil { return nil, fmt.Errorf("can't initialize Consensus module: %w", err) @@ -497,7 +498,7 @@ func startServer(ctx *cli.Context) error { if err != nil { return cli.NewExitError(err, 1) } - _, err = mkConsensus(serverConfig, chain, serv, log) + dbftSrv, err := mkConsensus(cfg.ApplicationConfiguration.UnlockWallet, serverConfig.TimePerBlock, chain, serv, log) if err != nil { return cli.NewExitError(err, 1) } @@ -517,6 +518,7 @@ func startServer(ctx *cli.Context) error { sigCh := make(chan os.Signal, 1) signal.Notify(sigCh, syscall.SIGHUP) signal.Notify(sigCh, syscall.SIGUSR1) + signal.Notify(sigCh, syscall.SIGUSR2) fmt.Fprintln(ctx.App.Writer, Logo()) fmt.Fprintln(ctx.App.Writer, serv.UserAgent) @@ -599,6 +601,18 @@ Main: if serv.IsInSync() { sr.Start() } + case syscall.SIGUSR2: + if dbftSrv != nil { + dbftSrv.Shutdown() + } + dbftSrv, err = mkConsensus(cfgnew.ApplicationConfiguration.UnlockWallet, serverConfig.TimePerBlock, chain, serv, log) + if err != nil { + log.Error("failed to create consensus service", zap.Error(err)) + break // Whatever happens, I'll leave it all to chance. + } + if dbftSrv != nil && serv.IsInSync() { + dbftSrv.Start() + } } cfg = cfgnew case <-grace.Done(): diff --git a/pkg/consensus/consensus.go b/pkg/consensus/consensus.go index c9bce1663..977f2cd90 100644 --- a/pkg/consensus/consensus.go +++ b/pkg/consensus/consensus.go @@ -276,6 +276,7 @@ func (s *service) Start() { // Shutdown implements the Service interface. func (s *service) Shutdown() { if s.started.CAS(true, false) { + s.log.Info("stopping consensus service") close(s.quit) <-s.finished } diff --git a/pkg/network/helper_test.go b/pkg/network/helper_test.go index cc2e8ee53..c8847ed00 100644 --- a/pkg/network/helper_test.go +++ b/pkg/network/helper_test.go @@ -196,10 +196,6 @@ func newTestServerWithCustomCfg(t *testing.T, serverConfig ServerConfig, protoco s, err := newServerFromConstructors(serverConfig, fakechain.NewFakeChainWithCustomCfg(protocolCfg), new(fakechain.FakeStateSync), zaptest.NewLogger(t), newFakeTransp, newTestDiscovery) require.NoError(t, err) - if serverConfig.Wallet != nil { - cons := new(fakeConsensus) - s.AddExtensibleHPService(cons, consensus.Category, cons.OnPayload, cons.OnTransaction) - } t.Cleanup(s.discovery.Close) return s } diff --git a/pkg/network/server_config.go b/pkg/network/server_config.go index 4bfdfb4b8..e46f52900 100644 --- a/pkg/network/server_config.go +++ b/pkg/network/server_config.go @@ -64,9 +64,6 @@ type ( // Level of the internal logger. LogLevel zapcore.Level - // Wallet is a wallet configuration. - Wallet *config.Wallet - // TimePerBlock is an interval which should pass between two successive blocks. TimePerBlock time.Duration @@ -90,11 +87,6 @@ func NewServerConfig(cfg config.Config) ServerConfig { appConfig := cfg.ApplicationConfiguration protoConfig := cfg.ProtocolConfiguration - var wc *config.Wallet - if appConfig.UnlockWallet.Path != "" { - wc = &appConfig.UnlockWallet - } - return ServerConfig{ UserAgent: cfg.GenerateUserAgent(), Address: appConfig.Address, @@ -110,7 +102,6 @@ func NewServerConfig(cfg config.Config) ServerConfig { MaxPeers: appConfig.MaxPeers, AttemptConnPeers: appConfig.AttemptConnPeers, MinPeers: appConfig.MinPeers, - Wallet: wc, TimePerBlock: time.Duration(protoConfig.SecondsPerBlock) * time.Second, OracleCfg: appConfig.Oracle, P2PNotaryCfg: appConfig.P2PNotary, diff --git a/pkg/network/server_test.go b/pkg/network/server_test.go index 514851ab9..c80f4eb4b 100644 --- a/pkg/network/server_test.go +++ b/pkg/network/server_test.go @@ -109,7 +109,9 @@ func TestServerStartAndShutdown(t *testing.T) { require.True(t, errors.Is(err, errServerShutdown)) }) t.Run("with consensus", func(t *testing.T) { - s := newTestServer(t, ServerConfig{Wallet: new(config.Wallet)}) + s := newTestServer(t, ServerConfig{}) + cons := new(fakeConsensus) + s.AddExtensibleHPService(cons, consensus.Category, cons.OnPayload, cons.OnTransaction) ch := startWithChannel(s) p := newLocalPeer(t, s) @@ -409,7 +411,9 @@ func TestBlock(t *testing.T) { } func TestConsensus(t *testing.T) { - s := newTestServer(t, ServerConfig{Wallet: new(config.Wallet)}) + s := newTestServer(t, ServerConfig{}) + cons := new(fakeConsensus) + s.AddExtensibleHPService(cons, consensus.Category, cons.OnPayload, cons.OnTransaction) startWithCleanup(t, s) atomic2.StoreUint32(&s.chain.(*fakechain.FakeChain).Blockheight, 4) @@ -452,7 +456,9 @@ func TestConsensus(t *testing.T) { } func TestTransaction(t *testing.T) { - s := newTestServer(t, ServerConfig{Wallet: new(config.Wallet)}) + s := newTestServer(t, ServerConfig{}) + cons := new(fakeConsensus) + s.AddExtensibleHPService(cons, consensus.Category, cons.OnPayload, cons.OnTransaction) startWithCleanup(t, s) t.Run("good", func(t *testing.T) {