forked from TrueCloudLab/neoneo-go
core: get rid of global variables in tests
It can lead to unnecessary race conditions and is just a bad practice.
This commit is contained in:
parent
8ca94e23c8
commit
357bb4ce41
2 changed files with 22 additions and 21 deletions
|
@ -3,6 +3,7 @@ package core
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/core/block"
|
||||
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
||||
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
||||
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
||||
|
@ -14,9 +15,10 @@ import (
|
|||
|
||||
func TestAddHeaders(t *testing.T) {
|
||||
bc := newTestChain(t)
|
||||
h1 := newBlock(1).Header()
|
||||
h2 := newBlock(2).Header()
|
||||
h3 := newBlock(3).Header()
|
||||
lastBlock := bc.topBlock.Load().(*block.Block)
|
||||
h1 := newBlock(bc.config, 1, lastBlock.Hash()).Header()
|
||||
h2 := newBlock(bc.config, 2, h1.Hash()).Header()
|
||||
h3 := newBlock(bc.config, 3, h2.Hash()).Header()
|
||||
|
||||
if err := bc.AddHeaders(h1, h2, h3); err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -83,7 +85,7 @@ func TestScriptFromWitness(t *testing.T) {
|
|||
|
||||
func TestGetHeader(t *testing.T) {
|
||||
bc := newTestChain(t)
|
||||
block := newBlock(1, newMinerTX())
|
||||
block := bc.newBlock(newMinerTX())
|
||||
err := bc.AddBlock(block)
|
||||
assert.Nil(t, err)
|
||||
|
||||
|
@ -94,7 +96,7 @@ func TestGetHeader(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
assert.Equal(t, block.Header(), header)
|
||||
|
||||
b2 := newBlock(2)
|
||||
b2 := bc.newBlock()
|
||||
_, err = bc.GetHeader(b2.Hash())
|
||||
assert.Error(t, err)
|
||||
assert.NoError(t, bc.persist())
|
||||
|
@ -130,7 +132,7 @@ func TestHasBlock(t *testing.T) {
|
|||
for i := 0; i < len(blocks); i++ {
|
||||
assert.True(t, bc.HasBlock(blocks[i].Hash()))
|
||||
}
|
||||
newBlock := newBlock(51)
|
||||
newBlock := bc.newBlock()
|
||||
assert.False(t, bc.HasBlock(newBlock.Hash()))
|
||||
assert.NoError(t, bc.persist())
|
||||
}
|
||||
|
|
|
@ -24,9 +24,6 @@ import (
|
|||
"go.uber.org/zap/zaptest"
|
||||
)
|
||||
|
||||
var newBlockPrevHash util.Uint256
|
||||
var unitTestNetCfg config.Config
|
||||
|
||||
var privNetKeys = []string{
|
||||
"KxyjQ8eUa4FHt3Gvioyt1Wz29cTUrE4eTqX3yFSk1YFCsPL8uNsY",
|
||||
"KzfPUYDC9n2yf4fK5ro4C8KMcdeXtFuEnStycbZgX3GomiUsvX6W",
|
||||
|
@ -37,8 +34,7 @@ var privNetKeys = []string{
|
|||
// newTestChain should be called before newBlock invocation to properly setup
|
||||
// global state.
|
||||
func newTestChain(t *testing.T) *Blockchain {
|
||||
var err error
|
||||
unitTestNetCfg, err = config.Load("../../config", config.ModeUnitTestNet)
|
||||
unitTestNetCfg, err := config.Load("../../config", config.ModeUnitTestNet)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -47,14 +43,16 @@ func newTestChain(t *testing.T) *Blockchain {
|
|||
t.Fatal(err)
|
||||
}
|
||||
go chain.Run()
|
||||
zeroHash, err := chain.GetHeader(chain.GetHeaderHash(0))
|
||||
require.Nil(t, err)
|
||||
newBlockPrevHash = zeroHash.Hash()
|
||||
return chain
|
||||
}
|
||||
|
||||
func newBlock(index uint32, txs ...*transaction.Transaction) *block.Block {
|
||||
validators, _ := getValidators(unitTestNetCfg.ProtocolConfiguration)
|
||||
func (bc *Blockchain) newBlock(txs ...*transaction.Transaction) *block.Block {
|
||||
lastBlock := bc.topBlock.Load().(*block.Block)
|
||||
return newBlock(bc.config, lastBlock.Index+1, lastBlock.Hash(), txs...)
|
||||
}
|
||||
|
||||
func newBlock(cfg config.ProtocolConfiguration, index uint32, prev util.Uint256, txs ...*transaction.Transaction) *block.Block {
|
||||
validators, _ := getValidators(cfg)
|
||||
vlen := len(validators)
|
||||
valScript, _ := smartcontract.CreateMultiSigRedeemScript(
|
||||
vlen-(vlen-1)/3,
|
||||
|
@ -66,7 +64,7 @@ func newBlock(index uint32, txs ...*transaction.Transaction) *block.Block {
|
|||
b := &block.Block{
|
||||
Base: block.Base{
|
||||
Version: 0,
|
||||
PrevHash: newBlockPrevHash,
|
||||
PrevHash: prev,
|
||||
Timestamp: uint32(time.Now().UTC().Unix()) + index,
|
||||
Index: index,
|
||||
ConsensusData: 1111,
|
||||
|
@ -76,7 +74,6 @@ func newBlock(index uint32, txs ...*transaction.Transaction) *block.Block {
|
|||
Transactions: txs,
|
||||
}
|
||||
_ = b.RebuildMerkleRoot()
|
||||
newBlockPrevHash = b.Hash()
|
||||
|
||||
invScript := make([]byte, 0)
|
||||
for _, wif := range privNetKeys {
|
||||
|
@ -98,11 +95,13 @@ func newBlock(index uint32, txs ...*transaction.Transaction) *block.Block {
|
|||
|
||||
func (bc *Blockchain) genBlocks(n int) ([]*block.Block, error) {
|
||||
blocks := make([]*block.Block, n)
|
||||
lastHash := bc.topBlock.Load().(*block.Block).Hash()
|
||||
for i := 0; i < n; i++ {
|
||||
blocks[i] = newBlock(uint32(i)+1, newMinerTX())
|
||||
blocks[i] = newBlock(bc.config, uint32(i)+1, lastHash, newMinerTX())
|
||||
if err := bc.AddBlock(blocks[i]); err != nil {
|
||||
return blocks, err
|
||||
}
|
||||
lastHash = blocks[i].Hash()
|
||||
}
|
||||
return blocks, nil
|
||||
}
|
||||
|
@ -207,7 +206,7 @@ func _(t *testing.T) {
|
|||
|
||||
tx2 := transaction.NewInvocationTX(txScript, util.Fixed8FromFloat(100))
|
||||
|
||||
block := newBlock(uint32(n+1), tx1, tx2)
|
||||
block := bc.newBlock(tx1, tx2)
|
||||
if err := bc.AddBlock(block); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -221,7 +220,7 @@ func _(t *testing.T) {
|
|||
emit.AppCall(script.BinWriter, hash.Hash160(avm), false)
|
||||
|
||||
tx3 := transaction.NewInvocationTX(script.Bytes(), util.Fixed8FromFloat(100))
|
||||
b := newBlock(uint32(n+2), newMinerTX(), tx3)
|
||||
b := bc.newBlock(newMinerTX(), tx3)
|
||||
require.NoError(t, bc.AddBlock(b))
|
||||
|
||||
outStream, err := os.Create("../rpc/testdata/testblocks.acc")
|
||||
|
|
Loading…
Reference in a new issue