privval: return error on getpubkey (#4534)

closes #3602

Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
This commit is contained in:
Marko
2020-03-12 11:10:36 +04:00
committed by GitHub
co-authored by Anton Kaliaev
parent 038aff1fdb
commit 48f073d796
30 changed files with 501 additions and 216 deletions
+3 -2
View File
@@ -475,9 +475,10 @@ func TestCommitToVoteSetWithVotesForNilBlock(t *testing.T) {
vi := 0
for n := range tc.blockIDs {
for i := 0; i < tc.numVotes[n]; i++ {
addr := vals[vi].GetPubKey().Address()
pubKey, err := vals[vi].GetPubKey()
require.NoError(t, err)
vote := &Vote{
ValidatorAddress: addr,
ValidatorAddress: pubKey.Address(),
ValidatorIndex: vi,
Height: height - 1,
Round: round,
+33 -30
View File
@@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/crypto/tmhash"
)
@@ -17,17 +18,20 @@ type voteData struct {
valid bool
}
func makeVote(val PrivValidator, chainID string, valIndex int, height int64, round, step int, blockID BlockID) *Vote {
addr := val.GetPubKey().Address()
func makeVote(
t *testing.T, val PrivValidator, chainID string, valIndex int, height int64, round, step int, blockID BlockID,
) *Vote {
pubKey, err := val.GetPubKey()
require.NoError(t, err)
v := &Vote{
ValidatorAddress: addr,
ValidatorAddress: pubKey.Address(),
ValidatorIndex: valIndex,
Height: height,
Round: round,
Type: SignedMsgType(step),
BlockID: blockID,
}
err := val.SignVote(chainID, v)
err = val.SignVote(chainID, v)
if err != nil {
panic(err)
}
@@ -45,28 +49,27 @@ func TestEvidence(t *testing.T) {
const chainID = "mychain"
vote1 := makeVote(val, chainID, 0, 10, 2, 1, blockID)
badVote := makeVote(val, chainID, 0, 10, 2, 1, blockID)
vote1 := makeVote(t, val, chainID, 0, 10, 2, 1, blockID)
badVote := makeVote(t, val, chainID, 0, 10, 2, 1, blockID)
err := val2.SignVote(chainID, badVote)
if err != nil {
panic(err)
}
assert.NoError(t, err)
cases := []voteData{
{vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID2), true}, // different block ids
{vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID3), true},
{vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID4), true},
{vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID), false}, // wrong block id
{vote1, makeVote(val, "mychain2", 0, 10, 2, 1, blockID2), false}, // wrong chain id
{vote1, makeVote(val, chainID, 1, 10, 2, 1, blockID2), false}, // wrong val index
{vote1, makeVote(val, chainID, 0, 11, 2, 1, blockID2), false}, // wrong height
{vote1, makeVote(val, chainID, 0, 10, 3, 1, blockID2), false}, // wrong round
{vote1, makeVote(val, chainID, 0, 10, 2, 2, blockID2), false}, // wrong step
{vote1, makeVote(val2, chainID, 0, 10, 2, 1, blockID), false}, // wrong validator
{vote1, makeVote(t, val, chainID, 0, 10, 2, 1, blockID2), true}, // different block ids
{vote1, makeVote(t, val, chainID, 0, 10, 2, 1, blockID3), true},
{vote1, makeVote(t, val, chainID, 0, 10, 2, 1, blockID4), true},
{vote1, makeVote(t, val, chainID, 0, 10, 2, 1, blockID), false}, // wrong block id
{vote1, makeVote(t, val, "mychain2", 0, 10, 2, 1, blockID2), false}, // wrong chain id
{vote1, makeVote(t, val, chainID, 1, 10, 2, 1, blockID2), false}, // wrong val index
{vote1, makeVote(t, val, chainID, 0, 11, 2, 1, blockID2), false}, // wrong height
{vote1, makeVote(t, val, chainID, 0, 10, 3, 1, blockID2), false}, // wrong round
{vote1, makeVote(t, val, chainID, 0, 10, 2, 2, blockID2), false}, // wrong step
{vote1, makeVote(t, val2, chainID, 0, 10, 2, 1, blockID), false}, // wrong validator
{vote1, badVote, false}, // signed by wrong key
}
pubKey := val.GetPubKey()
pubKey, err := val.GetPubKey()
require.NoError(t, err)
for _, c := range cases {
ev := &DuplicateVoteEvidence{
VoteA: c.vote1,
@@ -81,14 +84,14 @@ func TestEvidence(t *testing.T) {
}
func TestDuplicatedVoteEvidence(t *testing.T) {
ev := randomDuplicatedVoteEvidence()
ev := randomDuplicatedVoteEvidence(t)
assert.True(t, ev.Equal(ev))
assert.False(t, ev.Equal(&DuplicateVoteEvidence{}))
}
func TestEvidenceList(t *testing.T) {
ev := randomDuplicatedVoteEvidence()
ev := randomDuplicatedVoteEvidence(t)
evl := EvidenceList([]Evidence{ev})
assert.NotNil(t, evl.Hash())
@@ -103,8 +106,8 @@ func TestMaxEvidenceBytes(t *testing.T) {
const chainID = "mychain"
ev := &DuplicateVoteEvidence{
PubKey: secp256k1.GenPrivKey().PubKey(), // use secp because it's pubkey is longer
VoteA: makeVote(val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, math.MaxInt64, blockID),
VoteB: makeVote(val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, math.MaxInt64, blockID2),
VoteA: makeVote(t, val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, math.MaxInt64, blockID),
VoteB: makeVote(t, val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, math.MaxInt64, blockID2),
}
bz, err := cdc.MarshalBinaryLengthPrefixed(ev)
@@ -113,14 +116,14 @@ func TestMaxEvidenceBytes(t *testing.T) {
assert.EqualValues(t, MaxEvidenceBytes, len(bz))
}
func randomDuplicatedVoteEvidence() *DuplicateVoteEvidence {
func randomDuplicatedVoteEvidence(t *testing.T) *DuplicateVoteEvidence {
val := NewMockPV()
blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))
const chainID = "mychain"
return &DuplicateVoteEvidence{
VoteA: makeVote(val, chainID, 0, 10, 2, 1, blockID),
VoteB: makeVote(val, chainID, 0, 10, 2, 1, blockID2),
VoteA: makeVote(t, val, chainID, 0, 10, 2, 1, blockID),
VoteB: makeVote(t, val, chainID, 0, 10, 2, 1, blockID2),
}
}
@@ -143,7 +146,7 @@ func TestDuplicateVoteEvidenceValidation(t *testing.T) {
ev.VoteB = nil
}, true},
{"Invalid vote type", func(ev *DuplicateVoteEvidence) {
ev.VoteA = makeVote(val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, 0, blockID2)
ev.VoteA = makeVote(t, val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, 0, blockID2)
}, true},
{"Invalid vote order", func(ev *DuplicateVoteEvidence) {
swap := ev.VoteA.Copy()
@@ -155,8 +158,8 @@ func TestDuplicateVoteEvidenceValidation(t *testing.T) {
tc := tc
t.Run(tc.testName, func(t *testing.T) {
pk := secp256k1.GenPrivKey().PubKey()
vote1 := makeVote(val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, 0x02, blockID)
vote2 := makeVote(val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, 0x02, blockID2)
vote1 := makeVote(t, val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, 0x02, blockID)
vote2 := makeVote(t, val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, 0x02, blockID2)
ev := NewDuplicateVoteEvidence(pk, vote1, vote2)
tc.malleateEvidence(ev)
assert.Equal(t, tc.expectErr, ev.ValidateBasic() != nil, "Validate Basic had an unexpected result")
+15 -7
View File
@@ -12,8 +12,7 @@ import (
// PrivValidator defines the functionality of a local Tendermint validator
// that signs votes and proposals, and never double signs.
type PrivValidator interface {
// TODO: Extend the interface to return errors too. Issue: https://github.com/tendermint/tendermint/issues/3602
GetPubKey() crypto.PubKey
GetPubKey() (crypto.PubKey, error)
SignVote(chainID string, vote *Vote) error
SignProposal(chainID string, proposal *Proposal) error
@@ -29,7 +28,16 @@ func (pvs PrivValidatorsByAddress) Len() int {
}
func (pvs PrivValidatorsByAddress) Less(i, j int) bool {
return bytes.Compare(pvs[i].GetPubKey().Address(), pvs[j].GetPubKey().Address()) == -1
pvi, err := pvs[i].GetPubKey()
if err != nil {
panic(err)
}
pvj, err := pvs[j].GetPubKey()
if err != nil {
panic(err)
}
return bytes.Compare(pvi.Address(), pvj.Address()) == -1
}
func (pvs PrivValidatorsByAddress) Swap(i, j int) {
@@ -61,8 +69,8 @@ func NewMockPVWithParams(privKey crypto.PrivKey, breakProposalSigning, breakVote
}
// Implements PrivValidator.
func (pv MockPV) GetPubKey() crypto.PubKey {
return pv.PrivKey.PubKey()
func (pv MockPV) GetPubKey() (crypto.PubKey, error) {
return pv.PrivKey.PubKey(), nil
}
// Implements PrivValidator.
@@ -97,8 +105,8 @@ func (pv MockPV) SignProposal(chainID string, proposal *Proposal) error {
// String returns a string representation of the MockPV.
func (pv MockPV) String() string {
addr := pv.GetPubKey().Address()
return fmt.Sprintf("MockPV{%v}", addr)
mpv, _ := pv.GetPubKey() // mockPV will never return an error, ignored here
return fmt.Sprintf("MockPV{%v}", mpv.Address())
}
// XXX: Implement.
+6 -4
View File
@@ -45,7 +45,8 @@ func TestProposalString(t *testing.T) {
func TestProposalVerifySignature(t *testing.T) {
privVal := NewMockPV()
pubKey := privVal.GetPubKey()
pubKey, err := privVal.GetPubKey()
require.NoError(t, err)
prop := NewProposal(
4, 2, 2,
@@ -53,7 +54,7 @@ func TestProposalVerifySignature(t *testing.T) {
signBytes := prop.SignBytes("test_chain_id")
// sign it
err := privVal.SignProposal("test_chain_id", prop)
err = privVal.SignProposal("test_chain_id", prop)
require.NoError(t, err)
// verify the same proposal
@@ -93,8 +94,9 @@ func BenchmarkProposalSign(b *testing.B) {
func BenchmarkProposalVerifySignature(b *testing.B) {
privVal := NewMockPV()
err := privVal.SignProposal("test_chain_id", testProposal)
require.Nil(b, err)
pubKey := privVal.GetPubKey()
require.NoError(b, err)
pubKey, err := privVal.GetPubKey()
require.NoError(b, err)
for i := 0; i < b.N; i++ {
pubKey.VerifyBytes(testProposal.SignBytes("test_chain_id"), testProposal.Signature)
+5 -3
View File
@@ -6,6 +6,7 @@ import (
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
amino "github.com/tendermint/go-amino"
abci "github.com/tendermint/tendermint/abci/types"
@@ -131,11 +132,12 @@ func TestABCIEvidence(t *testing.T) {
blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))
const chainID = "mychain"
pubKey := val.GetPubKey()
pubKey, err := val.GetPubKey()
require.NoError(t, err)
ev := &DuplicateVoteEvidence{
PubKey: pubKey,
VoteA: makeVote(val, chainID, 0, 10, 2, 1, blockID),
VoteB: makeVote(val, chainID, 0, 10, 2, 1, blockID2),
VoteA: makeVote(t, val, chainID, 0, 10, 2, 1, blockID),
VoteB: makeVote(t, val, chainID, 0, 10, 2, 1, blockID2),
}
abciEv := TM2PB.Evidence(
ev,
+13 -4
View File
@@ -2,6 +2,8 @@ package types
import (
"time"
"github.com/pkg/errors"
)
func MakeCommit(blockID BlockID, height int64, round int,
@@ -9,9 +11,12 @@ func MakeCommit(blockID BlockID, height int64, round int,
// all sign
for i := 0; i < len(validators); i++ {
addr := validators[i].GetPubKey().Address()
pubKey, err := validators[i].GetPubKey()
if err != nil {
return nil, errors.Wrap(err, "can't get pubkey")
}
vote := &Vote{
ValidatorAddress: addr,
ValidatorAddress: pubKey.Address(),
ValidatorIndex: i,
Height: height,
Round: round,
@@ -20,7 +25,7 @@ func MakeCommit(blockID BlockID, height int64, round int,
Timestamp: now,
}
_, err := signAddVote(validators[i], vote, voteSet)
_, err = signAddVote(validators[i], vote, voteSet)
if err != nil {
return nil, err
}
@@ -45,7 +50,11 @@ func MakeVote(
chainID string,
now time.Time,
) (*Vote, error) {
addr := privVal.GetPubKey().Address()
pubKey, err := privVal.GetPubKey()
if err != nil {
return nil, errors.Wrap(err, "can't get pubkey")
}
addr := pubKey.Address()
idx, _ := valSet.GetByAddress(addr)
vote := &Vote{
ValidatorAddress: addr,
+4 -1
View File
@@ -105,7 +105,10 @@ func RandValidator(randPower bool, minPower int64) (*Validator, PrivValidator) {
if randPower {
votePower += int64(tmrand.Uint32())
}
pubKey := privVal.GetPubKey()
pubKey, err := privVal.GetPubKey()
if err != nil {
panic(fmt.Errorf("could not retrieve pubkey %w", err))
}
val := NewValidator(pubKey, votePower)
return val, privVal
}
+86 -37
View File
@@ -5,6 +5,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto"
tmrand "github.com/tendermint/tendermint/libs/rand"
@@ -73,7 +74,10 @@ func TestAddVote(t *testing.T) {
// t.Logf(">> %v", voteSet)
val0Addr := val0.GetPubKey().Address()
val0p, err := val0.GetPubKey()
require.NoError(t, err)
val0Addr := val0p.Address()
if voteSet.GetByAddress(val0Addr) != nil {
t.Errorf("expected GetByAddress(val0.Address) to be nil")
}
@@ -94,7 +98,7 @@ func TestAddVote(t *testing.T) {
Timestamp: tmtime.Now(),
BlockID: BlockID{nil, PartSetHeader{}},
}
_, err := signAddVote(val0, vote, voteSet)
_, err = signAddVote(val0, vote, voteSet)
if err != nil {
t.Error(err)
}
@@ -126,9 +130,11 @@ func Test2_3Majority(t *testing.T) {
}
// 6 out of 10 voted for nil.
for i := 0; i < 6; i++ {
addr := privValidators[i].GetPubKey().Address()
pubKey, err := privValidators[i].GetPubKey()
require.NoError(t, err)
addr := pubKey.Address()
vote := withValidator(voteProto, addr, i)
_, err := signAddVote(privValidators[i], vote, voteSet)
_, err = signAddVote(privValidators[i], vote, voteSet)
if err != nil {
t.Error(err)
}
@@ -140,9 +146,11 @@ func Test2_3Majority(t *testing.T) {
// 7th validator voted for some blockhash
{
addr := privValidators[6].GetPubKey().Address()
pubKey, err := privValidators[6].GetPubKey()
require.NoError(t, err)
addr := pubKey.Address()
vote := withValidator(voteProto, addr, 6)
_, err := signAddVote(privValidators[6], withBlockHash(vote, tmrand.Bytes(32)), voteSet)
_, err = signAddVote(privValidators[6], withBlockHash(vote, tmrand.Bytes(32)), voteSet)
if err != nil {
t.Error(err)
}
@@ -154,9 +162,11 @@ func Test2_3Majority(t *testing.T) {
// 8th validator voted for nil.
{
addr := privValidators[7].GetPubKey().Address()
pubKey, err := privValidators[7].GetPubKey()
require.NoError(t, err)
addr := pubKey.Address()
vote := withValidator(voteProto, addr, 7)
_, err := signAddVote(privValidators[7], vote, voteSet)
_, err = signAddVote(privValidators[7], vote, voteSet)
if err != nil {
t.Error(err)
}
@@ -187,9 +197,11 @@ func Test2_3MajorityRedux(t *testing.T) {
// 66 out of 100 voted for nil.
for i := 0; i < 66; i++ {
addr := privValidators[i].GetPubKey().Address()
pubKey, err := privValidators[i].GetPubKey()
require.NoError(t, err)
addr := pubKey.Address()
vote := withValidator(voteProto, addr, i)
_, err := signAddVote(privValidators[i], vote, voteSet)
_, err = signAddVote(privValidators[i], vote, voteSet)
if err != nil {
t.Error(err)
}
@@ -201,9 +213,11 @@ func Test2_3MajorityRedux(t *testing.T) {
// 67th validator voted for nil
{
adrr := privValidators[66].GetPubKey().Address()
pubKey, err := privValidators[66].GetPubKey()
require.NoError(t, err)
adrr := pubKey.Address()
vote := withValidator(voteProto, adrr, 66)
_, err := signAddVote(privValidators[66], withBlockHash(vote, nil), voteSet)
_, err = signAddVote(privValidators[66], withBlockHash(vote, nil), voteSet)
if err != nil {
t.Error(err)
}
@@ -215,10 +229,12 @@ func Test2_3MajorityRedux(t *testing.T) {
// 68th validator voted for a different BlockParts PartSetHeader
{
addr := privValidators[67].GetPubKey().Address()
pubKey, err := privValidators[67].GetPubKey()
require.NoError(t, err)
addr := pubKey.Address()
vote := withValidator(voteProto, addr, 67)
blockPartsHeader := PartSetHeader{blockPartsTotal, crypto.CRandBytes(32)}
_, err := signAddVote(privValidators[67], withBlockPartsHeader(vote, blockPartsHeader), voteSet)
_, err = signAddVote(privValidators[67], withBlockPartsHeader(vote, blockPartsHeader), voteSet)
if err != nil {
t.Error(err)
}
@@ -230,10 +246,12 @@ func Test2_3MajorityRedux(t *testing.T) {
// 69th validator voted for different BlockParts Total
{
addr := privValidators[68].GetPubKey().Address()
pubKey, err := privValidators[68].GetPubKey()
require.NoError(t, err)
addr := pubKey.Address()
vote := withValidator(voteProto, addr, 68)
blockPartsHeader := PartSetHeader{blockPartsTotal + 1, blockPartsHeader.Hash}
_, err := signAddVote(privValidators[68], withBlockPartsHeader(vote, blockPartsHeader), voteSet)
_, err = signAddVote(privValidators[68], withBlockPartsHeader(vote, blockPartsHeader), voteSet)
if err != nil {
t.Error(err)
}
@@ -245,9 +263,11 @@ func Test2_3MajorityRedux(t *testing.T) {
// 70th validator voted for different BlockHash
{
addr := privValidators[69].GetPubKey().Address()
pubKey, err := privValidators[69].GetPubKey()
require.NoError(t, err)
addr := pubKey.Address()
vote := withValidator(voteProto, addr, 69)
_, err := signAddVote(privValidators[69], withBlockHash(vote, tmrand.Bytes(32)), voteSet)
_, err = signAddVote(privValidators[69], withBlockHash(vote, tmrand.Bytes(32)), voteSet)
if err != nil {
t.Error(err)
}
@@ -259,9 +279,11 @@ func Test2_3MajorityRedux(t *testing.T) {
// 71st validator voted for the right BlockHash & BlockPartsHeader
{
addr := privValidators[70].GetPubKey().Address()
pubKey, err := privValidators[70].GetPubKey()
require.NoError(t, err)
addr := pubKey.Address()
vote := withValidator(voteProto, addr, 70)
_, err := signAddVote(privValidators[70], vote, voteSet)
_, err = signAddVote(privValidators[70], vote, voteSet)
if err != nil {
t.Error(err)
}
@@ -288,7 +310,9 @@ func TestBadVotes(t *testing.T) {
// val0 votes for nil.
{
addr := privValidators[0].GetPubKey().Address()
pubKey, err := privValidators[0].GetPubKey()
require.NoError(t, err)
addr := pubKey.Address()
vote := withValidator(voteProto, addr, 0)
added, err := signAddVote(privValidators[0], vote, voteSet)
if !added || err != nil {
@@ -298,7 +322,9 @@ func TestBadVotes(t *testing.T) {
// val0 votes again for some block.
{
addr := privValidators[0].GetPubKey().Address()
pubKey, err := privValidators[0].GetPubKey()
require.NoError(t, err)
addr := pubKey.Address()
vote := withValidator(voteProto, addr, 0)
added, err := signAddVote(privValidators[0], withBlockHash(vote, tmrand.Bytes(32)), voteSet)
if added || err == nil {
@@ -308,7 +334,9 @@ func TestBadVotes(t *testing.T) {
// val1 votes on another height
{
addr := privValidators[1].GetPubKey().Address()
pubKey, err := privValidators[1].GetPubKey()
require.NoError(t, err)
addr := pubKey.Address()
vote := withValidator(voteProto, addr, 1)
added, err := signAddVote(privValidators[1], withHeight(vote, height+1), voteSet)
if added || err == nil {
@@ -318,7 +346,9 @@ func TestBadVotes(t *testing.T) {
// val2 votes on another round
{
addr := privValidators[2].GetPubKey().Address()
pubKey, err := privValidators[2].GetPubKey()
require.NoError(t, err)
addr := pubKey.Address()
vote := withValidator(voteProto, addr, 2)
added, err := signAddVote(privValidators[2], withRound(vote, round+1), voteSet)
if added || err == nil {
@@ -328,7 +358,9 @@ func TestBadVotes(t *testing.T) {
// val3 votes of another type.
{
addr := privValidators[3].GetPubKey().Address()
pubKey, err := privValidators[3].GetPubKey()
require.NoError(t, err)
addr := pubKey.Address()
vote := withValidator(voteProto, addr, 3)
added, err := signAddVote(privValidators[3], withType(vote, byte(PrecommitType)), voteSet)
if added || err == nil {
@@ -353,7 +385,10 @@ func TestConflicts(t *testing.T) {
BlockID: BlockID{nil, PartSetHeader{}},
}
val0Addr := privValidators[0].GetPubKey().Address()
val0, err := privValidators[0].GetPubKey()
require.NoError(t, err)
val0Addr := val0.Address()
// val0 votes for nil.
{
vote := withValidator(voteProto, val0Addr, 0)
@@ -407,7 +442,9 @@ func TestConflicts(t *testing.T) {
// val1 votes for blockHash1.
{
addr := privValidators[1].GetPubKey().Address()
pv, err := privValidators[1].GetPubKey()
assert.NoError(t, err)
addr := pv.Address()
vote := withValidator(voteProto, addr, 1)
added, err := signAddVote(privValidators[1], withBlockHash(vote, blockHash1), voteSet)
if !added || err != nil {
@@ -425,7 +462,9 @@ func TestConflicts(t *testing.T) {
// val2 votes for blockHash2.
{
addr := privValidators[2].GetPubKey().Address()
pv, err := privValidators[2].GetPubKey()
assert.NoError(t, err)
addr := pv.Address()
vote := withValidator(voteProto, addr, 2)
added, err := signAddVote(privValidators[2], withBlockHash(vote, blockHash2), voteSet)
if !added || err != nil {
@@ -446,7 +485,9 @@ func TestConflicts(t *testing.T) {
// val2 votes for blockHash1.
{
addr := privValidators[2].GetPubKey().Address()
pv, err := privValidators[2].GetPubKey()
assert.NoError(t, err)
addr := pv.Address()
vote := withValidator(voteProto, addr, 2)
added, err := signAddVote(privValidators[2], withBlockHash(vote, blockHash1), voteSet)
if !added {
@@ -488,9 +529,11 @@ func TestMakeCommit(t *testing.T) {
// 6 out of 10 voted for some block.
for i := 0; i < 6; i++ {
addr := privValidators[i].GetPubKey().Address()
pv, err := privValidators[i].GetPubKey()
assert.NoError(t, err)
addr := pv.Address()
vote := withValidator(voteProto, addr, i)
_, err := signAddVote(privValidators[i], vote, voteSet)
_, err = signAddVote(privValidators[i], vote, voteSet)
if err != nil {
t.Error(err)
}
@@ -501,12 +544,14 @@ func TestMakeCommit(t *testing.T) {
// 7th voted for some other block.
{
addr := privValidators[6].GetPubKey().Address()
pv, err := privValidators[6].GetPubKey()
assert.NoError(t, err)
addr := pv.Address()
vote := withValidator(voteProto, addr, 6)
vote = withBlockHash(vote, tmrand.Bytes(32))
vote = withBlockPartsHeader(vote, PartSetHeader{123, tmrand.Bytes(32)})
_, err := signAddVote(privValidators[6], vote, voteSet)
_, err = signAddVote(privValidators[6], vote, voteSet)
if err != nil {
t.Error(err)
}
@@ -514,9 +559,11 @@ func TestMakeCommit(t *testing.T) {
// The 8th voted like everyone else.
{
addr := privValidators[7].GetPubKey().Address()
pv, err := privValidators[7].GetPubKey()
assert.NoError(t, err)
addr := pv.Address()
vote := withValidator(voteProto, addr, 7)
_, err := signAddVote(privValidators[7], vote, voteSet)
_, err = signAddVote(privValidators[7], vote, voteSet)
if err != nil {
t.Error(err)
}
@@ -524,11 +571,13 @@ func TestMakeCommit(t *testing.T) {
// The 9th voted for nil.
{
addr := privValidators[8].GetPubKey().Address()
pv, err := privValidators[8].GetPubKey()
assert.NoError(t, err)
addr := pv.Address()
vote := withValidator(voteProto, addr, 8)
vote.BlockID = BlockID{}
_, err := signAddVote(privValidators[8], vote, voteSet)
_, err = signAddVote(privValidators[8], vote, voteSet)
if err != nil {
t.Error(err)
}
+6 -4
View File
@@ -143,13 +143,14 @@ func TestVoteProposalNotEq(t *testing.T) {
func TestVoteVerifySignature(t *testing.T) {
privVal := NewMockPV()
pubkey := privVal.GetPubKey()
pubkey, err := privVal.GetPubKey()
require.NoError(t, err)
vote := examplePrecommit()
signBytes := vote.SignBytes("test_chain_id")
// sign it
err := privVal.SignVote("test_chain_id", vote)
err = privVal.SignVote("test_chain_id", vote)
require.NoError(t, err)
// verify the same vote
@@ -193,12 +194,13 @@ func TestIsVoteTypeValid(t *testing.T) {
func TestVoteVerify(t *testing.T) {
privVal := NewMockPV()
pubkey := privVal.GetPubKey()
pubkey, err := privVal.GetPubKey()
require.NoError(t, err)
vote := examplePrevote()
vote.ValidatorAddress = pubkey.Address()
err := vote.Verify("test_chain_id", ed25519.GenPrivKey().PubKey())
err = vote.Verify("test_chain_id", ed25519.GenPrivKey().PubKey())
if assert.Error(t, err) {
assert.Equal(t, ErrVoteInvalidValidatorAddress, err)
}