2015-12-01 20:12:01 -08:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
"context"
|
2015-12-01 20:12:01 -08:00
|
|
|
"fmt"
|
2016-11-23 18:20:46 -05:00
|
|
|
"io/ioutil"
|
2017-01-12 14:44:42 -05:00
|
|
|
"os"
|
2018-12-22 05:58:27 +08:00
|
|
|
"path/filepath"
|
2015-12-01 20:12:01 -08:00
|
|
|
"sort"
|
2016-01-06 17:14:20 -08:00
|
|
|
"sync"
|
2015-12-01 20:12:01 -08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-12-22 05:58:27 +08:00
|
|
|
"github.com/go-kit/kit/log/term"
|
|
|
|
|
2019-05-02 05:15:53 +08:00
|
|
|
"path"
|
|
|
|
|
2018-06-22 06:59:02 +02:00
|
|
|
abcicli "github.com/tendermint/tendermint/abci/client"
|
2018-12-22 05:58:27 +08:00
|
|
|
"github.com/tendermint/tendermint/abci/example/counter"
|
|
|
|
"github.com/tendermint/tendermint/abci/example/kvstore"
|
2018-06-22 06:59:02 +02:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2017-05-02 00:43:49 -04:00
|
|
|
cfg "github.com/tendermint/tendermint/config"
|
2017-10-10 12:39:21 +04:00
|
|
|
cstypes "github.com/tendermint/tendermint/consensus/types"
|
2018-07-27 13:52:42 -04:00
|
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2019-02-23 08:11:27 +04:00
|
|
|
tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
|
2015-12-01 20:12:01 -08:00
|
|
|
mempl "github.com/tendermint/tendermint/mempool"
|
2017-04-08 22:04:06 -04:00
|
|
|
"github.com/tendermint/tendermint/p2p"
|
2018-06-01 19:17:37 +02:00
|
|
|
"github.com/tendermint/tendermint/privval"
|
2015-12-01 20:12:01 -08:00
|
|
|
sm "github.com/tendermint/tendermint/state"
|
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"
|
2015-12-01 20:12:01 -08:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2018-09-01 01:33:51 +02:00
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
2019-07-31 11:34:17 +02:00
|
|
|
dbm "github.com/tendermint/tm-db"
|
2015-12-01 20:12:01 -08:00
|
|
|
)
|
|
|
|
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
const (
|
|
|
|
testSubscriber = "test-client"
|
|
|
|
)
|
|
|
|
|
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()
|
|
|
|
|
2017-05-01 20:09:29 -04:00
|
|
|
// genesis, chain_id, priv_val
|
2018-10-12 22:13:01 +02:00
|
|
|
var config *cfg.Config // NOTE: must be reset for each _test.go file
|
2019-02-18 08:45:27 +01:00
|
|
|
var consensusReplayConfig *cfg.Config
|
2018-10-12 22:13:01 +02:00
|
|
|
var ensureTimeout = time.Millisecond * 100
|
2015-12-10 11:41:18 -05:00
|
|
|
|
2017-01-12 14:44:42 -05:00
|
|
|
func ensureDir(dir string, mode os.FileMode) {
|
2017-10-04 16:40:45 -04:00
|
|
|
if err := cmn.EnsureDir(dir, mode); err != nil {
|
2017-01-12 14:44:42 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 22:33:08 -04:00
|
|
|
func ResetConfig(name string) *cfg.Config {
|
|
|
|
return cfg.ResetTestRoot(name)
|
2017-05-02 00:43:49 -04:00
|
|
|
}
|
|
|
|
|
2017-01-12 12:37:24 -05:00
|
|
|
//-------------------------------------------------------------------------------
|
2018-02-27 14:01:10 +00:00
|
|
|
// validator stub (a kvstore consensus peer we control)
|
2017-01-12 12:37:24 -05:00
|
|
|
|
2015-12-01 20:12:01 -08:00
|
|
|
type validatorStub struct {
|
2016-07-01 17:47:31 -04:00
|
|
|
Index int // Validator index. NOTE: we don't assume validator set changes.
|
2017-12-01 19:04:53 -06:00
|
|
|
Height int64
|
2015-12-01 20:12:01 -08:00
|
|
|
Round int
|
2017-09-18 23:16:14 -04:00
|
|
|
types.PrivValidator
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
|
2017-12-02 01:47:55 -05:00
|
|
|
var testMinPower int64 = 10
|
2016-11-23 18:20:46 -05:00
|
|
|
|
2017-09-18 23:16:14 -04:00
|
|
|
func NewValidatorStub(privValidator types.PrivValidator, valIndex int) *validatorStub {
|
2015-12-01 20:12:01 -08:00
|
|
|
return &validatorStub{
|
2016-07-01 17:47:31 -04:00
|
|
|
Index: valIndex,
|
2015-12-01 20:12:01 -08:00
|
|
|
PrivValidator: privValidator,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 01:21:46 +02:00
|
|
|
func (vs *validatorStub) signVote(voteType types.SignedMsgType, hash []byte, header types.PartSetHeader) (*types.Vote, error) {
|
2018-12-22 06:36:45 +01:00
|
|
|
addr := vs.PrivValidator.GetPubKey().Address()
|
2015-12-01 20:12:01 -08:00
|
|
|
vote := &types.Vote{
|
2016-07-01 17:47:31 -04:00
|
|
|
ValidatorIndex: vs.Index,
|
2018-12-22 06:36:45 +01:00
|
|
|
ValidatorAddress: addr,
|
2015-12-01 20:12:01 -08:00
|
|
|
Height: vs.Height,
|
|
|
|
Round: vs.Round,
|
2018-09-01 01:33:51 +02:00
|
|
|
Timestamp: tmtime.Now(),
|
2015-12-01 20:12:01 -08:00
|
|
|
Type: voteType,
|
2019-06-21 07:58:32 +02:00
|
|
|
BlockID: types.BlockID{Hash: hash, PartsHeader: header},
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
2017-12-10 20:43:58 -05:00
|
|
|
err := vs.PrivValidator.SignVote(config.ChainID(), vote)
|
2015-12-01 20:12:01 -08:00
|
|
|
return vote, err
|
|
|
|
}
|
|
|
|
|
2016-07-01 17:47:31 -04:00
|
|
|
// Sign vote for type/hash/header
|
2018-10-13 01:21:46 +02:00
|
|
|
func signVote(vs *validatorStub, voteType types.SignedMsgType, hash []byte, header types.PartSetHeader) *types.Vote {
|
2015-12-01 20:12:01 -08:00
|
|
|
v, err := vs.signVote(voteType, hash, header)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to sign vote: %v", err))
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2018-10-13 01:21:46 +02:00
|
|
|
func signVotes(voteType types.SignedMsgType, hash []byte, header types.PartSetHeader, vss ...*validatorStub) []*types.Vote {
|
2017-01-12 12:37:24 -05:00
|
|
|
votes := make([]*types.Vote, len(vss))
|
|
|
|
for i, vs := range vss {
|
|
|
|
votes[i] = signVote(vs, voteType, hash, header)
|
|
|
|
}
|
|
|
|
return votes
|
|
|
|
}
|
|
|
|
|
|
|
|
func incrementHeight(vss ...*validatorStub) {
|
|
|
|
for _, vs := range vss {
|
2018-04-02 10:21:17 +02:00
|
|
|
vs.Height++
|
2017-01-12 12:37:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func incrementRound(vss ...*validatorStub) {
|
|
|
|
for _, vs := range vss {
|
2018-04-02 10:21:17 +02:00
|
|
|
vs.Round++
|
2017-01-12 12:37:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 05:15:53 +08:00
|
|
|
type ValidatorStubsByAddress []*validatorStub
|
|
|
|
|
|
|
|
func (vss ValidatorStubsByAddress) Len() int {
|
|
|
|
return len(vss)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vss ValidatorStubsByAddress) Less(i, j int) bool {
|
|
|
|
return bytes.Compare(vss[i].GetPubKey().Address(), vss[j].GetPubKey().Address()) == -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vss ValidatorStubsByAddress) Swap(i, j int) {
|
|
|
|
it := vss[i]
|
|
|
|
vss[i] = vss[j]
|
|
|
|
vss[i].Index = i
|
|
|
|
vss[j] = it
|
|
|
|
vss[j].Index = j
|
|
|
|
}
|
|
|
|
|
2017-01-12 12:37:24 -05:00
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
// Functions for transitioning the consensus state
|
|
|
|
|
2017-12-01 19:04:53 -06:00
|
|
|
func startTestRound(cs *ConsensusState, height int64, round int) {
|
2017-01-12 12:37:24 -05:00
|
|
|
cs.enterNewRound(height, round)
|
|
|
|
cs.startRoutines(0)
|
|
|
|
}
|
|
|
|
|
2019-02-22 11:48:40 +04:00
|
|
|
// Create proposal block from cs1 but sign it with vs.
|
2017-12-01 19:04:53 -06:00
|
|
|
func decideProposal(cs1 *ConsensusState, vs *validatorStub, height int64, round int) (proposal *types.Proposal, block *types.Block) {
|
2019-02-18 08:29:41 +01:00
|
|
|
cs1.mtx.Lock()
|
2015-12-01 20:12:01 -08:00
|
|
|
block, blockParts := cs1.createProposalBlock()
|
2019-02-22 11:48:40 +04:00
|
|
|
validRound := cs1.ValidRound
|
|
|
|
chainID := cs1.state.ChainID
|
2019-02-18 08:29:41 +01:00
|
|
|
cs1.mtx.Unlock()
|
2019-02-22 11:48:40 +04:00
|
|
|
if block == nil {
|
|
|
|
panic("Failed to createProposalBlock. Did you forget to add commit for previous block?")
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make proposal
|
2019-06-21 07:58:32 +02:00
|
|
|
polRound, propBlockID := validRound, types.BlockID{Hash: block.Hash(), PartsHeader: blockParts.Header()}
|
2018-10-31 15:27:11 +01:00
|
|
|
proposal = types.NewProposal(height, round, polRound, propBlockID)
|
2019-02-18 08:29:41 +01:00
|
|
|
if err := vs.SignProposal(chainID, proposal); err != nil {
|
2015-12-01 20:12:01 -08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-01 17:47:31 -04:00
|
|
|
func addVotes(to *ConsensusState, votes ...*types.Vote) {
|
|
|
|
for _, vote := range votes {
|
|
|
|
to.peerMsgQueue <- msgInfo{Msg: &VoteMessage{vote}}
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 01:21:46 +02:00
|
|
|
func signAddVotes(to *ConsensusState, voteType types.SignedMsgType, hash []byte, header types.PartSetHeader, vss ...*validatorStub) {
|
2016-07-01 17:47:31 -04:00
|
|
|
votes := signVotes(voteType, hash, header, vss...)
|
|
|
|
addVotes(to, votes...)
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func validatePrevote(t *testing.T, cs *ConsensusState, round int, privVal *validatorStub, blockHash []byte) {
|
|
|
|
prevotes := cs.Votes.Prevotes(round)
|
2018-12-22 06:36:45 +01:00
|
|
|
address := privVal.GetPubKey().Address()
|
2015-12-01 20:12:01 -08:00
|
|
|
var vote *types.Vote
|
2018-12-22 06:36:45 +01:00
|
|
|
if vote = prevotes.GetByAddress(address); vote == nil {
|
2015-12-01 20:12:01 -08:00
|
|
|
panic("Failed to find prevote from validator")
|
|
|
|
}
|
|
|
|
if blockHash == nil {
|
2016-08-16 14:59:19 -07:00
|
|
|
if vote.BlockID.Hash != nil {
|
|
|
|
panic(fmt.Sprintf("Expected prevote to be for nil, got %X", vote.BlockID.Hash))
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
} else {
|
2016-08-16 14:59:19 -07:00
|
|
|
if !bytes.Equal(vote.BlockID.Hash, blockHash) {
|
|
|
|
panic(fmt.Sprintf("Expected prevote to be for %X, got %X", blockHash, vote.BlockID.Hash))
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 14:56:05 -05:00
|
|
|
func validateLastPrecommit(t *testing.T, cs *ConsensusState, privVal *validatorStub, blockHash []byte) {
|
|
|
|
votes := cs.LastCommit
|
2018-12-22 06:36:45 +01:00
|
|
|
address := privVal.GetPubKey().Address()
|
2015-12-13 14:56:05 -05:00
|
|
|
var vote *types.Vote
|
2018-12-22 06:36:45 +01:00
|
|
|
if vote = votes.GetByAddress(address); vote == nil {
|
2015-12-13 14:56:05 -05:00
|
|
|
panic("Failed to find precommit from validator")
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
2016-08-16 14:59:19 -07:00
|
|
|
if !bytes.Equal(vote.BlockID.Hash, blockHash) {
|
|
|
|
panic(fmt.Sprintf("Expected precommit to be for %X, got %X", blockHash, vote.BlockID.Hash))
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validatePrecommit(t *testing.T, cs *ConsensusState, thisRound, lockRound int, privVal *validatorStub, votedBlockHash, lockedBlockHash []byte) {
|
|
|
|
precommits := cs.Votes.Precommits(thisRound)
|
2018-12-22 06:36:45 +01:00
|
|
|
address := privVal.GetPubKey().Address()
|
2015-12-01 20:12:01 -08:00
|
|
|
var vote *types.Vote
|
2018-12-22 06:36:45 +01:00
|
|
|
if vote = precommits.GetByAddress(address); vote == nil {
|
2015-12-01 20:12:01 -08:00
|
|
|
panic("Failed to find precommit from validator")
|
|
|
|
}
|
|
|
|
|
|
|
|
if votedBlockHash == nil {
|
2016-08-16 14:59:19 -07:00
|
|
|
if vote.BlockID.Hash != nil {
|
2015-12-01 20:12:01 -08:00
|
|
|
panic("Expected precommit to be for nil")
|
|
|
|
}
|
|
|
|
} else {
|
2016-08-16 14:59:19 -07:00
|
|
|
if !bytes.Equal(vote.BlockID.Hash, votedBlockHash) {
|
2015-12-01 20:12:01 -08:00
|
|
|
panic("Expected precommit to be for proposal block")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if lockedBlockHash == nil {
|
|
|
|
if cs.LockedRound != lockRound || cs.LockedBlock != nil {
|
|
|
|
panic(fmt.Sprintf("Expected to be locked on nil at round %d. Got locked at round %d with block %v", lockRound, cs.LockedRound, cs.LockedBlock))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if cs.LockedRound != lockRound || !bytes.Equal(cs.LockedBlock.Hash(), lockedBlockHash) {
|
|
|
|
panic(fmt.Sprintf("Expected block to be locked on round %d, got %d. Got locked block %X, expected %X", lockRound, cs.LockedRound, cs.LockedBlock.Hash(), lockedBlockHash))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func validatePrevoteAndPrecommit(t *testing.T, cs *ConsensusState, thisRound, lockRound int, privVal *validatorStub, votedBlockHash, lockedBlockHash []byte) {
|
|
|
|
// verify the prevote
|
|
|
|
validatePrevote(t, cs, thisRound, privVal, votedBlockHash)
|
|
|
|
// verify precommit
|
|
|
|
cs.mtx.Lock()
|
|
|
|
validatePrecommit(t, cs, thisRound, lockRound, privVal, votedBlockHash, lockedBlockHash)
|
|
|
|
cs.mtx.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func subscribeToVoter(cs *ConsensusState, addr []byte) <-chan tmpubsub.Message {
|
2019-05-02 05:15:53 +08:00
|
|
|
votesSub, err := cs.eventBus.SubscribeUnbuffered(context.Background(), testSubscriber, types.EventQueryVote)
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to subscribe %s to %v", testSubscriber, types.EventQueryVote))
|
|
|
|
}
|
2019-02-23 08:11:27 +04:00
|
|
|
ch := make(chan tmpubsub.Message)
|
2017-01-12 12:37:24 -05:00
|
|
|
go func() {
|
2019-02-23 08:11:27 +04:00
|
|
|
for msg := range votesSub.Out() {
|
|
|
|
vote := msg.Data().(types.EventDataVote)
|
2017-01-12 12:37:24 -05:00
|
|
|
// we only fire for our own votes
|
|
|
|
if bytes.Equal(addr, vote.Vote.ValidatorAddress) {
|
2019-02-23 08:11:27 +04:00
|
|
|
ch <- msg
|
2017-01-12 12:37:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2019-02-23 08:11:27 +04:00
|
|
|
return ch
|
2016-01-18 15:57:57 -05:00
|
|
|
}
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2017-01-12 12:37:24 -05:00
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
// consensus states
|
|
|
|
|
2017-12-27 22:09:48 -05:00
|
|
|
func newConsensusState(state sm.State, pv types.PrivValidator, app abci.Application) *ConsensusState {
|
2019-02-18 08:45:27 +01:00
|
|
|
config := cfg.ResetTestRoot("consensus_state_test")
|
2017-01-12 12:37:24 -05:00
|
|
|
return newConsensusStateWithConfig(config, state, pv, app)
|
2016-10-11 12:51:48 -04:00
|
|
|
}
|
|
|
|
|
2017-12-27 22:09:48 -05:00
|
|
|
func newConsensusStateWithConfig(thisConfig *cfg.Config, state sm.State, pv types.PrivValidator, app abci.Application) *ConsensusState {
|
2015-12-01 20:12:01 -08:00
|
|
|
blockDB := dbm.NewMemDB()
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
return newConsensusStateWithConfigAndBlockStore(thisConfig, state, pv, app, blockDB)
|
|
|
|
}
|
|
|
|
|
2017-12-27 22:09:48 -05:00
|
|
|
func newConsensusStateWithConfigAndBlockStore(thisConfig *cfg.Config, state sm.State, pv types.PrivValidator, app abci.Application, blockDB dbm.DB) *ConsensusState {
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
// Get BlockStore
|
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)
|
2015-12-01 20:12:01 -08:00
|
|
|
|
|
|
|
// one for mempool, one for consensus
|
2016-04-11 18:08:28 -04:00
|
|
|
mtx := new(sync.Mutex)
|
2017-01-12 15:53:32 -05:00
|
|
|
proxyAppConnMem := abcicli.NewLocalClient(mtx, app)
|
|
|
|
proxyAppConnCon := abcicli.NewLocalClient(mtx, app)
|
2015-12-01 20:12:01 -08:00
|
|
|
|
|
|
|
// Make Mempool
|
2019-05-04 10:41:31 +04:00
|
|
|
mempool := mempl.NewCListMempool(thisConfig.Mempool, proxyAppConnMem, 0)
|
2017-05-02 11:53:32 +04:00
|
|
|
mempool.SetLogger(log.TestingLogger().With("module", "mempool"))
|
2017-08-04 21:46:17 -04:00
|
|
|
if thisConfig.Consensus.WaitForTxs() {
|
2017-07-25 13:57:11 -04:00
|
|
|
mempool.EnableTxsAvailable()
|
|
|
|
}
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2017-11-19 02:02:58 +00:00
|
|
|
// mock the evidence pool
|
2018-06-04 13:46:34 -07:00
|
|
|
evpool := sm.MockEvidencePool{}
|
2017-11-19 02:02:58 +00:00
|
|
|
|
2018-05-15 14:32:06 +04:00
|
|
|
// Make ConsensusState
|
2019-05-02 05:15:53 +08:00
|
|
|
stateDB := blockDB
|
|
|
|
sm.SaveState(stateDB, state) //for save height 1's validators info
|
2017-12-28 18:26:13 -05:00
|
|
|
blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyAppConnCon, mempool, evpool)
|
2017-12-27 22:09:48 -05:00
|
|
|
cs := NewConsensusState(thisConfig.Consensus, state, blockExec, blockStore, mempool, evpool)
|
2018-01-24 23:34:57 -05:00
|
|
|
cs.SetLogger(log.TestingLogger().With("module", "consensus"))
|
2016-01-18 15:57:57 -05:00
|
|
|
cs.SetPrivValidator(pv)
|
2015-12-01 20:12:01 -08:00
|
|
|
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
eventBus := types.NewEventBus()
|
|
|
|
eventBus.SetLogger(log.TestingLogger().With("module", "events"))
|
|
|
|
eventBus.Start()
|
|
|
|
cs.SetEventBus(eventBus)
|
2016-01-18 15:57:57 -05:00
|
|
|
return cs
|
|
|
|
}
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2018-06-01 19:17:37 +02:00
|
|
|
func loadPrivValidator(config *cfg.Config) *privval.FilePV {
|
2018-12-22 05:58:27 +08:00
|
|
|
privValidatorKeyFile := config.PrivValidatorKeyFile()
|
|
|
|
ensureDir(filepath.Dir(privValidatorKeyFile), 0700)
|
|
|
|
privValidatorStateFile := config.PrivValidatorStateFile()
|
|
|
|
privValidator := privval.LoadOrGenFilePV(privValidatorKeyFile, privValidatorStateFile)
|
2017-01-12 12:37:24 -05:00
|
|
|
privValidator.Reset()
|
|
|
|
return privValidator
|
|
|
|
}
|
|
|
|
|
2016-01-18 15:57:57 -05:00
|
|
|
func randConsensusState(nValidators int) (*ConsensusState, []*validatorStub) {
|
|
|
|
// Get State
|
|
|
|
state, privVals := randGenesisState(nValidators, false, 10)
|
|
|
|
|
|
|
|
vss := make([]*validatorStub, nValidators)
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2016-04-11 18:08:28 -04:00
|
|
|
cs := newConsensusState(state, privVals[0], counter.NewCounterApplication(true))
|
2015-12-08 16:00:59 -05:00
|
|
|
|
2015-12-01 20:12:01 -08:00
|
|
|
for i := 0; i < nValidators; i++ {
|
2016-07-01 17:47:31 -04:00
|
|
|
vss[i] = NewValidatorStub(privVals[i], i)
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
// since cs1 starts at 1
|
|
|
|
incrementHeight(vss[1:]...)
|
|
|
|
|
|
|
|
return cs, vss
|
|
|
|
}
|
|
|
|
|
2017-01-12 12:37:24 -05:00
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func ensureNoNewEvent(ch <-chan tmpubsub.Message, timeout time.Duration,
|
2018-10-04 15:37:13 +02:00
|
|
|
errorMessage string) {
|
|
|
|
select {
|
|
|
|
case <-time.After(timeout):
|
|
|
|
break
|
|
|
|
case <-ch:
|
|
|
|
panic(errorMessage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func ensureNoNewEventOnChannel(ch <-chan tmpubsub.Message) {
|
2018-10-12 22:13:01 +02:00
|
|
|
ensureNoNewEvent(
|
|
|
|
ch,
|
|
|
|
ensureTimeout,
|
|
|
|
"We should be stuck waiting, not receiving new event on the channel")
|
|
|
|
}
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func ensureNoNewRoundStep(stepCh <-chan tmpubsub.Message) {
|
2018-10-12 22:13:01 +02:00
|
|
|
ensureNoNewEvent(
|
|
|
|
stepCh,
|
|
|
|
ensureTimeout,
|
|
|
|
"We should be stuck waiting, not receiving NewRoundStep event")
|
|
|
|
}
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func ensureNoNewUnlock(unlockCh <-chan tmpubsub.Message) {
|
2018-10-12 22:13:01 +02:00
|
|
|
ensureNoNewEvent(
|
|
|
|
unlockCh,
|
|
|
|
ensureTimeout,
|
|
|
|
"We should be stuck waiting, not receiving Unlock event")
|
2018-10-04 15:37:13 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func ensureNoNewTimeout(stepCh <-chan tmpubsub.Message, timeout int64) {
|
2019-04-23 11:19:16 +02:00
|
|
|
timeoutDuration := time.Duration(timeout*10) * time.Nanosecond
|
2018-10-12 22:13:01 +02:00
|
|
|
ensureNoNewEvent(
|
|
|
|
stepCh,
|
|
|
|
timeoutDuration,
|
|
|
|
"We should be stuck waiting, not receiving NewTimeout event")
|
2018-10-04 15:37:13 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func ensureNewEvent(ch <-chan tmpubsub.Message, height int64, round int, timeout time.Duration, errorMessage string) {
|
2017-01-12 12:37:24 -05:00
|
|
|
select {
|
2018-10-04 15:37:13 +02:00
|
|
|
case <-time.After(timeout):
|
|
|
|
panic(errorMessage)
|
2019-02-23 08:11:27 +04:00
|
|
|
case msg := <-ch:
|
|
|
|
roundStateEvent, ok := msg.Data().(types.EventDataRoundState)
|
2018-10-12 22:13:01 +02:00
|
|
|
if !ok {
|
2019-02-23 08:11:27 +04:00
|
|
|
panic(fmt.Sprintf("expected a EventDataRoundState, got %T. Wrong subscription channel?",
|
|
|
|
msg.Data()))
|
2018-10-12 22:13:01 +02:00
|
|
|
}
|
2019-02-23 08:11:27 +04:00
|
|
|
if roundStateEvent.Height != height {
|
|
|
|
panic(fmt.Sprintf("expected height %v, got %v", height, roundStateEvent.Height))
|
2018-10-12 22:13:01 +02:00
|
|
|
}
|
2019-02-23 08:11:27 +04:00
|
|
|
if roundStateEvent.Round != round {
|
|
|
|
panic(fmt.Sprintf("expected round %v, got %v", round, roundStateEvent.Round))
|
2018-10-12 22:13:01 +02:00
|
|
|
}
|
|
|
|
// TODO: We could check also for a step at this point!
|
2017-07-13 15:03:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func ensureNewRound(roundCh <-chan tmpubsub.Message, height int64, round int) {
|
2018-11-15 18:40:42 -05:00
|
|
|
select {
|
|
|
|
case <-time.After(ensureTimeout):
|
|
|
|
panic("Timeout expired while waiting for NewRound event")
|
2019-02-23 08:11:27 +04:00
|
|
|
case msg := <-roundCh:
|
|
|
|
newRoundEvent, ok := msg.Data().(types.EventDataNewRound)
|
2018-11-15 18:40:42 -05:00
|
|
|
if !ok {
|
2019-02-23 08:11:27 +04:00
|
|
|
panic(fmt.Sprintf("expected a EventDataNewRound, got %T. Wrong subscription channel?",
|
|
|
|
msg.Data()))
|
2018-11-15 18:40:42 -05:00
|
|
|
}
|
2019-02-23 08:11:27 +04:00
|
|
|
if newRoundEvent.Height != height {
|
|
|
|
panic(fmt.Sprintf("expected height %v, got %v", height, newRoundEvent.Height))
|
2018-11-15 18:40:42 -05:00
|
|
|
}
|
2019-02-23 08:11:27 +04:00
|
|
|
if newRoundEvent.Round != round {
|
|
|
|
panic(fmt.Sprintf("expected round %v, got %v", round, newRoundEvent.Round))
|
2018-11-15 18:40:42 -05:00
|
|
|
}
|
|
|
|
}
|
2018-10-04 15:37:13 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func ensureNewTimeout(timeoutCh <-chan tmpubsub.Message, height int64, round int, timeout int64) {
|
2019-04-23 11:19:16 +02:00
|
|
|
timeoutDuration := time.Duration(timeout*10) * time.Nanosecond
|
2018-10-12 22:13:01 +02:00
|
|
|
ensureNewEvent(timeoutCh, height, round, timeoutDuration,
|
2018-10-04 15:37:13 +02:00
|
|
|
"Timeout expired while waiting for NewTimeout event")
|
|
|
|
}
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func ensureNewProposal(proposalCh <-chan tmpubsub.Message, height int64, round int) {
|
2018-11-15 18:40:42 -05:00
|
|
|
select {
|
|
|
|
case <-time.After(ensureTimeout):
|
|
|
|
panic("Timeout expired while waiting for NewProposal event")
|
2019-02-23 08:11:27 +04:00
|
|
|
case msg := <-proposalCh:
|
|
|
|
proposalEvent, ok := msg.Data().(types.EventDataCompleteProposal)
|
2018-11-15 18:40:42 -05:00
|
|
|
if !ok {
|
2019-02-23 08:11:27 +04:00
|
|
|
panic(fmt.Sprintf("expected a EventDataCompleteProposal, got %T. Wrong subscription channel?",
|
|
|
|
msg.Data()))
|
2018-11-15 18:40:42 -05:00
|
|
|
}
|
2019-02-23 08:11:27 +04:00
|
|
|
if proposalEvent.Height != height {
|
|
|
|
panic(fmt.Sprintf("expected height %v, got %v", height, proposalEvent.Height))
|
2018-11-15 18:40:42 -05:00
|
|
|
}
|
2019-02-23 08:11:27 +04:00
|
|
|
if proposalEvent.Round != round {
|
|
|
|
panic(fmt.Sprintf("expected round %v, got %v", round, proposalEvent.Round))
|
2018-11-15 18:40:42 -05:00
|
|
|
}
|
|
|
|
}
|
2018-10-04 15:37:13 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func ensureNewValidBlock(validBlockCh <-chan tmpubsub.Message, height int64, round int) {
|
2018-10-31 14:20:36 +01:00
|
|
|
ensureNewEvent(validBlockCh, height, round, ensureTimeout,
|
|
|
|
"Timeout expired while waiting for NewValidBlock event")
|
|
|
|
}
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func ensureNewBlock(blockCh <-chan tmpubsub.Message, height int64) {
|
2018-10-12 22:13:01 +02:00
|
|
|
select {
|
|
|
|
case <-time.After(ensureTimeout):
|
|
|
|
panic("Timeout expired while waiting for NewBlock event")
|
2019-02-23 08:11:27 +04:00
|
|
|
case msg := <-blockCh:
|
|
|
|
blockEvent, ok := msg.Data().(types.EventDataNewBlock)
|
2018-10-12 22:13:01 +02:00
|
|
|
if !ok {
|
2019-02-23 08:11:27 +04:00
|
|
|
panic(fmt.Sprintf("expected a EventDataNewBlock, got %T. Wrong subscription channel?",
|
|
|
|
msg.Data()))
|
2018-10-12 22:13:01 +02:00
|
|
|
}
|
2019-02-23 08:11:27 +04:00
|
|
|
if blockEvent.Block.Height != height {
|
|
|
|
panic(fmt.Sprintf("expected height %v, got %v", height, blockEvent.Block.Height))
|
2018-10-12 22:13:01 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-04 15:37:13 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func ensureNewBlockHeader(blockCh <-chan tmpubsub.Message, height int64, blockHash cmn.HexBytes) {
|
2018-10-12 22:13:01 +02:00
|
|
|
select {
|
|
|
|
case <-time.After(ensureTimeout):
|
|
|
|
panic("Timeout expired while waiting for NewBlockHeader event")
|
2019-02-23 08:11:27 +04:00
|
|
|
case msg := <-blockCh:
|
|
|
|
blockHeaderEvent, ok := msg.Data().(types.EventDataNewBlockHeader)
|
2018-10-12 22:13:01 +02:00
|
|
|
if !ok {
|
2019-02-23 08:11:27 +04:00
|
|
|
panic(fmt.Sprintf("expected a EventDataNewBlockHeader, got %T. Wrong subscription channel?",
|
|
|
|
msg.Data()))
|
2018-10-12 22:13:01 +02:00
|
|
|
}
|
2019-02-23 08:11:27 +04:00
|
|
|
if blockHeaderEvent.Header.Height != height {
|
|
|
|
panic(fmt.Sprintf("expected height %v, got %v", height, blockHeaderEvent.Header.Height))
|
2018-10-12 22:13:01 +02:00
|
|
|
}
|
2019-02-23 08:11:27 +04:00
|
|
|
if !bytes.Equal(blockHeaderEvent.Header.Hash(), blockHash) {
|
|
|
|
panic(fmt.Sprintf("expected header %X, got %X", blockHash, blockHeaderEvent.Header.Hash()))
|
2018-10-12 22:13:01 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-04 15:37:13 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func ensureNewUnlock(unlockCh <-chan tmpubsub.Message, height int64, round int) {
|
2018-10-12 22:13:01 +02:00
|
|
|
ensureNewEvent(unlockCh, height, round, ensureTimeout,
|
2018-10-04 15:37:13 +02:00
|
|
|
"Timeout expired while waiting for NewUnlock event")
|
|
|
|
}
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func ensureProposal(proposalCh <-chan tmpubsub.Message, height int64, round int, propID types.BlockID) {
|
2017-07-13 15:03:19 -04:00
|
|
|
select {
|
2018-10-04 15:37:13 +02:00
|
|
|
case <-time.After(ensureTimeout):
|
2019-02-23 08:11:27 +04:00
|
|
|
panic("Timeout expired while waiting for NewProposal event")
|
|
|
|
case msg := <-proposalCh:
|
|
|
|
proposalEvent, ok := msg.Data().(types.EventDataCompleteProposal)
|
2018-10-04 15:37:13 +02:00
|
|
|
if !ok {
|
2019-02-23 08:11:27 +04:00
|
|
|
panic(fmt.Sprintf("expected a EventDataCompleteProposal, got %T. Wrong subscription channel?",
|
|
|
|
msg.Data()))
|
2018-10-04 15:37:13 +02:00
|
|
|
}
|
2019-02-23 08:11:27 +04:00
|
|
|
if proposalEvent.Height != height {
|
|
|
|
panic(fmt.Sprintf("expected height %v, got %v", height, proposalEvent.Height))
|
2018-10-04 15:37:13 +02:00
|
|
|
}
|
2019-02-23 08:11:27 +04:00
|
|
|
if proposalEvent.Round != round {
|
|
|
|
panic(fmt.Sprintf("expected round %v, got %v", round, proposalEvent.Round))
|
2018-10-04 15:37:13 +02:00
|
|
|
}
|
2019-02-23 08:11:27 +04:00
|
|
|
if !proposalEvent.BlockID.Equals(propID) {
|
|
|
|
panic("Proposed block does not match expected block")
|
2018-10-04 15:37:13 +02:00
|
|
|
}
|
2017-01-12 12:37:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func ensurePrecommit(voteCh <-chan tmpubsub.Message, height int64, round int) {
|
|
|
|
ensureVote(voteCh, height, round, types.PrecommitType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ensurePrevote(voteCh <-chan tmpubsub.Message, height int64, round int) {
|
|
|
|
ensureVote(voteCh, height, round, types.PrevoteType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ensureVote(voteCh <-chan tmpubsub.Message, height int64, round int,
|
|
|
|
voteType types.SignedMsgType) {
|
2018-11-15 18:40:42 -05:00
|
|
|
select {
|
|
|
|
case <-time.After(ensureTimeout):
|
2019-02-23 08:11:27 +04:00
|
|
|
panic("Timeout expired while waiting for NewVote event")
|
|
|
|
case msg := <-voteCh:
|
|
|
|
voteEvent, ok := msg.Data().(types.EventDataVote)
|
2018-11-15 18:40:42 -05:00
|
|
|
if !ok {
|
2019-02-23 08:11:27 +04:00
|
|
|
panic(fmt.Sprintf("expected a EventDataVote, got %T. Wrong subscription channel?",
|
|
|
|
msg.Data()))
|
2018-11-15 18:40:42 -05:00
|
|
|
}
|
2019-02-23 08:11:27 +04:00
|
|
|
vote := voteEvent.Vote
|
|
|
|
if vote.Height != height {
|
|
|
|
panic(fmt.Sprintf("expected height %v, got %v", height, vote.Height))
|
2018-11-15 18:40:42 -05:00
|
|
|
}
|
2019-02-23 08:11:27 +04:00
|
|
|
if vote.Round != round {
|
|
|
|
panic(fmt.Sprintf("expected round %v, got %v", round, vote.Round))
|
2018-11-15 18:40:42 -05:00
|
|
|
}
|
2019-02-23 08:11:27 +04:00
|
|
|
if vote.Type != voteType {
|
|
|
|
panic(fmt.Sprintf("expected type %v, got %v", voteType, vote.Type))
|
2018-11-15 18:40:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 08:11:27 +04:00
|
|
|
func ensureNewEventOnChannel(ch <-chan tmpubsub.Message) {
|
2018-10-12 22:13:01 +02:00
|
|
|
select {
|
|
|
|
case <-time.After(ensureTimeout):
|
|
|
|
panic("Timeout expired while waiting for new activity on the channel")
|
|
|
|
case <-ch:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 12:37:24 -05:00
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
// consensus nets
|
|
|
|
|
2017-05-14 21:44:01 +02:00
|
|
|
// consensusLogger is a TestingLogger which uses a different
|
|
|
|
// color for each validator ("validator" key must exist).
|
|
|
|
func consensusLogger() log.Logger {
|
|
|
|
return log.TestingLoggerWithColorFn(func(keyvals ...interface{}) term.FgBgColor {
|
|
|
|
for i := 0; i < len(keyvals)-1; i += 2 {
|
|
|
|
if keyvals[i] == "validator" {
|
|
|
|
return term.FgBgColor{Fg: term.Color(uint8(keyvals[i+1].(int) + 1))}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return term.FgBgColor{}
|
2018-01-24 23:34:57 -05:00
|
|
|
}).With("module", "consensus")
|
2017-05-14 21:44:01 +02:00
|
|
|
}
|
|
|
|
|
2019-02-18 08:45:27 +01:00
|
|
|
func randConsensusNet(nValidators int, testName string, tickerFunc func() TimeoutTicker,
|
|
|
|
appFunc func() abci.Application, configOpts ...func(*cfg.Config)) ([]*ConsensusState, cleanupFunc) {
|
2017-12-25 14:07:45 -06:00
|
|
|
genDoc, privVals := randGenesisDoc(nValidators, false, 30)
|
2016-06-26 00:40:53 -04:00
|
|
|
css := make([]*ConsensusState, nValidators)
|
2017-05-14 21:44:01 +02:00
|
|
|
logger := consensusLogger()
|
2019-02-18 08:45:27 +01:00
|
|
|
configRootDirs := make([]string, 0, nValidators)
|
2016-06-26 00:40:53 -04:00
|
|
|
for i := 0; i < nValidators; i++ {
|
2017-12-27 22:09:48 -05:00
|
|
|
stateDB := dbm.NewMemDB() // each state needs its own db
|
2017-12-28 18:26:13 -05:00
|
|
|
state, _ := sm.LoadStateFromDBOrGenesisDoc(stateDB, genDoc)
|
2018-08-10 00:25:57 -05:00
|
|
|
thisConfig := ResetConfig(fmt.Sprintf("%s_%d", testName, i))
|
2019-02-18 08:45:27 +01:00
|
|
|
configRootDirs = append(configRootDirs, thisConfig.RootDir)
|
2017-08-10 01:09:04 -04:00
|
|
|
for _, opt := range configOpts {
|
|
|
|
opt(thisConfig)
|
|
|
|
}
|
2018-12-22 05:58:27 +08:00
|
|
|
ensureDir(filepath.Dir(thisConfig.Consensus.WalFile()), 0700) // dir for wal
|
2017-10-23 10:03:54 -04:00
|
|
|
app := appFunc()
|
2018-08-06 00:18:24 -04:00
|
|
|
vals := types.TM2PB.ValidatorUpdates(state.Validators)
|
2017-10-23 10:03:54 -04:00
|
|
|
app.InitChain(abci.RequestInitChain{Validators: vals})
|
|
|
|
|
2019-05-02 05:15:53 +08:00
|
|
|
css[i] = newConsensusStateWithConfigAndBlockStore(thisConfig, state, privVals[i], app, stateDB)
|
2016-12-19 10:44:25 -05:00
|
|
|
css[i].SetTimeoutTicker(tickerFunc())
|
2018-01-24 23:34:57 -05:00
|
|
|
css[i].SetLogger(logger.With("validator", i, "module", "consensus"))
|
2016-06-26 00:40:53 -04:00
|
|
|
}
|
2019-02-18 08:45:27 +01:00
|
|
|
return css, func() {
|
|
|
|
for _, dir := range configRootDirs {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}
|
|
|
|
}
|
2016-06-26 00:40:53 -04:00
|
|
|
}
|
|
|
|
|
2016-11-23 18:20:46 -05:00
|
|
|
// nPeers = nValidators + nNotValidator
|
2019-05-02 05:15:53 +08:00
|
|
|
func randConsensusNetWithPeers(nValidators, nPeers int, testName string, tickerFunc func() TimeoutTicker, appFunc func(string) abci.Application) ([]*ConsensusState, *types.GenesisDoc, *cfg.Config, cleanupFunc) {
|
2017-12-02 01:47:55 -05:00
|
|
|
genDoc, privVals := randGenesisDoc(nValidators, false, testMinPower)
|
2016-11-23 18:20:46 -05:00
|
|
|
css := make([]*ConsensusState, nPeers)
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
logger := consensusLogger()
|
2019-05-02 05:15:53 +08:00
|
|
|
var peer0Config *cfg.Config
|
2019-02-18 08:45:27 +01:00
|
|
|
configRootDirs := make([]string, 0, nPeers)
|
2016-11-23 18:20:46 -05:00
|
|
|
for i := 0; i < nPeers; i++ {
|
2017-12-27 22:09:48 -05:00
|
|
|
stateDB := dbm.NewMemDB() // each state needs its own db
|
2017-12-28 18:26:13 -05:00
|
|
|
state, _ := sm.LoadStateFromDBOrGenesisDoc(stateDB, genDoc)
|
2018-08-10 00:25:57 -05:00
|
|
|
thisConfig := ResetConfig(fmt.Sprintf("%s_%d", testName, i))
|
2019-02-18 08:45:27 +01:00
|
|
|
configRootDirs = append(configRootDirs, thisConfig.RootDir)
|
2018-12-22 05:58:27 +08:00
|
|
|
ensureDir(filepath.Dir(thisConfig.Consensus.WalFile()), 0700) // dir for wal
|
2019-05-02 05:15:53 +08:00
|
|
|
if i == 0 {
|
|
|
|
peer0Config = thisConfig
|
|
|
|
}
|
2017-09-18 23:16:14 -04:00
|
|
|
var privVal types.PrivValidator
|
2016-11-23 18:20:46 -05:00
|
|
|
if i < nValidators {
|
|
|
|
privVal = privVals[i]
|
|
|
|
} else {
|
2018-12-22 05:58:27 +08:00
|
|
|
tempKeyFile, err := ioutil.TempFile("", "priv_validator_key_")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
tempStateFile, err := ioutil.TempFile("", "priv_validator_state_")
|
2018-07-30 16:19:33 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-22 05:58:27 +08:00
|
|
|
|
|
|
|
privVal = privval.GenFilePV(tempKeyFile.Name(), tempStateFile.Name())
|
2016-11-23 18:20:46 -05:00
|
|
|
}
|
|
|
|
|
2019-05-02 05:15:53 +08:00
|
|
|
app := appFunc(path.Join(config.DBDir(), fmt.Sprintf("%s_%d", testName, i)))
|
2018-08-06 00:18:24 -04:00
|
|
|
vals := types.TM2PB.ValidatorUpdates(state.Validators)
|
2019-05-02 05:15:53 +08:00
|
|
|
if _, ok := app.(*kvstore.PersistentKVStoreApplication); ok {
|
|
|
|
state.Version.Consensus.App = kvstore.ProtocolVersion //simulate handshake, receive app version. If don't do this, replay test will fail
|
|
|
|
}
|
2017-10-23 10:03:54 -04:00
|
|
|
app.InitChain(abci.RequestInitChain{Validators: vals})
|
2019-05-02 05:15:53 +08:00
|
|
|
//sm.SaveState(stateDB,state) //height 1's validatorsInfo already saved in LoadStateFromDBOrGenesisDoc above
|
2017-10-23 10:03:54 -04:00
|
|
|
|
|
|
|
css[i] = newConsensusStateWithConfig(thisConfig, state, privVal, app)
|
2016-12-19 10:44:25 -05:00
|
|
|
css[i].SetTimeoutTicker(tickerFunc())
|
2018-01-24 23:34:57 -05:00
|
|
|
css[i].SetLogger(logger.With("validator", i, "module", "consensus"))
|
2016-11-23 18:20:46 -05:00
|
|
|
}
|
2019-05-02 05:15:53 +08:00
|
|
|
return css, genDoc, peer0Config, func() {
|
2019-02-18 08:45:27 +01:00
|
|
|
for _, dir := range configRootDirs {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}
|
|
|
|
}
|
2016-11-23 18:20:46 -05:00
|
|
|
}
|
|
|
|
|
2017-09-12 20:49:22 -04:00
|
|
|
func getSwitchIndex(switches []*p2p.Switch, peer p2p.Peer) int {
|
2017-01-12 12:37:24 -05:00
|
|
|
for i, s := range switches {
|
2018-10-12 19:25:33 -04:00
|
|
|
if peer.NodeInfo().ID() == s.NodeInfo().ID() {
|
2017-01-12 12:37:24 -05:00
|
|
|
return i
|
2016-07-11 20:40:48 -04:00
|
|
|
}
|
2017-01-12 12:37:24 -05:00
|
|
|
}
|
|
|
|
panic("didnt find peer in switches")
|
2016-07-11 20:40:48 -04:00
|
|
|
}
|
|
|
|
|
2017-01-12 12:37:24 -05:00
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
// genesis
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2018-04-03 07:03:08 -07:00
|
|
|
func randGenesisDoc(numValidators int, randPower bool, minPower int64) (*types.GenesisDoc, []types.PrivValidator) {
|
2015-12-01 20:12:01 -08:00
|
|
|
validators := make([]types.GenesisValidator, numValidators)
|
2018-04-03 07:03:08 -07:00
|
|
|
privValidators := make([]types.PrivValidator, numValidators)
|
2015-12-01 20:12:01 -08:00
|
|
|
for i := 0; i < numValidators; i++ {
|
|
|
|
val, privVal := types.RandValidator(randPower, minPower)
|
|
|
|
validators[i] = types.GenesisValidator{
|
|
|
|
PubKey: val.PubKey,
|
2017-09-21 14:37:34 -04:00
|
|
|
Power: val.VotingPower,
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
privValidators[i] = privVal
|
|
|
|
}
|
|
|
|
sort.Sort(types.PrivValidatorsByAddress(privValidators))
|
2017-12-10 20:43:58 -05:00
|
|
|
|
2015-12-01 20:12:01 -08:00
|
|
|
return &types.GenesisDoc{
|
2018-09-01 01:33:51 +02:00
|
|
|
GenesisTime: tmtime.Now(),
|
2017-12-10 20:43:58 -05:00
|
|
|
ChainID: config.ChainID(),
|
2015-12-01 20:12:01 -08:00
|
|
|
Validators: validators,
|
|
|
|
}, privValidators
|
2015-12-12 01:28:53 -05:00
|
|
|
}
|
2016-11-23 18:20:46 -05:00
|
|
|
|
2018-04-03 07:03:08 -07:00
|
|
|
func randGenesisState(numValidators int, randPower bool, minPower int64) (sm.State, []types.PrivValidator) {
|
2017-01-12 12:37:24 -05:00
|
|
|
genDoc, privValidators := randGenesisDoc(numValidators, randPower, minPower)
|
2017-12-27 22:09:48 -05:00
|
|
|
s0, _ := sm.MakeGenesisState(genDoc)
|
2017-01-12 12:37:24 -05:00
|
|
|
return s0, privValidators
|
2016-11-23 18:20:46 -05:00
|
|
|
}
|
2016-12-06 19:54:10 -05:00
|
|
|
|
2016-12-19 10:44:25 -05:00
|
|
|
//------------------------------------
|
2017-01-12 12:37:24 -05:00
|
|
|
// mock ticker
|
2016-12-19 10:44:25 -05:00
|
|
|
|
|
|
|
func newMockTickerFunc(onlyOnce bool) func() TimeoutTicker {
|
|
|
|
return func() TimeoutTicker {
|
|
|
|
return &mockTicker{
|
2016-12-19 22:29:32 -05:00
|
|
|
c: make(chan timeoutInfo, 10),
|
2016-12-19 10:44:25 -05:00
|
|
|
onlyOnce: onlyOnce,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 21:51:58 -05:00
|
|
|
// mock ticker only fires on RoundStepNewHeight
|
2016-12-19 10:44:25 -05:00
|
|
|
// and only once if onlyOnce=true
|
|
|
|
type mockTicker struct {
|
2016-12-19 22:29:32 -05:00
|
|
|
c chan timeoutInfo
|
2016-12-19 10:44:25 -05:00
|
|
|
|
2016-12-19 22:29:32 -05:00
|
|
|
mtx sync.Mutex
|
2016-12-19 10:44:25 -05:00
|
|
|
onlyOnce bool
|
|
|
|
fired bool
|
|
|
|
}
|
|
|
|
|
2017-11-06 13:20:39 -05:00
|
|
|
func (m *mockTicker) Start() error {
|
|
|
|
return nil
|
2016-12-19 10:44:25 -05:00
|
|
|
}
|
|
|
|
|
2017-11-06 13:20:39 -05:00
|
|
|
func (m *mockTicker) Stop() error {
|
|
|
|
return nil
|
2016-12-19 22:29:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTicker) ScheduleTimeout(ti timeoutInfo) {
|
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
2016-12-19 10:44:25 -05:00
|
|
|
if m.onlyOnce && m.fired {
|
|
|
|
return
|
|
|
|
}
|
2017-10-10 12:39:21 +04:00
|
|
|
if ti.Step == cstypes.RoundStepNewHeight {
|
2016-12-19 22:29:32 -05:00
|
|
|
m.c <- ti
|
2016-12-19 10:44:25 -05:00
|
|
|
m.fired = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-19 22:29:32 -05:00
|
|
|
func (m *mockTicker) Chan() <-chan timeoutInfo {
|
2016-12-19 10:44:25 -05:00
|
|
|
return m.c
|
|
|
|
}
|
|
|
|
|
2019-02-11 16:31:34 +04:00
|
|
|
func (*mockTicker) SetLogger(log.Logger) {}
|
2017-05-12 23:07:53 +02:00
|
|
|
|
2016-12-19 10:44:25 -05:00
|
|
|
//------------------------------------
|
2017-01-11 15:32:03 -05:00
|
|
|
|
2017-01-12 15:53:32 -05:00
|
|
|
func newCounter() abci.Application {
|
2017-01-11 15:32:03 -05:00
|
|
|
return counter.NewCounterApplication(true)
|
|
|
|
}
|
|
|
|
|
2018-02-27 14:01:10 +00:00
|
|
|
func newPersistentKVStore() abci.Application {
|
2019-02-18 08:45:27 +01:00
|
|
|
dir, err := ioutil.TempDir("", "persistent-kvstore")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-02-27 14:01:10 +00:00
|
|
|
return kvstore.NewPersistentKVStoreApplication(dir)
|
2017-01-11 15:32:03 -05:00
|
|
|
}
|
2019-05-02 05:15:53 +08:00
|
|
|
|
|
|
|
func newPersistentKVStoreWithPath(dbDir string) abci.Application {
|
|
|
|
return kvstore.NewPersistentKVStoreApplication(dbDir)
|
|
|
|
}
|