tendermint/consensus/vote_set_test.go

269 lines
8.2 KiB
Go
Raw Normal View History

2014-10-14 13:11:54 -07:00
package consensus
import (
2014-10-31 18:35:38 -07:00
"bytes"
2015-04-01 17:30:16 -07:00
. "github.com/tendermint/tendermint/common"
. "github.com/tendermint/tendermint/common/test"
_ "github.com/tendermint/tendermint/config/tendermint_test"
2015-05-29 14:53:57 -07:00
sm "github.com/tendermint/tendermint/state"
2015-04-01 17:30:16 -07:00
"github.com/tendermint/tendermint/types"
2014-10-14 13:11:54 -07:00
"testing"
)
2014-10-17 16:48:27 -07:00
// NOTE: see consensus/test.go for common test methods.
2014-10-15 20:15:38 -07:00
2014-12-23 23:20:49 -08:00
// Convenience: Return new vote with different height
func withHeight(vote *types.Vote, height int) *types.Vote {
2014-12-23 23:20:49 -08:00
vote = vote.Copy()
vote.Height = height
return vote
}
// Convenience: Return new vote with different round
func withRound(vote *types.Vote, round int) *types.Vote {
2014-12-23 23:20:49 -08:00
vote = vote.Copy()
vote.Round = round
return vote
}
// Convenience: Return new vote with different type
func withType(vote *types.Vote, type_ byte) *types.Vote {
2014-12-23 23:20:49 -08:00
vote = vote.Copy()
vote.Type = type_
return vote
}
// Convenience: Return new vote with different blockHash
func withBlockHash(vote *types.Vote, blockHash []byte) *types.Vote {
2014-12-23 23:20:49 -08:00
vote = vote.Copy()
vote.BlockHash = blockHash
return vote
}
// Convenience: Return new vote with different blockParts
func withBlockParts(vote *types.Vote, blockParts types.PartSetHeader) *types.Vote {
2014-12-23 23:20:49 -08:00
vote = vote.Copy()
vote.BlockParts = blockParts
return vote
}
func signAddVote(privVal *sm.PrivValidator, vote *types.Vote, voteSet *VoteSet) (bool, error) {
2015-05-29 14:53:57 -07:00
privVal.SignVoteUnsafe(config.GetString("chain_id"), vote)
added, _, err := voteSet.AddByAddress(privVal.Address, vote)
2014-12-23 23:20:49 -08:00
return added, err
}
2014-10-14 13:11:54 -07:00
func TestAddVote(t *testing.T) {
height, round := 1, 0
voteSet, _, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
2014-12-17 01:37:13 -08:00
val0 := privValidators[0]
2014-10-15 20:15:38 -07:00
2014-10-15 23:55:52 -07:00
// t.Logf(">> %v", voteSet)
2014-10-15 20:15:38 -07:00
2014-12-17 01:37:13 -08:00
if voteSet.GetByAddress(val0.Address) != nil {
t.Errorf("Expected GetByAddress(val0.Address) to be nil")
2014-10-15 23:55:52 -07:00
}
if voteSet.BitArray().GetIndex(0) {
t.Errorf("Expected BitArray.GetIndex(0) to be false")
}
2014-10-30 03:32:09 -07:00
hash, header, ok := voteSet.TwoThirdsMajority()
if hash != nil || !header.IsZero() || ok {
2014-10-15 23:55:52 -07:00
t.Errorf("There should be no 2/3 majority")
}
vote := &types.Vote{Height: height, Round: round, Type: types.VoteTypePrevote, BlockHash: nil}
2014-12-23 23:20:49 -08:00
signAddVote(val0, vote, voteSet)
2014-10-15 20:15:38 -07:00
2014-12-17 01:37:13 -08:00
if voteSet.GetByAddress(val0.Address) == nil {
t.Errorf("Expected GetByAddress(val0.Address) to be present")
2014-10-15 23:55:52 -07:00
}
if !voteSet.BitArray().GetIndex(0) {
t.Errorf("Expected BitArray.GetIndex(0) to be true")
}
2014-10-30 03:32:09 -07:00
hash, header, ok = voteSet.TwoThirdsMajority()
if hash != nil || !header.IsZero() || ok {
2014-10-15 23:55:52 -07:00
t.Errorf("There should be no 2/3 majority")
}
2014-10-14 13:11:54 -07:00
}
func Test2_3Majority(t *testing.T) {
height, round := 1, 0
voteSet, _, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
2014-10-15 23:55:52 -07:00
vote := &types.Vote{Height: height, Round: round, Type: types.VoteTypePrevote, BlockHash: nil}
2014-12-23 23:20:49 -08:00
2014-10-15 23:55:52 -07:00
// 6 out of 10 voted for nil.
for i := 0; i < 6; i++ {
2014-12-23 23:20:49 -08:00
signAddVote(privValidators[i], vote, voteSet)
2014-10-15 23:55:52 -07:00
}
2014-10-30 03:32:09 -07:00
hash, header, ok := voteSet.TwoThirdsMajority()
if hash != nil || !header.IsZero() || ok {
2014-10-15 23:55:52 -07:00
t.Errorf("There should be no 2/3 majority")
}
// 7th validator voted for some blockhash
2014-10-31 18:35:38 -07:00
{
2014-12-23 23:20:49 -08:00
signAddVote(privValidators[6], withBlockHash(vote, RandBytes(32)), voteSet)
2014-10-31 18:35:38 -07:00
hash, header, ok = voteSet.TwoThirdsMajority()
if hash != nil || !header.IsZero() || ok {
t.Errorf("There should be no 2/3 majority")
}
}
// 8th validator voted for nil.
{
2014-12-23 23:20:49 -08:00
signAddVote(privValidators[7], vote, voteSet)
2014-10-31 18:35:38 -07:00
hash, header, ok = voteSet.TwoThirdsMajority()
if hash != nil || !header.IsZero() || !ok {
t.Errorf("There should be 2/3 majority for nil")
}
}
}
func Test2_3MajorityRedux(t *testing.T) {
height, round := 1, 0
voteSet, _, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 100, 1)
2014-10-31 18:35:38 -07:00
blockHash := CRandBytes(32)
blockPartsTotal := 123
blockParts := types.PartSetHeader{blockPartsTotal, CRandBytes(32)}
2014-10-31 18:35:38 -07:00
vote := &types.Vote{Height: height, Round: round, Type: types.VoteTypePrevote, BlockHash: blockHash, BlockParts: blockParts}
2014-12-23 23:20:49 -08:00
2014-10-31 18:35:38 -07:00
// 66 out of 100 voted for nil.
for i := 0; i < 66; i++ {
2014-12-23 23:20:49 -08:00
signAddVote(privValidators[i], vote, voteSet)
2014-10-31 18:35:38 -07:00
}
hash, header, ok := voteSet.TwoThirdsMajority()
2014-10-30 03:32:09 -07:00
if hash != nil || !header.IsZero() || ok {
2014-10-15 23:55:52 -07:00
t.Errorf("There should be no 2/3 majority")
}
2014-10-31 18:35:38 -07:00
// 67th validator voted for nil
{
2014-12-23 23:20:49 -08:00
signAddVote(privValidators[66], withBlockHash(vote, nil), voteSet)
2014-10-31 18:35:38 -07:00
hash, header, ok = voteSet.TwoThirdsMajority()
if hash != nil || !header.IsZero() || ok {
t.Errorf("There should be no 2/3 majority: last vote added was nil")
}
}
// 68th validator voted for a different BlockParts PartSetHeader
{
blockParts := types.PartSetHeader{blockPartsTotal, CRandBytes(32)}
2014-12-23 23:20:49 -08:00
signAddVote(privValidators[67], withBlockParts(vote, blockParts), voteSet)
2014-10-31 18:35:38 -07:00
hash, header, ok = voteSet.TwoThirdsMajority()
if hash != nil || !header.IsZero() || ok {
t.Errorf("There should be no 2/3 majority: last vote added had different PartSetHeader Hash")
}
2014-10-15 23:55:52 -07:00
}
2014-10-31 18:35:38 -07:00
// 69th validator voted for different BlockParts Total
{
blockParts := types.PartSetHeader{blockPartsTotal + 1, blockParts.Hash}
2014-12-23 23:20:49 -08:00
signAddVote(privValidators[68], withBlockParts(vote, blockParts), voteSet)
2014-10-31 18:35:38 -07:00
hash, header, ok = voteSet.TwoThirdsMajority()
if hash != nil || !header.IsZero() || ok {
t.Errorf("There should be no 2/3 majority: last vote added had different PartSetHeader Total")
}
}
// 70th validator voted for different BlockHash
{
2014-12-23 23:20:49 -08:00
signAddVote(privValidators[69], withBlockHash(vote, RandBytes(32)), voteSet)
2014-10-31 18:35:38 -07:00
hash, header, ok = voteSet.TwoThirdsMajority()
if hash != nil || !header.IsZero() || ok {
t.Errorf("There should be no 2/3 majority: last vote added had different BlockHash")
}
}
// 71st validator voted for the right BlockHash & BlockParts
{
2014-12-23 23:20:49 -08:00
signAddVote(privValidators[70], vote, voteSet)
2014-10-31 18:35:38 -07:00
hash, header, ok = voteSet.TwoThirdsMajority()
if !bytes.Equal(hash, blockHash) || !header.Equals(blockParts) || !ok {
t.Errorf("There should be 2/3 majority")
}
}
2014-10-15 23:55:52 -07:00
}
func TestBadVotes(t *testing.T) {
height, round := 1, 0
voteSet, _, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
2014-10-15 23:55:52 -07:00
// val0 votes for nil.
vote := &types.Vote{Height: height, Round: round, Type: types.VoteTypePrevote, BlockHash: nil}
2014-12-23 23:20:49 -08:00
added, err := signAddVote(privValidators[0], vote, voteSet)
2014-10-15 23:55:52 -07:00
if !added || err != nil {
t.Errorf("Expected VoteSet.Add to succeed")
2014-10-15 23:55:52 -07:00
}
// val0 votes again for some block.
2014-12-23 23:20:49 -08:00
added, err = signAddVote(privValidators[0], withBlockHash(vote, RandBytes(32)), voteSet)
2014-10-15 23:55:52 -07:00
if added || err == nil {
t.Errorf("Expected VoteSet.Add to fail, dupeout.")
2014-10-15 23:55:52 -07:00
}
// val1 votes on another height
2014-12-23 23:20:49 -08:00
added, err = signAddVote(privValidators[1], withHeight(vote, height+1), voteSet)
2014-10-15 23:55:52 -07:00
if added {
t.Errorf("Expected VoteSet.Add to fail, wrong height")
2014-10-15 23:55:52 -07:00
}
// val2 votes on another round
2014-12-23 23:20:49 -08:00
added, err = signAddVote(privValidators[2], withRound(vote, round+1), voteSet)
2014-10-15 23:55:52 -07:00
if added {
t.Errorf("Expected VoteSet.Add to fail, wrong round")
2014-10-15 23:55:52 -07:00
}
// val3 votes of another type.
added, err = signAddVote(privValidators[3], withType(vote, types.VoteTypePrecommit), voteSet)
2014-10-15 23:55:52 -07:00
if added {
t.Errorf("Expected VoteSet.Add to fail, wrong type")
2014-10-15 23:55:52 -07:00
}
}
2014-10-31 18:35:38 -07:00
func TestMakeValidation(t *testing.T) {
height, round := 1, 0
2015-06-05 14:15:40 -07:00
voteSet, _, privValidators := randVoteSet(height, round, types.VoteTypePrecommit, 10, 1)
blockHash, blockParts := CRandBytes(32), types.PartSetHeader{123, CRandBytes(32)}
2014-10-31 18:35:38 -07:00
2015-06-05 14:15:40 -07:00
vote := &types.Vote{Height: height, Round: round, Type: types.VoteTypePrecommit,
2014-10-31 18:35:38 -07:00
BlockHash: blockHash, BlockParts: blockParts}
2014-12-23 23:20:49 -08:00
// 6 out of 10 voted for some block.
2014-10-31 18:35:38 -07:00
for i := 0; i < 6; i++ {
2014-12-23 23:20:49 -08:00
signAddVote(privValidators[i], vote, voteSet)
2014-10-31 18:35:38 -07:00
}
// MakeValidation should fail.
AssertPanics(t, "Doesn't have +2/3 majority", func() { voteSet.MakeValidation() })
// 7th voted for some other block.
{
2014-12-23 23:20:49 -08:00
vote := withBlockHash(vote, RandBytes(32))
vote = withBlockParts(vote, types.PartSetHeader{123, RandBytes(32)})
2014-12-23 23:20:49 -08:00
signAddVote(privValidators[6], vote, voteSet)
2014-10-31 18:35:38 -07:00
}
// The 8th voted like everyone else.
{
2014-12-23 23:20:49 -08:00
signAddVote(privValidators[7], vote, voteSet)
2014-10-31 18:35:38 -07:00
}
validation := voteSet.MakeValidation()
// Validation should have 10 elements
2015-06-05 14:15:40 -07:00
if len(validation.Precommits) != 10 {
t.Errorf("Validation Precommits should have the same number of precommits as validators")
2014-10-31 18:35:38 -07:00
}
2015-06-05 14:15:40 -07:00
// Ensure that Validation precommits are ordered.
2014-12-17 01:37:13 -08:00
if err := validation.ValidateBasic(); err != nil {
t.Errorf("Error in Validation.ValidateBasic(): %v", err)
2014-10-31 18:35:38 -07:00
}
2014-12-17 01:37:13 -08:00
2014-10-31 18:35:38 -07:00
}