Persist blockchain with leveldb on disk (#48)

* Created test_data folder with block json files for testing + create separate file for block base.

* Fixed bug in WriteVarUint + Trim logic + unit tests

* Refactored store and add more tests for it.

* restore headerList from chain file

* Fix tx decode bug + lots of housekeeping.

* Implemented Node restore state from chain file.

* Created standalone package for storage. Added couple more methods to Batch and Store interfaces.

* Block persisting + tests

* bumped version -> 0.31.0
This commit is contained in:
Anthony De Meulemeester 2018-03-17 12:53:21 +01:00 committed by GitHub
parent b41e14e0f0
commit a67728628e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
45 changed files with 1419 additions and 530 deletions

View file

@ -3,22 +3,13 @@ package core
import (
"testing"
"github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
)
func TestNewBlockchain(t *testing.T) {
startHash, _ := util.Uint256DecodeString("996e37358dc369912041f966f8c5d8d3a8255ba5dcbd3447f8a82b55db869099")
bc := NewBlockchain(nil, startHash)
assert.Equal(t, uint32(0), bc.BlockHeight())
assert.Equal(t, uint32(0), bc.HeaderHeight())
assert.Equal(t, uint32(1), bc.storedHeaderCount)
assert.Equal(t, startHash, bc.startHash)
}
func TestAddHeaders(t *testing.T) {
bc := newTestBC()
bc := newTestChain(t)
h1 := newBlock(1).Header()
h2 := newBlock(2).Header()
h3 := newBlock(3).Header()
@ -45,7 +36,7 @@ func TestAddHeaders(t *testing.T) {
}
func TestAddBlock(t *testing.T) {
bc := newTestBC()
bc := newTestChain(t)
blocks := []*Block{
newBlock(1),
newBlock(2),
@ -73,8 +64,69 @@ func TestAddBlock(t *testing.T) {
assert.Equal(t, 0, bc.blockCache.Len())
}
func newTestBC() *Blockchain {
startHash, _ := util.Uint256DecodeString("a")
bc := NewBlockchain(NewMemoryStore(), startHash)
return bc
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()))
}
func newTestChain(t *testing.T) *Blockchain {
startHash, _ := util.Uint256DecodeString("a")
chain, err := NewBlockchain(storage.NewMemoryStore(), startHash)
if err != nil {
t.Fatal(err)
}
return chain
}