gitee.com/liu-zhao234568/cntest@v1.0.0/cmd/devp2p/nodeset.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU 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  // go-ethereum 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 General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"fmt"
    23  	"io/ioutil"
    24  	"os"
    25  	"sort"
    26  	"time"
    27  
    28  	"gitee.com/liu-zhao234568/cntest/common"
    29  	"gitee.com/liu-zhao234568/cntest/p2p/enode"
    30  )
    31  
    32  const jsonIndent = "    "
    33  
    34  // nodeSet is the nodes.json file format. It holds a set of node records
    35  // as a JSON object.
    36  type nodeSet map[enode.ID]nodeJSON
    37  
    38  type nodeJSON struct {
    39  	Seq uint64      `json:"seq"`
    40  	N   *enode.Node `json:"record"`
    41  
    42  	// The score tracks how many liveness checks were performed. It is incremented by one
    43  	// every time the node passes a check, and halved every time it doesn't.
    44  	Score int `json:"score,omitempty"`
    45  	// These two track the time of last successful contact.
    46  	FirstResponse time.Time `json:"firstResponse,omitempty"`
    47  	LastResponse  time.Time `json:"lastResponse,omitempty"`
    48  	// This one tracks the time of our last attempt to contact the node.
    49  	LastCheck time.Time `json:"lastCheck,omitempty"`
    50  }
    51  
    52  func loadNodesJSON(file string) nodeSet {
    53  	var nodes nodeSet
    54  	if err := common.LoadJSON(file, &nodes); err != nil {
    55  		exit(err)
    56  	}
    57  	return nodes
    58  }
    59  
    60  func writeNodesJSON(file string, nodes nodeSet) {
    61  	nodesJSON, err := json.MarshalIndent(nodes, "", jsonIndent)
    62  	if err != nil {
    63  		exit(err)
    64  	}
    65  	if file == "-" {
    66  		os.Stdout.Write(nodesJSON)
    67  		return
    68  	}
    69  	if err := ioutil.WriteFile(file, nodesJSON, 0644); err != nil {
    70  		exit(err)
    71  	}
    72  }
    73  
    74  // nodes returns the node records contained in the set.
    75  func (ns nodeSet) nodes() []*enode.Node {
    76  	result := make([]*enode.Node, 0, len(ns))
    77  	for _, n := range ns {
    78  		result = append(result, n.N)
    79  	}
    80  	// Sort by ID.
    81  	sort.Slice(result, func(i, j int) bool {
    82  		return bytes.Compare(result[i].ID().Bytes(), result[j].ID().Bytes()) < 0
    83  	})
    84  	return result
    85  }
    86  
    87  // add ensures the given nodes are present in the set.
    88  func (ns nodeSet) add(nodes ...*enode.Node) {
    89  	for _, n := range nodes {
    90  		v := ns[n.ID()]
    91  		v.N = n
    92  		v.Seq = n.Seq()
    93  		ns[n.ID()] = v
    94  	}
    95  }
    96  
    97  // topN returns the top n nodes by score as a new set.
    98  func (ns nodeSet) topN(n int) nodeSet {
    99  	if n >= len(ns) {
   100  		return ns
   101  	}
   102  
   103  	byscore := make([]nodeJSON, 0, len(ns))
   104  	for _, v := range ns {
   105  		byscore = append(byscore, v)
   106  	}
   107  	sort.Slice(byscore, func(i, j int) bool {
   108  		return byscore[i].Score >= byscore[j].Score
   109  	})
   110  	result := make(nodeSet, n)
   111  	for _, v := range byscore[:n] {
   112  		result[v.N.ID()] = v
   113  	}
   114  	return result
   115  }
   116  
   117  // verify performs integrity checks on the node set.
   118  func (ns nodeSet) verify() error {
   119  	for id, n := range ns {
   120  		if n.N.ID() != id {
   121  			return fmt.Errorf("invalid node %v: ID does not match ID %v in record", id, n.N.ID())
   122  		}
   123  		if n.N.Seq() != n.Seq {
   124  			return fmt.Errorf("invalid node %v: 'seq' does not match seq %d from record", id, n.N.Seq())
   125  		}
   126  	}
   127  	return nil
   128  }