stateroot: handle double start/shutdown

This commit is contained in:
Roman Khimov 2022-07-04 10:55:53 +03:00
parent bf462a81fe
commit 58b9ac41e2
2 changed files with 9 additions and 0 deletions

View file

@ -14,6 +14,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/network/payload" "github.com/nspcc-dev/neo-go/pkg/network/payload"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
"go.uber.org/atomic"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -44,6 +45,7 @@ type (
Network netmode.Magic Network netmode.Magic
log *zap.Logger log *zap.Logger
started *atomic.Bool
accMtx sync.RWMutex accMtx sync.RWMutex
accHeight uint32 accHeight uint32
myIndex byte myIndex byte
@ -72,6 +74,7 @@ func New(cfg config.StateRoot, sm *stateroot.Module, log *zap.Logger, bc Ledger,
s := &service{ s := &service{
Module: sm, Module: sm,
Network: bcConf.Magic, Network: bcConf.Magic,
started: atomic.NewBool(false),
chain: bc, chain: bc,
log: log, log: log,
incompleteRoots: make(map[uint32]*incompleteRoot), incompleteRoots: make(map[uint32]*incompleteRoot),

View file

@ -24,6 +24,9 @@ func (s *service) Name() string {
// Start runs service instance in a separate goroutine. // Start runs service instance in a separate goroutine.
func (s *service) Start() { func (s *service) Start() {
if !s.started.CAS(false, true) {
return
}
s.log.Info("starting state validation service") s.log.Info("starting state validation service")
s.chain.SubscribeForBlocks(s.blockCh) s.chain.SubscribeForBlocks(s.blockCh)
go s.run() go s.run()
@ -60,6 +63,9 @@ drainloop:
// Shutdown stops the service. // Shutdown stops the service.
func (s *service) Shutdown() { func (s *service) Shutdown() {
if !s.started.CAS(true, false) {
return
}
s.chain.UnsubscribeFromBlocks(s.blockCh) s.chain.UnsubscribeFromBlocks(s.blockCh)
close(s.done) close(s.done)
} }