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.

846 lines
20 KiB

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