github.com/ethw3/go-ethereuma@v0.0.0-20221013053120-c14602a4c23c/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  	"os"
    24  	"sort"
    25  	"time"
    26  
    27  	"github.com/ethw3/go-ethereuma/common"
    28  	"github.com/ethw3/go-ethereuma/p2p/enode"
    29  )
    30  
    31  const jsonIndent = "    "
    32  
    33  // nodeSet is the nodes.json file format. It holds a set of node records
    34  // as a JSON object.
    35  type nodeSet map[enode.ID]nodeJSON
    36  
    37  type nodeJSON struct {
    38  	Seq uint64      `json:"seq"`
    39  	N   *enode.Node `json:"record"`
    40  
    41  	// The score tracks how many liveness checks were performed. It is incremented by one
    42  	// every time the node passes a check, and halved every time it doesn't.
    43  	Score int `json:"score,omitempty"`
    44  	// These two track the time of last successful contact.
    45  	FirstResponse time.Time `json:"firstResponse,omitempty"`
    46  	LastResponse  time.Time `json:"lastResponse,omitempty"`
    47  	// This one tracks the time of our last attempt to contact the node.
    48  	LastCheck time.Time `json:"lastCheck,omitempty"`
    49  }
    50  
    51  func loadNodesJSON(file string) nodeSet {
    52  	var nodes nodeSet
    53  	if err := common.LoadJSON(file, &nodes); err != nil {
    54  		exit(err)
    55  	}
    56  	return nodes
    57  }
    58  
    59  func writeNodesJSON(file string, nodes nodeSet) {
    60  	nodesJSON, err := json.MarshalIndent(nodes, "", jsonIndent)
    61  	if err != nil {
    62  		exit(err)
    63  	}
    64  	if file == "-" {
    65  		os.Stdout.Write(nodesJSON)
    66  		return
    67  	}
    68  	if err := os.WriteFile(file, nodesJSON, 0644); err != nil {
    69  		exit(err)
    70  	}
    71  }
    72  
    73  // nodes returns the node records contained in the set.
    74  func (ns nodeSet) nodes() []*enode.Node {
    75  	result := make([]*enode.Node, 0, len(ns))
    76  	for _, n := range ns {
    77  		result = append(result, n.N)
    78  	}
    79  	// Sort by ID.
    80  	sort.Slice(result, func(i, j int) bool {
    81  		return bytes.Compare(result[i].ID().Bytes(), result[j].ID().Bytes()) < 0
    82  	})
    83  	return result
    84  }
    85  
    86  // add ensures the given nodes are present in the set.
    87  func (ns nodeSet) add(nodes ...*enode.Node) {
    88  	for _, n := range nodes {
    89  		v := ns[n.ID()]
    90  		v.N = n
    91  		v.Seq = n.Seq()
    92  		ns[n.ID()] = v
    93  	}
    94  }
    95  
    96  // topN returns the top n nodes by score as a new set.
    97  func (ns nodeSet) topN(n int) nodeSet {
    98  	if n >= len(ns) {
    99  		return ns
   100  	}
   101  
   102  	byscore := make([]nodeJSON, 0, len(ns))
   103  	for _, v := range ns {
   104  		byscore = append(byscore, v)
   105  	}
   106  	sort.Slice(byscore, func(i, j int) bool {
   107  		return byscore[i].Score >= byscore[j].Score
   108  	})
   109  	result := make(nodeSet, n)
   110  	for _, v := range byscore[:n] {
   111  		result[v.N.ID()] = v
   112  	}
   113  	return result
   114  }
   115  
   116  // verify performs integrity checks on the node set.
   117  func (ns nodeSet) verify() error {
   118  	for id, n := range ns {
   119  		if n.N.ID() != id {
   120  			return fmt.Errorf("invalid node %v: ID does not match ID %v in record", id, n.N.ID())
   121  		}
   122  		if n.N.Seq() != n.Seq {
   123  			return fmt.Errorf("invalid node %v: 'seq' does not match seq %d from record", id, n.N.Seq())
   124  		}
   125  	}
   126  	return nil
   127  }