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-02-20 17:39:32 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
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)
|
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, 0, bc.blockCache.Len())
|
|
|
|
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)
|
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, 3, bc.blockCache.Len())
|
|
|
|
assert.Equal(t, lastBlock.Index, bc.HeaderHeight())
|
|
|
|
assert.Equal(t, lastBlock.Hash(), bc.CurrentHeaderHash())
|
2018-03-25 10:45:54 +00:00
|
|
|
|
|
|
|
t.Log(bc.blockCache)
|
2018-03-09 15:55:25 +00:00
|
|
|
|
2019-02-19 11:48:48 +00:00
|
|
|
if err := bc.persist(context.Background()); err != nil {
|
2018-03-09 15:55:25 +00:00
|
|
|
t.Fatal(err)
|
2018-02-06 06:43:32 +00:00
|
|
|
}
|
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())
|
|
|
|
assert.Equal(t, 0, bc.blockCache.Len())
|
|
|
|
}
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
func TestGetHeader(t *testing.T) {
|
|
|
|
bc := newTestChain(t)
|
|
|
|
block := newBlock(1)
|
|
|
|
err := bc.AddBlock(block)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
hash := block.Hash()
|
2019-02-20 17:39:32 +00:00
|
|
|
header, err := bc.GetHeader(hash)
|
|
|
|
require.NoError(t, err)
|
2018-03-17 11:53:21 +00:00
|
|
|
assert.Equal(t, block.Header(), header)
|
|
|
|
|
|
|
|
block = newBlock(2)
|
2019-02-20 17:39:32 +00:00
|
|
|
_, err = bc.GetHeader(block.Hash())
|
|
|
|
assert.Error(t, err)
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2019-02-19 11:48:48 +00:00
|
|
|
assert.Nil(t, bc.persist(context.Background()))
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
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()))
|
|
|
|
}
|
|
|
|
|
2018-03-21 16:11:04 +00:00
|
|
|
func TestGetTransaction(t *testing.T) {
|
2019-09-10 20:21:26 +00:00
|
|
|
block := getDecodedBlock(t, 2)
|
2018-03-21 16:11:04 +00:00
|
|
|
bc := newTestChain(t)
|
|
|
|
|
|
|
|
assert.Nil(t, bc.AddBlock(block))
|
|
|
|
assert.Nil(t, bc.persistBlock(block))
|
|
|
|
|
|
|
|
tx, height, err := bc.GetTransaction(block.Transactions[0].Hash())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, block.Index, height)
|
|
|
|
assert.Equal(t, block.Transactions[0], tx)
|
2019-09-10 20:21:26 +00:00
|
|
|
assert.Equal(t, 10, tx.Size())
|
|
|
|
assert.Equal(t, 1, util.GetVarSize(tx.Attributes))
|
|
|
|
assert.Equal(t, 1, util.GetVarSize(tx.Inputs))
|
|
|
|
assert.Equal(t, 1, util.GetVarSize(tx.Outputs))
|
|
|
|
assert.Equal(t, 1, util.GetVarSize(tx.Scripts))
|
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-02-19 11:48:48 +00:00
|
|
|
chain, err := NewBlockchain(context.Background(), storage.NewMemoryStore(), cfg.ProtocolConfiguration)
|
2018-03-17 11:53:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return chain
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|