diff --git a/cmd/tendermint/commands/show_validator.go b/cmd/tendermint/commands/show_validator.go index ad12dbb1..5bec27f3 100644 --- a/cmd/tendermint/commands/show_validator.go +++ b/cmd/tendermint/commands/show_validator.go @@ -2,12 +2,20 @@ package commands import ( "fmt" + "os" "github.com/spf13/cobra" + "github.com/tendermint/tendermint/crypto" + cmn "github.com/tendermint/tendermint/libs/common" + "github.com/tendermint/tendermint/node" "github.com/tendermint/tendermint/privval" ) +func init() { + ShowValidatorCmd.Flags().String("priv_validator_laddr", config.PrivValidatorListenAddr, "Socket address to listen on for connections from external priv_validator process") +} + // ShowValidatorCmd adds capabilities for showing the validator info. var ShowValidatorCmd = &cobra.Command{ Use: "show_validator", @@ -16,10 +24,25 @@ var ShowValidatorCmd = &cobra.Command{ } func showValidator(cmd *cobra.Command, args []string) { - // TODO(ismail): add a flag and check if we actually want to see the pub key - // of the remote signer instead of the FilePV - privValidator := privval.LoadOrGenFilePV(config.PrivValidatorFile()) - key := privValidator.GetPubKey() - pubKeyJSONBytes, _ := cdc.MarshalJSON(key) + var pubKey crypto.PubKey + if config.PrivValidatorListenAddr != "" { + // If an address is provided, listen on the socket for a connection from an + // external signing process and request the public key from there. + privValidator, err := node.CreateAndStartPrivValidatorSocketClient(config.PrivValidatorListenAddr, logger) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to get private validator's public key: %v", err) + os.Exit(-1) + } + if pvsc, ok := privValidator.(cmn.Service); ok { + if err := pvsc.Stop(); err != nil { + fmt.Fprintf(os.Stderr, "Failed to get stop private validator client: %v", err) + } + } + pubKey = privValidator.GetPubKey() + } else { + privValidator := privval.LoadOrGenFilePV(config.PrivValidatorFile()) + pubKey = privValidator.GetPubKey() + } + pubKeyJSONBytes, _ := cdc.MarshalJSON(pubKey) fmt.Println(string(pubKeyJSONBytes)) } diff --git a/node/node.go b/node/node.go index 00effa2e..48d447c7 100644 --- a/node/node.go +++ b/node/node.go @@ -229,7 +229,7 @@ func NewNode(config *cfg.Config, // If an address is provided, listen on the socket for a connection from an // external signing process. // FIXME: we should start services inside OnStart - privValidator, err = createAndStartPrivValidatorSocketClient(config.PrivValidatorListenAddr, logger) + privValidator, err = CreateAndStartPrivValidatorSocketClient(config.PrivValidatorListenAddr, logger) if err != nil { return nil, errors.Wrap(err, "Error with private validator socket client") } @@ -853,7 +853,7 @@ func saveGenesisDoc(db dbm.DB, genDoc *types.GenesisDoc) { db.SetSync(genesisDocKey, bytes) } -func createAndStartPrivValidatorSocketClient( +func CreateAndStartPrivValidatorSocketClient( listenAddr string, logger log.Logger, ) (types.PrivValidator, error) { diff --git a/privval/remote_signer.go b/privval/remote_signer.go index ed2a2877..b4c3816c 100644 --- a/privval/remote_signer.go +++ b/privval/remote_signer.go @@ -230,6 +230,7 @@ func writeMsg(w io.Writer, msg interface{}) (err error) { } func handleRequest(req RemoteSignerMsg, chainID string, privVal types.PrivValidator) (RemoteSignerMsg, error) { + fmt.Println("handle req", req) var res RemoteSignerMsg var err error diff --git a/privval/tcp.go b/privval/tcp.go index 1fb736e6..04e5a204 100644 --- a/privval/tcp.go +++ b/privval/tcp.go @@ -115,10 +115,13 @@ func (sc *TCPVal) OnStart() error { // Start a routine to keep the connection alive sc.cancelPing = make(chan struct{}, 1) sc.pingTicker = time.NewTicker(sc.connHeartbeat) - go func() { + /*go func() { for { select { case <-sc.pingTicker.C: + sc.Logger.Error( + "Pinging", + ) err := sc.Ping() if err != nil { sc.Logger.Error( @@ -127,11 +130,14 @@ func (sc *TCPVal) OnStart() error { ) } case <-sc.cancelPing: + sc.Logger.Error( + "Pinging Stopped", + ) sc.pingTicker.Stop() return } } - }() + }()*/ return nil } diff --git a/privval/tcp_server.go b/privval/tcp_server.go index 694023d7..2ccc5460 100644 --- a/privval/tcp_server.go +++ b/privval/tcp_server.go @@ -1,6 +1,7 @@ package privval import ( + "fmt" "io" "net" "time" @@ -63,13 +64,7 @@ func NewRemoteSigner( // OnStart implements cmn.Service. func (rs *RemoteSigner) OnStart() error { - conn, err := rs.connect() - if err != nil { - rs.Logger.Error("OnStart", "err", err) - return err - } - - go rs.handleConnection(conn) + go rs.handleConnection() return nil } @@ -86,75 +81,86 @@ func (rs *RemoteSigner) OnStop() { } func (rs *RemoteSigner) connect() (net.Conn, error) { - for retries := rs.connRetries; retries > 0; retries-- { - // Don't sleep if it is the first retry. - if retries != rs.connRetries { - time.Sleep(rs.connDeadline) - } + //for retries := rs.connRetries; retries > 0; retries-- { + // Don't sleep if it is the first retry. + //if retries != rs.connRetries { + // time.Sleep(rs.connDeadline) + //} - conn, err := cmn.Connect(rs.addr) - if err != nil { - rs.Logger.Error( - "connect", - "addr", rs.addr, - "err", err, - ) + conn, err := cmn.Connect(rs.addr) + if err != nil { + rs.Logger.Error( + "connect", + "addr", rs.addr, + "err", err, + ) - continue - } - - if err := conn.SetDeadline(time.Now().Add(connTimeout)); err != nil { - rs.Logger.Error( - "connect", - "err", err, - ) - continue - } - - conn, err = p2pconn.MakeSecretConnection(conn, rs.privKey) - if err != nil { - rs.Logger.Error( - "connect", - "err", err, - ) - - continue - } - - return conn, nil + return nil, err } + //if err := conn.SetDeadline(time.Now().Add(connTimeout)); err != nil { + // rs.Logger.Error( + // "connect", + // "err", err, + // ) + // //continue + // return nil, err + //} + + conn, err = p2pconn.MakeSecretConnection(conn, rs.privKey) + if err != nil { + rs.Logger.Error( + "connect", + "err", err, + ) + + return nil, err + } + fmt.Println("connected", conn.RemoteAddr()) + return conn, nil + //} + return nil, ErrDialRetryMax } -func (rs *RemoteSigner) handleConnection(conn net.Conn) { - for { +func (rs *RemoteSigner) handleConnection() { + for { // establish connection loop: if !rs.IsRunning() { return // Ignore error from listener closing. } - + fmt.Println("Connecting again ...") + conn, err := rs.connect() + if err != nil { + rs.Logger.Error("OnStart", "err", err) + fmt.Println("failed to connect", err) + time.Sleep(rs.connDeadline) + continue + } // Reset the connection deadline - conn.SetDeadline(time.Now().Add(rs.connDeadline)) + //conn.SetDeadline(time.Now().Add(rs.connDeadline*10)) - req, err := readMsg(conn) - if err != nil { - if err != io.EOF { - rs.Logger.Error("handleConnection", "err", err) + for { // handle request loop + req, err := readMsg(conn) + if err != nil { + if err != io.EOF { + rs.Logger.Error("handleConnection readMsg", "err", err) + } + conn.Close() + break } - return - } - res, err := handleRequest(req, rs.chainID, rs.privVal) + res, err := handleRequest(req, rs.chainID, rs.privVal) - if err != nil { - // only log the error; we'll reply with an error in res - rs.Logger.Error("handleConnection", "err", err) - } + if err != nil { + // only log the error; we'll reply with an error in res + rs.Logger.Error("handleConnection handleRequest", "err", err) + } - err = writeMsg(conn, res) - if err != nil { - rs.Logger.Error("handleConnection", "err", err) - return + err = writeMsg(conn, res) + if err != nil { + rs.Logger.Error("handleConnection writeMsg", "err", err) + break + } } } }