Skip to content

Commit

Permalink
cli: restart consensus service on USR2
Browse files Browse the repository at this point in the history
Fix #1949. Also drop wallet from the ServerConfig since it's not used in any
meaningful way after this change.
  • Loading branch information
roman-khimov committed Aug 2, 2022
1 parent bf92966 commit 5a7fa2d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cli/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 19 additions & 5 deletions cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down Expand Up @@ -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():
Expand Down
1 change: 1 addition & 0 deletions pkg/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/network/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 0 additions & 9 deletions pkg/network/server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand All @@ -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,
Expand Down
12 changes: 9 additions & 3 deletions pkg/network/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 5a7fa2d

Please sign in to comment.