You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.4 KiB

privval: improve Remote Signer implementation (#3351) This issue is related to #3107 This is a first renaming/refactoring step before reworking and removing heartbeats. As discussed with @Liamsi , we preferred to go for a couple of independent and separate PRs to simplify review work. The changes: Help to clarify the relation between the validator and remote signer endpoints Differentiate between timeouts and deadlines Prepare to encapsulate networking related code behind RemoteSigner in the next PR My intention is to separate and encapsulate the "network related" code from the actual signer. SignerRemote ---(uses/contains)--> SignerValidatorEndpoint <--(connects to)--> SignerServiceEndpoint ---> SignerService (future.. not here yet but would like to decouple too) All reconnection/heartbeat/whatever code goes in the endpoints. Signer[Remote/Service] do not need to know about that. I agree Endpoint may not be the perfect name. I tried to find something "Go-ish" enough. It is a common name in go-kit, kubernetes, etc. Right now: SignerValidatorEndpoint: handles the listener contains SignerRemote Implements the PrivValidator interface connects and sets a connection object in a contained SignerRemote delegates PrivValidator some calls to SignerRemote which in turn uses the conn object that was set externally SignerRemote: Implements the PrivValidator interface read/writes from a connection object directly handles heartbeats SignerServiceEndpoint: Does most things in a single place delegates to a PrivValidator IIRC. * cleanup * Refactoring step 1 * Refactoring step 2 * move messages to another file * mark for future work / next steps * mark deprecated classes in docs * Fix linter problems * additional linter fixes
5 years ago
  1. package privval
  2. import (
  3. "io"
  4. "net"
  5. "time"
  6. cmn "github.com/tendermint/tendermint/libs/common"
  7. "github.com/tendermint/tendermint/libs/log"
  8. "github.com/tendermint/tendermint/types"
  9. )
  10. // SignerServiceEndpointOption sets an optional parameter on the SignerServiceEndpoint.
  11. type SignerServiceEndpointOption func(*SignerServiceEndpoint)
  12. // SignerServiceEndpointTimeoutReadWrite sets the read and write timeout for connections
  13. // from external signing processes.
  14. func SignerServiceEndpointTimeoutReadWrite(timeout time.Duration) SignerServiceEndpointOption {
  15. return func(ss *SignerServiceEndpoint) { ss.timeoutReadWrite = timeout }
  16. }
  17. // SignerServiceEndpointConnRetries sets the amount of attempted retries to connect.
  18. func SignerServiceEndpointConnRetries(retries int) SignerServiceEndpointOption {
  19. return func(ss *SignerServiceEndpoint) { ss.connRetries = retries }
  20. }
  21. // SignerServiceEndpoint dials using its dialer and responds to any
  22. // signature requests using its privVal.
  23. type SignerServiceEndpoint struct {
  24. cmn.BaseService
  25. chainID string
  26. timeoutReadWrite time.Duration
  27. connRetries int
  28. privVal types.PrivValidator
  29. dialer SocketDialer
  30. conn net.Conn
  31. }
  32. // NewSignerServiceEndpoint returns a SignerServiceEndpoint that will dial using the given
  33. // dialer and respond to any signature requests over the connection
  34. // using the given privVal.
  35. func NewSignerServiceEndpoint(
  36. logger log.Logger,
  37. chainID string,
  38. privVal types.PrivValidator,
  39. dialer SocketDialer,
  40. ) *SignerServiceEndpoint {
  41. se := &SignerServiceEndpoint{
  42. chainID: chainID,
  43. timeoutReadWrite: time.Second * defaultTimeoutReadWriteSeconds,
  44. connRetries: defaultMaxDialRetries,
  45. privVal: privVal,
  46. dialer: dialer,
  47. }
  48. se.BaseService = *cmn.NewBaseService(logger, "SignerServiceEndpoint", se)
  49. return se
  50. }
  51. // OnStart implements cmn.Service.
  52. func (se *SignerServiceEndpoint) OnStart() error {
  53. conn, err := se.connect()
  54. if err != nil {
  55. se.Logger.Error("OnStart", "err", err)
  56. return err
  57. }
  58. se.conn = conn
  59. go se.handleConnection(conn)
  60. return nil
  61. }
  62. // OnStop implements cmn.Service.
  63. func (se *SignerServiceEndpoint) OnStop() {
  64. if se.conn == nil {
  65. return
  66. }
  67. if err := se.conn.Close(); err != nil {
  68. se.Logger.Error("OnStop", "err", cmn.ErrorWrap(err, "closing listener failed"))
  69. }
  70. }
  71. func (se *SignerServiceEndpoint) connect() (net.Conn, error) {
  72. for retries := 0; retries < se.connRetries; retries++ {
  73. // Don't sleep if it is the first retry.
  74. if retries > 0 {
  75. time.Sleep(se.timeoutReadWrite)
  76. }
  77. conn, err := se.dialer()
  78. if err == nil {
  79. return conn, nil
  80. }
  81. se.Logger.Error("dialing", "err", err)
  82. }
  83. return nil, ErrDialRetryMax
  84. }
  85. func (se *SignerServiceEndpoint) handleConnection(conn net.Conn) {
  86. for {
  87. if !se.IsRunning() {
  88. return // Ignore error from listener closing.
  89. }
  90. // Reset the connection deadline
  91. deadline := time.Now().Add(se.timeoutReadWrite)
  92. err := conn.SetDeadline(deadline)
  93. if err != nil {
  94. return
  95. }
  96. req, err := readMsg(conn)
  97. if err != nil {
  98. if err != io.EOF {
  99. se.Logger.Error("handleConnection readMsg", "err", err)
  100. }
  101. return
  102. }
  103. res, err := handleRequest(req, se.chainID, se.privVal)
  104. if err != nil {
  105. // only log the error; we'll reply with an error in res
  106. se.Logger.Error("handleConnection handleRequest", "err", err)
  107. }
  108. err = writeMsg(conn, res)
  109. if err != nil {
  110. se.Logger.Error("handleConnection writeMsg", "err", err)
  111. return
  112. }
  113. }
  114. }