Browse Source

Merge pull request #1284 from tendermint/feature/xla-follow-priv-val

Follow-ups to PrivValidator
pull/1286/head
Ethan Buchman 7 years ago
committed by GitHub
parent
commit
2ce57a65ff
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 30 deletions
  1. +6
    -1
      node/node.go
  2. +9
    -7
      types/priv_validator/json.go
  3. +21
    -21
      types/priv_validator/socket.go
  4. +1
    -1
      types/priv_validator/socket_test.go

+ 6
- 1
node/node.go View File

@ -441,8 +441,13 @@ func (n *Node) OnStop() {
}
n.eventBus.Stop()
n.indexerService.Stop()
if pvsc, ok := n.privValidator.(*priv_val.SocketClient); ok {
if err := pvsc.Stop(); err != nil {
n.Logger.Error("Error stopping priv validator socket client", "err", err)
}
}
}
// RunForever waits for an interrupt signal and stops the node.


+ 9
- 7
types/priv_validator/json.go View File

@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/tendermint/types"
@ -25,12 +24,12 @@ func (pk PrivKey) Sign(msg []byte) (crypto.Signature, error) {
return crypto.PrivKey(pk).Sign(msg), nil
}
// MarshalJSON
// MarshalJSON satisfies json.Marshaler.
func (pk PrivKey) MarshalJSON() ([]byte, error) {
return crypto.PrivKey(pk).MarshalJSON()
}
// UnmarshalJSON
// UnmarshalJSON satisfies json.Unmarshaler.
func (pk *PrivKey) UnmarshalJSON(b []byte) error {
cpk := new(crypto.PrivKey)
if err := cpk.UnmarshalJSON(b); err != nil {
@ -84,23 +83,24 @@ func (pvj *PrivValidatorJSON) String() string {
return fmt.Sprintf("PrivValidator{%v %v}", addr, pvj.PrivValidatorUnencrypted.String())
}
// Save persists the PrivValidatorJSON to disk.
func (pvj *PrivValidatorJSON) Save() {
pvj.save()
}
func (pvj *PrivValidatorJSON) save() {
if pvj.filePath == "" {
cmn.PanicSanity("Cannot save PrivValidator: filePath not set")
panic("Cannot save PrivValidator: filePath not set")
}
jsonBytes, err := json.Marshal(pvj)
if err != nil {
// ; BOOM!!!
cmn.PanicCrisis(err)
panic(err)
}
err = cmn.WriteFileAtomic(pvj.filePath, jsonBytes, 0600)
if err != nil {
// ; BOOM!!!
cmn.PanicCrisis(err)
panic(err)
}
}
@ -144,7 +144,7 @@ func LoadPrivValidatorJSON(filePath string) *PrivValidatorJSON {
// or else generates a new one and saves it to the filePath.
func LoadOrGenPrivValidatorJSON(filePath string) *PrivValidatorJSON {
var pvj *PrivValidatorJSON
if _, err := os.Stat(filePath); err == nil {
if cmn.FileExists(filePath) {
pvj = LoadPrivValidatorJSON(filePath)
} else {
pvj = GenPrivValidatorJSON(filePath)
@ -168,6 +168,8 @@ func NewTestPrivValidator(signer types.TestSigner) *PrivValidatorJSON {
//------------------------------------------------------
// PrivValidatorsByAddress is a list of PrivValidatorJSON ordered by their
// addresses.
type PrivValidatorsByAddress []*PrivValidatorJSON
func (pvs PrivValidatorsByAddress) Len() int {


+ 21
- 21
types/priv_validator/socket.go View File

@ -32,17 +32,17 @@ var (
)
// SocketClientOption sets an optional parameter on the SocketClient.
type SocketClientOption func(*socketClient)
type SocketClientOption func(*SocketClient)
// SocketClientTimeout sets the timeout for connecting to the external socket
// address.
func SocketClientTimeout(timeout time.Duration) SocketClientOption {
return func(sc *socketClient) { sc.connectTimeout = timeout }
return func(sc *SocketClient) { sc.connectTimeout = timeout }
}
// socketClient implements PrivValidator, it uses a socket to request signatures
// SocketClient implements PrivValidator, it uses a socket to request signatures
// from an external process.
type socketClient struct {
type SocketClient struct {
cmn.BaseService
conn net.Conn
@ -52,28 +52,28 @@ type socketClient struct {
connectTimeout time.Duration
}
// Check that socketClient implements PrivValidator2.
var _ types.PrivValidator2 = (*socketClient)(nil)
// Check that SocketClient implements PrivValidator2.
var _ types.PrivValidator2 = (*SocketClient)(nil)
// NewSocketClient returns an instance of socketClient.
// NewSocketClient returns an instance of SocketClient.
func NewSocketClient(
logger log.Logger,
socketAddr string,
privKey *crypto.PrivKeyEd25519,
) *socketClient {
sc := &socketClient{
) *SocketClient {
sc := &SocketClient{
addr: socketAddr,
connectTimeout: time.Second * defaultConnDeadlineSeconds,
privKey: privKey,
}
sc.BaseService = *cmn.NewBaseService(logger, "privValidatorsocketClient", sc)
sc.BaseService = *cmn.NewBaseService(logger, "privValidatorSocketClient", sc)
return sc
}
// OnStart implements cmn.Service.
func (sc *socketClient) OnStart() error {
func (sc *SocketClient) OnStart() error {
if err := sc.BaseService.OnStart(); err != nil {
return err
}
@ -89,7 +89,7 @@ func (sc *socketClient) OnStart() error {
}
// OnStop implements cmn.Service.
func (sc *socketClient) OnStop() {
func (sc *SocketClient) OnStop() {
sc.BaseService.OnStop()
if sc.conn != nil {
@ -99,7 +99,7 @@ func (sc *socketClient) OnStop() {
// GetAddress implements PrivValidator.
// TODO(xla): Remove when PrivValidator2 replaced PrivValidator.
func (sc *socketClient) GetAddress() types.Address {
func (sc *SocketClient) GetAddress() types.Address {
addr, err := sc.Address()
if err != nil {
panic(err)
@ -109,7 +109,7 @@ func (sc *socketClient) GetAddress() types.Address {
}
// Address is an alias for PubKey().Address().
func (sc *socketClient) Address() (cmn.HexBytes, error) {
func (sc *SocketClient) Address() (cmn.HexBytes, error) {
p, err := sc.PubKey()
if err != nil {
return nil, err
@ -120,7 +120,7 @@ func (sc *socketClient) Address() (cmn.HexBytes, error) {
// GetPubKey implements PrivValidator.
// TODO(xla): Remove when PrivValidator2 replaced PrivValidator.
func (sc *socketClient) GetPubKey() crypto.PubKey {
func (sc *SocketClient) GetPubKey() crypto.PubKey {
pubKey, err := sc.PubKey()
if err != nil {
panic(err)
@ -130,7 +130,7 @@ func (sc *socketClient) GetPubKey() crypto.PubKey {
}
// PubKey implements PrivValidator2.
func (sc *socketClient) PubKey() (crypto.PubKey, error) {
func (sc *SocketClient) PubKey() (crypto.PubKey, error) {
err := writeMsg(sc.conn, &PubKeyMsg{})
if err != nil {
return crypto.PubKey{}, err
@ -145,7 +145,7 @@ func (sc *socketClient) PubKey() (crypto.PubKey, error) {
}
// SignVote implements PrivValidator2.
func (sc *socketClient) SignVote(chainID string, vote *types.Vote) error {
func (sc *SocketClient) SignVote(chainID string, vote *types.Vote) error {
err := writeMsg(sc.conn, &SignVoteMsg{Vote: vote})
if err != nil {
return err
@ -162,7 +162,7 @@ func (sc *socketClient) SignVote(chainID string, vote *types.Vote) error {
}
// SignProposal implements PrivValidator2.
func (sc *socketClient) SignProposal(chainID string, proposal *types.Proposal) error {
func (sc *SocketClient) SignProposal(chainID string, proposal *types.Proposal) error {
err := writeMsg(sc.conn, &SignProposalMsg{Proposal: proposal})
if err != nil {
return err
@ -179,7 +179,7 @@ func (sc *socketClient) SignProposal(chainID string, proposal *types.Proposal) e
}
// SignHeartbeat implements PrivValidator2.
func (sc *socketClient) SignHeartbeat(chainID string, heartbeat *types.Heartbeat) error {
func (sc *SocketClient) SignHeartbeat(chainID string, heartbeat *types.Heartbeat) error {
err := writeMsg(sc.conn, &SignHeartbeatMsg{Heartbeat: heartbeat})
if err != nil {
return err
@ -195,7 +195,7 @@ func (sc *socketClient) SignHeartbeat(chainID string, heartbeat *types.Heartbeat
return nil
}
func (sc *socketClient) connect() (net.Conn, error) {
func (sc *SocketClient) connect() (net.Conn, error) {
retries := defaultDialRetryMax
RETRY_LOOP:
@ -314,7 +314,7 @@ func (pvss *PrivValidatorSocketServer) acceptConnections() {
return // Ignore error from listener closing.
}
pvss.Logger.Error(
"accpetConnections",
"acceptConnections",
"err", errors.Wrap(err, "failed to accept connection"),
)
continue


+ 1
- 1
types/priv_validator/socket_test.go View File

@ -128,7 +128,7 @@ func TestSocketClientConnectRetryMax(t *testing.T) {
assert.EqualError(sc.Start(), ErrDialRetryMax.Error())
}
func testSetupSocketPair(t *testing.T, chainID string) (*socketClient, *PrivValidatorSocketServer) {
func testSetupSocketPair(t *testing.T, chainID string) (*SocketClient, *PrivValidatorSocketServer) {
var (
assert, require = assert.New(t), require.New(t)
logger = log.TestingLogger()


Loading…
Cancel
Save