tendermint/consensus/pol_test.go

213 lines
6.4 KiB
Go
Raw Normal View History

2014-10-17 16:48:27 -07:00
package consensus
import (
2015-04-01 17:30:16 -07:00
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"
2014-10-17 16:48:27 -07:00
"bytes"
"testing"
)
// NOTE: see consensus/test.go for common test methods.
2014-12-17 01:37:13 -08:00
// Convenience method.
// Signs the vote and sets the POL's vote at the desired index
// Returns the POLVoteSignature pointer, so you can modify it afterwards.
func signAddPOLVoteSignature(val *sm.PrivValidator, valSet *sm.ValidatorSet, vote *types.Vote, pol *POL) *POLVoteSignature {
vote = vote.Copy()
err := val.SignVote(vote)
if err != nil {
panic(err)
}
2014-12-17 01:37:13 -08:00
idx, _ := valSet.GetByAddress(val.Address) // now we have the index
pol.Votes[idx] = POLVoteSignature{vote.Round, vote.Signature}
2014-12-17 01:37:13 -08:00
return &pol.Votes[idx]
}
2014-10-17 16:48:27 -07:00
func TestVerifyVotes(t *testing.T) {
2014-12-17 01:37:13 -08:00
height, round := uint(1), uint(0)
_, valSet, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
2014-10-17 16:48:27 -07:00
// Make a POL with -2/3 votes.
blockHash := RandBytes(32)
pol := &POL{
2014-10-31 18:35:38 -07:00
Height: height, Round: round, BlockHash: blockHash,
2014-12-17 01:37:13 -08:00
Votes: make([]POLVoteSignature, valSet.Size()),
2014-10-17 16:48:27 -07:00
}
voteProto := &types.Vote{
Height: height, Round: round, Type: types.VoteTypePrevote, BlockHash: blockHash,
2014-10-17 16:48:27 -07:00
}
for i := 0; i < 6; i++ {
2014-12-17 01:37:13 -08:00
signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
2014-10-17 16:48:27 -07:00
}
// Check that validation fails.
if err := pol.Verify(valSet); err == nil {
t.Errorf("Expected POL.Verify() to fail, not enough votes.")
}
2014-12-17 01:37:13 -08:00
// Insert another vote to make +2/3
signAddPOLVoteSignature(privValidators[7], valSet, voteProto, pol)
2014-10-17 16:48:27 -07:00
// Check that validation succeeds.
if err := pol.Verify(valSet); err != nil {
2014-12-17 01:37:13 -08:00
t.Errorf("POL.Verify() failed: %v", err)
2014-10-17 16:48:27 -07:00
}
}
func TestVerifyInvalidVote(t *testing.T) {
2014-12-17 01:37:13 -08:00
height, round := uint(1), uint(0)
_, valSet, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
2014-10-17 16:48:27 -07:00
// Make a POL with +2/3 votes with the wrong signature.
blockHash := RandBytes(32)
pol := &POL{
2014-10-31 18:35:38 -07:00
Height: height, Round: round, BlockHash: blockHash,
2014-12-17 01:37:13 -08:00
Votes: make([]POLVoteSignature, valSet.Size()),
2014-10-17 16:48:27 -07:00
}
voteProto := &types.Vote{
Height: height, Round: round, Type: types.VoteTypePrevote, BlockHash: blockHash,
2014-10-17 16:48:27 -07:00
}
for i := 0; i < 7; i++ {
2014-12-17 01:37:13 -08:00
polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
polVoteSig.Signature[0] += byte(0x01) // mutated!
2014-10-17 16:48:27 -07:00
}
// Check that validation fails.
if err := pol.Verify(valSet); err == nil {
t.Errorf("Expected POL.Verify() to fail, wrong signatures.")
}
}
func TestVerifyCommits(t *testing.T) {
2014-12-17 01:37:13 -08:00
height, round := uint(1), uint(2)
_, valSet, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
2014-10-17 16:48:27 -07:00
// Make a POL with +2/3 votes.
blockHash := RandBytes(32)
pol := &POL{
2014-10-31 18:35:38 -07:00
Height: height, Round: round, BlockHash: blockHash,
2014-12-17 01:37:13 -08:00
Votes: make([]POLVoteSignature, valSet.Size()),
2014-10-17 16:48:27 -07:00
}
voteProto := &types.Vote{
Height: height, Round: round - 1, Type: types.VoteTypeCommit, BlockHash: blockHash,
2014-10-17 16:48:27 -07:00
}
for i := 0; i < 7; i++ {
2014-12-17 01:37:13 -08:00
signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
2014-10-17 16:48:27 -07:00
}
// Check that validation succeeds.
if err := pol.Verify(valSet); err != nil {
2014-12-17 01:37:13 -08:00
t.Errorf("POL.Verify() failed: %v", err)
2014-10-17 16:48:27 -07:00
}
}
func TestVerifyInvalidCommits(t *testing.T) {
2014-12-17 01:37:13 -08:00
height, round := uint(1), uint(2)
_, valSet, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
2014-10-17 16:48:27 -07:00
// Make a POL with +2/3 votes with the wrong signature.
blockHash := RandBytes(32)
pol := &POL{
2014-10-31 18:35:38 -07:00
Height: height, Round: round, BlockHash: blockHash,
2014-12-17 01:37:13 -08:00
Votes: make([]POLVoteSignature, valSet.Size()),
2014-10-17 16:48:27 -07:00
}
voteProto := &types.Vote{
Height: height, Round: round - 1, Type: types.VoteTypeCommit, BlockHash: blockHash,
2014-10-17 16:48:27 -07:00
}
for i := 0; i < 7; i++ {
2014-12-17 01:37:13 -08:00
polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
polVoteSig.Signature[0] += byte(0x01)
2014-10-17 16:48:27 -07:00
}
// Check that validation fails.
if err := pol.Verify(valSet); err == nil {
t.Errorf("Expected POL.Verify() to fail, wrong signatures.")
}
}
func TestVerifyInvalidCommitRounds(t *testing.T) {
2014-12-17 01:37:13 -08:00
height, round := uint(1), uint(2)
_, valSet, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
2014-10-17 16:48:27 -07:00
// Make a POL with +2/3 commits for the current round.
blockHash := RandBytes(32)
pol := &POL{
2014-10-31 18:35:38 -07:00
Height: height, Round: round, BlockHash: blockHash,
2014-12-17 01:37:13 -08:00
Votes: make([]POLVoteSignature, valSet.Size()),
2014-10-17 16:48:27 -07:00
}
voteProto := &types.Vote{
Height: height, Round: round, Type: types.VoteTypeCommit, BlockHash: blockHash,
2014-10-17 16:48:27 -07:00
}
for i := 0; i < 7; i++ {
2014-12-17 01:37:13 -08:00
signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
2014-10-17 16:48:27 -07:00
}
// Check that validation fails.
if err := pol.Verify(valSet); err == nil {
t.Errorf("Expected POL.Verify() to fail, same round.")
}
}
func TestVerifyInvalidCommitRounds2(t *testing.T) {
2014-12-17 01:37:13 -08:00
height, round := uint(1), uint(2)
_, valSet, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
2014-10-17 16:48:27 -07:00
// Make a POL with +2/3 commits for future round.
blockHash := RandBytes(32)
pol := &POL{
2014-10-31 18:35:38 -07:00
Height: height, Round: round, BlockHash: blockHash,
2014-12-17 01:37:13 -08:00
Votes: make([]POLVoteSignature, valSet.Size()),
2014-10-17 16:48:27 -07:00
}
voteProto := &types.Vote{
Height: height, Round: round + 1, Type: types.VoteTypeCommit, BlockHash: blockHash,
2014-10-17 16:48:27 -07:00
}
for i := 0; i < 7; i++ {
2014-12-17 01:37:13 -08:00
polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
polVoteSig.Round += 1 // mutate round
2014-10-17 16:48:27 -07:00
}
// Check that validation fails.
if err := pol.Verify(valSet); err == nil {
t.Errorf("Expected POL.Verify() to fail, future round.")
}
}
func TestReadWrite(t *testing.T) {
2014-12-17 01:37:13 -08:00
height, round := uint(1), uint(2)
_, valSet, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
2014-10-17 16:48:27 -07:00
// Make a POL with +2/3 votes.
blockHash := RandBytes(32)
pol := &POL{
2014-10-31 18:35:38 -07:00
Height: height, Round: round, BlockHash: blockHash,
2014-12-17 01:37:13 -08:00
Votes: make([]POLVoteSignature, valSet.Size()),
2014-10-17 16:48:27 -07:00
}
voteProto := &types.Vote{
Height: height, Round: round, Type: types.VoteTypePrevote, BlockHash: blockHash,
2014-10-17 16:48:27 -07:00
}
for i := 0; i < 7; i++ {
2014-12-17 01:37:13 -08:00
signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
2014-10-17 16:48:27 -07:00
}
// Write it to a buffer.
2014-12-17 01:37:13 -08:00
buf, n, err := new(bytes.Buffer), new(int64), new(error)
binary.WriteBinary(pol, buf, n, err)
2014-12-17 01:37:13 -08:00
if *err != nil {
t.Fatalf("Failed to write POL: %v", *err)
2014-10-17 16:48:27 -07:00
}
// Read from buffer.
pol2 := binary.ReadBinary(&POL{}, buf, n, err).(*POL)
2014-12-17 01:37:13 -08:00
if *err != nil {
t.Fatalf("Failed to read POL: %v", *err)
2014-10-17 16:48:27 -07:00
}
// Check that validation succeeds.
if err := pol2.Verify(valSet); err != nil {
2014-12-17 01:37:13 -08:00
t.Errorf("POL.Verify() failed: %v", err)
2014-10-17 16:48:27 -07:00
}
}