2014-10-17 16:48:27 -07:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
2015-08-12 14:00:23 -04:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-04-01 17:30:16 -07:00
|
|
|
bc "github.com/tendermint/tendermint/blockchain"
|
|
|
|
dbm "github.com/tendermint/tendermint/db"
|
|
|
|
mempl "github.com/tendermint/tendermint/mempool"
|
2015-08-12 14:00:23 -04:00
|
|
|
"github.com/tendermint/tendermint/p2p"
|
2015-04-01 17:30:16 -07:00
|
|
|
sm "github.com/tendermint/tendermint/state"
|
2015-08-10 20:38:45 -07:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2014-10-17 16:48:27 -07:00
|
|
|
)
|
|
|
|
|
2015-08-12 14:00:23 -04:00
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
// utils
|
|
|
|
|
|
|
|
// add vote to one cs from another
|
|
|
|
func addVoteToFrom(t *testing.T, voteType byte, to, from *ConsensusState, hash []byte, header types.PartSetHeader) {
|
|
|
|
vote, err := from.signVote(voteType, hash, header)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintln("Failed to sign vote", err))
|
|
|
|
}
|
|
|
|
valIndex, _ := to.Validators.GetByAddress(from.privValidator.Address)
|
|
|
|
added, err := to.TryAddVote(to.GetRoundState(), vote, valIndex, "")
|
|
|
|
if _, ok := err.(*types.ErrVoteConflictingSignature); ok {
|
|
|
|
// let it fly
|
|
|
|
} else if !added {
|
|
|
|
panic("Failed to add vote")
|
|
|
|
} else if err != nil {
|
|
|
|
panic(fmt.Sprintln("Failed to add vote:", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ensureNoNewStep(t *testing.T, cs *ConsensusState) {
|
|
|
|
timeout := time.NewTicker(2 * time.Second)
|
|
|
|
select {
|
|
|
|
case <-timeout.C:
|
|
|
|
break
|
|
|
|
case <-cs.NewStepCh():
|
|
|
|
t.Fatal("We should be stuck waiting for more prevotes, not moving to the next step")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validatePrevote(t *testing.T, cs *ConsensusState, round int, privVal *types.PrivValidator, blockHash []byte) {
|
|
|
|
prevotes := cs.Votes.Prevotes(round)
|
|
|
|
var vote *types.Vote
|
|
|
|
if vote = prevotes.GetByAddress(privVal.Address); vote == nil {
|
|
|
|
t.Fatal("Failed to find prevote from validator")
|
|
|
|
}
|
|
|
|
if blockHash == nil {
|
|
|
|
if vote.BlockHash != nil {
|
|
|
|
t.Fatal("Expected prevote to be for nil")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !bytes.Equal(vote.BlockHash, blockHash) {
|
|
|
|
t.Fatal("Expected prevote to be for proposal block")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validatePrecommit(t *testing.T, cs *ConsensusState, thisRound, lockRound int, privVal *types.PrivValidator, votedBlock, lockedBlock *types.Block) {
|
|
|
|
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 votedBlock == nil {
|
|
|
|
if vote.BlockHash != nil {
|
|
|
|
panic("Expected precommit to be for nil")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !bytes.Equal(vote.BlockHash, votedBlock.Hash()) {
|
|
|
|
panic("Expected precommit to be for proposal block")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if lockedBlock == nil {
|
|
|
|
if cs.LockedRound != lockRound || cs.LockedBlock != nil {
|
|
|
|
panic(fmt.Sprintf("Expected to be locked on nil. Got %v", cs.LockedBlock))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if cs.LockedRound != lockRound || cs.LockedBlock != lockedBlock {
|
|
|
|
panic(fmt.Sprintf("Expected block to be locked on round %d, got %d. Got locked block %v, expected %v", lockRound, cs.LockedRound, cs.LockedBlock, lockedBlock))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func simpleConsensusState(nValidators int) ([]*ConsensusState, []*types.PrivValidator) {
|
|
|
|
// Get State
|
|
|
|
state, privAccs, privVals := sm.RandGenesisState(10, true, 1000, nValidators, false, 10)
|
|
|
|
_, _ = privAccs, privVals
|
|
|
|
|
|
|
|
fmt.Println(state.BondedValidators)
|
|
|
|
|
|
|
|
css := make([]*ConsensusState, nValidators)
|
|
|
|
for i := 0; i < nValidators; i++ {
|
|
|
|
// Get BlockStore
|
|
|
|
blockDB := dbm.NewMemDB()
|
|
|
|
blockStore := bc.NewBlockStore(blockDB)
|
|
|
|
|
|
|
|
// Make MempoolReactor
|
|
|
|
mempool := mempl.NewMempool(state.Copy())
|
|
|
|
mempoolReactor := mempl.NewMempoolReactor(mempool)
|
|
|
|
|
|
|
|
mempoolReactor.SetSwitch(p2p.NewSwitch())
|
|
|
|
|
|
|
|
// Make ConsensusReactor
|
|
|
|
cs := NewConsensusState(state, blockStore, mempoolReactor)
|
|
|
|
|
|
|
|
// read off the NewHeightStep
|
|
|
|
<-cs.NewStepCh()
|
|
|
|
|
|
|
|
css[i] = cs
|
|
|
|
}
|
|
|
|
|
|
|
|
return css, privVals
|
|
|
|
}
|
|
|
|
|
2015-08-10 20:38:45 -07:00
|
|
|
func randConsensusState() (*ConsensusState, []*types.PrivValidator) {
|
2015-01-17 01:56:55 -08:00
|
|
|
state, _, privValidators := sm.RandGenesisState(20, false, 1000, 10, false, 1000)
|
2015-03-25 00:15:18 -07:00
|
|
|
blockStore := bc.NewBlockStore(dbm.NewMemDB())
|
2015-01-17 01:56:55 -08:00
|
|
|
mempool := mempl.NewMempool(state)
|
|
|
|
mempoolReactor := mempl.NewMempoolReactor(mempool)
|
|
|
|
cs := NewConsensusState(state, blockStore, mempoolReactor)
|
|
|
|
return cs, privValidators
|
2014-10-17 16:48:27 -07:00
|
|
|
}
|