forked from TrueCloudLab/neoneo-go
core: add ability to check whether blockchain is running
This commit is contained in:
parent
eb0494764c
commit
1dac45bbbb
2 changed files with 23 additions and 0 deletions
|
@ -151,6 +151,8 @@ type Blockchain struct {
|
||||||
// Stop synchronization mechanisms.
|
// Stop synchronization mechanisms.
|
||||||
stopCh chan struct{}
|
stopCh chan struct{}
|
||||||
runToExitCh chan struct{}
|
runToExitCh chan struct{}
|
||||||
|
// isRunning denotes whether blockchain routines are currently running.
|
||||||
|
isRunning atomic.Value
|
||||||
|
|
||||||
memPool *mempool.Pool
|
memPool *mempool.Pool
|
||||||
|
|
||||||
|
@ -302,6 +304,7 @@ func NewBlockchain(s storage.Store, cfg config.ProtocolConfiguration, log *zap.L
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bc.isRunning.Store(false)
|
||||||
return bc, nil
|
return bc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -658,6 +661,7 @@ func (bc *Blockchain) initializeNativeCache(blockHeight uint32, d *dao.Simple) e
|
||||||
// Run runs chain loop, it needs to be run as goroutine and executing it is
|
// Run runs chain loop, it needs to be run as goroutine and executing it is
|
||||||
// critical for correct Blockchain operation.
|
// critical for correct Blockchain operation.
|
||||||
func (bc *Blockchain) Run() {
|
func (bc *Blockchain) Run() {
|
||||||
|
bc.isRunning.Store(true)
|
||||||
persistTimer := time.NewTimer(persistInterval)
|
persistTimer := time.NewTimer(persistInterval)
|
||||||
defer func() {
|
defer func() {
|
||||||
persistTimer.Stop()
|
persistTimer.Stop()
|
||||||
|
@ -667,6 +671,7 @@ func (bc *Blockchain) Run() {
|
||||||
if err := bc.dao.Store.Close(); err != nil {
|
if err := bc.dao.Store.Close(); err != nil {
|
||||||
bc.log.Warn("failed to close db", zap.Error(err))
|
bc.log.Warn("failed to close db", zap.Error(err))
|
||||||
}
|
}
|
||||||
|
bc.isRunning.Store(false)
|
||||||
close(bc.runToExitCh)
|
close(bc.runToExitCh)
|
||||||
}()
|
}()
|
||||||
go bc.notificationDispatcher()
|
go bc.notificationDispatcher()
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -340,3 +341,20 @@ func TestBlockchain_BaseExecFeeBaseStoragePrice_Compat(t *testing.T) {
|
||||||
check(t)
|
check(t)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBlockchain_IsRunning(t *testing.T) {
|
||||||
|
chain := initTestChain(t, nil, nil)
|
||||||
|
require.False(t, chain.isRunning.Load().(bool))
|
||||||
|
oldPersisted := atomic.LoadUint32(&chain.persistedHeight)
|
||||||
|
|
||||||
|
go chain.Run()
|
||||||
|
require.NoError(t, chain.AddBlock(chain.newBlock()))
|
||||||
|
require.Eventually(t, func() bool {
|
||||||
|
persisted := atomic.LoadUint32(&chain.persistedHeight)
|
||||||
|
return persisted > oldPersisted
|
||||||
|
}, 2*persistInterval, 100*time.Millisecond)
|
||||||
|
require.True(t, chain.isRunning.Load().(bool))
|
||||||
|
|
||||||
|
chain.Close()
|
||||||
|
require.False(t, chain.isRunning.Load().(bool))
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue