Browse Source

GetPubKey can return errors

pull/3370/head
Juan Leni 6 years ago
parent
commit
68c06f19b4
No known key found for this signature in database GPG Key ID: 23F1452155140419
7 changed files with 59 additions and 25 deletions
  1. +5
    -5
      privval/signer_client.go
  2. +6
    -4
      privval/signer_client_test.go
  3. +4
    -1
      privval/signer_service.go
  4. +20
    -7
      types/priv_validator.go
  5. +7
    -2
      types/test_util.go
  6. +8
    -3
      types/validator.go
  7. +9
    -3
      types/validator_set.go

+ 5
- 5
privval/signer_client.go View File

@ -53,25 +53,25 @@ func (sc *SignerClient) WaitForConnection(maxWait time.Duration) error {
// Implement PrivValidator
// GetPubKey retrieves a public key from a remote signer
func (sc *SignerClient) GetPubKey() crypto.PubKey {
func (sc *SignerClient) GetPubKey() (crypto.PubKey, error) {
response, err := sc.endpoint.SendRequest(&PubKeyRequest{})
if err != nil {
sc.endpoint.Logger.Error("error sending request", "err", err)
return nil
return nil, err
}
pubKeyResp, ok := response.(*PubKeyResponse)
if !ok {
sc.endpoint.Logger.Error("response is not PubKeyResponse")
return nil
return nil, err
}
if pubKeyResp.Error != nil {
sc.endpoint.Logger.Error("failed to get private validator's public key", "err", pubKeyResp.Error)
return nil
return nil, err
}
return pubKeyResp.PubKey
return pubKeyResp.PubKey, nil
}
// SignVote requests a remote signer to sign a vote


+ 6
- 4
privval/signer_client_test.go View File

@ -58,13 +58,15 @@ func TestSignerGetPubKey(t *testing.T) {
defer tc.signerService.OnStop()
defer tc.signer.Close()
pubKey := tc.signer.GetPubKey()
expectedPubKey := tc.mockPV.GetPubKey()
pubKey, err := tc.signer.GetPubKey()
assert.NoError(t, err)
expectedPubKey, err := tc.mockPV.GetPubKey()
assert.NoError(t, err)
assert.Equal(t, expectedPubKey, pubKey)
addr := tc.signer.GetPubKey().Address()
expectedAddr := tc.mockPV.GetPubKey().Address()
addr := pubKey.Address()
expectedAddr := expectedPubKey.Address()
assert.Equal(t, expectedAddr, addr)
}()


+ 4
- 1
privval/signer_service.go View File

@ -14,7 +14,10 @@ func HandleValidatorRequest(req RemoteSignerMsg, chainID string, privVal types.P
switch r := req.(type) {
case *PubKeyRequest:
var p crypto.PubKey
p = privVal.GetPubKey()
p, err = privVal.GetPubKey()
if err != nil {
return nil, err
}
res = &PubKeyResponse{p, nil}
case *SignVoteRequest:


+ 20
- 7
types/priv_validator.go View File

@ -12,9 +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: Why it is not possible to return an error? nil feels not enough..
GetPubKey() crypto.PubKey
GetPubKey() (crypto.PubKey, error)
SignVote(chainID string, vote *Vote) error
SignProposal(chainID string, proposal *Proposal) error
}
@ -29,7 +27,17 @@ 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
pk1, err := pvs[i].GetPubKey()
if err != nil {
return false // Send pk with errors to the end
}
pk2, err := pvs[j].GetPubKey()
if err != nil {
return true // Send pk with errors to the end
}
return bytes.Compare(pk1.Address(), pk2.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,7 +105,12 @@ 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()
pk, err := pv.GetPubKey()
if err != nil {
return fmt.Sprintf("MockPV{%v}", err)
}
addr := pk.Address()
return fmt.Sprintf("MockPV{%v}", addr)
}


+ 7
- 2
types/test_util.go View File

@ -10,7 +10,12 @@ func MakeCommit(blockID BlockID, height int64, round int,
// all sign
for i := 0; i < len(validators); i++ {
addr := validators[i].GetPubKey().Address()
pk, err := validators[i].GetPubKey()
if err != nil {
return nil, err
}
addr := pk.Address()
vote := &Vote{
ValidatorAddress: addr,
ValidatorIndex: i,
@ -21,7 +26,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
}


+ 8
- 3
types/validator.go View File

@ -98,13 +98,18 @@ func (v *Validator) Bytes() []byte {
// RandValidator returns a randomized validator, useful for testing.
// UNSTABLE
func RandValidator(randPower bool, minPower int64) (*Validator, PrivValidator) {
func RandValidator(randPower bool, minPower int64) (*Validator, PrivValidator, error) {
privVal := NewMockPV()
votePower := minPower
if randPower {
votePower += int64(cmn.RandUint32())
}
pubKey := privVal.GetPubKey()
pubKey, err := privVal.GetPubKey()
if err != nil {
return nil, nil, err
}
val := NewValidator(pubKey, votePower)
return val, privVal
return val, privVal, nil
}

+ 9
- 3
types/validator_set.go View File

@ -793,17 +793,23 @@ func (valz ValidatorsByAddress) Swap(i, j int) {
// RandValidatorSet returns a randomized validator set, useful for testing.
// NOTE: PrivValidator are in order.
// UNSTABLE
func RandValidatorSet(numValidators int, votingPower int64) (*ValidatorSet, []PrivValidator) {
func RandValidatorSet(numValidators int, votingPower int64) (*ValidatorSet, []PrivValidator, error) {
valz := make([]*Validator, numValidators)
privValidators := make([]PrivValidator, numValidators)
for i := 0; i < numValidators; i++ {
val, privValidator := RandValidator(false, votingPower)
val, privValidator, err := RandValidator(false, votingPower)
if err != nil {
return nil, nil, err
}
valz[i] = val
privValidators[i] = privValidator
}
vals := NewValidatorSet(valz)
sort.Sort(PrivValidatorsByAddress(privValidators))
return vals, privValidators
return vals, privValidators, nil
}
///////////////////////////////////////////////////////////////////////////////


Loading…
Cancel
Save