blockchain: less fragile and involved tests for blockstore

With feedback from @ebuchman, to make the tests nicer
and less fragile.
This commit is contained in:
Emmanuel Odeke
2017-10-14 19:21:13 -06:00
committed by Ethan Buchman
parent 83b40b25d6
commit 2da5299924
4 changed files with 258 additions and 39 deletions

View File

@ -12,9 +12,9 @@ import (
"github.com/stretchr/testify/require"
"github.com/tendermint/go-wire"
"github.com/tendermint/go-wire/data"
"github.com/tendermint/tendermint/types"
"github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
)
func TestLoadBlockStoreStateJSON(t *testing.T) {
@ -97,31 +97,31 @@ func freshBlockStore() (*BlockStore, db.DB) {
}
var (
// Setup, test data
// If needed, the parts' data can be generated by running
// the code at https://gist.github.com/odeke-em/9ffac2b5df44595fad7084ece4c9bd98
part1 = &types.Part{Index: 0, Bytes: data.Bytes([]byte{
0x01, 0x01, 0x01, 0x0a, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x5f, 0x74, 0x65,
0x73, 0x74, 0x01, 0x01, 0xa1, 0xb2, 0x03, 0xeb, 0x3d, 0x1f, 0x44, 0x40, 0x01, 0x64, 0x00,
})}
part2 = &types.Part{Index: 1, Bytes: data.Bytes([]byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00,
0x00, 0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
})}
state, _ = makeStateAndBlockStore(log.NewTMLogger(new(bytes.Buffer)))
block = makeBlock(1, state)
partSet = block.MakePartSet(2)
part1 = partSet.GetPart(0)
part2 = partSet.GetPart(1)
seenCommit1 = &types.Commit{Precommits: []*types.Vote{{Height: 10}}}
)
func TestBlockStoreSaveLoadBlock(t *testing.T) {
bs, _ := freshBlockStore()
state, bs := makeStateAndBlockStore(log.NewTMLogger(new(bytes.Buffer)))
require.Equal(t, bs.Height(), 0, "initially the height should be zero")
noBlockHeights := []int{0, -1, 100, 1000, 2}
for i, height := range noBlockHeights {
if g := bs.LoadBlock(height); g != nil {
t.Errorf("#%d: height(%d) got a block; want nil", i, height)
}
}
validPartSet := types.NewPartSetFromHeader(types.PartSetHeader{Total: 2})
validPartSet.AddPart(part1, false)
validPartSet.AddPart(part2, false)
block := makeBlock(bs.Height()+1, state)
validPartSet := block.MakePartSet(2)
seenCommit := &types.Commit{Precommits: []*types.Vote{{Height: 10}}}
bs.SaveBlock(block, partSet, seenCommit)
require.Equal(t, bs.Height(), block.Header.Height, "expecting the new height to be changed")
incompletePartSet := types.NewPartSetFromHeader(types.PartSetHeader{Total: 2})
@ -382,25 +382,14 @@ func TestLoadBlockMeta(t *testing.T) {
}
func TestBlockFetchAtHeight(t *testing.T) {
bs, _ := freshBlockStore()
block := &types.Block{
Header: &types.Header{
Height: 1,
NumTxs: 100,
ChainID: "block_test",
},
LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}},
}
seenCommit := seenCommit1
validPartSet := types.NewPartSetFromHeader(types.PartSetHeader{Total: 2})
validPartSet.AddPart(part1, false)
validPartSet.AddPart(part2, false)
parts := validPartSet
state, bs := makeStateAndBlockStore(log.NewTMLogger(new(bytes.Buffer)))
require.Equal(t, bs.Height(), 0, "initially the height should be zero")
require.NotEqual(t, bs.Height(), block.Header.Height, "expecting different heights initially")
block := makeBlock(bs.Height()+1, state)
bs.SaveBlock(block, parts, seenCommit)
partSet := block.MakePartSet(2)
seenCommit := &types.Commit{Precommits: []*types.Vote{{Height: 10}}}
bs.SaveBlock(block, partSet, seenCommit)
require.Equal(t, bs.Height(), block.Header.Height, "expecting the new height to be changed")
blockAtHeight := bs.LoadBlock(bs.Height())