github.com/nitinawathare/ethereumassignment3@v0.0.0-20211021213010-f07344c2b868/go-ethereum/swarm/network/simulation/kademlia_test.go (about) 1 // Copyright 2018 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 simulation 18 19 import ( 20 "context" 21 "sync" 22 "testing" 23 "time" 24 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/log" 27 "github.com/ethereum/go-ethereum/node" 28 "github.com/ethereum/go-ethereum/p2p/simulations/adapters" 29 "github.com/ethereum/go-ethereum/swarm/network" 30 ) 31 32 /* 33 TestWaitTillHealthy tests that we indeed get a healthy network after we wait for it. 34 For this to be tested, a bit of a snake tail bite needs to happen: 35 * First we create a first simulation 36 * Run it as nodes connected in a ring 37 * Wait until the network is healthy 38 * Then we create a snapshot 39 * With this snapshot we create a new simulation 40 * This simulation is expected to have a healthy configuration, as it uses the snapshot 41 * Thus we just iterate all nodes and check that their kademlias are healthy 42 * If all kademlias are healthy, the test succeeded, otherwise it failed 43 */ 44 func TestWaitTillHealthy(t *testing.T) { 45 46 testNodesNum := 10 47 48 // create the first simulation 49 sim := New(createSimServiceMap(true)) 50 51 // connect and... 52 nodeIDs, err := sim.AddNodesAndConnectRing(testNodesNum) 53 if err != nil { 54 t.Fatal(err) 55 } 56 57 // array of all overlay addresses 58 var addrs [][]byte 59 // iterate once to be able to build the peer map 60 for _, node := range nodeIDs { 61 //get the kademlia overlay address from this ID 62 a := node.Bytes() 63 //append it to the array of all overlay addresses 64 addrs = append(addrs, a) 65 } 66 // build a PeerPot only once 67 pp := network.NewPeerPotMap(network.NewKadParams().NeighbourhoodSize, addrs) 68 69 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second) 70 defer cancel() 71 72 // ...wait until healthy 73 ill, err := sim.WaitTillHealthy(ctx) 74 if err != nil { 75 for id, kad := range ill { 76 t.Log("Node", id) 77 t.Log(kad.String()) 78 } 79 t.Fatal(err) 80 } 81 82 // now create a snapshot of this network 83 snap, err := sim.Net.Snapshot() 84 if err != nil { 85 t.Fatal(err) 86 } 87 88 // close the initial simulation 89 sim.Close() 90 // create a control simulation 91 controlSim := New(createSimServiceMap(false)) 92 defer controlSim.Close() 93 94 // load the snapshot into this control simulation 95 err = controlSim.Net.Load(snap) 96 if err != nil { 97 t.Fatal(err) 98 } 99 _, err = controlSim.WaitTillHealthy(ctx) 100 if err != nil { 101 t.Fatal(err) 102 } 103 104 for _, node := range nodeIDs { 105 // ...get its kademlia 106 item, ok := controlSim.NodeItem(node, BucketKeyKademlia) 107 if !ok { 108 t.Fatal("No kademlia bucket item") 109 } 110 kad := item.(*network.Kademlia) 111 // get its base address 112 kid := common.Bytes2Hex(kad.BaseAddr()) 113 114 //get the health info 115 info := kad.GetHealthInfo(pp[kid]) 116 log.Trace("Health info", "info", info) 117 // check that it is healthy 118 healthy := info.Healthy() 119 if !healthy { 120 t.Fatalf("Expected node %v of control simulation to be healthy, but it is not, unhealthy kademlias: %v", node, kad.String()) 121 } 122 } 123 } 124 125 // createSimServiceMap returns the services map 126 // this function will create the sim services with or without discovery enabled 127 // based on the flag passed 128 func createSimServiceMap(discovery bool) map[string]ServiceFunc { 129 return map[string]ServiceFunc{ 130 "bzz": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) { 131 addr := network.NewAddr(ctx.Config.Node()) 132 hp := network.NewHiveParams() 133 hp.Discovery = discovery 134 config := &network.BzzConfig{ 135 OverlayAddr: addr.Over(), 136 UnderlayAddr: addr.Under(), 137 HiveParams: hp, 138 } 139 kad := network.NewKademlia(addr.Over(), network.NewKadParams()) 140 // store kademlia in node's bucket under BucketKeyKademlia 141 // so that it can be found by WaitTillHealthy method. 142 b.Store(BucketKeyKademlia, kad) 143 return network.NewBzz(config, kad, nil, nil, nil), nil, nil 144 }, 145 } 146 } 147 148 // TestWaitTillSnapshotRecreated tests that we indeed have a network 149 // configuration specified in the snapshot file, after we wait for it. 150 // 151 // First we create a first simulation 152 // Run it as nodes connected in a ring 153 // Wait until the network is healthy 154 // Then we create a snapshot 155 // With this snapshot we create a new simulation 156 // Call WaitTillSnapshotRecreated() function and wait until it returns 157 // Iterate the nodes and check if all the connections are successfully recreated 158 func TestWaitTillSnapshotRecreated(t *testing.T) { 159 var err error 160 sim := New(createSimServiceMap(true)) 161 _, err = sim.AddNodesAndConnectRing(16) 162 if err != nil { 163 t.Fatal(err) 164 } 165 ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) 166 defer cancel() 167 _, err = sim.WaitTillHealthy(ctx) 168 if err != nil { 169 t.Fatal(err) 170 } 171 172 originalConnections := sim.getActualConnections() 173 snap, err := sim.Net.Snapshot() 174 sim.Close() 175 if err != nil { 176 t.Fatal(err) 177 } 178 179 controlSim := New(createSimServiceMap(false)) 180 defer controlSim.Close() 181 err = controlSim.Net.Load(snap) 182 if err != nil { 183 t.Fatal(err) 184 } 185 err = controlSim.WaitTillSnapshotRecreated(ctx, snap) 186 if err != nil { 187 t.Fatal(err) 188 } 189 controlConnections := controlSim.getActualConnections() 190 191 for _, c := range originalConnections { 192 if !exist(controlConnections, c) { 193 t.Fatal("connection was not recreated") 194 } 195 } 196 } 197 198 // exist returns true if val is found in arr 199 func exist(arr []uint64, val uint64) bool { 200 for _, c := range arr { 201 if c == val { 202 return true 203 } 204 } 205 return false 206 } 207 208 func TestRemoveDuplicatesAndSingletons(t *testing.T) { 209 singletons := []uint64{ 210 0x3c127c6f6cb026b0, 211 0x0f45190d72e71fc5, 212 0xb0184c02449e0bb6, 213 0xa85c7b84239c54d3, 214 0xe3b0c44298fc1c14, 215 0x9afbf4c8996fb924, 216 0x27ae41e4649b934c, 217 0xa495991b7852b855, 218 } 219 220 doubles := []uint64{ 221 0x1b879f878de7fc7a, 222 0xc6791470521bdab4, 223 0xdd34b0ee39bbccc6, 224 0x4d904fbf0f31da10, 225 0x6403c2560432c8f8, 226 0x18954e33cf3ad847, 227 0x90db00e98dc7a8a6, 228 0x92886b0dfcc1809b, 229 } 230 231 var arr []uint64 232 arr = append(arr, doubles...) 233 arr = append(arr, singletons...) 234 arr = append(arr, doubles...) 235 arr = removeDuplicatesAndSingletons(arr) 236 237 for _, i := range singletons { 238 if exist(arr, i) { 239 t.Fatalf("singleton not removed: %d", i) 240 } 241 } 242 243 for _, i := range doubles { 244 if !exist(arr, i) { 245 t.Fatalf("wrong value removed: %d", i) 246 } 247 } 248 249 for j := 0; j < len(doubles); j++ { 250 v := doubles[j] + singletons[j] 251 if exist(arr, v) { 252 t.Fatalf("non-existing value found, index: %d", j) 253 } 254 } 255 } 256 257 func TestIsAllDeployed(t *testing.T) { 258 a := []uint64{ 259 0x3c127c6f6cb026b0, 260 0x0f45190d72e71fc5, 261 0xb0184c02449e0bb6, 262 0xa85c7b84239c54d3, 263 0xe3b0c44298fc1c14, 264 0x9afbf4c8996fb924, 265 0x27ae41e4649b934c, 266 0xa495991b7852b855, 267 } 268 269 b := []uint64{ 270 0x1b879f878de7fc7a, 271 0xc6791470521bdab4, 272 0xdd34b0ee39bbccc6, 273 0x4d904fbf0f31da10, 274 0x6403c2560432c8f8, 275 0x18954e33cf3ad847, 276 0x90db00e98dc7a8a6, 277 0x92886b0dfcc1809b, 278 } 279 280 var c []uint64 281 c = append(c, a...) 282 c = append(c, b...) 283 284 if !isAllDeployed(a, c) { 285 t.Fatal("isAllDeployed failed") 286 } 287 288 if !isAllDeployed(b, c) { 289 t.Fatal("isAllDeployed failed") 290 } 291 292 if isAllDeployed(c, a) { 293 t.Fatal("isAllDeployed failed: false positive") 294 } 295 296 if isAllDeployed(c, b) { 297 t.Fatal("isAllDeployed failed: false positive") 298 } 299 300 c = c[2:] 301 302 if isAllDeployed(a, c) { 303 t.Fatal("isAllDeployed failed: false positive") 304 } 305 306 if !isAllDeployed(b, c) { 307 t.Fatal("isAllDeployed failed") 308 } 309 }