stateroot: handle double start/shutdown
This commit is contained in:
parent
bf462a81fe
commit
58b9ac41e2
2 changed files with 9 additions and 0 deletions
|
@ -14,6 +14,7 @@ import (
|
|||
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
||||
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
||||
"go.uber.org/atomic"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
|
@ -44,6 +45,7 @@ type (
|
|||
Network netmode.Magic
|
||||
|
||||
log *zap.Logger
|
||||
started *atomic.Bool
|
||||
accMtx sync.RWMutex
|
||||
accHeight uint32
|
||||
myIndex byte
|
||||
|
@ -72,6 +74,7 @@ func New(cfg config.StateRoot, sm *stateroot.Module, log *zap.Logger, bc Ledger,
|
|||
s := &service{
|
||||
Module: sm,
|
||||
Network: bcConf.Magic,
|
||||
started: atomic.NewBool(false),
|
||||
chain: bc,
|
||||
log: log,
|
||||
incompleteRoots: make(map[uint32]*incompleteRoot),
|
||||
|
|
|
@ -24,6 +24,9 @@ func (s *service) Name() string {
|
|||
|
||||
// Start runs service instance in a separate goroutine.
|
||||
func (s *service) Start() {
|
||||
if !s.started.CAS(false, true) {
|
||||
return
|
||||
}
|
||||
s.log.Info("starting state validation service")
|
||||
s.chain.SubscribeForBlocks(s.blockCh)
|
||||
go s.run()
|
||||
|
@ -60,6 +63,9 @@ drainloop:
|
|||
|
||||
// Shutdown stops the service.
|
||||
func (s *service) Shutdown() {
|
||||
if !s.started.CAS(true, false) {
|
||||
return
|
||||
}
|
||||
s.chain.UnsubscribeFromBlocks(s.blockCh)
|
||||
close(s.done)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue