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.

107 lines
2.4 KiB

privval: refactor Remote signers (#3370) 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 68c06f19b4650389d7e5ab1659b318889028202c. * 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
5 years ago
  1. package privval
  2. import (
  3. "io"
  4. "sync"
  5. cmn "github.com/tendermint/tendermint/libs/common"
  6. "github.com/tendermint/tendermint/types"
  7. )
  8. // ValidationRequestHandlerFunc handles different remoteSigner requests
  9. type ValidationRequestHandlerFunc func(
  10. privVal types.PrivValidator,
  11. requestMessage SignerMessage,
  12. chainID string) (SignerMessage, error)
  13. type SignerServer struct {
  14. cmn.BaseService
  15. endpoint *SignerDialerEndpoint
  16. chainID string
  17. privVal types.PrivValidator
  18. handlerMtx sync.Mutex
  19. validationRequestHandler ValidationRequestHandlerFunc
  20. }
  21. func NewSignerServer(endpoint *SignerDialerEndpoint, chainID string, privVal types.PrivValidator) *SignerServer {
  22. ss := &SignerServer{
  23. endpoint: endpoint,
  24. chainID: chainID,
  25. privVal: privVal,
  26. validationRequestHandler: DefaultValidationRequestHandler,
  27. }
  28. ss.BaseService = *cmn.NewBaseService(endpoint.Logger, "SignerServer", ss)
  29. return ss
  30. }
  31. // OnStart implements cmn.Service.
  32. func (ss *SignerServer) OnStart() error {
  33. go ss.serviceLoop()
  34. return nil
  35. }
  36. // OnStop implements cmn.Service.
  37. func (ss *SignerServer) OnStop() {
  38. ss.endpoint.Logger.Debug("SignerServer: OnStop calling Close")
  39. _ = ss.endpoint.Close()
  40. }
  41. // SetRequestHandler override the default function that is used to service requests
  42. func (ss *SignerServer) SetRequestHandler(validationRequestHandler ValidationRequestHandlerFunc) {
  43. ss.handlerMtx.Lock()
  44. defer ss.handlerMtx.Unlock()
  45. ss.validationRequestHandler = validationRequestHandler
  46. }
  47. func (ss *SignerServer) servicePendingRequest() {
  48. if !ss.IsRunning() {
  49. return // Ignore error from closing.
  50. }
  51. req, err := ss.endpoint.ReadMessage()
  52. if err != nil {
  53. if err != io.EOF {
  54. ss.Logger.Error("SignerServer: HandleMessage", "err", err)
  55. }
  56. return
  57. }
  58. var res SignerMessage
  59. {
  60. // limit the scope of the lock
  61. ss.handlerMtx.Lock()
  62. defer ss.handlerMtx.Unlock()
  63. res, err = ss.validationRequestHandler(ss.privVal, req, ss.chainID)
  64. if err != nil {
  65. // only log the error; we'll reply with an error in res
  66. ss.Logger.Error("SignerServer: handleMessage", "err", err)
  67. }
  68. }
  69. if res != nil {
  70. err = ss.endpoint.WriteMessage(res)
  71. if err != nil {
  72. ss.Logger.Error("SignerServer: writeMessage", "err", err)
  73. }
  74. }
  75. }
  76. func (ss *SignerServer) serviceLoop() {
  77. for {
  78. select {
  79. default:
  80. err := ss.endpoint.ensureConnection()
  81. if err != nil {
  82. return
  83. }
  84. ss.servicePendingRequest()
  85. case <-ss.Quit():
  86. return
  87. }
  88. }
  89. }