From 8c86bb802477e126d6a99b07a1c4308651030897 Mon Sep 17 00:00:00 2001 From: Emmanuel Odeke Date: Wed, 20 Sep 2017 21:21:39 -0600 Subject: [PATCH 1/8] blockchain: add tests and more docs for BlockStore Add tests to test store, to the fullest reasonable extent for paths that can taken by input arguments altering internal behavior, as well as by mutating content in the DB. --- blockchain/store.go | 41 ++++ blockchain/store_test.go | 459 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 500 insertions(+) create mode 100644 blockchain/store_test.go diff --git a/blockchain/store.go b/blockchain/store.go index c77f67ed8..cff26c42b 100644 --- a/blockchain/store.go +++ b/blockchain/store.go @@ -35,6 +35,9 @@ type BlockStore struct { height int64 } +// NewBlockStore loads a blockStore's JSON serialized form from the +// database, db to retrieve the starting height of the blockstore +// and backs db as the internal database of the blockstore. func NewBlockStore(db dbm.DB) *BlockStore { bsjson := LoadBlockStoreStateJSON(db) return &BlockStore{ @@ -50,6 +53,10 @@ func (bs *BlockStore) Height() int64 { return bs.height } +// GetReader conveniently wraps the result of the database +// lookup for key key into an io.Reader. If no result is found, +// it returns nil otherwise it creates an io.Reader. +// Its utility is mainly for use with wire.ReadBinary. func (bs *BlockStore) GetReader(key []byte) io.Reader { bytez := bs.db.Get(key) if bytez == nil { @@ -58,6 +65,13 @@ func (bs *BlockStore) GetReader(key []byte) io.Reader { return bytes.NewReader(bytez) } +// LoadBlock retrieves the serialized block, keyed by height in the +// store's database. If the data at the requested height is not found, +// it returns nil. However, if the block meta data is found but +// cannot be deserialized by wire.ReadBinary, it panics. +// The serialized data consists of the BlockMeta data and different +// parts that are reassembled by their internal Data. If the final +// reassembled data cannot be deserialized by wire.ReadBinary, it panics. func (bs *BlockStore) LoadBlock(height int64) *types.Block { var n int var err error @@ -81,6 +95,11 @@ func (bs *BlockStore) LoadBlock(height int64) *types.Block { return block } +// LoadBlockPart tries to load a blockPart from the +// backing database, keyed by height and index. +// If it doesn't find the requested blockPart, it +// returns nil. Otherwise, If the found part is +// corrupted/not deserializable by wire.ReadBinary, it panics. func (bs *BlockStore) LoadBlockPart(height int64, index int) *types.Part { var n int var err error @@ -95,6 +114,10 @@ func (bs *BlockStore) LoadBlockPart(height int64, index int) *types.Part { return part } +// LoadBlockMeta tries to load a block meta from the backing database, +// keyed by height. The block meta must have been wire.Binary serialized. +// If it doesn't find the requested meta, it returns nil. Otherwise, +// if the found data cannot be deserialized by wire.ReadBinary, it panics. func (bs *BlockStore) LoadBlockMeta(height int64) *types.BlockMeta { var n int var err error @@ -109,6 +132,11 @@ func (bs *BlockStore) LoadBlockMeta(height int64) *types.BlockMeta { return blockMeta } +// LoadBlockCommit tries to load a commit from the backing database, +// keyed by height. The commit must have been wire.Binary serialized. +// If it doesn't find the requested commit in the database, it returns nil. +// Otherwise, if the found data cannot be deserialized by wire.ReadBinary, it panics. +// // The +2/3 and other Precommit-votes for block at `height`. // This Commit comes from block.LastCommit for `height+1`. func (bs *BlockStore) LoadBlockCommit(height int64) *types.Commit { @@ -125,6 +153,11 @@ func (bs *BlockStore) LoadBlockCommit(height int64) *types.Commit { return commit } +// LoadSeenCommit tries to load the seen commit from the backing database, +// keyed by height. The commit must have been wire.Binary serialized. +// If it doesn't find the requested commit in the database, it returns nil. +// Otherwise, if the found data cannot be deserialized by wire.ReadBinary, it panics. +// // NOTE: the Precommit-vote heights are for the block at `height` func (bs *BlockStore) LoadSeenCommit(height int64) *types.Commit { var n int @@ -146,6 +179,9 @@ func (bs *BlockStore) LoadSeenCommit(height int64) *types.Commit { // we need this to reload the precommits to catch-up nodes to the // most recent height. Otherwise they'd stall at H-1. func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) { + if block == nil { + PanicSanity("BlockStore can only save a non-nil block") + } height := block.Height if height != bs.Height()+1 { cmn.PanicSanity(cmn.Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.Height()+1, height)) @@ -219,6 +255,7 @@ type BlockStoreStateJSON struct { Height int64 } +// Save JSON marshals the blockStore state to the database, saving it synchronously. func (bsj BlockStoreStateJSON) Save(db dbm.DB) { bytes, err := json.Marshal(bsj) if err != nil { @@ -227,6 +264,10 @@ func (bsj BlockStoreStateJSON) Save(db dbm.DB) { db.SetSync(blockStoreKey, bytes) } +// LoadBlockStoreStateJSON JSON unmarshals the +// blockStore state from the database, keyed by +// key "blockStore". If it cannot lookup the state, +// it returns the zero value BlockStoreStateJSON. func LoadBlockStoreStateJSON(db dbm.DB) BlockStoreStateJSON { bytes := db.Get(blockStoreKey) if bytes == nil { diff --git a/blockchain/store_test.go b/blockchain/store_test.go new file mode 100644 index 000000000..65832e22f --- /dev/null +++ b/blockchain/store_test.go @@ -0,0 +1,459 @@ +package blockchain + +import ( + "bytes" + "fmt" + "io/ioutil" + "runtime/debug" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "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" +) + +func TestLoadBlockStoreStateJSON(t *testing.T) { + db := db.NewMemDB() + + bsj := &BlockStoreStateJSON{Height: 1000} + bsj.Save(db) + + retrBSJ := LoadBlockStoreStateJSON(db) + + assert.Equal(t, *bsj, retrBSJ, "expected the retrieved DBs to match") +} + +func TestNewBlockStore(t *testing.T) { + db := db.NewMemDB() + db.Set(blockStoreKey, []byte(`{"height": 10000}`)) + bs := NewBlockStore(db) + assert.Equal(t, bs.Height(), 10000, "failed to properly parse blockstore") + + panicCausers := []struct { + data []byte + wantErr string + }{ + {[]byte("artful-doger"), "not unmarshal bytes"}, + {[]byte(""), "unmarshal bytes"}, + {[]byte(" "), "unmarshal bytes"}, + } + + for i, tt := range panicCausers { + // Expecting a panic here on trying to parse an invalid blockStore + _, _, panicErr := doFn(func() (interface{}, error) { + db.Set(blockStoreKey, tt.data) + _ = NewBlockStore(db) + return nil, nil + }) + require.NotNil(t, panicErr, "#%d panicCauser: %q expected a panic", i, tt.data) + assert.Contains(t, panicErr.Error(), tt.wantErr, "#%d data: %q", i, tt.data) + } + + db.Set(blockStoreKey, nil) + bs = NewBlockStore(db) + assert.Equal(t, bs.Height(), 0, "expecting nil bytes to be unmarshaled alright") +} + +func TestBlockStoreGetReader(t *testing.T) { + db := db.NewMemDB() + // Initial setup + db.Set([]byte("Foo"), []byte("Bar")) + db.Set([]byte("Foo1"), nil) + + bs := NewBlockStore(db) + + tests := [...]struct { + key []byte + want []byte + }{ + 0: {key: []byte("Foo"), want: []byte("Bar")}, + 1: {key: []byte("KnoxNonExistent"), want: nil}, + 2: {key: []byte("Foo1"), want: nil}, + } + + for i, tt := range tests { + r := bs.GetReader(tt.key) + if r == nil { + assert.Nil(t, tt.want, "#%d: expected a non-nil reader", i) + continue + } + slurp, err := ioutil.ReadAll(r) + if err != nil { + t.Errorf("#%d: unexpected Read err: %v", i, err) + } else { + assert.Equal(t, slurp, tt.want, "#%d: mismatch", i) + } + } +} + +func freshBlockStore() (*BlockStore, db.DB) { + db := db.NewMemDB() + return NewBlockStore(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, + })} + seenCommit1 = &types.Commit{Precommits: []*types.Vote{{Height: 10}}} +) + +func TestBlockStoreSaveLoadBlock(t *testing.T) { + bs, _ := freshBlockStore() + 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) + + incompletePartSet := types.NewPartSetFromHeader(types.PartSetHeader{Total: 2}) + + uncontiguousPartSet := types.NewPartSetFromHeader(types.PartSetHeader{Total: 0}) + uncontiguousPartSet.AddPart(part2, false) + + // End of setup, test data + + tuples := []struct { + block *types.Block + parts *types.PartSet + seenCommit *types.Commit + wantErr bool + wantPanic string + + corruptBlockInDB bool + corruptCommitInDB bool + corruptSeenCommitInDB bool + eraseCommitInDB bool + eraseSeenCommitInDB bool + }{ + { + block: &types.Block{ + Header: &types.Header{ + Height: 1, + NumTxs: 100, + ChainID: "block_test", + }, + LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, + }, + parts: validPartSet, + seenCommit: seenCommit1, + }, + + { + block: nil, + wantPanic: "only save a non-nil block", + }, + + { + block: &types.Block{ + Header: &types.Header{ + Height: 4, + NumTxs: 100, + ChainID: "block_test", + }, + LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, + }, + parts: uncontiguousPartSet, + wantPanic: "only save contiguous blocks", // and incomplete and uncontiguous parts + }, + + { + block: &types.Block{ + Header: &types.Header{ + Height: 1, + NumTxs: 100, + ChainID: "block_test", + }, + LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, + }, + parts: incompletePartSet, + wantPanic: "only save complete block", // incomplete parts + }, + + { + block: &types.Block{ + Header: &types.Header{ + Height: 1, + NumTxs: 100, + ChainID: "block_test", + }, + LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, + }, + parts: validPartSet, + seenCommit: seenCommit1, + corruptCommitInDB: true, // Corrupt the DB's commit entry + wantPanic: "rror reading commit", + }, + + { + block: &types.Block{ + Header: &types.Header{ + Height: 1, + NumTxs: 100, + ChainID: "block_test", + }, + LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, + }, + parts: validPartSet, + seenCommit: seenCommit1, + wantPanic: "rror reading block", + corruptBlockInDB: true, // Corrupt the DB's block entry + }, + + { + block: &types.Block{ + Header: &types.Header{ + Height: 1, + NumTxs: 100, + ChainID: "block_test", + }, + LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, + }, + parts: validPartSet, + seenCommit: seenCommit1, + + // Expecting no error and we want a nil back + eraseSeenCommitInDB: true, + }, + + { + block: &types.Block{ + Header: &types.Header{ + Height: 1, + NumTxs: 100, + ChainID: "block_test", + }, + LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, + }, + parts: validPartSet, + seenCommit: seenCommit1, + + corruptSeenCommitInDB: true, + wantPanic: "rror reading commit", + }, + + { + block: &types.Block{ + Header: &types.Header{ + Height: 1, + NumTxs: 100, + ChainID: "block_test", + }, + LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, + }, + parts: validPartSet, + seenCommit: seenCommit1, + + // Expecting no error and we want a nil back + eraseCommitInDB: true, + }, + } + + type quad struct { + block *types.Block + commit *types.Commit + meta *types.BlockMeta + + seenCommit *types.Commit + } + + for i, tuple := range tuples { + bs, db := freshBlockStore() + // SaveBlock + res, err, panicErr := doFn(func() (interface{}, error) { + bs.SaveBlock(tuple.block, tuple.parts, tuple.seenCommit) + if tuple.block == nil { + return nil, nil + } + + if tuple.corruptBlockInDB { + db.Set(calcBlockMetaKey(tuple.block.Height), []byte("block-bogus")) + } + bBlock := bs.LoadBlock(tuple.block.Height) + bBlockMeta := bs.LoadBlockMeta(tuple.block.Height) + + if tuple.eraseSeenCommitInDB { + db.Delete(calcSeenCommitKey(tuple.block.Height)) + } + if tuple.corruptSeenCommitInDB { + db.Set(calcSeenCommitKey(tuple.block.Height), []byte("bogus-seen-commit")) + } + bSeenCommit := bs.LoadSeenCommit(tuple.block.Height) + + commitHeight := tuple.block.Height - 1 + if tuple.eraseCommitInDB { + db.Delete(calcBlockCommitKey(commitHeight)) + } + if tuple.corruptCommitInDB { + db.Set(calcBlockCommitKey(commitHeight), []byte("foo-bogus")) + } + bCommit := bs.LoadBlockCommit(commitHeight) + return &quad{block: bBlock, seenCommit: bSeenCommit, commit: bCommit, meta: bBlockMeta}, nil + }) + + if subStr := tuple.wantPanic; subStr != "" { + if panicErr == nil { + t.Errorf("#%d: want a non-nil panic", i) + } else if got := panicErr.Error(); !strings.Contains(got, subStr) { + t.Errorf("#%d:\n\tgotErr: %q\nwant substring: %q", i, got, subStr) + } + continue + } + + if tuple.wantErr { + if err == nil { + t.Errorf("#%d: got nil error", i) + } + continue + } + + assert.Nil(t, panicErr, "#%d: unexpected panic", i) + assert.Nil(t, err, "#%d: expecting a non-nil error", i) + qua, ok := res.(*quad) + if !ok || qua == nil { + t.Errorf("#%d: got nil quad back; gotType=%T", i, res) + continue + } + if tuple.eraseSeenCommitInDB { + assert.Nil(t, qua.seenCommit, "erased the seenCommit in the DB hence we should get back a nil seenCommit") + } + if tuple.eraseCommitInDB { + assert.Nil(t, qua.commit, "erased the commit in the DB hence we should get back a nil commit") + } + } +} + +func binarySerializeIt(v interface{}) []byte { + var n int + var err error + buf := new(bytes.Buffer) + wire.WriteBinary(v, buf, &n, &err) + return buf.Bytes() +} + +func TestLoadBlockPart(t *testing.T) { + bs, db := freshBlockStore() + height, index := 10, 1 + loadPart := func() (interface{}, error) { + part := bs.LoadBlockPart(height, index) + return part, nil + } + + // Initially no contents. + // 1. Requesting for a non-existent block shouldn't fail + res, _, panicErr := doFn(loadPart) + require.Nil(t, panicErr, "a non-existent block part shouldn't cause a panic") + require.Nil(t, res, "a non-existent block part should return nil") + + // 2. Next save a corrupted block then try to load it + db.Set(calcBlockPartKey(height, index), []byte("Tendermint")) + res, _, panicErr = doFn(loadPart) + require.NotNil(t, panicErr, "expecting a non-nil panic") + require.Contains(t, panicErr.Error(), "Error reading block part") + + // 3. A good block serialized and saved to the DB should be retrievable + db.Set(calcBlockPartKey(height, index), binarySerializeIt(part1)) + gotPart, _, panicErr := doFn(loadPart) + require.Nil(t, panicErr, "an existent and proper block should not panic") + require.Nil(t, res, "a properly saved block should return a proper block") + require.Equal(t, gotPart.(*types.Part).Hash(), part1.Hash(), "expecting successful retrieval of previously saved block") +} + +func TestLoadBlockMeta(t *testing.T) { + bs, db := freshBlockStore() + height := 10 + loadMeta := func() (interface{}, error) { + meta := bs.LoadBlockMeta(height) + return meta, nil + } + + // Initially no contents. + // 1. Requesting for a non-existent blockMeta shouldn't fail + res, _, panicErr := doFn(loadMeta) + require.Nil(t, panicErr, "a non-existent blockMeta shouldn't cause a panic") + require.Nil(t, res, "a non-existent blockMeta should return nil") + + // 2. Next save a corrupted blockMeta then try to load it + db.Set(calcBlockMetaKey(height), []byte("Tendermint-Meta")) + res, _, panicErr = doFn(loadMeta) + require.NotNil(t, panicErr, "expecting a non-nil panic") + require.Contains(t, panicErr.Error(), "Error reading block meta") + + // 3. A good blockMeta serialized and saved to the DB should be retrievable + meta := &types.BlockMeta{} + db.Set(calcBlockMetaKey(height), binarySerializeIt(meta)) + gotMeta, _, panicErr := doFn(loadMeta) + require.Nil(t, panicErr, "an existent and proper block should not panic") + require.Nil(t, res, "a properly saved blockMeta should return a proper blocMeta ") + require.Equal(t, binarySerializeIt(meta), binarySerializeIt(gotMeta), "expecting successful retrieval of previously saved blockMeta") +} + +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 + + require.Equal(t, bs.Height(), 0, "initially the height should be zero") + require.NotEqual(t, bs.Height(), block.Header.Height, "expecting different heights initially") + + bs.SaveBlock(block, parts, seenCommit) + require.Equal(t, bs.Height(), block.Header.Height, "expecting the new height to be changed") + + blockAtHeight := bs.LoadBlock(bs.Height()) + require.Equal(t, block.Hash(), blockAtHeight.Hash(), "expecting a successful load of the last saved block") + + blockAtHeightPlus1 := bs.LoadBlock(bs.Height() + 1) + require.Nil(t, blockAtHeightPlus1, "expecting an unsuccessful load of Height()+1") + blockAtHeightPlus2 := bs.LoadBlock(bs.Height() + 2) + require.Nil(t, blockAtHeightPlus2, "expecting an unsuccessful load of Height()+2") +} + +func doFn(fn func() (interface{}, error)) (res interface{}, err error, panicErr error) { + defer func() { + if r := recover(); r != nil { + switch e := r.(type) { + case error: + panicErr = e + case string: + panicErr = fmt.Errorf("%s", e) + default: + if st, ok := r.(fmt.Stringer); ok { + panicErr = fmt.Errorf("%s", st) + } else { + panicErr = fmt.Errorf("%s", debug.Stack()) + } + } + } + }() + + res, err = fn() + return res, err, panicErr +} From 116a61beb14058d5e104494f1a8ccb495ec3dd91 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Wed, 4 Oct 2017 15:04:23 -0400 Subject: [PATCH 2/8] blockchain: update store comments --- blockchain/store.go | 72 +++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 45 deletions(-) diff --git a/blockchain/store.go b/blockchain/store.go index cff26c42b..ec569938a 100644 --- a/blockchain/store.go +++ b/blockchain/store.go @@ -14,7 +14,7 @@ import ( ) /* -Simple low level store for blocks. +BlockStore is a simple low level store for blocks. There are three types of information stored: - BlockMeta: Meta information about each block @@ -23,7 +23,7 @@ There are three types of information stored: Currently the precommit signatures are duplicated in the Block parts as well as the Commit. In the future this may change, perhaps by moving -the Commit data outside the Block. +the Commit data outside the Block. (TODO) // NOTE: BlockStore methods will panic if they encounter errors // deserializing loaded data, indicating probable corruption on disk. @@ -35,9 +35,8 @@ type BlockStore struct { height int64 } -// NewBlockStore loads a blockStore's JSON serialized form from the -// database, db to retrieve the starting height of the blockstore -// and backs db as the internal database of the blockstore. +// NewBlockStore returns a new BlockStore with the given db, +// initialized to the last committed height. func NewBlockStore(db dbm.DB) *BlockStore { bsjson := LoadBlockStoreStateJSON(db) return &BlockStore{ @@ -46,17 +45,16 @@ func NewBlockStore(db dbm.DB) *BlockStore { } } -// Height() returns the last known contiguous block height. +// Height returns the last known contiguous block height. func (bs *BlockStore) Height() int64 { bs.mtx.RLock() defer bs.mtx.RUnlock() return bs.height } -// GetReader conveniently wraps the result of the database -// lookup for key key into an io.Reader. If no result is found, -// it returns nil otherwise it creates an io.Reader. -// Its utility is mainly for use with wire.ReadBinary. +// GetReader returns the value associated with the given key wrapped in an io.Reader. +// If no value is found, it returns nil. +// Its mainly for use with wire.ReadBinary. func (bs *BlockStore) GetReader(key []byte) io.Reader { bytez := bs.db.Get(key) if bytez == nil { @@ -65,13 +63,8 @@ func (bs *BlockStore) GetReader(key []byte) io.Reader { return bytes.NewReader(bytez) } -// LoadBlock retrieves the serialized block, keyed by height in the -// store's database. If the data at the requested height is not found, -// it returns nil. However, if the block meta data is found but -// cannot be deserialized by wire.ReadBinary, it panics. -// The serialized data consists of the BlockMeta data and different -// parts that are reassembled by their internal Data. If the final -// reassembled data cannot be deserialized by wire.ReadBinary, it panics. +// 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 n int var err error @@ -95,11 +88,9 @@ func (bs *BlockStore) LoadBlock(height int64) *types.Block { return block } -// LoadBlockPart tries to load a blockPart from the -// backing database, keyed by height and index. -// If it doesn't find the requested blockPart, it -// returns nil. Otherwise, If the found part is -// corrupted/not deserializable by wire.ReadBinary, it panics. +// LoadBlockPart returns the Part at the given index +// 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 n int var err error @@ -114,10 +105,8 @@ func (bs *BlockStore) LoadBlockPart(height int64, index int) *types.Part { return part } -// LoadBlockMeta tries to load a block meta from the backing database, -// keyed by height. The block meta must have been wire.Binary serialized. -// If it doesn't find the requested meta, it returns nil. Otherwise, -// if the found data cannot be deserialized by wire.ReadBinary, it panics. +// 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 n int var err error @@ -132,13 +121,10 @@ func (bs *BlockStore) LoadBlockMeta(height int64) *types.BlockMeta { return blockMeta } -// LoadBlockCommit tries to load a commit from the backing database, -// keyed by height. The commit must have been wire.Binary serialized. -// If it doesn't find the requested commit in the database, it returns nil. -// Otherwise, if the found data cannot be deserialized by wire.ReadBinary, it panics. -// -// The +2/3 and other Precommit-votes for block at `height`. -// This Commit comes from block.LastCommit for `height+1`. +// LoadBlockCommit returns the Commit for the given height. +// This commit consists of the +2/3 and other Precommit-votes for block at `height`, +// 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 n int var err error @@ -153,12 +139,9 @@ func (bs *BlockStore) LoadBlockCommit(height int64) *types.Commit { return commit } -// LoadSeenCommit tries to load the seen commit from the backing database, -// keyed by height. The commit must have been wire.Binary serialized. -// If it doesn't find the requested commit in the database, it returns nil. -// Otherwise, if the found data cannot be deserialized by wire.ReadBinary, it panics. -// -// NOTE: the Precommit-vote heights are for the block at `height` +// LoadSeenCommit returns the locally seen Commit for the given height. +// 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 n int var err error @@ -173,6 +156,7 @@ func (bs *BlockStore) LoadSeenCommit(height int64) *types.Commit { return commit } +// SaveBlock persists the given block, blockParts, and seenCommit to the underlying db. // blockParts: Must be parts of the block // seenCommit: The +2/3 precommits that were seen which committed at height. // If all the nodes restart after committing a block, @@ -180,7 +164,7 @@ func (bs *BlockStore) LoadSeenCommit(height int64) *types.Commit { // most recent height. Otherwise they'd stall at H-1. func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) { if block == nil { - PanicSanity("BlockStore can only save a non-nil block") + cmn.PanicSanity("BlockStore can only save a non-nil block") } height := block.Height if height != bs.Height()+1 { @@ -255,7 +239,7 @@ type BlockStoreStateJSON struct { Height int64 } -// Save JSON marshals the blockStore state to the database, saving it synchronously. +// Save persists the blockStore state to the database as JSON. func (bsj BlockStoreStateJSON) Save(db dbm.DB) { bytes, err := json.Marshal(bsj) if err != nil { @@ -264,10 +248,8 @@ func (bsj BlockStoreStateJSON) Save(db dbm.DB) { db.SetSync(blockStoreKey, bytes) } -// LoadBlockStoreStateJSON JSON unmarshals the -// blockStore state from the database, keyed by -// key "blockStore". If it cannot lookup the state, -// it returns the zero value BlockStoreStateJSON. +// LoadBlockStoreStateJSON returns the BlockStoreStateJSON as loaded from disk. +// If no BlockStoreStateJSON was previously persisted, it returns a zero value one. func LoadBlockStoreStateJSON(db dbm.DB) BlockStoreStateJSON { bytes := db.Get(blockStoreKey) if bytes == nil { From 05f30b3e28078c68b9b1705023f078a32b21304b Mon Sep 17 00:00:00 2001 From: Emmanuel Odeke Date: Thu, 5 Oct 2017 17:03:01 -0600 Subject: [PATCH 3/8] blockchain: updated store docs/comments from review --- blockchain/store.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/blockchain/store.go b/blockchain/store.go index ec569938a..7d739a502 100644 --- a/blockchain/store.go +++ b/blockchain/store.go @@ -35,8 +35,8 @@ type BlockStore struct { height int64 } -// NewBlockStore returns a new BlockStore with the given db, -// initialized to the last committed height. +// NewBlockStore returns a new BlockStore with the given DB, +// initialized to the last height that was committed to the DB. func NewBlockStore(db dbm.DB) *BlockStore { bsjson := LoadBlockStoreStateJSON(db) return &BlockStore{ @@ -54,7 +54,7 @@ func (bs *BlockStore) Height() int64 { // GetReader returns the value associated with the given key wrapped in an io.Reader. // If no value is found, it returns nil. -// Its mainly for use with wire.ReadBinary. +// It's mainly for use with wire.ReadBinary. func (bs *BlockStore) GetReader(key []byte) io.Reader { bytez := bs.db.Get(key) if bytez == nil { @@ -249,7 +249,7 @@ func (bsj BlockStoreStateJSON) Save(db dbm.DB) { } // LoadBlockStoreStateJSON returns the BlockStoreStateJSON as loaded from disk. -// If no BlockStoreStateJSON was previously persisted, it returns a zero value one. +// If no BlockStoreStateJSON was previously persisted, it returns the zero value. func LoadBlockStoreStateJSON(db dbm.DB) BlockStoreStateJSON { bytes := db.Get(blockStoreKey) if bytes == nil { From 83b40b25d65b858adefab812a93464b819304911 Mon Sep 17 00:00:00 2001 From: Emmanuel Odeke Date: Thu, 5 Oct 2017 21:20:01 -0600 Subject: [PATCH 4/8] blockchain: deduplicate store header value tests --- blockchain/store_test.go | 56 ++++++++++++---------------------------- 1 file changed, 16 insertions(+), 40 deletions(-) diff --git a/blockchain/store_test.go b/blockchain/store_test.go index 65832e22f..e026025e7 100644 --- a/blockchain/store_test.go +++ b/blockchain/store_test.go @@ -128,6 +128,14 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { uncontiguousPartSet := types.NewPartSetFromHeader(types.PartSetHeader{Total: 0}) uncontiguousPartSet.AddPart(part2, false) + header1 := types.Header{ + Height: 1, + NumTxs: 100, + ChainID: "block_test", + } + header2 := header1 + header2.Height = 4 + // End of setup, test data tuples := []struct { @@ -145,11 +153,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { }{ { block: &types.Block{ - Header: &types.Header{ - Height: 1, - NumTxs: 100, - ChainID: "block_test", - }, + Header: &header1, LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, }, parts: validPartSet, @@ -163,11 +167,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { { block: &types.Block{ - Header: &types.Header{ - Height: 4, - NumTxs: 100, - ChainID: "block_test", - }, + Header: &header2, LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, }, parts: uncontiguousPartSet, @@ -176,11 +176,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { { block: &types.Block{ - Header: &types.Header{ - Height: 1, - NumTxs: 100, - ChainID: "block_test", - }, + Header: &header1, LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, }, parts: incompletePartSet, @@ -189,11 +185,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { { block: &types.Block{ - Header: &types.Header{ - Height: 1, - NumTxs: 100, - ChainID: "block_test", - }, + Header: &header1, LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, }, parts: validPartSet, @@ -204,11 +196,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { { block: &types.Block{ - Header: &types.Header{ - Height: 1, - NumTxs: 100, - ChainID: "block_test", - }, + Header: &header1, LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, }, parts: validPartSet, @@ -219,11 +207,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { { block: &types.Block{ - Header: &types.Header{ - Height: 1, - NumTxs: 100, - ChainID: "block_test", - }, + Header: &header1, LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, }, parts: validPartSet, @@ -235,11 +219,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { { block: &types.Block{ - Header: &types.Header{ - Height: 1, - NumTxs: 100, - ChainID: "block_test", - }, + Header: &header1, LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, }, parts: validPartSet, @@ -251,11 +231,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { { block: &types.Block{ - Header: &types.Header{ - Height: 1, - NumTxs: 100, - ChainID: "block_test", - }, + Header: &header1, LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, }, parts: validPartSet, From 2da529992472bcdb0d214d1942c118da627744c6 Mon Sep 17 00:00:00 2001 From: Emmanuel Odeke Date: Sat, 14 Oct 2017 19:21:13 -0600 Subject: [PATCH 5/8] blockchain: less fragile and involved tests for blockstore With feedback from @ebuchman, to make the tests nicer and less fragile. --- blockchain/outf.outf | 225 +++++++++++++++++++++++++++++++++++++ blockchain/reactor_test.go | 13 ++- blockchain/store.go | 4 +- blockchain/store_test.go | 55 ++++----- 4 files changed, 258 insertions(+), 39 deletions(-) create mode 100644 blockchain/outf.outf diff --git a/blockchain/outf.outf b/blockchain/outf.outf new file mode 100644 index 000000000..954a10d6f --- /dev/null +++ b/blockchain/outf.outf @@ -0,0 +1,225 @@ +mode: set +github.com/tendermint/tendermint/blockchain/pool.go:51.99,64.2 3 1 +github.com/tendermint/tendermint/blockchain/pool.go:66.40,70.2 3 1 +github.com/tendermint/tendermint/blockchain/pool.go:72.33,74.2 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:77.48,78.6 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:78.6,79.24 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:82.3,83.39 2 1 +github.com/tendermint/tendermint/blockchain/pool.go:79.24,80.9 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:83.39,88.4 2 1 +github.com/tendermint/tendermint/blockchain/pool.go:88.4,88.49 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:88.49,93.4 2 1 +github.com/tendermint/tendermint/blockchain/pool.go:93.4,96.4 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:100.46,104.34 3 1 +github.com/tendermint/tendermint/blockchain/pool.go:104.34,105.46 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:114.3,114.22 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:105.46,108.45 2 1 +github.com/tendermint/tendermint/blockchain/pool.go:108.45,112.5 3 0 +github.com/tendermint/tendermint/blockchain/pool.go:114.22,116.4 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:120.86,125.2 3 1 +github.com/tendermint/tendermint/blockchain/pool.go:128.42,133.26 3 0 +github.com/tendermint/tendermint/blockchain/pool.go:138.2,139.34 2 0 +github.com/tendermint/tendermint/blockchain/pool.go:144.2,148.19 5 0 +github.com/tendermint/tendermint/blockchain/pool.go:133.26,136.3 2 0 +github.com/tendermint/tendermint/blockchain/pool.go:139.34,141.3 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:154.82,158.49 3 1 +github.com/tendermint/tendermint/blockchain/pool.go:161.2,161.51 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:164.2,164.8 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:158.49,160.3 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:161.51,163.3 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:169.37,173.49 3 0 +github.com/tendermint/tendermint/blockchain/pool.go:173.49,182.3 3 0 +github.com/tendermint/tendermint/blockchain/pool.go:182.3,184.3 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:189.48,194.26 4 0 +github.com/tendermint/tendermint/blockchain/pool.go:199.2,199.33 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:194.26,196.3 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:203.83,208.22 4 1 +github.com/tendermint/tendermint/blockchain/pool.go:212.2,212.39 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:208.22,210.3 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:212.39,216.3 3 1 +github.com/tendermint/tendermint/blockchain/pool.go:216.3,218.3 0 0 +github.com/tendermint/tendermint/blockchain/pool.go:222.65,227.17 4 1 +github.com/tendermint/tendermint/blockchain/pool.go:227.17,229.3 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:229.3,233.3 3 1 +github.com/tendermint/tendermint/blockchain/pool.go:236.50,241.2 3 0 +github.com/tendermint/tendermint/blockchain/pool.go:243.50,244.44 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:252.2,252.28 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:244.44,245.38 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:245.38,246.35 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:249.4,249.23 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:246.35,248.5 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:257.69,261.34 3 1 +github.com/tendermint/tendermint/blockchain/pool.go:275.2,275.12 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:261.34,262.22 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:266.3,266.51 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:269.3,269.30 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:272.3,273.14 2 1 +github.com/tendermint/tendermint/blockchain/pool.go:262.22,264.12 2 0 +github.com/tendermint/tendermint/blockchain/pool.go:266.51,267.12 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:269.30,270.12 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:278.44,290.2 8 1 +github.com/tendermint/tendermint/blockchain/pool.go:292.63,293.23 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:296.2,296.49 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:293.23,295.3 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:299.51,300.23 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:303.2,303.27 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:300.23,302.3 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:307.39,312.66 4 0 +github.com/tendermint/tendermint/blockchain/pool.go:320.2,320.12 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:312.66,313.32 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:313.32,315.4 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:315.4,318.4 2 0 +github.com/tendermint/tendermint/blockchain/pool.go:338.68,347.2 2 1 +github.com/tendermint/tendermint/blockchain/pool.go:349.45,351.2 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:353.36,357.2 3 1 +github.com/tendermint/tendermint/blockchain/pool.go:359.36,360.25 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:360.25,362.3 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:362.3,364.3 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:367.35,368.26 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:372.2,372.19 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:368.26,371.3 2 1 +github.com/tendermint/tendermint/blockchain/pool.go:375.47,377.26 2 1 +github.com/tendermint/tendermint/blockchain/pool.go:377.26,379.3 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:379.3,382.3 2 1 +github.com/tendermint/tendermint/blockchain/pool.go:385.33,392.2 5 1 +github.com/tendermint/tendermint/blockchain/pool.go:408.63,420.2 3 1 +github.com/tendermint/tendermint/blockchain/pool.go:422.41,425.2 2 1 +github.com/tendermint/tendermint/blockchain/pool.go:428.74,430.46 2 1 +github.com/tendermint/tendermint/blockchain/pool.go:434.2,438.13 4 1 +github.com/tendermint/tendermint/blockchain/pool.go:430.46,433.3 2 0 +github.com/tendermint/tendermint/blockchain/pool.go:441.49,445.2 3 1 +github.com/tendermint/tendermint/blockchain/pool.go:447.44,451.2 3 0 +github.com/tendermint/tendermint/blockchain/pool.go:453.33,458.2 4 0 +github.com/tendermint/tendermint/blockchain/pool.go:462.32,464.2 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:468.42,470.6 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:470.6,474.7 2 1 +github.com/tendermint/tendermint/blockchain/pool.go:486.3,492.10 5 1 +github.com/tendermint/tendermint/blockchain/pool.go:474.7,475.49 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:478.4,479.19 2 1 +github.com/tendermint/tendermint/blockchain/pool.go:484.4,484.24 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:475.49,477.5 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:479.19,482.28 2 1 +github.com/tendermint/tendermint/blockchain/pool.go:493.24,495.10 2 1 +github.com/tendermint/tendermint/blockchain/pool.go:496.19,497.10 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:498.21,500.23 2 0 +github.com/tendermint/tendermint/blockchain/pool.go:501.25,503.11 1 1 +github.com/tendermint/tendermint/blockchain/pool.go:504.25,506.11 2 1 +github.com/tendermint/tendermint/blockchain/pool.go:507.20,508.11 1 0 +github.com/tendermint/tendermint/blockchain/pool.go:509.22,511.24 2 0 +github.com/tendermint/tendermint/blockchain/reactor.go:55.134,56.47 1 1 +github.com/tendermint/tendermint/blockchain/reactor.go:59.2,59.45 1 1 +github.com/tendermint/tendermint/blockchain/reactor.go:62.2,79.12 6 1 +github.com/tendermint/tendermint/blockchain/reactor.go:56.47,58.3 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:59.45,61.3 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:83.47,85.18 2 1 +github.com/tendermint/tendermint/blockchain/reactor.go:92.2,92.12 1 1 +github.com/tendermint/tendermint/blockchain/reactor.go:85.18,87.17 2 1 +github.com/tendermint/tendermint/blockchain/reactor.go:90.3,90.23 1 1 +github.com/tendermint/tendermint/blockchain/reactor.go:87.17,89.4 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:96.40,99.2 2 1 +github.com/tendermint/tendermint/blockchain/reactor.go:102.70,110.2 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:113.54,114.110 1 1 +github.com/tendermint/tendermint/blockchain/reactor.go:114.110,116.3 0 0 +github.com/tendermint/tendermint/blockchain/reactor.go:120.77,122.2 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:128.101,130.18 2 1 +github.com/tendermint/tendermint/blockchain/reactor.go:135.2,139.4 2 1 +github.com/tendermint/tendermint/blockchain/reactor.go:130.18,133.3 2 1 +github.com/tendermint/tendermint/blockchain/reactor.go:143.81,145.16 2 1 +github.com/tendermint/tendermint/blockchain/reactor.go:150.2,153.27 2 1 +github.com/tendermint/tendermint/blockchain/reactor.go:145.16,148.3 2 0 +github.com/tendermint/tendermint/blockchain/reactor.go:154.30,155.53 1 1 +github.com/tendermint/tendermint/blockchain/reactor.go:158.31,160.57 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:161.31,164.14 2 0 +github.com/tendermint/tendermint/blockchain/reactor.go:167.32,169.48 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:170.10,171.76 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:155.53,157.4 0 0 +github.com/tendermint/tendermint/blockchain/reactor.go:164.14,166.4 0 0 +github.com/tendermint/tendermint/blockchain/reactor.go:177.48,179.2 1 1 +github.com/tendermint/tendermint/blockchain/reactor.go:184.45,191.6 4 1 +github.com/tendermint/tendermint/blockchain/reactor.go:191.6,192.10 1 1 +github.com/tendermint/tendermint/blockchain/reactor.go:193.36,195.19 2 0 +github.com/tendermint/tendermint/blockchain/reactor.go:198.4,200.15 3 0 +github.com/tendermint/tendermint/blockchain/reactor.go:205.35,208.19 2 0 +github.com/tendermint/tendermint/blockchain/reactor.go:211.31,213.35 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:214.36,219.29 4 0 +github.com/tendermint/tendermint/blockchain/reactor.go:228.26,231.28 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:267.4,267.21 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:268.19,269.18 1 1 +github.com/tendermint/tendermint/blockchain/reactor.go:195.19,196.22 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:200.15,203.22 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:208.19,210.5 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:219.29,226.19 5 0 +github.com/tendermint/tendermint/blockchain/reactor.go:231.28,235.38 2 0 +github.com/tendermint/tendermint/blockchain/reactor.go:239.5,247.19 4 0 +github.com/tendermint/tendermint/blockchain/reactor.go:235.38,237.21 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:247.19,250.21 3 0 +github.com/tendermint/tendermint/blockchain/reactor.go:251.6,261.20 4 0 +github.com/tendermint/tendermint/blockchain/reactor.go:261.20,264.7 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:275.62,278.2 2 0 +github.com/tendermint/tendermint/blockchain/reactor.go:281.70,283.2 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:310.93,315.32 5 1 +github.com/tendermint/tendermint/blockchain/reactor.go:318.2,318.8 1 1 +github.com/tendermint/tendermint/blockchain/reactor.go:315.32,317.3 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:327.49,329.2 1 1 +github.com/tendermint/tendermint/blockchain/reactor.go:335.54,337.2 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:346.50,348.2 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:356.50,358.2 1 0 +github.com/tendermint/tendermint/blockchain/reactor.go:366.51,368.2 1 0 +github.com/tendermint/tendermint/blockchain/store.go:40.43,46.2 2 1 +github.com/tendermint/tendermint/blockchain/store.go:49.36,53.2 3 1 +github.com/tendermint/tendermint/blockchain/store.go:58.55,60.18 2 1 +github.com/tendermint/tendermint/blockchain/store.go:63.2,63.31 1 1 +github.com/tendermint/tendermint/blockchain/store.go:60.18,62.3 1 1 +github.com/tendermint/tendermint/blockchain/store.go:68.58,72.14 4 1 +github.com/tendermint/tendermint/blockchain/store.go:75.2,76.16 2 1 +github.com/tendermint/tendermint/blockchain/store.go:79.2,80.59 2 1 +github.com/tendermint/tendermint/blockchain/store.go:84.2,85.16 2 1 +github.com/tendermint/tendermint/blockchain/store.go:88.2,88.14 1 1 +github.com/tendermint/tendermint/blockchain/store.go:72.14,74.3 1 1 +github.com/tendermint/tendermint/blockchain/store.go:76.16,78.3 1 1 +github.com/tendermint/tendermint/blockchain/store.go:80.59,83.3 2 1 +github.com/tendermint/tendermint/blockchain/store.go:85.16,87.3 1 0 +github.com/tendermint/tendermint/blockchain/store.go:94.72,98.14 4 1 +github.com/tendermint/tendermint/blockchain/store.go:101.2,102.16 2 1 +github.com/tendermint/tendermint/blockchain/store.go:105.2,105.13 1 1 +github.com/tendermint/tendermint/blockchain/store.go:98.14,100.3 1 1 +github.com/tendermint/tendermint/blockchain/store.go:102.16,104.3 1 1 +github.com/tendermint/tendermint/blockchain/store.go:110.66,114.14 4 1 +github.com/tendermint/tendermint/blockchain/store.go:117.2,118.16 2 1 +github.com/tendermint/tendermint/blockchain/store.go:121.2,121.18 1 1 +github.com/tendermint/tendermint/blockchain/store.go:114.14,116.3 1 1 +github.com/tendermint/tendermint/blockchain/store.go:118.16,120.3 1 1 +github.com/tendermint/tendermint/blockchain/store.go:128.65,132.14 4 1 +github.com/tendermint/tendermint/blockchain/store.go:135.2,136.16 2 1 +github.com/tendermint/tendermint/blockchain/store.go:139.2,139.15 1 1 +github.com/tendermint/tendermint/blockchain/store.go:132.14,134.3 1 1 +github.com/tendermint/tendermint/blockchain/store.go:136.16,138.3 1 1 +github.com/tendermint/tendermint/blockchain/store.go:145.64,149.14 4 1 +github.com/tendermint/tendermint/blockchain/store.go:152.2,153.16 2 1 +github.com/tendermint/tendermint/blockchain/store.go:156.2,156.15 1 1 +github.com/tendermint/tendermint/blockchain/store.go:149.14,151.3 1 1 +github.com/tendermint/tendermint/blockchain/store.go:153.16,155.3 1 1 +github.com/tendermint/tendermint/blockchain/store.go:165.106,166.18 1 1 +github.com/tendermint/tendermint/blockchain/store.go:169.2,170.43 2 1 +github.com/tendermint/tendermint/blockchain/store.go:173.2,173.30 1 1 +github.com/tendermint/tendermint/blockchain/store.go:178.2,183.42 4 1 +github.com/tendermint/tendermint/blockchain/store.go:188.2,205.25 9 1 +github.com/tendermint/tendermint/blockchain/store.go:166.18,168.3 1 1 +github.com/tendermint/tendermint/blockchain/store.go:170.43,172.3 1 1 +github.com/tendermint/tendermint/blockchain/store.go:173.30,175.3 1 1 +github.com/tendermint/tendermint/blockchain/store.go:183.42,185.3 1 1 +github.com/tendermint/tendermint/blockchain/store.go:208.78,209.29 1 1 +github.com/tendermint/tendermint/blockchain/store.go:212.2,213.55 2 1 +github.com/tendermint/tendermint/blockchain/store.go:209.29,211.3 1 0 +github.com/tendermint/tendermint/blockchain/store.go:218.42,220.2 1 1 +github.com/tendermint/tendermint/blockchain/store.go:222.57,224.2 1 1 +github.com/tendermint/tendermint/blockchain/store.go:226.44,228.2 1 1 +github.com/tendermint/tendermint/blockchain/store.go:230.43,232.2 1 1 +github.com/tendermint/tendermint/blockchain/store.go:243.48,245.16 2 1 +github.com/tendermint/tendermint/blockchain/store.go:248.2,248.34 1 1 +github.com/tendermint/tendermint/blockchain/store.go:245.16,247.3 1 0 +github.com/tendermint/tendermint/blockchain/store.go:253.61,255.18 2 1 +github.com/tendermint/tendermint/blockchain/store.go:260.2,262.16 3 1 +github.com/tendermint/tendermint/blockchain/store.go:265.2,265.12 1 1 +github.com/tendermint/tendermint/blockchain/store.go:255.18,259.3 1 1 +github.com/tendermint/tendermint/blockchain/store.go:262.16,264.3 1 1 diff --git a/blockchain/reactor_test.go b/blockchain/reactor_test.go index 7342b72c0..c473cddf1 100644 --- a/blockchain/reactor_test.go +++ b/blockchain/reactor_test.go @@ -14,10 +14,8 @@ import ( "github.com/tendermint/tendermint/types" ) -func newBlockchainReactor(maxBlockHeight int64) *BlockchainReactor { - logger := log.TestingLogger() +func makeStateAndBlockStore(logger log.Logger) (*sm.State, *BlockStore) { config := cfg.ResetTestRoot("blockchain_reactor_test") - blockStore := NewBlockStore(dbm.NewMemDB()) // Get State @@ -25,6 +23,13 @@ func newBlockchainReactor(maxBlockHeight int64) *BlockchainReactor { state.SetLogger(logger.With("module", "state")) state.Save() + return state, blockStore +} + +func newBlockchainReactor(logger log.Logger, maxBlockHeight int) *BlockchainReactor { + logger := log.TestingLogger() + state, blockStore := makeStateAndBlockStore(logger) + // Make the blockchainReactor itself fastSync := true bcReactor := NewBlockchainReactor(state.Copy(), nil, blockStore, fastSync) @@ -47,7 +52,7 @@ func newBlockchainReactor(maxBlockHeight int64) *BlockchainReactor { func TestNoBlockMessageResponse(t *testing.T) { maxBlockHeight := int64(20) - bcr := newBlockchainReactor(maxBlockHeight) + bcr := newBlockchainReactor(log.NewNopLogger(), maxBlockHeight) bcr.Start() defer bcr.Stop() diff --git a/blockchain/store.go b/blockchain/store.go index 7d739a502..1033999fe 100644 --- a/blockchain/store.go +++ b/blockchain/store.go @@ -167,8 +167,8 @@ func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, s cmn.PanicSanity("BlockStore can only save a non-nil block") } height := block.Height - if height != bs.Height()+1 { - cmn.PanicSanity(cmn.Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.Height()+1, height)) + if g, w := height, bs.Height()+1; g != w { + cmn.PanicSanity(cmn.Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", w, g)) } if !blockParts.IsComplete() { cmn.PanicSanity(cmn.Fmt("BlockStore can only save complete block part sets")) diff --git a/blockchain/store_test.go b/blockchain/store_test.go index e026025e7..9b6728391 100644 --- a/blockchain/store_test.go +++ b/blockchain/store_test.go @@ -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()) From 96998a5498cb238f62d5e9e3d4c7e8e93e8827f2 Mon Sep 17 00:00:00 2001 From: Emmanuel Odeke Date: Tue, 24 Oct 2017 00:02:45 -0700 Subject: [PATCH 6/8] blockchain: Block creator helper for compressing tests as per @ebuchman --- blockchain/outf.outf | 225 --------------------------------------- blockchain/store_test.go | 48 +++------ 2 files changed, 16 insertions(+), 257 deletions(-) delete mode 100644 blockchain/outf.outf diff --git a/blockchain/outf.outf b/blockchain/outf.outf deleted file mode 100644 index 954a10d6f..000000000 --- a/blockchain/outf.outf +++ /dev/null @@ -1,225 +0,0 @@ -mode: set -github.com/tendermint/tendermint/blockchain/pool.go:51.99,64.2 3 1 -github.com/tendermint/tendermint/blockchain/pool.go:66.40,70.2 3 1 -github.com/tendermint/tendermint/blockchain/pool.go:72.33,74.2 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:77.48,78.6 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:78.6,79.24 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:82.3,83.39 2 1 -github.com/tendermint/tendermint/blockchain/pool.go:79.24,80.9 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:83.39,88.4 2 1 -github.com/tendermint/tendermint/blockchain/pool.go:88.4,88.49 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:88.49,93.4 2 1 -github.com/tendermint/tendermint/blockchain/pool.go:93.4,96.4 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:100.46,104.34 3 1 -github.com/tendermint/tendermint/blockchain/pool.go:104.34,105.46 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:114.3,114.22 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:105.46,108.45 2 1 -github.com/tendermint/tendermint/blockchain/pool.go:108.45,112.5 3 0 -github.com/tendermint/tendermint/blockchain/pool.go:114.22,116.4 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:120.86,125.2 3 1 -github.com/tendermint/tendermint/blockchain/pool.go:128.42,133.26 3 0 -github.com/tendermint/tendermint/blockchain/pool.go:138.2,139.34 2 0 -github.com/tendermint/tendermint/blockchain/pool.go:144.2,148.19 5 0 -github.com/tendermint/tendermint/blockchain/pool.go:133.26,136.3 2 0 -github.com/tendermint/tendermint/blockchain/pool.go:139.34,141.3 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:154.82,158.49 3 1 -github.com/tendermint/tendermint/blockchain/pool.go:161.2,161.51 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:164.2,164.8 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:158.49,160.3 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:161.51,163.3 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:169.37,173.49 3 0 -github.com/tendermint/tendermint/blockchain/pool.go:173.49,182.3 3 0 -github.com/tendermint/tendermint/blockchain/pool.go:182.3,184.3 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:189.48,194.26 4 0 -github.com/tendermint/tendermint/blockchain/pool.go:199.2,199.33 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:194.26,196.3 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:203.83,208.22 4 1 -github.com/tendermint/tendermint/blockchain/pool.go:212.2,212.39 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:208.22,210.3 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:212.39,216.3 3 1 -github.com/tendermint/tendermint/blockchain/pool.go:216.3,218.3 0 0 -github.com/tendermint/tendermint/blockchain/pool.go:222.65,227.17 4 1 -github.com/tendermint/tendermint/blockchain/pool.go:227.17,229.3 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:229.3,233.3 3 1 -github.com/tendermint/tendermint/blockchain/pool.go:236.50,241.2 3 0 -github.com/tendermint/tendermint/blockchain/pool.go:243.50,244.44 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:252.2,252.28 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:244.44,245.38 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:245.38,246.35 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:249.4,249.23 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:246.35,248.5 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:257.69,261.34 3 1 -github.com/tendermint/tendermint/blockchain/pool.go:275.2,275.12 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:261.34,262.22 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:266.3,266.51 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:269.3,269.30 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:272.3,273.14 2 1 -github.com/tendermint/tendermint/blockchain/pool.go:262.22,264.12 2 0 -github.com/tendermint/tendermint/blockchain/pool.go:266.51,267.12 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:269.30,270.12 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:278.44,290.2 8 1 -github.com/tendermint/tendermint/blockchain/pool.go:292.63,293.23 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:296.2,296.49 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:293.23,295.3 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:299.51,300.23 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:303.2,303.27 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:300.23,302.3 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:307.39,312.66 4 0 -github.com/tendermint/tendermint/blockchain/pool.go:320.2,320.12 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:312.66,313.32 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:313.32,315.4 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:315.4,318.4 2 0 -github.com/tendermint/tendermint/blockchain/pool.go:338.68,347.2 2 1 -github.com/tendermint/tendermint/blockchain/pool.go:349.45,351.2 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:353.36,357.2 3 1 -github.com/tendermint/tendermint/blockchain/pool.go:359.36,360.25 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:360.25,362.3 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:362.3,364.3 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:367.35,368.26 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:372.2,372.19 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:368.26,371.3 2 1 -github.com/tendermint/tendermint/blockchain/pool.go:375.47,377.26 2 1 -github.com/tendermint/tendermint/blockchain/pool.go:377.26,379.3 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:379.3,382.3 2 1 -github.com/tendermint/tendermint/blockchain/pool.go:385.33,392.2 5 1 -github.com/tendermint/tendermint/blockchain/pool.go:408.63,420.2 3 1 -github.com/tendermint/tendermint/blockchain/pool.go:422.41,425.2 2 1 -github.com/tendermint/tendermint/blockchain/pool.go:428.74,430.46 2 1 -github.com/tendermint/tendermint/blockchain/pool.go:434.2,438.13 4 1 -github.com/tendermint/tendermint/blockchain/pool.go:430.46,433.3 2 0 -github.com/tendermint/tendermint/blockchain/pool.go:441.49,445.2 3 1 -github.com/tendermint/tendermint/blockchain/pool.go:447.44,451.2 3 0 -github.com/tendermint/tendermint/blockchain/pool.go:453.33,458.2 4 0 -github.com/tendermint/tendermint/blockchain/pool.go:462.32,464.2 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:468.42,470.6 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:470.6,474.7 2 1 -github.com/tendermint/tendermint/blockchain/pool.go:486.3,492.10 5 1 -github.com/tendermint/tendermint/blockchain/pool.go:474.7,475.49 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:478.4,479.19 2 1 -github.com/tendermint/tendermint/blockchain/pool.go:484.4,484.24 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:475.49,477.5 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:479.19,482.28 2 1 -github.com/tendermint/tendermint/blockchain/pool.go:493.24,495.10 2 1 -github.com/tendermint/tendermint/blockchain/pool.go:496.19,497.10 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:498.21,500.23 2 0 -github.com/tendermint/tendermint/blockchain/pool.go:501.25,503.11 1 1 -github.com/tendermint/tendermint/blockchain/pool.go:504.25,506.11 2 1 -github.com/tendermint/tendermint/blockchain/pool.go:507.20,508.11 1 0 -github.com/tendermint/tendermint/blockchain/pool.go:509.22,511.24 2 0 -github.com/tendermint/tendermint/blockchain/reactor.go:55.134,56.47 1 1 -github.com/tendermint/tendermint/blockchain/reactor.go:59.2,59.45 1 1 -github.com/tendermint/tendermint/blockchain/reactor.go:62.2,79.12 6 1 -github.com/tendermint/tendermint/blockchain/reactor.go:56.47,58.3 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:59.45,61.3 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:83.47,85.18 2 1 -github.com/tendermint/tendermint/blockchain/reactor.go:92.2,92.12 1 1 -github.com/tendermint/tendermint/blockchain/reactor.go:85.18,87.17 2 1 -github.com/tendermint/tendermint/blockchain/reactor.go:90.3,90.23 1 1 -github.com/tendermint/tendermint/blockchain/reactor.go:87.17,89.4 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:96.40,99.2 2 1 -github.com/tendermint/tendermint/blockchain/reactor.go:102.70,110.2 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:113.54,114.110 1 1 -github.com/tendermint/tendermint/blockchain/reactor.go:114.110,116.3 0 0 -github.com/tendermint/tendermint/blockchain/reactor.go:120.77,122.2 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:128.101,130.18 2 1 -github.com/tendermint/tendermint/blockchain/reactor.go:135.2,139.4 2 1 -github.com/tendermint/tendermint/blockchain/reactor.go:130.18,133.3 2 1 -github.com/tendermint/tendermint/blockchain/reactor.go:143.81,145.16 2 1 -github.com/tendermint/tendermint/blockchain/reactor.go:150.2,153.27 2 1 -github.com/tendermint/tendermint/blockchain/reactor.go:145.16,148.3 2 0 -github.com/tendermint/tendermint/blockchain/reactor.go:154.30,155.53 1 1 -github.com/tendermint/tendermint/blockchain/reactor.go:158.31,160.57 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:161.31,164.14 2 0 -github.com/tendermint/tendermint/blockchain/reactor.go:167.32,169.48 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:170.10,171.76 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:155.53,157.4 0 0 -github.com/tendermint/tendermint/blockchain/reactor.go:164.14,166.4 0 0 -github.com/tendermint/tendermint/blockchain/reactor.go:177.48,179.2 1 1 -github.com/tendermint/tendermint/blockchain/reactor.go:184.45,191.6 4 1 -github.com/tendermint/tendermint/blockchain/reactor.go:191.6,192.10 1 1 -github.com/tendermint/tendermint/blockchain/reactor.go:193.36,195.19 2 0 -github.com/tendermint/tendermint/blockchain/reactor.go:198.4,200.15 3 0 -github.com/tendermint/tendermint/blockchain/reactor.go:205.35,208.19 2 0 -github.com/tendermint/tendermint/blockchain/reactor.go:211.31,213.35 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:214.36,219.29 4 0 -github.com/tendermint/tendermint/blockchain/reactor.go:228.26,231.28 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:267.4,267.21 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:268.19,269.18 1 1 -github.com/tendermint/tendermint/blockchain/reactor.go:195.19,196.22 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:200.15,203.22 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:208.19,210.5 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:219.29,226.19 5 0 -github.com/tendermint/tendermint/blockchain/reactor.go:231.28,235.38 2 0 -github.com/tendermint/tendermint/blockchain/reactor.go:239.5,247.19 4 0 -github.com/tendermint/tendermint/blockchain/reactor.go:235.38,237.21 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:247.19,250.21 3 0 -github.com/tendermint/tendermint/blockchain/reactor.go:251.6,261.20 4 0 -github.com/tendermint/tendermint/blockchain/reactor.go:261.20,264.7 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:275.62,278.2 2 0 -github.com/tendermint/tendermint/blockchain/reactor.go:281.70,283.2 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:310.93,315.32 5 1 -github.com/tendermint/tendermint/blockchain/reactor.go:318.2,318.8 1 1 -github.com/tendermint/tendermint/blockchain/reactor.go:315.32,317.3 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:327.49,329.2 1 1 -github.com/tendermint/tendermint/blockchain/reactor.go:335.54,337.2 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:346.50,348.2 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:356.50,358.2 1 0 -github.com/tendermint/tendermint/blockchain/reactor.go:366.51,368.2 1 0 -github.com/tendermint/tendermint/blockchain/store.go:40.43,46.2 2 1 -github.com/tendermint/tendermint/blockchain/store.go:49.36,53.2 3 1 -github.com/tendermint/tendermint/blockchain/store.go:58.55,60.18 2 1 -github.com/tendermint/tendermint/blockchain/store.go:63.2,63.31 1 1 -github.com/tendermint/tendermint/blockchain/store.go:60.18,62.3 1 1 -github.com/tendermint/tendermint/blockchain/store.go:68.58,72.14 4 1 -github.com/tendermint/tendermint/blockchain/store.go:75.2,76.16 2 1 -github.com/tendermint/tendermint/blockchain/store.go:79.2,80.59 2 1 -github.com/tendermint/tendermint/blockchain/store.go:84.2,85.16 2 1 -github.com/tendermint/tendermint/blockchain/store.go:88.2,88.14 1 1 -github.com/tendermint/tendermint/blockchain/store.go:72.14,74.3 1 1 -github.com/tendermint/tendermint/blockchain/store.go:76.16,78.3 1 1 -github.com/tendermint/tendermint/blockchain/store.go:80.59,83.3 2 1 -github.com/tendermint/tendermint/blockchain/store.go:85.16,87.3 1 0 -github.com/tendermint/tendermint/blockchain/store.go:94.72,98.14 4 1 -github.com/tendermint/tendermint/blockchain/store.go:101.2,102.16 2 1 -github.com/tendermint/tendermint/blockchain/store.go:105.2,105.13 1 1 -github.com/tendermint/tendermint/blockchain/store.go:98.14,100.3 1 1 -github.com/tendermint/tendermint/blockchain/store.go:102.16,104.3 1 1 -github.com/tendermint/tendermint/blockchain/store.go:110.66,114.14 4 1 -github.com/tendermint/tendermint/blockchain/store.go:117.2,118.16 2 1 -github.com/tendermint/tendermint/blockchain/store.go:121.2,121.18 1 1 -github.com/tendermint/tendermint/blockchain/store.go:114.14,116.3 1 1 -github.com/tendermint/tendermint/blockchain/store.go:118.16,120.3 1 1 -github.com/tendermint/tendermint/blockchain/store.go:128.65,132.14 4 1 -github.com/tendermint/tendermint/blockchain/store.go:135.2,136.16 2 1 -github.com/tendermint/tendermint/blockchain/store.go:139.2,139.15 1 1 -github.com/tendermint/tendermint/blockchain/store.go:132.14,134.3 1 1 -github.com/tendermint/tendermint/blockchain/store.go:136.16,138.3 1 1 -github.com/tendermint/tendermint/blockchain/store.go:145.64,149.14 4 1 -github.com/tendermint/tendermint/blockchain/store.go:152.2,153.16 2 1 -github.com/tendermint/tendermint/blockchain/store.go:156.2,156.15 1 1 -github.com/tendermint/tendermint/blockchain/store.go:149.14,151.3 1 1 -github.com/tendermint/tendermint/blockchain/store.go:153.16,155.3 1 1 -github.com/tendermint/tendermint/blockchain/store.go:165.106,166.18 1 1 -github.com/tendermint/tendermint/blockchain/store.go:169.2,170.43 2 1 -github.com/tendermint/tendermint/blockchain/store.go:173.2,173.30 1 1 -github.com/tendermint/tendermint/blockchain/store.go:178.2,183.42 4 1 -github.com/tendermint/tendermint/blockchain/store.go:188.2,205.25 9 1 -github.com/tendermint/tendermint/blockchain/store.go:166.18,168.3 1 1 -github.com/tendermint/tendermint/blockchain/store.go:170.43,172.3 1 1 -github.com/tendermint/tendermint/blockchain/store.go:173.30,175.3 1 1 -github.com/tendermint/tendermint/blockchain/store.go:183.42,185.3 1 1 -github.com/tendermint/tendermint/blockchain/store.go:208.78,209.29 1 1 -github.com/tendermint/tendermint/blockchain/store.go:212.2,213.55 2 1 -github.com/tendermint/tendermint/blockchain/store.go:209.29,211.3 1 0 -github.com/tendermint/tendermint/blockchain/store.go:218.42,220.2 1 1 -github.com/tendermint/tendermint/blockchain/store.go:222.57,224.2 1 1 -github.com/tendermint/tendermint/blockchain/store.go:226.44,228.2 1 1 -github.com/tendermint/tendermint/blockchain/store.go:230.43,232.2 1 1 -github.com/tendermint/tendermint/blockchain/store.go:243.48,245.16 2 1 -github.com/tendermint/tendermint/blockchain/store.go:248.2,248.34 1 1 -github.com/tendermint/tendermint/blockchain/store.go:245.16,247.3 1 0 -github.com/tendermint/tendermint/blockchain/store.go:253.61,255.18 2 1 -github.com/tendermint/tendermint/blockchain/store.go:260.2,262.16 3 1 -github.com/tendermint/tendermint/blockchain/store.go:265.2,265.12 1 1 -github.com/tendermint/tendermint/blockchain/store.go:255.18,259.3 1 1 -github.com/tendermint/tendermint/blockchain/store.go:262.16,264.3 1 1 diff --git a/blockchain/store_test.go b/blockchain/store_test.go index 9b6728391..e967a1b9a 100644 --- a/blockchain/store_test.go +++ b/blockchain/store_test.go @@ -138,6 +138,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { // End of setup, test data + commitAtH10 := &types.Commit{Precommits: []*types.Vote{{Height: 10}}} tuples := []struct { block *types.Block parts *types.PartSet @@ -152,10 +153,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { eraseSeenCommitInDB bool }{ { - block: &types.Block{ - Header: &header1, - LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, - }, + block: newBlock(&header1, commitAtH10), parts: validPartSet, seenCommit: seenCommit1, }, @@ -166,28 +164,19 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { }, { - block: &types.Block{ - Header: &header2, - LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, - }, + block: newBlock(&header2, commitAtH10), parts: uncontiguousPartSet, wantPanic: "only save contiguous blocks", // and incomplete and uncontiguous parts }, { - block: &types.Block{ - Header: &header1, - LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, - }, + block: newBlock(&header1, commitAtH10), parts: incompletePartSet, wantPanic: "only save complete block", // incomplete parts }, { - block: &types.Block{ - Header: &header1, - LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, - }, + block: newBlock(&header1, commitAtH10), parts: validPartSet, seenCommit: seenCommit1, corruptCommitInDB: true, // Corrupt the DB's commit entry @@ -195,10 +184,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { }, { - block: &types.Block{ - Header: &header1, - LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, - }, + block: newBlock(&header1, commitAtH10), parts: validPartSet, seenCommit: seenCommit1, wantPanic: "rror reading block", @@ -206,10 +192,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { }, { - block: &types.Block{ - Header: &header1, - LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, - }, + block: newBlock(&header1, commitAtH10), parts: validPartSet, seenCommit: seenCommit1, @@ -218,10 +201,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { }, { - block: &types.Block{ - Header: &header1, - LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, - }, + block: newBlock(&header1, commitAtH10), parts: validPartSet, seenCommit: seenCommit1, @@ -230,10 +210,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { }, { - block: &types.Block{ - Header: &header1, - LastCommit: &types.Commit{Precommits: []*types.Vote{{Height: 10}}}, - }, + block: newBlock(&header1, commitAtH10), parts: validPartSet, seenCommit: seenCommit1, @@ -422,3 +399,10 @@ func doFn(fn func() (interface{}, error)) (res interface{}, err error, panicErr res, err = fn() return res, err, panicErr } + +func newBlock(hdr *types.Header, lastCommit *types.Commit) *types.Block { + return &types.Block{ + Header: hdr, + LastCommit: lastCommit, + } +} From 0bfc11f1bad6b029103de91edfb04cd61dbefcc8 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Sun, 10 Dec 2017 20:03:58 -0500 Subject: [PATCH 7/8] blockchain: note about store tests needing simplification ... --- blockchain/store_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/blockchain/store_test.go b/blockchain/store_test.go index e967a1b9a..3fd311015 100644 --- a/blockchain/store_test.go +++ b/blockchain/store_test.go @@ -106,25 +106,28 @@ var ( seenCommit1 = &types.Commit{Precommits: []*types.Vote{{Height: 10}}} ) +// TODO: This test should be simplified ... + func TestBlockStoreSaveLoadBlock(t *testing.T) { state, bs := makeStateAndBlockStore(log.NewTMLogger(new(bytes.Buffer))) require.Equal(t, bs.Height(), 0, "initially the height should be zero") + // check there are no blocks at various heights 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) } } - block := makeBlock(bs.Height()+1, state) + // save a block + 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}) - uncontiguousPartSet := types.NewPartSetFromHeader(types.PartSetHeader{Total: 0}) uncontiguousPartSet.AddPart(part2, false) From 78a682e4b6575647b7a077da78a324055c0d8658 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Fri, 15 Dec 2017 12:07:48 -0500 Subject: [PATCH 8/8] blockchain: test fixes --- blockchain/reactor_test.go | 3 +-- blockchain/store_test.go | 16 +++++++++------- p2p/pex_reactor.go | 1 + 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/blockchain/reactor_test.go b/blockchain/reactor_test.go index c473cddf1..ffe9cf4ec 100644 --- a/blockchain/reactor_test.go +++ b/blockchain/reactor_test.go @@ -26,8 +26,7 @@ func makeStateAndBlockStore(logger log.Logger) (*sm.State, *BlockStore) { return state, blockStore } -func newBlockchainReactor(logger log.Logger, maxBlockHeight int) *BlockchainReactor { - logger := log.TestingLogger() +func newBlockchainReactor(logger log.Logger, maxBlockHeight int64) *BlockchainReactor { state, blockStore := makeStateAndBlockStore(logger) // Make the blockchainReactor itself diff --git a/blockchain/store_test.go b/blockchain/store_test.go index 3fd311015..2b77371f6 100644 --- a/blockchain/store_test.go +++ b/blockchain/store_test.go @@ -7,6 +7,7 @@ import ( "runtime/debug" "strings" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -32,7 +33,7 @@ func TestNewBlockStore(t *testing.T) { db := db.NewMemDB() db.Set(blockStoreKey, []byte(`{"height": 10000}`)) bs := NewBlockStore(db) - assert.Equal(t, bs.Height(), 10000, "failed to properly parse blockstore") + assert.Equal(t, bs.Height(), int64(10000), "failed to properly parse blockstore") panicCausers := []struct { data []byte @@ -56,7 +57,7 @@ func TestNewBlockStore(t *testing.T) { db.Set(blockStoreKey, nil) bs = NewBlockStore(db) - assert.Equal(t, bs.Height(), 0, "expecting nil bytes to be unmarshaled alright") + assert.Equal(t, bs.Height(), int64(0), "expecting nil bytes to be unmarshaled alright") } func TestBlockStoreGetReader(t *testing.T) { @@ -110,10 +111,10 @@ var ( func TestBlockStoreSaveLoadBlock(t *testing.T) { state, bs := makeStateAndBlockStore(log.NewTMLogger(new(bytes.Buffer))) - require.Equal(t, bs.Height(), 0, "initially the height should be zero") + require.Equal(t, bs.Height(), int64(0), "initially the height should be zero") // check there are no blocks at various heights - noBlockHeights := []int{0, -1, 100, 1000, 2} + noBlockHeights := []int64{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) @@ -135,6 +136,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { Height: 1, NumTxs: 100, ChainID: "block_test", + Time: time.Now(), } header2 := header1 header2.Height = 4 @@ -306,7 +308,7 @@ func binarySerializeIt(v interface{}) []byte { func TestLoadBlockPart(t *testing.T) { bs, db := freshBlockStore() - height, index := 10, 1 + height, index := int64(10), 1 loadPart := func() (interface{}, error) { part := bs.LoadBlockPart(height, index) return part, nil @@ -334,7 +336,7 @@ func TestLoadBlockPart(t *testing.T) { func TestLoadBlockMeta(t *testing.T) { bs, db := freshBlockStore() - height := 10 + height := int64(10) loadMeta := func() (interface{}, error) { meta := bs.LoadBlockMeta(height) return meta, nil @@ -363,7 +365,7 @@ func TestLoadBlockMeta(t *testing.T) { func TestBlockFetchAtHeight(t *testing.T) { state, bs := makeStateAndBlockStore(log.NewTMLogger(new(bytes.Buffer))) - require.Equal(t, bs.Height(), 0, "initially the height should be zero") + require.Equal(t, bs.Height(), int64(0), "initially the height should be zero") block := makeBlock(bs.Height()+1, state) partSet := block.MakePartSet(2) diff --git a/p2p/pex_reactor.go b/p2p/pex_reactor.go index 960c8c641..b229a97bd 100644 --- a/p2p/pex_reactor.go +++ b/p2p/pex_reactor.go @@ -264,6 +264,7 @@ func (r *PEXReactor) ensurePeers() { if dialling := r.Switch.IsDialing(try); dialling { continue } + // XXX: does this work ?! if connected := r.Switch.Peers().Has(try.IP.String()); connected { continue }