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.

762 lines
21 KiB

9 years ago
9 years ago
9 years ago
9 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
7 years ago
9 years ago
9 years ago
9 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
9 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
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
8 years ago
7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 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
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 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: introduce peerConn to simplify peer creation (#1226) * expose AuthEnc in the P2P config if AuthEnc is true, dialed peers must have a node ID in the address and it must match the persistent pubkey from the secret handshake. Refs #1157 * fixes after my own review * fix docs * fix build failure ``` p2p/pex/pex_reactor_test.go:288:88: cannot use seed.NodeInfo().NetAddress() (type *p2p.NetAddress) as type string in array or slice literal ``` * p2p: introduce peerConn to simplify peer creation * Introduce `peerConn` containing the known fields of `peer` * `peer` only created in `sw.addPeer` once handshake is complete and NodeInfo is checked * Eliminates some mutable variables and makes the code flow better * Simplifies the `newXxxPeer` funcs * Use ID instead of PubKey where possible. * SetPubKeyFilter -> SetIDFilter * nodeInfo.Validate takes ID * remove peer.PubKey() * persistent node ids * fixes from review * test: use ip_plus_id.sh more * fix invalid memory panic during fast_sync test ``` 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: panic: runtime error: invalid memory address or nil pointer dereference 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x98dd3e] 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: goroutine 3432 [running]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.newOutboundPeerConn(0xc423fd1380, 0xc420933e00, 0x1, 0x1239a60, 0 xc420128c40, 0x2, 0x42caf6, 0xc42001f300, 0xc422831d98, 0xc4227951c0, ...) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/peer.go:123 +0x31e 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).addOutboundPeerWithConfig(0xc4200ad040, 0xc423fd1380, 0 xc420933e00, 0xc423f48801, 0x28, 0x2) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:455 +0x12b 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).DialPeerWithAddress(0xc4200ad040, 0xc423fd1380, 0x1, 0x 0, 0x0) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:371 +0xdc 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).reconnectToPeer(0xc4200ad040, 0x123e000, 0xc42007bb00) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:290 +0x25f 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: created by github.com/tendermint/tendermint/p2p.(*Switch).StopPeerForError 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:256 +0x1b7 ```
7 years ago
p2p: introduce peerConn to simplify peer creation (#1226) * expose AuthEnc in the P2P config if AuthEnc is true, dialed peers must have a node ID in the address and it must match the persistent pubkey from the secret handshake. Refs #1157 * fixes after my own review * fix docs * fix build failure ``` p2p/pex/pex_reactor_test.go:288:88: cannot use seed.NodeInfo().NetAddress() (type *p2p.NetAddress) as type string in array or slice literal ``` * p2p: introduce peerConn to simplify peer creation * Introduce `peerConn` containing the known fields of `peer` * `peer` only created in `sw.addPeer` once handshake is complete and NodeInfo is checked * Eliminates some mutable variables and makes the code flow better * Simplifies the `newXxxPeer` funcs * Use ID instead of PubKey where possible. * SetPubKeyFilter -> SetIDFilter * nodeInfo.Validate takes ID * remove peer.PubKey() * persistent node ids * fixes from review * test: use ip_plus_id.sh more * fix invalid memory panic during fast_sync test ``` 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: panic: runtime error: invalid memory address or nil pointer dereference 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x98dd3e] 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: goroutine 3432 [running]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.newOutboundPeerConn(0xc423fd1380, 0xc420933e00, 0x1, 0x1239a60, 0 xc420128c40, 0x2, 0x42caf6, 0xc42001f300, 0xc422831d98, 0xc4227951c0, ...) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/peer.go:123 +0x31e 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).addOutboundPeerWithConfig(0xc4200ad040, 0xc423fd1380, 0 xc420933e00, 0xc423f48801, 0x28, 0x2) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:455 +0x12b 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).DialPeerWithAddress(0xc4200ad040, 0xc423fd1380, 0x1, 0x 0, 0x0) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:371 +0xdc 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).reconnectToPeer(0xc4200ad040, 0x123e000, 0xc42007bb00) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:290 +0x25f 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: created by github.com/tendermint/tendermint/p2p.(*Switch).StopPeerForError 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:256 +0x1b7 ```
7 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
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
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
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
9 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
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package pex
  2. import (
  3. "fmt"
  4. "reflect"
  5. "sync"
  6. "time"
  7. "github.com/pkg/errors"
  8. amino "github.com/tendermint/go-amino"
  9. cmn "github.com/tendermint/tendermint/libs/common"
  10. "github.com/tendermint/tendermint/p2p"
  11. "github.com/tendermint/tendermint/p2p/conn"
  12. )
  13. type Peer = p2p.Peer
  14. const (
  15. // PexChannel is a channel for PEX messages
  16. PexChannel = byte(0x00)
  17. // over-estimate of max NetAddress size
  18. // hexID (40) + IP (16) + Port (2) + Name (100) ...
  19. // NOTE: dont use massive DNS name ..
  20. maxAddressSize = 256
  21. // NOTE: amplificaiton factor!
  22. // small request results in up to maxMsgSize response
  23. maxMsgSize = maxAddressSize * maxGetSelection
  24. // ensure we have enough peers
  25. defaultEnsurePeersPeriod = 30 * time.Second
  26. // Seed/Crawler constants
  27. // minTimeBetweenCrawls is a minimum time between attempts to crawl a peer.
  28. minTimeBetweenCrawls = 2 * time.Minute
  29. // check some peers every this
  30. crawlPeerPeriod = 30 * time.Second
  31. maxAttemptsToDial = 16 // ~ 35h in total (last attempt - 18h)
  32. // if node connects to seed, it does not have any trusted peers.
  33. // Especially in the beginning, node should have more trusted peers than
  34. // untrusted.
  35. biasToSelectNewPeers = 30 // 70 to select good peers
  36. )
  37. type errMaxAttemptsToDial struct {
  38. }
  39. func (e errMaxAttemptsToDial) Error() string {
  40. return fmt.Sprintf("reached max attempts %d to dial", maxAttemptsToDial)
  41. }
  42. type errTooEarlyToDial struct {
  43. backoffDuration time.Duration
  44. lastDialed time.Time
  45. }
  46. func (e errTooEarlyToDial) Error() string {
  47. return fmt.Sprintf(
  48. "too early to dial (backoff duration: %d, last dialed: %v, time since: %v)",
  49. e.backoffDuration, e.lastDialed, time.Since(e.lastDialed))
  50. }
  51. // PEXReactor handles PEX (peer exchange) and ensures that an
  52. // adequate number of peers are connected to the switch.
  53. //
  54. // It uses `AddrBook` (address book) to store `NetAddress`es of the peers.
  55. //
  56. // ## Preventing abuse
  57. //
  58. // Only accept pexAddrsMsg from peers we sent a corresponding pexRequestMsg too.
  59. // Only accept one pexRequestMsg every ~defaultEnsurePeersPeriod.
  60. type PEXReactor struct {
  61. p2p.BaseReactor
  62. book AddrBook
  63. config *PEXReactorConfig
  64. ensurePeersPeriod time.Duration // TODO: should go in the config
  65. // maps to prevent abuse
  66. requestsSent *cmn.CMap // ID->struct{}: unanswered send requests
  67. lastReceivedRequests *cmn.CMap // ID->time.Time: last time peer requested from us
  68. seedAddrs []*p2p.NetAddress
  69. attemptsToDial sync.Map // address (string) -> {number of attempts (int), last time dialed (time.Time)}
  70. // seed/crawled mode fields
  71. crawlPeerInfos map[p2p.ID]crawlPeerInfo
  72. }
  73. func (r *PEXReactor) minReceiveRequestInterval() time.Duration {
  74. // NOTE: must be less than ensurePeersPeriod, otherwise we'll request
  75. // peers too quickly from others and they'll think we're bad!
  76. return r.ensurePeersPeriod / 3
  77. }
  78. // PEXReactorConfig holds reactor specific configuration data.
  79. type PEXReactorConfig struct {
  80. // Seed/Crawler mode
  81. SeedMode bool
  82. // We want seeds to only advertise good peers. Therefore they should wait at
  83. // least as long as we expect it to take for a peer to become good before
  84. // disconnecting.
  85. SeedDisconnectWaitPeriod time.Duration
  86. // Seeds is a list of addresses reactor may use
  87. // if it can't connect to peers in the addrbook.
  88. Seeds []string
  89. }
  90. type _attemptsToDial struct {
  91. number int
  92. lastDialed time.Time
  93. }
  94. // NewPEXReactor creates new PEX reactor.
  95. func NewPEXReactor(b AddrBook, config *PEXReactorConfig) *PEXReactor {
  96. r := &PEXReactor{
  97. book: b,
  98. config: config,
  99. ensurePeersPeriod: defaultEnsurePeersPeriod,
  100. requestsSent: cmn.NewCMap(),
  101. lastReceivedRequests: cmn.NewCMap(),
  102. crawlPeerInfos: make(map[p2p.ID]crawlPeerInfo),
  103. }
  104. r.BaseReactor = *p2p.NewBaseReactor("PEXReactor", r)
  105. return r
  106. }
  107. // OnStart implements BaseService
  108. func (r *PEXReactor) OnStart() error {
  109. err := r.book.Start()
  110. if err != nil && err != cmn.ErrAlreadyStarted {
  111. return err
  112. }
  113. numOnline, seedAddrs, err := r.checkSeeds()
  114. if err != nil {
  115. return err
  116. } else if numOnline == 0 && r.book.Empty() {
  117. return errors.New("Address book is empty and couldn't resolve any seed nodes")
  118. }
  119. r.seedAddrs = seedAddrs
  120. // Check if this node should run
  121. // in seed/crawler mode
  122. if r.config.SeedMode {
  123. go r.crawlPeersRoutine()
  124. } else {
  125. go r.ensurePeersRoutine()
  126. }
  127. return nil
  128. }
  129. // OnStop implements BaseService
  130. func (r *PEXReactor) OnStop() {
  131. r.book.Stop()
  132. }
  133. // GetChannels implements Reactor
  134. func (r *PEXReactor) GetChannels() []*conn.ChannelDescriptor {
  135. return []*conn.ChannelDescriptor{
  136. {
  137. ID: PexChannel,
  138. Priority: 1,
  139. SendQueueCapacity: 10,
  140. },
  141. }
  142. }
  143. // AddPeer implements Reactor by adding peer to the address book (if inbound)
  144. // or by requesting more addresses (if outbound).
  145. func (r *PEXReactor) AddPeer(p Peer) {
  146. if p.IsOutbound() {
  147. // For outbound peers, the address is already in the books -
  148. // either via DialPeersAsync or r.Receive.
  149. // Ask it for more peers if we need.
  150. if r.book.NeedMoreAddrs() {
  151. r.RequestAddrs(p)
  152. }
  153. } else {
  154. // inbound peer is its own source
  155. addr, err := p.NodeInfo().NetAddress()
  156. if err != nil {
  157. r.Logger.Error("Failed to get peer NetAddress", "err", err, "peer", p)
  158. return
  159. }
  160. // Make it explicit that addr and src are the same for an inbound peer.
  161. src := addr
  162. // add to book. dont RequestAddrs right away because
  163. // we don't trust inbound as much - let ensurePeersRoutine handle it.
  164. err = r.book.AddAddress(addr, src)
  165. r.logErrAddrBook(err)
  166. }
  167. }
  168. func (r *PEXReactor) logErrAddrBook(err error) {
  169. if err != nil {
  170. switch err.(type) {
  171. case ErrAddrBookNilAddr:
  172. r.Logger.Error("Failed to add new address", "err", err)
  173. default:
  174. // non-routable, self, full book, private, etc.
  175. r.Logger.Debug("Failed to add new address", "err", err)
  176. }
  177. }
  178. }
  179. // RemovePeer implements Reactor.
  180. func (r *PEXReactor) RemovePeer(p Peer, reason interface{}) {
  181. id := string(p.ID())
  182. r.requestsSent.Delete(id)
  183. r.lastReceivedRequests.Delete(id)
  184. }
  185. // Receive implements Reactor by handling incoming PEX messages.
  186. func (r *PEXReactor) Receive(chID byte, src Peer, msgBytes []byte) {
  187. msg, err := decodeMsg(msgBytes)
  188. if err != nil {
  189. r.Logger.Error("Error decoding message", "src", src, "chId", chID, "msg", msg, "err", err, "bytes", msgBytes)
  190. r.Switch.StopPeerForError(src, err)
  191. return
  192. }
  193. r.Logger.Debug("Received message", "src", src, "chId", chID, "msg", msg)
  194. switch msg := msg.(type) {
  195. case *pexRequestMessage:
  196. // NOTE: this is a prime candidate for amplification attacks,
  197. // so it's important we
  198. // 1) restrict how frequently peers can request
  199. // 2) limit the output size
  200. // If we're a seed and this is an inbound peer,
  201. // respond once and disconnect.
  202. if r.config.SeedMode && !src.IsOutbound() {
  203. id := string(src.ID())
  204. v := r.lastReceivedRequests.Get(id)
  205. if v != nil {
  206. // FlushStop/StopPeer are already
  207. // running in a go-routine.
  208. return
  209. }
  210. r.lastReceivedRequests.Set(id, time.Now())
  211. // Send addrs and disconnect
  212. r.SendAddrs(src, r.book.GetSelectionWithBias(biasToSelectNewPeers))
  213. go func() {
  214. // In a go-routine so it doesn't block .Receive.
  215. src.FlushStop()
  216. r.Switch.StopPeerGracefully(src)
  217. }()
  218. } else {
  219. // Check we're not receiving requests too frequently.
  220. if err := r.receiveRequest(src); err != nil {
  221. r.Switch.StopPeerForError(src, err)
  222. return
  223. }
  224. r.SendAddrs(src, r.book.GetSelection())
  225. }
  226. case *pexAddrsMessage:
  227. // If we asked for addresses, add them to the book
  228. if err := r.ReceiveAddrs(msg.Addrs, src); err != nil {
  229. r.Switch.StopPeerForError(src, err)
  230. return
  231. }
  232. default:
  233. r.Logger.Error(fmt.Sprintf("Unknown message type %v", reflect.TypeOf(msg)))
  234. }
  235. }
  236. // enforces a minimum amount of time between requests
  237. func (r *PEXReactor) receiveRequest(src Peer) error {
  238. id := string(src.ID())
  239. v := r.lastReceivedRequests.Get(id)
  240. if v == nil {
  241. // initialize with empty time
  242. lastReceived := time.Time{}
  243. r.lastReceivedRequests.Set(id, lastReceived)
  244. return nil
  245. }
  246. lastReceived := v.(time.Time)
  247. if lastReceived.Equal(time.Time{}) {
  248. // first time gets a free pass. then we start tracking the time
  249. lastReceived = time.Now()
  250. r.lastReceivedRequests.Set(id, lastReceived)
  251. return nil
  252. }
  253. now := time.Now()
  254. minInterval := r.minReceiveRequestInterval()
  255. if now.Sub(lastReceived) < minInterval {
  256. return fmt.Errorf("Peer (%v) sent next PEX request too soon. lastReceived: %v, now: %v, minInterval: %v. Disconnecting",
  257. src.ID(),
  258. lastReceived,
  259. now,
  260. minInterval,
  261. )
  262. }
  263. r.lastReceivedRequests.Set(id, now)
  264. return nil
  265. }
  266. // RequestAddrs asks peer for more addresses if we do not already
  267. // have a request out for this peer.
  268. func (r *PEXReactor) RequestAddrs(p Peer) {
  269. r.Logger.Debug("Request addrs", "from", p)
  270. id := string(p.ID())
  271. if r.requestsSent.Has(id) {
  272. return
  273. }
  274. r.requestsSent.Set(id, struct{}{})
  275. p.Send(PexChannel, cdc.MustMarshalBinaryBare(&pexRequestMessage{}))
  276. }
  277. // ReceiveAddrs adds the given addrs to the addrbook if theres an open
  278. // request for this peer and deletes the open request.
  279. // If there's no open request for the src peer, it returns an error.
  280. func (r *PEXReactor) ReceiveAddrs(addrs []*p2p.NetAddress, src Peer) error {
  281. id := string(src.ID())
  282. if !r.requestsSent.Has(id) {
  283. return errors.New("Unsolicited pexAddrsMessage")
  284. }
  285. r.requestsSent.Delete(id)
  286. srcAddr, err := src.NodeInfo().NetAddress()
  287. if err != nil {
  288. return err
  289. }
  290. for _, netAddr := range addrs {
  291. // Validate netAddr. Disconnect from a peer if it sends us invalid data.
  292. if netAddr == nil {
  293. return errors.New("nil address in pexAddrsMessage")
  294. }
  295. // TODO: extract validating logic from NewNetAddressStringWithOptionalID
  296. // and put it in netAddr#Valid (#2722)
  297. na, err := p2p.NewNetAddressString(netAddr.String())
  298. if err != nil {
  299. return fmt.Errorf("%s address in pexAddrsMessage is invalid: %v",
  300. netAddr.String(),
  301. err,
  302. )
  303. }
  304. // NOTE: we check netAddr validity and routability in book#AddAddress.
  305. err = r.book.AddAddress(na, srcAddr)
  306. if err != nil {
  307. r.logErrAddrBook(err)
  308. // XXX: should we be strict about incoming data and disconnect from a
  309. // peer here too?
  310. continue
  311. }
  312. // If this address came from a seed node, try to connect to it without
  313. // waiting.
  314. for _, seedAddr := range r.seedAddrs {
  315. if seedAddr.Equals(srcAddr) {
  316. r.ensurePeers()
  317. }
  318. }
  319. }
  320. return nil
  321. }
  322. // SendAddrs sends addrs to the peer.
  323. func (r *PEXReactor) SendAddrs(p Peer, netAddrs []*p2p.NetAddress) {
  324. p.Send(PexChannel, cdc.MustMarshalBinaryBare(&pexAddrsMessage{Addrs: netAddrs}))
  325. }
  326. // SetEnsurePeersPeriod sets period to ensure peers connected.
  327. func (r *PEXReactor) SetEnsurePeersPeriod(d time.Duration) {
  328. r.ensurePeersPeriod = d
  329. }
  330. // Ensures that sufficient peers are connected. (continuous)
  331. func (r *PEXReactor) ensurePeersRoutine() {
  332. var (
  333. seed = cmn.NewRand()
  334. jitter = seed.Int63n(r.ensurePeersPeriod.Nanoseconds())
  335. )
  336. // Randomize first round of communication to avoid thundering herd.
  337. // If no peers are present directly start connecting so we guarantee swift
  338. // setup with the help of configured seeds.
  339. if r.nodeHasSomePeersOrDialingAny() {
  340. time.Sleep(time.Duration(jitter))
  341. }
  342. // fire once immediately.
  343. // ensures we dial the seeds right away if the book is empty
  344. r.ensurePeers()
  345. // fire periodically
  346. ticker := time.NewTicker(r.ensurePeersPeriod)
  347. for {
  348. select {
  349. case <-ticker.C:
  350. r.ensurePeers()
  351. case <-r.Quit():
  352. ticker.Stop()
  353. return
  354. }
  355. }
  356. }
  357. // ensurePeers ensures that sufficient peers are connected. (once)
  358. //
  359. // heuristic that we haven't perfected yet, or, perhaps is manually edited by
  360. // the node operator. It should not be used to compute what addresses are
  361. // already connected or not.
  362. func (r *PEXReactor) ensurePeers() {
  363. var (
  364. out, in, dial = r.Switch.NumPeers()
  365. numToDial = r.Switch.MaxNumOutboundPeers() - (out + dial)
  366. )
  367. r.Logger.Info(
  368. "Ensure peers",
  369. "numOutPeers", out,
  370. "numInPeers", in,
  371. "numDialing", dial,
  372. "numToDial", numToDial,
  373. )
  374. if numToDial <= 0 {
  375. return
  376. }
  377. // bias to prefer more vetted peers when we have fewer connections.
  378. // not perfect, but somewhate ensures that we prioritize connecting to more-vetted
  379. // NOTE: range here is [10, 90]. Too high ?
  380. newBias := cmn.MinInt(out, 8)*10 + 10
  381. toDial := make(map[p2p.ID]*p2p.NetAddress)
  382. // Try maxAttempts times to pick numToDial addresses to dial
  383. maxAttempts := numToDial * 3
  384. for i := 0; i < maxAttempts && len(toDial) < numToDial; i++ {
  385. try := r.book.PickAddress(newBias)
  386. if try == nil {
  387. continue
  388. }
  389. if _, selected := toDial[try.ID]; selected {
  390. continue
  391. }
  392. if r.Switch.IsDialingOrExistingAddress(try) {
  393. continue
  394. }
  395. // TODO: consider moving some checks from toDial into here
  396. // so we don't even consider dialing peers that we want to wait
  397. // before dialling again, or have dialed too many times already
  398. r.Logger.Info("Will dial address", "addr", try)
  399. toDial[try.ID] = try
  400. }
  401. // Dial picked addresses
  402. for _, addr := range toDial {
  403. go func(addr *p2p.NetAddress) {
  404. err := r.dialPeer(addr)
  405. if err != nil {
  406. switch err.(type) {
  407. case errMaxAttemptsToDial, errTooEarlyToDial:
  408. r.Logger.Debug(err.Error(), "addr", addr)
  409. default:
  410. r.Logger.Error(err.Error(), "addr", addr)
  411. }
  412. }
  413. }(addr)
  414. }
  415. if r.book.NeedMoreAddrs() {
  416. // 1) Pick a random peer and ask for more.
  417. peers := r.Switch.Peers().List()
  418. peersCount := len(peers)
  419. if peersCount > 0 {
  420. peer := peers[cmn.RandInt()%peersCount] // nolint: gas
  421. r.Logger.Info("We need more addresses. Sending pexRequest to random peer", "peer", peer)
  422. r.RequestAddrs(peer)
  423. }
  424. // 2) Dial seeds if we are not dialing anyone.
  425. // This is done in addition to asking a peer for addresses to work-around
  426. // peers not participating in PEX.
  427. if len(toDial) == 0 {
  428. r.Logger.Info("No addresses to dial. Falling back to seeds")
  429. r.dialSeeds()
  430. }
  431. }
  432. }
  433. func (r *PEXReactor) dialAttemptsInfo(addr *p2p.NetAddress) (attempts int, lastDialed time.Time) {
  434. _attempts, ok := r.attemptsToDial.Load(addr.DialString())
  435. if !ok {
  436. return
  437. }
  438. atd := _attempts.(_attemptsToDial)
  439. return atd.number, atd.lastDialed
  440. }
  441. func (r *PEXReactor) dialPeer(addr *p2p.NetAddress) error {
  442. attempts, lastDialed := r.dialAttemptsInfo(addr)
  443. if attempts > maxAttemptsToDial {
  444. // TODO(melekes): have a blacklist in the addrbook with peers whom we've
  445. // failed to connect to. Then we can clean up attemptsToDial, which acts as
  446. // a blacklist currently.
  447. // https://github.com/tendermint/tendermint/issues/3572
  448. r.book.MarkBad(addr)
  449. return errMaxAttemptsToDial{}
  450. }
  451. // exponential backoff if it's not our first attempt to dial given address
  452. if attempts > 0 {
  453. jitterSeconds := time.Duration(cmn.RandFloat64() * float64(time.Second)) // 1s == (1e9 ns)
  454. backoffDuration := jitterSeconds + ((1 << uint(attempts)) * time.Second)
  455. sinceLastDialed := time.Since(lastDialed)
  456. if sinceLastDialed < backoffDuration {
  457. return errTooEarlyToDial{backoffDuration, lastDialed}
  458. }
  459. }
  460. err := r.Switch.DialPeerWithAddress(addr)
  461. if err != nil {
  462. if _, ok := err.(p2p.ErrCurrentlyDialingOrExistingAddress); ok {
  463. return err
  464. }
  465. markAddrInBookBasedOnErr(addr, r.book, err)
  466. switch err.(type) {
  467. case p2p.ErrSwitchAuthenticationFailure:
  468. // NOTE: addr is removed from addrbook in markAddrInBookBasedOnErr
  469. r.attemptsToDial.Delete(addr.DialString())
  470. default:
  471. r.attemptsToDial.Store(addr.DialString(), _attemptsToDial{attempts + 1, time.Now()})
  472. }
  473. return errors.Wrapf(err, "dialing failed (attempts: %d)", attempts+1)
  474. }
  475. // cleanup any history
  476. r.attemptsToDial.Delete(addr.DialString())
  477. return nil
  478. }
  479. // checkSeeds checks that addresses are well formed.
  480. // Returns number of seeds we can connect to, along with all seeds addrs.
  481. // return err if user provided any badly formatted seed addresses.
  482. // Doesn't error if the seed node can't be reached.
  483. // numOnline returns -1 if no seed nodes were in the initial configuration.
  484. func (r *PEXReactor) checkSeeds() (numOnline int, netAddrs []*p2p.NetAddress, err error) {
  485. lSeeds := len(r.config.Seeds)
  486. if lSeeds == 0 {
  487. return -1, nil, nil
  488. }
  489. netAddrs, errs := p2p.NewNetAddressStrings(r.config.Seeds)
  490. numOnline = lSeeds - len(errs)
  491. for _, err := range errs {
  492. switch e := err.(type) {
  493. case p2p.ErrNetAddressLookup:
  494. r.Logger.Error("Connecting to seed failed", "err", e)
  495. default:
  496. return 0, nil, errors.Wrap(e, "seed node configuration has error")
  497. }
  498. }
  499. return numOnline, netAddrs, nil
  500. }
  501. // randomly dial seeds until we connect to one or exhaust them
  502. func (r *PEXReactor) dialSeeds() {
  503. perm := cmn.RandPerm(len(r.seedAddrs))
  504. // perm := r.Switch.rng.Perm(lSeeds)
  505. for _, i := range perm {
  506. // dial a random seed
  507. seedAddr := r.seedAddrs[i]
  508. err := r.Switch.DialPeerWithAddress(seedAddr)
  509. if err == nil {
  510. return
  511. }
  512. r.Switch.Logger.Error("Error dialing seed", "err", err, "seed", seedAddr)
  513. }
  514. r.Switch.Logger.Error("Couldn't connect to any seeds")
  515. }
  516. // AttemptsToDial returns the number of attempts to dial specific address. It
  517. // returns 0 if never attempted or successfully connected.
  518. func (r *PEXReactor) AttemptsToDial(addr *p2p.NetAddress) int {
  519. lAttempts, attempted := r.attemptsToDial.Load(addr.DialString())
  520. if attempted {
  521. return lAttempts.(_attemptsToDial).number
  522. }
  523. return 0
  524. }
  525. //----------------------------------------------------------
  526. // Explores the network searching for more peers. (continuous)
  527. // Seed/Crawler Mode causes this node to quickly disconnect
  528. // from peers, except other seed nodes.
  529. func (r *PEXReactor) crawlPeersRoutine() {
  530. // If we have any seed nodes, consult them first
  531. if len(r.seedAddrs) > 0 {
  532. r.dialSeeds()
  533. } else {
  534. // Do an initial crawl
  535. r.crawlPeers(r.book.GetSelection())
  536. }
  537. // Fire periodically
  538. ticker := time.NewTicker(crawlPeerPeriod)
  539. for {
  540. select {
  541. case <-ticker.C:
  542. r.attemptDisconnects()
  543. r.crawlPeers(r.book.GetSelection())
  544. r.cleanupCrawlPeerInfos()
  545. case <-r.Quit():
  546. return
  547. }
  548. }
  549. }
  550. // nodeHasSomePeersOrDialingAny returns true if the node is connected to some
  551. // peers or dialing them currently.
  552. func (r *PEXReactor) nodeHasSomePeersOrDialingAny() bool {
  553. out, in, dial := r.Switch.NumPeers()
  554. return out+in+dial > 0
  555. }
  556. // crawlPeerInfo handles temporary data needed for the network crawling
  557. // performed during seed/crawler mode.
  558. type crawlPeerInfo struct {
  559. Addr *p2p.NetAddress `json:"addr"`
  560. // The last time we crawled the peer or attempted to do so.
  561. LastCrawled time.Time `json:"last_crawled"`
  562. }
  563. // crawlPeers will crawl the network looking for new peer addresses.
  564. func (r *PEXReactor) crawlPeers(addrs []*p2p.NetAddress) {
  565. now := time.Now()
  566. for _, addr := range addrs {
  567. peerInfo, ok := r.crawlPeerInfos[addr.ID]
  568. // Do not attempt to connect with peers we recently crawled.
  569. if ok && now.Sub(peerInfo.LastCrawled) < minTimeBetweenCrawls {
  570. continue
  571. }
  572. // Record crawling attempt.
  573. r.crawlPeerInfos[addr.ID] = crawlPeerInfo{
  574. Addr: addr,
  575. LastCrawled: now,
  576. }
  577. err := r.dialPeer(addr)
  578. if err != nil {
  579. switch err.(type) {
  580. case errMaxAttemptsToDial, errTooEarlyToDial:
  581. r.Logger.Debug(err.Error(), "addr", addr)
  582. default:
  583. r.Logger.Error(err.Error(), "addr", addr)
  584. }
  585. continue
  586. }
  587. peer := r.Switch.Peers().Get(addr.ID)
  588. if peer != nil {
  589. r.RequestAddrs(peer)
  590. }
  591. }
  592. }
  593. func (r *PEXReactor) cleanupCrawlPeerInfos() {
  594. for id, info := range r.crawlPeerInfos {
  595. // If we did not crawl a peer for 24 hours, it means the peer was removed
  596. // from the addrbook => remove
  597. //
  598. // 10000 addresses / maxGetSelection = 40 cycles to get all addresses in
  599. // the ideal case,
  600. // 40 * crawlPeerPeriod ~ 20 minutes
  601. if time.Since(info.LastCrawled) > 24*time.Hour {
  602. delete(r.crawlPeerInfos, id)
  603. }
  604. }
  605. }
  606. // attemptDisconnects checks if we've been with each peer long enough to disconnect
  607. func (r *PEXReactor) attemptDisconnects() {
  608. for _, peer := range r.Switch.Peers().List() {
  609. if peer.Status().Duration < r.config.SeedDisconnectWaitPeriod {
  610. continue
  611. }
  612. if peer.IsPersistent() {
  613. continue
  614. }
  615. r.Switch.StopPeerGracefully(peer)
  616. }
  617. }
  618. func markAddrInBookBasedOnErr(addr *p2p.NetAddress, book AddrBook, err error) {
  619. // TODO: detect more "bad peer" scenarios
  620. switch err.(type) {
  621. case p2p.ErrSwitchAuthenticationFailure:
  622. book.MarkBad(addr)
  623. default:
  624. book.MarkAttempt(addr)
  625. }
  626. }
  627. //-----------------------------------------------------------------------------
  628. // Messages
  629. // PexMessage is a primary type for PEX messages. Underneath, it could contain
  630. // either pexRequestMessage, or pexAddrsMessage messages.
  631. type PexMessage interface{}
  632. func RegisterPexMessage(cdc *amino.Codec) {
  633. cdc.RegisterInterface((*PexMessage)(nil), nil)
  634. cdc.RegisterConcrete(&pexRequestMessage{}, "tendermint/p2p/PexRequestMessage", nil)
  635. cdc.RegisterConcrete(&pexAddrsMessage{}, "tendermint/p2p/PexAddrsMessage", nil)
  636. }
  637. func decodeMsg(bz []byte) (msg PexMessage, err error) {
  638. if len(bz) > maxMsgSize {
  639. return msg, fmt.Errorf("Msg exceeds max size (%d > %d)", len(bz), maxMsgSize)
  640. }
  641. err = cdc.UnmarshalBinaryBare(bz, &msg)
  642. return
  643. }
  644. /*
  645. A pexRequestMessage requests additional peer addresses.
  646. */
  647. type pexRequestMessage struct {
  648. }
  649. func (m *pexRequestMessage) String() string {
  650. return "[pexRequest]"
  651. }
  652. /*
  653. A message with announced peer addresses.
  654. */
  655. type pexAddrsMessage struct {
  656. Addrs []*p2p.NetAddress
  657. }
  658. func (m *pexAddrsMessage) String() string {
  659. return fmt.Sprintf("[pexAddrs %v]", m.Addrs)
  660. }