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.

753 lines
19 KiB

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
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. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net"
  9. "net/http"
  10. "net/http/httptest"
  11. "regexp"
  12. "strconv"
  13. "sync"
  14. "sync/atomic"
  15. "testing"
  16. "time"
  17. "github.com/prometheus/client_golang/prometheus/promhttp"
  18. "github.com/stretchr/testify/assert"
  19. "github.com/stretchr/testify/require"
  20. "github.com/tendermint/tendermint/config"
  21. "github.com/tendermint/tendermint/crypto/ed25519"
  22. "github.com/tendermint/tendermint/libs/log"
  23. "github.com/tendermint/tendermint/p2p/conn"
  24. )
  25. var (
  26. cfg *config.P2PConfig
  27. )
  28. func init() {
  29. cfg = config.DefaultP2PConfig()
  30. cfg.PexReactor = true
  31. cfg.AllowDuplicateIP = true
  32. }
  33. type PeerMessage struct {
  34. PeerID ID
  35. Bytes []byte
  36. Counter int
  37. }
  38. type TestReactor struct {
  39. BaseReactor
  40. mtx sync.Mutex
  41. channels []*conn.ChannelDescriptor
  42. logMessages bool
  43. msgsCounter int
  44. msgsReceived map[byte][]PeerMessage
  45. }
  46. func NewTestReactor(channels []*conn.ChannelDescriptor, logMessages bool) *TestReactor {
  47. tr := &TestReactor{
  48. channels: channels,
  49. logMessages: logMessages,
  50. msgsReceived: make(map[byte][]PeerMessage),
  51. }
  52. tr.BaseReactor = *NewBaseReactor("TestReactor", tr)
  53. tr.SetLogger(log.TestingLogger())
  54. return tr
  55. }
  56. func (tr *TestReactor) GetChannels() []*conn.ChannelDescriptor {
  57. return tr.channels
  58. }
  59. func (tr *TestReactor) AddPeer(peer Peer) {}
  60. func (tr *TestReactor) RemovePeer(peer Peer, reason interface{}) {}
  61. func (tr *TestReactor) Receive(chID byte, peer Peer, msgBytes []byte) {
  62. if tr.logMessages {
  63. tr.mtx.Lock()
  64. defer tr.mtx.Unlock()
  65. //fmt.Printf("Received: %X, %X\n", chID, msgBytes)
  66. tr.msgsReceived[chID] = append(tr.msgsReceived[chID], PeerMessage{peer.ID(), msgBytes, tr.msgsCounter})
  67. tr.msgsCounter++
  68. }
  69. }
  70. func (tr *TestReactor) getMsgs(chID byte) []PeerMessage {
  71. tr.mtx.Lock()
  72. defer tr.mtx.Unlock()
  73. return tr.msgsReceived[chID]
  74. }
  75. //-----------------------------------------------------------------------------
  76. // convenience method for creating two switches connected to each other.
  77. // XXX: note this uses net.Pipe and not a proper TCP conn
  78. func MakeSwitchPair(t testing.TB, initSwitch func(int, *Switch) *Switch) (*Switch, *Switch) {
  79. // Create two switches that will be interconnected.
  80. switches := MakeConnectedSwitches(cfg, 2, initSwitch, Connect2Switches)
  81. return switches[0], switches[1]
  82. }
  83. func initSwitchFunc(i int, sw *Switch) *Switch {
  84. sw.SetAddrBook(&addrBookMock{
  85. addrs: make(map[string]struct{}),
  86. ourAddrs: make(map[string]struct{})})
  87. // Make two reactors of two channels each
  88. sw.AddReactor("foo", NewTestReactor([]*conn.ChannelDescriptor{
  89. {ID: byte(0x00), Priority: 10},
  90. {ID: byte(0x01), Priority: 10},
  91. }, true))
  92. sw.AddReactor("bar", NewTestReactor([]*conn.ChannelDescriptor{
  93. {ID: byte(0x02), Priority: 10},
  94. {ID: byte(0x03), Priority: 10},
  95. }, true))
  96. return sw
  97. }
  98. func TestSwitches(t *testing.T) {
  99. s1, s2 := MakeSwitchPair(t, initSwitchFunc)
  100. defer s1.Stop()
  101. defer s2.Stop()
  102. if s1.Peers().Size() != 1 {
  103. t.Errorf("expected exactly 1 peer in s1, got %v", s1.Peers().Size())
  104. }
  105. if s2.Peers().Size() != 1 {
  106. t.Errorf("expected exactly 1 peer in s2, got %v", s2.Peers().Size())
  107. }
  108. // Lets send some messages
  109. ch0Msg := []byte("channel zero")
  110. ch1Msg := []byte("channel foo")
  111. ch2Msg := []byte("channel bar")
  112. s1.Broadcast(byte(0x00), ch0Msg)
  113. s1.Broadcast(byte(0x01), ch1Msg)
  114. s1.Broadcast(byte(0x02), ch2Msg)
  115. assertMsgReceivedWithTimeout(t,
  116. ch0Msg,
  117. byte(0x00),
  118. s2.Reactor("foo").(*TestReactor), 10*time.Millisecond, 5*time.Second)
  119. assertMsgReceivedWithTimeout(t,
  120. ch1Msg,
  121. byte(0x01),
  122. s2.Reactor("foo").(*TestReactor), 10*time.Millisecond, 5*time.Second)
  123. assertMsgReceivedWithTimeout(t,
  124. ch2Msg,
  125. byte(0x02),
  126. s2.Reactor("bar").(*TestReactor), 10*time.Millisecond, 5*time.Second)
  127. }
  128. func assertMsgReceivedWithTimeout(
  129. t *testing.T,
  130. msgBytes []byte,
  131. channel byte,
  132. reactor *TestReactor,
  133. checkPeriod,
  134. timeout time.Duration,
  135. ) {
  136. ticker := time.NewTicker(checkPeriod)
  137. for {
  138. select {
  139. case <-ticker.C:
  140. msgs := reactor.getMsgs(channel)
  141. if len(msgs) > 0 {
  142. if !bytes.Equal(msgs[0].Bytes, msgBytes) {
  143. t.Fatalf("Unexpected message bytes. Wanted: %X, Got: %X", msgBytes, msgs[0].Bytes)
  144. }
  145. return
  146. }
  147. case <-time.After(timeout):
  148. t.Fatalf("Expected to have received 1 message in channel #%v, got zero", channel)
  149. }
  150. }
  151. }
  152. func TestSwitchFiltersOutItself(t *testing.T) {
  153. s1 := MakeSwitch(cfg, 1, "127.0.0.1", "123.123.123", initSwitchFunc)
  154. // simulate s1 having a public IP by creating a remote peer with the same ID
  155. rp := &remotePeer{PrivKey: s1.nodeKey.PrivKey, Config: cfg}
  156. rp.Start()
  157. // addr should be rejected in addPeer based on the same ID
  158. err := s1.DialPeerWithAddress(rp.Addr())
  159. if assert.Error(t, err) {
  160. if err, ok := err.(ErrRejected); ok {
  161. if !err.IsSelf() {
  162. t.Errorf("expected self to be rejected")
  163. }
  164. } else {
  165. t.Errorf("expected ErrRejected")
  166. }
  167. }
  168. assert.True(t, s1.addrBook.OurAddress(rp.Addr()))
  169. assert.False(t, s1.addrBook.HasAddress(rp.Addr()))
  170. rp.Stop()
  171. assertNoPeersAfterTimeout(t, s1, 100*time.Millisecond)
  172. }
  173. func TestSwitchPeerFilter(t *testing.T) {
  174. var (
  175. filters = []PeerFilterFunc{
  176. func(_ IPeerSet, _ Peer) error { return nil },
  177. func(_ IPeerSet, _ Peer) error { return fmt.Errorf("denied!") },
  178. func(_ IPeerSet, _ Peer) error { return nil },
  179. }
  180. sw = MakeSwitch(
  181. cfg,
  182. 1,
  183. "testing",
  184. "123.123.123",
  185. initSwitchFunc,
  186. SwitchPeerFilters(filters...),
  187. )
  188. )
  189. defer sw.Stop()
  190. // simulate remote peer
  191. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  192. rp.Start()
  193. defer rp.Stop()
  194. p, err := sw.transport.Dial(*rp.Addr(), peerConfig{
  195. chDescs: sw.chDescs,
  196. onPeerError: sw.StopPeerForError,
  197. isPersistent: sw.isPeerPersistentFn(),
  198. reactorsByCh: sw.reactorsByCh,
  199. })
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. err = sw.addPeer(p)
  204. if err, ok := err.(ErrRejected); ok {
  205. if !err.IsFiltered() {
  206. t.Errorf("expected peer to be filtered")
  207. }
  208. } else {
  209. t.Errorf("expected ErrRejected")
  210. }
  211. }
  212. func TestSwitchPeerFilterTimeout(t *testing.T) {
  213. var (
  214. filters = []PeerFilterFunc{
  215. func(_ IPeerSet, _ Peer) error {
  216. time.Sleep(10 * time.Millisecond)
  217. return nil
  218. },
  219. }
  220. sw = MakeSwitch(
  221. cfg,
  222. 1,
  223. "testing",
  224. "123.123.123",
  225. initSwitchFunc,
  226. SwitchFilterTimeout(5*time.Millisecond),
  227. SwitchPeerFilters(filters...),
  228. )
  229. )
  230. defer sw.Stop()
  231. // simulate remote peer
  232. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  233. rp.Start()
  234. defer rp.Stop()
  235. p, err := sw.transport.Dial(*rp.Addr(), peerConfig{
  236. chDescs: sw.chDescs,
  237. onPeerError: sw.StopPeerForError,
  238. isPersistent: sw.isPeerPersistentFn(),
  239. reactorsByCh: sw.reactorsByCh,
  240. })
  241. if err != nil {
  242. t.Fatal(err)
  243. }
  244. err = sw.addPeer(p)
  245. if _, ok := err.(ErrFilterTimeout); !ok {
  246. t.Errorf("expected ErrFilterTimeout")
  247. }
  248. }
  249. func TestSwitchPeerFilterDuplicate(t *testing.T) {
  250. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  251. sw.Start()
  252. defer sw.Stop()
  253. // simulate remote peer
  254. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  255. rp.Start()
  256. defer rp.Stop()
  257. p, err := sw.transport.Dial(*rp.Addr(), peerConfig{
  258. chDescs: sw.chDescs,
  259. onPeerError: sw.StopPeerForError,
  260. isPersistent: sw.isPeerPersistentFn(),
  261. reactorsByCh: sw.reactorsByCh,
  262. })
  263. if err != nil {
  264. t.Fatal(err)
  265. }
  266. if err := sw.addPeer(p); err != nil {
  267. t.Fatal(err)
  268. }
  269. err = sw.addPeer(p)
  270. if errRej, ok := err.(ErrRejected); ok {
  271. if !errRej.IsDuplicate() {
  272. t.Errorf("expected peer to be duplicate. got %v", errRej)
  273. }
  274. } else {
  275. t.Errorf("expected ErrRejected, got %v", err)
  276. }
  277. }
  278. func assertNoPeersAfterTimeout(t *testing.T, sw *Switch, timeout time.Duration) {
  279. time.Sleep(timeout)
  280. if sw.Peers().Size() != 0 {
  281. t.Fatalf("Expected %v to not connect to some peers, got %d", sw, sw.Peers().Size())
  282. }
  283. }
  284. func TestSwitchStopsNonPersistentPeerOnError(t *testing.T) {
  285. assert, require := assert.New(t), require.New(t)
  286. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  287. err := sw.Start()
  288. if err != nil {
  289. t.Error(err)
  290. }
  291. defer sw.Stop()
  292. // simulate remote peer
  293. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  294. rp.Start()
  295. defer rp.Stop()
  296. p, err := sw.transport.Dial(*rp.Addr(), peerConfig{
  297. chDescs: sw.chDescs,
  298. onPeerError: sw.StopPeerForError,
  299. isPersistent: sw.isPeerPersistentFn(),
  300. reactorsByCh: sw.reactorsByCh,
  301. })
  302. require.Nil(err)
  303. err = sw.addPeer(p)
  304. require.Nil(err)
  305. require.NotNil(sw.Peers().Get(rp.ID()))
  306. // simulate failure by closing connection
  307. p.(*peer).CloseConn()
  308. assertNoPeersAfterTimeout(t, sw, 100*time.Millisecond)
  309. assert.False(p.IsRunning())
  310. }
  311. func TestSwitchStopPeerForError(t *testing.T) {
  312. s := httptest.NewServer(promhttp.Handler())
  313. defer s.Close()
  314. scrapeMetrics := func() string {
  315. resp, err := http.Get(s.URL)
  316. assert.NoError(t, err)
  317. defer resp.Body.Close()
  318. buf, _ := ioutil.ReadAll(resp.Body)
  319. return string(buf)
  320. }
  321. namespace, subsystem, name := config.TestInstrumentationConfig().Namespace, MetricsSubsystem, "peers"
  322. re := regexp.MustCompile(namespace + `_` + subsystem + `_` + name + ` ([0-9\.]+)`)
  323. peersMetricValue := func() float64 {
  324. matches := re.FindStringSubmatch(scrapeMetrics())
  325. f, _ := strconv.ParseFloat(matches[1], 64)
  326. return f
  327. }
  328. p2pMetrics := PrometheusMetrics(namespace)
  329. // make two connected switches
  330. sw1, sw2 := MakeSwitchPair(t, func(i int, sw *Switch) *Switch {
  331. // set metrics on sw1
  332. if i == 0 {
  333. opt := WithMetrics(p2pMetrics)
  334. opt(sw)
  335. }
  336. return initSwitchFunc(i, sw)
  337. })
  338. assert.Equal(t, len(sw1.Peers().List()), 1)
  339. assert.EqualValues(t, 1, peersMetricValue())
  340. // send messages to the peer from sw1
  341. p := sw1.Peers().List()[0]
  342. p.Send(0x1, []byte("here's a message to send"))
  343. // stop sw2. this should cause the p to fail,
  344. // which results in calling StopPeerForError internally
  345. sw2.Stop()
  346. // now call StopPeerForError explicitly, eg. from a reactor
  347. sw1.StopPeerForError(p, fmt.Errorf("some err"))
  348. assert.Equal(t, len(sw1.Peers().List()), 0)
  349. assert.EqualValues(t, 0, peersMetricValue())
  350. }
  351. func TestSwitchReconnectsToOutboundPersistentPeer(t *testing.T) {
  352. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  353. err := sw.Start()
  354. require.NoError(t, err)
  355. defer sw.Stop()
  356. // 1. simulate failure by closing connection
  357. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  358. rp.Start()
  359. defer rp.Stop()
  360. err = sw.AddPersistentPeers([]string{rp.Addr().String()})
  361. require.NoError(t, err)
  362. err = sw.DialPeerWithAddress(rp.Addr())
  363. require.Nil(t, err)
  364. require.NotNil(t, sw.Peers().Get(rp.ID()))
  365. p := sw.Peers().List()[0]
  366. p.(*peer).CloseConn()
  367. waitUntilSwitchHasAtLeastNPeers(sw, 1)
  368. assert.False(t, p.IsRunning()) // old peer instance
  369. assert.Equal(t, 1, sw.Peers().Size()) // new peer instance
  370. // 2. simulate first time dial failure
  371. rp = &remotePeer{
  372. PrivKey: ed25519.GenPrivKey(),
  373. Config: cfg,
  374. // Use different interface to prevent duplicate IP filter, this will break
  375. // beyond two peers.
  376. listenAddr: "127.0.0.1:0",
  377. }
  378. rp.Start()
  379. defer rp.Stop()
  380. conf := config.DefaultP2PConfig()
  381. conf.TestDialFail = true // will trigger a reconnect
  382. err = sw.addOutboundPeerWithConfig(rp.Addr(), conf)
  383. require.NotNil(t, err)
  384. // DialPeerWithAddres - sw.peerConfig resets the dialer
  385. waitUntilSwitchHasAtLeastNPeers(sw, 2)
  386. assert.Equal(t, 2, sw.Peers().Size())
  387. }
  388. func TestSwitchReconnectsToInboundPersistentPeer(t *testing.T) {
  389. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  390. err := sw.Start()
  391. require.NoError(t, err)
  392. defer sw.Stop()
  393. // 1. simulate failure by closing the connection
  394. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  395. rp.Start()
  396. defer rp.Stop()
  397. err = sw.AddPersistentPeers([]string{rp.Addr().String()})
  398. require.NoError(t, err)
  399. conn, err := rp.Dial(sw.NetAddress())
  400. require.NoError(t, err)
  401. time.Sleep(50 * time.Millisecond)
  402. require.NotNil(t, sw.Peers().Get(rp.ID()))
  403. conn.Close()
  404. waitUntilSwitchHasAtLeastNPeers(sw, 1)
  405. assert.Equal(t, 1, sw.Peers().Size())
  406. }
  407. func TestSwitchDialPeersAsync(t *testing.T) {
  408. if testing.Short() {
  409. return
  410. }
  411. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  412. err := sw.Start()
  413. require.NoError(t, err)
  414. defer sw.Stop()
  415. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  416. rp.Start()
  417. defer rp.Stop()
  418. err = sw.DialPeersAsync([]string{rp.Addr().String()})
  419. require.NoError(t, err)
  420. time.Sleep(dialRandomizerIntervalMilliseconds * time.Millisecond)
  421. require.NotNil(t, sw.Peers().Get(rp.ID()))
  422. }
  423. func waitUntilSwitchHasAtLeastNPeers(sw *Switch, n int) {
  424. for i := 0; i < 20; i++ {
  425. time.Sleep(250 * time.Millisecond)
  426. has := sw.Peers().Size()
  427. if has >= n {
  428. break
  429. }
  430. }
  431. }
  432. func TestSwitchFullConnectivity(t *testing.T) {
  433. switches := MakeConnectedSwitches(cfg, 3, initSwitchFunc, Connect2Switches)
  434. defer func() {
  435. for _, sw := range switches {
  436. sw.Stop()
  437. }
  438. }()
  439. for i, sw := range switches {
  440. if sw.Peers().Size() != 2 {
  441. t.Fatalf("Expected each switch to be connected to 2 other, but %d switch only connected to %d", sw.Peers().Size(), i)
  442. }
  443. }
  444. }
  445. func TestSwitchAcceptRoutine(t *testing.T) {
  446. cfg.MaxNumInboundPeers = 5
  447. // make switch
  448. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  449. err := sw.Start()
  450. require.NoError(t, err)
  451. defer sw.Stop()
  452. remotePeers := make([]*remotePeer, 0)
  453. assert.Equal(t, 0, sw.Peers().Size())
  454. // 1. check we connect up to MaxNumInboundPeers
  455. for i := 0; i < cfg.MaxNumInboundPeers; i++ {
  456. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  457. remotePeers = append(remotePeers, rp)
  458. rp.Start()
  459. c, err := rp.Dial(sw.NetAddress())
  460. require.NoError(t, err)
  461. // spawn a reading routine to prevent connection from closing
  462. go func(c net.Conn) {
  463. for {
  464. one := make([]byte, 1)
  465. _, err := c.Read(one)
  466. if err != nil {
  467. return
  468. }
  469. }
  470. }(c)
  471. }
  472. time.Sleep(10 * time.Millisecond)
  473. assert.Equal(t, cfg.MaxNumInboundPeers, sw.Peers().Size())
  474. // 2. check we close new connections if we already have MaxNumInboundPeers peers
  475. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  476. rp.Start()
  477. conn, err := rp.Dial(sw.NetAddress())
  478. require.NoError(t, err)
  479. // check conn is closed
  480. one := make([]byte, 1)
  481. conn.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
  482. _, err = conn.Read(one)
  483. assert.Equal(t, io.EOF, err)
  484. assert.Equal(t, cfg.MaxNumInboundPeers, sw.Peers().Size())
  485. rp.Stop()
  486. // stop remote peers
  487. for _, rp := range remotePeers {
  488. rp.Stop()
  489. }
  490. }
  491. type errorTransport struct {
  492. acceptErr error
  493. }
  494. func (et errorTransport) NetAddress() NetAddress {
  495. panic("not implemented")
  496. }
  497. func (et errorTransport) Accept(c peerConfig) (Peer, error) {
  498. return nil, et.acceptErr
  499. }
  500. func (errorTransport) Dial(NetAddress, peerConfig) (Peer, error) {
  501. panic("not implemented")
  502. }
  503. func (errorTransport) Cleanup(Peer) {
  504. panic("not implemented")
  505. }
  506. func TestSwitchAcceptRoutineErrorCases(t *testing.T) {
  507. sw := NewSwitch(cfg, errorTransport{ErrFilterTimeout{}})
  508. assert.NotPanics(t, func() {
  509. err := sw.Start()
  510. assert.NoError(t, err)
  511. sw.Stop()
  512. })
  513. sw = NewSwitch(cfg, errorTransport{ErrRejected{conn: nil, err: errors.New("filtered"), isFiltered: true}})
  514. assert.NotPanics(t, func() {
  515. err := sw.Start()
  516. assert.NoError(t, err)
  517. sw.Stop()
  518. })
  519. // TODO(melekes) check we remove our address from addrBook
  520. sw = NewSwitch(cfg, errorTransport{ErrTransportClosed{}})
  521. assert.NotPanics(t, func() {
  522. err := sw.Start()
  523. assert.NoError(t, err)
  524. sw.Stop()
  525. })
  526. }
  527. // mockReactor checks that InitPeer never called before RemovePeer. If that's
  528. // not true, InitCalledBeforeRemoveFinished will return true.
  529. type mockReactor struct {
  530. *BaseReactor
  531. // atomic
  532. removePeerInProgress uint32
  533. initCalledBeforeRemoveFinished uint32
  534. }
  535. func (r *mockReactor) RemovePeer(peer Peer, reason interface{}) {
  536. atomic.StoreUint32(&r.removePeerInProgress, 1)
  537. defer atomic.StoreUint32(&r.removePeerInProgress, 0)
  538. time.Sleep(100 * time.Millisecond)
  539. }
  540. func (r *mockReactor) InitPeer(peer Peer) Peer {
  541. if atomic.LoadUint32(&r.removePeerInProgress) == 1 {
  542. atomic.StoreUint32(&r.initCalledBeforeRemoveFinished, 1)
  543. }
  544. return peer
  545. }
  546. func (r *mockReactor) InitCalledBeforeRemoveFinished() bool {
  547. return atomic.LoadUint32(&r.initCalledBeforeRemoveFinished) == 1
  548. }
  549. // see stopAndRemovePeer
  550. func TestSwitchInitPeerIsNotCalledBeforeRemovePeer(t *testing.T) {
  551. // make reactor
  552. reactor := &mockReactor{}
  553. reactor.BaseReactor = NewBaseReactor("mockReactor", reactor)
  554. // make switch
  555. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", func(i int, sw *Switch) *Switch {
  556. sw.AddReactor("mock", reactor)
  557. return sw
  558. })
  559. err := sw.Start()
  560. require.NoError(t, err)
  561. defer sw.Stop()
  562. // add peer
  563. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  564. rp.Start()
  565. defer rp.Stop()
  566. _, err = rp.Dial(sw.NetAddress())
  567. require.NoError(t, err)
  568. // wait till the switch adds rp to the peer set
  569. time.Sleep(50 * time.Millisecond)
  570. // stop peer asynchronously
  571. go sw.StopPeerForError(sw.Peers().Get(rp.ID()), "test")
  572. // simulate peer reconnecting to us
  573. _, err = rp.Dial(sw.NetAddress())
  574. require.NoError(t, err)
  575. // wait till the switch adds rp to the peer set
  576. time.Sleep(50 * time.Millisecond)
  577. // make sure reactor.RemovePeer is finished before InitPeer is called
  578. assert.False(t, reactor.InitCalledBeforeRemoveFinished())
  579. }
  580. func BenchmarkSwitchBroadcast(b *testing.B) {
  581. s1, s2 := MakeSwitchPair(b, func(i int, sw *Switch) *Switch {
  582. // Make bar reactors of bar channels each
  583. sw.AddReactor("foo", NewTestReactor([]*conn.ChannelDescriptor{
  584. {ID: byte(0x00), Priority: 10},
  585. {ID: byte(0x01), Priority: 10},
  586. }, false))
  587. sw.AddReactor("bar", NewTestReactor([]*conn.ChannelDescriptor{
  588. {ID: byte(0x02), Priority: 10},
  589. {ID: byte(0x03), Priority: 10},
  590. }, false))
  591. return sw
  592. })
  593. defer s1.Stop()
  594. defer s2.Stop()
  595. // Allow time for goroutines to boot up
  596. time.Sleep(1 * time.Second)
  597. b.ResetTimer()
  598. numSuccess, numFailure := 0, 0
  599. // Send random message from foo channel to another
  600. for i := 0; i < b.N; i++ {
  601. chID := byte(i % 4)
  602. successChan := s1.Broadcast(chID, []byte("test data"))
  603. for s := range successChan {
  604. if s {
  605. numSuccess++
  606. } else {
  607. numFailure++
  608. }
  609. }
  610. }
  611. b.Logf("success: %v, failure: %v", numSuccess, numFailure)
  612. }
  613. type addrBookMock struct {
  614. addrs map[string]struct{}
  615. ourAddrs map[string]struct{}
  616. }
  617. var _ AddrBook = (*addrBookMock)(nil)
  618. func (book *addrBookMock) AddAddress(addr *NetAddress, src *NetAddress) error {
  619. book.addrs[addr.String()] = struct{}{}
  620. return nil
  621. }
  622. func (book *addrBookMock) AddOurAddress(addr *NetAddress) { book.ourAddrs[addr.String()] = struct{}{} }
  623. func (book *addrBookMock) OurAddress(addr *NetAddress) bool {
  624. _, ok := book.ourAddrs[addr.String()]
  625. return ok
  626. }
  627. func (book *addrBookMock) MarkGood(ID) {}
  628. func (book *addrBookMock) HasAddress(addr *NetAddress) bool {
  629. _, ok := book.addrs[addr.String()]
  630. return ok
  631. }
  632. func (book *addrBookMock) RemoveAddress(addr *NetAddress) {
  633. delete(book.addrs, addr.String())
  634. }
  635. func (book *addrBookMock) Save() {}