Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cmd/tendermint/commands/run_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func AddNodeFlags(cmd *cobra.Command) {

// priv val flags
cmd.Flags().String("priv_validator_addr", config.PrivValidatorAddr, "Socket address for private validator")
cmd.Flags().Int("priv_validator_max_conn", config.PrivValidatorMaxConn, "Limit of concurrent connections to the PrivValidator")

// node flags
cmd.Flags().Bool("fast_sync", config.FastSync, "Fast blockchain syncing")
Expand Down
35 changes: 15 additions & 20 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ var (
defaultConfigFileName = "config.toml"
defaultGenesisJSONName = "genesis.json"

defaultPrivValName = "priv_validator.json"
defaultPrivValMaxConn = 3
defaultNodeKeyName = "node_key.json"
defaultAddrBookName = "addrbook.json"
defaultPrivValName = "priv_validator.json"
defaultNodeKeyName = "node_key.json"
defaultAddrBookName = "addrbook.json"

defaultConfigFilePath = filepath.Join(defaultConfigDir, defaultConfigFileName)
defaultGenesisJSONPath = filepath.Join(defaultConfigDir, defaultGenesisJSONName)
Expand Down Expand Up @@ -108,9 +107,6 @@ type BaseConfig struct {
// TCP or UNIX socket address of the PrivValidator server
PrivValidatorAddr string `mapstructure:"priv_validator_addr"`

// Limit of concurrent connections to the PrivValidator.
PrivValidatorMaxConn int `mapstructure:"priv_validator_max_conn"`

// TCP or UNIX socket address of the ABCI application,
// or the name of an ABCI application compiled in with the Tendermint binary
ProxyApp string `mapstructure:"proxy_app"`
Expand Down Expand Up @@ -147,19 +143,18 @@ func (c BaseConfig) ChainID() string {
// DefaultBaseConfig returns a default base configuration for a Tendermint node
func DefaultBaseConfig() BaseConfig {
return BaseConfig{
Genesis: defaultGenesisJSONPath,
PrivValidator: defaultPrivValPath,
PrivValidatorMaxConn: defaultPrivValMaxConn,
NodeKey: defaultNodeKeyPath,
Moniker: defaultMoniker,
ProxyApp: "tcp://127.0.0.1:46658",
ABCI: "socket",
LogLevel: DefaultPackageLogLevels(),
ProfListenAddress: "",
FastSync: true,
FilterPeers: false,
DBBackend: "leveldb",
DBPath: "data",
Genesis: defaultGenesisJSONPath,
PrivValidator: defaultPrivValPath,
NodeKey: defaultNodeKeyPath,
Moniker: defaultMoniker,
ProxyApp: "tcp://127.0.0.1:46658",
ABCI: "socket",
LogLevel: DefaultPackageLogLevels(),
ProfListenAddress: "",
FastSync: true,
FilterPeers: false,
DBBackend: "leveldb",
DBPath: "data",
}
}

Expand Down
12 changes: 7 additions & 5 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,22 @@ func NewNode(config *cfg.Config,
// reload the state (it may have been updated by the handshake)
state = sm.LoadState(stateDB)

// Connect to external signing process, if an address is provided.
if config.PrivValidatorAddr != "" {
var (
privKey = crypto.GenPrivKeyEd25519()
pvss = priv_val.NewPrivValidatorSocketServer(
pvsc = priv_val.NewSocketClient(
logger.With("module", "priv_val"),
config.ChainID(),
config.PrivValidatorAddr,
config.PrivValidatorMaxConn,
priv_val.LoadPrivValidatorJSON(config.PrivValidatorFile()),
&privKey,
)
)

pvss.Start()
if err := pvsc.Start(); err != nil {
return nil, fmt.Errorf("Error starting private validator client: %v", err)
}

privValidator = pvsc
}

// Decide whether to fast-sync or not
Expand Down
150 changes: 90 additions & 60 deletions types/priv_validator/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
)

const (
connDeadlineSeconds = 3
dialRetryIntervalSeconds = 1
dialRetryMax = 10
defaultConnDeadlineSeconds = 3

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the purpose of default prefix if you can't change it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to overwrite the timeout for the client. Usually the default prefix is used for constants.

defaultDialRetryIntervalSeconds = 1
defaultDialRetryMax = 10
)

// Socket errors.
Expand All @@ -29,100 +29,130 @@ var (
)

var (
connDeadline = time.Second * connDeadlineSeconds
connDeadline = time.Second * defaultConnDeadlineSeconds
)

//-----------------------------------------------------------------
// SocketClientOption sets an optional parameter on the SocketClient.
type SocketClientOption func(*socketClient)

var _ types.PrivValidator2 = (*PrivValidatorSocketClient)(nil)
// SocketClientTimeout sets the timeout for connecting to the external socket
// address.
func SocketClientTimeout(timeout time.Duration) SocketClientOption {
return func(sc *socketClient) { sc.connectTimeout = timeout }
}

// PrivValidatorSocketClient implements PrivValidator.
// It uses a socket to request signatures.
type PrivValidatorSocketClient struct {
// socketClient implements PrivValidator, it uses a socket to request signatures
// from an external process.
type socketClient struct {
cmn.BaseService

conn net.Conn
privKey *crypto.PrivKeyEd25519

ID types.ValidatorID
SocketAddress string
addr string
connectTimeout time.Duration
}

// NewPrivValidatorSocketClient returns an instance of
// PrivValidatorSocketClient.
func NewPrivValidatorSocketClient(
// Check that socketClient implements PrivValidator2.
var _ types.PrivValidator2 = (*socketClient)(nil)

// NewsocketClient returns an instance of socketClient.
func NewSocketClient(
logger log.Logger,
socketAddr string,
privKey *crypto.PrivKeyEd25519,
) *PrivValidatorSocketClient {
pvsc := &PrivValidatorSocketClient{
SocketAddress: socketAddr,
privKey: privKey,
) *socketClient {
sc := &socketClient{
addr: socketAddr,
connectTimeout: time.Second * defaultConnDeadlineSeconds,
privKey: privKey,
}

pvsc.BaseService = *cmn.NewBaseService(logger, "privValidatorSocketClient", pvsc)
sc.BaseService = *cmn.NewBaseService(logger, "privValidatorsocketClient", sc)

return pvsc
return sc
}

// OnStart implements cmn.Service.
func (pvsc *PrivValidatorSocketClient) OnStart() error {
if err := pvsc.BaseService.OnStart(); err != nil {
func (sc *socketClient) OnStart() error {
if err := sc.BaseService.OnStart(); err != nil {
return err
}

conn, err := pvsc.connect()
conn, err := sc.connect()
if err != nil {
return err
}

pvsc.conn = conn
sc.conn = conn

return nil
}

// OnStop implements cmn.Service.
func (pvsc *PrivValidatorSocketClient) OnStop() {
pvsc.BaseService.OnStop()
func (sc *socketClient) OnStop() {
sc.BaseService.OnStop()

if pvsc.conn != nil {
pvsc.conn.Close()
if sc.conn != nil {
sc.conn.Close()
}
}

// GetAddress implements PrivValidator.
// TODO(xla): Remove when PrivValidator2 replaced PrivValidator.
func (sc *socketClient) GetAddress() types.Address {
addr, err := sc.Address()
if err != nil {
panic(err)
}

return addr
}

// Address is an alias for PubKey().Address().
func (pvsc *PrivValidatorSocketClient) Address() (cmn.HexBytes, error) {
p, err := pvsc.PubKey()
func (sc *socketClient) Address() (cmn.HexBytes, error) {
p, err := sc.PubKey()
if err != nil {
return nil, err
}

return p.Address(), nil
}

// PubKey implements PrivValidator.
func (pvsc *PrivValidatorSocketClient) PubKey() (crypto.PubKey, error) {
err := writeMsg(pvsc.conn, &PubKeyMsg{})
// GetPubKey implements PrivValidator.
// TODO(xla): Remove when PrivValidator2 replaced PrivValidator.
func (sc *socketClient) GetPubKey() crypto.PubKey {
pubKey, err := sc.PubKey()
if err != nil {
panic(err)
}

return pubKey
}

// PubKey implements PrivValidator2.
func (sc *socketClient) PubKey() (crypto.PubKey, error) {
err := writeMsg(sc.conn, &PubKeyMsg{})
if err != nil {
return crypto.PubKey{}, err
}

res, err := readMsg(pvsc.conn)
res, err := readMsg(sc.conn)
if err != nil {
return crypto.PubKey{}, err
}

return res.(*PubKeyMsg).PubKey, nil
}

// SignVote implements PrivValidator.
func (pvsc *PrivValidatorSocketClient) SignVote(chainID string, vote *types.Vote) error {
err := writeMsg(pvsc.conn, &SignVoteMsg{Vote: vote})
// SignVote implements PrivValidator2.
func (sc *socketClient) SignVote(chainID string, vote *types.Vote) error {
err := writeMsg(sc.conn, &SignVoteMsg{Vote: vote})
if err != nil {
return err
}

res, err := readMsg(pvsc.conn)
res, err := readMsg(sc.conn)
if err != nil {
return err
}
Expand All @@ -132,14 +162,14 @@ func (pvsc *PrivValidatorSocketClient) SignVote(chainID string, vote *types.Vote
return nil
}

// SignProposal implements PrivValidator.
func (pvsc *PrivValidatorSocketClient) SignProposal(chainID string, proposal *types.Proposal) error {
err := writeMsg(pvsc.conn, &SignProposalMsg{Proposal: proposal})
// SignProposal implements PrivValidator2.
func (sc *socketClient) SignProposal(chainID string, proposal *types.Proposal) error {
err := writeMsg(sc.conn, &SignProposalMsg{Proposal: proposal})
if err != nil {
return err
}

res, err := readMsg(pvsc.conn)
res, err := readMsg(sc.conn)
if err != nil {
return err
}
Expand All @@ -149,14 +179,14 @@ func (pvsc *PrivValidatorSocketClient) SignProposal(chainID string, proposal *ty
return nil
}

// SignHeartbeat implements PrivValidator.
func (pvsc *PrivValidatorSocketClient) SignHeartbeat(chainID string, heartbeat *types.Heartbeat) error {
err := writeMsg(pvsc.conn, &SignHeartbeatMsg{Heartbeat: heartbeat})
// SignHeartbeat implements PrivValidator2.
func (sc *socketClient) SignHeartbeat(chainID string, heartbeat *types.Heartbeat) error {
err := writeMsg(sc.conn, &SignHeartbeatMsg{Heartbeat: heartbeat})
if err != nil {
return err
}

res, err := readMsg(pvsc.conn)
res, err := readMsg(sc.conn)
if err != nil {
return err
}
Expand All @@ -166,41 +196,41 @@ func (pvsc *PrivValidatorSocketClient) SignHeartbeat(chainID string, heartbeat *
return nil
}

func (pvsc *PrivValidatorSocketClient) connect() (net.Conn, error) {
retries := dialRetryMax
func (sc *socketClient) connect() (net.Conn, error) {
retries := defaultDialRetryMax

RETRY_LOOP:
for retries > 0 {
if retries != dialRetryMax {
time.Sleep(time.Second * dialRetryIntervalSeconds)
if retries != defaultDialRetryMax {
time.Sleep(sc.connectTimeout)
}

retries--

conn, err := cmn.Connect(pvsc.SocketAddress)
conn, err := cmn.Connect(sc.addr)
if err != nil {
pvsc.Logger.Error(
"pvsc connect",
"addr", pvsc.SocketAddress,
sc.Logger.Error(
"sc connect",
"addr", sc.addr,
"err", errors.Wrap(err, "connection failed"),
)

continue RETRY_LOOP
}

if err := conn.SetDeadline(time.Now().Add(connDeadline)); err != nil {
pvsc.Logger.Error(
"pvsc connect",
sc.Logger.Error(
"sc connect",
"err", errors.Wrap(err, "setting connection timeout failed"),
)
continue
}

if pvsc.privKey != nil {
conn, err = p2pconn.MakeSecretConnection(conn, pvsc.privKey.Wrap())
if sc.privKey != nil {
conn, err = p2pconn.MakeSecretConnection(conn, sc.privKey.Wrap())
if err != nil {
pvsc.Logger.Error(
"pvsc connect",
sc.Logger.Error(
"sc connect",
"err", errors.Wrap(err, "encrypting connection failed"),
)

Expand Down
Loading