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.

246 lines
6.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
6 years ago
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
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
6 years ago
6 years ago
6 years ago
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
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
6 years ago
6 years ago
6 years ago
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
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
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
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
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package privval
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/tendermint/libs/common"
  8. "github.com/tendermint/tendermint/types"
  9. )
  10. type signerTestCase struct {
  11. chainID string
  12. mockPV types.PrivValidator
  13. signer *SignerClient
  14. signerService *SignerDialerEndpoint
  15. }
  16. func getSignerTestCases(t *testing.T) []signerTestCase {
  17. testCases := make([]signerTestCase, 0)
  18. for _, dtc := range getDialerTestCases(t) {
  19. chainID := common.RandStr(12)
  20. mockPV := types.NewMockPV()
  21. ve, se := getMockEndpoints(t, chainID, mockPV, dtc.addr, dtc.dialer)
  22. sr, err := NewSignerClient(ve)
  23. assert.NoError(t, err)
  24. tc := signerTestCase{
  25. chainID: chainID,
  26. mockPV: mockPV,
  27. signer: sr,
  28. signerService: se,
  29. }
  30. testCases = append(testCases, tc)
  31. break
  32. }
  33. return testCases
  34. }
  35. func TestSignerClose(t *testing.T) {
  36. for _, tc := range getSignerTestCases(t) {
  37. func() {
  38. err := tc.signer.Close()
  39. assert.NoError(t, err)
  40. err = tc.signerService.Stop()
  41. assert.NoError(t, err)
  42. }()
  43. }
  44. }
  45. func TestSignerGetPubKey(t *testing.T) {
  46. for _, tc := range getSignerTestCases(t) {
  47. func() {
  48. defer tc.signerService.OnStop()
  49. defer tc.signer.Close()
  50. pubKey := tc.signer.GetPubKey()
  51. expectedPubKey := tc.mockPV.GetPubKey()
  52. assert.Equal(t, expectedPubKey, pubKey)
  53. addr := tc.signer.GetPubKey().Address()
  54. expectedAddr := tc.mockPV.GetPubKey().Address()
  55. assert.Equal(t, expectedAddr, addr)
  56. }()
  57. }
  58. }
  59. func TestSignerProposal(t *testing.T) {
  60. for _, tc := range getSignerTestCases(t) {
  61. func() {
  62. ts := time.Now()
  63. want := &types.Proposal{Timestamp: ts}
  64. have := &types.Proposal{Timestamp: ts}
  65. defer tc.signerService.OnStop()
  66. defer tc.signer.Close()
  67. require.NoError(t, tc.mockPV.SignProposal(tc.chainID, want))
  68. require.NoError(t, tc.signer.SignProposal(tc.chainID, have))
  69. assert.Equal(t, want.Signature, have.Signature)
  70. }()
  71. }
  72. }
  73. func TestSignerVote(t *testing.T) {
  74. for _, tc := range getSignerTestCases(t) {
  75. func() {
  76. ts := time.Now()
  77. want := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
  78. have := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
  79. defer tc.signerService.OnStop()
  80. defer tc.signer.Close()
  81. require.NoError(t, tc.mockPV.SignVote(tc.chainID, want))
  82. require.NoError(t, tc.signer.SignVote(tc.chainID, have))
  83. assert.Equal(t, want.Signature, have.Signature)
  84. }()
  85. }
  86. }
  87. func TestSignerVoteResetDeadline(t *testing.T) {
  88. for _, tc := range getSignerTestCases(t) {
  89. func() {
  90. ts := time.Now()
  91. want := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
  92. have := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
  93. defer tc.signerService.OnStop()
  94. defer tc.signer.Close()
  95. time.Sleep(testTimeoutReadWrite2o3)
  96. require.NoError(t, tc.mockPV.SignVote(tc.chainID, want))
  97. require.NoError(t, tc.signer.SignVote(tc.chainID, have))
  98. assert.Equal(t, want.Signature, have.Signature)
  99. // TODO(jleni): Clarify what is actually being tested
  100. // This would exceed the deadline if it was not extended by the previous message
  101. time.Sleep(testTimeoutReadWrite2o3)
  102. require.NoError(t, tc.mockPV.SignVote(tc.chainID, want))
  103. require.NoError(t, tc.signer.SignVote(tc.chainID, have))
  104. assert.Equal(t, want.Signature, have.Signature)
  105. }()
  106. }
  107. }
  108. func TestSignerVoteKeepAlive(t *testing.T) {
  109. for _, tc := range getSignerTestCases(t) {
  110. func() {
  111. ts := time.Now()
  112. want := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
  113. have := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
  114. defer tc.signerService.OnStop()
  115. defer tc.signer.Close()
  116. // Check that even if the client does not request a
  117. // signature for a long time. The service is will available
  118. tc.signerService.Logger.Info("TEST. Forced Wait")
  119. time.Sleep(testTimeoutReadWrite * 2)
  120. tc.signerService.Logger.Info("TEST. Forced Wait - DONE")
  121. require.NoError(t, tc.mockPV.SignVote(tc.chainID, want))
  122. require.NoError(t, tc.signer.SignVote(tc.chainID, have))
  123. assert.Equal(t, want.Signature, have.Signature)
  124. }()
  125. }
  126. }
  127. func TestSignerSignProposalErrors(t *testing.T) {
  128. for _, tc := range getSignerTestCases(t) {
  129. func() {
  130. // Replace service with a mock that always fails
  131. tc.signerService.privVal = types.NewErroringMockPV()
  132. tc.mockPV = types.NewErroringMockPV()
  133. defer tc.signerService.OnStop()
  134. defer tc.signer.Close()
  135. //ts := time.Now()
  136. //proposal := &types.Proposal{Timestamp: ts}
  137. //err := tc.signer.SignProposal(tc.chainID, proposal)
  138. //require.Equal(t, err.(*RemoteSignerError).Description, types.ErroringMockPVErr.Error())
  139. //
  140. //err = tc.mockPV.SignProposal(tc.chainID, proposal)
  141. //require.Error(t, err)
  142. //
  143. //err = tc.signer.SignProposal(tc.chainID, proposal)
  144. //require.Error(t, err)
  145. }()
  146. }
  147. }
  148. func TestSignerSignVoteErrors(t *testing.T) {
  149. for _, tc := range getSignerTestCases(t) {
  150. func() {
  151. ts := time.Now()
  152. vote := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
  153. // Replace signer service privval with one that always fails
  154. tc.signerService.privVal = types.NewErroringMockPV()
  155. tc.mockPV = types.NewErroringMockPV()
  156. defer tc.signerService.OnStop()
  157. defer tc.signer.Close()
  158. err := tc.signer.SignVote(tc.chainID, vote)
  159. require.Equal(t, err.(*RemoteSignerError).Description, types.ErroringMockPVErr.Error())
  160. err = tc.mockPV.SignVote(tc.chainID, vote)
  161. require.Error(t, err)
  162. err = tc.signer.SignVote(tc.chainID, vote)
  163. require.Error(t, err)
  164. }()
  165. }
  166. }
  167. type BrokenSignerDialerEndpoint struct {
  168. *SignerDialerEndpoint
  169. }
  170. func (ss *BrokenSignerDialerEndpoint) writeMessage(msg RemoteSignerMsg) (err error) {
  171. _, err = cdc.MarshalBinaryLengthPrefixedWriter(ss.conn, PubKeyResponse{})
  172. return
  173. }
  174. func TestSignerUnexpectedResponse(t *testing.T) {
  175. for _, tc := range getSignerTestCases(t) {
  176. func() {
  177. // TODO(jleni): This test is actually not working. Fails for a different reason
  178. tc.signerService.privVal = types.NewErroringMockPV()
  179. tc.mockPV = types.NewErroringMockPV()
  180. // Replace signer service with a broken one
  181. tc.signerService.OnStop()
  182. tmp := BrokenSignerDialerEndpoint{tc.signerService}
  183. tmp.OnStart()
  184. defer tmp.OnStop()
  185. defer tc.signer.Close()
  186. ts := time.Now()
  187. want := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
  188. e := tc.signer.SignVote(tc.chainID, want)
  189. println(e.Error())
  190. require.Error(t, e)
  191. }()
  192. }
  193. }