tendermint/consensus/common_test.go

451 lines
13 KiB
Go
Raw Normal View History

2015-12-01 20:12:01 -08:00
package consensus
import (
"bytes"
"fmt"
2016-11-23 15:20:46 -08:00
"io/ioutil"
2017-01-12 11:44:42 -08:00
"os"
"path"
2015-12-01 20:12:01 -08:00
"sort"
"sync"
2015-12-01 20:12:01 -08:00
"testing"
"time"
. "github.com/tendermint/go-common"
2016-05-08 15:00:58 -07:00
cfg "github.com/tendermint/go-config"
2015-12-01 20:12:01 -08:00
dbm "github.com/tendermint/go-db"
2016-11-23 15:20:46 -08:00
"github.com/tendermint/go-p2p"
2015-12-01 20:12:01 -08:00
bc "github.com/tendermint/tendermint/blockchain"
2016-09-13 19:25:11 -07:00
"github.com/tendermint/tendermint/config/tendermint_test"
2015-12-01 20:12:01 -08:00
mempl "github.com/tendermint/tendermint/mempool"
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"
2017-01-12 12:53:32 -08:00
abcicli "github.com/tendermint/abci/client"
abci "github.com/tendermint/abci/types"
2015-12-01 20:12:01 -08:00
2017-01-12 12:53:32 -08:00
"github.com/tendermint/abci/example/counter"
"github.com/tendermint/abci/example/dummy"
2015-12-01 20:12:01 -08:00
)
2016-05-08 15:00:58 -07:00
var config cfg.Config // NOTE: must be reset for each _test.go file
var ensureTimeout = time.Duration(2)
2017-01-12 11:44:42 -08:00
func ensureDir(dir string, mode os.FileMode) {
if err := EnsureDir(dir, mode); err != nil {
panic(err)
}
}
//-------------------------------------------------------------------------------
// validator stub (a dummy consensus peer we control)
2015-12-01 20:12:01 -08:00
type validatorStub struct {
Index int // Validator index. NOTE: we don't assume validator set changes.
2015-12-01 20:12:01 -08:00
Height int
Round int
*types.PrivValidator
}
2016-11-23 15:20:46 -08:00
var testMinPower = 10
func NewValidatorStub(privValidator *types.PrivValidator, valIndex int) *validatorStub {
2015-12-01 20:12:01 -08:00
return &validatorStub{
Index: valIndex,
2015-12-01 20:12:01 -08:00
PrivValidator: privValidator,
}
}
func (vs *validatorStub) signVote(voteType byte, hash []byte, header types.PartSetHeader) (*types.Vote, error) {
vote := &types.Vote{
ValidatorIndex: vs.Index,
ValidatorAddress: vs.PrivValidator.Address,
2015-12-01 20:12:01 -08:00
Height: vs.Height,
Round: vs.Round,
Type: voteType,
2016-08-16 14:59:19 -07:00
BlockID: types.BlockID{hash, header},
2015-12-01 20:12:01 -08:00
}
2016-05-08 15:00:58 -07:00
err := vs.PrivValidator.SignVote(config.GetString("chain_id"), vote)
2015-12-01 20:12:01 -08:00
return vote, err
}
// Sign vote for type/hash/header
2015-12-01 20:12:01 -08:00
func signVote(vs *validatorStub, voteType byte, hash []byte, header types.PartSetHeader) *types.Vote {
v, err := vs.signVote(voteType, hash, header)
if err != nil {
panic(fmt.Errorf("failed to sign vote: %v", err))
}
return v
}
func signVotes(voteType byte, hash []byte, header types.PartSetHeader, vss ...*validatorStub) []*types.Vote {
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 {
vs.Height += 1
}
}
func incrementRound(vss ...*validatorStub) {
for _, vs := range vss {
vs.Round += 1
}
}
//-------------------------------------------------------------------------------
// Functions for transitioning the consensus state
func startTestRound(cs *ConsensusState, height, round int) {
cs.enterNewRound(height, round)
cs.startRoutines(0)
}
// Create proposal block from cs1 but sign it with vs
func decideProposal(cs1 *ConsensusState, vs *validatorStub, height, round int) (proposal *types.Proposal, block *types.Block) {
2015-12-01 20:12:01 -08:00
block, blockParts := cs1.createProposalBlock()
if block == nil { // on error
panic("error creating proposal block")
}
// Make proposal
polRound, polBlockID := cs1.Votes.POLInfo()
proposal = types.NewProposal(height, round, blockParts.Header(), polRound, polBlockID)
if err := vs.SignProposal(config.GetString("chain_id"), proposal); err != nil {
2015-12-01 20:12:01 -08:00
panic(err)
}
return
}
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
}
}
func signAddVotes(to *ConsensusState, voteType byte, hash []byte, header types.PartSetHeader, vss ...*validatorStub) {
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)
var vote *types.Vote
if vote = prevotes.GetByAddress(privVal.Address); vote == nil {
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 11:56:05 -08:00
func validateLastPrecommit(t *testing.T, cs *ConsensusState, privVal *validatorStub, blockHash []byte) {
votes := cs.LastCommit
var vote *types.Vote
if vote = votes.GetByAddress(privVal.Address); vote == nil {
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)
var vote *types.Vote
if vote = precommits.GetByAddress(privVal.Address); vote == nil {
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()
}
// genesis
func subscribeToVoter(cs *ConsensusState, addr []byte) chan interface{} {
voteCh0 := subscribeToEvent(cs.evsw, "tester", types.EventStringVote(), 1)
voteCh := make(chan interface{})
go func() {
for {
v := <-voteCh0
vote := v.(types.EventDataVote)
// we only fire for our own votes
if bytes.Equal(addr, vote.Vote.ValidatorAddress) {
voteCh <- v
}
}
}()
return voteCh
2016-01-18 12:57:57 -08:00
}
2015-12-01 20:12:01 -08:00
func readVotes(ch chan interface{}, reads int) chan struct{} {
wg := make(chan struct{})
go func() {
for i := 0; i < reads; i++ {
<-ch // read the precommit event
}
close(wg)
}()
return wg
}
//-------------------------------------------------------------------------------
// consensus states
2017-01-12 12:53:32 -08:00
func newConsensusState(state *sm.State, pv *types.PrivValidator, app abci.Application) *ConsensusState {
return newConsensusStateWithConfig(config, state, pv, app)
2016-10-11 09:51:48 -07:00
}
2017-01-12 12:53:32 -08:00
func newConsensusStateWithConfig(thisConfig cfg.Config, state *sm.State, pv *types.PrivValidator, app abci.Application) *ConsensusState {
2015-12-01 20:12:01 -08:00
// Get BlockStore
blockDB := dbm.NewMemDB()
blockStore := bc.NewBlockStore(blockDB)
// one for mempool, one for consensus
mtx := new(sync.Mutex)
2017-01-12 12:53:32 -08:00
proxyAppConnMem := abcicli.NewLocalClient(mtx, app)
proxyAppConnCon := abcicli.NewLocalClient(mtx, app)
2015-12-01 20:12:01 -08:00
// Make Mempool
2016-09-13 19:25:11 -07:00
mempool := mempl.NewMempool(thisConfig, proxyAppConnMem)
2015-12-01 20:12:01 -08:00
// Make ConsensusReactor
2016-09-13 19:25:11 -07:00
cs := NewConsensusState(thisConfig, state, proxyAppConnCon, blockStore, mempool)
2016-01-18 12:57:57 -08:00
cs.SetPrivValidator(pv)
2015-12-01 20:12:01 -08:00
2016-10-09 23:58:13 -07:00
evsw := types.NewEventSwitch()
cs.SetEventSwitch(evsw)
evsw.Start()
2016-01-18 12:57:57 -08:00
return cs
}
2015-12-01 20:12:01 -08:00
2017-01-12 11:44:42 -08:00
func loadPrivValidator(conf cfg.Config) *types.PrivValidator {
privValidatorFile := conf.GetString("priv_validator_file")
ensureDir(path.Dir(privValidatorFile), 0700)
privValidator := types.LoadOrGenPrivValidator(privValidatorFile)
privValidator.Reset()
return privValidator
}
func fixedConsensusState() *ConsensusState {
stateDB := dbm.NewMemDB()
state := sm.MakeGenesisStateFromFile(stateDB, config.GetString("genesis_file"))
privValidator := loadPrivValidator(config)
cs := newConsensusState(state, privValidator, counter.NewCounterApplication(true))
return cs
}
func fixedConsensusStateDummy() *ConsensusState {
stateDB := dbm.NewMemDB()
state := sm.MakeGenesisStateFromFile(stateDB, config.GetString("genesis_file"))
privValidator := loadPrivValidator(config)
cs := newConsensusState(state, privValidator, dummy.NewDummyApplication())
return cs
2016-09-13 19:25:11 -07:00
}
2016-01-18 12:57:57 -08: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
cs := newConsensusState(state, privVals[0], counter.NewCounterApplication(true))
2015-12-08 13:00:59 -08:00
2015-12-01 20:12:01 -08:00
for i := 0; i < nValidators; i++ {
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
}
//-------------------------------------------------------------------------------
func ensureNoNewStep(stepCh chan interface{}) {
timeout := time.NewTicker(ensureTimeout * time.Second)
select {
case <-timeout.C:
break
case <-stepCh:
panic("We should be stuck waiting for more votes, not moving to the next step")
}
}
//-------------------------------------------------------------------------------
// consensus nets
2017-01-12 12:53:32 -08:00
func randConsensusNet(nValidators int, testName string, tickerFunc func() TimeoutTicker, appFunc func() abci.Application) []*ConsensusState {
2016-06-25 21:40:53 -07:00
genDoc, privVals := randGenesisDoc(nValidators, false, 10)
css := make([]*ConsensusState, nValidators)
for i := 0; i < nValidators; i++ {
db := dbm.NewMemDB() // each state needs its own db
state := sm.MakeGenesisState(db, genDoc)
state.Save()
2016-12-17 10:24:54 -08:00
thisConfig := tendermint_test.ResetConfig(Fmt("%s_%d", testName, i))
2017-01-12 11:44:42 -08:00
ensureDir(thisConfig.GetString("cs_wal_dir"), 0700) // dir for wal
css[i] = newConsensusStateWithConfig(thisConfig, state, privVals[i], appFunc())
css[i].SetTimeoutTicker(tickerFunc())
2016-06-25 21:40:53 -07:00
}
return css
}
2016-11-23 15:20:46 -08:00
// nPeers = nValidators + nNotValidator
2017-01-12 12:53:32 -08:00
func randConsensusNetWithPeers(nValidators, nPeers int, testName string, tickerFunc func() TimeoutTicker, appFunc func() abci.Application) []*ConsensusState {
2016-11-23 15:20:46 -08:00
genDoc, privVals := randGenesisDoc(nValidators, false, int64(testMinPower))
css := make([]*ConsensusState, nPeers)
for i := 0; i < nPeers; i++ {
db := dbm.NewMemDB() // each state needs its own db
state := sm.MakeGenesisState(db, genDoc)
state.Save()
2016-12-17 10:24:54 -08:00
thisConfig := tendermint_test.ResetConfig(Fmt("%s_%d", testName, i))
2017-01-12 11:44:42 -08:00
ensureDir(thisConfig.GetString("cs_wal_dir"), 0700) // dir for wal
2016-11-23 15:20:46 -08:00
var privVal *types.PrivValidator
if i < nValidators {
privVal = privVals[i]
} else {
privVal = types.GenPrivValidator()
_, tempFilePath := Tempfile("priv_validator_")
privVal.SetFile(tempFilePath)
}
css[i] = newConsensusStateWithConfig(thisConfig, state, privVal, appFunc())
css[i].SetTimeoutTicker(tickerFunc())
2016-11-23 15:20:46 -08:00
}
return css
}
func getSwitchIndex(switches []*p2p.Switch, peer *p2p.Peer) int {
for i, s := range switches {
if bytes.Equal(peer.NodeInfo.PubKey.Address(), s.NodeInfo().PubKey.Address()) {
return i
2016-07-11 17:40:48 -07:00
}
}
panic("didnt find peer in switches")
return -1
2016-07-11 17:40:48 -07:00
}
//-------------------------------------------------------------------------------
// genesis
2015-12-01 20:12:01 -08:00
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,
Amount: val.VotingPower,
}
privValidators[i] = privVal
}
sort.Sort(types.PrivValidatorsByAddress(privValidators))
return &types.GenesisDoc{
GenesisTime: time.Now(),
ChainID: config.GetString("chain_id"),
Validators: validators,
}, privValidators
}
2016-11-23 15:20:46 -08:00
func randGenesisState(numValidators int, randPower bool, minPower int64) (*sm.State, []*types.PrivValidator) {
genDoc, privValidators := randGenesisDoc(numValidators, randPower, minPower)
db := dbm.NewMemDB()
s0 := sm.MakeGenesisState(db, genDoc)
s0.Save()
return s0, privValidators
2016-11-23 15:20:46 -08:00
}
2016-12-06 16:54:10 -08:00
//------------------------------------
// mock ticker
func newMockTickerFunc(onlyOnce bool) func() TimeoutTicker {
return func() TimeoutTicker {
return &mockTicker{
c: make(chan timeoutInfo, 10),
onlyOnce: onlyOnce,
}
}
}
2016-12-22 18:51:58 -08:00
// mock ticker only fires on RoundStepNewHeight
// and only once if onlyOnce=true
type mockTicker struct {
c chan timeoutInfo
mtx sync.Mutex
onlyOnce bool
fired bool
}
func (m *mockTicker) Start() (bool, error) {
return true, nil
}
func (m *mockTicker) Stop() bool {
return true
}
func (m *mockTicker) ScheduleTimeout(ti timeoutInfo) {
m.mtx.Lock()
defer m.mtx.Unlock()
if m.onlyOnce && m.fired {
return
}
if ti.Step == RoundStepNewHeight {
m.c <- ti
m.fired = true
}
}
func (m *mockTicker) Chan() <-chan timeoutInfo {
return m.c
}
//------------------------------------
2017-01-12 12:53:32 -08:00
func newCounter() abci.Application {
return counter.NewCounterApplication(true)
}
2017-01-12 12:53:32 -08:00
func newPersistentDummy() abci.Application {
dir, _ := ioutil.TempDir("/tmp", "persistent-dummy")
return dummy.NewPersistentDummyApplication(dir)
}