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.

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