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.

676 lines
19 KiB

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: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
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: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
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: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
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: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
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: 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
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
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
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
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
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
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
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
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
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
  1. package pex
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "time"
  10. "github.com/gogo/protobuf/proto"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. "github.com/tendermint/tendermint/config"
  14. "github.com/tendermint/tendermint/libs/log"
  15. "github.com/tendermint/tendermint/p2p"
  16. "github.com/tendermint/tendermint/p2p/mock"
  17. tmp2p "github.com/tendermint/tendermint/proto/tendermint/p2p"
  18. )
  19. var (
  20. cfg *config.P2PConfig
  21. )
  22. func init() {
  23. cfg = config.DefaultP2PConfig()
  24. cfg.PexReactor = true
  25. cfg.AllowDuplicateIP = true
  26. }
  27. func TestPEXReactorBasic(t *testing.T) {
  28. r, _ := createReactor(t, &ReactorConfig{})
  29. assert.NotNil(t, r)
  30. assert.NotEmpty(t, r.GetChannels())
  31. }
  32. func TestPEXReactorAddRemovePeer(t *testing.T) {
  33. r, book := createReactor(t, &ReactorConfig{})
  34. size := book.Size()
  35. peer := p2p.CreateRandomPeer(false)
  36. r.AddPeer(peer)
  37. assert.Equal(t, size+1, book.Size())
  38. r.RemovePeer(peer, "peer not available")
  39. outboundPeer := p2p.CreateRandomPeer(true)
  40. r.AddPeer(outboundPeer)
  41. assert.Equal(t, size+1, book.Size(), "outbound peers should not be added to the address book")
  42. r.RemovePeer(outboundPeer, "peer not available")
  43. }
  44. // --- FAIL: TestPEXReactorRunning (11.10s)
  45. // pex_reactor_test.go:411: expected all switches to be connected to at
  46. // least one peer (switches: 0 => {outbound: 1, inbound: 0}, 1 =>
  47. // {outbound: 0, inbound: 1}, 2 => {outbound: 0, inbound: 0}, )
  48. //
  49. // EXPLANATION: peers are getting rejected because in switch#addPeer we check
  50. // if any peer (who we already connected to) has the same IP. Even though local
  51. // peers have different IP addresses, they all have the same underlying remote
  52. // IP: 127.0.0.1.
  53. //
  54. func TestPEXReactorRunning(t *testing.T) {
  55. N := 3
  56. switches := make([]*p2p.Switch, N)
  57. // directory to store address books
  58. dir := tempDir(t)
  59. books := make([]AddrBook, N)
  60. logger := log.TestingLogger()
  61. // create switches
  62. for i := 0; i < N; i++ {
  63. switches[i] = p2p.MakeSwitch(cfg, i, "testing", "123.123.123", func(i int, sw *p2p.Switch) *p2p.Switch {
  64. books[i] = NewAddrBook(filepath.Join(dir, fmt.Sprintf("addrbook%d.json", i)), false)
  65. books[i].SetLogger(logger.With("pex", i))
  66. sw.SetAddrBook(books[i])
  67. sw.SetLogger(logger.With("pex", i))
  68. r := NewReactor(books[i], &ReactorConfig{})
  69. r.SetLogger(logger.With("pex", i))
  70. r.SetEnsurePeersPeriod(250 * time.Millisecond)
  71. sw.AddReactor("pex", r)
  72. return sw
  73. })
  74. }
  75. for _, sw := range switches {
  76. err := sw.Start() // start switch and reactors
  77. require.Nil(t, err)
  78. }
  79. addOtherNodeAddrToAddrBook := func(switchIndex, otherSwitchIndex int) {
  80. addr := switches[otherSwitchIndex].NetAddress()
  81. err := books[switchIndex].AddAddress(addr, addr)
  82. require.NoError(t, err)
  83. }
  84. addOtherNodeAddrToAddrBook(0, 1)
  85. addOtherNodeAddrToAddrBook(1, 0)
  86. addOtherNodeAddrToAddrBook(2, 1)
  87. assertPeersWithTimeout(t, switches, 10*time.Millisecond, 10*time.Second, N-1)
  88. // stop them
  89. for _, s := range switches {
  90. err := s.Stop()
  91. require.NoError(t, err)
  92. }
  93. }
  94. func TestPEXReactorReceive(t *testing.T) {
  95. r, book := createReactor(t, &ReactorConfig{})
  96. peer := p2p.CreateRandomPeer(false)
  97. // we have to send a request to receive responses
  98. r.RequestAddrs(peer)
  99. size := book.Size()
  100. na, err := peer.NodeInfo().NetAddress()
  101. require.NoError(t, err)
  102. msg := mustEncode(&tmp2p.PexResponse{Addresses: []tmp2p.PexAddress{na.ToProto()}})
  103. r.Receive(PexChannel, peer, msg)
  104. assert.Equal(t, size+1, book.Size())
  105. msg = mustEncode(&tmp2p.PexRequest{})
  106. r.Receive(PexChannel, peer, msg) // should not panic.
  107. }
  108. func TestPEXReactorRequestMessageAbuse(t *testing.T) {
  109. r, book := createReactor(t, &ReactorConfig{})
  110. sw := createSwitchAndAddReactors(r)
  111. sw.SetAddrBook(book)
  112. peer := mock.NewPeer(nil)
  113. peerAddr := peer.SocketAddr()
  114. p2p.AddPeerToSwitchPeerSet(sw, peer)
  115. assert.True(t, sw.Peers().Has(peer.ID()))
  116. err := book.AddAddress(peerAddr, peerAddr)
  117. require.NoError(t, err)
  118. require.True(t, book.HasAddress(peerAddr))
  119. id := string(peer.ID())
  120. msg := mustEncode(&tmp2p.PexRequest{})
  121. // first time creates the entry
  122. r.Receive(PexChannel, peer, msg)
  123. assert.True(t, r.lastReceivedRequests.Has(id))
  124. assert.True(t, sw.Peers().Has(peer.ID()))
  125. // next time sets the last time value
  126. r.Receive(PexChannel, peer, msg)
  127. assert.True(t, r.lastReceivedRequests.Has(id))
  128. assert.True(t, sw.Peers().Has(peer.ID()))
  129. // third time is too many too soon - peer is removed
  130. r.Receive(PexChannel, peer, msg)
  131. assert.False(t, r.lastReceivedRequests.Has(id))
  132. assert.False(t, sw.Peers().Has(peer.ID()))
  133. assert.True(t, book.IsBanned(peerAddr))
  134. }
  135. func TestPEXReactorAddrsMessageAbuse(t *testing.T) {
  136. r, book := createReactor(t, &ReactorConfig{})
  137. sw := createSwitchAndAddReactors(r)
  138. sw.SetAddrBook(book)
  139. peer := mock.NewPeer(nil)
  140. p2p.AddPeerToSwitchPeerSet(sw, peer)
  141. assert.True(t, sw.Peers().Has(peer.ID()))
  142. id := string(peer.ID())
  143. // request addrs from the peer
  144. r.RequestAddrs(peer)
  145. assert.True(t, r.requestsSent.Has(id))
  146. assert.True(t, sw.Peers().Has(peer.ID()))
  147. msg := mustEncode(&tmp2p.PexResponse{Addresses: []tmp2p.PexAddress{peer.SocketAddr().ToProto()}})
  148. // receive some addrs. should clear the request
  149. r.Receive(PexChannel, peer, msg)
  150. assert.False(t, r.requestsSent.Has(id))
  151. assert.True(t, sw.Peers().Has(peer.ID()))
  152. // receiving more unsolicited addrs causes a disconnect and ban
  153. r.Receive(PexChannel, peer, msg)
  154. assert.False(t, sw.Peers().Has(peer.ID()))
  155. assert.True(t, book.IsBanned(peer.SocketAddr()))
  156. }
  157. func TestCheckSeeds(t *testing.T) {
  158. // directory to store address books
  159. dir := tempDir(t)
  160. // 1. test creating peer with no seeds works
  161. peerSwitch := testCreateDefaultPeer(dir, 0)
  162. require.Nil(t, peerSwitch.Start())
  163. peerSwitch.Stop() // nolint:errcheck // ignore for tests
  164. // 2. create seed
  165. seed := testCreateSeed(dir, 1, []*p2p.NetAddress{}, []*p2p.NetAddress{})
  166. // 3. test create peer with online seed works
  167. peerSwitch = testCreatePeerWithSeed(dir, 2, seed)
  168. require.Nil(t, peerSwitch.Start())
  169. peerSwitch.Stop() // nolint:errcheck // ignore for tests
  170. // 4. test create peer with all seeds having unresolvable DNS fails
  171. badPeerConfig := &ReactorConfig{
  172. Seeds: []string{"ed3dfd27bfc4af18f67a49862f04cc100696e84d@bad.network.addr:26657",
  173. "d824b13cb5d40fa1d8a614e089357c7eff31b670@anotherbad.network.addr:26657"},
  174. }
  175. peerSwitch = testCreatePeerWithConfig(dir, 2, badPeerConfig)
  176. require.Error(t, peerSwitch.Start())
  177. peerSwitch.Stop() // nolint:errcheck // ignore for tests
  178. // 5. test create peer with one good seed address succeeds
  179. badPeerConfig = &ReactorConfig{
  180. Seeds: []string{"ed3dfd27bfc4af18f67a49862f04cc100696e84d@bad.network.addr:26657",
  181. "d824b13cb5d40fa1d8a614e089357c7eff31b670@anotherbad.network.addr:26657",
  182. seed.NetAddress().String()},
  183. }
  184. peerSwitch = testCreatePeerWithConfig(dir, 2, badPeerConfig)
  185. require.Nil(t, peerSwitch.Start())
  186. peerSwitch.Stop() // nolint:errcheck // ignore for tests
  187. }
  188. func TestPEXReactorUsesSeedsIfNeeded(t *testing.T) {
  189. // directory to store address books
  190. dir := tempDir(t)
  191. // 1. create seed
  192. seed := testCreateSeed(dir, 0, []*p2p.NetAddress{}, []*p2p.NetAddress{})
  193. require.Nil(t, seed.Start())
  194. t.Cleanup(func() { _ = seed.Stop() })
  195. // 2. create usual peer with only seed configured.
  196. peer := testCreatePeerWithSeed(dir, 1, seed)
  197. require.Nil(t, peer.Start())
  198. t.Cleanup(func() { _ = peer.Stop() })
  199. // 3. check that the peer connects to seed immediately
  200. assertPeersWithTimeout(t, []*p2p.Switch{peer}, 10*time.Millisecond, 3*time.Second, 1)
  201. }
  202. func TestConnectionSpeedForPeerReceivedFromSeed(t *testing.T) {
  203. // directory to store address books
  204. dir := tempDir(t)
  205. // 1. create peer
  206. peerSwitch := testCreateDefaultPeer(dir, 1)
  207. require.Nil(t, peerSwitch.Start())
  208. t.Cleanup(func() { _ = peerSwitch.Stop() })
  209. // 2. Create seed which knows about the peer
  210. peerAddr := peerSwitch.NetAddress()
  211. seed := testCreateSeed(dir, 2, []*p2p.NetAddress{peerAddr}, []*p2p.NetAddress{peerAddr})
  212. require.Nil(t, seed.Start())
  213. t.Cleanup(func() { _ = seed.Stop() })
  214. // 3. create another peer with only seed configured.
  215. secondPeer := testCreatePeerWithSeed(dir, 3, seed)
  216. require.Nil(t, secondPeer.Start())
  217. t.Cleanup(func() { _ = secondPeer.Stop() })
  218. // 4. check that the second peer connects to seed immediately
  219. assertPeersWithTimeout(t, []*p2p.Switch{secondPeer}, 10*time.Millisecond, 3*time.Second, 1)
  220. // 5. check that the second peer connects to the first peer immediately
  221. assertPeersWithTimeout(t, []*p2p.Switch{secondPeer}, 10*time.Millisecond, 1*time.Second, 2)
  222. }
  223. func TestPEXReactorSeedMode(t *testing.T) {
  224. // directory to store address books
  225. dir := tempDir(t)
  226. pexRConfig := &ReactorConfig{SeedMode: true, SeedDisconnectWaitPeriod: 10 * time.Millisecond}
  227. pexR, book := createReactor(t, pexRConfig)
  228. sw := createSwitchAndAddReactors(pexR)
  229. sw.SetAddrBook(book)
  230. require.NoError(t, sw.Start())
  231. t.Cleanup(func() { _ = sw.Stop() })
  232. assert.Zero(t, sw.Peers().Size())
  233. peerSwitch := testCreateDefaultPeer(dir, 1)
  234. require.NoError(t, peerSwitch.Start())
  235. t.Cleanup(func() { _ = peerSwitch.Stop() })
  236. // 1. Test crawlPeers dials the peer
  237. pexR.crawlPeers([]*p2p.NetAddress{peerSwitch.NetAddress()})
  238. assert.Equal(t, 1, sw.Peers().Size())
  239. assert.True(t, sw.Peers().Has(peerSwitch.NodeInfo().ID()))
  240. // 2. attemptDisconnects should not disconnect because of wait period
  241. pexR.attemptDisconnects()
  242. assert.Equal(t, 1, sw.Peers().Size())
  243. // sleep for SeedDisconnectWaitPeriod
  244. time.Sleep(pexRConfig.SeedDisconnectWaitPeriod + 1*time.Millisecond)
  245. // 3. attemptDisconnects should disconnect after wait period
  246. pexR.attemptDisconnects()
  247. assert.Equal(t, 0, sw.Peers().Size())
  248. }
  249. func TestPEXReactorDoesNotDisconnectFromPersistentPeerInSeedMode(t *testing.T) {
  250. // directory to store address books
  251. dir := tempDir(t)
  252. pexRConfig := &ReactorConfig{SeedMode: true, SeedDisconnectWaitPeriod: 1 * time.Millisecond}
  253. pexR, book := createReactor(t, pexRConfig)
  254. sw := createSwitchAndAddReactors(pexR)
  255. sw.SetAddrBook(book)
  256. require.NoError(t, sw.Start())
  257. t.Cleanup(func() { _ = sw.Stop() })
  258. assert.Zero(t, sw.Peers().Size())
  259. peerSwitch := testCreatePeerWithConfig(dir, 1, pexRConfig)
  260. require.NoError(t, peerSwitch.Start())
  261. t.Cleanup(func() { _ = peerSwitch.Stop() })
  262. require.NoError(t, sw.AddPersistentPeers([]string{peerSwitch.NetAddress().String()}))
  263. // 1. Test crawlPeers dials the peer
  264. pexR.crawlPeers([]*p2p.NetAddress{peerSwitch.NetAddress()})
  265. assert.Equal(t, 1, sw.Peers().Size())
  266. assert.True(t, sw.Peers().Has(peerSwitch.NodeInfo().ID()))
  267. // sleep for SeedDisconnectWaitPeriod
  268. time.Sleep(pexRConfig.SeedDisconnectWaitPeriod + 1*time.Millisecond)
  269. // 2. attemptDisconnects should not disconnect because the peer is persistent
  270. pexR.attemptDisconnects()
  271. assert.Equal(t, 1, sw.Peers().Size())
  272. }
  273. func TestPEXReactorDialsPeerUpToMaxAttemptsInSeedMode(t *testing.T) {
  274. // directory to store address books
  275. pexR, book := createReactor(t, &ReactorConfig{SeedMode: true})
  276. sw := createSwitchAndAddReactors(pexR)
  277. sw.SetAddrBook(book)
  278. // No need to start sw since crawlPeers is called manually here.
  279. peer := mock.NewPeer(nil)
  280. addr := peer.SocketAddr()
  281. require.NoError(t, book.AddAddress(addr, addr))
  282. assert.True(t, book.HasAddress(addr))
  283. // imitate maxAttemptsToDial reached
  284. pexR.attemptsToDial.Store(addr.DialString(), _attemptsToDial{maxAttemptsToDial + 1, time.Now()})
  285. pexR.crawlPeers([]*p2p.NetAddress{addr})
  286. assert.False(t, book.HasAddress(addr))
  287. }
  288. // connect a peer to a seed, wait a bit, then stop it.
  289. // this should give it time to request addrs and for the seed
  290. // to call FlushStop, and allows us to test calling Stop concurrently
  291. // with FlushStop. Before a fix, this non-deterministically reproduced
  292. // https://github.com/tendermint/tendermint/issues/3231.
  293. func TestPEXReactorSeedModeFlushStop(t *testing.T) {
  294. N := 2
  295. switches := make([]*p2p.Switch, N)
  296. // directory to store address books
  297. dir := tempDir(t)
  298. books := make([]AddrBook, N)
  299. logger := log.TestingLogger()
  300. // create switches
  301. for i := 0; i < N; i++ {
  302. switches[i] = p2p.MakeSwitch(cfg, i, "testing", "123.123.123", func(i int, sw *p2p.Switch) *p2p.Switch {
  303. books[i] = NewAddrBook(filepath.Join(dir, fmt.Sprintf("addrbook%d.json", i)), false)
  304. books[i].SetLogger(logger.With("pex", i))
  305. sw.SetAddrBook(books[i])
  306. sw.SetLogger(logger.With("pex", i))
  307. config := &ReactorConfig{}
  308. if i == 0 {
  309. // first one is a seed node
  310. config = &ReactorConfig{SeedMode: true}
  311. }
  312. r := NewReactor(books[i], config)
  313. r.SetLogger(logger.With("pex", i))
  314. r.SetEnsurePeersPeriod(250 * time.Millisecond)
  315. sw.AddReactor("pex", r)
  316. return sw
  317. })
  318. }
  319. for _, sw := range switches {
  320. err := sw.Start() // start switch and reactors
  321. require.Nil(t, err)
  322. }
  323. reactor := switches[0].Reactors()["pex"].(*Reactor)
  324. peerID := switches[1].NodeInfo().ID()
  325. assert.NoError(t, switches[1].DialPeerWithAddress(switches[0].NetAddress()))
  326. // sleep up to a second while waiting for the peer to send us a message.
  327. // this isn't perfect since it's possible the peer sends us a msg and we FlushStop
  328. // before this loop catches it. but non-deterministically it works pretty well.
  329. for i := 0; i < 1000; i++ {
  330. v := reactor.lastReceivedRequests.Get(string(peerID))
  331. if v != nil {
  332. break
  333. }
  334. time.Sleep(time.Millisecond)
  335. }
  336. // by now the FlushStop should have happened. Try stopping the peer.
  337. // it should be safe to do this.
  338. peers := switches[0].Peers().List()
  339. for _, peer := range peers {
  340. err := peer.Stop()
  341. require.NoError(t, err)
  342. }
  343. // stop the switches
  344. for _, s := range switches {
  345. err := s.Stop()
  346. require.NoError(t, err)
  347. }
  348. }
  349. func TestPEXReactorDoesNotAddPrivatePeersToAddrBook(t *testing.T) {
  350. peer := p2p.CreateRandomPeer(false)
  351. pexR, book := createReactor(t, &ReactorConfig{})
  352. book.AddPrivateIDs([]string{string(peer.NodeInfo().ID())})
  353. // we have to send a request to receive responses
  354. pexR.RequestAddrs(peer)
  355. size := book.Size()
  356. na, err := peer.NodeInfo().NetAddress()
  357. require.NoError(t, err)
  358. msg := mustEncode(&tmp2p.PexResponse{Addresses: []tmp2p.PexAddress{na.ToProto()}})
  359. pexR.Receive(PexChannel, peer, msg)
  360. assert.Equal(t, size, book.Size())
  361. pexR.AddPeer(peer)
  362. assert.Equal(t, size, book.Size())
  363. }
  364. func TestPEXReactorDialPeer(t *testing.T) {
  365. pexR, book := createReactor(t, &ReactorConfig{})
  366. sw := createSwitchAndAddReactors(pexR)
  367. sw.SetAddrBook(book)
  368. peer := mock.NewPeer(nil)
  369. addr := peer.SocketAddr()
  370. assert.Equal(t, 0, pexR.AttemptsToDial(addr))
  371. // 1st unsuccessful attempt
  372. err := pexR.dialPeer(addr)
  373. require.Error(t, err)
  374. assert.Equal(t, 1, pexR.AttemptsToDial(addr))
  375. // 2nd unsuccessful attempt
  376. err = pexR.dialPeer(addr)
  377. require.Error(t, err)
  378. // must be skipped because it is too early
  379. assert.Equal(t, 1, pexR.AttemptsToDial(addr))
  380. if !testing.Short() {
  381. time.Sleep(3 * time.Second)
  382. // 3rd attempt
  383. err = pexR.dialPeer(addr)
  384. require.Error(t, err)
  385. assert.Equal(t, 2, pexR.AttemptsToDial(addr))
  386. }
  387. }
  388. func assertPeersWithTimeout(
  389. t *testing.T,
  390. switches []*p2p.Switch,
  391. checkPeriod, timeout time.Duration,
  392. nPeers int,
  393. ) {
  394. var (
  395. ticker = time.NewTicker(checkPeriod)
  396. remaining = timeout
  397. )
  398. for {
  399. select {
  400. case <-ticker.C:
  401. // check peers are connected
  402. allGood := true
  403. for _, s := range switches {
  404. outbound, inbound, _ := s.NumPeers()
  405. if outbound+inbound < nPeers {
  406. allGood = false
  407. break
  408. }
  409. }
  410. remaining -= checkPeriod
  411. if remaining < 0 {
  412. remaining = 0
  413. }
  414. if allGood {
  415. return
  416. }
  417. case <-time.After(remaining):
  418. numPeersStr := ""
  419. for i, s := range switches {
  420. outbound, inbound, _ := s.NumPeers()
  421. numPeersStr += fmt.Sprintf("%d => {outbound: %d, inbound: %d}, ", i, outbound, inbound)
  422. }
  423. t.Errorf(
  424. "expected all switches to be connected to at least %d peer(s) (switches: %s)",
  425. nPeers, numPeersStr,
  426. )
  427. return
  428. }
  429. }
  430. }
  431. // Creates a peer with the provided config
  432. func testCreatePeerWithConfig(dir string, id int, config *ReactorConfig) *p2p.Switch {
  433. peer := p2p.MakeSwitch(
  434. cfg,
  435. id,
  436. "127.0.0.1",
  437. "123.123.123",
  438. func(i int, sw *p2p.Switch) *p2p.Switch {
  439. book := NewAddrBook(filepath.Join(dir, fmt.Sprintf("addrbook%d.json", id)), false)
  440. book.SetLogger(log.TestingLogger())
  441. sw.SetAddrBook(book)
  442. sw.SetLogger(log.TestingLogger())
  443. r := NewReactor(
  444. book,
  445. config,
  446. )
  447. r.SetLogger(log.TestingLogger())
  448. sw.AddReactor("pex", r)
  449. return sw
  450. },
  451. )
  452. return peer
  453. }
  454. // Creates a peer with the default config
  455. func testCreateDefaultPeer(dir string, id int) *p2p.Switch {
  456. return testCreatePeerWithConfig(dir, id, &ReactorConfig{})
  457. }
  458. // Creates a seed which knows about the provided addresses / source address pairs.
  459. // Starting and stopping the seed is left to the caller
  460. func testCreateSeed(dir string, id int, knownAddrs, srcAddrs []*p2p.NetAddress) *p2p.Switch {
  461. seed := p2p.MakeSwitch(
  462. cfg,
  463. id,
  464. "127.0.0.1",
  465. "123.123.123",
  466. func(i int, sw *p2p.Switch) *p2p.Switch {
  467. book := NewAddrBook(filepath.Join(dir, "addrbookSeed.json"), false)
  468. book.SetLogger(log.TestingLogger())
  469. for j := 0; j < len(knownAddrs); j++ {
  470. book.AddAddress(knownAddrs[j], srcAddrs[j]) // nolint:errcheck // ignore for tests
  471. book.MarkGood(knownAddrs[j].ID)
  472. }
  473. sw.SetAddrBook(book)
  474. sw.SetLogger(log.TestingLogger())
  475. r := NewReactor(book, &ReactorConfig{})
  476. r.SetLogger(log.TestingLogger())
  477. sw.AddReactor("pex", r)
  478. return sw
  479. },
  480. )
  481. return seed
  482. }
  483. // Creates a peer which knows about the provided seed.
  484. // Starting and stopping the peer is left to the caller
  485. func testCreatePeerWithSeed(dir string, id int, seed *p2p.Switch) *p2p.Switch {
  486. conf := &ReactorConfig{
  487. Seeds: []string{seed.NetAddress().String()},
  488. }
  489. return testCreatePeerWithConfig(dir, id, conf)
  490. }
  491. func createReactor(t *testing.T, conf *ReactorConfig) (r *Reactor, book AddrBook) {
  492. // directory to store address book
  493. book = NewAddrBook(filepath.Join(tempDir(t), "addrbook.json"), true)
  494. book.SetLogger(log.TestingLogger())
  495. r = NewReactor(book, conf)
  496. r.SetLogger(log.TestingLogger())
  497. return
  498. }
  499. func createSwitchAndAddReactors(reactors ...p2p.Reactor) *p2p.Switch {
  500. sw := p2p.MakeSwitch(cfg, 0, "127.0.0.1", "123.123.123", func(i int, sw *p2p.Switch) *p2p.Switch {
  501. for _, r := range reactors {
  502. sw.AddReactor(r.String(), r)
  503. }
  504. return sw
  505. })
  506. sw.SetLogger(log.TestingLogger())
  507. return sw
  508. }
  509. func TestPexVectors(t *testing.T) {
  510. addr := tmp2p.PexAddress{
  511. ID: "1",
  512. IP: "127.0.0.1",
  513. Port: 9090,
  514. }
  515. testCases := []struct {
  516. testName string
  517. msg proto.Message
  518. expBytes string
  519. }{
  520. {"PexRequest", &tmp2p.PexRequest{}, "0a00"},
  521. {"PexAddrs", &tmp2p.PexResponse{Addresses: []tmp2p.PexAddress{addr}}, "12130a110a013112093132372e302e302e31188247"},
  522. }
  523. for _, tc := range testCases {
  524. tc := tc
  525. bz := mustEncode(tc.msg)
  526. require.Equal(t, tc.expBytes, hex.EncodeToString(bz), tc.testName)
  527. }
  528. }
  529. // FIXME: This function is used in place of testing.TB.TempDir()
  530. // as the latter seems to cause test cases to fail when it is
  531. // unable to remove the temporary directory once the test case
  532. // execution terminates. This seems to happen often with pex
  533. // reactor test cases.
  534. //
  535. // References:
  536. // https://github.com/tendermint/tendermint/pull/5733
  537. // https://github.com/tendermint/tendermint/issues/5732
  538. func tempDir(t *testing.T) string {
  539. t.Helper()
  540. dir, err := ioutil.TempDir("", "")
  541. require.NoError(t, err)
  542. t.Cleanup(func() { _ = os.RemoveAll(dir) })
  543. return dir
  544. }