github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/chain/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 }