tendermint/consensus/common_test.go

422 lines
12 KiB
Go
Raw Normal View History

2015-12-01 20:12:01 -08:00
package consensus
import (
"bytes"
"fmt"
"sort"
"sync"
2015-12-01 20:12:01 -08:00
"testing"
"time"
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"
bc "github.com/tendermint/tendermint/blockchain"
mempl "github.com/tendermint/tendermint/mempool"
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"
2016-03-24 10:42:05 -07:00
tmspcli "github.com/tendermint/tmsp/client"
tmsp "github.com/tendermint/tmsp/types"
2015-12-01 20:12:01 -08:00
2016-02-14 15:03:55 -08:00
"github.com/tendermint/tmsp/example/counter"
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)
2015-12-01 20:12:01 -08:00
type validatorStub struct {
Height int
Round int
*types.PrivValidator
}
func NewValidatorStub(privValidator *types.PrivValidator) *validatorStub {
return &validatorStub{
PrivValidator: privValidator,
}
}
func (vs *validatorStub) signVote(voteType byte, hash []byte, header types.PartSetHeader) (*types.Vote, error) {
vote := &types.Vote{
Height: vs.Height,
Round: vs.Round,
Type: voteType,
BlockHash: hash,
BlockPartsHeader: header,
}
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
}
// convenienve function for testing
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
}
// create proposal block from cs1 but sign it with vs
func decideProposal(cs1 *ConsensusState, cs2 *validatorStub, height, round int) (proposal *types.Proposal, block *types.Block) {
block, blockParts := cs1.createProposalBlock()
if block == nil { // on error
panic("error creating proposal block")
}
// Make proposal
proposal = types.NewProposal(height, round, blockParts.Header(), cs1.Votes.POLRound())
2016-05-08 15:00:58 -07:00
if err := cs2.SignProposal(config.GetString("chain_id"), proposal); err != nil {
2015-12-01 20:12:01 -08:00
panic(err)
}
return
}
//-------------------------------------------------------------------------------
// utils
/*
func nilRound(t *testing.T, cs1 *ConsensusState, vss ...*validatorStub) {
cs1.mtx.Lock()
2015-12-01 20:12:01 -08:00
height, round := cs1.Height, cs1.Round
cs1.mtx.Unlock()
2015-12-01 20:12:01 -08:00
waitFor(t, cs1, height, round, RoundStepPrevote)
signAddVoteToFromMany(types.VoteTypePrevote, cs1, nil, cs1.ProposalBlockParts.Header(), vss...)
waitFor(t, cs1, height, round, RoundStepPrecommit)
signAddVoteToFromMany(types.VoteTypePrecommit, cs1, nil, cs1.ProposalBlockParts.Header(), vss...)
waitFor(t, cs1, height, round+1, RoundStepNewRound)
}
*/
2015-12-01 20:12:01 -08:00
// NOTE: this switches the propser as far as `perspectiveOf` is concerned,
// but for simplicity we return a block it generated.
func changeProposer(t *testing.T, perspectiveOf *ConsensusState, newProposer *validatorStub) *types.Block {
_, v1 := perspectiveOf.Validators.GetByAddress(perspectiveOf.privValidator.Address)
v1.Accum, v1.VotingPower = 0, 0
if updated := perspectiveOf.Validators.Update(v1); !updated {
2016-07-11 18:10:05 -07:00
panic("failed to update validator")
2015-12-01 20:12:01 -08:00
}
_, v2 := perspectiveOf.Validators.GetByAddress(newProposer.Address)
v2.Accum, v2.VotingPower = 100, 100
if updated := perspectiveOf.Validators.Update(v2); !updated {
2016-07-11 18:10:05 -07:00
panic("failed to update validator")
2015-12-01 20:12:01 -08:00
}
// make the proposal
propBlock, _ := perspectiveOf.createProposalBlock()
if propBlock == nil {
2016-07-11 18:10:05 -07:00
panic("Failed to create proposal block with cs2")
2015-12-01 20:12:01 -08:00
}
return propBlock
}
func fixVotingPower(t *testing.T, cs1 *ConsensusState, addr2 []byte) {
_, v1 := cs1.Validators.GetByAddress(cs1.privValidator.Address)
_, v2 := cs1.Validators.GetByAddress(addr2)
v1.Accum, v1.VotingPower = v2.Accum, v2.VotingPower
if updated := cs1.Validators.Update(v1); !updated {
2016-07-11 18:10:05 -07:00
panic("failed to update validator")
2015-12-01 20:12:01 -08:00
}
}
func addVoteToFromMany(to *ConsensusState, votes []*types.Vote, froms ...*validatorStub) {
if len(votes) != len(froms) {
panic("len(votes) and len(froms) must match")
}
2016-07-11 17:40:48 -07:00
2015-12-01 20:12:01 -08:00
for i, from := range froms {
addVoteToFrom(to, from, votes[i])
}
}
func addVoteToFrom(to *ConsensusState, from *validatorStub, vote *types.Vote) {
2016-07-11 17:40:48 -07:00
to.mtx.Lock() // NOTE: wont need this when the vote comes with the index!
2015-12-01 20:12:01 -08:00
valIndex, _ := to.Validators.GetByAddress(from.PrivValidator.Address)
2016-07-11 17:40:48 -07:00
to.mtx.Unlock()
2015-12-23 18:43:48 -08:00
to.peerMsgQueue <- msgInfo{Msg: &VoteMessage{valIndex, vote}}
// added, err := to.TryAddVote(valIndex, vote, "")
/*
if _, ok := err.(*types.ErrVoteConflictingSignature); ok {
// let it fly
} else if !added {
fmt.Println("to, from, vote:", to.Height, from.Height, vote.Height)
panic(fmt.Sprintln("Failed to add vote. Err:", err))
} else if err != nil {
panic(fmt.Sprintln("Failed to add vote:", err))
}*/
2015-12-01 20:12:01 -08:00
}
func signVoteMany(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
}
// add vote to one cs from another
2016-07-11 17:40:48 -07:00
// if voteCh is not nil, read all votes
func signAddVoteToFromMany(voteType byte, to *ConsensusState, hash []byte, header types.PartSetHeader, voteCh chan interface{}, froms ...*validatorStub) {
var wg chan struct{} // when done reading all votes
if voteCh != nil {
wg = readVotes(voteCh, len(froms))
}
2015-12-01 20:12:01 -08:00
for _, from := range froms {
vote := signVote(from, voteType, hash, header)
addVoteToFrom(to, from, vote)
}
2016-07-11 17:40:48 -07:00
if voteCh != nil {
<-wg
}
2015-12-01 20:12:01 -08:00
}
2016-07-11 17:40:48 -07:00
func signAddVoteToFrom(voteType byte, to *ConsensusState, from *validatorStub, hash []byte, header types.PartSetHeader, voteCh chan interface{}) *types.Vote {
var wg chan struct{} // when done reading all votes
if voteCh != nil {
wg = readVotes(voteCh, 1)
}
2015-12-01 20:12:01 -08:00
vote := signVote(from, voteType, hash, header)
addVoteToFrom(to, from, vote)
2016-07-11 17:40:48 -07:00
if voteCh != nil {
<-wg
}
2015-12-01 20:12:01 -08:00
return vote
}
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")
}
}
/*
2015-12-01 20:12:01 -08:00
func ensureNoNewStep(t *testing.T, cs *ConsensusState) {
timeout := time.NewTicker(ensureTimeout * time.Second)
2015-12-01 20:12:01 -08:00
select {
case <-timeout.C:
break
case <-cs.NewStepCh():
panic("We should be stuck waiting for more votes, not moving to the next step")
}
}
func ensureNewStep(t *testing.T, cs *ConsensusState) *RoundState {
timeout := time.NewTicker(ensureTimeout * time.Second)
2015-12-01 20:12:01 -08:00
select {
case <-timeout.C:
panic("We should have gone to the next step, not be stuck waiting")
case rs := <-cs.NewStepCh():
return rs
}
}
func waitFor(t *testing.T, cs *ConsensusState, height int, round int, step RoundStepType) {
for {
rs := ensureNewStep(t, cs)
if CompareHRS(rs.Height, rs.Round, rs.Step, height, round, step) < 0 {
continue
} else {
break
}
}
}
*/
2015-12-01 20:12:01 -08:00
2015-12-13 11:56:05 -08:00
func incrementHeight(vss ...*validatorStub) {
for _, vs := range vss {
vs.Height += 1
}
}
func incrementRound(vss ...*validatorStub) {
for _, vs := range vss {
vs.Round += 1
}
}
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 {
if vote.BlockHash != nil {
panic(fmt.Sprintf("Expected prevote to be for nil, got %X", vote.BlockHash))
}
} else {
if !bytes.Equal(vote.BlockHash, blockHash) {
panic(fmt.Sprintf("Expected prevote to be for %X, got %X", blockHash, vote.BlockHash))
}
}
}
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
}
2015-12-13 11:56:05 -08:00
if !bytes.Equal(vote.BlockHash, blockHash) {
panic(fmt.Sprintf("Expected precommit to be for %X, got %X", blockHash, vote.BlockHash))
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 {
if vote.BlockHash != nil {
panic("Expected precommit to be for nil")
}
} else {
if !bytes.Equal(vote.BlockHash, votedBlockHash) {
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()
}
2016-01-18 12:57:57 -08:00
func fixedConsensusState() *ConsensusState {
stateDB := dbm.NewMemDB()
state := sm.MakeGenesisStateFromFile(stateDB, config.GetString("genesis_file"))
privValidatorFile := config.GetString("priv_validator_file")
privValidator := types.LoadOrGenPrivValidator(privValidatorFile)
privValidator.Reset()
2016-08-17 20:08:43 -07:00
cs := newConsensusState(state, privValidator, counter.NewCounterApplication(true))
return cs
2016-01-18 12:57:57 -08:00
}
2015-12-01 20:12:01 -08:00
func newConsensusState(state *sm.State, pv *types.PrivValidator, app tmsp.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)
2016-03-24 10:42:05 -07:00
proxyAppConnMem := tmspcli.NewLocalClient(mtx, app)
proxyAppConnCon := tmspcli.NewLocalClient(mtx, app)
2015-12-01 20:12:01 -08:00
// Make Mempool
2016-05-08 15:00:58 -07:00
mempool := mempl.NewMempool(config, proxyAppConnMem)
2015-12-01 20:12:01 -08:00
// Make ConsensusReactor
2016-05-08 15:00:58 -07:00
cs := NewConsensusState(config, 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
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])
}
// since cs1 starts at 1
incrementHeight(vss[1:]...)
return cs, vss
}
2015-12-13 11:56:05 -08:00
func subscribeToVoter(cs *ConsensusState, addr []byte) chan interface{} {
2016-01-18 12:18:09 -08:00
voteCh0 := subscribeToEvent(cs.evsw, "tester", types.EventStringVote(), 1)
2015-12-13 11:56:05 -08:00
voteCh := make(chan interface{})
go func() {
for {
v := <-voteCh0
2015-12-23 18:43:48 -08:00
vote := v.(types.EventDataVote)
2015-12-13 11:56:05 -08:00
// we only fire for our own votes
if bytes.Equal(addr, vote.Address) {
voteCh <- v
}
}
}()
return voteCh
}
2016-07-11 17:40:48 -07: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
}
2015-12-01 20:12:01 -08:00
func randGenesisState(numValidators int, randPower bool, minPower int64) (*sm.State, []*types.PrivValidator) {
db := dbm.NewMemDB()
genDoc, privValidators := randGenesisDoc(numValidators, randPower, minPower)
s0 := sm.MakeGenesisState(db, genDoc)
s0.Save()
return s0, privValidators
}
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
}
func startTestRound(cs *ConsensusState, height, round int) {
cs.enterNewRound(height, round)
cs.startRoutines(0)
}