Fix blockchain tests

This commit is contained in:
Jae Kwon 2018-04-05 05:17:43 -07:00
parent 196f8410ba
commit 1b9323f105
7 changed files with 49 additions and 22 deletions

10
Gopkg.lock generated
View File

@ -254,8 +254,8 @@
[[projects]]
name = "github.com/tendermint/go-amino"
packages = ["."]
revision = "f55c3351f30e5987500020631f00e87a035ed415"
version = "0.9.3"
revision = "26718ab6738f938d4b33d593543cee7681f2a6a6"
version = "0.9.5"
[[projects]]
name = "github.com/tendermint/go-crypto"
@ -288,8 +288,8 @@
"pubsub/query",
"test"
]
revision = "e9cf47606cfcbdc28a7c16671b4a70b459e9d4cc"
version = "v0.8.0-dev"
revision = "2e24b64fc121dcdf1cabceab8dc2f7257675483c"
version = "0.8.1"
[[projects]]
branch = "master"
@ -386,6 +386,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "9aace7a292e5402f97327860531e13b77f036c19843b698566ee958773404f48"
inputs-digest = "8cd32c0a5faec4d8cbca3d9ea5d97b0e4e90bfc2a9f0e6b71a4846cb6030be94"
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -79,11 +79,11 @@
[[constraint]]
name = "github.com/tendermint/go-amino"
version = "0.9.3"
version = "0.9.5"
[[constraint]]
name = "github.com/tendermint/tmlibs"
version = "0.8.0-dev"
version = "0.8.1"
[[constraint]]
name = "google.golang.org/grpc"

View File

@ -16,8 +16,12 @@ import (
func makeStateAndBlockStore(logger log.Logger) (sm.State, *BlockStore) {
config := cfg.ResetTestRoot("blockchain_reactor_test")
blockStore := NewBlockStore(dbm.NewMemDB())
state, err := sm.LoadStateFromDBOrGenesisFile(dbm.NewMemDB(), config.GenesisFile())
// blockDB := dbm.NewDebugDB("blockDB", dbm.NewMemDB())
// stateDB := dbm.NewDebugDB("stateDB", dbm.NewMemDB())
blockDB := dbm.NewMemDB()
stateDB := dbm.NewMemDB()
blockStore := NewBlockStore(blockDB)
state, err := sm.LoadStateFromDBOrGenesisFile(stateDB, config.GenesisFile())
if err != nil {
panic(cmn.ErrorWrap(err, "error constructing state from genesis file"))
}

View File

@ -53,14 +53,17 @@ func (bs *BlockStore) Height() int64 {
// LoadBlock returns the block with the given height.
// If no block is found for that height, it returns nil.
func (bs *BlockStore) LoadBlock(height int64) *types.Block {
var blockMeta *types.BlockMeta
var blockMeta = new(types.BlockMeta)
bz := bs.db.Get(calcBlockMetaKey(height))
if len(bz) == 0 {
return nil
}
err := cdc.UnmarshalBinaryBare(bz, blockMeta)
if err != nil {
panic(cmn.ErrorWrap(err, "Error reading block meta"))
}
var block *types.Block
var block = new(types.Block)
buf := []byte{}
for i := 0; i < blockMeta.BlockID.PartsHeader.Total; i++ {
part := bs.LoadBlockPart(height, i)
@ -68,6 +71,8 @@ func (bs *BlockStore) LoadBlock(height int64) *types.Block {
}
err = cdc.UnmarshalBinaryBare(buf, block)
if err != nil {
// NOTE: The existence of meta should imply the existence of the
// block. So, make sure meta is only saved after blocks are saved.
panic(cmn.ErrorWrap(err, "Error reading block"))
}
return block
@ -77,8 +82,11 @@ func (bs *BlockStore) LoadBlock(height int64) *types.Block {
// from the block at the given height.
// If no part is found for the given height and index, it returns nil.
func (bs *BlockStore) LoadBlockPart(height int64, index int) *types.Part {
var part *types.Part
var part = new(types.Part)
bz := bs.db.Get(calcBlockPartKey(height, index))
if len(bz) == 0 {
return nil
}
err := cdc.UnmarshalBinaryBare(bz, part)
if err != nil {
panic(cmn.ErrorWrap(err, "Error reading block part"))
@ -89,8 +97,11 @@ func (bs *BlockStore) LoadBlockPart(height int64, index int) *types.Part {
// LoadBlockMeta returns the BlockMeta for the given height.
// If no block is found for the given height, it returns nil.
func (bs *BlockStore) LoadBlockMeta(height int64) *types.BlockMeta {
var blockMeta *types.BlockMeta
var blockMeta = new(types.BlockMeta)
bz := bs.db.Get(calcBlockMetaKey(height))
if len(bz) == 0 {
return nil
}
err := cdc.UnmarshalBinaryBare(bz, blockMeta)
if err != nil {
panic(cmn.ErrorWrap(err, "Error reading block meta"))
@ -103,8 +114,11 @@ func (bs *BlockStore) LoadBlockMeta(height int64) *types.BlockMeta {
// and it comes from the block.LastCommit for `height+1`.
// If no commit is found for the given height, it returns nil.
func (bs *BlockStore) LoadBlockCommit(height int64) *types.Commit {
var commit *types.Commit
var commit = new(types.Commit)
bz := bs.db.Get(calcBlockCommitKey(height))
if len(bz) == 0 {
return nil
}
err := cdc.UnmarshalBinaryBare(bz, commit)
if err != nil {
panic(cmn.ErrorWrap(err, "Error reading block commit"))
@ -116,8 +130,11 @@ func (bs *BlockStore) LoadBlockCommit(height int64) *types.Commit {
// This is useful when we've seen a commit, but there has not yet been
// a new block at `height + 1` that includes this commit in its block.LastCommit.
func (bs *BlockStore) LoadSeenCommit(height int64) *types.Commit {
var commit *types.Commit
var commit = new(types.Commit)
bz := bs.db.Get(calcSeenCommitKey(height))
if len(bz) == 0 {
return nil
}
err := cdc.UnmarshalBinaryBare(bz, commit)
if err != nil {
panic(cmn.ErrorWrap(err, "Error reading block commit"))
@ -150,7 +167,8 @@ func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, s
// Save block parts
for i := 0; i < blockParts.Total(); i++ {
bs.saveBlockPart(height, i, blockParts.GetPart(i))
part := blockParts.GetPart(i)
bs.saveBlockPart(height, i, part)
}
// Save block commit (duplicate and separate from the Block)

View File

@ -153,14 +153,14 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) {
parts: validPartSet,
seenCommit: seenCommit1,
corruptCommitInDB: true, // Corrupt the DB's commit entry
wantPanic: "rror reading commit",
wantPanic: "Error reading block commit",
},
{
block: newBlock(&header1, commitAtH10),
parts: validPartSet,
seenCommit: seenCommit1,
wantPanic: "rror reading block",
wantPanic: "Error reading block",
corruptBlockInDB: true, // Corrupt the DB's block entry
},
@ -179,7 +179,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) {
seenCommit: seenCommit1,
corruptSeenCommitInDB: true,
wantPanic: "rror reading commit",
wantPanic: "Error reading block commit",
},
{
@ -341,6 +341,9 @@ func TestBlockFetchAtHeight(t *testing.T) {
require.Equal(t, bs.Height(), block.Header.Height, "expecting the new height to be changed")
blockAtHeight := bs.LoadBlock(bs.Height())
bz1 := cdc.MustMarshalBinaryBare(block)
bz2 := cdc.MustMarshalBinaryBare(blockAtHeight)
require.Equal(t, bz1, bz2)
require.Equal(t, block.Hash(), blockAtHeight.Hash(),
"expecting a successful load of the last saved block")

View File

@ -2,10 +2,12 @@ package blockchain
import (
"github.com/tendermint/go-amino"
"github.com/tendermint/go-crypto"
)
var cdc = amino.NewCodec()
func init() {
RegisterBlockchainMessages(cdc)
crypto.RegisterAmino(cdc)
}

View File

@ -94,7 +94,7 @@ func (b *Block) Hash() cmn.HexBytes {
// MakePartSet returns a PartSet containing parts of a serialized block.
// This is the form in which the block is gossipped to peers.
func (b *Block) MakePartSet(partSize int) *PartSet {
bz, err := cdc.MarshalBinary(b)
bz, err := cdc.MarshalBinaryBare(b)
if err != nil {
panic(err)
}
@ -498,7 +498,7 @@ func (blockID BlockID) Equals(other BlockID) bool {
// Key returns a machine-readable string representation of the BlockID
func (blockID BlockID) Key() string {
bz, err := cdc.MarshalBinary(blockID.PartsHeader)
bz, err := cdc.MarshalBinaryBare(blockID.PartsHeader)
if err != nil {
panic(err)
}
@ -518,7 +518,7 @@ type hasher struct {
func (h hasher) Hash() []byte {
hasher := ripemd160.New()
if h.item != nil && !cmn.IsTypedNil(h.item) {
if h.item != nil && !cmn.IsTypedNil(h.item) && !cmn.IsEmpty(h.item) {
bz, err := cdc.MarshalBinaryBare(h.item)
if err != nil {
panic(err)