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.

838 lines
20 KiB

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
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
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
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
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. "bytes"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "net"
  8. "net/http"
  9. "net/http/httptest"
  10. "regexp"
  11. "strconv"
  12. "sync/atomic"
  13. "testing"
  14. "time"
  15. "github.com/prometheus/client_golang/prometheus/promhttp"
  16. "github.com/stretchr/testify/assert"
  17. "github.com/stretchr/testify/require"
  18. "github.com/tendermint/tendermint/config"
  19. "github.com/tendermint/tendermint/crypto/ed25519"
  20. "github.com/tendermint/tendermint/libs/log"
  21. tmsync "github.com/tendermint/tendermint/libs/sync"
  22. "github.com/tendermint/tendermint/p2p/conn"
  23. )
  24. var (
  25. cfg *config.P2PConfig
  26. )
  27. func init() {
  28. cfg = config.DefaultP2PConfig()
  29. cfg.PexReactor = true
  30. cfg.AllowDuplicateIP = true
  31. }
  32. type PeerMessage struct {
  33. PeerID ID
  34. Bytes []byte
  35. Counter int
  36. }
  37. type TestReactor struct {
  38. BaseReactor
  39. mtx tmsync.Mutex
  40. channels []*conn.ChannelDescriptor
  41. logMessages bool
  42. msgsCounter int
  43. msgsReceived map[byte][]PeerMessage
  44. }
  45. func NewTestReactor(channels []*conn.ChannelDescriptor, logMessages bool) *TestReactor {
  46. tr := &TestReactor{
  47. channels: channels,
  48. logMessages: logMessages,
  49. msgsReceived: make(map[byte][]PeerMessage),
  50. }
  51. tr.BaseReactor = *NewBaseReactor("TestReactor", tr)
  52. tr.SetLogger(log.TestingLogger())
  53. return tr
  54. }
  55. func (tr *TestReactor) GetChannels() []*conn.ChannelDescriptor {
  56. return tr.channels
  57. }
  58. func (tr *TestReactor) AddPeer(peer Peer) {}
  59. func (tr *TestReactor) RemovePeer(peer Peer, reason interface{}) {}
  60. func (tr *TestReactor) Receive(chID byte, peer Peer, msgBytes []byte) {
  61. if tr.logMessages {
  62. tr.mtx.Lock()
  63. defer tr.mtx.Unlock()
  64. // fmt.Printf("Received: %X, %X\n", chID, msgBytes)
  65. tr.msgsReceived[chID] = append(tr.msgsReceived[chID], PeerMessage{peer.ID(), msgBytes, tr.msgsCounter})
  66. tr.msgsCounter++
  67. }
  68. }
  69. func (tr *TestReactor) getMsgs(chID byte) []PeerMessage {
  70. tr.mtx.Lock()
  71. defer tr.mtx.Unlock()
  72. return tr.msgsReceived[chID]
  73. }
  74. //-----------------------------------------------------------------------------
  75. // convenience method for creating two switches connected to each other.
  76. // XXX: note this uses net.Pipe and not a proper TCP conn
  77. func MakeSwitchPair(t testing.TB, initSwitch func(int, *Switch) *Switch) (*Switch, *Switch) {
  78. // Create two switches that will be interconnected.
  79. switches := MakeConnectedSwitches(cfg, 2, initSwitch, Connect2Switches)
  80. return switches[0], switches[1]
  81. }
  82. func initSwitchFunc(i int, sw *Switch) *Switch {
  83. sw.SetAddrBook(&AddrBookMock{
  84. Addrs: make(map[string]struct{}),
  85. OurAddrs: make(map[string]struct{})})
  86. // Make two reactors of two channels each
  87. sw.AddReactor("foo", NewTestReactor([]*conn.ChannelDescriptor{
  88. {ID: byte(0x00), Priority: 10},
  89. {ID: byte(0x01), Priority: 10},
  90. }, true))
  91. sw.AddReactor("bar", NewTestReactor([]*conn.ChannelDescriptor{
  92. {ID: byte(0x02), Priority: 10},
  93. {ID: byte(0x03), Priority: 10},
  94. }, true))
  95. return sw
  96. }
  97. func TestSwitches(t *testing.T) {
  98. s1, s2 := MakeSwitchPair(t, initSwitchFunc)
  99. t.Cleanup(func() {
  100. if err := s1.Stop(); err != nil {
  101. t.Error(err)
  102. }
  103. })
  104. t.Cleanup(func() {
  105. if err := s2.Stop(); err != nil {
  106. t.Error(err)
  107. }
  108. })
  109. if s1.Peers().Size() != 1 {
  110. t.Errorf("expected exactly 1 peer in s1, got %v", s1.Peers().Size())
  111. }
  112. if s2.Peers().Size() != 1 {
  113. t.Errorf("expected exactly 1 peer in s2, got %v", s2.Peers().Size())
  114. }
  115. // Lets send some messages
  116. ch0Msg := []byte("channel zero")
  117. ch1Msg := []byte("channel foo")
  118. ch2Msg := []byte("channel bar")
  119. s1.Broadcast(byte(0x00), ch0Msg)
  120. s1.Broadcast(byte(0x01), ch1Msg)
  121. s1.Broadcast(byte(0x02), ch2Msg)
  122. assertMsgReceivedWithTimeout(t,
  123. ch0Msg,
  124. byte(0x00),
  125. s2.Reactor("foo").(*TestReactor), 10*time.Millisecond, 5*time.Second)
  126. assertMsgReceivedWithTimeout(t,
  127. ch1Msg,
  128. byte(0x01),
  129. s2.Reactor("foo").(*TestReactor), 10*time.Millisecond, 5*time.Second)
  130. assertMsgReceivedWithTimeout(t,
  131. ch2Msg,
  132. byte(0x02),
  133. s2.Reactor("bar").(*TestReactor), 10*time.Millisecond, 5*time.Second)
  134. }
  135. func assertMsgReceivedWithTimeout(
  136. t *testing.T,
  137. msgBytes []byte,
  138. channel byte,
  139. reactor *TestReactor,
  140. checkPeriod,
  141. timeout time.Duration,
  142. ) {
  143. ticker := time.NewTicker(checkPeriod)
  144. for {
  145. select {
  146. case <-ticker.C:
  147. msgs := reactor.getMsgs(channel)
  148. if len(msgs) > 0 {
  149. if !bytes.Equal(msgs[0].Bytes, msgBytes) {
  150. t.Fatalf("Unexpected message bytes. Wanted: %X, Got: %X", msgBytes, msgs[0].Bytes)
  151. }
  152. return
  153. }
  154. case <-time.After(timeout):
  155. t.Fatalf("Expected to have received 1 message in channel #%v, got zero", channel)
  156. }
  157. }
  158. }
  159. func TestSwitchFiltersOutItself(t *testing.T) {
  160. s1 := MakeSwitch(cfg, 1, "127.0.0.1", "123.123.123", initSwitchFunc)
  161. // simulate s1 having a public IP by creating a remote peer with the same ID
  162. rp := &remotePeer{PrivKey: s1.nodeKey.PrivKey, Config: cfg}
  163. rp.Start()
  164. // addr should be rejected in addPeer based on the same ID
  165. err := s1.DialPeerWithAddress(rp.Addr())
  166. if assert.Error(t, err) {
  167. if err, ok := err.(ErrRejected); ok {
  168. if !err.IsSelf() {
  169. t.Errorf("expected self to be rejected")
  170. }
  171. } else {
  172. t.Errorf("expected ErrRejected")
  173. }
  174. }
  175. assert.True(t, s1.addrBook.OurAddress(rp.Addr()))
  176. assert.False(t, s1.addrBook.HasAddress(rp.Addr()))
  177. rp.Stop()
  178. assertNoPeersAfterTimeout(t, s1, 100*time.Millisecond)
  179. }
  180. func TestSwitchPeerFilter(t *testing.T) {
  181. var (
  182. filters = []PeerFilterFunc{
  183. func(_ IPeerSet, _ Peer) error { return nil },
  184. func(_ IPeerSet, _ Peer) error { return fmt.Errorf("denied") },
  185. func(_ IPeerSet, _ Peer) error { return nil },
  186. }
  187. sw = MakeSwitch(
  188. cfg,
  189. 1,
  190. "testing",
  191. "123.123.123",
  192. initSwitchFunc,
  193. SwitchPeerFilters(filters...),
  194. )
  195. )
  196. err := sw.Start()
  197. require.NoError(t, err)
  198. t.Cleanup(func() {
  199. if err := sw.Stop(); err != nil {
  200. t.Error(err)
  201. }
  202. })
  203. // simulate remote peer
  204. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  205. rp.Start()
  206. t.Cleanup(rp.Stop)
  207. p, err := sw.transport.Dial(*rp.Addr(), peerConfig{
  208. chDescs: sw.chDescs,
  209. onPeerError: sw.StopPeerForError,
  210. isPersistent: sw.IsPeerPersistent,
  211. reactorsByCh: sw.reactorsByCh,
  212. })
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. err = sw.addPeer(p)
  217. if err, ok := err.(ErrRejected); ok {
  218. if !err.IsFiltered() {
  219. t.Errorf("expected peer to be filtered")
  220. }
  221. } else {
  222. t.Errorf("expected ErrRejected")
  223. }
  224. }
  225. func TestSwitchPeerFilterTimeout(t *testing.T) {
  226. var (
  227. filters = []PeerFilterFunc{
  228. func(_ IPeerSet, _ Peer) error {
  229. time.Sleep(10 * time.Millisecond)
  230. return nil
  231. },
  232. }
  233. sw = MakeSwitch(
  234. cfg,
  235. 1,
  236. "testing",
  237. "123.123.123",
  238. initSwitchFunc,
  239. SwitchFilterTimeout(5*time.Millisecond),
  240. SwitchPeerFilters(filters...),
  241. )
  242. )
  243. err := sw.Start()
  244. require.NoError(t, err)
  245. t.Cleanup(func() {
  246. if err := sw.Stop(); err != nil {
  247. t.Log(err)
  248. }
  249. })
  250. // simulate remote peer
  251. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  252. rp.Start()
  253. defer rp.Stop()
  254. p, err := sw.transport.Dial(*rp.Addr(), peerConfig{
  255. chDescs: sw.chDescs,
  256. onPeerError: sw.StopPeerForError,
  257. isPersistent: sw.IsPeerPersistent,
  258. reactorsByCh: sw.reactorsByCh,
  259. })
  260. if err != nil {
  261. t.Fatal(err)
  262. }
  263. err = sw.addPeer(p)
  264. if _, ok := err.(ErrFilterTimeout); !ok {
  265. t.Errorf("expected ErrFilterTimeout")
  266. }
  267. }
  268. func TestSwitchPeerFilterDuplicate(t *testing.T) {
  269. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  270. err := sw.Start()
  271. require.NoError(t, err)
  272. t.Cleanup(func() {
  273. if err := sw.Stop(); err != nil {
  274. t.Error(err)
  275. }
  276. })
  277. // simulate remote peer
  278. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  279. rp.Start()
  280. defer rp.Stop()
  281. p, err := sw.transport.Dial(*rp.Addr(), peerConfig{
  282. chDescs: sw.chDescs,
  283. onPeerError: sw.StopPeerForError,
  284. isPersistent: sw.IsPeerPersistent,
  285. reactorsByCh: sw.reactorsByCh,
  286. })
  287. if err != nil {
  288. t.Fatal(err)
  289. }
  290. if err := sw.addPeer(p); err != nil {
  291. t.Fatal(err)
  292. }
  293. err = sw.addPeer(p)
  294. if errRej, ok := err.(ErrRejected); ok {
  295. if !errRej.IsDuplicate() {
  296. t.Errorf("expected peer to be duplicate. got %v", errRej)
  297. }
  298. } else {
  299. t.Errorf("expected ErrRejected, got %v", err)
  300. }
  301. }
  302. func assertNoPeersAfterTimeout(t *testing.T, sw *Switch, timeout time.Duration) {
  303. time.Sleep(timeout)
  304. if sw.Peers().Size() != 0 {
  305. t.Fatalf("Expected %v to not connect to some peers, got %d", sw, sw.Peers().Size())
  306. }
  307. }
  308. func TestSwitchStopsNonPersistentPeerOnError(t *testing.T) {
  309. assert, require := assert.New(t), require.New(t)
  310. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  311. err := sw.Start()
  312. if err != nil {
  313. t.Error(err)
  314. }
  315. t.Cleanup(func() {
  316. if err := sw.Stop(); err != nil {
  317. t.Error(err)
  318. }
  319. })
  320. // simulate remote peer
  321. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  322. rp.Start()
  323. defer rp.Stop()
  324. p, err := sw.transport.Dial(*rp.Addr(), peerConfig{
  325. chDescs: sw.chDescs,
  326. onPeerError: sw.StopPeerForError,
  327. isPersistent: sw.IsPeerPersistent,
  328. reactorsByCh: sw.reactorsByCh,
  329. })
  330. require.Nil(err)
  331. err = sw.addPeer(p)
  332. require.Nil(err)
  333. require.NotNil(sw.Peers().Get(rp.ID()))
  334. // simulate failure by closing connection
  335. err = p.(*peer).CloseConn()
  336. require.NoError(err)
  337. assertNoPeersAfterTimeout(t, sw, 100*time.Millisecond)
  338. assert.False(p.IsRunning())
  339. }
  340. func TestSwitchStopPeerForError(t *testing.T) {
  341. s := httptest.NewServer(promhttp.Handler())
  342. defer s.Close()
  343. scrapeMetrics := func() string {
  344. resp, err := http.Get(s.URL)
  345. require.NoError(t, err)
  346. defer resp.Body.Close()
  347. buf, _ := ioutil.ReadAll(resp.Body)
  348. return string(buf)
  349. }
  350. namespace, subsystem, name := config.TestInstrumentationConfig().Namespace, MetricsSubsystem, "peers"
  351. re := regexp.MustCompile(namespace + `_` + subsystem + `_` + name + ` ([0-9\.]+)`)
  352. peersMetricValue := func() float64 {
  353. matches := re.FindStringSubmatch(scrapeMetrics())
  354. f, _ := strconv.ParseFloat(matches[1], 64)
  355. return f
  356. }
  357. p2pMetrics := PrometheusMetrics(namespace)
  358. // make two connected switches
  359. sw1, sw2 := MakeSwitchPair(t, func(i int, sw *Switch) *Switch {
  360. // set metrics on sw1
  361. if i == 0 {
  362. opt := WithMetrics(p2pMetrics)
  363. opt(sw)
  364. }
  365. return initSwitchFunc(i, sw)
  366. })
  367. assert.Equal(t, len(sw1.Peers().List()), 1)
  368. assert.EqualValues(t, 1, peersMetricValue())
  369. // send messages to the peer from sw1
  370. p := sw1.Peers().List()[0]
  371. p.Send(0x1, []byte("here's a message to send"))
  372. // stop sw2. this should cause the p to fail,
  373. // which results in calling StopPeerForError internally
  374. t.Cleanup(func() {
  375. if err := sw2.Stop(); err != nil {
  376. t.Error(err)
  377. }
  378. })
  379. // now call StopPeerForError explicitly, eg. from a reactor
  380. sw1.StopPeerForError(p, fmt.Errorf("some err"))
  381. assert.Equal(t, len(sw1.Peers().List()), 0)
  382. assert.EqualValues(t, 0, peersMetricValue())
  383. }
  384. func TestSwitchReconnectsToOutboundPersistentPeer(t *testing.T) {
  385. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  386. err := sw.Start()
  387. require.NoError(t, err)
  388. t.Cleanup(func() {
  389. if err := sw.Stop(); err != nil {
  390. t.Error(err)
  391. }
  392. })
  393. // 1. simulate failure by closing connection
  394. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  395. rp.Start()
  396. defer rp.Stop()
  397. err = sw.AddPersistentPeers([]string{rp.Addr().String()})
  398. require.NoError(t, err)
  399. err = sw.DialPeerWithAddress(rp.Addr())
  400. require.Nil(t, err)
  401. require.NotNil(t, sw.Peers().Get(rp.ID()))
  402. p := sw.Peers().List()[0]
  403. err = p.(*peer).CloseConn()
  404. require.NoError(t, err)
  405. waitUntilSwitchHasAtLeastNPeers(sw, 1)
  406. assert.False(t, p.IsRunning()) // old peer instance
  407. assert.Equal(t, 1, sw.Peers().Size()) // new peer instance
  408. // 2. simulate first time dial failure
  409. rp = &remotePeer{
  410. PrivKey: ed25519.GenPrivKey(),
  411. Config: cfg,
  412. // Use different interface to prevent duplicate IP filter, this will break
  413. // beyond two peers.
  414. listenAddr: "127.0.0.1:0",
  415. }
  416. rp.Start()
  417. defer rp.Stop()
  418. conf := config.DefaultP2PConfig()
  419. conf.TestDialFail = true // will trigger a reconnect
  420. err = sw.addOutboundPeerWithConfig(rp.Addr(), conf)
  421. require.NotNil(t, err)
  422. // DialPeerWithAddres - sw.peerConfig resets the dialer
  423. waitUntilSwitchHasAtLeastNPeers(sw, 2)
  424. assert.Equal(t, 2, sw.Peers().Size())
  425. }
  426. func TestSwitchReconnectsToInboundPersistentPeer(t *testing.T) {
  427. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  428. err := sw.Start()
  429. require.NoError(t, err)
  430. t.Cleanup(func() {
  431. if err := sw.Stop(); err != nil {
  432. t.Error(err)
  433. }
  434. })
  435. // 1. simulate failure by closing the connection
  436. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  437. rp.Start()
  438. defer rp.Stop()
  439. err = sw.AddPersistentPeers([]string{rp.Addr().String()})
  440. require.NoError(t, err)
  441. conn, err := rp.Dial(sw.NetAddress())
  442. require.NoError(t, err)
  443. time.Sleep(50 * time.Millisecond)
  444. require.NotNil(t, sw.Peers().Get(rp.ID()))
  445. conn.Close()
  446. waitUntilSwitchHasAtLeastNPeers(sw, 1)
  447. assert.Equal(t, 1, sw.Peers().Size())
  448. }
  449. func TestSwitchDialPeersAsync(t *testing.T) {
  450. if testing.Short() {
  451. return
  452. }
  453. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  454. err := sw.Start()
  455. require.NoError(t, err)
  456. t.Cleanup(func() {
  457. if err := sw.Stop(); err != nil {
  458. t.Error(err)
  459. }
  460. })
  461. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  462. rp.Start()
  463. defer rp.Stop()
  464. err = sw.DialPeersAsync([]string{rp.Addr().String()})
  465. require.NoError(t, err)
  466. time.Sleep(dialRandomizerIntervalMilliseconds * time.Millisecond)
  467. require.NotNil(t, sw.Peers().Get(rp.ID()))
  468. }
  469. func waitUntilSwitchHasAtLeastNPeers(sw *Switch, n int) {
  470. for i := 0; i < 20; i++ {
  471. time.Sleep(250 * time.Millisecond)
  472. has := sw.Peers().Size()
  473. if has >= n {
  474. break
  475. }
  476. }
  477. }
  478. func TestSwitchFullConnectivity(t *testing.T) {
  479. switches := MakeConnectedSwitches(cfg, 3, initSwitchFunc, Connect2Switches)
  480. defer func() {
  481. for _, sw := range switches {
  482. sw := sw
  483. t.Cleanup(func() {
  484. if err := sw.Stop(); err != nil {
  485. t.Error(err)
  486. }
  487. })
  488. }
  489. }()
  490. for i, sw := range switches {
  491. if sw.Peers().Size() != 2 {
  492. t.Fatalf("Expected each switch to be connected to 2 other, but %d switch only connected to %d", sw.Peers().Size(), i)
  493. }
  494. }
  495. }
  496. func TestSwitchAcceptRoutine(t *testing.T) {
  497. cfg.MaxNumInboundPeers = 5
  498. // Create some unconditional peers.
  499. const unconditionalPeersNum = 2
  500. var (
  501. unconditionalPeers = make([]*remotePeer, unconditionalPeersNum)
  502. unconditionalPeerIDs = make([]string, unconditionalPeersNum)
  503. )
  504. for i := 0; i < unconditionalPeersNum; i++ {
  505. peer := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  506. peer.Start()
  507. unconditionalPeers[i] = peer
  508. unconditionalPeerIDs[i] = string(peer.ID())
  509. }
  510. // make switch
  511. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  512. err := sw.AddUnconditionalPeerIDs(unconditionalPeerIDs)
  513. require.NoError(t, err)
  514. err = sw.Start()
  515. require.NoError(t, err)
  516. t.Cleanup(func() {
  517. err := sw.Stop()
  518. require.NoError(t, err)
  519. })
  520. // 0. check there are no peers
  521. assert.Equal(t, 0, sw.Peers().Size())
  522. // 1. check we connect up to MaxNumInboundPeers
  523. peers := make([]*remotePeer, 0)
  524. for i := 0; i < cfg.MaxNumInboundPeers; i++ {
  525. peer := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  526. peers = append(peers, peer)
  527. peer.Start()
  528. c, err := peer.Dial(sw.NetAddress())
  529. require.NoError(t, err)
  530. // spawn a reading routine to prevent connection from closing
  531. go func(c net.Conn) {
  532. for {
  533. one := make([]byte, 1)
  534. _, err := c.Read(one)
  535. if err != nil {
  536. return
  537. }
  538. }
  539. }(c)
  540. }
  541. time.Sleep(100 * time.Millisecond)
  542. assert.Equal(t, cfg.MaxNumInboundPeers, sw.Peers().Size())
  543. // 2. check we close new connections if we already have MaxNumInboundPeers peers
  544. peer := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  545. peer.Start()
  546. conn, err := peer.Dial(sw.NetAddress())
  547. require.NoError(t, err)
  548. // check conn is closed
  549. one := make([]byte, 1)
  550. _ = conn.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
  551. _, err = conn.Read(one)
  552. assert.Error(t, err)
  553. assert.Equal(t, cfg.MaxNumInboundPeers, sw.Peers().Size())
  554. peer.Stop()
  555. // 3. check we connect to unconditional peers despite the limit.
  556. for _, peer := range unconditionalPeers {
  557. c, err := peer.Dial(sw.NetAddress())
  558. require.NoError(t, err)
  559. // spawn a reading routine to prevent connection from closing
  560. go func(c net.Conn) {
  561. for {
  562. one := make([]byte, 1)
  563. _, err := c.Read(one)
  564. if err != nil {
  565. return
  566. }
  567. }
  568. }(c)
  569. }
  570. time.Sleep(10 * time.Millisecond)
  571. assert.Equal(t, cfg.MaxNumInboundPeers+unconditionalPeersNum, sw.Peers().Size())
  572. for _, peer := range peers {
  573. peer.Stop()
  574. }
  575. for _, peer := range unconditionalPeers {
  576. peer.Stop()
  577. }
  578. }
  579. type errorTransport struct {
  580. acceptErr error
  581. }
  582. func (et errorTransport) NetAddress() NetAddress {
  583. panic("not implemented")
  584. }
  585. func (et errorTransport) Accept(c peerConfig) (Peer, error) {
  586. return nil, et.acceptErr
  587. }
  588. func (errorTransport) Dial(NetAddress, peerConfig) (Peer, error) {
  589. panic("not implemented")
  590. }
  591. func (errorTransport) Cleanup(Peer) {
  592. panic("not implemented")
  593. }
  594. func TestSwitchAcceptRoutineErrorCases(t *testing.T) {
  595. sw := NewSwitch(cfg, errorTransport{ErrFilterTimeout{}})
  596. assert.NotPanics(t, func() {
  597. err := sw.Start()
  598. require.NoError(t, err)
  599. err = sw.Stop()
  600. require.NoError(t, err)
  601. })
  602. sw = NewSwitch(cfg, errorTransport{ErrRejected{conn: nil, err: errors.New("filtered"), isFiltered: true}})
  603. assert.NotPanics(t, func() {
  604. err := sw.Start()
  605. require.NoError(t, err)
  606. err = sw.Stop()
  607. require.NoError(t, err)
  608. })
  609. // TODO(melekes) check we remove our address from addrBook
  610. sw = NewSwitch(cfg, errorTransport{ErrTransportClosed{}})
  611. assert.NotPanics(t, func() {
  612. err := sw.Start()
  613. require.NoError(t, err)
  614. err = sw.Stop()
  615. require.NoError(t, err)
  616. })
  617. }
  618. // mockReactor checks that InitPeer never called before RemovePeer. If that's
  619. // not true, InitCalledBeforeRemoveFinished will return true.
  620. type mockReactor struct {
  621. *BaseReactor
  622. // atomic
  623. removePeerInProgress uint32
  624. initCalledBeforeRemoveFinished uint32
  625. }
  626. func (r *mockReactor) RemovePeer(peer Peer, reason interface{}) {
  627. atomic.StoreUint32(&r.removePeerInProgress, 1)
  628. defer atomic.StoreUint32(&r.removePeerInProgress, 0)
  629. time.Sleep(100 * time.Millisecond)
  630. }
  631. func (r *mockReactor) InitPeer(peer Peer) Peer {
  632. if atomic.LoadUint32(&r.removePeerInProgress) == 1 {
  633. atomic.StoreUint32(&r.initCalledBeforeRemoveFinished, 1)
  634. }
  635. return peer
  636. }
  637. func (r *mockReactor) InitCalledBeforeRemoveFinished() bool {
  638. return atomic.LoadUint32(&r.initCalledBeforeRemoveFinished) == 1
  639. }
  640. // see stopAndRemovePeer
  641. func TestSwitchInitPeerIsNotCalledBeforeRemovePeer(t *testing.T) {
  642. // make reactor
  643. reactor := &mockReactor{}
  644. reactor.BaseReactor = NewBaseReactor("mockReactor", reactor)
  645. // make switch
  646. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", func(i int, sw *Switch) *Switch {
  647. sw.AddReactor("mock", reactor)
  648. return sw
  649. })
  650. err := sw.Start()
  651. require.NoError(t, err)
  652. t.Cleanup(func() {
  653. if err := sw.Stop(); err != nil {
  654. t.Error(err)
  655. }
  656. })
  657. // add peer
  658. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  659. rp.Start()
  660. defer rp.Stop()
  661. _, err = rp.Dial(sw.NetAddress())
  662. require.NoError(t, err)
  663. // wait till the switch adds rp to the peer set, then stop the peer asynchronously
  664. for {
  665. time.Sleep(20 * time.Millisecond)
  666. if peer := sw.Peers().Get(rp.ID()); peer != nil {
  667. go sw.StopPeerForError(peer, "test")
  668. break
  669. }
  670. }
  671. // simulate peer reconnecting to us
  672. _, err = rp.Dial(sw.NetAddress())
  673. require.NoError(t, err)
  674. // wait till the switch adds rp to the peer set
  675. time.Sleep(50 * time.Millisecond)
  676. // make sure reactor.RemovePeer is finished before InitPeer is called
  677. assert.False(t, reactor.InitCalledBeforeRemoveFinished())
  678. }
  679. func BenchmarkSwitchBroadcast(b *testing.B) {
  680. s1, s2 := MakeSwitchPair(b, func(i int, sw *Switch) *Switch {
  681. // Make bar reactors of bar channels each
  682. sw.AddReactor("foo", NewTestReactor([]*conn.ChannelDescriptor{
  683. {ID: byte(0x00), Priority: 10},
  684. {ID: byte(0x01), Priority: 10},
  685. }, false))
  686. sw.AddReactor("bar", NewTestReactor([]*conn.ChannelDescriptor{
  687. {ID: byte(0x02), Priority: 10},
  688. {ID: byte(0x03), Priority: 10},
  689. }, false))
  690. return sw
  691. })
  692. b.Cleanup(func() {
  693. if err := s1.Stop(); err != nil {
  694. b.Error(err)
  695. }
  696. })
  697. b.Cleanup(func() {
  698. if err := s2.Stop(); err != nil {
  699. b.Error(err)
  700. }
  701. })
  702. // Allow time for goroutines to boot up
  703. time.Sleep(1 * time.Second)
  704. b.ResetTimer()
  705. numSuccess, numFailure := 0, 0
  706. // Send random message from foo channel to another
  707. for i := 0; i < b.N; i++ {
  708. chID := byte(i % 4)
  709. successChan := s1.Broadcast(chID, []byte("test data"))
  710. for s := range successChan {
  711. if s {
  712. numSuccess++
  713. } else {
  714. numFailure++
  715. }
  716. }
  717. }
  718. b.Logf("success: %v, failure: %v", numSuccess, numFailure)
  719. }