mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-08 06:15:33 +00:00
The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
135 lines
3.7 KiB
Go
135 lines
3.7 KiB
Go
package privval
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/tendermint/tendermint/crypto"
|
|
"github.com/tendermint/tendermint/crypto/encoding"
|
|
privvalproto "github.com/tendermint/tendermint/proto/tendermint/privval"
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
"github.com/tendermint/tendermint/types"
|
|
)
|
|
|
|
// SignerClient implements PrivValidator.
|
|
// 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, 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, chainID: chainID}, nil
|
|
}
|
|
|
|
// Close closes the underlying connection
|
|
func (sc *SignerClient) Close() error {
|
|
return sc.endpoint.Close()
|
|
}
|
|
|
|
// IsConnected indicates with the signer is connected to a remote signing service
|
|
func (sc *SignerClient) IsConnected() bool {
|
|
return sc.endpoint.IsConnected()
|
|
}
|
|
|
|
// WaitForConnection waits maxWait for a connection or returns a timeout error
|
|
func (sc *SignerClient) WaitForConnection(maxWait time.Duration) error {
|
|
return sc.endpoint.WaitForConnection(maxWait)
|
|
}
|
|
|
|
//--------------------------------------------------------
|
|
// Implement PrivValidator
|
|
|
|
// Ping sends a ping request to the remote signer
|
|
func (sc *SignerClient) Ping() error {
|
|
response, err := sc.endpoint.SendRequest(mustWrapMsg(&privvalproto.PingRequest{}))
|
|
if err != nil {
|
|
sc.endpoint.Logger.Error("SignerClient::Ping", "err", err)
|
|
return nil
|
|
}
|
|
|
|
pb := response.GetPingResponse()
|
|
if pb == nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// 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(ctx context.Context) (crypto.PubKey, error) {
|
|
response, err := sc.endpoint.SendRequest(mustWrapMsg(&privvalproto.PubKeyRequest{ChainId: sc.chainID}))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("send: %w", err)
|
|
}
|
|
|
|
resp := response.GetPubKeyResponse()
|
|
if resp == nil {
|
|
return nil, ErrUnexpectedResponse
|
|
}
|
|
if resp.Error != nil {
|
|
return nil, &RemoteSignerError{Code: int(resp.Error.Code), Description: resp.Error.Description}
|
|
}
|
|
|
|
pk, err := encoding.PubKeyFromProto(resp.PubKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return pk, nil
|
|
}
|
|
|
|
// SignVote requests a remote signer to sign a vote
|
|
func (sc *SignerClient) SignVote(ctx context.Context, chainID string, vote *tmproto.Vote) error {
|
|
response, err := sc.endpoint.SendRequest(mustWrapMsg(&privvalproto.SignVoteRequest{Vote: vote, ChainId: chainID}))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp := response.GetSignedVoteResponse()
|
|
if resp == nil {
|
|
return ErrUnexpectedResponse
|
|
}
|
|
if resp.Error != nil {
|
|
return &RemoteSignerError{Code: int(resp.Error.Code), Description: resp.Error.Description}
|
|
}
|
|
|
|
*vote = resp.Vote
|
|
|
|
return nil
|
|
}
|
|
|
|
// SignProposal requests a remote signer to sign a proposal
|
|
func (sc *SignerClient) SignProposal(ctx context.Context, chainID string, proposal *tmproto.Proposal) error {
|
|
response, err := sc.endpoint.SendRequest(mustWrapMsg(
|
|
&privvalproto.SignProposalRequest{Proposal: proposal, ChainId: chainID},
|
|
))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp := response.GetSignedProposalResponse()
|
|
if resp == nil {
|
|
return ErrUnexpectedResponse
|
|
}
|
|
if resp.Error != nil {
|
|
return &RemoteSignerError{Code: int(resp.Error.Code), Description: resp.Error.Description}
|
|
}
|
|
|
|
*proposal = resp.Proposal
|
|
|
|
return nil
|
|
}
|