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.

284 lines
6.6 KiB

fix non deterministic test failures and race in privval socket (#3258) * node: decrease retry conn timeout in test Should fix #3256 The retry timeout was set to the default, which is the same as the accept timeout, so it's no wonder this would fail. Here we decrease the retry timeout so we can try many times before the accept timeout. * p2p: increase handshake timeout in test This fails sometimes, presumably because the handshake timeout is so low (only 50ms). So increase it to 1s. Should fix #3187 * privval: fix race with ping. closes #3237 Pings happen in a go-routine and can happen concurrently with other messages. Since we use a request/response protocol, we expect to send a request and get back the corresponding response. But with pings happening concurrently, this assumption could be violated. We were using a mutex, but only a RWMutex, where the RLock was being held for sending messages - this was to allow the underlying connection to be replaced if it fails. Turns out we actually need to use a full lock (not just a read lock) to prevent multiple requests from happening concurrently. * node: fix test name. DelayedStop -> DelayedStart * autofile: Wait() method In the TestWALTruncate in consensus/wal_test.go we remove the WAL directory at the end of the test. However the wal.Stop() does not properly wait for the autofile group to finish shutting down. Hence it was possible that the group's go-routine is still running when the cleanup happens, which causes a panic since the directory disappeared. Here we add a Wait() method to properly wait until the go-routine exits so we can safely clean up. This fixes #2852.
6 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
  1. package p2p
  2. import (
  3. "fmt"
  4. "net"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/tendermint/tendermint/crypto"
  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/libs/rand"
  12. "github.com/tendermint/tendermint/config"
  13. "github.com/tendermint/tendermint/p2p/conn"
  14. )
  15. const testCh = 0x01
  16. //------------------------------------------------
  17. type mockNodeInfo struct {
  18. addr *NetAddress
  19. }
  20. func (ni mockNodeInfo) ID() ID { return ni.addr.ID }
  21. func (ni mockNodeInfo) NetAddress() (*NetAddress, error) { return ni.addr, nil }
  22. func (ni mockNodeInfo) Validate() error { return nil }
  23. func (ni mockNodeInfo) CompatibleWith(other NodeInfo) error { return nil }
  24. func AddPeerToSwitchPeerSet(sw *Switch, peer Peer) {
  25. sw.peers.Add(peer)
  26. }
  27. func CreateRandomPeer(outbound bool) *peer {
  28. addr, netAddr := CreateRoutableAddr()
  29. p := &peer{
  30. peerConn: peerConn{
  31. outbound: outbound,
  32. socketAddr: netAddr,
  33. },
  34. nodeInfo: mockNodeInfo{netAddr},
  35. mconn: &conn.MConnection{},
  36. metrics: NopMetrics(),
  37. }
  38. p.SetLogger(log.TestingLogger().With("peer", addr))
  39. return p
  40. }
  41. func CreateRoutableAddr() (addr string, netAddr *NetAddress) {
  42. for {
  43. var err error
  44. addr = fmt.Sprintf("%X@%v.%v.%v.%v:26656",
  45. rand.RandBytes(20),
  46. rand.RandInt()%256,
  47. rand.RandInt()%256,
  48. rand.RandInt()%256,
  49. rand.RandInt()%256)
  50. netAddr, err = NewNetAddressString(addr)
  51. if err != nil {
  52. panic(err)
  53. }
  54. if netAddr.Routable() {
  55. break
  56. }
  57. }
  58. return
  59. }
  60. //------------------------------------------------------------------
  61. // Connects switches via arbitrary net.Conn. Used for testing.
  62. const TestHost = "localhost"
  63. // MakeConnectedSwitches returns n switches, connected according to the connect func.
  64. // If connect==Connect2Switches, the switches will be fully connected.
  65. // initSwitch defines how the i'th switch should be initialized (ie. with what reactors).
  66. // NOTE: panics if any switch fails to start.
  67. func MakeConnectedSwitches(cfg *config.P2PConfig,
  68. n int,
  69. initSwitch func(int, *Switch) *Switch,
  70. connect func([]*Switch, int, int),
  71. ) []*Switch {
  72. switches := make([]*Switch, n)
  73. for i := 0; i < n; i++ {
  74. switches[i] = MakeSwitch(cfg, i, TestHost, "123.123.123", initSwitch)
  75. }
  76. if err := StartSwitches(switches); err != nil {
  77. panic(err)
  78. }
  79. for i := 0; i < n; i++ {
  80. for j := i + 1; j < n; j++ {
  81. connect(switches, i, j)
  82. }
  83. }
  84. return switches
  85. }
  86. // Connect2Switches will connect switches i and j via net.Pipe().
  87. // Blocks until a connection is established.
  88. // NOTE: caller ensures i and j are within bounds.
  89. func Connect2Switches(switches []*Switch, i, j int) {
  90. switchI := switches[i]
  91. switchJ := switches[j]
  92. c1, c2 := conn.NetPipe()
  93. doneCh := make(chan struct{})
  94. go func() {
  95. err := switchI.addPeerWithConnection(c1)
  96. if err != nil {
  97. panic(err)
  98. }
  99. doneCh <- struct{}{}
  100. }()
  101. go func() {
  102. err := switchJ.addPeerWithConnection(c2)
  103. if err != nil {
  104. panic(err)
  105. }
  106. doneCh <- struct{}{}
  107. }()
  108. <-doneCh
  109. <-doneCh
  110. }
  111. func (sw *Switch) addPeerWithConnection(conn net.Conn) error {
  112. pc, err := testInboundPeerConn(conn, sw.config, sw.nodeKey.PrivKey)
  113. if err != nil {
  114. if err := conn.Close(); err != nil {
  115. sw.Logger.Error("Error closing connection", "err", err)
  116. }
  117. return err
  118. }
  119. ni, err := handshake(conn, time.Second, sw.nodeInfo)
  120. if err != nil {
  121. if err := conn.Close(); err != nil {
  122. sw.Logger.Error("Error closing connection", "err", err)
  123. }
  124. return err
  125. }
  126. p := newPeer(
  127. pc,
  128. MConnConfig(sw.config),
  129. ni,
  130. sw.reactorsByCh,
  131. sw.chDescs,
  132. sw.StopPeerForError,
  133. )
  134. if err = sw.addPeer(p); err != nil {
  135. pc.CloseConn()
  136. return err
  137. }
  138. return nil
  139. }
  140. // StartSwitches calls sw.Start() for each given switch.
  141. // It returns the first encountered error.
  142. func StartSwitches(switches []*Switch) error {
  143. for _, s := range switches {
  144. err := s.Start() // start switch and reactors
  145. if err != nil {
  146. return err
  147. }
  148. }
  149. return nil
  150. }
  151. func MakeSwitch(
  152. cfg *config.P2PConfig,
  153. i int,
  154. network, version string,
  155. initSwitch func(int, *Switch) *Switch,
  156. opts ...SwitchOption,
  157. ) *Switch {
  158. nodeKey := NodeKey{
  159. PrivKey: ed25519.GenPrivKey(),
  160. }
  161. nodeInfo := testNodeInfo(nodeKey.ID(), fmt.Sprintf("node%d", i))
  162. addr, err := NewNetAddressString(
  163. IDAddressString(nodeKey.ID(), nodeInfo.(DefaultNodeInfo).ListenAddr),
  164. )
  165. if err != nil {
  166. panic(err)
  167. }
  168. t := NewMultiplexTransport(nodeInfo, nodeKey, MConnConfig(cfg))
  169. if err := t.Listen(*addr); err != nil {
  170. panic(err)
  171. }
  172. // TODO: let the config be passed in?
  173. sw := initSwitch(i, NewSwitch(cfg, t, opts...))
  174. sw.SetLogger(log.TestingLogger().With("switch", i))
  175. sw.SetNodeKey(&nodeKey)
  176. ni := nodeInfo.(DefaultNodeInfo)
  177. for ch := range sw.reactorsByCh {
  178. ni.Channels = append(ni.Channels, ch)
  179. }
  180. nodeInfo = ni
  181. // TODO: We need to setup reactors ahead of time so the NodeInfo is properly
  182. // populated and we don't have to do those awkward overrides and setters.
  183. t.nodeInfo = nodeInfo
  184. sw.SetNodeInfo(nodeInfo)
  185. return sw
  186. }
  187. func testInboundPeerConn(
  188. conn net.Conn,
  189. config *config.P2PConfig,
  190. ourNodePrivKey crypto.PrivKey,
  191. ) (peerConn, error) {
  192. return testPeerConn(conn, config, false, false, ourNodePrivKey, nil)
  193. }
  194. func testPeerConn(
  195. rawConn net.Conn,
  196. cfg *config.P2PConfig,
  197. outbound, persistent bool,
  198. ourNodePrivKey crypto.PrivKey,
  199. socketAddr *NetAddress,
  200. ) (pc peerConn, err error) {
  201. conn := rawConn
  202. // Fuzz connection
  203. if cfg.TestFuzz {
  204. // so we have time to do peer handshakes and get set up
  205. conn = FuzzConnAfterFromConfig(conn, 10*time.Second, cfg.TestFuzzConfig)
  206. }
  207. // Encrypt connection
  208. conn, err = upgradeSecretConn(conn, cfg.HandshakeTimeout, ourNodePrivKey)
  209. if err != nil {
  210. return pc, errors.Wrap(err, "Error creating peer")
  211. }
  212. // Only the information we already have
  213. return newPeerConn(outbound, persistent, conn, socketAddr), nil
  214. }
  215. //----------------------------------------------------------------
  216. // rand node info
  217. func testNodeInfo(id ID, name string) NodeInfo {
  218. return testNodeInfoWithNetwork(id, name, "testing")
  219. }
  220. func testNodeInfoWithNetwork(id ID, name, network string) NodeInfo {
  221. return DefaultNodeInfo{
  222. ProtocolVersion: defaultProtocolVersion,
  223. DefaultNodeID: id,
  224. ListenAddr: fmt.Sprintf("127.0.0.1:%d", getFreePort()),
  225. Network: network,
  226. Version: "1.2.3-rc0-deadbeef",
  227. Channels: []byte{testCh},
  228. Moniker: name,
  229. Other: DefaultNodeInfoOther{
  230. TxIndex: "on",
  231. RPCAddress: fmt.Sprintf("127.0.0.1:%d", getFreePort()),
  232. },
  233. }
  234. }
  235. func getFreePort() int {
  236. port, err := cmn.GetFreePort()
  237. if err != nil {
  238. panic(err)
  239. }
  240. return port
  241. }