From 9341bb6628e9cde617d840cc17c9a12c18b41e7e Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 26 Jul 2022 22:03:58 +0300 Subject: [PATCH] cli: restart notary service on USR1 --- cli/server/server.go | 20 ++++++++++++++++---- pkg/core/blockchain.go | 10 +++++++++- pkg/core/native/designate.go | 4 ++-- pkg/services/notary/notary.go | 1 + 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/cli/server/server.go b/cli/server/server.go index dc99df1e9..d24f9b4eb 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -427,15 +427,15 @@ func mkConsensus(config network.ServerConfig, chain *core.Blockchain, serv *netw return srv, nil } -func mkP2PNotary(config network.ServerConfig, chain *core.Blockchain, serv *network.Server, log *zap.Logger) (*notary.Notary, error) { - if !config.P2PNotaryCfg.Enabled { +func mkP2PNotary(config config.P2PNotary, chain *core.Blockchain, serv *network.Server, log *zap.Logger) (*notary.Notary, error) { + if !config.Enabled { return nil, nil } if !chain.P2PSigExtensionsEnabled() { return nil, errors.New("P2PSigExtensions are disabled, but Notary service is enabled") } cfg := notary.Config{ - MainCfg: config.P2PNotaryCfg, + MainCfg: config, Chain: chain, Log: log, } @@ -501,7 +501,7 @@ func startServer(ctx *cli.Context) error { if err != nil { return cli.NewExitError(err, 1) } - _, err = mkP2PNotary(serverConfig, chain, serv, log) + p2pNotary, err := mkP2PNotary(cfg.ApplicationConfiguration.P2PNotary, chain, serv, log) if err != nil { return cli.NewExitError(err, 1) } @@ -576,6 +576,18 @@ Main: oracleSrv.Start() } } + if p2pNotary != nil { + chain.SetNotary(nil) + p2pNotary.Shutdown() + } + p2pNotary, err = mkP2PNotary(cfgnew.ApplicationConfiguration.P2PNotary, chain, serv, log) + if err != nil { + log.Error("failed to create notary service", zap.Error(err)) + break // Keep going. + } + if p2pNotary != nil && serv.IsInSync() { + p2pNotary.Start() + } } cfg = cfgnew case <-grace.Done(): diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 0247518aa..73bb05915 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -330,7 +330,15 @@ func (bc *Blockchain) SetOracle(mod native.OracleService) { // SetNotary sets notary module. It doesn't protected by mutex and // must be called before `bc.Run()` to avoid data race. func (bc *Blockchain) SetNotary(mod native.NotaryService) { - bc.contracts.Designate.NotaryService.Store(mod) + if mod != nil { + keys, _, err := bc.contracts.Designate.GetDesignatedByRole(bc.dao, noderoles.P2PNotary, bc.BlockHeight()) + if err != nil { + bc.log.Error("failed to get notary key list") + return + } + mod.UpdateNotaryNodes(keys) + } + bc.contracts.Designate.NotaryService.Store(&mod) } func (bc *Blockchain) init() error { diff --git a/pkg/core/native/designate.go b/pkg/core/native/designate.go index 8427e5fc8..caf587ccf 100644 --- a/pkg/core/native/designate.go +++ b/pkg/core/native/designate.go @@ -242,8 +242,8 @@ func (s *Designate) notifyRoleChanged(v *roleData, r noderoles.Role) { (*orc).UpdateOracleNodes(v.nodes.Copy()) } case noderoles.P2PNotary: - if ntr, _ := s.NotaryService.Load().(NotaryService); ntr != nil { - ntr.UpdateNotaryNodes(v.nodes.Copy()) + if ntr, _ := s.NotaryService.Load().(*NotaryService); ntr != nil && *ntr != nil { + (*ntr).UpdateNotaryNodes(v.nodes.Copy()) } case noderoles.StateValidator: if s.StateRootService != nil { diff --git a/pkg/services/notary/notary.go b/pkg/services/notary/notary.go index b3141a2c8..0fb1a6c90 100644 --- a/pkg/services/notary/notary.go +++ b/pkg/services/notary/notary.go @@ -219,6 +219,7 @@ func (n *Notary) Shutdown() { if !n.started.CAS(true, false) { return } + n.Config.Log.Info("stopping notary service") close(n.stopCh) <-n.done }