PrivValidator.GetAddress() and PrivValidator.GetPublicKey() return an

error instead of panic
This commit is contained in:
Ismail Khoffi
2018-11-29 18:52:19 +01:00
parent 44b769b1ac
commit 417c30b9c1
23 changed files with 345 additions and 151 deletions
+8 -3
View File
@@ -17,15 +17,19 @@ type voteData struct {
}
func makeVote(val PrivValidator, chainID string, valIndex int, height int64, round, step int, blockID BlockID) *Vote {
addr, err := val.GetAddress()
if err != nil {
panic(err)
}
v := &Vote{
ValidatorAddress: val.GetAddress(),
ValidatorAddress: addr,
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)
}
@@ -64,7 +68,8 @@ func TestEvidence(t *testing.T) {
{vote1, badVote, false}, // signed by wrong key
}
pubKey := val.GetPubKey()
pubKey, err := val.GetPubKey()
assert.NoError(t, err)
for _, c := range cases {
ev := &DuplicateVoteEvidence{
VoteA: c.vote1,
+20 -8
View File
@@ -12,8 +12,10 @@ import (
// PrivValidator defines the functionality of a local Tendermint validator
// that signs votes and proposals, and never double signs.
type PrivValidator interface {
GetAddress() Address // redundant since .PubKey().Address()
GetPubKey() crypto.PubKey
// TODO: shouldn't we remove GetAddress? In case of a remote signer this will trigger
// another request even if the pubkey was already retrieved.
GetAddress() (Address, error) // redundant since .PubKey().Address()
GetPubKey() (crypto.PubKey, error)
SignVote(chainID string, vote *Vote) error
SignProposal(chainID string, proposal *Proposal) error
@@ -29,7 +31,16 @@ func (pvs PrivValidatorsByAddress) Len() int {
}
func (pvs PrivValidatorsByAddress) Less(i, j int) bool {
return bytes.Compare(pvs[i].GetAddress(), pvs[j].GetAddress()) == -1
// this is used in tests only; it's OK to panic here
addr_i, err := pvs[i].GetAddress()
if err != nil {
panic(err)
}
addr_j, err := pvs[j].GetAddress()
if err != nil {
panic(err)
}
return bytes.Compare(addr_i, addr_j) == -1
}
func (pvs PrivValidatorsByAddress) Swap(i, j int) {
@@ -52,13 +63,13 @@ func NewMockPV() *MockPV {
}
// Implements PrivValidator.
func (pv *MockPV) GetAddress() Address {
return pv.privKey.PubKey().Address()
func (pv *MockPV) GetAddress() (Address, error) {
return pv.privKey.PubKey().Address(), nil
}
// 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.
@@ -85,7 +96,8 @@ func (pv *MockPV) SignProposal(chainID string, proposal *Proposal) error {
// String returns a string representation of the MockPV.
func (pv *MockPV) String() string {
return fmt.Sprintf("MockPV{%v}", pv.GetAddress())
addr, _ := pv.GetAddress()
return fmt.Sprintf("MockPV{%v}", addr)
}
// XXX: Implement.
+5 -3
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()
assert.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
@@ -94,7 +95,8 @@ func BenchmarkProposalVerifySignature(b *testing.B) {
privVal := NewMockPV()
err := privVal.SignProposal("test_chain_id", testProposal)
require.Nil(b, err)
pubKey := privVal.GetPubKey()
pubKey, err := privVal.GetPubKey()
require.NoError(b, err)
for i := 0; i < b.N; i++ {
pubKey.VerifyBytes(testProposal.SignBytes("test_chain_id"), testProposal.Signature)
+4 -2
View File
@@ -142,14 +142,16 @@ func TestABCIEvidence(t *testing.T) {
blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))
const chainID = "mychain"
pubKey, err := val.GetPubKey()
assert.NoError(t, err)
ev := &DuplicateVoteEvidence{
PubKey: val.GetPubKey(),
PubKey: pubKey,
VoteA: makeVote(val, chainID, 0, 10, 2, 1, blockID),
VoteB: makeVote(val, chainID, 0, 10, 2, 1, blockID2),
}
abciEv := TM2PB.Evidence(
ev,
NewValidatorSet([]*Validator{NewValidator(val.GetPubKey(), 10)}),
NewValidatorSet([]*Validator{NewValidator(pubKey, 10)}),
time.Now(),
)
+6 -3
View File
@@ -10,9 +10,12 @@ func MakeCommit(blockID BlockID, height int64, round int,
// all sign
for i := 0; i < len(validators); i++ {
addr, err := validators[i].GetAddress()
if err != nil {
return nil, err
}
vote := &Vote{
ValidatorAddress: validators[i].GetAddress(),
ValidatorAddress: addr,
ValidatorIndex: i,
Height: height,
Round: round,
@@ -21,7 +24,7 @@ func MakeCommit(blockID BlockID, height int64, round int,
Timestamp: tmtime.Now(),
}
_, err := signAddVote(validators[i], vote, voteSet)
_, err = signAddVote(validators[i], vote, voteSet)
if err != nil {
return nil, err
}
+2 -1
View File
@@ -101,6 +101,7 @@ func RandValidator(randPower bool, minPower int64) (*Validator, PrivValidator) {
if randPower {
votePower += int64(cmn.RandUint32())
}
val := NewValidator(privVal.GetPubKey(), votePower)
pubKey, _ := privVal.GetPubKey()
val := NewValidator(pubKey, votePower)
return val, privVal
}
+85 -40
View File
@@ -2,6 +2,7 @@ package types
import (
"bytes"
"github.com/stretchr/testify/assert"
"testing"
"github.com/tendermint/tendermint/crypto"
@@ -66,7 +67,9 @@ func TestAddVote(t *testing.T) {
// t.Logf(">> %v", voteSet)
if voteSet.GetByAddress(val0.GetAddress()) != nil {
val0Addr, err := val0.GetAddress()
assert.NoError(t, err)
if voteSet.GetByAddress(val0Addr) != nil {
t.Errorf("Expected GetByAddress(val0.Address) to be nil")
}
if voteSet.BitArray().GetIndex(0) {
@@ -78,7 +81,7 @@ func TestAddVote(t *testing.T) {
}
vote := &Vote{
ValidatorAddress: val0.GetAddress(),
ValidatorAddress: val0Addr,
ValidatorIndex: 0, // since privValidators are in order
Height: height,
Round: round,
@@ -86,12 +89,12 @@ 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)
}
if voteSet.GetByAddress(val0.GetAddress()) == nil {
if voteSet.GetByAddress(val0Addr) == nil {
t.Errorf("Expected GetByAddress(val0.Address) to be present")
}
if !voteSet.BitArray().GetIndex(0) {
@@ -118,8 +121,10 @@ func Test2_3Majority(t *testing.T) {
}
// 6 out of 10 voted for nil.
for i := 0; i < 6; i++ {
vote := withValidator(voteProto, privValidators[i].GetAddress(), i)
_, err := signAddVote(privValidators[i], vote, voteSet)
addr, err := privValidators[i].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, i)
_, err = signAddVote(privValidators[i], vote, voteSet)
if err != nil {
t.Error(err)
}
@@ -131,8 +136,10 @@ func Test2_3Majority(t *testing.T) {
// 7th validator voted for some blockhash
{
vote := withValidator(voteProto, privValidators[6].GetAddress(), 6)
_, err := signAddVote(privValidators[6], withBlockHash(vote, cmn.RandBytes(32)), voteSet)
addr, err := privValidators[6].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 6)
_, err = signAddVote(privValidators[6], withBlockHash(vote, cmn.RandBytes(32)), voteSet)
if err != nil {
t.Error(err)
}
@@ -144,8 +151,10 @@ func Test2_3Majority(t *testing.T) {
// 8th validator voted for nil.
{
vote := withValidator(voteProto, privValidators[7].GetAddress(), 7)
_, err := signAddVote(privValidators[7], vote, voteSet)
addr, err := privValidators[7].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 7)
_, err = signAddVote(privValidators[7], vote, voteSet)
if err != nil {
t.Error(err)
}
@@ -176,8 +185,10 @@ func Test2_3MajorityRedux(t *testing.T) {
// 66 out of 100 voted for nil.
for i := 0; i < 66; i++ {
vote := withValidator(voteProto, privValidators[i].GetAddress(), i)
_, err := signAddVote(privValidators[i], vote, voteSet)
addr, err := privValidators[i].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, i)
_, err = signAddVote(privValidators[i], vote, voteSet)
if err != nil {
t.Error(err)
}
@@ -189,8 +200,10 @@ func Test2_3MajorityRedux(t *testing.T) {
// 67th validator voted for nil
{
vote := withValidator(voteProto, privValidators[66].GetAddress(), 66)
_, err := signAddVote(privValidators[66], withBlockHash(vote, nil), voteSet)
adrr, err := privValidators[66].GetAddress()
vote := withValidator(voteProto, adrr, 66)
assert.NoError(t, err)
_, err = signAddVote(privValidators[66], withBlockHash(vote, nil), voteSet)
if err != nil {
t.Error(err)
}
@@ -202,9 +215,11 @@ func Test2_3MajorityRedux(t *testing.T) {
// 68th validator voted for a different BlockParts PartSetHeader
{
vote := withValidator(voteProto, privValidators[67].GetAddress(), 67)
addr, err := privValidators[67].GetAddress()
assert.NoError(t, err)
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)
}
@@ -216,9 +231,11 @@ func Test2_3MajorityRedux(t *testing.T) {
// 69th validator voted for different BlockParts Total
{
vote := withValidator(voteProto, privValidators[68].GetAddress(), 68)
addr, err := privValidators[68].GetAddress()
assert.NoError(t, err)
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)
}
@@ -230,8 +247,10 @@ func Test2_3MajorityRedux(t *testing.T) {
// 70th validator voted for different BlockHash
{
vote := withValidator(voteProto, privValidators[69].GetAddress(), 69)
_, err := signAddVote(privValidators[69], withBlockHash(vote, cmn.RandBytes(32)), voteSet)
addr, err := privValidators[69].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 69)
_, err = signAddVote(privValidators[69], withBlockHash(vote, cmn.RandBytes(32)), voteSet)
if err != nil {
t.Error(err)
}
@@ -243,8 +262,10 @@ func Test2_3MajorityRedux(t *testing.T) {
// 71st validator voted for the right BlockHash & BlockPartsHeader
{
vote := withValidator(voteProto, privValidators[70].GetAddress(), 70)
_, err := signAddVote(privValidators[70], vote, voteSet)
addr, err := privValidators[70].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 70)
_, err = signAddVote(privValidators[70], vote, voteSet)
if err != nil {
t.Error(err)
}
@@ -271,7 +292,9 @@ func TestBadVotes(t *testing.T) {
// val0 votes for nil.
{
vote := withValidator(voteProto, privValidators[0].GetAddress(), 0)
addr, err := privValidators[0].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 0)
added, err := signAddVote(privValidators[0], vote, voteSet)
if !added || err != nil {
t.Errorf("Expected VoteSet.Add to succeed")
@@ -280,7 +303,9 @@ func TestBadVotes(t *testing.T) {
// val0 votes again for some block.
{
vote := withValidator(voteProto, privValidators[0].GetAddress(), 0)
addr, err := privValidators[0].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 0)
added, err := signAddVote(privValidators[0], withBlockHash(vote, cmn.RandBytes(32)), voteSet)
if added || err == nil {
t.Errorf("Expected VoteSet.Add to fail, conflicting vote.")
@@ -289,7 +314,9 @@ func TestBadVotes(t *testing.T) {
// val1 votes on another height
{
vote := withValidator(voteProto, privValidators[1].GetAddress(), 1)
addr, err := privValidators[1].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 1)
added, err := signAddVote(privValidators[1], withHeight(vote, height+1), voteSet)
if added || err == nil {
t.Errorf("Expected VoteSet.Add to fail, wrong height")
@@ -298,7 +325,9 @@ func TestBadVotes(t *testing.T) {
// val2 votes on another round
{
vote := withValidator(voteProto, privValidators[2].GetAddress(), 2)
addr, err := privValidators[2].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 2)
added, err := signAddVote(privValidators[2], withRound(vote, round+1), voteSet)
if added || err == nil {
t.Errorf("Expected VoteSet.Add to fail, wrong round")
@@ -307,7 +336,9 @@ func TestBadVotes(t *testing.T) {
// val3 votes of another type.
{
vote := withValidator(voteProto, privValidators[3].GetAddress(), 3)
addr, err := privValidators[3].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 3)
added, err := signAddVote(privValidators[3], withType(vote, byte(PrecommitType)), voteSet)
if added || err == nil {
t.Errorf("Expected VoteSet.Add to fail, wrong type")
@@ -331,9 +362,11 @@ func TestConflicts(t *testing.T) {
BlockID: BlockID{nil, PartSetHeader{}},
}
val0Addr, err := privValidators[0].GetAddress()
assert.NoError(t, err)
// val0 votes for nil.
{
vote := withValidator(voteProto, privValidators[0].GetAddress(), 0)
vote := withValidator(voteProto, val0Addr, 0)
added, err := signAddVote(privValidators[0], vote, voteSet)
if !added || err != nil {
t.Errorf("Expected VoteSet.Add to succeed")
@@ -342,7 +375,7 @@ func TestConflicts(t *testing.T) {
// val0 votes again for blockHash1.
{
vote := withValidator(voteProto, privValidators[0].GetAddress(), 0)
vote := withValidator(voteProto, val0Addr, 0)
added, err := signAddVote(privValidators[0], withBlockHash(vote, blockHash1), voteSet)
if added {
t.Errorf("Expected VoteSet.Add to fail, conflicting vote.")
@@ -357,7 +390,7 @@ func TestConflicts(t *testing.T) {
// val0 votes again for blockHash1.
{
vote := withValidator(voteProto, privValidators[0].GetAddress(), 0)
vote := withValidator(voteProto, val0Addr, 0)
added, err := signAddVote(privValidators[0], withBlockHash(vote, blockHash1), voteSet)
if !added {
t.Errorf("Expected VoteSet.Add to succeed, called SetPeerMaj23().")
@@ -372,7 +405,7 @@ func TestConflicts(t *testing.T) {
// val0 votes again for blockHash1.
{
vote := withValidator(voteProto, privValidators[0].GetAddress(), 0)
vote := withValidator(voteProto, val0Addr, 0)
added, err := signAddVote(privValidators[0], withBlockHash(vote, blockHash2), voteSet)
if added {
t.Errorf("Expected VoteSet.Add to fail, duplicate SetPeerMaj23() from peerA")
@@ -384,7 +417,9 @@ func TestConflicts(t *testing.T) {
// val1 votes for blockHash1.
{
vote := withValidator(voteProto, privValidators[1].GetAddress(), 1)
addr, err := privValidators[1].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 1)
added, err := signAddVote(privValidators[1], withBlockHash(vote, blockHash1), voteSet)
if !added || err != nil {
t.Errorf("Expected VoteSet.Add to succeed")
@@ -401,7 +436,9 @@ func TestConflicts(t *testing.T) {
// val2 votes for blockHash2.
{
vote := withValidator(voteProto, privValidators[2].GetAddress(), 2)
addr, err := privValidators[2].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 2)
added, err := signAddVote(privValidators[2], withBlockHash(vote, blockHash2), voteSet)
if !added || err != nil {
t.Errorf("Expected VoteSet.Add to succeed")
@@ -421,7 +458,9 @@ func TestConflicts(t *testing.T) {
// val2 votes for blockHash1.
{
vote := withValidator(voteProto, privValidators[2].GetAddress(), 2)
addr, err := privValidators[2].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 2)
added, err := signAddVote(privValidators[2], withBlockHash(vote, blockHash1), voteSet)
if !added {
t.Errorf("Expected VoteSet.Add to succeed")
@@ -462,8 +501,10 @@ func TestMakeCommit(t *testing.T) {
// 6 out of 10 voted for some block.
for i := 0; i < 6; i++ {
vote := withValidator(voteProto, privValidators[i].GetAddress(), i)
_, err := signAddVote(privValidators[i], vote, voteSet)
addr, err := privValidators[i].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, i)
_, err = signAddVote(privValidators[i], vote, voteSet)
if err != nil {
t.Error(err)
}
@@ -474,11 +515,13 @@ func TestMakeCommit(t *testing.T) {
// 7th voted for some other block.
{
vote := withValidator(voteProto, privValidators[6].GetAddress(), 6)
addr, err := privValidators[6].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 6)
vote = withBlockHash(vote, cmn.RandBytes(32))
vote = withBlockPartsHeader(vote, PartSetHeader{123, cmn.RandBytes(32)})
_, err := signAddVote(privValidators[6], vote, voteSet)
_, err = signAddVote(privValidators[6], vote, voteSet)
if err != nil {
t.Error(err)
}
@@ -486,8 +529,10 @@ func TestMakeCommit(t *testing.T) {
// The 8th voted like everyone else.
{
vote := withValidator(voteProto, privValidators[7].GetAddress(), 7)
_, err := signAddVote(privValidators[7], vote, voteSet)
addr, err := privValidators[7].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 7)
_, err = signAddVote(privValidators[7], vote, voteSet)
if err != nil {
t.Error(err)
}
+6 -4
View File
@@ -140,13 +140,14 @@ func TestVoteProposalNotEq(t *testing.T) {
func TestVoteVerifySignature(t *testing.T) {
privVal := NewMockPV()
pubkey := privVal.GetPubKey()
pubkey, err := privVal.GetPubKey()
assert.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
@@ -190,12 +191,13 @@ func TestIsVoteTypeValid(t *testing.T) {
func TestVoteVerify(t *testing.T) {
privVal := NewMockPV()
pubkey := privVal.GetPubKey()
pubkey, err := privVal.GetPubKey()
assert.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)
}