github.com/theQRL/go-zond@v0.1.1/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 "time" 25 26 "github.com/theQRL/go-zond/common" 27 "github.com/theQRL/go-zond/p2p/enode" 28 "golang.org/x/exp/slices" 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 slices.SortFunc(result, func(a, b *enode.Node) int { 81 return bytes.Compare(a.ID().Bytes(), b.ID().Bytes()) 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 slices.SortFunc(byscore, func(a, b nodeJSON) int { 107 if a.Score > b.Score { 108 return -1 109 } 110 if a.Score < b.Score { 111 return 1 112 } 113 return 0 114 }) 115 result := make(nodeSet, n) 116 for _, v := range byscore[:n] { 117 result[v.N.ID()] = v 118 } 119 return result 120 } 121 122 // verify performs integrity checks on the node set. 123 func (ns nodeSet) verify() error { 124 for id, n := range ns { 125 if n.N.ID() != id { 126 return fmt.Errorf("invalid node %v: ID does not match ID %v in record", id, n.N.ID()) 127 } 128 if n.N.Seq() != n.Seq { 129 return fmt.Errorf("invalid node %v: 'seq' does not match seq %d from record", id, n.N.Seq()) 130 } 131 } 132 return nil 133 }