hive_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package network
  17. import (
  18. "io/ioutil"
  19. "os"
  20. "testing"
  21. "time"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. "github.com/ethereum/go-ethereum/p2p"
  24. p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
  25. "github.com/ethereum/go-ethereum/swarm/state"
  26. )
  27. func newHiveTester(params *HiveParams, n int, store state.Store) (*bzzTester, *Hive, error) {
  28. // setup
  29. prvkey, err := crypto.GenerateKey()
  30. if err != nil {
  31. return nil, nil, err
  32. }
  33. addr := PrivateKeyToBzzKey(prvkey)
  34. to := NewKademlia(addr, NewKadParams())
  35. pp := NewHive(params, to, store) // hive
  36. bt, err := newBzzBaseTester(n, prvkey, DiscoverySpec, pp.Run)
  37. if err != nil {
  38. return nil, nil, err
  39. }
  40. return bt, pp, nil
  41. }
  42. // TestRegisterAndConnect verifies that the protocol runs successfully
  43. // and that the peer connection exists afterwards
  44. func TestRegisterAndConnect(t *testing.T) {
  45. params := NewHiveParams()
  46. s, pp, err := newHiveTester(params, 1, nil)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. node := s.Nodes[0]
  51. raddr := NewAddr(node)
  52. pp.Register(raddr)
  53. // start the hive
  54. err = pp.Start(s.Server)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. defer pp.Stop()
  59. // both hive connect and disconect check have time delays
  60. // therefore we need to verify that peer is connected
  61. // so that we are sure that the disconnect timeout doesn't complete
  62. // before the hive connect method is run at least once
  63. timeout := time.After(time.Second)
  64. for {
  65. select {
  66. case <-timeout:
  67. t.Fatalf("expected connection")
  68. default:
  69. }
  70. i := 0
  71. pp.Kademlia.EachConn(nil, 256, func(addr *Peer, po int) bool {
  72. i++
  73. return true
  74. })
  75. if i > 0 {
  76. break
  77. }
  78. time.Sleep(time.Millisecond)
  79. }
  80. // check that the connection actually exists
  81. // the timeout error means no disconnection events
  82. // were received within the a certain timeout
  83. err = s.TestDisconnected(&p2ptest.Disconnect{
  84. Peer: s.Nodes[0].ID(),
  85. Error: nil,
  86. })
  87. if err == nil || err.Error() != "timed out waiting for peers to disconnect" {
  88. t.Fatalf("expected no disconnection event")
  89. }
  90. }
  91. // TestHiveStatePersistance creates a protocol simulation with n peers for a node
  92. // After protocols complete, the node is shut down and the state is stored.
  93. // Another simulation is created, where 0 nodes are created, but where the stored state is passed
  94. // The test succeeds if all the peers from the stored state are known after the protocols of the
  95. // second simulation have completed
  96. //
  97. // Actual connectivity is not in scope for this test, as the peers loaded from state are not known to
  98. // the simulation; the test only verifies that the peers are known to the node
  99. func TestHiveStatePersistance(t *testing.T) {
  100. dir, err := ioutil.TempDir("", "hive_test_store")
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. defer os.RemoveAll(dir)
  105. const peersCount = 5
  106. startHive := func(t *testing.T, dir string) (h *Hive) {
  107. store, err := state.NewDBStore(dir)
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. params := NewHiveParams()
  112. params.Discovery = false
  113. prvkey, err := crypto.GenerateKey()
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. h = NewHive(params, NewKademlia(PrivateKeyToBzzKey(prvkey), NewKadParams()), store)
  118. s := p2ptest.NewProtocolTester(prvkey, 0, func(p *p2p.Peer, rw p2p.MsgReadWriter) error { return nil })
  119. if err := h.Start(s.Server); err != nil {
  120. t.Fatal(err)
  121. }
  122. return h
  123. }
  124. h1 := startHive(t, dir)
  125. peers := make(map[string]bool)
  126. for i := 0; i < peersCount; i++ {
  127. raddr := RandomAddr()
  128. h1.Register(raddr)
  129. peers[raddr.String()] = true
  130. }
  131. if err = h1.Stop(); err != nil {
  132. t.Fatal(err)
  133. }
  134. // start the hive and check that we know of all expected peers
  135. h2 := startHive(t, dir)
  136. defer func() {
  137. if err = h2.Stop(); err != nil {
  138. t.Fatal(err)
  139. }
  140. }()
  141. i := 0
  142. h2.Kademlia.EachAddr(nil, 256, func(addr *BzzAddr, po int) bool {
  143. delete(peers, addr.String())
  144. i++
  145. return true
  146. })
  147. if i != peersCount {
  148. t.Fatalf("invalid number of entries: got %v, want %v", i, peersCount)
  149. }
  150. if len(peers) != 0 {
  151. t.Fatalf("%d peers left over: %v", len(peers), peers)
  152. }
  153. }