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.

68 lines
1.7 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
6 years ago
  1. package privval
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/tendermint/crypto/ed25519"
  9. cmn "github.com/tendermint/tendermint/libs/common"
  10. "github.com/tendermint/tendermint/libs/log"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. // TestSignerRemoteRetryTCPOnly will test connection retry attempts over TCP. We
  14. // don't need this for Unix sockets because the OS instantly knows the state of
  15. // both ends of the socket connection. This basically causes the
  16. // SignerServiceEndpoint.dialer() call inside SignerServiceEndpoint.connect() to return
  17. // successfully immediately, putting an instant stop to any retry attempts.
  18. func TestSignerRemoteRetryTCPOnly(t *testing.T) {
  19. var (
  20. attemptCh = make(chan int)
  21. retries = 2
  22. )
  23. ln, err := net.Listen("tcp", "127.0.0.1:0")
  24. require.NoError(t, err)
  25. go func(ln net.Listener, attemptCh chan<- int) {
  26. attempts := 0
  27. for {
  28. conn, err := ln.Accept()
  29. require.NoError(t, err)
  30. err = conn.Close()
  31. require.NoError(t, err)
  32. attempts++
  33. if attempts == retries {
  34. attemptCh <- attempts
  35. break
  36. }
  37. }
  38. }(ln, attemptCh)
  39. serviceEndpoint := NewSignerServiceEndpoint(
  40. log.TestingLogger(),
  41. cmn.RandStr(12),
  42. types.NewMockPV(),
  43. DialTCPFn(ln.Addr().String(), testTimeoutReadWrite, ed25519.GenPrivKey()),
  44. )
  45. defer serviceEndpoint.Stop()
  46. SignerServiceEndpointTimeoutReadWrite(time.Millisecond)(serviceEndpoint)
  47. SignerServiceEndpointConnRetries(retries)(serviceEndpoint)
  48. assert.Equal(t, serviceEndpoint.Start(), ErrDialRetryMax)
  49. select {
  50. case attempts := <-attemptCh:
  51. assert.Equal(t, retries, attempts)
  52. case <-time.After(100 * time.Millisecond):
  53. t.Error("expected remote to observe connection attempts")
  54. }
  55. }