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.

1039 lines
29 KiB

p2p: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
p2p: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
p2p: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
p2p: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
p2p: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
p2p: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
p2p: 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: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
p2p: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
p2p: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
p2p: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
p2p: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
p2p: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
p2p: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
  1. package p2p
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "math"
  7. mrand "math/rand"
  8. "net"
  9. "sync"
  10. "time"
  11. "github.com/tendermint/tendermint/config"
  12. "github.com/tendermint/tendermint/crypto"
  13. "github.com/tendermint/tendermint/internal/p2p/conn"
  14. "github.com/tendermint/tendermint/libs/cmap"
  15. tmrand "github.com/tendermint/tendermint/libs/rand"
  16. "github.com/tendermint/tendermint/libs/service"
  17. )
  18. const (
  19. // wait a random amount of time from this interval
  20. // before dialing peers or reconnecting to help prevent DoS
  21. dialRandomizerIntervalMilliseconds = 3000
  22. // repeatedly try to reconnect for a few minutes
  23. // ie. 5 * 20 = 100s
  24. reconnectAttempts = 20
  25. reconnectInterval = 5 * time.Second
  26. // then move into exponential backoff mode for ~1day
  27. // ie. 3**10 = 16hrs
  28. reconnectBackOffAttempts = 10
  29. reconnectBackOffBaseSeconds = 3
  30. defaultFilterTimeout = 5 * time.Second
  31. )
  32. // MConnConfig returns an MConnConfig with fields updated
  33. // from the P2PConfig.
  34. func MConnConfig(cfg *config.P2PConfig) conn.MConnConfig {
  35. mConfig := conn.DefaultMConnConfig()
  36. mConfig.FlushThrottle = cfg.FlushThrottleTimeout
  37. mConfig.SendRate = cfg.SendRate
  38. mConfig.RecvRate = cfg.RecvRate
  39. mConfig.MaxPacketMsgPayloadSize = cfg.MaxPacketMsgPayloadSize
  40. return mConfig
  41. }
  42. //-----------------------------------------------------------------------------
  43. // An AddrBook represents an address book from the pex package, which is used
  44. // to store peer addresses.
  45. type AddrBook interface {
  46. AddAddress(addr *NetAddress, src *NetAddress) error
  47. AddPrivateIDs([]string)
  48. AddOurAddress(*NetAddress)
  49. OurAddress(*NetAddress) bool
  50. MarkGood(NodeID)
  51. RemoveAddress(*NetAddress)
  52. HasAddress(*NetAddress) bool
  53. Save()
  54. }
  55. // ConnFilterFunc is a callback for connection filtering. If it returns an
  56. // error, the connection is rejected. The set of existing connections is passed
  57. // along with the new connection and all resolved IPs.
  58. type ConnFilterFunc func(ConnSet, net.Conn, []net.IP) error
  59. // PeerFilterFunc to be implemented by filter hooks after a new Peer has been
  60. // fully setup.
  61. type PeerFilterFunc func(IPeerSet, Peer) error
  62. // ConnDuplicateIPFilter resolves and keeps all ips for an incoming connection
  63. // and refuses new ones if they come from a known ip.
  64. var ConnDuplicateIPFilter ConnFilterFunc = func(cs ConnSet, c net.Conn, ips []net.IP) error {
  65. for _, ip := range ips {
  66. if cs.HasIP(ip) {
  67. return ErrRejected{
  68. conn: c,
  69. err: fmt.Errorf("ip<%v> already connected", ip),
  70. isDuplicate: true,
  71. }
  72. }
  73. }
  74. return nil
  75. }
  76. //-----------------------------------------------------------------------------
  77. // Switch handles peer connections and exposes an API to receive incoming messages
  78. // on `Reactors`. Each `Reactor` is responsible for handling incoming messages of one
  79. // or more `Channels`. So while sending outgoing messages is typically performed on the peer,
  80. // incoming messages are received on the reactor.
  81. type Switch struct {
  82. service.BaseService
  83. config *config.P2PConfig
  84. reactors map[string]Reactor
  85. chDescs []*conn.ChannelDescriptor
  86. reactorsByCh map[byte]Reactor
  87. peers *PeerSet
  88. dialing *cmap.CMap
  89. reconnecting *cmap.CMap
  90. nodeInfo NodeInfo // our node info
  91. nodeKey NodeKey // our node privkey
  92. addrBook AddrBook
  93. // peers addresses with whom we'll maintain constant connection
  94. persistentPeersAddrs []*NetAddress
  95. unconditionalPeerIDs map[NodeID]struct{}
  96. transport Transport
  97. filterTimeout time.Duration
  98. peerFilters []PeerFilterFunc
  99. connFilters []ConnFilterFunc
  100. conns ConnSet
  101. metrics *Metrics
  102. }
  103. // NetAddress returns the first address the switch is listening on,
  104. // or nil if no addresses are found.
  105. func (sw *Switch) NetAddress() *NetAddress {
  106. endpoints := sw.transport.Endpoints()
  107. if len(endpoints) == 0 {
  108. return nil
  109. }
  110. return &NetAddress{
  111. ID: sw.nodeInfo.NodeID,
  112. IP: endpoints[0].IP,
  113. Port: endpoints[0].Port,
  114. }
  115. }
  116. // SwitchOption sets an optional parameter on the Switch.
  117. type SwitchOption func(*Switch)
  118. // NewSwitch creates a new Switch with the given config.
  119. func NewSwitch(
  120. cfg *config.P2PConfig,
  121. transport Transport,
  122. options ...SwitchOption,
  123. ) *Switch {
  124. sw := &Switch{
  125. config: cfg,
  126. reactors: make(map[string]Reactor),
  127. chDescs: make([]*conn.ChannelDescriptor, 0),
  128. reactorsByCh: make(map[byte]Reactor),
  129. peers: NewPeerSet(),
  130. dialing: cmap.NewCMap(),
  131. reconnecting: cmap.NewCMap(),
  132. metrics: NopMetrics(),
  133. transport: transport,
  134. persistentPeersAddrs: make([]*NetAddress, 0),
  135. unconditionalPeerIDs: make(map[NodeID]struct{}),
  136. filterTimeout: defaultFilterTimeout,
  137. conns: NewConnSet(),
  138. }
  139. // Ensure PRNG is reseeded.
  140. tmrand.Reseed()
  141. sw.BaseService = *service.NewBaseService(nil, "P2P Switch", sw)
  142. for _, option := range options {
  143. option(sw)
  144. }
  145. return sw
  146. }
  147. // SwitchFilterTimeout sets the timeout used for peer filters.
  148. func SwitchFilterTimeout(timeout time.Duration) SwitchOption {
  149. return func(sw *Switch) { sw.filterTimeout = timeout }
  150. }
  151. // SwitchPeerFilters sets the filters for rejection of new peers.
  152. func SwitchPeerFilters(filters ...PeerFilterFunc) SwitchOption {
  153. return func(sw *Switch) { sw.peerFilters = filters }
  154. }
  155. // SwitchConnFilters sets the filters for rejection of connections.
  156. func SwitchConnFilters(filters ...ConnFilterFunc) SwitchOption {
  157. return func(sw *Switch) { sw.connFilters = filters }
  158. }
  159. // WithMetrics sets the metrics.
  160. func WithMetrics(metrics *Metrics) SwitchOption {
  161. return func(sw *Switch) { sw.metrics = metrics }
  162. }
  163. //---------------------------------------------------------------------
  164. // Switch setup
  165. // AddReactor adds the given reactor to the switch.
  166. // NOTE: Not goroutine safe.
  167. func (sw *Switch) AddReactor(name string, reactor Reactor) Reactor {
  168. for _, chDesc := range reactor.GetChannels() {
  169. chID := chDesc.ID
  170. // No two reactors can share the same channel.
  171. if sw.reactorsByCh[chID] != nil {
  172. panic(fmt.Sprintf("Channel %X has multiple reactors %v & %v", chID, sw.reactorsByCh[chID], reactor))
  173. }
  174. sw.chDescs = append(sw.chDescs, chDesc)
  175. sw.reactorsByCh[chID] = reactor
  176. }
  177. sw.reactors[name] = reactor
  178. reactor.SetSwitch(sw)
  179. return reactor
  180. }
  181. // RemoveReactor removes the given Reactor from the Switch.
  182. // NOTE: Not goroutine safe.
  183. func (sw *Switch) RemoveReactor(name string, reactor Reactor) {
  184. for _, chDesc := range reactor.GetChannels() {
  185. // remove channel description
  186. for i := 0; i < len(sw.chDescs); i++ {
  187. if chDesc.ID == sw.chDescs[i].ID {
  188. sw.chDescs = append(sw.chDescs[:i], sw.chDescs[i+1:]...)
  189. break
  190. }
  191. }
  192. delete(sw.reactorsByCh, chDesc.ID)
  193. }
  194. delete(sw.reactors, name)
  195. reactor.SetSwitch(nil)
  196. }
  197. // Reactors returns a map of reactors registered on the switch.
  198. // NOTE: Not goroutine safe.
  199. func (sw *Switch) Reactors() map[string]Reactor {
  200. return sw.reactors
  201. }
  202. // Reactor returns the reactor with the given name.
  203. // NOTE: Not goroutine safe.
  204. func (sw *Switch) Reactor(name string) Reactor {
  205. return sw.reactors[name]
  206. }
  207. // SetNodeInfo sets the switch's NodeInfo for checking compatibility and handshaking with other nodes.
  208. // NOTE: Not goroutine safe.
  209. func (sw *Switch) SetNodeInfo(nodeInfo NodeInfo) {
  210. sw.nodeInfo = nodeInfo
  211. }
  212. // NodeInfo returns the switch's NodeInfo.
  213. // NOTE: Not goroutine safe.
  214. func (sw *Switch) NodeInfo() NodeInfo {
  215. return sw.nodeInfo
  216. }
  217. // SetNodeKey sets the switch's private key for authenticated encryption.
  218. // NOTE: Not goroutine safe.
  219. func (sw *Switch) SetNodeKey(nodeKey NodeKey) {
  220. sw.nodeKey = nodeKey
  221. }
  222. //---------------------------------------------------------------------
  223. // Service start/stop
  224. // OnStart implements BaseService. It starts all the reactors and peers.
  225. func (sw *Switch) OnStart() error {
  226. // FIXME: Temporary hack to pass channel descriptors to MConn transport,
  227. // since they are not available when it is constructed. This will be
  228. // fixed when we implement the new router abstraction.
  229. if t, ok := sw.transport.(*MConnTransport); ok {
  230. t.channelDescs = sw.chDescs
  231. }
  232. // Start reactors
  233. for _, reactor := range sw.reactors {
  234. err := reactor.Start()
  235. if err != nil {
  236. return fmt.Errorf("failed to start %v: %w", reactor, err)
  237. }
  238. }
  239. // Start accepting Peers.
  240. go sw.acceptRoutine()
  241. return nil
  242. }
  243. // OnStop implements BaseService. It stops all peers and reactors.
  244. func (sw *Switch) OnStop() {
  245. // Stop peers
  246. for _, p := range sw.peers.List() {
  247. sw.stopAndRemovePeer(p, nil)
  248. }
  249. // Stop reactors
  250. sw.Logger.Debug("Switch: Stopping reactors")
  251. for _, reactor := range sw.reactors {
  252. if err := reactor.Stop(); err != nil {
  253. sw.Logger.Error("error while stopping reactor", "reactor", reactor, "error", err)
  254. }
  255. }
  256. }
  257. //---------------------------------------------------------------------
  258. // Peers
  259. // Broadcast runs a go routine for each attempted send, which will block trying
  260. // to send for defaultSendTimeoutSeconds. Returns a channel which receives
  261. // success values for each attempted send (false if times out). Channel will be
  262. // closed once msg bytes are sent to all peers (or time out).
  263. //
  264. // NOTE: Broadcast uses goroutines, so order of broadcast may not be preserved.
  265. func (sw *Switch) Broadcast(chID byte, msgBytes []byte) chan bool {
  266. sw.Logger.Debug("Broadcast", "channel", chID, "msgBytes", msgBytes)
  267. peers := sw.peers.List()
  268. var wg sync.WaitGroup
  269. wg.Add(len(peers))
  270. successChan := make(chan bool, len(peers))
  271. for _, peer := range peers {
  272. go func(p Peer) {
  273. defer wg.Done()
  274. success := p.Send(chID, msgBytes)
  275. successChan <- success
  276. }(peer)
  277. }
  278. go func() {
  279. wg.Wait()
  280. close(successChan)
  281. }()
  282. return successChan
  283. }
  284. // NumPeers returns the count of outbound/inbound and outbound-dialing peers.
  285. // unconditional peers are not counted here.
  286. func (sw *Switch) NumPeers() (outbound, inbound, dialing int) {
  287. peers := sw.peers.List()
  288. for _, peer := range peers {
  289. if peer.IsOutbound() {
  290. if !sw.IsPeerUnconditional(peer.ID()) {
  291. outbound++
  292. }
  293. } else {
  294. if !sw.IsPeerUnconditional(peer.ID()) {
  295. inbound++
  296. }
  297. }
  298. }
  299. dialing = sw.dialing.Size()
  300. return
  301. }
  302. func (sw *Switch) IsPeerUnconditional(id NodeID) bool {
  303. _, ok := sw.unconditionalPeerIDs[id]
  304. return ok
  305. }
  306. // MaxNumOutboundPeers returns a maximum number of outbound peers.
  307. func (sw *Switch) MaxNumOutboundPeers() int {
  308. return sw.config.MaxNumOutboundPeers
  309. }
  310. // Peers returns the set of peers that are connected to the switch.
  311. func (sw *Switch) Peers() IPeerSet {
  312. return sw.peers
  313. }
  314. // StopPeerForError disconnects from a peer due to external error.
  315. // If the peer is persistent, it will attempt to reconnect.
  316. // TODO: make record depending on reason.
  317. func (sw *Switch) StopPeerForError(peer Peer, reason interface{}) {
  318. if !peer.IsRunning() {
  319. return
  320. }
  321. sw.Logger.Error("Stopping peer for error", "peer", peer, "err", reason)
  322. sw.stopAndRemovePeer(peer, reason)
  323. if peer.IsPersistent() {
  324. var addr *NetAddress
  325. if peer.IsOutbound() { // socket address for outbound peers
  326. addr = peer.SocketAddr()
  327. } else { // self-reported address for inbound peers
  328. var err error
  329. addr, err = peer.NodeInfo().NetAddress()
  330. if err != nil {
  331. sw.Logger.Error("Wanted to reconnect to inbound peer, but self-reported address is wrong",
  332. "peer", peer, "err", err)
  333. return
  334. }
  335. }
  336. go sw.reconnectToPeer(addr)
  337. }
  338. }
  339. // StopPeerGracefully disconnects from a peer gracefully.
  340. // TODO: handle graceful disconnects.
  341. func (sw *Switch) StopPeerGracefully(peer Peer) {
  342. sw.Logger.Info("Stopping peer gracefully")
  343. sw.stopAndRemovePeer(peer, nil)
  344. }
  345. func (sw *Switch) stopAndRemovePeer(peer Peer, reason interface{}) {
  346. if err := peer.Stop(); err != nil {
  347. sw.Logger.Error("error while stopping peer", "error", err) // TODO: should return error to be handled accordingly
  348. }
  349. for _, reactor := range sw.reactors {
  350. reactor.RemovePeer(peer, reason)
  351. }
  352. // Removing a peer should go last to avoid a situation where a peer
  353. // reconnect to our node and the switch calls InitPeer before
  354. // RemovePeer is finished.
  355. // https://github.com/tendermint/tendermint/issues/3338
  356. if sw.peers.Remove(peer) {
  357. sw.metrics.Peers.Add(float64(-1))
  358. }
  359. sw.conns.RemoveAddr(peer.RemoteAddr())
  360. }
  361. // reconnectToPeer tries to reconnect to the addr, first repeatedly
  362. // with a fixed interval, then with exponential backoff.
  363. // If no success after all that, it stops trying, and leaves it
  364. // to the PEX/Addrbook to find the peer with the addr again
  365. // NOTE: this will keep trying even if the handshake or auth fails.
  366. // TODO: be more explicit with error types so we only retry on certain failures
  367. // - ie. if we're getting ErrDuplicatePeer we can stop
  368. // because the addrbook got us the peer back already
  369. func (sw *Switch) reconnectToPeer(addr *NetAddress) {
  370. if sw.reconnecting.Has(string(addr.ID)) {
  371. return
  372. }
  373. sw.reconnecting.Set(string(addr.ID), addr)
  374. defer sw.reconnecting.Delete(string(addr.ID))
  375. start := time.Now()
  376. sw.Logger.Info("Reconnecting to peer", "addr", addr)
  377. for i := 0; i < reconnectAttempts; i++ {
  378. if !sw.IsRunning() {
  379. return
  380. }
  381. err := sw.DialPeerWithAddress(addr)
  382. if err == nil {
  383. return // success
  384. } else if _, ok := err.(ErrCurrentlyDialingOrExistingAddress); ok {
  385. return
  386. }
  387. sw.Logger.Info("Error reconnecting to peer. Trying again", "tries", i, "err", err, "addr", addr)
  388. // sleep a set amount
  389. sw.randomSleep(reconnectInterval)
  390. continue
  391. }
  392. sw.Logger.Error("Failed to reconnect to peer. Beginning exponential backoff",
  393. "addr", addr, "elapsed", time.Since(start))
  394. for i := 0; i < reconnectBackOffAttempts; i++ {
  395. if !sw.IsRunning() {
  396. return
  397. }
  398. // sleep an exponentially increasing amount
  399. sleepIntervalSeconds := math.Pow(reconnectBackOffBaseSeconds, float64(i))
  400. sw.randomSleep(time.Duration(sleepIntervalSeconds) * time.Second)
  401. err := sw.DialPeerWithAddress(addr)
  402. if err == nil {
  403. return // success
  404. } else if _, ok := err.(ErrCurrentlyDialingOrExistingAddress); ok {
  405. return
  406. }
  407. sw.Logger.Info("Error reconnecting to peer. Trying again", "tries", i, "err", err, "addr", addr)
  408. }
  409. sw.Logger.Error("Failed to reconnect to peer. Giving up", "addr", addr, "elapsed", time.Since(start))
  410. }
  411. // SetAddrBook allows to set address book on Switch.
  412. func (sw *Switch) SetAddrBook(addrBook AddrBook) {
  413. sw.addrBook = addrBook
  414. }
  415. // MarkPeerAsGood marks the given peer as good when it did something useful
  416. // like contributed to consensus.
  417. func (sw *Switch) MarkPeerAsGood(peer Peer) {
  418. if sw.addrBook != nil {
  419. sw.addrBook.MarkGood(peer.ID())
  420. }
  421. }
  422. //---------------------------------------------------------------------
  423. // Dialing
  424. type privateAddr interface {
  425. PrivateAddr() bool
  426. }
  427. func isPrivateAddr(err error) bool {
  428. te, ok := err.(privateAddr)
  429. return ok && te.PrivateAddr()
  430. }
  431. // DialPeersAsync dials a list of peers asynchronously in random order.
  432. // Used to dial peers from config on startup or from unsafe-RPC (trusted sources).
  433. // It ignores ErrNetAddressLookup. However, if there are other errors, first
  434. // encounter is returned.
  435. // Nop if there are no peers.
  436. func (sw *Switch) DialPeersAsync(peers []string) error {
  437. netAddrs, errs := NewNetAddressStrings(peers)
  438. // report all the errors
  439. for _, err := range errs {
  440. sw.Logger.Error("Error in peer's address", "err", err)
  441. }
  442. // return first non-ErrNetAddressLookup error
  443. for _, err := range errs {
  444. if _, ok := err.(ErrNetAddressLookup); ok {
  445. continue
  446. }
  447. return err
  448. }
  449. sw.dialPeersAsync(netAddrs)
  450. return nil
  451. }
  452. func (sw *Switch) dialPeersAsync(netAddrs []*NetAddress) {
  453. ourAddr := sw.NetAddress()
  454. // TODO: this code feels like it's in the wrong place.
  455. // The integration tests depend on the addrBook being saved
  456. // right away but maybe we can change that. Recall that
  457. // the addrBook is only written to disk every 2min
  458. if sw.addrBook != nil {
  459. // add peers to `addrBook`
  460. for _, netAddr := range netAddrs {
  461. // do not add our address or ID
  462. if !netAddr.Same(ourAddr) {
  463. if err := sw.addrBook.AddAddress(netAddr, ourAddr); err != nil {
  464. if isPrivateAddr(err) {
  465. sw.Logger.Debug("Won't add peer's address to addrbook", "err", err)
  466. } else {
  467. sw.Logger.Error("Can't add peer's address to addrbook", "err", err)
  468. }
  469. }
  470. }
  471. }
  472. // Persist some peers to disk right away.
  473. // NOTE: integration tests depend on this
  474. sw.addrBook.Save()
  475. }
  476. // permute the list, dial them in random order.
  477. perm := mrand.Perm(len(netAddrs))
  478. for i := 0; i < len(perm); i++ {
  479. go func(i int) {
  480. j := perm[i]
  481. addr := netAddrs[j]
  482. if addr.Same(ourAddr) {
  483. sw.Logger.Debug("Ignore attempt to connect to ourselves", "addr", addr, "ourAddr", ourAddr)
  484. return
  485. }
  486. sw.randomSleep(0)
  487. err := sw.DialPeerWithAddress(addr)
  488. if err != nil {
  489. switch err.(type) {
  490. case ErrSwitchConnectToSelf, ErrSwitchDuplicatePeerID, ErrCurrentlyDialingOrExistingAddress:
  491. sw.Logger.Debug("Error dialing peer", "err", err)
  492. default:
  493. sw.Logger.Error("Error dialing peer", "err", err)
  494. }
  495. }
  496. }(i)
  497. }
  498. }
  499. // DialPeerWithAddress dials the given peer and runs sw.addPeer if it connects
  500. // and authenticates successfully.
  501. // If we're currently dialing this address or it belongs to an existing peer,
  502. // ErrCurrentlyDialingOrExistingAddress is returned.
  503. func (sw *Switch) DialPeerWithAddress(addr *NetAddress) error {
  504. if sw.IsDialingOrExistingAddress(addr) {
  505. return ErrCurrentlyDialingOrExistingAddress{addr.String()}
  506. }
  507. sw.dialing.Set(string(addr.ID), addr)
  508. defer sw.dialing.Delete(string(addr.ID))
  509. return sw.addOutboundPeerWithConfig(addr, sw.config)
  510. }
  511. // sleep for interval plus some random amount of ms on [0, dialRandomizerIntervalMilliseconds]
  512. func (sw *Switch) randomSleep(interval time.Duration) {
  513. // nolint:gosec // G404: Use of weak random number generator
  514. r := time.Duration(mrand.Int63n(dialRandomizerIntervalMilliseconds)) * time.Millisecond
  515. time.Sleep(r + interval)
  516. }
  517. // IsDialingOrExistingAddress returns true if switch has a peer with the given
  518. // address or dialing it at the moment.
  519. func (sw *Switch) IsDialingOrExistingAddress(addr *NetAddress) bool {
  520. return sw.dialing.Has(string(addr.ID)) ||
  521. sw.peers.Has(addr.ID) ||
  522. (!sw.config.AllowDuplicateIP && sw.peers.HasIP(addr.IP))
  523. }
  524. // AddPersistentPeers allows you to set persistent peers. It ignores
  525. // ErrNetAddressLookup. However, if there are other errors, first encounter is
  526. // returned.
  527. func (sw *Switch) AddPersistentPeers(addrs []string) error {
  528. sw.Logger.Info("Adding persistent peers", "addrs", addrs)
  529. netAddrs, errs := NewNetAddressStrings(addrs)
  530. // report all the errors
  531. for _, err := range errs {
  532. sw.Logger.Error("Error in peer's address", "err", err)
  533. }
  534. // return first non-ErrNetAddressLookup error
  535. for _, err := range errs {
  536. if _, ok := err.(ErrNetAddressLookup); ok {
  537. continue
  538. }
  539. return err
  540. }
  541. sw.persistentPeersAddrs = netAddrs
  542. return nil
  543. }
  544. func (sw *Switch) AddUnconditionalPeerIDs(ids []string) error {
  545. sw.Logger.Info("Adding unconditional peer ids", "ids", ids)
  546. for i, id := range ids {
  547. err := NodeID(id).Validate()
  548. if err != nil {
  549. return fmt.Errorf("wrong ID #%d: %w", i, err)
  550. }
  551. sw.unconditionalPeerIDs[NodeID(id)] = struct{}{}
  552. }
  553. return nil
  554. }
  555. func (sw *Switch) AddPrivatePeerIDs(ids []string) error {
  556. validIDs := make([]string, 0, len(ids))
  557. for i, id := range ids {
  558. err := NodeID(id).Validate()
  559. if err != nil {
  560. return fmt.Errorf("wrong ID #%d: %w", i, err)
  561. }
  562. validIDs = append(validIDs, id)
  563. }
  564. sw.addrBook.AddPrivateIDs(validIDs)
  565. return nil
  566. }
  567. func (sw *Switch) IsPeerPersistent(na *NetAddress) bool {
  568. for _, pa := range sw.persistentPeersAddrs {
  569. if pa.Equals(na) {
  570. return true
  571. }
  572. }
  573. return false
  574. }
  575. func (sw *Switch) acceptRoutine() {
  576. for {
  577. var peerNodeInfo NodeInfo
  578. c, err := sw.transport.Accept()
  579. if err == nil {
  580. // NOTE: The legacy MConn transport did handshaking in Accept(),
  581. // which was asynchronous and avoided head-of-line-blocking.
  582. // However, as handshakes are being migrated out from the transport,
  583. // we just do it synchronously here for now.
  584. peerNodeInfo, _, err = sw.handshakePeer(c, "")
  585. }
  586. if err == nil {
  587. err = sw.filterConn(c.(*mConnConnection).conn)
  588. }
  589. if err != nil {
  590. if c != nil {
  591. _ = c.Close()
  592. }
  593. if err == io.EOF {
  594. err = ErrTransportClosed{}
  595. }
  596. switch err := err.(type) {
  597. case ErrRejected:
  598. if err.IsSelf() {
  599. // Remove the given address from the address book and add to our addresses
  600. // to avoid dialing in the future.
  601. addr := err.Addr()
  602. sw.addrBook.RemoveAddress(&addr)
  603. sw.addrBook.AddOurAddress(&addr)
  604. }
  605. sw.Logger.Info(
  606. "Inbound Peer rejected",
  607. "err", err,
  608. "numPeers", sw.peers.Size(),
  609. )
  610. continue
  611. case ErrFilterTimeout:
  612. sw.Logger.Error(
  613. "Peer filter timed out",
  614. "err", err,
  615. )
  616. continue
  617. case ErrTransportClosed:
  618. sw.Logger.Error(
  619. "Stopped accept routine, as transport is closed",
  620. "numPeers", sw.peers.Size(),
  621. )
  622. default:
  623. sw.Logger.Error(
  624. "Accept on transport errored",
  625. "err", err,
  626. "numPeers", sw.peers.Size(),
  627. )
  628. // We could instead have a retry loop around the acceptRoutine,
  629. // but that would need to stop and let the node shutdown eventually.
  630. // So might as well panic and let process managers restart the node.
  631. // There's no point in letting the node run without the acceptRoutine,
  632. // since it won't be able to accept new connections.
  633. panic(fmt.Errorf("accept routine exited: %v", err))
  634. }
  635. break
  636. }
  637. isPersistent := false
  638. addr, err := peerNodeInfo.NetAddress()
  639. if err == nil {
  640. isPersistent = sw.IsPeerPersistent(addr)
  641. }
  642. p := newPeer(
  643. peerNodeInfo,
  644. newPeerConn(false, isPersistent, c),
  645. sw.reactorsByCh,
  646. sw.StopPeerForError,
  647. PeerMetrics(sw.metrics),
  648. )
  649. if !sw.IsPeerUnconditional(p.NodeInfo().ID()) {
  650. // Ignore connection if we already have enough peers.
  651. _, in, _ := sw.NumPeers()
  652. if in >= sw.config.MaxNumInboundPeers {
  653. sw.Logger.Info(
  654. "Ignoring inbound connection: already have enough inbound peers",
  655. "address", p.SocketAddr(),
  656. "have", in,
  657. "max", sw.config.MaxNumInboundPeers,
  658. )
  659. _ = p.CloseConn()
  660. continue
  661. }
  662. }
  663. if err := sw.addPeer(p); err != nil {
  664. _ = p.CloseConn()
  665. if p.IsRunning() {
  666. _ = p.Stop()
  667. }
  668. sw.conns.RemoveAddr(p.RemoteAddr())
  669. sw.Logger.Info(
  670. "Ignoring inbound connection: error while adding peer",
  671. "err", err,
  672. "id", p.ID(),
  673. )
  674. }
  675. }
  676. }
  677. // dial the peer; make secret connection; authenticate against the dialed ID;
  678. // add the peer.
  679. // if dialing fails, start the reconnect loop. If handshake fails, it's over.
  680. // If peer is started successfully, reconnectLoop will start when
  681. // StopPeerForError is called.
  682. func (sw *Switch) addOutboundPeerWithConfig(
  683. addr *NetAddress,
  684. cfg *config.P2PConfig,
  685. ) error {
  686. sw.Logger.Info("Dialing peer", "address", addr)
  687. // XXX(xla): Remove the leakage of test concerns in implementation.
  688. if cfg.TestDialFail {
  689. go sw.reconnectToPeer(addr)
  690. return fmt.Errorf("dial err (peerConfig.DialFail == true)")
  691. }
  692. // Hardcoded timeout moved from MConn transport during refactoring.
  693. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  694. defer cancel()
  695. var peerNodeInfo NodeInfo
  696. c, err := sw.transport.Dial(ctx, Endpoint{
  697. Protocol: MConnProtocol,
  698. IP: addr.IP,
  699. Port: addr.Port,
  700. })
  701. if err == nil {
  702. peerNodeInfo, _, err = sw.handshakePeer(c, addr.ID)
  703. }
  704. if err == nil {
  705. err = sw.filterConn(c.(*mConnConnection).conn)
  706. }
  707. if err != nil {
  708. if c != nil {
  709. _ = c.Close()
  710. }
  711. if e, ok := err.(ErrRejected); ok {
  712. if e.IsSelf() {
  713. // Remove the given address from the address book and add to our addresses
  714. // to avoid dialing in the future.
  715. sw.addrBook.RemoveAddress(addr)
  716. sw.addrBook.AddOurAddress(addr)
  717. return err
  718. }
  719. }
  720. // retry persistent peers after
  721. // any dial error besides IsSelf()
  722. if sw.IsPeerPersistent(addr) {
  723. go sw.reconnectToPeer(addr)
  724. }
  725. return err
  726. }
  727. p := newPeer(
  728. peerNodeInfo,
  729. newPeerConn(true, sw.IsPeerPersistent(addr), c),
  730. sw.reactorsByCh,
  731. sw.StopPeerForError,
  732. PeerMetrics(sw.metrics),
  733. )
  734. if err := sw.addPeer(p); err != nil {
  735. _ = p.CloseConn()
  736. if p.IsRunning() {
  737. _ = p.Stop()
  738. }
  739. sw.conns.RemoveAddr(p.RemoteAddr())
  740. return err
  741. }
  742. return nil
  743. }
  744. func (sw *Switch) handshakePeer(c Connection, expectPeerID NodeID) (NodeInfo, crypto.PubKey, error) {
  745. // Moved from transport and hardcoded until legacy P2P stack removal.
  746. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  747. defer cancel()
  748. peerInfo, peerKey, err := c.Handshake(ctx, sw.nodeInfo, sw.nodeKey.PrivKey)
  749. if err != nil {
  750. return peerInfo, peerKey, ErrRejected{
  751. conn: c.(*mConnConnection).conn,
  752. err: fmt.Errorf("handshake failed: %v", err),
  753. isAuthFailure: true,
  754. }
  755. }
  756. if err = peerInfo.Validate(); err != nil {
  757. return peerInfo, peerKey, ErrRejected{
  758. conn: c.(*mConnConnection).conn,
  759. err: err,
  760. isNodeInfoInvalid: true,
  761. }
  762. }
  763. // For outgoing conns, ensure connection key matches dialed key.
  764. if expectPeerID != "" {
  765. peerID := NodeIDFromPubKey(peerKey)
  766. if expectPeerID != peerID {
  767. return peerInfo, peerKey, ErrRejected{
  768. conn: c.(*mConnConnection).conn,
  769. id: peerID,
  770. err: fmt.Errorf(
  771. "conn.ID (%v) dialed ID (%v) mismatch",
  772. peerID,
  773. expectPeerID,
  774. ),
  775. isAuthFailure: true,
  776. }
  777. }
  778. }
  779. if sw.nodeInfo.ID() == peerInfo.ID() {
  780. return peerInfo, peerKey, ErrRejected{
  781. addr: *NewNetAddress(peerInfo.ID(), c.(*mConnConnection).conn.RemoteAddr()),
  782. conn: c.(*mConnConnection).conn,
  783. id: peerInfo.ID(),
  784. isSelf: true,
  785. }
  786. }
  787. if err = sw.nodeInfo.CompatibleWith(peerInfo); err != nil {
  788. return peerInfo, peerKey, ErrRejected{
  789. conn: c.(*mConnConnection).conn,
  790. err: err,
  791. id: peerInfo.ID(),
  792. isIncompatible: true,
  793. }
  794. }
  795. return peerInfo, peerKey, nil
  796. }
  797. func (sw *Switch) filterPeer(p Peer) error {
  798. // Avoid duplicate
  799. if sw.peers.Has(p.ID()) {
  800. return ErrRejected{id: p.ID(), isDuplicate: true}
  801. }
  802. errc := make(chan error, len(sw.peerFilters))
  803. for _, f := range sw.peerFilters {
  804. go func(f PeerFilterFunc, p Peer, errc chan<- error) {
  805. errc <- f(sw.peers, p)
  806. }(f, p, errc)
  807. }
  808. for i := 0; i < cap(errc); i++ {
  809. select {
  810. case err := <-errc:
  811. if err != nil {
  812. return ErrRejected{id: p.ID(), err: err, isFiltered: true}
  813. }
  814. case <-time.After(sw.filterTimeout):
  815. return ErrFilterTimeout{}
  816. }
  817. }
  818. return nil
  819. }
  820. // filterConn filters a connection, rejecting it if this function errors.
  821. //
  822. // FIXME: This is only here for compatibility with the current Switch code. In
  823. // the new P2P stack, peer/connection filtering should be moved into the Router
  824. // or PeerManager and removed from here.
  825. func (sw *Switch) filterConn(conn net.Conn) error {
  826. if sw.conns.Has(conn) {
  827. return ErrRejected{conn: conn, isDuplicate: true}
  828. }
  829. host, _, err := net.SplitHostPort(conn.RemoteAddr().String())
  830. if err != nil {
  831. return err
  832. }
  833. ip := net.ParseIP(host)
  834. if ip == nil {
  835. return fmt.Errorf("connection address has invalid IP address %q", host)
  836. }
  837. // Apply filter callbacks.
  838. chErr := make(chan error, len(sw.connFilters))
  839. for _, connFilter := range sw.connFilters {
  840. go func(connFilter ConnFilterFunc) {
  841. chErr <- connFilter(sw.conns, conn, []net.IP{ip})
  842. }(connFilter)
  843. }
  844. for i := 0; i < cap(chErr); i++ {
  845. select {
  846. case err := <-chErr:
  847. if err != nil {
  848. return ErrRejected{conn: conn, err: err, isFiltered: true}
  849. }
  850. case <-time.After(sw.filterTimeout):
  851. return ErrFilterTimeout{}
  852. }
  853. }
  854. // FIXME: Doesn't really make sense to set this here, but we preserve the
  855. // behavior from the previous P2P transport implementation.
  856. sw.conns.Set(conn, []net.IP{ip})
  857. return nil
  858. }
  859. // addPeer starts up the Peer and adds it to the Switch. Error is returned if
  860. // the peer is filtered out or failed to start or can't be added.
  861. func (sw *Switch) addPeer(p Peer) error {
  862. if err := sw.filterPeer(p); err != nil {
  863. return err
  864. }
  865. p.SetLogger(sw.Logger.With("peer", p.SocketAddr()))
  866. // Handle the shut down case where the switch has stopped but we're
  867. // concurrently trying to add a peer.
  868. if !sw.IsRunning() {
  869. // XXX should this return an error or just log and terminate?
  870. sw.Logger.Error("Won't start a peer - switch is not running", "peer", p)
  871. return nil
  872. }
  873. // Add some data to the peer, which is required by reactors.
  874. for _, reactor := range sw.reactors {
  875. p = reactor.InitPeer(p)
  876. }
  877. // Start the peer's send/recv routines.
  878. // Must start it before adding it to the peer set
  879. // to prevent Start and Stop from being called concurrently.
  880. err := p.Start()
  881. if err != nil {
  882. // Should never happen
  883. sw.Logger.Error("Error starting peer", "err", err, "peer", p)
  884. return err
  885. }
  886. // Add the peer to PeerSet. Do this before starting the reactors
  887. // so that if Receive errors, we will find the peer and remove it.
  888. // Add should not err since we already checked peers.Has().
  889. if err := sw.peers.Add(p); err != nil {
  890. return err
  891. }
  892. sw.metrics.Peers.Add(float64(1))
  893. // Start all the reactor protocols on the peer.
  894. for _, reactor := range sw.reactors {
  895. reactor.AddPeer(p)
  896. }
  897. sw.Logger.Info("Added peer", "peer", p)
  898. return nil
  899. }