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.

287 lines
6.8 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: 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. mrand "math/rand"
  6. "net"
  7. "github.com/tendermint/tendermint/libs/log"
  8. tmnet "github.com/tendermint/tendermint/libs/net"
  9. tmrand "github.com/tendermint/tendermint/libs/rand"
  10. "github.com/tendermint/tendermint/config"
  11. "github.com/tendermint/tendermint/internal/p2p/conn"
  12. )
  13. const testCh = 0x01
  14. //------------------------------------------------
  15. func AddPeerToSwitchPeerSet(sw *Switch, peer Peer) {
  16. sw.peers.Add(peer) //nolint:errcheck // ignore error
  17. }
  18. func CreateRandomPeer(outbound bool) Peer {
  19. addr, netAddr := CreateRoutableAddr()
  20. p := &peer{
  21. peerConn: peerConn{outbound: outbound},
  22. nodeInfo: NodeInfo{
  23. NodeID: netAddr.ID,
  24. ListenAddr: netAddr.DialString(),
  25. },
  26. metrics: NopMetrics(),
  27. }
  28. p.SetLogger(log.TestingLogger().With("peer", addr))
  29. return p
  30. }
  31. // nolint:gosec // G404: Use of weak random number generator
  32. func CreateRoutableAddr() (addr string, netAddr *NetAddress) {
  33. for {
  34. var err error
  35. addr = fmt.Sprintf("%X@%v.%v.%v.%v:26656",
  36. tmrand.Bytes(20),
  37. mrand.Int()%256,
  38. mrand.Int()%256,
  39. mrand.Int()%256,
  40. mrand.Int()%256)
  41. netAddr, err = NewNetAddressString(addr)
  42. if err != nil {
  43. panic(err)
  44. }
  45. if netAddr.Routable() {
  46. break
  47. }
  48. }
  49. return
  50. }
  51. //------------------------------------------------------------------
  52. // Connects switches via arbitrary net.Conn. Used for testing.
  53. const TestHost = "localhost"
  54. // MakeConnectedSwitches returns n switches, connected according to the connect func.
  55. // If connect==Connect2Switches, the switches will be fully connected.
  56. // initSwitch defines how the i'th switch should be initialized (ie. with what reactors).
  57. // NOTE: panics if any switch fails to start.
  58. func MakeConnectedSwitches(cfg *config.P2PConfig,
  59. n int,
  60. initSwitch func(int, *Switch) *Switch,
  61. connect func([]*Switch, int, int),
  62. ) []*Switch {
  63. switches := make([]*Switch, n)
  64. for i := 0; i < n; i++ {
  65. switches[i] = MakeSwitch(cfg, i, TestHost, "123.123.123", initSwitch, log.TestingLogger())
  66. }
  67. if err := StartSwitches(switches); err != nil {
  68. panic(err)
  69. }
  70. for i := 0; i < n; i++ {
  71. for j := i + 1; j < n; j++ {
  72. connect(switches, i, j)
  73. }
  74. }
  75. return switches
  76. }
  77. // Connect2Switches will connect switches i and j via net.Pipe().
  78. // Blocks until a connection is established.
  79. // NOTE: caller ensures i and j are within bounds.
  80. func Connect2Switches(switches []*Switch, i, j int) {
  81. switchI := switches[i]
  82. switchJ := switches[j]
  83. c1, c2 := conn.NetPipe()
  84. doneCh := make(chan struct{})
  85. go func() {
  86. err := switchI.addPeerWithConnection(c1)
  87. if err != nil {
  88. panic(err)
  89. }
  90. doneCh <- struct{}{}
  91. }()
  92. go func() {
  93. err := switchJ.addPeerWithConnection(c2)
  94. if err != nil {
  95. panic(err)
  96. }
  97. doneCh <- struct{}{}
  98. }()
  99. <-doneCh
  100. <-doneCh
  101. }
  102. func (sw *Switch) addPeerWithConnection(conn net.Conn) error {
  103. pc, err := testInboundPeerConn(sw.transport.(*MConnTransport), conn)
  104. if err != nil {
  105. if err := conn.Close(); err != nil {
  106. sw.Logger.Error("Error closing connection", "err", err)
  107. }
  108. return err
  109. }
  110. peerNodeInfo, _, err := pc.conn.Handshake(context.Background(), sw.nodeInfo, sw.nodeKey.PrivKey)
  111. if err != nil {
  112. if err := conn.Close(); err != nil {
  113. sw.Logger.Error("Error closing connection", "err", err)
  114. }
  115. return err
  116. }
  117. p := newPeer(
  118. peerNodeInfo,
  119. pc,
  120. sw.reactorsByCh,
  121. sw.StopPeerForError,
  122. )
  123. if err = sw.addPeer(p); err != nil {
  124. pc.CloseConn()
  125. return err
  126. }
  127. return nil
  128. }
  129. // StartSwitches calls sw.Start() for each given switch.
  130. // It returns the first encountered error.
  131. func StartSwitches(switches []*Switch) error {
  132. for _, s := range switches {
  133. err := s.Start() // start switch and reactors
  134. if err != nil {
  135. return err
  136. }
  137. }
  138. return nil
  139. }
  140. func MakeSwitch(
  141. cfg *config.P2PConfig,
  142. i int,
  143. network, version string,
  144. initSwitch func(int, *Switch) *Switch,
  145. logger log.Logger,
  146. opts ...SwitchOption,
  147. ) *Switch {
  148. nodeKey := GenNodeKey()
  149. nodeInfo := testNodeInfo(nodeKey.ID, fmt.Sprintf("node%d", i))
  150. addr, err := NewNetAddressString(
  151. IDAddressString(nodeKey.ID, nodeInfo.ListenAddr),
  152. )
  153. if err != nil {
  154. panic(err)
  155. }
  156. swLogger := logger.With("switch", i)
  157. t := NewMConnTransport(swLogger, MConnConfig(cfg),
  158. []*ChannelDescriptor{}, MConnTransportOptions{})
  159. // TODO: let the config be passed in?
  160. sw := initSwitch(i, NewSwitch(cfg, t, opts...))
  161. sw.SetLogger(swLogger)
  162. sw.SetNodeKey(nodeKey)
  163. if err := t.Listen(addr.Endpoint()); err != nil {
  164. panic(err)
  165. }
  166. ni := nodeInfo
  167. ni.Channels = []byte{}
  168. for ch := range sw.reactorsByCh {
  169. ni.Channels = append(ni.Channels, ch)
  170. }
  171. nodeInfo = ni
  172. // TODO: We need to setup reactors ahead of time so the NodeInfo is properly
  173. // populated and we don't have to do those awkward overrides and setters.
  174. sw.SetNodeInfo(nodeInfo)
  175. return sw
  176. }
  177. func testInboundPeerConn(
  178. transport *MConnTransport,
  179. conn net.Conn,
  180. ) (peerConn, error) {
  181. return testPeerConn(transport, conn, false, false)
  182. }
  183. func testPeerConn(
  184. transport *MConnTransport,
  185. rawConn net.Conn,
  186. outbound, persistent bool,
  187. ) (pc peerConn, err error) {
  188. conn := newMConnConnection(transport.logger, rawConn, transport.mConnConfig, transport.channelDescs)
  189. return newPeerConn(outbound, persistent, conn), nil
  190. }
  191. //----------------------------------------------------------------
  192. // rand node info
  193. func testNodeInfo(id NodeID, name string) NodeInfo {
  194. return testNodeInfoWithNetwork(id, name, "testing")
  195. }
  196. func testNodeInfoWithNetwork(id NodeID, name, network string) NodeInfo {
  197. return NodeInfo{
  198. ProtocolVersion: defaultProtocolVersion,
  199. NodeID: id,
  200. ListenAddr: fmt.Sprintf("127.0.0.1:%d", getFreePort()),
  201. Network: network,
  202. Version: "1.2.3-rc0-deadbeef",
  203. Channels: []byte{testCh},
  204. Moniker: name,
  205. Other: NodeInfoOther{
  206. TxIndex: "on",
  207. RPCAddress: fmt.Sprintf("127.0.0.1:%d", getFreePort()),
  208. },
  209. }
  210. }
  211. func getFreePort() int {
  212. port, err := tmnet.GetFreePort()
  213. if err != nil {
  214. panic(err)
  215. }
  216. return port
  217. }
  218. type AddrBookMock struct {
  219. Addrs map[string]struct{}
  220. OurAddrs map[string]struct{}
  221. PrivateAddrs map[string]struct{}
  222. }
  223. var _ AddrBook = (*AddrBookMock)(nil)
  224. func (book *AddrBookMock) AddAddress(addr *NetAddress, src *NetAddress) error {
  225. book.Addrs[addr.String()] = struct{}{}
  226. return nil
  227. }
  228. func (book *AddrBookMock) AddOurAddress(addr *NetAddress) { book.OurAddrs[addr.String()] = struct{}{} }
  229. func (book *AddrBookMock) OurAddress(addr *NetAddress) bool {
  230. _, ok := book.OurAddrs[addr.String()]
  231. return ok
  232. }
  233. func (book *AddrBookMock) MarkGood(NodeID) {}
  234. func (book *AddrBookMock) HasAddress(addr *NetAddress) bool {
  235. _, ok := book.Addrs[addr.String()]
  236. return ok
  237. }
  238. func (book *AddrBookMock) RemoveAddress(addr *NetAddress) {
  239. delete(book.Addrs, addr.String())
  240. }
  241. func (book *AddrBookMock) Save() {}
  242. func (book *AddrBookMock) AddPrivateIDs(addrs []string) {
  243. for _, addr := range addrs {
  244. book.PrivateAddrs[addr] = struct{}{}
  245. }
  246. }