github.com/nitinawathare/ethereumassignment3@v0.0.0-20211021213010-f07344c2b868/go-ethereum/swarm/network/hive_test.go (about) 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 17 package network 18 19 import ( 20 "io/ioutil" 21 "os" 22 "testing" 23 "time" 24 25 "github.com/ethereum/go-ethereum/crypto" 26 "github.com/ethereum/go-ethereum/p2p" 27 p2ptest "github.com/ethereum/go-ethereum/p2p/testing" 28 "github.com/ethereum/go-ethereum/swarm/state" 29 ) 30 31 func newHiveTester(params *HiveParams, n int, store state.Store) (*bzzTester, *Hive, error) { 32 // setup 33 prvkey, err := crypto.GenerateKey() 34 if err != nil { 35 return nil, nil, err 36 } 37 addr := PrivateKeyToBzzKey(prvkey) 38 to := NewKademlia(addr, NewKadParams()) 39 pp := NewHive(params, to, store) // hive 40 41 bt, err := newBzzBaseTester(n, prvkey, DiscoverySpec, pp.Run) 42 if err != nil { 43 return nil, nil, err 44 } 45 return bt, pp, nil 46 } 47 48 // TestRegisterAndConnect verifies that the protocol runs successfully 49 // and that the peer connection exists afterwards 50 func TestRegisterAndConnect(t *testing.T) { 51 params := NewHiveParams() 52 s, pp, err := newHiveTester(params, 1, nil) 53 if err != nil { 54 t.Fatal(err) 55 } 56 57 node := s.Nodes[0] 58 raddr := NewAddr(node) 59 pp.Register(raddr) 60 61 // start the hive 62 err = pp.Start(s.Server) 63 if err != nil { 64 t.Fatal(err) 65 } 66 defer pp.Stop() 67 68 // both hive connect and disconect check have time delays 69 // therefore we need to verify that peer is connected 70 // so that we are sure that the disconnect timeout doesn't complete 71 // before the hive connect method is run at least once 72 timeout := time.After(time.Second) 73 for { 74 select { 75 case <-timeout: 76 t.Fatalf("expected connection") 77 default: 78 } 79 i := 0 80 pp.Kademlia.EachConn(nil, 256, func(addr *Peer, po int) bool { 81 i++ 82 return true 83 }) 84 if i > 0 { 85 break 86 } 87 time.Sleep(time.Millisecond) 88 } 89 90 // check that the connection actually exists 91 // the timeout error means no disconnection events 92 // were received within the a certain timeout 93 err = s.TestDisconnected(&p2ptest.Disconnect{ 94 Peer: s.Nodes[0].ID(), 95 Error: nil, 96 }) 97 98 if err == nil || err.Error() != "timed out waiting for peers to disconnect" { 99 t.Fatalf("expected no disconnection event") 100 } 101 } 102 103 // TestHiveStatePersistance creates a protocol simulation with n peers for a node 104 // After protocols complete, the node is shut down and the state is stored. 105 // Another simulation is created, where 0 nodes are created, but where the stored state is passed 106 // The test succeeds if all the peers from the stored state are known after the protocols of the 107 // second simulation have completed 108 // 109 // Actual connectivity is not in scope for this test, as the peers loaded from state are not known to 110 // the simulation; the test only verifies that the peers are known to the node 111 func TestHiveStatePersistance(t *testing.T) { 112 dir, err := ioutil.TempDir("", "hive_test_store") 113 if err != nil { 114 t.Fatal(err) 115 } 116 defer os.RemoveAll(dir) 117 118 const peersCount = 5 119 120 startHive := func(t *testing.T, dir string) (h *Hive) { 121 store, err := state.NewDBStore(dir) 122 if err != nil { 123 t.Fatal(err) 124 } 125 126 params := NewHiveParams() 127 params.Discovery = false 128 129 prvkey, err := crypto.GenerateKey() 130 if err != nil { 131 t.Fatal(err) 132 } 133 134 h = NewHive(params, NewKademlia(PrivateKeyToBzzKey(prvkey), NewKadParams()), store) 135 s := p2ptest.NewProtocolTester(prvkey, 0, func(p *p2p.Peer, rw p2p.MsgReadWriter) error { return nil }) 136 137 if err := h.Start(s.Server); err != nil { 138 t.Fatal(err) 139 } 140 return h 141 } 142 143 h1 := startHive(t, dir) 144 peers := make(map[string]bool) 145 for i := 0; i < peersCount; i++ { 146 raddr := RandomAddr() 147 h1.Register(raddr) 148 peers[raddr.String()] = true 149 } 150 if err = h1.Stop(); err != nil { 151 t.Fatal(err) 152 } 153 154 // start the hive and check that we know of all expected peers 155 h2 := startHive(t, dir) 156 defer func() { 157 if err = h2.Stop(); err != nil { 158 t.Fatal(err) 159 } 160 }() 161 162 i := 0 163 h2.Kademlia.EachAddr(nil, 256, func(addr *BzzAddr, po int) bool { 164 delete(peers, addr.String()) 165 i++ 166 return true 167 }) 168 if i != peersCount { 169 t.Fatalf("invalid number of entries: got %v, want %v", i, peersCount) 170 } 171 if len(peers) != 0 { 172 t.Fatalf("%d peers left over: %v", len(peers), peers) 173 } 174 }