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.

752 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, _ := http.Get(s.URL)
  316. buf, _ := ioutil.ReadAll(resp.Body)
  317. resp.Body.Close()
  318. return string(buf)
  319. }
  320. namespace, subsystem, name := config.TestInstrumentationConfig().Namespace, MetricsSubsystem, "peers"
  321. re := regexp.MustCompile(namespace + `_` + subsystem + `_` + name + ` ([0-9\.]+)`)
  322. peersMetricValue := func() float64 {
  323. matches := re.FindStringSubmatch(scrapeMetrics())
  324. f, _ := strconv.ParseFloat(matches[1], 64)
  325. return f
  326. }
  327. p2pMetrics := PrometheusMetrics(namespace)
  328. // make two connected switches
  329. sw1, sw2 := MakeSwitchPair(t, func(i int, sw *Switch) *Switch {
  330. // set metrics on sw1
  331. if i == 0 {
  332. opt := WithMetrics(p2pMetrics)
  333. opt(sw)
  334. }
  335. return initSwitchFunc(i, sw)
  336. })
  337. assert.Equal(t, len(sw1.Peers().List()), 1)
  338. assert.EqualValues(t, 1, peersMetricValue())
  339. // send messages to the peer from sw1
  340. p := sw1.Peers().List()[0]
  341. p.Send(0x1, []byte("here's a message to send"))
  342. // stop sw2. this should cause the p to fail,
  343. // which results in calling StopPeerForError internally
  344. sw2.Stop()
  345. // now call StopPeerForError explicitly, eg. from a reactor
  346. sw1.StopPeerForError(p, fmt.Errorf("some err"))
  347. assert.Equal(t, len(sw1.Peers().List()), 0)
  348. assert.EqualValues(t, 0, peersMetricValue())
  349. }
  350. func TestSwitchReconnectsToOutboundPersistentPeer(t *testing.T) {
  351. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  352. err := sw.Start()
  353. require.NoError(t, err)
  354. defer sw.Stop()
  355. // 1. simulate failure by closing connection
  356. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  357. rp.Start()
  358. defer rp.Stop()
  359. err = sw.AddPersistentPeers([]string{rp.Addr().String()})
  360. require.NoError(t, err)
  361. err = sw.DialPeerWithAddress(rp.Addr())
  362. require.Nil(t, err)
  363. require.NotNil(t, sw.Peers().Get(rp.ID()))
  364. p := sw.Peers().List()[0]
  365. p.(*peer).CloseConn()
  366. waitUntilSwitchHasAtLeastNPeers(sw, 1)
  367. assert.False(t, p.IsRunning()) // old peer instance
  368. assert.Equal(t, 1, sw.Peers().Size()) // new peer instance
  369. // 2. simulate first time dial failure
  370. rp = &remotePeer{
  371. PrivKey: ed25519.GenPrivKey(),
  372. Config: cfg,
  373. // Use different interface to prevent duplicate IP filter, this will break
  374. // beyond two peers.
  375. listenAddr: "127.0.0.1:0",
  376. }
  377. rp.Start()
  378. defer rp.Stop()
  379. conf := config.DefaultP2PConfig()
  380. conf.TestDialFail = true // will trigger a reconnect
  381. err = sw.addOutboundPeerWithConfig(rp.Addr(), conf)
  382. require.NotNil(t, err)
  383. // DialPeerWithAddres - sw.peerConfig resets the dialer
  384. waitUntilSwitchHasAtLeastNPeers(sw, 2)
  385. assert.Equal(t, 2, sw.Peers().Size())
  386. }
  387. func TestSwitchReconnectsToInboundPersistentPeer(t *testing.T) {
  388. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  389. err := sw.Start()
  390. require.NoError(t, err)
  391. defer sw.Stop()
  392. // 1. simulate failure by closing the connection
  393. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  394. rp.Start()
  395. defer rp.Stop()
  396. err = sw.AddPersistentPeers([]string{rp.Addr().String()})
  397. require.NoError(t, err)
  398. conn, err := rp.Dial(sw.NetAddress())
  399. require.NoError(t, err)
  400. time.Sleep(50 * time.Millisecond)
  401. require.NotNil(t, sw.Peers().Get(rp.ID()))
  402. conn.Close()
  403. waitUntilSwitchHasAtLeastNPeers(sw, 1)
  404. assert.Equal(t, 1, sw.Peers().Size())
  405. }
  406. func TestSwitchDialPeersAsync(t *testing.T) {
  407. if testing.Short() {
  408. return
  409. }
  410. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  411. err := sw.Start()
  412. require.NoError(t, err)
  413. defer sw.Stop()
  414. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  415. rp.Start()
  416. defer rp.Stop()
  417. err = sw.DialPeersAsync([]string{rp.Addr().String()})
  418. require.NoError(t, err)
  419. time.Sleep(dialRandomizerIntervalMilliseconds * time.Millisecond)
  420. require.NotNil(t, sw.Peers().Get(rp.ID()))
  421. }
  422. func waitUntilSwitchHasAtLeastNPeers(sw *Switch, n int) {
  423. for i := 0; i < 20; i++ {
  424. time.Sleep(250 * time.Millisecond)
  425. has := sw.Peers().Size()
  426. if has >= n {
  427. break
  428. }
  429. }
  430. }
  431. func TestSwitchFullConnectivity(t *testing.T) {
  432. switches := MakeConnectedSwitches(cfg, 3, initSwitchFunc, Connect2Switches)
  433. defer func() {
  434. for _, sw := range switches {
  435. sw.Stop()
  436. }
  437. }()
  438. for i, sw := range switches {
  439. if sw.Peers().Size() != 2 {
  440. t.Fatalf("Expected each switch to be connected to 2 other, but %d switch only connected to %d", sw.Peers().Size(), i)
  441. }
  442. }
  443. }
  444. func TestSwitchAcceptRoutine(t *testing.T) {
  445. cfg.MaxNumInboundPeers = 5
  446. // make switch
  447. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  448. err := sw.Start()
  449. require.NoError(t, err)
  450. defer sw.Stop()
  451. remotePeers := make([]*remotePeer, 0)
  452. assert.Equal(t, 0, sw.Peers().Size())
  453. // 1. check we connect up to MaxNumInboundPeers
  454. for i := 0; i < cfg.MaxNumInboundPeers; i++ {
  455. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  456. remotePeers = append(remotePeers, rp)
  457. rp.Start()
  458. c, err := rp.Dial(sw.NetAddress())
  459. require.NoError(t, err)
  460. // spawn a reading routine to prevent connection from closing
  461. go func(c net.Conn) {
  462. for {
  463. one := make([]byte, 1)
  464. _, err := c.Read(one)
  465. if err != nil {
  466. return
  467. }
  468. }
  469. }(c)
  470. }
  471. time.Sleep(10 * time.Millisecond)
  472. assert.Equal(t, cfg.MaxNumInboundPeers, sw.Peers().Size())
  473. // 2. check we close new connections if we already have MaxNumInboundPeers peers
  474. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  475. rp.Start()
  476. conn, err := rp.Dial(sw.NetAddress())
  477. require.NoError(t, err)
  478. // check conn is closed
  479. one := make([]byte, 1)
  480. conn.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
  481. _, err = conn.Read(one)
  482. assert.Equal(t, io.EOF, err)
  483. assert.Equal(t, cfg.MaxNumInboundPeers, sw.Peers().Size())
  484. rp.Stop()
  485. // stop remote peers
  486. for _, rp := range remotePeers {
  487. rp.Stop()
  488. }
  489. }
  490. type errorTransport struct {
  491. acceptErr error
  492. }
  493. func (et errorTransport) NetAddress() NetAddress {
  494. panic("not implemented")
  495. }
  496. func (et errorTransport) Accept(c peerConfig) (Peer, error) {
  497. return nil, et.acceptErr
  498. }
  499. func (errorTransport) Dial(NetAddress, peerConfig) (Peer, error) {
  500. panic("not implemented")
  501. }
  502. func (errorTransport) Cleanup(Peer) {
  503. panic("not implemented")
  504. }
  505. func TestSwitchAcceptRoutineErrorCases(t *testing.T) {
  506. sw := NewSwitch(cfg, errorTransport{ErrFilterTimeout{}})
  507. assert.NotPanics(t, func() {
  508. err := sw.Start()
  509. assert.NoError(t, err)
  510. sw.Stop()
  511. })
  512. sw = NewSwitch(cfg, errorTransport{ErrRejected{conn: nil, err: errors.New("filtered"), isFiltered: true}})
  513. assert.NotPanics(t, func() {
  514. err := sw.Start()
  515. assert.NoError(t, err)
  516. sw.Stop()
  517. })
  518. // TODO(melekes) check we remove our address from addrBook
  519. sw = NewSwitch(cfg, errorTransport{ErrTransportClosed{}})
  520. assert.NotPanics(t, func() {
  521. err := sw.Start()
  522. assert.NoError(t, err)
  523. sw.Stop()
  524. })
  525. }
  526. // mockReactor checks that InitPeer never called before RemovePeer. If that's
  527. // not true, InitCalledBeforeRemoveFinished will return true.
  528. type mockReactor struct {
  529. *BaseReactor
  530. // atomic
  531. removePeerInProgress uint32
  532. initCalledBeforeRemoveFinished uint32
  533. }
  534. func (r *mockReactor) RemovePeer(peer Peer, reason interface{}) {
  535. atomic.StoreUint32(&r.removePeerInProgress, 1)
  536. defer atomic.StoreUint32(&r.removePeerInProgress, 0)
  537. time.Sleep(100 * time.Millisecond)
  538. }
  539. func (r *mockReactor) InitPeer(peer Peer) Peer {
  540. if atomic.LoadUint32(&r.removePeerInProgress) == 1 {
  541. atomic.StoreUint32(&r.initCalledBeforeRemoveFinished, 1)
  542. }
  543. return peer
  544. }
  545. func (r *mockReactor) InitCalledBeforeRemoveFinished() bool {
  546. return atomic.LoadUint32(&r.initCalledBeforeRemoveFinished) == 1
  547. }
  548. // see stopAndRemovePeer
  549. func TestSwitchInitPeerIsNotCalledBeforeRemovePeer(t *testing.T) {
  550. // make reactor
  551. reactor := &mockReactor{}
  552. reactor.BaseReactor = NewBaseReactor("mockReactor", reactor)
  553. // make switch
  554. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", func(i int, sw *Switch) *Switch {
  555. sw.AddReactor("mock", reactor)
  556. return sw
  557. })
  558. err := sw.Start()
  559. require.NoError(t, err)
  560. defer sw.Stop()
  561. // add peer
  562. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  563. rp.Start()
  564. defer rp.Stop()
  565. _, err = rp.Dial(sw.NetAddress())
  566. require.NoError(t, err)
  567. // wait till the switch adds rp to the peer set
  568. time.Sleep(50 * time.Millisecond)
  569. // stop peer asynchronously
  570. go sw.StopPeerForError(sw.Peers().Get(rp.ID()), "test")
  571. // simulate peer reconnecting to us
  572. _, err = rp.Dial(sw.NetAddress())
  573. require.NoError(t, err)
  574. // wait till the switch adds rp to the peer set
  575. time.Sleep(50 * time.Millisecond)
  576. // make sure reactor.RemovePeer is finished before InitPeer is called
  577. assert.False(t, reactor.InitCalledBeforeRemoveFinished())
  578. }
  579. func BenchmarkSwitchBroadcast(b *testing.B) {
  580. s1, s2 := MakeSwitchPair(b, func(i int, sw *Switch) *Switch {
  581. // Make bar reactors of bar channels each
  582. sw.AddReactor("foo", NewTestReactor([]*conn.ChannelDescriptor{
  583. {ID: byte(0x00), Priority: 10},
  584. {ID: byte(0x01), Priority: 10},
  585. }, false))
  586. sw.AddReactor("bar", NewTestReactor([]*conn.ChannelDescriptor{
  587. {ID: byte(0x02), Priority: 10},
  588. {ID: byte(0x03), Priority: 10},
  589. }, false))
  590. return sw
  591. })
  592. defer s1.Stop()
  593. defer s2.Stop()
  594. // Allow time for goroutines to boot up
  595. time.Sleep(1 * time.Second)
  596. b.ResetTimer()
  597. numSuccess, numFailure := 0, 0
  598. // Send random message from foo channel to another
  599. for i := 0; i < b.N; i++ {
  600. chID := byte(i % 4)
  601. successChan := s1.Broadcast(chID, []byte("test data"))
  602. for s := range successChan {
  603. if s {
  604. numSuccess++
  605. } else {
  606. numFailure++
  607. }
  608. }
  609. }
  610. b.Logf("success: %v, failure: %v", numSuccess, numFailure)
  611. }
  612. type addrBookMock struct {
  613. addrs map[string]struct{}
  614. ourAddrs map[string]struct{}
  615. }
  616. var _ AddrBook = (*addrBookMock)(nil)
  617. func (book *addrBookMock) AddAddress(addr *NetAddress, src *NetAddress) error {
  618. book.addrs[addr.String()] = struct{}{}
  619. return nil
  620. }
  621. func (book *addrBookMock) AddOurAddress(addr *NetAddress) { book.ourAddrs[addr.String()] = struct{}{} }
  622. func (book *addrBookMock) OurAddress(addr *NetAddress) bool {
  623. _, ok := book.ourAddrs[addr.String()]
  624. return ok
  625. }
  626. func (book *addrBookMock) MarkGood(ID) {}
  627. func (book *addrBookMock) HasAddress(addr *NetAddress) bool {
  628. _, ok := book.addrs[addr.String()]
  629. return ok
  630. }
  631. func (book *addrBookMock) RemoveAddress(addr *NetAddress) {
  632. delete(book.addrs, addr.String())
  633. }
  634. func (book *addrBookMock) Save() {}