blockchain: Reorg reactor (#3561)
* go routines in blockchain reactor
* Added reference to the go routine diagram
* Initial commit
* cleanup
* Undo testing_logger change, committed by mistake
* Fix the test loggers
* pulled some fsm code into pool.go
* added pool tests
* changes to the design
added block requests under peer
moved the request trigger in the reactor poolRoutine, triggered now by a ticker
in general moved everything required for making block requests smarter in the poolRoutine
added a simple map of heights to keep track of what will need to be requested next
added a few more tests
* send errors to FSM in a different channel than blocks
send errors (RemovePeer) from switch on a different channel than the
one receiving blocks
renamed channels
added more pool tests
* more pool tests
* lint errors
* more tests
* more tests
* switch fast sync to new implementation
* fixed data race in tests
* cleanup
* finished fsm tests
* address golangci comments :)
* address golangci comments :)
* Added timeout on next block needed to advance
* updating docs and cleanup
* fix issue in test from previous cleanup
* cleanup
* Added termination scenarios, tests and more cleanup
* small fixes to adr, comments and cleanup
* Fix bug in sendRequest()
If we tried to send a request to a peer not present in the switch, a
missing continue statement caused the request to be blackholed in a peer
that was removed and never retried.
While this bug was manifesting, the reactor kept asking for other
blocks that would be stored and never consumed. Added the number of
unconsumed blocks in the math for requesting blocks ahead of current
processing height so eventually there will be no more blocks requested
until the already received ones are consumed.
* remove bpPeer's didTimeout field
* Use distinct err codes for peer timeout and FSM timeouts
* Don't allow peers to update with lower height
* review comments from Ethan and Zarko
* some cleanup, renaming, comments
* Move block execution in separate goroutine
* Remove pool's numPending
* review comments
* fix lint, remove old blockchain reactor and duplicates in fsm tests
* small reorg around peer after review comments
* add the reactor spec
* verify block only once
* review comments
* change to int for max number of pending requests
* cleanup and godoc
* Add configuration flag fast sync version
* golangci fixes
* fix config template
* move both reactor versions under blockchain
* cleanup, golint, renaming stuff
* updated documentation, fixed more golint warnings
* integrate with behavior package
* sync with master
* gofmt
* add changelog_pending entry
* move to improvments
* suggestion to changelog entry
2019-07-23 10:58:52 +02:00
|
|
|
package store
|
2018-06-20 17:35:30 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2019-02-18 08:45:27 +01:00
|
|
|
"os"
|
2018-06-20 17:35:30 -07:00
|
|
|
"runtime/debug"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2019-02-04 13:01:59 -05:00
|
|
|
"time"
|
2018-06-20 17:35:30 -07:00
|
|
|
|
2019-08-08 12:31:13 +02:00
|
|
|
"github.com/pkg/errors"
|
2018-06-20 17:35:30 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-07-31 11:34:17 +02:00
|
|
|
db "github.com/tendermint/tm-db"
|
|
|
|
dbm "github.com/tendermint/tm-db"
|
2019-07-19 07:54:45 +02:00
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
cfg "github.com/tendermint/tendermint/config"
|
2018-07-01 22:36:49 -04:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2018-11-26 15:31:11 -05:00
|
|
|
sm "github.com/tendermint/tendermint/state"
|
2018-06-20 17:35:30 -07:00
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/types"
|
2018-09-01 01:33:51 +02:00
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
2018-06-20 17:35:30 -07:00
|
|
|
)
|
|
|
|
|
2019-02-18 13:23:40 +04:00
|
|
|
// A cleanupFunc cleans up any config / test files created for a particular
|
|
|
|
// test.
|
2019-02-18 08:45:27 +01:00
|
|
|
type cleanupFunc func()
|
|
|
|
|
2019-02-04 13:01:59 -05:00
|
|
|
// make a Commit with a single vote containing just the height and a timestamp
|
|
|
|
func makeTestCommit(height int64, timestamp time.Time) *types.Commit {
|
2019-02-08 18:40:41 -05:00
|
|
|
commitSigs := []*types.CommitSig{{Height: height, Timestamp: timestamp}}
|
|
|
|
return types.NewCommit(types.BlockID{}, commitSigs)
|
2019-02-04 13:01:59 -05:00
|
|
|
}
|
|
|
|
|
blockchain: Reorg reactor (#3561)
* go routines in blockchain reactor
* Added reference to the go routine diagram
* Initial commit
* cleanup
* Undo testing_logger change, committed by mistake
* Fix the test loggers
* pulled some fsm code into pool.go
* added pool tests
* changes to the design
added block requests under peer
moved the request trigger in the reactor poolRoutine, triggered now by a ticker
in general moved everything required for making block requests smarter in the poolRoutine
added a simple map of heights to keep track of what will need to be requested next
added a few more tests
* send errors to FSM in a different channel than blocks
send errors (RemovePeer) from switch on a different channel than the
one receiving blocks
renamed channels
added more pool tests
* more pool tests
* lint errors
* more tests
* more tests
* switch fast sync to new implementation
* fixed data race in tests
* cleanup
* finished fsm tests
* address golangci comments :)
* address golangci comments :)
* Added timeout on next block needed to advance
* updating docs and cleanup
* fix issue in test from previous cleanup
* cleanup
* Added termination scenarios, tests and more cleanup
* small fixes to adr, comments and cleanup
* Fix bug in sendRequest()
If we tried to send a request to a peer not present in the switch, a
missing continue statement caused the request to be blackholed in a peer
that was removed and never retried.
While this bug was manifesting, the reactor kept asking for other
blocks that would be stored and never consumed. Added the number of
unconsumed blocks in the math for requesting blocks ahead of current
processing height so eventually there will be no more blocks requested
until the already received ones are consumed.
* remove bpPeer's didTimeout field
* Use distinct err codes for peer timeout and FSM timeouts
* Don't allow peers to update with lower height
* review comments from Ethan and Zarko
* some cleanup, renaming, comments
* Move block execution in separate goroutine
* Remove pool's numPending
* review comments
* fix lint, remove old blockchain reactor and duplicates in fsm tests
* small reorg around peer after review comments
* add the reactor spec
* verify block only once
* review comments
* change to int for max number of pending requests
* cleanup and godoc
* Add configuration flag fast sync version
* golangci fixes
* fix config template
* move both reactor versions under blockchain
* cleanup, golint, renaming stuff
* updated documentation, fixed more golint warnings
* integrate with behavior package
* sync with master
* gofmt
* add changelog_pending entry
* move to improvments
* suggestion to changelog entry
2019-07-23 10:58:52 +02:00
|
|
|
func makeTxs(height int64) (txs []types.Tx) {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
txs = append(txs, types.Tx([]byte{byte(height), byte(i)}))
|
|
|
|
}
|
|
|
|
return txs
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeBlock(height int64, state sm.State, lastCommit *types.Commit) *types.Block {
|
|
|
|
block, _ := state.MakeBlock(height, makeTxs(height), lastCommit, nil, state.Validators.GetProposer().Address)
|
|
|
|
return block
|
|
|
|
}
|
|
|
|
|
2019-02-18 08:45:27 +01:00
|
|
|
func makeStateAndBlockStore(logger log.Logger) (sm.State, *BlockStore, cleanupFunc) {
|
2018-11-26 15:31:11 -05:00
|
|
|
config := cfg.ResetTestRoot("blockchain_reactor_test")
|
|
|
|
// blockDB := dbm.NewDebugDB("blockDB", dbm.NewMemDB())
|
|
|
|
// stateDB := dbm.NewDebugDB("stateDB", dbm.NewMemDB())
|
|
|
|
blockDB := dbm.NewMemDB()
|
|
|
|
stateDB := dbm.NewMemDB()
|
|
|
|
state, err := sm.LoadStateFromDBOrGenesisFile(stateDB, config.GenesisFile())
|
|
|
|
if err != nil {
|
2019-08-08 12:31:13 +02:00
|
|
|
panic(errors.Wrap(err, "error constructing state from genesis file"))
|
2018-11-26 15:31:11 -05:00
|
|
|
}
|
2019-02-18 08:45:27 +01:00
|
|
|
return state, NewBlockStore(blockDB), func() { os.RemoveAll(config.RootDir) }
|
2018-11-26 15:31:11 -05:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:35:30 -07:00
|
|
|
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)
|
|
|
|
require.Equal(t, int64(10000), bs.Height(), "failed to properly parse blockstore")
|
|
|
|
|
|
|
|
panicCausers := []struct {
|
|
|
|
data []byte
|
|
|
|
wantErr string
|
|
|
|
}{
|
|
|
|
{[]byte("artful-doger"), "not unmarshal bytes"},
|
|
|
|
{[]byte(" "), "unmarshal bytes"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range panicCausers {
|
2019-09-11 01:15:18 -04:00
|
|
|
tt := tt
|
2018-06-20 17:35:30 -07:00
|
|
|
// 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)
|
2018-06-09 04:25:48 -07:00
|
|
|
assert.Contains(t, fmt.Sprintf("%#v", panicErr), tt.wantErr, "#%d data: %q", i, tt.data)
|
2018-06-20 17:35:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
db.Set(blockStoreKey, nil)
|
|
|
|
bs = NewBlockStore(db)
|
|
|
|
assert.Equal(t, bs.Height(), int64(0), "expecting nil bytes to be unmarshaled alright")
|
|
|
|
}
|
|
|
|
|
|
|
|
func freshBlockStore() (*BlockStore, db.DB) {
|
|
|
|
db := db.NewMemDB()
|
|
|
|
return NewBlockStore(db), db
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2019-02-18 08:45:27 +01:00
|
|
|
state sm.State
|
|
|
|
block *types.Block
|
|
|
|
partSet *types.PartSet
|
|
|
|
part1 *types.Part
|
|
|
|
part2 *types.Part
|
|
|
|
seenCommit1 *types.Commit
|
|
|
|
)
|
2018-06-20 17:35:30 -07:00
|
|
|
|
2019-02-18 08:45:27 +01:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
var cleanup cleanupFunc
|
|
|
|
state, _, cleanup = makeStateAndBlockStore(log.NewTMLogger(new(bytes.Buffer)))
|
|
|
|
block = makeBlock(1, state, new(types.Commit))
|
|
|
|
partSet = block.MakePartSet(2)
|
|
|
|
part1 = partSet.GetPart(0)
|
|
|
|
part2 = partSet.GetPart(1)
|
2019-02-04 13:01:59 -05:00
|
|
|
seenCommit1 = makeTestCommit(10, tmtime.Now())
|
2019-02-18 08:45:27 +01:00
|
|
|
code := m.Run()
|
|
|
|
cleanup()
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
2018-06-20 17:35:30 -07:00
|
|
|
|
|
|
|
// TODO: This test should be simplified ...
|
|
|
|
|
|
|
|
func TestBlockStoreSaveLoadBlock(t *testing.T) {
|
2019-02-18 08:45:27 +01:00
|
|
|
state, bs, cleanup := makeStateAndBlockStore(log.NewTMLogger(new(bytes.Buffer)))
|
|
|
|
defer cleanup()
|
2018-06-20 17:35:30 -07:00
|
|
|
require.Equal(t, bs.Height(), int64(0), "initially the height should be zero")
|
|
|
|
|
|
|
|
// check there are no blocks at various heights
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// save a block
|
2018-11-26 15:31:11 -05:00
|
|
|
block := makeBlock(bs.Height()+1, state, new(types.Commit))
|
2018-06-20 17:35:30 -07:00
|
|
|
validPartSet := block.MakePartSet(2)
|
2019-02-04 13:01:59 -05:00
|
|
|
seenCommit := makeTestCommit(10, tmtime.Now())
|
2018-06-20 17:35:30 -07:00
|
|
|
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)
|
|
|
|
|
|
|
|
header1 := types.Header{
|
|
|
|
Height: 1,
|
|
|
|
NumTxs: 100,
|
|
|
|
ChainID: "block_test",
|
2018-09-01 01:33:51 +02:00
|
|
|
Time: tmtime.Now(),
|
2018-06-20 17:35:30 -07:00
|
|
|
}
|
|
|
|
header2 := header1
|
|
|
|
header2.Height = 4
|
|
|
|
|
|
|
|
// End of setup, test data
|
|
|
|
|
2019-02-04 13:01:59 -05:00
|
|
|
commitAtH10 := makeTestCommit(10, tmtime.Now())
|
2018-06-20 17:35:30 -07:00
|
|
|
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
|
|
|
|
}{
|
|
|
|
{
|
2018-07-13 12:05:54 +04:00
|
|
|
block: newBlock(header1, commitAtH10),
|
2018-06-20 17:35:30 -07:00
|
|
|
parts: validPartSet,
|
|
|
|
seenCommit: seenCommit1,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
block: nil,
|
|
|
|
wantPanic: "only save a non-nil block",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2018-07-13 12:05:54 +04:00
|
|
|
block: newBlock(header2, commitAtH10),
|
2018-06-20 17:35:30 -07:00
|
|
|
parts: uncontiguousPartSet,
|
|
|
|
wantPanic: "only save contiguous blocks", // and incomplete and uncontiguous parts
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2018-07-13 12:05:54 +04:00
|
|
|
block: newBlock(header1, commitAtH10),
|
2018-06-20 17:35:30 -07:00
|
|
|
parts: incompletePartSet,
|
|
|
|
wantPanic: "only save complete block", // incomplete parts
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2018-07-13 12:05:54 +04:00
|
|
|
block: newBlock(header1, commitAtH10),
|
2018-06-20 17:35:30 -07:00
|
|
|
parts: validPartSet,
|
|
|
|
seenCommit: seenCommit1,
|
|
|
|
corruptCommitInDB: true, // Corrupt the DB's commit entry
|
2018-07-01 22:12:58 -04:00
|
|
|
wantPanic: "unmarshal to types.Commit failed",
|
2018-06-20 17:35:30 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2018-07-13 12:05:54 +04:00
|
|
|
block: newBlock(header1, commitAtH10),
|
2018-06-20 17:35:30 -07:00
|
|
|
parts: validPartSet,
|
|
|
|
seenCommit: seenCommit1,
|
2018-07-01 22:12:58 -04:00
|
|
|
wantPanic: "unmarshal to types.BlockMeta failed",
|
2018-06-20 17:35:30 -07:00
|
|
|
corruptBlockInDB: true, // Corrupt the DB's block entry
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2018-07-13 12:05:54 +04:00
|
|
|
block: newBlock(header1, commitAtH10),
|
2018-06-20 17:35:30 -07:00
|
|
|
parts: validPartSet,
|
|
|
|
seenCommit: seenCommit1,
|
|
|
|
|
|
|
|
// Expecting no error and we want a nil back
|
|
|
|
eraseSeenCommitInDB: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2018-07-13 12:05:54 +04:00
|
|
|
block: newBlock(header1, commitAtH10),
|
2018-06-20 17:35:30 -07:00
|
|
|
parts: validPartSet,
|
|
|
|
seenCommit: seenCommit1,
|
|
|
|
|
|
|
|
corruptSeenCommitInDB: true,
|
2018-07-01 22:12:58 -04:00
|
|
|
wantPanic: "unmarshal to types.Commit failed",
|
2018-06-20 17:35:30 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2018-07-13 12:05:54 +04:00
|
|
|
block: newBlock(header1, commitAtH10),
|
2018-06-20 17:35:30 -07:00
|
|
|
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 {
|
2019-09-11 01:15:18 -04:00
|
|
|
tuple := tuple
|
2018-06-20 17:35:30 -07:00
|
|
|
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)
|
2018-06-09 04:25:48 -07:00
|
|
|
} else if got := fmt.Sprintf("%#v", panicErr); !strings.Contains(got, subStr) {
|
2018-06-20 17:35:30 -07:00
|
|
|
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 TestLoadBlockPart(t *testing.T) {
|
|
|
|
bs, db := freshBlockStore()
|
|
|
|
height, index := int64(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")
|
2018-07-01 22:12:58 -04:00
|
|
|
require.Contains(t, panicErr.Error(), "unmarshal to types.Part failed")
|
2018-06-20 17:35:30 -07:00
|
|
|
|
|
|
|
// 3. A good block serialized and saved to the DB should be retrievable
|
|
|
|
db.Set(calcBlockPartKey(height, index), cdc.MustMarshalBinaryBare(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")
|
2019-01-13 17:02:38 -06:00
|
|
|
require.Equal(t, gotPart.(*types.Part), part1,
|
2018-06-20 17:35:30 -07:00
|
|
|
"expecting successful retrieval of previously saved block")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadBlockMeta(t *testing.T) {
|
|
|
|
bs, db := freshBlockStore()
|
|
|
|
height := int64(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")
|
2018-07-01 22:12:58 -04:00
|
|
|
require.Contains(t, panicErr.Error(), "unmarshal to types.BlockMeta")
|
2018-06-20 17:35:30 -07:00
|
|
|
|
|
|
|
// 3. A good blockMeta serialized and saved to the DB should be retrievable
|
|
|
|
meta := &types.BlockMeta{}
|
|
|
|
db.Set(calcBlockMetaKey(height), cdc.MustMarshalBinaryBare(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, cdc.MustMarshalBinaryBare(meta), cdc.MustMarshalBinaryBare(gotMeta),
|
|
|
|
"expecting successful retrieval of previously saved blockMeta")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlockFetchAtHeight(t *testing.T) {
|
2019-02-18 08:45:27 +01:00
|
|
|
state, bs, cleanup := makeStateAndBlockStore(log.NewTMLogger(new(bytes.Buffer)))
|
|
|
|
defer cleanup()
|
2018-06-20 17:35:30 -07:00
|
|
|
require.Equal(t, bs.Height(), int64(0), "initially the height should be zero")
|
2018-11-26 15:31:11 -05:00
|
|
|
block := makeBlock(bs.Height()+1, state, new(types.Commit))
|
2018-06-20 17:35:30 -07:00
|
|
|
|
|
|
|
partSet := block.MakePartSet(2)
|
2019-02-04 13:01:59 -05:00
|
|
|
seenCommit := makeTestCommit(10, tmtime.Now())
|
2018-06-20 17:35:30 -07:00
|
|
|
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())
|
|
|
|
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")
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-07-13 12:05:54 +04:00
|
|
|
func newBlock(hdr types.Header, lastCommit *types.Commit) *types.Block {
|
2018-06-20 17:35:30 -07:00
|
|
|
return &types.Block{
|
|
|
|
Header: hdr,
|
|
|
|
LastCommit: lastCommit,
|
|
|
|
}
|
|
|
|
}
|