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.

459 lines
12 KiB

  1. package pex
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. crypto "github.com/tendermint/tendermint/crypto"
  13. cmn "github.com/tendermint/tmlibs/common"
  14. "github.com/tendermint/tmlibs/log"
  15. "github.com/tendermint/tendermint/config"
  16. "github.com/tendermint/tendermint/p2p"
  17. "github.com/tendermint/tendermint/p2p/conn"
  18. )
  19. var (
  20. cfg *config.P2PConfig
  21. )
  22. func init() {
  23. cfg = config.DefaultP2PConfig()
  24. cfg.PexReactor = true
  25. cfg.AllowDuplicateIP = true
  26. }
  27. func TestPEXReactorBasic(t *testing.T) {
  28. r, book := createReactor(&PEXReactorConfig{})
  29. defer teardownReactor(book)
  30. assert.NotNil(t, r)
  31. assert.NotEmpty(t, r.GetChannels())
  32. }
  33. func TestPEXReactorAddRemovePeer(t *testing.T) {
  34. r, book := createReactor(&PEXReactorConfig{})
  35. defer teardownReactor(book)
  36. size := book.Size()
  37. peer := p2p.CreateRandomPeer(false)
  38. r.AddPeer(peer)
  39. assert.Equal(t, size+1, book.Size())
  40. r.RemovePeer(peer, "peer not available")
  41. outboundPeer := p2p.CreateRandomPeer(true)
  42. r.AddPeer(outboundPeer)
  43. assert.Equal(t, size+1, book.Size(), "outbound peers should not be added to the address book")
  44. r.RemovePeer(outboundPeer, "peer not available")
  45. }
  46. // --- FAIL: TestPEXReactorRunning (11.10s)
  47. // pex_reactor_test.go:411: expected all switches to be connected to at
  48. // least one peer (switches: 0 => {outbound: 1, inbound: 0}, 1 =>
  49. // {outbound: 0, inbound: 1}, 2 => {outbound: 0, inbound: 0}, )
  50. //
  51. // EXPLANATION: peers are getting rejected because in switch#addPeer we check
  52. // if any peer (who we already connected to) has the same IP. Even though local
  53. // peers have different IP addresses, they all have the same underlying remote
  54. // IP: 127.0.0.1.
  55. //
  56. func TestPEXReactorRunning(t *testing.T) {
  57. N := 3
  58. switches := make([]*p2p.Switch, N)
  59. // directory to store address books
  60. dir, err := ioutil.TempDir("", "pex_reactor")
  61. require.Nil(t, err)
  62. defer os.RemoveAll(dir) // nolint: errcheck
  63. books := make([]*addrBook, N)
  64. logger := log.TestingLogger()
  65. // create switches
  66. for i := 0; i < N; i++ {
  67. switches[i] = p2p.MakeSwitch(cfg, i, "testing", "123.123.123", func(i int, sw *p2p.Switch) *p2p.Switch {
  68. books[i] = NewAddrBook(filepath.Join(dir, fmt.Sprintf("addrbook%d.json", i)), false)
  69. books[i].SetLogger(logger.With("pex", i))
  70. sw.SetAddrBook(books[i])
  71. sw.SetLogger(logger.With("pex", i))
  72. r := NewPEXReactor(books[i], &PEXReactorConfig{})
  73. r.SetLogger(logger.With("pex", i))
  74. r.SetEnsurePeersPeriod(250 * time.Millisecond)
  75. sw.AddReactor("pex", r)
  76. return sw
  77. })
  78. }
  79. addOtherNodeAddrToAddrBook := func(switchIndex, otherSwitchIndex int) {
  80. addr := switches[otherSwitchIndex].NodeInfo().NetAddress()
  81. books[switchIndex].AddAddress(addr, addr)
  82. }
  83. addOtherNodeAddrToAddrBook(0, 1)
  84. addOtherNodeAddrToAddrBook(1, 0)
  85. addOtherNodeAddrToAddrBook(2, 1)
  86. for i, sw := range switches {
  87. sw.AddListener(p2p.NewDefaultListener("tcp", sw.NodeInfo().ListenAddr, true, logger.With("pex", i)))
  88. err := sw.Start() // start switch and reactors
  89. require.Nil(t, err)
  90. }
  91. assertPeersWithTimeout(t, switches, 10*time.Millisecond, 10*time.Second, N-1)
  92. // stop them
  93. for _, s := range switches {
  94. s.Stop()
  95. }
  96. }
  97. func TestPEXReactorReceive(t *testing.T) {
  98. r, book := createReactor(&PEXReactorConfig{})
  99. defer teardownReactor(book)
  100. peer := p2p.CreateRandomPeer(false)
  101. // we have to send a request to receive responses
  102. r.RequestAddrs(peer)
  103. size := book.Size()
  104. addrs := []*p2p.NetAddress{peer.NodeInfo().NetAddress()}
  105. msg := cdc.MustMarshalBinary(&pexAddrsMessage{Addrs: addrs})
  106. r.Receive(PexChannel, peer, msg)
  107. assert.Equal(t, size+1, book.Size())
  108. msg = cdc.MustMarshalBinary(&pexRequestMessage{})
  109. r.Receive(PexChannel, peer, msg) // should not panic.
  110. }
  111. func TestPEXReactorRequestMessageAbuse(t *testing.T) {
  112. r, book := createReactor(&PEXReactorConfig{})
  113. defer teardownReactor(book)
  114. sw := createSwitchAndAddReactors(r)
  115. sw.SetAddrBook(book)
  116. peer := newMockPeer()
  117. p2p.AddPeerToSwitch(sw, peer)
  118. assert.True(t, sw.Peers().Has(peer.ID()))
  119. id := string(peer.ID())
  120. msg := cdc.MustMarshalBinary(&pexRequestMessage{})
  121. // first time creates the entry
  122. r.Receive(PexChannel, peer, msg)
  123. assert.True(t, r.lastReceivedRequests.Has(id))
  124. assert.True(t, sw.Peers().Has(peer.ID()))
  125. // next time sets the last time value
  126. r.Receive(PexChannel, peer, msg)
  127. assert.True(t, r.lastReceivedRequests.Has(id))
  128. assert.True(t, sw.Peers().Has(peer.ID()))
  129. // third time is too many too soon - peer is removed
  130. r.Receive(PexChannel, peer, msg)
  131. assert.False(t, r.lastReceivedRequests.Has(id))
  132. assert.False(t, sw.Peers().Has(peer.ID()))
  133. }
  134. func TestPEXReactorAddrsMessageAbuse(t *testing.T) {
  135. r, book := createReactor(&PEXReactorConfig{})
  136. defer teardownReactor(book)
  137. sw := createSwitchAndAddReactors(r)
  138. sw.SetAddrBook(book)
  139. peer := newMockPeer()
  140. p2p.AddPeerToSwitch(sw, peer)
  141. assert.True(t, sw.Peers().Has(peer.ID()))
  142. id := string(peer.ID())
  143. // request addrs from the peer
  144. r.RequestAddrs(peer)
  145. assert.True(t, r.requestsSent.Has(id))
  146. assert.True(t, sw.Peers().Has(peer.ID()))
  147. addrs := []*p2p.NetAddress{peer.NodeInfo().NetAddress()}
  148. msg := cdc.MustMarshalBinary(&pexAddrsMessage{Addrs: addrs})
  149. // receive some addrs. should clear the request
  150. r.Receive(PexChannel, peer, msg)
  151. assert.False(t, r.requestsSent.Has(id))
  152. assert.True(t, sw.Peers().Has(peer.ID()))
  153. // receiving more addrs causes a disconnect
  154. r.Receive(PexChannel, peer, msg)
  155. assert.False(t, sw.Peers().Has(peer.ID()))
  156. }
  157. func TestPEXReactorUsesSeedsIfNeeded(t *testing.T) {
  158. // directory to store address books
  159. dir, err := ioutil.TempDir("", "pex_reactor")
  160. require.Nil(t, err)
  161. defer os.RemoveAll(dir) // nolint: errcheck
  162. // 1. create seed
  163. seed := p2p.MakeSwitch(
  164. cfg,
  165. 0,
  166. "127.0.0.1",
  167. "123.123.123",
  168. func(i int, sw *p2p.Switch) *p2p.Switch {
  169. book := NewAddrBook(filepath.Join(dir, "addrbook0.json"), false)
  170. book.SetLogger(log.TestingLogger())
  171. sw.SetAddrBook(book)
  172. sw.SetLogger(log.TestingLogger())
  173. r := NewPEXReactor(book, &PEXReactorConfig{})
  174. r.SetLogger(log.TestingLogger())
  175. sw.AddReactor("pex", r)
  176. return sw
  177. },
  178. )
  179. seed.AddListener(
  180. p2p.NewDefaultListener(
  181. "tcp",
  182. seed.NodeInfo().ListenAddr,
  183. true,
  184. log.TestingLogger(),
  185. ),
  186. )
  187. require.Nil(t, seed.Start())
  188. defer seed.Stop()
  189. // 2. create usual peer with only seed configured.
  190. peer := p2p.MakeSwitch(
  191. cfg,
  192. 1,
  193. "127.0.0.1",
  194. "123.123.123",
  195. func(i int, sw *p2p.Switch) *p2p.Switch {
  196. book := NewAddrBook(filepath.Join(dir, "addrbook1.json"), false)
  197. book.SetLogger(log.TestingLogger())
  198. sw.SetAddrBook(book)
  199. sw.SetLogger(log.TestingLogger())
  200. r := NewPEXReactor(
  201. book,
  202. &PEXReactorConfig{
  203. Seeds: []string{seed.NodeInfo().NetAddress().String()},
  204. },
  205. )
  206. r.SetLogger(log.TestingLogger())
  207. sw.AddReactor("pex", r)
  208. return sw
  209. },
  210. )
  211. require.Nil(t, peer.Start())
  212. defer peer.Stop()
  213. // 3. check that the peer connects to seed immediately
  214. assertPeersWithTimeout(t, []*p2p.Switch{peer}, 10*time.Millisecond, 3*time.Second, 1)
  215. }
  216. func TestPEXReactorCrawlStatus(t *testing.T) {
  217. pexR, book := createReactor(&PEXReactorConfig{SeedMode: true})
  218. defer teardownReactor(book)
  219. // Seed/Crawler mode uses data from the Switch
  220. sw := createSwitchAndAddReactors(pexR)
  221. sw.SetAddrBook(book)
  222. // Create a peer, add it to the peer set and the addrbook.
  223. peer := p2p.CreateRandomPeer(false)
  224. p2p.AddPeerToSwitch(pexR.Switch, peer)
  225. addr1 := peer.NodeInfo().NetAddress()
  226. pexR.book.AddAddress(addr1, addr1)
  227. // Add a non-connected address to the book.
  228. _, addr2 := p2p.CreateRoutableAddr()
  229. pexR.book.AddAddress(addr2, addr1)
  230. // Get some peerInfos to crawl
  231. peerInfos := pexR.getPeersToCrawl()
  232. // Make sure it has the proper number of elements
  233. assert.Equal(t, 2, len(peerInfos))
  234. // TODO: test
  235. }
  236. func TestPEXReactorDoesNotAddPrivatePeersToAddrBook(t *testing.T) {
  237. peer := p2p.CreateRandomPeer(false)
  238. pexR, book := createReactor(&PEXReactorConfig{PrivatePeerIDs: []string{string(peer.NodeInfo().ID)}})
  239. defer teardownReactor(book)
  240. // we have to send a request to receive responses
  241. pexR.RequestAddrs(peer)
  242. size := book.Size()
  243. addrs := []*p2p.NetAddress{peer.NodeInfo().NetAddress()}
  244. msg := cdc.MustMarshalBinary(&pexAddrsMessage{Addrs: addrs})
  245. pexR.Receive(PexChannel, peer, msg)
  246. assert.Equal(t, size, book.Size())
  247. pexR.AddPeer(peer)
  248. assert.Equal(t, size, book.Size())
  249. }
  250. func TestPEXReactorDialPeer(t *testing.T) {
  251. pexR, book := createReactor(&PEXReactorConfig{})
  252. defer teardownReactor(book)
  253. sw := createSwitchAndAddReactors(pexR)
  254. sw.SetAddrBook(book)
  255. peer := newMockPeer()
  256. addr := peer.NodeInfo().NetAddress()
  257. assert.Equal(t, 0, pexR.AttemptsToDial(addr))
  258. // 1st unsuccessful attempt
  259. pexR.dialPeer(addr)
  260. assert.Equal(t, 1, pexR.AttemptsToDial(addr))
  261. // 2nd unsuccessful attempt
  262. pexR.dialPeer(addr)
  263. // must be skipped because it is too early
  264. assert.Equal(t, 1, pexR.AttemptsToDial(addr))
  265. if !testing.Short() {
  266. time.Sleep(3 * time.Second)
  267. // 3rd attempt
  268. pexR.dialPeer(addr)
  269. assert.Equal(t, 2, pexR.AttemptsToDial(addr))
  270. }
  271. }
  272. type mockPeer struct {
  273. *cmn.BaseService
  274. pubKey crypto.PubKey
  275. addr *p2p.NetAddress
  276. outbound, persistent bool
  277. }
  278. func newMockPeer() mockPeer {
  279. _, netAddr := p2p.CreateRoutableAddr()
  280. mp := mockPeer{
  281. addr: netAddr,
  282. pubKey: crypto.GenPrivKeyEd25519().PubKey(),
  283. }
  284. mp.BaseService = cmn.NewBaseService(nil, "MockPeer", mp)
  285. mp.Start()
  286. return mp
  287. }
  288. func (mp mockPeer) ID() p2p.ID { return mp.addr.ID }
  289. func (mp mockPeer) IsOutbound() bool { return mp.outbound }
  290. func (mp mockPeer) IsPersistent() bool { return mp.persistent }
  291. func (mp mockPeer) NodeInfo() p2p.NodeInfo {
  292. return p2p.NodeInfo{
  293. ID: mp.addr.ID,
  294. ListenAddr: mp.addr.DialString(),
  295. }
  296. }
  297. func (mp mockPeer) RemoteIP() net.IP { return net.ParseIP("127.0.0.1") }
  298. func (mp mockPeer) Status() conn.ConnectionStatus { return conn.ConnectionStatus{} }
  299. func (mp mockPeer) Send(byte, []byte) bool { return false }
  300. func (mp mockPeer) TrySend(byte, []byte) bool { return false }
  301. func (mp mockPeer) Set(string, interface{}) {}
  302. func (mp mockPeer) Get(string) interface{} { return nil }
  303. func assertPeersWithTimeout(
  304. t *testing.T,
  305. switches []*p2p.Switch,
  306. checkPeriod, timeout time.Duration,
  307. nPeers int,
  308. ) {
  309. var (
  310. ticker = time.NewTicker(checkPeriod)
  311. remaining = timeout
  312. )
  313. for {
  314. select {
  315. case <-ticker.C:
  316. // check peers are connected
  317. allGood := true
  318. for _, s := range switches {
  319. outbound, inbound, _ := s.NumPeers()
  320. if outbound+inbound < nPeers {
  321. allGood = false
  322. }
  323. }
  324. remaining -= checkPeriod
  325. if remaining < 0 {
  326. remaining = 0
  327. }
  328. if allGood {
  329. return
  330. }
  331. case <-time.After(remaining):
  332. numPeersStr := ""
  333. for i, s := range switches {
  334. outbound, inbound, _ := s.NumPeers()
  335. numPeersStr += fmt.Sprintf("%d => {outbound: %d, inbound: %d}, ", i, outbound, inbound)
  336. }
  337. t.Errorf(
  338. "expected all switches to be connected to at least one peer (switches: %s)",
  339. numPeersStr,
  340. )
  341. return
  342. }
  343. }
  344. }
  345. func createReactor(conf *PEXReactorConfig) (r *PEXReactor, book *addrBook) {
  346. // directory to store address book
  347. dir, err := ioutil.TempDir("", "pex_reactor")
  348. if err != nil {
  349. panic(err)
  350. }
  351. book = NewAddrBook(filepath.Join(dir, "addrbook.json"), true)
  352. book.SetLogger(log.TestingLogger())
  353. r = NewPEXReactor(book, conf)
  354. r.SetLogger(log.TestingLogger())
  355. return
  356. }
  357. func teardownReactor(book *addrBook) {
  358. err := os.RemoveAll(filepath.Dir(book.FilePath()))
  359. if err != nil {
  360. panic(err)
  361. }
  362. }
  363. func createSwitchAndAddReactors(reactors ...p2p.Reactor) *p2p.Switch {
  364. sw := p2p.MakeSwitch(cfg, 0, "127.0.0.1", "123.123.123", func(i int, sw *p2p.Switch) *p2p.Switch { return sw })
  365. sw.SetLogger(log.TestingLogger())
  366. for _, r := range reactors {
  367. sw.AddReactor(r.String(), r)
  368. r.SetSwitch(sw)
  369. }
  370. return sw
  371. }