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.

305 lines
7.7 KiB

8 years ago
8 years ago
7 years ago
Close and retry a RemoteSigner on err (#2923) * Close and recreate a RemoteSigner on err * Update changelog * Address Anton's comments / suggestions: - update changelog - restart TCPVal - shut down on `ErrUnexpectedResponse` * re-init remote signer client with fresh connection if Ping fails - add/update TODOs in secret connection - rename tcp.go -> tcp_client.go, same with ipc to clarify their purpose * account for `conn returned by waitConnection can be `nil` - also add TODO about RemoteSigner conn field * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn - add rwmutex for conn field in IPC * comments and doc.go * fix ipc tests. fixes #2677 * use constants for tests * cleanup some error statements * fixes #2784, race in tests * remove print statement * minor fixes from review * update comment on sts spec * cosmetics * p2p/conn: add failing tests * p2p/conn: make SecretConnection thread safe * changelog * IPCVal signer refactor - use a .reset() method - don't use embedded RemoteSignerClient - guard RemoteSignerClient with mutex - drop the .conn - expose Close() on RemoteSignerClient * apply IPCVal refactor to TCPVal * remove mtx from RemoteSignerClient * consolidate IPCVal and TCPVal, fixes #3104 - done in tcp_client.go - now called SocketVal - takes a listener in the constructor - make tcpListener and unixListener contain all the differences * delete ipc files * introduce unix and tcp dialer for RemoteSigner * rename files - drop tcp_ prefix - rename priv_validator.go to file.go * bring back listener options * fix node * fix priv_val_server * fix node test * minor cleanup and comments
6 years ago
Close and retry a RemoteSigner on err (#2923) * Close and recreate a RemoteSigner on err * Update changelog * Address Anton's comments / suggestions: - update changelog - restart TCPVal - shut down on `ErrUnexpectedResponse` * re-init remote signer client with fresh connection if Ping fails - add/update TODOs in secret connection - rename tcp.go -> tcp_client.go, same with ipc to clarify their purpose * account for `conn returned by waitConnection can be `nil` - also add TODO about RemoteSigner conn field * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn - add rwmutex for conn field in IPC * comments and doc.go * fix ipc tests. fixes #2677 * use constants for tests * cleanup some error statements * fixes #2784, race in tests * remove print statement * minor fixes from review * update comment on sts spec * cosmetics * p2p/conn: add failing tests * p2p/conn: make SecretConnection thread safe * changelog * IPCVal signer refactor - use a .reset() method - don't use embedded RemoteSignerClient - guard RemoteSignerClient with mutex - drop the .conn - expose Close() on RemoteSignerClient * apply IPCVal refactor to TCPVal * remove mtx from RemoteSignerClient * consolidate IPCVal and TCPVal, fixes #3104 - done in tcp_client.go - now called SocketVal - takes a listener in the constructor - make tcpListener and unixListener contain all the differences * delete ipc files * introduce unix and tcp dialer for RemoteSigner * rename files - drop tcp_ prefix - rename priv_validator.go to file.go * bring back listener options * fix node * fix priv_val_server * fix node test * minor cleanup and comments
6 years ago
Close and retry a RemoteSigner on err (#2923) * Close and recreate a RemoteSigner on err * Update changelog * Address Anton's comments / suggestions: - update changelog - restart TCPVal - shut down on `ErrUnexpectedResponse` * re-init remote signer client with fresh connection if Ping fails - add/update TODOs in secret connection - rename tcp.go -> tcp_client.go, same with ipc to clarify their purpose * account for `conn returned by waitConnection can be `nil` - also add TODO about RemoteSigner conn field * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn - add rwmutex for conn field in IPC * comments and doc.go * fix ipc tests. fixes #2677 * use constants for tests * cleanup some error statements * fixes #2784, race in tests * remove print statement * minor fixes from review * update comment on sts spec * cosmetics * p2p/conn: add failing tests * p2p/conn: make SecretConnection thread safe * changelog * IPCVal signer refactor - use a .reset() method - don't use embedded RemoteSignerClient - guard RemoteSignerClient with mutex - drop the .conn - expose Close() on RemoteSignerClient * apply IPCVal refactor to TCPVal * remove mtx from RemoteSignerClient * consolidate IPCVal and TCPVal, fixes #3104 - done in tcp_client.go - now called SocketVal - takes a listener in the constructor - make tcpListener and unixListener contain all the differences * delete ipc files * introduce unix and tcp dialer for RemoteSigner * rename files - drop tcp_ prefix - rename priv_validator.go to file.go * bring back listener options * fix node * fix priv_val_server * fix node test * minor cleanup and comments
6 years ago
Close and retry a RemoteSigner on err (#2923) * Close and recreate a RemoteSigner on err * Update changelog * Address Anton's comments / suggestions: - update changelog - restart TCPVal - shut down on `ErrUnexpectedResponse` * re-init remote signer client with fresh connection if Ping fails - add/update TODOs in secret connection - rename tcp.go -> tcp_client.go, same with ipc to clarify their purpose * account for `conn returned by waitConnection can be `nil` - also add TODO about RemoteSigner conn field * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn - add rwmutex for conn field in IPC * comments and doc.go * fix ipc tests. fixes #2677 * use constants for tests * cleanup some error statements * fixes #2784, race in tests * remove print statement * minor fixes from review * update comment on sts spec * cosmetics * p2p/conn: add failing tests * p2p/conn: make SecretConnection thread safe * changelog * IPCVal signer refactor - use a .reset() method - don't use embedded RemoteSignerClient - guard RemoteSignerClient with mutex - drop the .conn - expose Close() on RemoteSignerClient * apply IPCVal refactor to TCPVal * remove mtx from RemoteSignerClient * consolidate IPCVal and TCPVal, fixes #3104 - done in tcp_client.go - now called SocketVal - takes a listener in the constructor - make tcpListener and unixListener contain all the differences * delete ipc files * introduce unix and tcp dialer for RemoteSigner * rename files - drop tcp_ prefix - rename priv_validator.go to file.go * bring back listener options * fix node * fix priv_val_server * fix node test * minor cleanup and comments
6 years ago
Close and retry a RemoteSigner on err (#2923) * Close and recreate a RemoteSigner on err * Update changelog * Address Anton's comments / suggestions: - update changelog - restart TCPVal - shut down on `ErrUnexpectedResponse` * re-init remote signer client with fresh connection if Ping fails - add/update TODOs in secret connection - rename tcp.go -> tcp_client.go, same with ipc to clarify their purpose * account for `conn returned by waitConnection can be `nil` - also add TODO about RemoteSigner conn field * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn - add rwmutex for conn field in IPC * comments and doc.go * fix ipc tests. fixes #2677 * use constants for tests * cleanup some error statements * fixes #2784, race in tests * remove print statement * minor fixes from review * update comment on sts spec * cosmetics * p2p/conn: add failing tests * p2p/conn: make SecretConnection thread safe * changelog * IPCVal signer refactor - use a .reset() method - don't use embedded RemoteSignerClient - guard RemoteSignerClient with mutex - drop the .conn - expose Close() on RemoteSignerClient * apply IPCVal refactor to TCPVal * remove mtx from RemoteSignerClient * consolidate IPCVal and TCPVal, fixes #3104 - done in tcp_client.go - now called SocketVal - takes a listener in the constructor - make tcpListener and unixListener contain all the differences * delete ipc files * introduce unix and tcp dialer for RemoteSigner * rename files - drop tcp_ prefix - rename priv_validator.go to file.go * bring back listener options * fix node * fix priv_val_server * fix node test * minor cleanup and comments
6 years ago
Close and retry a RemoteSigner on err (#2923) * Close and recreate a RemoteSigner on err * Update changelog * Address Anton's comments / suggestions: - update changelog - restart TCPVal - shut down on `ErrUnexpectedResponse` * re-init remote signer client with fresh connection if Ping fails - add/update TODOs in secret connection - rename tcp.go -> tcp_client.go, same with ipc to clarify their purpose * account for `conn returned by waitConnection can be `nil` - also add TODO about RemoteSigner conn field * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn - add rwmutex for conn field in IPC * comments and doc.go * fix ipc tests. fixes #2677 * use constants for tests * cleanup some error statements * fixes #2784, race in tests * remove print statement * minor fixes from review * update comment on sts spec * cosmetics * p2p/conn: add failing tests * p2p/conn: make SecretConnection thread safe * changelog * IPCVal signer refactor - use a .reset() method - don't use embedded RemoteSignerClient - guard RemoteSignerClient with mutex - drop the .conn - expose Close() on RemoteSignerClient * apply IPCVal refactor to TCPVal * remove mtx from RemoteSignerClient * consolidate IPCVal and TCPVal, fixes #3104 - done in tcp_client.go - now called SocketVal - takes a listener in the constructor - make tcpListener and unixListener contain all the differences * delete ipc files * introduce unix and tcp dialer for RemoteSigner * rename files - drop tcp_ prefix - rename priv_validator.go to file.go * bring back listener options * fix node * fix priv_val_server * fix node test * minor cleanup and comments
6 years ago
Close and retry a RemoteSigner on err (#2923) * Close and recreate a RemoteSigner on err * Update changelog * Address Anton's comments / suggestions: - update changelog - restart TCPVal - shut down on `ErrUnexpectedResponse` * re-init remote signer client with fresh connection if Ping fails - add/update TODOs in secret connection - rename tcp.go -> tcp_client.go, same with ipc to clarify their purpose * account for `conn returned by waitConnection can be `nil` - also add TODO about RemoteSigner conn field * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn - add rwmutex for conn field in IPC * comments and doc.go * fix ipc tests. fixes #2677 * use constants for tests * cleanup some error statements * fixes #2784, race in tests * remove print statement * minor fixes from review * update comment on sts spec * cosmetics * p2p/conn: add failing tests * p2p/conn: make SecretConnection thread safe * changelog * IPCVal signer refactor - use a .reset() method - don't use embedded RemoteSignerClient - guard RemoteSignerClient with mutex - drop the .conn - expose Close() on RemoteSignerClient * apply IPCVal refactor to TCPVal * remove mtx from RemoteSignerClient * consolidate IPCVal and TCPVal, fixes #3104 - done in tcp_client.go - now called SocketVal - takes a listener in the constructor - make tcpListener and unixListener contain all the differences * delete ipc files * introduce unix and tcp dialer for RemoteSigner * rename files - drop tcp_ prefix - rename priv_validator.go to file.go * bring back listener options * fix node * fix priv_val_server * fix node test * minor cleanup and comments
6 years ago
Close and retry a RemoteSigner on err (#2923) * Close and recreate a RemoteSigner on err * Update changelog * Address Anton's comments / suggestions: - update changelog - restart TCPVal - shut down on `ErrUnexpectedResponse` * re-init remote signer client with fresh connection if Ping fails - add/update TODOs in secret connection - rename tcp.go -> tcp_client.go, same with ipc to clarify their purpose * account for `conn returned by waitConnection can be `nil` - also add TODO about RemoteSigner conn field * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn - add rwmutex for conn field in IPC * comments and doc.go * fix ipc tests. fixes #2677 * use constants for tests * cleanup some error statements * fixes #2784, race in tests * remove print statement * minor fixes from review * update comment on sts spec * cosmetics * p2p/conn: add failing tests * p2p/conn: make SecretConnection thread safe * changelog * IPCVal signer refactor - use a .reset() method - don't use embedded RemoteSignerClient - guard RemoteSignerClient with mutex - drop the .conn - expose Close() on RemoteSignerClient * apply IPCVal refactor to TCPVal * remove mtx from RemoteSignerClient * consolidate IPCVal and TCPVal, fixes #3104 - done in tcp_client.go - now called SocketVal - takes a listener in the constructor - make tcpListener and unixListener contain all the differences * delete ipc files * introduce unix and tcp dialer for RemoteSigner * rename files - drop tcp_ prefix - rename priv_validator.go to file.go * bring back listener options * fix node * fix priv_val_server * fix node test * minor cleanup and comments
6 years ago
Close and retry a RemoteSigner on err (#2923) * Close and recreate a RemoteSigner on err * Update changelog * Address Anton's comments / suggestions: - update changelog - restart TCPVal - shut down on `ErrUnexpectedResponse` * re-init remote signer client with fresh connection if Ping fails - add/update TODOs in secret connection - rename tcp.go -> tcp_client.go, same with ipc to clarify their purpose * account for `conn returned by waitConnection can be `nil` - also add TODO about RemoteSigner conn field * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn - add rwmutex for conn field in IPC * comments and doc.go * fix ipc tests. fixes #2677 * use constants for tests * cleanup some error statements * fixes #2784, race in tests * remove print statement * minor fixes from review * update comment on sts spec * cosmetics * p2p/conn: add failing tests * p2p/conn: make SecretConnection thread safe * changelog * IPCVal signer refactor - use a .reset() method - don't use embedded RemoteSignerClient - guard RemoteSignerClient with mutex - drop the .conn - expose Close() on RemoteSignerClient * apply IPCVal refactor to TCPVal * remove mtx from RemoteSignerClient * consolidate IPCVal and TCPVal, fixes #3104 - done in tcp_client.go - now called SocketVal - takes a listener in the constructor - make tcpListener and unixListener contain all the differences * delete ipc files * introduce unix and tcp dialer for RemoteSigner * rename files - drop tcp_ prefix - rename priv_validator.go to file.go * bring back listener options * fix node * fix priv_val_server * fix node test * minor cleanup and comments
6 years ago
Close and retry a RemoteSigner on err (#2923) * Close and recreate a RemoteSigner on err * Update changelog * Address Anton's comments / suggestions: - update changelog - restart TCPVal - shut down on `ErrUnexpectedResponse` * re-init remote signer client with fresh connection if Ping fails - add/update TODOs in secret connection - rename tcp.go -> tcp_client.go, same with ipc to clarify their purpose * account for `conn returned by waitConnection can be `nil` - also add TODO about RemoteSigner conn field * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn - add rwmutex for conn field in IPC * comments and doc.go * fix ipc tests. fixes #2677 * use constants for tests * cleanup some error statements * fixes #2784, race in tests * remove print statement * minor fixes from review * update comment on sts spec * cosmetics * p2p/conn: add failing tests * p2p/conn: make SecretConnection thread safe * changelog * IPCVal signer refactor - use a .reset() method - don't use embedded RemoteSignerClient - guard RemoteSignerClient with mutex - drop the .conn - expose Close() on RemoteSignerClient * apply IPCVal refactor to TCPVal * remove mtx from RemoteSignerClient * consolidate IPCVal and TCPVal, fixes #3104 - done in tcp_client.go - now called SocketVal - takes a listener in the constructor - make tcpListener and unixListener contain all the differences * delete ipc files * introduce unix and tcp dialer for RemoteSigner * rename files - drop tcp_ prefix - rename priv_validator.go to file.go * bring back listener options * fix node * fix priv_val_server * fix node test * minor cleanup and comments
6 years ago
Close and retry a RemoteSigner on err (#2923) * Close and recreate a RemoteSigner on err * Update changelog * Address Anton's comments / suggestions: - update changelog - restart TCPVal - shut down on `ErrUnexpectedResponse` * re-init remote signer client with fresh connection if Ping fails - add/update TODOs in secret connection - rename tcp.go -> tcp_client.go, same with ipc to clarify their purpose * account for `conn returned by waitConnection can be `nil` - also add TODO about RemoteSigner conn field * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn - add rwmutex for conn field in IPC * comments and doc.go * fix ipc tests. fixes #2677 * use constants for tests * cleanup some error statements * fixes #2784, race in tests * remove print statement * minor fixes from review * update comment on sts spec * cosmetics * p2p/conn: add failing tests * p2p/conn: make SecretConnection thread safe * changelog * IPCVal signer refactor - use a .reset() method - don't use embedded RemoteSignerClient - guard RemoteSignerClient with mutex - drop the .conn - expose Close() on RemoteSignerClient * apply IPCVal refactor to TCPVal * remove mtx from RemoteSignerClient * consolidate IPCVal and TCPVal, fixes #3104 - done in tcp_client.go - now called SocketVal - takes a listener in the constructor - make tcpListener and unixListener contain all the differences * delete ipc files * introduce unix and tcp dialer for RemoteSigner * rename files - drop tcp_ prefix - rename priv_validator.go to file.go * bring back listener options * fix node * fix priv_val_server * fix node test * minor cleanup and comments
6 years ago
  1. package node
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "os"
  7. "syscall"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "github.com/tendermint/tendermint/abci/example/kvstore"
  13. cfg "github.com/tendermint/tendermint/config"
  14. "github.com/tendermint/tendermint/crypto/ed25519"
  15. "github.com/tendermint/tendermint/evidence"
  16. cmn "github.com/tendermint/tendermint/libs/common"
  17. dbm "github.com/tendermint/tendermint/libs/db"
  18. "github.com/tendermint/tendermint/libs/log"
  19. mempl "github.com/tendermint/tendermint/mempool"
  20. "github.com/tendermint/tendermint/p2p"
  21. "github.com/tendermint/tendermint/privval"
  22. "github.com/tendermint/tendermint/proxy"
  23. sm "github.com/tendermint/tendermint/state"
  24. "github.com/tendermint/tendermint/types"
  25. tmtime "github.com/tendermint/tendermint/types/time"
  26. "github.com/tendermint/tendermint/version"
  27. )
  28. func TestNodeStartStop(t *testing.T) {
  29. config := cfg.ResetTestRoot("node_node_test")
  30. // create & start node
  31. n, err := DefaultNewNode(config, log.TestingLogger())
  32. require.NoError(t, err)
  33. err = n.Start()
  34. require.NoError(t, err)
  35. t.Logf("Started node %v", n.sw.NodeInfo())
  36. // wait for the node to produce a block
  37. blockCh := make(chan interface{})
  38. err = n.EventBus().Subscribe(context.Background(), "node_test", types.EventQueryNewBlock, blockCh)
  39. require.NoError(t, err)
  40. select {
  41. case <-blockCh:
  42. case <-time.After(10 * time.Second):
  43. t.Fatal("timed out waiting for the node to produce a block")
  44. }
  45. // stop the node
  46. go func() {
  47. n.Stop()
  48. }()
  49. select {
  50. case <-n.Quit():
  51. case <-time.After(5 * time.Second):
  52. pid := os.Getpid()
  53. p, err := os.FindProcess(pid)
  54. if err != nil {
  55. panic(err)
  56. }
  57. err = p.Signal(syscall.SIGABRT)
  58. fmt.Println(err)
  59. t.Fatal("timed out waiting for shutdown")
  60. }
  61. }
  62. func TestSplitAndTrimEmpty(t *testing.T) {
  63. testCases := []struct {
  64. s string
  65. sep string
  66. cutset string
  67. expected []string
  68. }{
  69. {"a,b,c", ",", " ", []string{"a", "b", "c"}},
  70. {" a , b , c ", ",", " ", []string{"a", "b", "c"}},
  71. {" a, b, c ", ",", " ", []string{"a", "b", "c"}},
  72. {" a, ", ",", " ", []string{"a"}},
  73. {" ", ",", " ", []string{}},
  74. }
  75. for _, tc := range testCases {
  76. assert.Equal(t, tc.expected, splitAndTrimEmpty(tc.s, tc.sep, tc.cutset), "%s", tc.s)
  77. }
  78. }
  79. func TestNodeDelayedStop(t *testing.T) {
  80. config := cfg.ResetTestRoot("node_delayed_node_test")
  81. now := tmtime.Now()
  82. // create & start node
  83. n, err := DefaultNewNode(config, log.TestingLogger())
  84. n.GenesisDoc().GenesisTime = now.Add(5 * time.Second)
  85. require.NoError(t, err)
  86. n.Start()
  87. startTime := tmtime.Now()
  88. assert.Equal(t, true, startTime.After(n.GenesisDoc().GenesisTime))
  89. }
  90. func TestNodeSetAppVersion(t *testing.T) {
  91. config := cfg.ResetTestRoot("node_app_version_test")
  92. // create & start node
  93. n, err := DefaultNewNode(config, log.TestingLogger())
  94. require.NoError(t, err)
  95. // default config uses the kvstore app
  96. var appVersion version.Protocol = kvstore.ProtocolVersion
  97. // check version is set in state
  98. state := sm.LoadState(n.stateDB)
  99. assert.Equal(t, state.Version.Consensus.App, appVersion)
  100. // check version is set in node info
  101. assert.Equal(t, n.nodeInfo.(p2p.DefaultNodeInfo).ProtocolVersion.App, appVersion)
  102. }
  103. func TestNodeSetPrivValTCP(t *testing.T) {
  104. addr := "tcp://" + testFreeAddr(t)
  105. config := cfg.ResetTestRoot("node_priv_val_tcp_test")
  106. config.BaseConfig.PrivValidatorListenAddr = addr
  107. dialer := privval.DialTCPFn(addr, 100*time.Millisecond, ed25519.GenPrivKey())
  108. pvsc := privval.NewRemoteSigner(
  109. log.TestingLogger(),
  110. config.ChainID(),
  111. types.NewMockPV(),
  112. dialer,
  113. )
  114. go func() {
  115. err := pvsc.Start()
  116. if err != nil {
  117. panic(err)
  118. }
  119. }()
  120. defer pvsc.Stop()
  121. n, err := DefaultNewNode(config, log.TestingLogger())
  122. require.NoError(t, err)
  123. assert.IsType(t, &privval.SocketVal{}, n.PrivValidator())
  124. }
  125. // address without a protocol must result in error
  126. func TestPrivValidatorListenAddrNoProtocol(t *testing.T) {
  127. addrNoPrefix := testFreeAddr(t)
  128. config := cfg.ResetTestRoot("node_priv_val_tcp_test")
  129. config.BaseConfig.PrivValidatorListenAddr = addrNoPrefix
  130. _, err := DefaultNewNode(config, log.TestingLogger())
  131. assert.Error(t, err)
  132. }
  133. func TestNodeSetPrivValIPC(t *testing.T) {
  134. tmpfile := "/tmp/kms." + cmn.RandStr(6) + ".sock"
  135. defer os.Remove(tmpfile) // clean up
  136. config := cfg.ResetTestRoot("node_priv_val_tcp_test")
  137. config.BaseConfig.PrivValidatorListenAddr = "unix://" + tmpfile
  138. dialer := privval.DialUnixFn(tmpfile)
  139. pvsc := privval.NewRemoteSigner(
  140. log.TestingLogger(),
  141. config.ChainID(),
  142. types.NewMockPV(),
  143. dialer,
  144. )
  145. done := make(chan struct{})
  146. go func() {
  147. defer close(done)
  148. n, err := DefaultNewNode(config, log.TestingLogger())
  149. require.NoError(t, err)
  150. assert.IsType(t, &privval.SocketVal{}, n.PrivValidator())
  151. }()
  152. err := pvsc.Start()
  153. require.NoError(t, err)
  154. defer pvsc.Stop()
  155. <-done
  156. }
  157. // testFreeAddr claims a free port so we don't block on listener being ready.
  158. func testFreeAddr(t *testing.T) string {
  159. ln, err := net.Listen("tcp", "127.0.0.1:0")
  160. require.NoError(t, err)
  161. defer ln.Close()
  162. return fmt.Sprintf("127.0.0.1:%d", ln.Addr().(*net.TCPAddr).Port)
  163. }
  164. // create a proposal block using real and full
  165. // mempool and evidence pool and validate it.
  166. func TestCreateProposalBlock(t *testing.T) {
  167. config := cfg.ResetTestRoot("node_create_proposal")
  168. cc := proxy.NewLocalClientCreator(kvstore.NewKVStoreApplication())
  169. proxyApp := proxy.NewAppConns(cc)
  170. err := proxyApp.Start()
  171. require.Nil(t, err)
  172. defer proxyApp.Stop()
  173. logger := log.TestingLogger()
  174. var height int64 = 1
  175. state, stateDB := state(1, height)
  176. maxBytes := 16384
  177. state.ConsensusParams.BlockSize.MaxBytes = int64(maxBytes)
  178. proposerAddr, _ := state.Validators.GetByIndex(0)
  179. // Make Mempool
  180. memplMetrics := mempl.PrometheusMetrics("node_test")
  181. mempool := mempl.NewMempool(
  182. config.Mempool,
  183. proxyApp.Mempool(),
  184. state.LastBlockHeight,
  185. mempl.WithMetrics(memplMetrics),
  186. mempl.WithPreCheck(sm.TxPreCheck(state)),
  187. mempl.WithPostCheck(sm.TxPostCheck(state)),
  188. )
  189. mempool.SetLogger(logger)
  190. // Make EvidencePool
  191. types.RegisterMockEvidencesGlobal()
  192. evidence.RegisterMockEvidences()
  193. evidenceDB := dbm.NewMemDB()
  194. evidenceStore := evidence.NewEvidenceStore(evidenceDB)
  195. evidencePool := evidence.NewEvidencePool(stateDB, evidenceStore)
  196. evidencePool.SetLogger(logger)
  197. // fill the evidence pool with more evidence
  198. // than can fit in a block
  199. minEvSize := 12
  200. numEv := (maxBytes / types.MaxEvidenceBytesDenominator) / minEvSize
  201. for i := 0; i < numEv; i++ {
  202. ev := types.NewMockRandomGoodEvidence(1, proposerAddr, cmn.RandBytes(minEvSize))
  203. err := evidencePool.AddEvidence(ev)
  204. assert.NoError(t, err)
  205. }
  206. // fill the mempool with more txs
  207. // than can fit in a block
  208. txLength := 1000
  209. for i := 0; i < maxBytes/txLength; i++ {
  210. tx := cmn.RandBytes(txLength)
  211. err := mempool.CheckTx(tx, nil)
  212. assert.NoError(t, err)
  213. }
  214. blockExec := sm.NewBlockExecutor(
  215. stateDB,
  216. logger,
  217. proxyApp.Consensus(),
  218. mempool,
  219. evidencePool,
  220. )
  221. commit := &types.Commit{}
  222. block, _ := blockExec.CreateProposalBlock(
  223. height,
  224. state, commit,
  225. proposerAddr,
  226. )
  227. err = blockExec.ValidateBlock(state, block)
  228. assert.NoError(t, err)
  229. }
  230. func state(nVals int, height int64) (sm.State, dbm.DB) {
  231. vals := make([]types.GenesisValidator, nVals)
  232. for i := 0; i < nVals; i++ {
  233. secret := []byte(fmt.Sprintf("test%d", i))
  234. pk := ed25519.GenPrivKeyFromSecret(secret)
  235. vals[i] = types.GenesisValidator{
  236. pk.PubKey().Address(),
  237. pk.PubKey(),
  238. 1000,
  239. fmt.Sprintf("test%d", i),
  240. }
  241. }
  242. s, _ := sm.MakeGenesisState(&types.GenesisDoc{
  243. ChainID: "test-chain",
  244. Validators: vals,
  245. AppHash: nil,
  246. })
  247. // save validators to db for 2 heights
  248. stateDB := dbm.NewMemDB()
  249. sm.SaveState(stateDB, s)
  250. for i := 1; i < int(height); i++ {
  251. s.LastBlockHeight++
  252. s.LastValidators = s.Validators.Copy()
  253. sm.SaveState(stateDB, s)
  254. }
  255. return s, stateDB
  256. }