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 (
"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"
@ -38,18 +37,10 @@ func TestAddHeaders(t *testing.T) {
}
func TestAddBlock(t *testing.T) {
const size = 3
bc := newTestChain(t)
blocks := []*block.Block{
newBlock(1, newMinerTX()),
newBlock(2, newMinerTX()),
newBlock(3, newMinerTX()),
}
for i := 0; i < len(blocks); i++ {
if err := bc.AddBlock(blocks[i]); err != nil {
t.Fatal(err)
}
}
blocks, err := bc.genBlocks(size)
require.NoError(t, err)
lastBlock := blocks[len(blocks)-1]
assert.Equal(t, lastBlock.Index, bc.HeaderHeight())
@ -112,13 +103,8 @@ func TestGetHeader(t *testing.T) {
func TestGetBlock(t *testing.T) {
bc := newTestChain(t)
blocks := makeBlocks(100)
for i := 0; i < len(blocks); i++ {
if err := bc.AddBlock(blocks[i]); err != nil {
t.Fatal(err)
}
}
blocks, err := bc.genBlocks(100)
require.NoError(t, err)
// Test unpersisted and persisted access
for j := 0; j < 2; j++ {
@ -136,13 +122,8 @@ func TestGetBlock(t *testing.T) {
func TestHasBlock(t *testing.T) {
bc := newTestChain(t)
blocks := makeBlocks(50)
for i := 0; i < len(blocks); i++ {
if err := bc.AddBlock(blocks[i]); err != nil {
t.Fatal(err)
}
}
blocks, err := bc.genBlocks(50)
require.NoError(t, err)
// Test unpersisted and persisted access
for j := 0; j < 2; j++ {
@ -187,10 +168,8 @@ func TestClose(t *testing.T) {
assert.NotNil(t, r)
}()
bc := newTestChain(t)
blocks := makeBlocks(10)
for i := 0; i < len(blocks); i++ {
require.NoError(t, bc.AddBlock(blocks[i]))
}
_, err := bc.genBlocks(10)
require.NoError(t, err)
bc.Close()
// It's a hack, but we use internal knowledge of MemoryStore
// 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
}
func makeBlocks(n int) []*block.Block {
func (bc *Blockchain) genBlocks(n int) ([]*block.Block, error) {
blocks := make([]*block.Block, n)
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 {
@ -175,13 +178,8 @@ func newDumbBlock() *block.Block {
func _(t *testing.T) {
bc := newTestChain(t)
n := 50
blocks := makeBlocks(n)
for i := 0; i < len(blocks); i++ {
if err := bc.AddBlock(blocks[i]); err != nil {
t.Fatal(err)
}
}
_, err := bc.genBlocks(n)
require.NoError(t, err)
tx1 := newMinerTX()