github.com/guiltylotus/go-ethereum@v1.9.7/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  	"github.com/ethereum/go-ethereum/common"
    29  	"github.com/ethereum/go-ethereum/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  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  func (ns nodeSet) add(nodes ...*enode.Node) {
    87  	for _, n := range nodes {
    88  		ns[n.ID()] = nodeJSON{Seq: n.Seq(), N: n}
    89  	}
    90  }
    91  
    92  func (ns nodeSet) verify() error {
    93  	for id, n := range ns {
    94  		if n.N.ID() != id {
    95  			return fmt.Errorf("invalid node %v: ID does not match ID %v in record", id, n.N.ID())
    96  		}
    97  		if n.N.Seq() != n.Seq {
    98  			return fmt.Errorf("invalid node %v: 'seq' does not match seq %d from record", id, n.N.Seq())
    99  		}
   100  	}
   101  	return nil
   102  }