|
|
@ -3,7 +3,7 @@ package consensus |
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
|
|
|
|
. "github.com/tendermint/tendermint/blocks" |
|
|
|
. "github.com/tendermint/tendermint/block" |
|
|
|
. "github.com/tendermint/tendermint/common" |
|
|
|
. "github.com/tendermint/tendermint/common/test" |
|
|
|
|
|
|
@ -13,13 +13,14 @@ import ( |
|
|
|
// NOTE: see consensus/test.go for common test methods.
|
|
|
|
|
|
|
|
func TestAddVote(t *testing.T) { |
|
|
|
height, round := uint32(1), uint16(0) |
|
|
|
voteSet, _, privAccounts := makeVoteSet(height, round, VoteTypePrevote, 10, 1) |
|
|
|
height, round := uint(1), uint(0) |
|
|
|
voteSet, _, privValidators := makeVoteSet(height, round, VoteTypePrevote, 10, 1) |
|
|
|
val0 := privValidators[0] |
|
|
|
|
|
|
|
// t.Logf(">> %v", voteSet)
|
|
|
|
|
|
|
|
if voteSet.GetById(0) != nil { |
|
|
|
t.Errorf("Expected GetById(0) to be nil") |
|
|
|
if voteSet.GetByAddress(val0.Address) != nil { |
|
|
|
t.Errorf("Expected GetByAddress(val0.Address) to be nil") |
|
|
|
} |
|
|
|
if voteSet.BitArray().GetIndex(0) { |
|
|
|
t.Errorf("Expected BitArray.GetIndex(0) to be false") |
|
|
@ -30,11 +31,11 @@ func TestAddVote(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
vote := &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: nil} |
|
|
|
privAccounts[0].Sign(vote) |
|
|
|
voteSet.Add(vote) |
|
|
|
vote.Signature = val0.SignVoteUnsafe(vote) |
|
|
|
voteSet.Add(val0.Address, vote) |
|
|
|
|
|
|
|
if voteSet.GetById(0) == nil { |
|
|
|
t.Errorf("Expected GetById(0) to be present") |
|
|
|
if voteSet.GetByAddress(val0.Address) == nil { |
|
|
|
t.Errorf("Expected GetByAddress(val0.Address) to be present") |
|
|
|
} |
|
|
|
if !voteSet.BitArray().GetIndex(0) { |
|
|
|
t.Errorf("Expected BitArray.GetIndex(0) to be true") |
|
|
@ -46,15 +47,15 @@ func TestAddVote(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func Test2_3Majority(t *testing.T) { |
|
|
|
height, round := uint32(1), uint16(0) |
|
|
|
voteSet, _, privAccounts := makeVoteSet(height, round, VoteTypePrevote, 10, 1) |
|
|
|
height, round := uint(1), uint(0) |
|
|
|
voteSet, _, privValidators := makeVoteSet(height, round, VoteTypePrevote, 10, 1) |
|
|
|
|
|
|
|
// 6 out of 10 voted for nil.
|
|
|
|
voteProto := &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: nil} |
|
|
|
for i := 0; i < 6; i++ { |
|
|
|
vote := voteProto.Copy() |
|
|
|
privAccounts[i].Sign(vote) |
|
|
|
voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[i].SignVoteUnsafe(vote) |
|
|
|
voteSet.Add(privValidators[i].Address, vote) |
|
|
|
} |
|
|
|
hash, header, ok := voteSet.TwoThirdsMajority() |
|
|
|
if hash != nil || !header.IsZero() || ok { |
|
|
@ -65,8 +66,8 @@ func Test2_3Majority(t *testing.T) { |
|
|
|
{ |
|
|
|
vote := voteProto.Copy() |
|
|
|
vote.BlockHash = CRandBytes(32) |
|
|
|
privAccounts[6].Sign(vote) |
|
|
|
voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[6].SignVoteUnsafe(vote) |
|
|
|
voteSet.Add(privValidators[6].Address, vote) |
|
|
|
hash, header, ok = voteSet.TwoThirdsMajority() |
|
|
|
if hash != nil || !header.IsZero() || ok { |
|
|
|
t.Errorf("There should be no 2/3 majority") |
|
|
@ -77,8 +78,8 @@ func Test2_3Majority(t *testing.T) { |
|
|
|
{ |
|
|
|
vote := voteProto.Copy() |
|
|
|
vote.BlockHash = nil |
|
|
|
privAccounts[7].Sign(vote) |
|
|
|
voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[7].SignVoteUnsafe(vote) |
|
|
|
voteSet.Add(privValidators[7].Address, vote) |
|
|
|
hash, header, ok = voteSet.TwoThirdsMajority() |
|
|
|
if hash != nil || !header.IsZero() || !ok { |
|
|
|
t.Errorf("There should be 2/3 majority for nil") |
|
|
@ -87,19 +88,19 @@ func Test2_3Majority(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func Test2_3MajorityRedux(t *testing.T) { |
|
|
|
height, round := uint32(1), uint16(0) |
|
|
|
voteSet, _, privAccounts := makeVoteSet(height, round, VoteTypePrevote, 100, 1) |
|
|
|
height, round := uint(1), uint(0) |
|
|
|
voteSet, _, privValidators := makeVoteSet(height, round, VoteTypePrevote, 100, 1) |
|
|
|
|
|
|
|
blockHash := CRandBytes(32) |
|
|
|
blockPartsTotal := uint16(123) |
|
|
|
blockPartsTotal := uint(123) |
|
|
|
blockParts := PartSetHeader{blockPartsTotal, CRandBytes(32)} |
|
|
|
|
|
|
|
// 66 out of 100 voted for nil.
|
|
|
|
voteProto := &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: blockHash, BlockParts: blockParts} |
|
|
|
for i := 0; i < 66; i++ { |
|
|
|
vote := voteProto.Copy() |
|
|
|
privAccounts[i].Sign(vote) |
|
|
|
voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[i].SignVoteUnsafe(vote) |
|
|
|
voteSet.Add(privValidators[i].Address, vote) |
|
|
|
} |
|
|
|
hash, header, ok := voteSet.TwoThirdsMajority() |
|
|
|
if hash != nil || !header.IsZero() || ok { |
|
|
@ -109,8 +110,8 @@ func Test2_3MajorityRedux(t *testing.T) { |
|
|
|
// 67th validator voted for nil
|
|
|
|
{ |
|
|
|
vote := &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: nil, BlockParts: PartSetHeader{}} |
|
|
|
privAccounts[66].Sign(vote) |
|
|
|
voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[66].SignVoteUnsafe(vote) |
|
|
|
voteSet.Add(privValidators[66].Address, vote) |
|
|
|
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") |
|
|
@ -120,8 +121,8 @@ func Test2_3MajorityRedux(t *testing.T) { |
|
|
|
// 68th validator voted for a different BlockParts PartSetHeader
|
|
|
|
{ |
|
|
|
vote := &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: blockHash, BlockParts: PartSetHeader{blockPartsTotal, CRandBytes(32)}} |
|
|
|
privAccounts[67].Sign(vote) |
|
|
|
voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[67].SignVoteUnsafe(vote) |
|
|
|
voteSet.Add(privValidators[67].Address, vote) |
|
|
|
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") |
|
|
@ -131,8 +132,8 @@ func Test2_3MajorityRedux(t *testing.T) { |
|
|
|
// 69th validator voted for different BlockParts Total
|
|
|
|
{ |
|
|
|
vote := &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: blockHash, BlockParts: PartSetHeader{blockPartsTotal + 1, blockParts.Hash}} |
|
|
|
privAccounts[68].Sign(vote) |
|
|
|
voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[68].SignVoteUnsafe(vote) |
|
|
|
voteSet.Add(privValidators[68].Address, vote) |
|
|
|
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") |
|
|
@ -142,8 +143,8 @@ func Test2_3MajorityRedux(t *testing.T) { |
|
|
|
// 70th validator voted for different BlockHash
|
|
|
|
{ |
|
|
|
vote := &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: CRandBytes(32), BlockParts: blockParts} |
|
|
|
privAccounts[69].Sign(vote) |
|
|
|
voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[69].SignVoteUnsafe(vote) |
|
|
|
voteSet.Add(privValidators[69].Address, vote) |
|
|
|
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") |
|
|
@ -153,8 +154,8 @@ func Test2_3MajorityRedux(t *testing.T) { |
|
|
|
// 71st validator voted for the right BlockHash & BlockParts
|
|
|
|
{ |
|
|
|
vote := voteProto.Copy() |
|
|
|
privAccounts[70].Sign(vote) |
|
|
|
voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[70].SignVoteUnsafe(vote) |
|
|
|
voteSet.Add(privValidators[70].Address, vote) |
|
|
|
hash, header, ok = voteSet.TwoThirdsMajority() |
|
|
|
if !bytes.Equal(hash, blockHash) || !header.Equals(blockParts) || !ok { |
|
|
|
t.Errorf("There should be 2/3 majority") |
|
|
@ -163,60 +164,60 @@ func Test2_3MajorityRedux(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestBadVotes(t *testing.T) { |
|
|
|
height, round := uint32(1), uint16(0) |
|
|
|
voteSet, _, privAccounts := makeVoteSet(height, round, VoteTypePrevote, 10, 1) |
|
|
|
height, round := uint(1), uint(0) |
|
|
|
voteSet, _, privValidators := makeVoteSet(height, round, VoteTypePrevote, 10, 1) |
|
|
|
|
|
|
|
// val0 votes for nil.
|
|
|
|
vote := &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: nil} |
|
|
|
privAccounts[0].Sign(vote) |
|
|
|
added, _, err := voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[0].SignVoteUnsafe(vote) |
|
|
|
added, _, err := voteSet.Add(privValidators[0].Address, vote) |
|
|
|
if !added || err != nil { |
|
|
|
t.Errorf("Expected Add(vote) to succeed") |
|
|
|
t.Errorf("Expected Add() to succeed") |
|
|
|
} |
|
|
|
|
|
|
|
// val0 votes again for some block.
|
|
|
|
vote = &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: CRandBytes(32)} |
|
|
|
privAccounts[0].Sign(vote) |
|
|
|
added, _, err = voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[0].SignVoteUnsafe(vote) |
|
|
|
added, _, err = voteSet.Add(privValidators[0].Address, vote) |
|
|
|
if added || err == nil { |
|
|
|
t.Errorf("Expected Add(vote) to fail, dupeout.") |
|
|
|
t.Errorf("Expected Add() to fail, dupeout.") |
|
|
|
} |
|
|
|
|
|
|
|
// val1 votes on another height
|
|
|
|
vote = &Vote{Height: height + 1, Round: round, Type: VoteTypePrevote, BlockHash: nil} |
|
|
|
privAccounts[1].Sign(vote) |
|
|
|
added, _, err = voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[1].SignVoteUnsafe(vote) |
|
|
|
added, _, err = voteSet.Add(privValidators[1].Address, vote) |
|
|
|
if added { |
|
|
|
t.Errorf("Expected Add(vote) to fail, wrong height") |
|
|
|
t.Errorf("Expected Add() to fail, wrong height") |
|
|
|
} |
|
|
|
|
|
|
|
// val2 votes on another round
|
|
|
|
vote = &Vote{Height: height, Round: round + 1, Type: VoteTypePrevote, BlockHash: nil} |
|
|
|
privAccounts[2].Sign(vote) |
|
|
|
added, _, err = voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[2].SignVoteUnsafe(vote) |
|
|
|
added, _, err = voteSet.Add(privValidators[2].Address, vote) |
|
|
|
if added { |
|
|
|
t.Errorf("Expected Add(vote) to fail, wrong round") |
|
|
|
t.Errorf("Expected Add() to fail, wrong round") |
|
|
|
} |
|
|
|
|
|
|
|
// val3 votes of another type.
|
|
|
|
vote = &Vote{Height: height, Round: round, Type: VoteTypePrecommit, BlockHash: nil} |
|
|
|
privAccounts[3].Sign(vote) |
|
|
|
added, _, err = voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[3].SignVoteUnsafe(vote) |
|
|
|
added, _, err = voteSet.Add(privValidators[3].Address, vote) |
|
|
|
if added { |
|
|
|
t.Errorf("Expected Add(vote) to fail, wrong type") |
|
|
|
t.Errorf("Expected Add() to fail, wrong type") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func TestAddCommitsToPrevoteVotes(t *testing.T) { |
|
|
|
height, round := uint32(2), uint16(5) |
|
|
|
voteSet, _, privAccounts := makeVoteSet(height, round, VoteTypePrevote, 10, 1) |
|
|
|
height, round := uint(2), uint(5) |
|
|
|
voteSet, _, privValidators := makeVoteSet(height, round, VoteTypePrevote, 10, 1) |
|
|
|
|
|
|
|
// val0, val1, val2, val3, val4, val5 vote for nil.
|
|
|
|
voteProto := &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: nil} |
|
|
|
for i := 0; i < 6; i++ { |
|
|
|
vote := voteProto.Copy() |
|
|
|
privAccounts[i].Sign(vote) |
|
|
|
voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[i].SignVoteUnsafe(vote) |
|
|
|
voteSet.Add(privValidators[i].Address, vote) |
|
|
|
} |
|
|
|
hash, header, ok := voteSet.TwoThirdsMajority() |
|
|
|
if hash != nil || !header.IsZero() || ok { |
|
|
@ -225,42 +226,42 @@ func TestAddCommitsToPrevoteVotes(t *testing.T) { |
|
|
|
|
|
|
|
// Attempt to add a commit from val6 at a previous height
|
|
|
|
vote := &Vote{Height: height - 1, Round: round, Type: VoteTypeCommit, BlockHash: nil} |
|
|
|
privAccounts[6].Sign(vote) |
|
|
|
added, _, _ := voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[6].SignVoteUnsafe(vote) |
|
|
|
added, _, _ := voteSet.Add(privValidators[6].Address, vote) |
|
|
|
if added { |
|
|
|
t.Errorf("Expected Add(vote) to fail, wrong height.") |
|
|
|
t.Errorf("Expected Add() to fail, wrong height.") |
|
|
|
} |
|
|
|
|
|
|
|
// Attempt to add a commit from val6 at a later round
|
|
|
|
vote = &Vote{Height: height, Round: round + 1, Type: VoteTypeCommit, BlockHash: nil} |
|
|
|
privAccounts[6].Sign(vote) |
|
|
|
added, _, _ = voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[6].SignVoteUnsafe(vote) |
|
|
|
added, _, _ = voteSet.Add(privValidators[6].Address, vote) |
|
|
|
if added { |
|
|
|
t.Errorf("Expected Add(vote) to fail, cannot add future round vote.") |
|
|
|
t.Errorf("Expected Add() to fail, cannot add future round vote.") |
|
|
|
} |
|
|
|
|
|
|
|
// Attempt to add a commit from val6 for currrent height/round.
|
|
|
|
vote = &Vote{Height: height, Round: round, Type: VoteTypeCommit, BlockHash: nil} |
|
|
|
privAccounts[6].Sign(vote) |
|
|
|
added, _, err := voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[6].SignVoteUnsafe(vote) |
|
|
|
added, _, err := voteSet.Add(privValidators[6].Address, vote) |
|
|
|
if added || err == nil { |
|
|
|
t.Errorf("Expected Add(vote) to fail, only prior round commits can be added.") |
|
|
|
t.Errorf("Expected Add() to fail, only prior round commits can be added.") |
|
|
|
} |
|
|
|
|
|
|
|
// Add commit from val6 at a previous round
|
|
|
|
vote = &Vote{Height: height, Round: round - 1, Type: VoteTypeCommit, BlockHash: nil} |
|
|
|
privAccounts[6].Sign(vote) |
|
|
|
added, _, err = voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[6].SignVoteUnsafe(vote) |
|
|
|
added, _, err = voteSet.Add(privValidators[6].Address, vote) |
|
|
|
if !added || err != nil { |
|
|
|
t.Errorf("Expected Add(vote) to succeed, commit for prior rounds are relevant.") |
|
|
|
t.Errorf("Expected Add() to succeed, commit for prior rounds are relevant.") |
|
|
|
} |
|
|
|
|
|
|
|
// Also add commit from val7 for previous round.
|
|
|
|
vote = &Vote{Height: height, Round: round - 2, Type: VoteTypeCommit, BlockHash: nil} |
|
|
|
privAccounts[7].Sign(vote) |
|
|
|
added, _, err = voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[7].SignVoteUnsafe(vote) |
|
|
|
added, _, err = voteSet.Add(privValidators[7].Address, vote) |
|
|
|
if !added || err != nil { |
|
|
|
t.Errorf("Expected Add(vote) to succeed. err: %v", err) |
|
|
|
t.Errorf("Expected Add() to succeed. err: %v", err) |
|
|
|
} |
|
|
|
|
|
|
|
// We should have 2/3 majority
|
|
|
@ -272,8 +273,8 @@ func TestAddCommitsToPrevoteVotes(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestMakeValidation(t *testing.T) { |
|
|
|
height, round := uint32(1), uint16(0) |
|
|
|
voteSet, _, privAccounts := makeVoteSet(height, round, VoteTypeCommit, 10, 1) |
|
|
|
height, round := uint(1), uint(0) |
|
|
|
voteSet, _, privValidators := makeVoteSet(height, round, VoteTypeCommit, 10, 1) |
|
|
|
blockHash, blockParts := CRandBytes(32), PartSetHeader{123, CRandBytes(32)} |
|
|
|
|
|
|
|
// 6 out of 10 voted for some block.
|
|
|
@ -281,8 +282,8 @@ func TestMakeValidation(t *testing.T) { |
|
|
|
BlockHash: blockHash, BlockParts: blockParts} |
|
|
|
for i := 0; i < 6; i++ { |
|
|
|
vote := voteProto.Copy() |
|
|
|
privAccounts[i].Sign(vote) |
|
|
|
voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[i].SignVoteUnsafe(vote) |
|
|
|
voteSet.Add(privValidators[i].Address, vote) |
|
|
|
} |
|
|
|
|
|
|
|
// MakeValidation should fail.
|
|
|
@ -293,15 +294,15 @@ func TestMakeValidation(t *testing.T) { |
|
|
|
vote := &Vote{Height: height, Round: round, Type: VoteTypeCommit, |
|
|
|
BlockHash: CRandBytes(32), |
|
|
|
BlockParts: PartSetHeader{123, CRandBytes(32)}} |
|
|
|
privAccounts[6].Sign(vote) |
|
|
|
voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[6].SignVoteUnsafe(vote) |
|
|
|
voteSet.Add(privValidators[6].Address, vote) |
|
|
|
} |
|
|
|
|
|
|
|
// The 8th voted like everyone else.
|
|
|
|
{ |
|
|
|
vote := voteProto.Copy() |
|
|
|
privAccounts[7].Sign(vote) |
|
|
|
voteSet.Add(vote) |
|
|
|
vote.Signature = privValidators[7].SignVoteUnsafe(vote) |
|
|
|
voteSet.Add(privValidators[7].Address, vote) |
|
|
|
} |
|
|
|
|
|
|
|
validation := voteSet.MakeValidation() |
|
|
@ -312,24 +313,8 @@ func TestMakeValidation(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
// Ensure that Validation commits are ordered.
|
|
|
|
for i, rsig := range validation.Commits { |
|
|
|
if i < 6 || i == 7 { |
|
|
|
if rsig.Round != round { |
|
|
|
t.Errorf("Expected round %v but got %v", round, rsig.Round) |
|
|
|
} |
|
|
|
if rsig.SignerId != uint64(i) { |
|
|
|
t.Errorf("Validation commit signer out of order. Expected %v, got %v", i, rsig.Signature) |
|
|
|
} |
|
|
|
vote := &Vote{Height: height, Round: rsig.Round, Type: VoteTypeCommit, |
|
|
|
BlockHash: blockHash, BlockParts: blockParts, |
|
|
|
Signature: rsig.Signature} |
|
|
|
if !privAccounts[i].Verify(vote) { |
|
|
|
t.Errorf("Validation commit did not verify") |
|
|
|
} |
|
|
|
} else { |
|
|
|
if !rsig.IsZero() { |
|
|
|
t.Errorf("Expected zero RoundSignature for the rest") |
|
|
|
} |
|
|
|
} |
|
|
|
if err := validation.ValidateBasic(); err != nil { |
|
|
|
t.Errorf("Error in Validation.ValidateBasic(): %v", err) |
|
|
|
} |
|
|
|
|
|
|
|
} |