mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-08 22:23:11 +00:00
This PR is related to #3107 and a continuation of #3351
It is important to emphasise that in the privval original design, client/server and listening/dialing roles are inverted and do not follow a conventional interaction.
Given two hosts A and B:
Host A is listener/client
Host B is dialer/server (contains the secret key)
When A requires a signature, it needs to wait for B to dial in before it can issue a request.
A only accepts a single connection and any failure leads to dropping the connection and waiting for B to reconnect.
The original rationale behind this design was based on security.
Host B only allows outbound connections to a list of whitelisted hosts.
It is not possible to reach B unless B dials in. There are no listening/open ports in B.
This PR results in the following changes:
Refactors ping/heartbeat to avoid previously existing race conditions.
Separates transport (dialer/listener) from signing (client/server) concerns to simplify workflow.
Unifies and abstracts away the differences between unix and tcp sockets.
A single signer endpoint implementation unifies connection handling code (read/write/close/connection obj)
The signer request handler (server side) is customizable to increase testability.
Updates and extends unit tests
A high level overview of the classes is as follows:
Transport (endpoints): The following classes take care of establishing a connection
SignerDialerEndpoint
SignerListeningEndpoint
SignerEndpoint groups common functionality (read/write/timeouts/etc.)
Signing (client/server): The following classes take care of exchanging request/responses
SignerClient
SignerServer
This PR also closes #3601
Commits:
* refactoring - work in progress
* reworking unit tests
* Encapsulating and fixing unit tests
* Improve tests
* Clean up
* Fix/improve unit tests
* clean up tests
* Improving service endpoint
* fixing unit test
* fix linter issues
* avoid invalid cache values (improve later?)
* complete implementation
* wip
* improved connection loop
* Improve reconnections + fixing unit tests
* addressing comments
* small formatting changes
* clean up
* Update node/node.go
Co-Authored-By: jleni <juan.leni@zondax.ch>
* Update privval/signer_client.go
Co-Authored-By: jleni <juan.leni@zondax.ch>
* Update privval/signer_client_test.go
Co-Authored-By: jleni <juan.leni@zondax.ch>
* check during initialization
* dropping connecting when writing fails
* removing break
* use t.log instead
* unifying and using cmn.GetFreePort()
* review fixes
* reordering and unifying drop connection
* closing instead of signalling
* refactored service loop
* removed superfluous brackets
* GetPubKey can return errors
* Revert "GetPubKey can return errors"
This reverts commit 68c06f19b4.
* adding entry to changelog
* Update CHANGELOG_PENDING.md
Co-Authored-By: jleni <juan.leni@zondax.ch>
* Update privval/signer_client.go
Co-Authored-By: jleni <juan.leni@zondax.ch>
* Update privval/signer_dialer_endpoint.go
Co-Authored-By: jleni <juan.leni@zondax.ch>
* Update privval/signer_dialer_endpoint.go
Co-Authored-By: jleni <juan.leni@zondax.ch>
* Update privval/signer_dialer_endpoint.go
Co-Authored-By: jleni <juan.leni@zondax.ch>
* Update privval/signer_dialer_endpoint.go
Co-Authored-By: jleni <juan.leni@zondax.ch>
* Update privval/signer_listener_endpoint_test.go
Co-Authored-By: jleni <juan.leni@zondax.ch>
* updating node.go
* review fixes
* fixes linter
* fixing unit test
* small fixes in comments
* addressing review comments
* addressing review comments 2
* reverting suggestion
* Update privval/signer_client_test.go
Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com>
* Update privval/signer_client_test.go
Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com>
* Update privval/signer_listener_endpoint_test.go
Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com>
* do not expose brokenSignerDialerEndpoint
* clean up logging
* unifying methods
shorten test time
signer also drops
* reenabling pings
* improving testability + unit test
* fixing go fmt + unit test
* remove unused code
* Addressing review comments
* simplifying connection workflow
* fix linter/go import issue
* using base service quit
* updating comment
* Simplifying design + adjusting names
* fixing linter issues
* refactoring test harness + fixes
* Addressing review comments
* cleaning up
* adding additional error check
157 lines
3.4 KiB
Go
157 lines
3.4 KiB
Go
package privval
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
|
)
|
|
|
|
const (
|
|
defaultTimeoutReadWriteSeconds = 3
|
|
)
|
|
|
|
type signerEndpoint struct {
|
|
cmn.BaseService
|
|
|
|
connMtx sync.Mutex
|
|
conn net.Conn
|
|
|
|
timeoutReadWrite time.Duration
|
|
}
|
|
|
|
// Close closes the underlying net.Conn.
|
|
func (se *signerEndpoint) Close() error {
|
|
se.DropConnection()
|
|
return nil
|
|
}
|
|
|
|
// IsConnected indicates if there is an active connection
|
|
func (se *signerEndpoint) IsConnected() bool {
|
|
se.connMtx.Lock()
|
|
defer se.connMtx.Unlock()
|
|
return se.isConnected()
|
|
}
|
|
|
|
// TryGetConnection retrieves a connection if it is already available
|
|
func (se *signerEndpoint) GetAvailableConnection(connectionAvailableCh chan net.Conn) bool {
|
|
se.connMtx.Lock()
|
|
defer se.connMtx.Unlock()
|
|
|
|
// Is there a connection ready?
|
|
select {
|
|
case se.conn = <-connectionAvailableCh:
|
|
return true
|
|
default:
|
|
}
|
|
return false
|
|
}
|
|
|
|
// TryGetConnection retrieves a connection if it is already available
|
|
func (se *signerEndpoint) WaitConnection(connectionAvailableCh chan net.Conn, maxWait time.Duration) error {
|
|
se.connMtx.Lock()
|
|
defer se.connMtx.Unlock()
|
|
|
|
select {
|
|
case se.conn = <-connectionAvailableCh:
|
|
case <-time.After(maxWait):
|
|
return ErrConnectionTimeout
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// SetConnection replaces the current connection object
|
|
func (se *signerEndpoint) SetConnection(newConnection net.Conn) {
|
|
se.connMtx.Lock()
|
|
defer se.connMtx.Unlock()
|
|
se.conn = newConnection
|
|
}
|
|
|
|
// IsConnected indicates if there is an active connection
|
|
func (se *signerEndpoint) DropConnection() {
|
|
se.connMtx.Lock()
|
|
defer se.connMtx.Unlock()
|
|
se.dropConnection()
|
|
}
|
|
|
|
// ReadMessage reads a message from the endpoint
|
|
func (se *signerEndpoint) ReadMessage() (msg SignerMessage, err error) {
|
|
se.connMtx.Lock()
|
|
defer se.connMtx.Unlock()
|
|
|
|
if !se.isConnected() {
|
|
return nil, fmt.Errorf("endpoint is not connected")
|
|
}
|
|
|
|
// Reset read deadline
|
|
deadline := time.Now().Add(se.timeoutReadWrite)
|
|
|
|
err = se.conn.SetReadDeadline(deadline)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
const maxRemoteSignerMsgSize = 1024 * 10
|
|
_, err = cdc.UnmarshalBinaryLengthPrefixedReader(se.conn, &msg, maxRemoteSignerMsgSize)
|
|
if _, ok := err.(timeoutError); ok {
|
|
if err != nil {
|
|
err = errors.Wrap(ErrReadTimeout, err.Error())
|
|
} else {
|
|
err = errors.Wrap(ErrReadTimeout, "Empty error")
|
|
}
|
|
se.Logger.Debug("Dropping [read]", "obj", se)
|
|
se.dropConnection()
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// WriteMessage writes a message from the endpoint
|
|
func (se *signerEndpoint) WriteMessage(msg SignerMessage) (err error) {
|
|
se.connMtx.Lock()
|
|
defer se.connMtx.Unlock()
|
|
|
|
if !se.isConnected() {
|
|
return errors.Wrap(ErrNoConnection, "endpoint is not connected")
|
|
}
|
|
|
|
// Reset read deadline
|
|
deadline := time.Now().Add(se.timeoutReadWrite)
|
|
se.Logger.Debug("Write::Error Resetting deadline", "obj", se)
|
|
|
|
err = se.conn.SetWriteDeadline(deadline)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
_, err = cdc.MarshalBinaryLengthPrefixedWriter(se.conn, msg)
|
|
if _, ok := err.(timeoutError); ok {
|
|
if err != nil {
|
|
err = errors.Wrap(ErrWriteTimeout, err.Error())
|
|
} else {
|
|
err = errors.Wrap(ErrWriteTimeout, "Empty error")
|
|
}
|
|
se.dropConnection()
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (se *signerEndpoint) isConnected() bool {
|
|
return se.conn != nil
|
|
}
|
|
|
|
func (se *signerEndpoint) dropConnection() {
|
|
if se.conn != nil {
|
|
if err := se.conn.Close(); err != nil {
|
|
se.Logger.Error("signerEndpoint::dropConnection", "err", err)
|
|
}
|
|
se.conn = nil
|
|
}
|
|
}
|