2018-02-04 19:54:51 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2019-02-19 11:48:48 +00:00
|
|
|
"context"
|
2018-02-04 19:54:51 +00:00
|
|
|
"testing"
|
2018-02-06 06:43:32 +00:00
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
"github.com/CityOfZion/neo-go/config"
|
2018-03-17 11:53:21 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
2019-09-16 09:33:53 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-03-09 15:55:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-20 17:39:32 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-02-04 19:54:51 +00:00
|
|
|
)
|
|
|
|
|
2018-02-06 06:43:32 +00:00
|
|
|
func TestAddHeaders(t *testing.T) {
|
2018-03-17 11:53:21 +00:00
|
|
|
bc := newTestChain(t)
|
2019-09-18 15:21:16 +00:00
|
|
|
defer func() {
|
|
|
|
require.NoError(t, bc.Close())
|
|
|
|
}()
|
2018-03-09 15:55:25 +00:00
|
|
|
h1 := newBlock(1).Header()
|
|
|
|
h2 := newBlock(2).Header()
|
|
|
|
h3 := newBlock(3).Header()
|
2018-02-04 19:54:51 +00:00
|
|
|
|
2018-02-06 06:43:32 +00:00
|
|
|
if err := bc.AddHeaders(h1, h2, h3); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-09 15:55:25 +00:00
|
|
|
|
|
|
|
assert.Equal(t, h3.Index, bc.HeaderHeight())
|
|
|
|
assert.Equal(t, uint32(0), bc.BlockHeight())
|
|
|
|
assert.Equal(t, h3.Hash(), bc.CurrentHeaderHash())
|
2018-03-10 12:04:06 +00:00
|
|
|
|
|
|
|
// Add them again, they should not be added.
|
|
|
|
if err := bc.AddHeaders(h3, h2, h1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, h3.Index, bc.HeaderHeight())
|
|
|
|
assert.Equal(t, uint32(0), bc.BlockHeight())
|
|
|
|
assert.Equal(t, h3.Hash(), bc.CurrentHeaderHash())
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddBlock(t *testing.T) {
|
2018-03-17 11:53:21 +00:00
|
|
|
bc := newTestChain(t)
|
2019-09-18 15:21:16 +00:00
|
|
|
defer func() {
|
|
|
|
require.NoError(t, bc.Close())
|
|
|
|
}()
|
2018-03-09 15:55:25 +00:00
|
|
|
blocks := []*Block{
|
|
|
|
newBlock(1),
|
|
|
|
newBlock(2),
|
|
|
|
newBlock(3),
|
2018-02-06 06:43:32 +00:00
|
|
|
}
|
2018-03-09 15:55:25 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(blocks); i++ {
|
|
|
|
if err := bc.AddBlock(blocks[i]); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lastBlock := blocks[len(blocks)-1]
|
|
|
|
assert.Equal(t, lastBlock.Index, bc.HeaderHeight())
|
|
|
|
assert.Equal(t, lastBlock.Hash(), bc.CurrentHeaderHash())
|
2018-03-25 10:45:54 +00:00
|
|
|
|
2019-09-24 15:51:20 +00:00
|
|
|
// This one tests persisting blocks, so it does need to persist()
|
|
|
|
require.NoError(t, bc.persist(context.Background()))
|
2018-03-09 15:55:25 +00:00
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
for _, block := range blocks {
|
|
|
|
key := storage.AppendPrefix(storage.DataBlock, block.Hash().BytesReverse())
|
|
|
|
if _, err := bc.Get(key); err != nil {
|
|
|
|
t.Fatalf("block %s not persisted", block.Hash())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
assert.Equal(t, lastBlock.Index, bc.BlockHeight())
|
|
|
|
assert.Equal(t, lastBlock.Hash(), bc.CurrentHeaderHash())
|
|
|
|
}
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
func TestGetHeader(t *testing.T) {
|
|
|
|
bc := newTestChain(t)
|
2019-09-18 15:21:16 +00:00
|
|
|
defer func() {
|
|
|
|
require.NoError(t, bc.Close())
|
|
|
|
}()
|
2018-03-17 11:53:21 +00:00
|
|
|
block := newBlock(1)
|
|
|
|
err := bc.AddBlock(block)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2019-09-24 15:51:20 +00:00
|
|
|
// Test unpersisted and persisted access
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
hash := block.Hash()
|
|
|
|
header, err := bc.GetHeader(hash)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, block.Header(), header)
|
|
|
|
|
|
|
|
b2 := newBlock(2)
|
|
|
|
_, err = bc.GetHeader(b2.Hash())
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.NoError(t, bc.persist(context.Background()))
|
|
|
|
}
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBlock(t *testing.T) {
|
|
|
|
bc := newTestChain(t)
|
2019-09-18 15:21:16 +00:00
|
|
|
defer func() {
|
|
|
|
require.NoError(t, bc.Close())
|
|
|
|
}()
|
2018-03-17 11:53:21 +00:00
|
|
|
blocks := makeBlocks(100)
|
|
|
|
|
|
|
|
for i := 0; i < len(blocks); i++ {
|
|
|
|
if err := bc.AddBlock(blocks[i]); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 15:51:20 +00:00
|
|
|
// Test unpersisted and persisted access
|
|
|
|
for j := 0; j < 2; j++ {
|
|
|
|
for i := 0; i < len(blocks); i++ {
|
|
|
|
block, err := bc.GetBlock(blocks[i].Hash())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, blocks[i].Index, block.Index)
|
|
|
|
assert.Equal(t, blocks[i].Hash(), block.Hash())
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
2019-09-24 15:51:20 +00:00
|
|
|
assert.NoError(t, bc.persist(context.Background()))
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHasBlock(t *testing.T) {
|
|
|
|
bc := newTestChain(t)
|
2019-09-18 15:21:16 +00:00
|
|
|
defer func() {
|
|
|
|
require.NoError(t, bc.Close())
|
|
|
|
}()
|
2018-03-17 11:53:21 +00:00
|
|
|
blocks := makeBlocks(50)
|
|
|
|
|
|
|
|
for i := 0; i < len(blocks); i++ {
|
|
|
|
if err := bc.AddBlock(blocks[i]); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 15:51:20 +00:00
|
|
|
// Test unpersisted and persisted access
|
|
|
|
for j := 0; j < 2; j++ {
|
|
|
|
for i := 0; i < len(blocks); i++ {
|
|
|
|
assert.True(t, bc.HasBlock(blocks[i].Hash()))
|
|
|
|
}
|
|
|
|
newBlock := newBlock(51)
|
|
|
|
assert.False(t, bc.HasBlock(newBlock.Hash()))
|
|
|
|
assert.NoError(t, bc.persist(context.Background()))
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 16:11:04 +00:00
|
|
|
func TestGetTransaction(t *testing.T) {
|
2019-09-26 15:14:00 +00:00
|
|
|
b1 := getDecodedBlock(t, 1)
|
2019-09-10 20:21:26 +00:00
|
|
|
block := getDecodedBlock(t, 2)
|
2018-03-21 16:11:04 +00:00
|
|
|
bc := newTestChain(t)
|
2019-09-18 15:21:16 +00:00
|
|
|
defer func() {
|
|
|
|
require.NoError(t, bc.Close())
|
|
|
|
}()
|
2018-03-21 16:11:04 +00:00
|
|
|
|
2019-09-26 15:14:00 +00:00
|
|
|
assert.Nil(t, bc.AddBlock(b1))
|
2018-03-21 16:11:04 +00:00
|
|
|
assert.Nil(t, bc.AddBlock(block))
|
|
|
|
|
2019-09-24 15:51:20 +00:00
|
|
|
// Test unpersisted and persisted access
|
|
|
|
for j := 0; j < 2; j++ {
|
|
|
|
tx, height, err := bc.GetTransaction(block.Transactions[0].Hash())
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, block.Index, height)
|
|
|
|
assert.Equal(t, block.Transactions[0], tx)
|
|
|
|
assert.Equal(t, 10, io.GetVarSize(tx))
|
|
|
|
assert.Equal(t, 1, io.GetVarSize(tx.Attributes))
|
|
|
|
assert.Equal(t, 1, io.GetVarSize(tx.Inputs))
|
|
|
|
assert.Equal(t, 1, io.GetVarSize(tx.Outputs))
|
|
|
|
assert.Equal(t, 1, io.GetVarSize(tx.Scripts))
|
|
|
|
assert.NoError(t, bc.persist(context.Background()))
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
func newTestChain(t *testing.T) *Blockchain {
|
2019-09-10 20:21:26 +00:00
|
|
|
cfg, err := config.Load("../../config", config.ModeUnitTestNet)
|
2018-03-25 10:45:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-09-17 12:27:40 +00:00
|
|
|
chain, err := NewBlockchain(storage.NewMemoryStore(), cfg.ProtocolConfiguration)
|
2018-03-17 11:53:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-09-18 15:21:16 +00:00
|
|
|
go chain.Run(context.Background())
|
2018-03-17 11:53:21 +00:00
|
|
|
return chain
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|