github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/chain/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/common"
    25  	"github.com/ethereum/go-ethereum/log"
    26  	"github.com/ethereum/go-ethereum/p2p/enode"
    27  	"github.com/ethereum/go-ethereum/swarm/network"
    28  )
    29  
    30  // BucketKeyKademlia is the key to be used for storing the kademlia
    31  // instance for particular node, usually inside the ServiceFunc function.
    32  var BucketKeyKademlia BucketKey = "kademlia"
    33  
    34  // WaitTillHealthy is blocking until the health of all kademlias is true.
    35  // If error is not nil, a map of kademlia that was found not healthy is returned.
    36  // TODO: Check correctness since change in kademlia depth calculation logic
    37  func (s *Simulation) WaitTillHealthy(ctx context.Context) (ill map[enode.ID]*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  	// TODO verify that all kademlias have same params
    43  	for _, k := range kademlias {
    44  		addrs = append(addrs, k.BaseAddr())
    45  	}
    46  	ppmap = network.NewPeerPotMap(s.neighbourhoodSize, addrs)
    47  
    48  	// Wait for healthy Kademlia on every node before checking files
    49  	ticker := time.NewTicker(200 * time.Millisecond)
    50  	defer ticker.Stop()
    51  
    52  	ill = make(map[enode.ID]*network.Kademlia)
    53  	for {
    54  		select {
    55  		case <-ctx.Done():
    56  			return ill, ctx.Err()
    57  		case <-ticker.C:
    58  			for k := range ill {
    59  				delete(ill, k)
    60  			}
    61  			log.Debug("kademlia health check", "addr count", len(addrs), "kad len", len(kademlias))
    62  			for id, k := range kademlias {
    63  				//PeerPot for this node
    64  				addr := common.Bytes2Hex(k.BaseAddr())
    65  				pp := ppmap[addr]
    66  				//call Healthy RPC
    67  				h := k.GetHealthInfo(pp)
    68  				//print info
    69  				log.Debug(k.String())
    70  				log.Debug("kademlia", "connectNN", h.ConnectNN, "knowNN", h.KnowNN)
    71  				log.Debug("kademlia", "health", h.ConnectNN && h.KnowNN, "addr", hex.EncodeToString(k.BaseAddr()), "node", id)
    72  				log.Debug("kademlia", "ill condition", !h.ConnectNN, "addr", hex.EncodeToString(k.BaseAddr()), "node", id)
    73  				if !h.Healthy() {
    74  					ill[id] = k
    75  				}
    76  			}
    77  			if len(ill) == 0 {
    78  				return nil, nil
    79  			}
    80  		}
    81  	}
    82  }
    83  
    84  // kademlias returns all Kademlia instances that are set
    85  // in simulation bucket.
    86  func (s *Simulation) kademlias() (ks map[enode.ID]*network.Kademlia) {
    87  	items := s.UpNodesItems(BucketKeyKademlia)
    88  	log.Debug("kademlia len items", "len", len(items))
    89  	ks = make(map[enode.ID]*network.Kademlia, len(items))
    90  	for id, v := range items {
    91  		k, ok := v.(*network.Kademlia)
    92  		if !ok {
    93  			continue
    94  		}
    95  		ks[id] = k
    96  	}
    97  	return ks
    98  }