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.

1047 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
5 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
5 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
5 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
5 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
5 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
5 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
5 years ago
p2p: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
p2p: 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. "net"
  8. "sync"
  9. "time"
  10. "github.com/tendermint/tendermint/config"
  11. "github.com/tendermint/tendermint/crypto"
  12. "github.com/tendermint/tendermint/libs/cmap"
  13. "github.com/tendermint/tendermint/libs/rand"
  14. "github.com/tendermint/tendermint/libs/service"
  15. "github.com/tendermint/tendermint/p2p/conn"
  16. )
  17. const (
  18. // wait a random amount of time from this interval
  19. // before dialing peers or reconnecting to help prevent DoS
  20. dialRandomizerIntervalMilliseconds = 3000
  21. // repeatedly try to reconnect for a few minutes
  22. // ie. 5 * 20 = 100s
  23. reconnectAttempts = 20
  24. reconnectInterval = 5 * time.Second
  25. // then move into exponential backoff mode for ~1day
  26. // ie. 3**10 = 16hrs
  27. reconnectBackOffAttempts = 10
  28. reconnectBackOffBaseSeconds = 3
  29. defaultFilterTimeout = 5 * time.Second
  30. )
  31. // MConnConfig returns an MConnConfig with fields updated
  32. // from the P2PConfig.
  33. func MConnConfig(cfg *config.P2PConfig) conn.MConnConfig {
  34. mConfig := conn.DefaultMConnConfig()
  35. mConfig.FlushThrottle = cfg.FlushThrottleTimeout
  36. mConfig.SendRate = cfg.SendRate
  37. mConfig.RecvRate = cfg.RecvRate
  38. mConfig.MaxPacketMsgPayloadSize = cfg.MaxPacketMsgPayloadSize
  39. return mConfig
  40. }
  41. //-----------------------------------------------------------------------------
  42. // An AddrBook represents an address book from the pex package, which is used
  43. // to store peer addresses.
  44. type AddrBook interface {
  45. AddAddress(addr *NetAddress, src *NetAddress) error
  46. AddPrivateIDs([]string)
  47. AddOurAddress(*NetAddress)
  48. OurAddress(*NetAddress) bool
  49. MarkGood(NodeID)
  50. RemoveAddress(*NetAddress)
  51. HasAddress(*NetAddress) bool
  52. Save()
  53. }
  54. // ConnFilterFunc is a callback for connection filtering. If it returns an
  55. // error, the connection is rejected. The set of existing connections is passed
  56. // along with the new connection and all resolved IPs.
  57. type ConnFilterFunc func(ConnSet, net.Conn, []net.IP) error
  58. // PeerFilterFunc to be implemented by filter hooks after a new Peer has been
  59. // fully setup.
  60. type PeerFilterFunc func(IPeerSet, Peer) error
  61. // ConnDuplicateIPFilter resolves and keeps all ips for an incoming connection
  62. // and refuses new ones if they come from a known ip.
  63. var ConnDuplicateIPFilter ConnFilterFunc = func(cs ConnSet, c net.Conn, ips []net.IP) error {
  64. for _, ip := range ips {
  65. if cs.HasIP(ip) {
  66. return ErrRejected{
  67. conn: c,
  68. err: fmt.Errorf("ip<%v> already connected", ip),
  69. isDuplicate: true,
  70. }
  71. }
  72. }
  73. return nil
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Switch handles peer connections and exposes an API to receive incoming messages
  77. // on `Reactors`. Each `Reactor` is responsible for handling incoming messages of one
  78. // or more `Channels`. So while sending outgoing messages is typically performed on the peer,
  79. // incoming messages are received on the reactor.
  80. type Switch struct {
  81. service.BaseService
  82. config *config.P2PConfig
  83. reactors map[string]Reactor
  84. chDescs []*conn.ChannelDescriptor
  85. reactorsByCh map[byte]Reactor
  86. peers *PeerSet
  87. dialing *cmap.CMap
  88. reconnecting *cmap.CMap
  89. nodeInfo NodeInfo // our node info
  90. nodeKey NodeKey // our node privkey
  91. addrBook AddrBook
  92. // peers addresses with whom we'll maintain constant connection
  93. persistentPeersAddrs []*NetAddress
  94. unconditionalPeerIDs map[NodeID]struct{}
  95. transport Transport
  96. filterTimeout time.Duration
  97. peerFilters []PeerFilterFunc
  98. connFilters []ConnFilterFunc
  99. conns ConnSet
  100. rng *rand.Rand // seed for randomizing dial times and orders
  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 we have a completely undeterministic PRNG.
  140. sw.rng = rand.NewRand()
  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 := sw.rng.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. r := time.Duration(sw.rng.Int63n(dialRandomizerIntervalMilliseconds)) * time.Millisecond
  514. time.Sleep(r + interval)
  515. }
  516. // IsDialingOrExistingAddress returns true if switch has a peer with the given
  517. // address or dialing it at the moment.
  518. func (sw *Switch) IsDialingOrExistingAddress(addr *NetAddress) bool {
  519. return sw.dialing.Has(string(addr.ID)) ||
  520. sw.peers.Has(addr.ID) ||
  521. (!sw.config.AllowDuplicateIP && sw.peers.HasIP(addr.IP))
  522. }
  523. // AddPersistentPeers allows you to set persistent peers. It ignores
  524. // ErrNetAddressLookup. However, if there are other errors, first encounter is
  525. // returned.
  526. func (sw *Switch) AddPersistentPeers(addrs []string) error {
  527. sw.Logger.Info("Adding persistent peers", "addrs", addrs)
  528. netAddrs, errs := NewNetAddressStrings(addrs)
  529. // report all the errors
  530. for _, err := range errs {
  531. sw.Logger.Error("Error in peer's address", "err", err)
  532. }
  533. // return first non-ErrNetAddressLookup error
  534. for _, err := range errs {
  535. if _, ok := err.(ErrNetAddressLookup); ok {
  536. continue
  537. }
  538. return err
  539. }
  540. sw.persistentPeersAddrs = netAddrs
  541. return nil
  542. }
  543. func (sw *Switch) AddUnconditionalPeerIDs(ids []string) error {
  544. sw.Logger.Info("Adding unconditional peer ids", "ids", ids)
  545. for i, id := range ids {
  546. err := NodeID(id).Validate()
  547. if err != nil {
  548. return fmt.Errorf("wrong ID #%d: %w", i, err)
  549. }
  550. sw.unconditionalPeerIDs[NodeID(id)] = struct{}{}
  551. }
  552. return nil
  553. }
  554. func (sw *Switch) AddPrivatePeerIDs(ids []string) error {
  555. validIDs := make([]string, 0, len(ids))
  556. for i, id := range ids {
  557. err := NodeID(id).Validate()
  558. if err != nil {
  559. return fmt.Errorf("wrong ID #%d: %w", i, err)
  560. }
  561. validIDs = append(validIDs, id)
  562. }
  563. sw.addrBook.AddPrivateIDs(validIDs)
  564. return nil
  565. }
  566. func (sw *Switch) IsPeerPersistent(na *NetAddress) bool {
  567. for _, pa := range sw.persistentPeersAddrs {
  568. if pa.Equals(na) {
  569. return true
  570. }
  571. }
  572. return false
  573. }
  574. func (sw *Switch) acceptRoutine() {
  575. for {
  576. var peerNodeInfo NodeInfo
  577. c, err := sw.transport.Accept()
  578. if err == nil {
  579. // NOTE: The legacy MConn transport did handshaking in Accept(),
  580. // which was asynchronous and avoided head-of-line-blocking.
  581. // However, as handshakes are being migrated out from the transport,
  582. // we just do it synchronously here for now.
  583. peerNodeInfo, _, err = sw.handshakePeer(c, "")
  584. }
  585. if err == nil {
  586. err = sw.filterConn(c.(*mConnConnection).conn)
  587. }
  588. if err != nil {
  589. if c != nil {
  590. _ = c.Close()
  591. }
  592. if err == io.EOF {
  593. err = ErrTransportClosed{}
  594. }
  595. switch err := err.(type) {
  596. case ErrRejected:
  597. if err.IsSelf() {
  598. // Remove the given address from the address book and add to our addresses
  599. // to avoid dialing in the future.
  600. addr := err.Addr()
  601. sw.addrBook.RemoveAddress(&addr)
  602. sw.addrBook.AddOurAddress(&addr)
  603. }
  604. sw.Logger.Info(
  605. "Inbound Peer rejected",
  606. "err", err,
  607. "numPeers", sw.peers.Size(),
  608. )
  609. continue
  610. case ErrFilterTimeout:
  611. sw.Logger.Error(
  612. "Peer filter timed out",
  613. "err", err,
  614. )
  615. continue
  616. case ErrTransportClosed:
  617. sw.Logger.Error(
  618. "Stopped accept routine, as transport is closed",
  619. "numPeers", sw.peers.Size(),
  620. )
  621. default:
  622. sw.Logger.Error(
  623. "Accept on transport errored",
  624. "err", err,
  625. "numPeers", sw.peers.Size(),
  626. )
  627. // We could instead have a retry loop around the acceptRoutine,
  628. // but that would need to stop and let the node shutdown eventually.
  629. // So might as well panic and let process managers restart the node.
  630. // There's no point in letting the node run without the acceptRoutine,
  631. // since it won't be able to accept new connections.
  632. panic(fmt.Errorf("accept routine exited: %v", err))
  633. }
  634. break
  635. }
  636. isPersistent := false
  637. addr, err := peerNodeInfo.NetAddress()
  638. if err == nil {
  639. isPersistent = sw.IsPeerPersistent(addr)
  640. }
  641. p := newPeer(
  642. peerNodeInfo,
  643. newPeerConn(false, isPersistent, c),
  644. sw.reactorsByCh,
  645. sw.StopPeerForError,
  646. PeerMetrics(sw.metrics),
  647. )
  648. if !sw.IsPeerUnconditional(p.NodeInfo().ID()) {
  649. // Ignore connection if we already have enough peers.
  650. _, in, _ := sw.NumPeers()
  651. if in >= sw.config.MaxNumInboundPeers {
  652. sw.Logger.Info(
  653. "Ignoring inbound connection: already have enough inbound peers",
  654. "address", p.SocketAddr(),
  655. "have", in,
  656. "max", sw.config.MaxNumInboundPeers,
  657. )
  658. _ = p.CloseConn()
  659. continue
  660. }
  661. }
  662. if err := sw.addPeer(p); err != nil {
  663. _ = p.CloseConn()
  664. if p.IsRunning() {
  665. _ = p.Stop()
  666. }
  667. sw.conns.RemoveAddr(p.RemoteAddr())
  668. sw.Logger.Info(
  669. "Ignoring inbound connection: error while adding peer",
  670. "err", err,
  671. "id", p.ID(),
  672. )
  673. }
  674. }
  675. }
  676. // dial the peer; make secret connection; authenticate against the dialed ID;
  677. // add the peer.
  678. // if dialing fails, start the reconnect loop. If handshake fails, it's over.
  679. // If peer is started successfully, reconnectLoop will start when
  680. // StopPeerForError is called.
  681. func (sw *Switch) addOutboundPeerWithConfig(
  682. addr *NetAddress,
  683. cfg *config.P2PConfig,
  684. ) error {
  685. sw.Logger.Info("Dialing peer", "address", addr)
  686. // XXX(xla): Remove the leakage of test concerns in implementation.
  687. if cfg.TestDialFail {
  688. go sw.reconnectToPeer(addr)
  689. return fmt.Errorf("dial err (peerConfig.DialFail == true)")
  690. }
  691. // Hardcoded timeout moved from MConn transport during refactoring.
  692. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  693. defer cancel()
  694. var peerNodeInfo NodeInfo
  695. c, err := sw.transport.Dial(ctx, Endpoint{
  696. Protocol: MConnProtocol,
  697. IP: addr.IP,
  698. Port: addr.Port,
  699. })
  700. if err == nil {
  701. peerNodeInfo, _, err = sw.handshakePeer(c, addr.ID)
  702. }
  703. if err == nil {
  704. err = sw.filterConn(c.(*mConnConnection).conn)
  705. }
  706. if err != nil {
  707. if c != nil {
  708. _ = c.Close()
  709. }
  710. if e, ok := err.(ErrRejected); ok {
  711. if e.IsSelf() {
  712. // Remove the given address from the address book and add to our addresses
  713. // to avoid dialing in the future.
  714. sw.addrBook.RemoveAddress(addr)
  715. sw.addrBook.AddOurAddress(addr)
  716. return err
  717. }
  718. }
  719. // retry persistent peers after
  720. // any dial error besides IsSelf()
  721. if sw.IsPeerPersistent(addr) {
  722. go sw.reconnectToPeer(addr)
  723. }
  724. return err
  725. }
  726. p := newPeer(
  727. peerNodeInfo,
  728. newPeerConn(true, sw.IsPeerPersistent(addr), c),
  729. sw.reactorsByCh,
  730. sw.StopPeerForError,
  731. PeerMetrics(sw.metrics),
  732. )
  733. if err := sw.addPeer(p); err != nil {
  734. _ = p.CloseConn()
  735. if p.IsRunning() {
  736. _ = p.Stop()
  737. }
  738. sw.conns.RemoveAddr(p.RemoteAddr())
  739. return err
  740. }
  741. return nil
  742. }
  743. func (sw *Switch) handshakePeer(c Connection, expectPeerID NodeID) (NodeInfo, crypto.PubKey, error) {
  744. // Moved from transport and hardcoded until legacy P2P stack removal.
  745. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  746. defer cancel()
  747. peerInfo, peerKey, err := c.Handshake(ctx, sw.nodeInfo, sw.nodeKey.PrivKey)
  748. if err != nil {
  749. return peerInfo, peerKey, ErrRejected{
  750. conn: c.(*mConnConnection).conn,
  751. err: fmt.Errorf("handshake failed: %v", err),
  752. isAuthFailure: true,
  753. }
  754. }
  755. if err = peerInfo.Validate(); err != nil {
  756. return peerInfo, peerKey, ErrRejected{
  757. conn: c.(*mConnConnection).conn,
  758. err: err,
  759. isNodeInfoInvalid: true,
  760. }
  761. }
  762. // For outgoing conns, ensure connection key matches dialed key.
  763. if expectPeerID != "" {
  764. peerID := NodeIDFromPubKey(peerKey)
  765. if expectPeerID != peerID {
  766. return peerInfo, peerKey, ErrRejected{
  767. conn: c.(*mConnConnection).conn,
  768. id: peerID,
  769. err: fmt.Errorf(
  770. "conn.ID (%v) dialed ID (%v) mismatch",
  771. peerID,
  772. expectPeerID,
  773. ),
  774. isAuthFailure: true,
  775. }
  776. }
  777. }
  778. if sw.nodeInfo.ID() == peerInfo.ID() {
  779. return peerInfo, peerKey, ErrRejected{
  780. addr: *NewNetAddress(peerInfo.ID(), c.(*mConnConnection).conn.RemoteAddr()),
  781. conn: c.(*mConnConnection).conn,
  782. id: peerInfo.ID(),
  783. isSelf: true,
  784. }
  785. }
  786. if err = sw.nodeInfo.CompatibleWith(peerInfo); err != nil {
  787. return peerInfo, peerKey, ErrRejected{
  788. conn: c.(*mConnConnection).conn,
  789. err: err,
  790. id: peerInfo.ID(),
  791. isIncompatible: true,
  792. }
  793. }
  794. return peerInfo, peerKey, nil
  795. }
  796. func (sw *Switch) filterPeer(p Peer) error {
  797. // Avoid duplicate
  798. if sw.peers.Has(p.ID()) {
  799. return ErrRejected{id: p.ID(), isDuplicate: true}
  800. }
  801. errc := make(chan error, len(sw.peerFilters))
  802. for _, f := range sw.peerFilters {
  803. go func(f PeerFilterFunc, p Peer, errc chan<- error) {
  804. errc <- f(sw.peers, p)
  805. }(f, p, errc)
  806. }
  807. for i := 0; i < cap(errc); i++ {
  808. select {
  809. case err := <-errc:
  810. if err != nil {
  811. return ErrRejected{id: p.ID(), err: err, isFiltered: true}
  812. }
  813. case <-time.After(sw.filterTimeout):
  814. return ErrFilterTimeout{}
  815. }
  816. }
  817. return nil
  818. }
  819. // filterConn filters a connection, rejecting it if this function errors.
  820. //
  821. // FIXME: This is only here for compatibility with the current Switch code. In
  822. // the new P2P stack, peer/connection filtering should be moved into the Router
  823. // or PeerManager and removed from here.
  824. func (sw *Switch) filterConn(conn net.Conn) error {
  825. if sw.conns.Has(conn) {
  826. return ErrRejected{conn: conn, isDuplicate: true}
  827. }
  828. host, _, err := net.SplitHostPort(conn.RemoteAddr().String())
  829. if err != nil {
  830. return err
  831. }
  832. ip := net.ParseIP(host)
  833. if ip == nil {
  834. return fmt.Errorf("connection address has invalid IP address %q", host)
  835. }
  836. // Apply filter callbacks.
  837. chErr := make(chan error, len(sw.connFilters))
  838. for _, connFilter := range sw.connFilters {
  839. go func(connFilter ConnFilterFunc) {
  840. chErr <- connFilter(sw.conns, conn, []net.IP{ip})
  841. }(connFilter)
  842. }
  843. for i := 0; i < cap(chErr); i++ {
  844. select {
  845. case err := <-chErr:
  846. if err != nil {
  847. return ErrRejected{conn: conn, err: err, isFiltered: true}
  848. }
  849. case <-time.After(sw.filterTimeout):
  850. return ErrFilterTimeout{}
  851. }
  852. }
  853. // FIXME: Doesn't really make sense to set this here, but we preserve the
  854. // behavior from the previous P2P transport implementation.
  855. sw.conns.Set(conn, []net.IP{ip})
  856. return nil
  857. }
  858. // addPeer starts up the Peer and adds it to the Switch. Error is returned if
  859. // the peer is filtered out or failed to start or can't be added.
  860. func (sw *Switch) addPeer(p Peer) error {
  861. if err := sw.filterPeer(p); err != nil {
  862. return err
  863. }
  864. p.SetLogger(sw.Logger.With("peer", p.SocketAddr()))
  865. // Handle the shut down case where the switch has stopped but we're
  866. // concurrently trying to add a peer.
  867. if !sw.IsRunning() {
  868. // XXX should this return an error or just log and terminate?
  869. sw.Logger.Error("Won't start a peer - switch is not running", "peer", p)
  870. return nil
  871. }
  872. // Add some data to the peer, which is required by reactors.
  873. for _, reactor := range sw.reactors {
  874. p = reactor.InitPeer(p)
  875. }
  876. // Start the peer's send/recv routines.
  877. // Must start it before adding it to the peer set
  878. // to prevent Start and Stop from being called concurrently.
  879. err := p.Start()
  880. if err != nil {
  881. // Should never happen
  882. sw.Logger.Error("Error starting peer", "err", err, "peer", p)
  883. return err
  884. }
  885. // Add the peer to PeerSet. Do this before starting the reactors
  886. // so that if Receive errors, we will find the peer and remove it.
  887. // Add should not err since we already checked peers.Has().
  888. if err := sw.peers.Add(p); err != nil {
  889. return err
  890. }
  891. sw.metrics.Peers.Add(float64(1))
  892. // Start all the reactor protocols on the peer.
  893. for _, reactor := range sw.reactors {
  894. reactor.AddPeer(p)
  895. }
  896. sw.Logger.Info("Added peer", "peer", p)
  897. return nil
  898. }
  899. // FIXME: Eww, needed to wire up the new P2P stack along with the old one. This
  900. // should be passed into the transport when it's constructed.
  901. func (sw *Switch) PutChannelDescsIntoTransport() {
  902. if t, ok := sw.transport.(*MConnTransport); ok {
  903. t.channelDescs = sw.chDescs
  904. }
  905. }