github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/cmd/devp2p/nodeset.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package main 19 20 import ( 21 "bytes" 22 "encoding/json" 23 "fmt" 24 "io/ioutil" 25 "os" 26 "sort" 27 "time" 28 29 "github.com/AigarNetwork/aigar/common" 30 "github.com/AigarNetwork/aigar/p2p/enode" 31 ) 32 33 const jsonIndent = " " 34 35 // nodeSet is the nodes.json file format. It holds a set of node records 36 // as a JSON object. 37 type nodeSet map[enode.ID]nodeJSON 38 39 type nodeJSON struct { 40 Seq uint64 `json:"seq"` 41 N *enode.Node `json:"record"` 42 43 // The score tracks how many liveness checks were performed. It is incremented by one 44 // every time the node passes a check, and halved every time it doesn't. 45 Score int `json:"score,omitempty"` 46 // These two track the time of last successful contact. 47 FirstResponse time.Time `json:"firstResponse,omitempty"` 48 LastResponse time.Time `json:"lastResponse,omitempty"` 49 // This one tracks the time of our last attempt to contact the node. 50 LastCheck time.Time `json:"lastCheck,omitempty"` 51 } 52 53 func loadNodesJSON(file string) nodeSet { 54 var nodes nodeSet 55 if err := common.LoadJSON(file, &nodes); err != nil { 56 exit(err) 57 } 58 return nodes 59 } 60 61 func writeNodesJSON(file string, nodes nodeSet) { 62 nodesJSON, err := json.MarshalIndent(nodes, "", jsonIndent) 63 if err != nil { 64 exit(err) 65 } 66 if file == "-" { 67 os.Stdout.Write(nodesJSON) 68 return 69 } 70 if err := ioutil.WriteFile(file, nodesJSON, 0644); err != nil { 71 exit(err) 72 } 73 } 74 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 func (ns nodeSet) add(nodes ...*enode.Node) { 88 for _, n := range nodes { 89 ns[n.ID()] = nodeJSON{Seq: n.Seq(), N: n} 90 } 91 } 92 93 func (ns nodeSet) verify() error { 94 for id, n := range ns { 95 if n.N.ID() != id { 96 return fmt.Errorf("invalid node %v: ID does not match ID %v in record", id, n.N.ID()) 97 } 98 if n.N.Seq() != n.Seq { 99 return fmt.Errorf("invalid node %v: 'seq' does not match seq %d from record", id, n.N.Seq()) 100 } 101 } 102 return nil 103 }