nodeset.go 3.3 KB

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