privval: add chainID to requests (#5239)

## Description

Add chainid to requests to privval. This is a non-breaking change and hardware devices can opt to ignore the field.
 
Closes: #4503 

Took the approach of passing chainID to the client instead of modifying `GetPubKey` because it would lead to a larger change throughout the codebase and in some places it could get tricky to get chainID.
This commit is contained in:
Marko
2020-08-17 11:07:15 +02:00
committed by GitHub
parent 22ef3f6e7a
commit 8cdaa7f515
9 changed files with 252 additions and 62 deletions

View File

@@ -15,20 +15,21 @@ import (
// Handles remote validator connections that provide signing services
type SignerClient struct {
endpoint *SignerListenerEndpoint
chainID string
}
var _ types.PrivValidator = (*SignerClient)(nil)
// NewSignerClient returns an instance of SignerClient.
// it will start the endpoint (if not already started)
func NewSignerClient(endpoint *SignerListenerEndpoint) (*SignerClient, error) {
func NewSignerClient(endpoint *SignerListenerEndpoint, chainID string) (*SignerClient, error) {
if !endpoint.IsRunning() {
if err := endpoint.Start(); err != nil {
return nil, fmt.Errorf("failed to start listener endpoint: %w", err)
}
}
return &SignerClient{endpoint: endpoint}, nil
return &SignerClient{endpoint: endpoint, chainID: chainID}, nil
}
// Close closes the underlying connection
@@ -68,7 +69,7 @@ func (sc *SignerClient) Ping() error {
// GetPubKey retrieves a public key from a remote signer
// returns an error if client is not able to provide the key
func (sc *SignerClient) GetPubKey() (crypto.PubKey, error) {
response, err := sc.endpoint.SendRequest(mustWrapMsg(&privvalproto.PubKeyRequest{}))
response, err := sc.endpoint.SendRequest(mustWrapMsg(&privvalproto.PubKeyRequest{ChainId: sc.chainID}))
if err != nil {
return nil, fmt.Errorf("send: %w", err)
}
@@ -91,7 +92,7 @@ func (sc *SignerClient) GetPubKey() (crypto.PubKey, error) {
// SignVote requests a remote signer to sign a vote
func (sc *SignerClient) SignVote(chainID string, vote *tmproto.Vote) error {
response, err := sc.endpoint.SendRequest(mustWrapMsg(&privvalproto.SignVoteRequest{Vote: vote}))
response, err := sc.endpoint.SendRequest(mustWrapMsg(&privvalproto.SignVoteRequest{Vote: vote, ChainId: chainID}))
if err != nil {
return err
}
@@ -111,7 +112,9 @@ func (sc *SignerClient) SignVote(chainID string, vote *tmproto.Vote) error {
// SignProposal requests a remote signer to sign a proposal
func (sc *SignerClient) SignProposal(chainID string, proposal *tmproto.Proposal) error {
response, err := sc.endpoint.SendRequest(mustWrapMsg(&privvalproto.SignProposalRequest{Proposal: proposal}))
response, err := sc.endpoint.SendRequest(mustWrapMsg(
&privvalproto.SignProposalRequest{Proposal: proposal, ChainId: chainID},
))
if err != nil {
return err
}

View File

@@ -33,7 +33,7 @@ func getSignerTestCases(t *testing.T) []signerTestCase {
// get a pair of signer listener, signer dialer endpoints
sl, sd := getMockEndpoints(t, dtc.addr, dtc.dialer)
sc, err := NewSignerClient(sl)
sc, err := NewSignerClient(sl, chainID)
require.NoError(t, err)
ss := NewSignerServer(sd, chainID, mockPV)

View File

@@ -21,6 +21,13 @@ func DefaultValidationRequestHandler(
switch r := req.Sum.(type) {
case *privvalproto.Message_PubKeyRequest:
if r.PubKeyRequest.GetChainId() != chainID {
res = mustWrapMsg(&privvalproto.SignedVoteResponse{
Vote: nil, Error: &privvalproto.RemoteSignerError{
Code: 0, Description: "unable to provide pubkey"}})
return res, fmt.Errorf("want chainID: %s, got chainID: %s", r.PubKeyRequest.GetChainId(), chainID)
}
var pubKey crypto.PubKey
pubKey, err = privVal.GetPubKey()
pk, err := cryptoenc.PubKeyToProto(pubKey)
@@ -36,6 +43,13 @@ func DefaultValidationRequestHandler(
}
case *privvalproto.Message_SignVoteRequest:
if r.SignVoteRequest.ChainId != chainID {
res = mustWrapMsg(&privvalproto.SignedVoteResponse{
Vote: nil, Error: &privvalproto.RemoteSignerError{
Code: 0, Description: "unable to sign vote"}})
return res, fmt.Errorf("want chainID: %s, got chainID: %s", r.SignVoteRequest.GetChainId(), chainID)
}
vote := r.SignVoteRequest.Vote
err = privVal.SignVote(chainID, vote)
@@ -47,6 +61,14 @@ func DefaultValidationRequestHandler(
}
case *privvalproto.Message_SignProposalRequest:
if r.SignProposalRequest.GetChainId() != chainID {
res = mustWrapMsg(&privvalproto.SignedVoteResponse{
Vote: nil, Error: &privvalproto.RemoteSignerError{
Code: 0,
Description: "unable to sign proposal"}})
return res, fmt.Errorf("want chainID: %s, got chainID: %s", r.SignProposalRequest.GetChainId(), chainID)
}
proposal := r.SignProposalRequest.Proposal
err = privVal.SignProposal(chainID, proposal)