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 v0
|
2017-08-26 02:33:19 -06:00
|
|
|
|
|
|
|
import (
|
2019-02-18 08:45:27 +01:00
|
|
|
"os"
|
2018-11-26 15:31:11 -05:00
|
|
|
"sort"
|
2017-08-26 02:33:19 -06:00
|
|
|
"testing"
|
2018-11-26 15:31:11 -05:00
|
|
|
"time"
|
2017-08-26 02:33:19 -06:00
|
|
|
|
2019-08-08 12:31:13 +02:00
|
|
|
"github.com/pkg/errors"
|
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
|
|
|
"github.com/tendermint/tendermint/store"
|
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
cfg "github.com/tendermint/tendermint/config"
|
2018-07-01 22:36:49 -04:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2019-05-04 10:41:31 +04:00
|
|
|
"github.com/tendermint/tendermint/mock"
|
2017-08-26 02:33:19 -06:00
|
|
|
"github.com/tendermint/tendermint/p2p"
|
2017-12-28 18:26:13 -05:00
|
|
|
"github.com/tendermint/tendermint/proxy"
|
2017-08-26 02:33:19 -06:00
|
|
|
sm "github.com/tendermint/tendermint/state"
|
|
|
|
"github.com/tendermint/tendermint/types"
|
2018-11-26 15:31:11 -05:00
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
2019-07-31 11:34:17 +02:00
|
|
|
dbm "github.com/tendermint/tm-db"
|
2017-08-26 02:33:19 -06:00
|
|
|
)
|
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
var config *cfg.Config
|
|
|
|
|
|
|
|
func randGenesisDoc(numValidators int, randPower bool, minPower int64) (*types.GenesisDoc, []types.PrivValidator) {
|
|
|
|
validators := make([]types.GenesisValidator, numValidators)
|
|
|
|
privValidators := make([]types.PrivValidator, numValidators)
|
|
|
|
for i := 0; i < numValidators; i++ {
|
|
|
|
val, privVal := types.RandValidator(randPower, minPower)
|
|
|
|
validators[i] = types.GenesisValidator{
|
|
|
|
PubKey: val.PubKey,
|
|
|
|
Power: val.VotingPower,
|
|
|
|
}
|
|
|
|
privValidators[i] = privVal
|
|
|
|
}
|
|
|
|
sort.Sort(types.PrivValidatorsByAddress(privValidators))
|
|
|
|
|
|
|
|
return &types.GenesisDoc{
|
|
|
|
GenesisTime: tmtime.Now(),
|
|
|
|
ChainID: config.ChainID(),
|
|
|
|
Validators: validators,
|
|
|
|
}, privValidators
|
|
|
|
}
|
|
|
|
|
|
|
|
type BlockchainReactorPair struct {
|
|
|
|
reactor *BlockchainReactor
|
|
|
|
app proxy.AppConns
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBlockchainReactor(logger log.Logger, genDoc *types.GenesisDoc, privVals []types.PrivValidator, maxBlockHeight int64) BlockchainReactorPair {
|
|
|
|
if len(privVals) != 1 {
|
|
|
|
panic("only support one validator")
|
|
|
|
}
|
|
|
|
|
|
|
|
app := &testApp{}
|
|
|
|
cc := proxy.NewLocalClientCreator(app)
|
|
|
|
proxyApp := proxy.NewAppConns(cc)
|
|
|
|
err := proxyApp.Start()
|
|
|
|
if err != nil {
|
2019-08-08 12:31:13 +02:00
|
|
|
panic(errors.Wrap(err, "error start app"))
|
2018-11-26 15:31:11 -05:00
|
|
|
}
|
|
|
|
|
2018-04-05 05:17:43 -07:00
|
|
|
blockDB := dbm.NewMemDB()
|
|
|
|
stateDB := dbm.NewMemDB()
|
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
|
|
|
blockStore := store.NewBlockStore(blockDB)
|
2018-11-26 15:31:11 -05:00
|
|
|
|
|
|
|
state, err := sm.LoadStateFromDBOrGenesisDoc(stateDB, genDoc)
|
2018-04-03 07:03:08 -07:00
|
|
|
if err != nil {
|
2019-08-08 12:31:13 +02:00
|
|
|
panic(errors.Wrap(err, "error constructing state from genesis file"))
|
2018-04-03 07:03:08 -07:00
|
|
|
}
|
2017-10-14 19:21:13 -06:00
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
// Make the BlockchainReactor itself.
|
|
|
|
// NOTE we have to create and commit the blocks first because
|
|
|
|
// pool.height is determined from the store.
|
2017-08-26 02:33:19 -06:00
|
|
|
fastSync := true
|
2019-05-02 05:15:53 +08:00
|
|
|
db := dbm.NewMemDB()
|
|
|
|
blockExec := sm.NewBlockExecutor(db, log.TestingLogger(), proxyApp.Consensus(),
|
2019-05-04 10:41:31 +04:00
|
|
|
mock.Mempool{}, sm.MockEvidencePool{})
|
2019-05-02 05:15:53 +08:00
|
|
|
sm.SaveState(db, state)
|
2017-12-27 20:40:36 -05:00
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
// let's add some blocks in
|
|
|
|
for blockHeight := int64(1); blockHeight <= maxBlockHeight; blockHeight++ {
|
2019-02-08 18:40:41 -05:00
|
|
|
lastCommit := types.NewCommit(types.BlockID{}, nil)
|
2018-11-26 15:31:11 -05:00
|
|
|
if blockHeight > 1 {
|
|
|
|
lastBlockMeta := blockStore.LoadBlockMeta(blockHeight - 1)
|
|
|
|
lastBlock := blockStore.LoadBlock(blockHeight - 1)
|
2017-08-26 02:33:19 -06:00
|
|
|
|
2019-07-25 10:13:19 +02:00
|
|
|
vote, err := types.MakeVote(lastBlock.Header.Height, lastBlockMeta.BlockID, state.Validators, privVals[0], lastBlock.Header.ChainID)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
voteCommitSig := vote.CommitSig()
|
|
|
|
lastCommit = types.NewCommit(lastBlockMeta.BlockID, []*types.CommitSig{voteCommitSig})
|
2018-11-26 15:31:11 -05:00
|
|
|
}
|
2017-08-26 02:33:19 -06:00
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
thisBlock := makeBlock(blockHeight, state, lastCommit)
|
|
|
|
|
|
|
|
thisParts := thisBlock.MakePartSet(types.BlockPartSizeBytes)
|
2019-06-21 07:58:32 +02:00
|
|
|
blockID := types.BlockID{Hash: thisBlock.Hash(), PartsHeader: thisParts.Header()}
|
2018-11-26 15:31:11 -05:00
|
|
|
|
|
|
|
state, err = blockExec.ApplyBlock(state, blockID, thisBlock)
|
|
|
|
if err != nil {
|
2019-08-08 12:31:13 +02:00
|
|
|
panic(errors.Wrap(err, "error apply block"))
|
2018-11-26 15:31:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
blockStore.SaveBlock(thisBlock, thisParts, lastCommit)
|
2017-08-26 02:33:19 -06:00
|
|
|
}
|
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
bcReactor := NewBlockchainReactor(state.Copy(), blockExec, blockStore, fastSync)
|
|
|
|
bcReactor.SetLogger(logger.With("module", "blockchain"))
|
|
|
|
|
|
|
|
return BlockchainReactorPair{bcReactor, proxyApp}
|
2017-08-26 02:33:19 -06:00
|
|
|
}
|
|
|
|
|
2018-01-23 22:06:40 -05:00
|
|
|
func TestNoBlockResponse(t *testing.T) {
|
2018-11-26 15:31:11 -05:00
|
|
|
config = cfg.ResetTestRoot("blockchain_reactor_test")
|
2019-02-18 08:45:27 +01:00
|
|
|
defer os.RemoveAll(config.RootDir)
|
2018-11-26 15:31:11 -05:00
|
|
|
genDoc, privVals := randGenesisDoc(1, false, 30)
|
|
|
|
|
|
|
|
maxBlockHeight := int64(65)
|
|
|
|
|
|
|
|
reactorPairs := make([]BlockchainReactorPair, 2)
|
|
|
|
|
|
|
|
reactorPairs[0] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, maxBlockHeight)
|
|
|
|
reactorPairs[1] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, 0)
|
2017-08-26 02:33:19 -06:00
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
p2p.MakeConnectedSwitches(config.P2P, 2, func(i int, s *p2p.Switch) *p2p.Switch {
|
|
|
|
s.AddReactor("BLOCKCHAIN", reactorPairs[i].reactor)
|
|
|
|
return s
|
2017-08-26 02:33:19 -06:00
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
}, p2p.Connect2Switches)
|
2017-08-26 02:33:19 -06:00
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
defer func() {
|
|
|
|
for _, r := range reactorPairs {
|
|
|
|
r.reactor.Stop()
|
|
|
|
r.app.Stop()
|
|
|
|
}
|
|
|
|
}()
|
2017-10-04 17:27:07 -04:00
|
|
|
|
|
|
|
tests := []struct {
|
2017-12-01 19:04:53 -06:00
|
|
|
height int64
|
2017-08-26 02:33:19 -06:00
|
|
|
existent bool
|
|
|
|
}{
|
2017-10-04 17:27:07 -04:00
|
|
|
{maxBlockHeight + 2, false},
|
|
|
|
{10, true},
|
|
|
|
{1, true},
|
|
|
|
{100, false},
|
2017-08-26 02:33:19 -06:00
|
|
|
}
|
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
for {
|
|
|
|
if reactorPairs[1].reactor.pool.IsCaughtUp() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, maxBlockHeight, reactorPairs[0].reactor.store.Height())
|
2017-10-04 17:27:07 -04:00
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
for _, tt := range tests {
|
|
|
|
block := reactorPairs[1].reactor.store.LoadBlock(tt.height)
|
2017-10-04 17:27:07 -04:00
|
|
|
if tt.existent {
|
2018-11-26 15:31:11 -05:00
|
|
|
assert.True(t, block != nil)
|
2017-08-26 02:33:19 -06:00
|
|
|
} else {
|
2018-11-26 15:31:11 -05:00
|
|
|
assert.True(t, block == nil)
|
2017-08-26 02:33:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 22:06:40 -05:00
|
|
|
// NOTE: This is too hard to test without
|
|
|
|
// an easy way to add test peer to switch
|
|
|
|
// or without significant refactoring of the module.
|
|
|
|
// Alternatively we could actually dial a TCP conn but
|
|
|
|
// that seems extreme.
|
|
|
|
func TestBadBlockStopsPeer(t *testing.T) {
|
2018-11-26 15:31:11 -05:00
|
|
|
config = cfg.ResetTestRoot("blockchain_reactor_test")
|
2019-02-18 08:45:27 +01:00
|
|
|
defer os.RemoveAll(config.RootDir)
|
2018-11-26 15:31:11 -05:00
|
|
|
genDoc, privVals := randGenesisDoc(1, false, 30)
|
|
|
|
|
|
|
|
maxBlockHeight := int64(148)
|
|
|
|
|
|
|
|
otherChain := newBlockchainReactor(log.TestingLogger(), genDoc, privVals, maxBlockHeight)
|
|
|
|
defer func() {
|
|
|
|
otherChain.reactor.Stop()
|
|
|
|
otherChain.app.Stop()
|
|
|
|
}()
|
|
|
|
|
|
|
|
reactorPairs := make([]BlockchainReactorPair, 4)
|
|
|
|
|
|
|
|
reactorPairs[0] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, maxBlockHeight)
|
|
|
|
reactorPairs[1] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, 0)
|
|
|
|
reactorPairs[2] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, 0)
|
|
|
|
reactorPairs[3] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, 0)
|
|
|
|
|
|
|
|
switches := p2p.MakeConnectedSwitches(config.P2P, 4, func(i int, s *p2p.Switch) *p2p.Switch {
|
|
|
|
s.AddReactor("BLOCKCHAIN", reactorPairs[i].reactor)
|
|
|
|
return s
|
|
|
|
|
|
|
|
}, p2p.Connect2Switches)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
for _, r := range reactorPairs {
|
|
|
|
r.reactor.Stop()
|
|
|
|
r.app.Stop()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
if reactorPairs[3].reactor.pool.IsCaughtUp() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
//at this time, reactors[0-3] is the newest
|
|
|
|
assert.Equal(t, 3, reactorPairs[1].reactor.Switch.Peers().Size())
|
2018-01-23 22:06:40 -05:00
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
//mark reactorPairs[3] is an invalid peer
|
|
|
|
reactorPairs[3].reactor.store = otherChain.reactor.store
|
2018-01-23 22:06:40 -05:00
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
lastReactorPair := newBlockchainReactor(log.TestingLogger(), genDoc, privVals, 0)
|
|
|
|
reactorPairs = append(reactorPairs, lastReactorPair)
|
2018-01-23 22:06:40 -05:00
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
switches = append(switches, p2p.MakeConnectedSwitches(config.P2P, 1, func(i int, s *p2p.Switch) *p2p.Switch {
|
|
|
|
s.AddReactor("BLOCKCHAIN", reactorPairs[len(reactorPairs)-1].reactor)
|
|
|
|
return s
|
2018-01-23 22:06:40 -05:00
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
}, p2p.Connect2Switches)...)
|
|
|
|
|
|
|
|
for i := 0; i < len(reactorPairs)-1; i++ {
|
|
|
|
p2p.Connect2Switches(switches, i, len(reactorPairs)-1)
|
|
|
|
}
|
2018-01-23 22:06:40 -05:00
|
|
|
|
|
|
|
for {
|
2018-11-26 15:31:11 -05:00
|
|
|
if lastReactorPair.reactor.pool.IsCaughtUp() || lastReactorPair.reactor.Switch.Peers().Size() == 0 {
|
|
|
|
break
|
2018-01-23 22:06:40 -05:00
|
|
|
}
|
2018-11-26 15:31:11 -05:00
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
2018-01-23 22:06:40 -05:00
|
|
|
}
|
2018-11-26 15:31:11 -05:00
|
|
|
|
|
|
|
assert.True(t, lastReactorPair.reactor.Switch.Peers().Size() < len(reactorPairs)-1)
|
2018-01-23 22:06:40 -05:00
|
|
|
}
|
|
|
|
|
2019-08-11 14:27:03 -04:00
|
|
|
func TestBcBlockRequestMessageValidateBasic(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
testName string
|
|
|
|
requestHeight int64
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{"Valid Request Message", 0, false},
|
|
|
|
{"Valid Request Message", 1, false},
|
|
|
|
{"Invalid Request Message", -1, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2019-09-11 01:15:18 -04:00
|
|
|
tc := tc
|
2019-08-11 14:27:03 -04:00
|
|
|
t.Run(tc.testName, func(t *testing.T) {
|
|
|
|
request := bcBlockRequestMessage{Height: tc.requestHeight}
|
|
|
|
assert.Equal(t, tc.expectErr, request.ValidateBasic() != nil, "Validate Basic had an unexpected result")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBcNoBlockResponseMessageValidateBasic(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
testName string
|
|
|
|
nonResponseHeight int64
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{"Valid Non-Response Message", 0, false},
|
|
|
|
{"Valid Non-Response Message", 1, false},
|
|
|
|
{"Invalid Non-Response Message", -1, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2019-09-11 01:15:18 -04:00
|
|
|
tc := tc
|
2019-08-11 14:27:03 -04:00
|
|
|
t.Run(tc.testName, func(t *testing.T) {
|
|
|
|
nonResponse := bcNoBlockResponseMessage{Height: tc.nonResponseHeight}
|
|
|
|
assert.Equal(t, tc.expectErr, nonResponse.ValidateBasic() != nil, "Validate Basic had an unexpected result")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBcStatusRequestMessageValidateBasic(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
testName string
|
|
|
|
requestHeight int64
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{"Valid Request Message", 0, false},
|
|
|
|
{"Valid Request Message", 1, false},
|
|
|
|
{"Invalid Request Message", -1, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2019-09-11 01:15:18 -04:00
|
|
|
tc := tc
|
2019-08-11 14:27:03 -04:00
|
|
|
t.Run(tc.testName, func(t *testing.T) {
|
|
|
|
request := bcStatusRequestMessage{Height: tc.requestHeight}
|
|
|
|
assert.Equal(t, tc.expectErr, request.ValidateBasic() != nil, "Validate Basic had an unexpected result")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBcStatusResponseMessageValidateBasic(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
testName string
|
|
|
|
responseHeight int64
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{"Valid Response Message", 0, false},
|
|
|
|
{"Valid Response Message", 1, false},
|
|
|
|
{"Invalid Response Message", -1, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2019-09-11 01:15:18 -04:00
|
|
|
tc := tc
|
2019-08-11 14:27:03 -04:00
|
|
|
t.Run(tc.testName, func(t *testing.T) {
|
|
|
|
response := bcStatusResponseMessage{Height: tc.responseHeight}
|
|
|
|
assert.Equal(t, tc.expectErr, response.ValidateBasic() != nil, "Validate Basic had an unexpected result")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 17:27:07 -04:00
|
|
|
//----------------------------------------------
|
|
|
|
// utility funcs
|
|
|
|
|
2017-12-01 19:04:53 -06:00
|
|
|
func makeTxs(height int64) (txs []types.Tx) {
|
2017-08-26 02:33:19 -06:00
|
|
|
for i := 0; i < 10; i++ {
|
2017-11-30 13:08:38 -06:00
|
|
|
txs = append(txs, types.Tx([]byte{byte(height), byte(i)}))
|
2017-08-26 02:33:19 -06:00
|
|
|
}
|
|
|
|
return txs
|
|
|
|
}
|
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
func makeBlock(height int64, state sm.State, lastCommit *types.Commit) *types.Block {
|
|
|
|
block, _ := state.MakeBlock(height, makeTxs(height), lastCommit, nil, state.Validators.GetProposer().Address)
|
2017-08-26 02:33:19 -06:00
|
|
|
return block
|
|
|
|
}
|
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
type testApp struct {
|
|
|
|
abci.BaseApplication
|
2017-08-26 02:33:19 -06:00
|
|
|
}
|
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
var _ abci.Application = (*testApp)(nil)
|
2017-08-26 02:33:19 -06:00
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
func (app *testApp) Info(req abci.RequestInfo) (resInfo abci.ResponseInfo) {
|
|
|
|
return abci.ResponseInfo{}
|
2017-10-04 17:27:07 -04:00
|
|
|
}
|
2017-08-26 02:33:19 -06:00
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
func (app *testApp) BeginBlock(req abci.RequestBeginBlock) abci.ResponseBeginBlock {
|
|
|
|
return abci.ResponseBeginBlock{}
|
|
|
|
}
|
2017-08-26 02:33:19 -06:00
|
|
|
|
2018-11-26 15:31:11 -05:00
|
|
|
func (app *testApp) EndBlock(req abci.RequestEndBlock) abci.ResponseEndBlock {
|
|
|
|
return abci.ResponseEndBlock{}
|
|
|
|
}
|
|
|
|
|
2019-06-21 01:56:27 -04:00
|
|
|
func (app *testApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx {
|
2019-06-12 14:03:45 +02:00
|
|
|
return abci.ResponseDeliverTx{Events: []abci.Event{}}
|
2018-11-26 15:31:11 -05:00
|
|
|
}
|
|
|
|
|
2019-06-21 01:56:27 -04:00
|
|
|
func (app *testApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
|
2018-11-26 15:31:11 -05:00
|
|
|
return abci.ResponseCheckTx{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *testApp) Commit() abci.ResponseCommit {
|
|
|
|
return abci.ResponseCommit{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *testApp) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery) {
|
|
|
|
return
|
|
|
|
}
|