2018-02-04 19:54:51 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-02-06 06:43:32 +00:00
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
2018-02-06 06:43:32 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2018-03-09 15:55:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
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(1), bc.storedHeaderCount)
|
|
|
|
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(1), bc.storedHeaderCount)
|
|
|
|
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())
|
|
|
|
assert.Equal(t, uint32(1), bc.storedHeaderCount)
|
|
|
|
|
|
|
|
if err := bc.persist(); err != nil {
|
|
|
|
t.Fatal(err)
|
2018-02-06 06:43:32 +00:00
|
|
|
}
|
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()
|
|
|
|
header, err := bc.getHeader(hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, block.Header(), header)
|
|
|
|
|
|
|
|
block = newBlock(2)
|
|
|
|
hash = block.Hash()
|
|
|
|
_, err = bc.getHeader(block.Hash())
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Nil(t, bc.persist())
|
|
|
|
|
|
|
|
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) {
|
|
|
|
block := getDecodedBlock(t, 1)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
func newTestChain(t *testing.T) *Blockchain {
|
2018-03-09 15:55:25 +00:00
|
|
|
startHash, _ := util.Uint256DecodeString("a")
|
2018-03-17 11:53:21 +00:00
|
|
|
chain, err := NewBlockchain(storage.NewMemoryStore(), startHash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return chain
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|