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.

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