core: replace makeBlocks() with addBlocks() in tests

This simplifies tests a bit.
This commit is contained in:
Evgenii Stratonikov 2020-02-29 17:16:13 +03:00
parent a2b9d85c80
commit 8ca94e23c8
2 changed files with 17 additions and 40 deletions

View file

@ -3,7 +3,6 @@ package core
import ( import (
"testing" "testing"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/hash" "github.com/CityOfZion/neo-go/pkg/crypto/hash"
@ -38,18 +37,10 @@ func TestAddHeaders(t *testing.T) {
} }
func TestAddBlock(t *testing.T) { func TestAddBlock(t *testing.T) {
const size = 3
bc := newTestChain(t) bc := newTestChain(t)
blocks := []*block.Block{ blocks, err := bc.genBlocks(size)
newBlock(1, newMinerTX()), require.NoError(t, err)
newBlock(2, newMinerTX()),
newBlock(3, newMinerTX()),
}
for i := 0; i < len(blocks); i++ {
if err := bc.AddBlock(blocks[i]); err != nil {
t.Fatal(err)
}
}
lastBlock := blocks[len(blocks)-1] lastBlock := blocks[len(blocks)-1]
assert.Equal(t, lastBlock.Index, bc.HeaderHeight()) assert.Equal(t, lastBlock.Index, bc.HeaderHeight())
@ -112,13 +103,8 @@ func TestGetHeader(t *testing.T) {
func TestGetBlock(t *testing.T) { func TestGetBlock(t *testing.T) {
bc := newTestChain(t) bc := newTestChain(t)
blocks := makeBlocks(100) blocks, err := bc.genBlocks(100)
require.NoError(t, err)
for i := 0; i < len(blocks); i++ {
if err := bc.AddBlock(blocks[i]); err != nil {
t.Fatal(err)
}
}
// Test unpersisted and persisted access // Test unpersisted and persisted access
for j := 0; j < 2; j++ { for j := 0; j < 2; j++ {
@ -136,13 +122,8 @@ func TestGetBlock(t *testing.T) {
func TestHasBlock(t *testing.T) { func TestHasBlock(t *testing.T) {
bc := newTestChain(t) bc := newTestChain(t)
blocks := makeBlocks(50) blocks, err := bc.genBlocks(50)
require.NoError(t, err)
for i := 0; i < len(blocks); i++ {
if err := bc.AddBlock(blocks[i]); err != nil {
t.Fatal(err)
}
}
// Test unpersisted and persisted access // Test unpersisted and persisted access
for j := 0; j < 2; j++ { for j := 0; j < 2; j++ {
@ -187,10 +168,8 @@ func TestClose(t *testing.T) {
assert.NotNil(t, r) assert.NotNil(t, r)
}() }()
bc := newTestChain(t) bc := newTestChain(t)
blocks := makeBlocks(10) _, err := bc.genBlocks(10)
for i := 0; i < len(blocks); i++ { require.NoError(t, err)
require.NoError(t, bc.AddBlock(blocks[i]))
}
bc.Close() bc.Close()
// It's a hack, but we use internal knowledge of MemoryStore // It's a hack, but we use internal knowledge of MemoryStore
// implementation which makes it completely unusable (up to panicing) // implementation which makes it completely unusable (up to panicing)

View file

@ -96,12 +96,15 @@ func newBlock(index uint32, txs ...*transaction.Transaction) *block.Block {
return b return b
} }
func makeBlocks(n int) []*block.Block { func (bc *Blockchain) genBlocks(n int) ([]*block.Block, error) {
blocks := make([]*block.Block, n) blocks := make([]*block.Block, n)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
blocks[i] = newBlock(uint32(i+1), newMinerTX()) blocks[i] = newBlock(uint32(i)+1, newMinerTX())
if err := bc.AddBlock(blocks[i]); err != nil {
return blocks, err
}
} }
return blocks return blocks, nil
} }
func newMinerTX() *transaction.Transaction { func newMinerTX() *transaction.Transaction {
@ -175,13 +178,8 @@ func newDumbBlock() *block.Block {
func _(t *testing.T) { func _(t *testing.T) {
bc := newTestChain(t) bc := newTestChain(t)
n := 50 n := 50
blocks := makeBlocks(n) _, err := bc.genBlocks(n)
require.NoError(t, err)
for i := 0; i < len(blocks); i++ {
if err := bc.AddBlock(blocks[i]); err != nil {
t.Fatal(err)
}
}
tx1 := newMinerTX() tx1 := newMinerTX()