github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/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 particuar 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  func (s *Simulation) WaitTillHealthy(ctx context.Context, kadMinProxSize int) (ill map[enode.ID]*network.Kademlia, err error) {
    37  	// Prepare PeerPot map for checking Kademlia health
    38  	var ppmap map[string]*network.PeerPot
    39  	kademlias := s.kademlias()
    40  	addrs := make([][]byte, 0, len(kademlias))
    41  	for _, k := range kademlias {
    42  		addrs = append(addrs, k.BaseAddr())
    43  	}
    44  	ppmap = network.NewPeerPotMap(kadMinProxSize, addrs)
    45  
    46  	// Wait for healthy Kademlia on every node before checking files
    47  	ticker := time.NewTicker(200 * time.Millisecond)
    48  	defer ticker.Stop()
    49  
    50  	ill = make(map[enode.ID]*network.Kademlia)
    51  	for {
    52  		select {
    53  		case <-ctx.Done():
    54  			return ill, ctx.Err()
    55  		case <-ticker.C:
    56  			for k := range ill {
    57  				delete(ill, k)
    58  			}
    59  			log.Debug("kademlia health check", "addr count", len(addrs))
    60  			for id, k := range kademlias {
    61  				//PeerPot for this node
    62  				addr := common.Bytes2Hex(k.BaseAddr())
    63  				pp := ppmap[addr]
    64  				//call Healthy RPC
    65  				h := k.Healthy(pp)
    66  				//print info
    67  				log.Debug(k.String())
    68  				log.Debug("kademlia", "empty bins", pp.EmptyBins, "gotNN", h.GotNN, "knowNN", h.KnowNN, "full", h.Full)
    69  				log.Debug("kademlia", "health", h.GotNN && h.KnowNN && h.Full, "addr", hex.EncodeToString(k.BaseAddr()), "node", id)
    70  				log.Debug("kademlia", "ill condition", !h.GotNN || !h.Full, "addr", hex.EncodeToString(k.BaseAddr()), "node", id)
    71  				if !h.GotNN || !h.Full {
    72  					ill[id] = k
    73  				}
    74  			}
    75  			if len(ill) == 0 {
    76  				return nil, nil
    77  			}
    78  		}
    79  	}
    80  }
    81  
    82  // kademlias returns all Kademlia instances that are set
    83  // in simulation bucket.
    84  func (s *Simulation) kademlias() (ks map[enode.ID]*network.Kademlia) {
    85  	items := s.UpNodesItems(BucketKeyKademlia)
    86  	ks = make(map[enode.ID]*network.Kademlia, len(items))
    87  	for id, v := range items {
    88  		k, ok := v.(*network.Kademlia)
    89  		if !ok {
    90  			continue
    91  		}
    92  		ks[id] = k
    93  	}
    94  	return ks
    95  }