github.com/divan/go-ethereum@v1.8.14-0.20180820134928-1de9ada4016d/swarm/network/simulation/kademlia.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 "encoding/hex" 22 "time" 23 24 "github.com/ethereum/go-ethereum/p2p/discover" 25 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/log" 28 "github.com/ethereum/go-ethereum/swarm/network" 29 ) 30 31 // BucketKeyKademlia is the key to be used for storing the kademlia 32 // instance for particuar node, usually inside the ServiceFunc function. 33 var BucketKeyKademlia BucketKey = "kademlia" 34 35 // WaitTillHealthy is blocking until the health of all kademlias is true. 36 // If error is not nil, a map of kademlia that was found not healthy is returned. 37 func (s *Simulation) WaitTillHealthy(ctx context.Context, kadMinProxSize int) (ill map[discover.NodeID]*network.Kademlia, err error) { 38 // Prepare PeerPot map for checking Kademlia health 39 var ppmap map[string]*network.PeerPot 40 kademlias := s.kademlias() 41 addrs := make([][]byte, 0, len(kademlias)) 42 for _, k := range kademlias { 43 addrs = append(addrs, k.BaseAddr()) 44 } 45 ppmap = network.NewPeerPotMap(kadMinProxSize, addrs) 46 47 // Wait for healthy Kademlia on every node before checking files 48 ticker := time.NewTicker(200 * time.Millisecond) 49 defer ticker.Stop() 50 51 ill = make(map[discover.NodeID]*network.Kademlia) 52 for { 53 select { 54 case <-ctx.Done(): 55 return ill, ctx.Err() 56 case <-ticker.C: 57 for k := range ill { 58 delete(ill, k) 59 } 60 log.Debug("kademlia health check", "addr count", len(addrs)) 61 for id, k := range kademlias { 62 //PeerPot for this node 63 addr := common.Bytes2Hex(k.BaseAddr()) 64 pp := ppmap[addr] 65 //call Healthy RPC 66 h := k.Healthy(pp) 67 //print info 68 log.Debug(k.String()) 69 log.Debug("kademlia", "empty bins", pp.EmptyBins, "gotNN", h.GotNN, "knowNN", h.KnowNN, "full", h.Full) 70 log.Debug("kademlia", "health", h.GotNN && h.KnowNN && h.Full, "addr", hex.EncodeToString(k.BaseAddr()), "node", id) 71 log.Debug("kademlia", "ill condition", !h.GotNN || !h.Full, "addr", hex.EncodeToString(k.BaseAddr()), "node", id) 72 if !h.GotNN || !h.Full { 73 ill[id] = k 74 } 75 } 76 if len(ill) == 0 { 77 return nil, nil 78 } 79 } 80 } 81 } 82 83 // kademlias returns all Kademlia instances that are set 84 // in simulation bucket. 85 func (s *Simulation) kademlias() (ks map[discover.NodeID]*network.Kademlia) { 86 items := s.UpNodesItems(BucketKeyKademlia) 87 ks = make(map[discover.NodeID]*network.Kademlia, len(items)) 88 for id, v := range items { 89 k, ok := v.(*network.Kademlia) 90 if !ok { 91 continue 92 } 93 ks[id] = k 94 } 95 return ks 96 }