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.

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