table_util_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser 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. // The go-ethereum library 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 Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package discover
  17. import (
  18. "crypto/ecdsa"
  19. "encoding/hex"
  20. "fmt"
  21. "math/rand"
  22. "net"
  23. "sort"
  24. "sync"
  25. "github.com/ethereum/go-ethereum/crypto"
  26. "github.com/ethereum/go-ethereum/log"
  27. "github.com/ethereum/go-ethereum/p2p/enode"
  28. "github.com/ethereum/go-ethereum/p2p/enr"
  29. )
  30. var nullNode *enode.Node
  31. func init() {
  32. var r enr.Record
  33. r.Set(enr.IP{0, 0, 0, 0})
  34. nullNode = enode.SignNull(&r, enode.ID{})
  35. }
  36. func newTestTable(t transport) (*Table, *enode.DB) {
  37. db, _ := enode.OpenDB("")
  38. tab, _ := newTable(t, db, nil, log.Root())
  39. go tab.loop()
  40. return tab, db
  41. }
  42. // nodeAtDistance creates a node for which enode.LogDist(base, n.id) == ld.
  43. func nodeAtDistance(base enode.ID, ld int, ip net.IP) *node {
  44. var r enr.Record
  45. r.Set(enr.IP(ip))
  46. return wrapNode(enode.SignNull(&r, idAtDistance(base, ld)))
  47. }
  48. // idAtDistance returns a random hash such that enode.LogDist(a, b) == n
  49. func idAtDistance(a enode.ID, n int) (b enode.ID) {
  50. if n == 0 {
  51. return a
  52. }
  53. // flip bit at position n, fill the rest with random bits
  54. b = a
  55. pos := len(a) - n/8 - 1
  56. bit := byte(0x01) << (byte(n%8) - 1)
  57. if bit == 0 {
  58. pos++
  59. bit = 0x80
  60. }
  61. b[pos] = a[pos]&^bit | ^a[pos]&bit // TODO: randomize end bits
  62. for i := pos + 1; i < len(a); i++ {
  63. b[i] = byte(rand.Intn(255))
  64. }
  65. return b
  66. }
  67. func intIP(i int) net.IP {
  68. return net.IP{byte(i), 0, 2, byte(i)}
  69. }
  70. // fillBucket inserts nodes into the given bucket until it is full.
  71. func fillBucket(tab *Table, n *node) (last *node) {
  72. ld := enode.LogDist(tab.self().ID(), n.ID())
  73. b := tab.bucket(n.ID())
  74. for len(b.entries) < bucketSize {
  75. b.entries = append(b.entries, nodeAtDistance(tab.self().ID(), ld, intIP(ld)))
  76. }
  77. return b.entries[bucketSize-1]
  78. }
  79. // fillTable adds nodes the table to the end of their corresponding bucket
  80. // if the bucket is not full. The caller must not hold tab.mutex.
  81. func fillTable(tab *Table, nodes []*node) {
  82. for _, n := range nodes {
  83. tab.addSeenNode(n)
  84. }
  85. }
  86. type pingRecorder struct {
  87. mu sync.Mutex
  88. dead, pinged map[enode.ID]bool
  89. n *enode.Node
  90. }
  91. func newPingRecorder() *pingRecorder {
  92. var r enr.Record
  93. r.Set(enr.IP{0, 0, 0, 0})
  94. n := enode.SignNull(&r, enode.ID{})
  95. return &pingRecorder{
  96. dead: make(map[enode.ID]bool),
  97. pinged: make(map[enode.ID]bool),
  98. n: n,
  99. }
  100. }
  101. func (t *pingRecorder) Self() *enode.Node {
  102. return nullNode
  103. }
  104. func (t *pingRecorder) ping(n *enode.Node) error {
  105. t.mu.Lock()
  106. defer t.mu.Unlock()
  107. t.pinged[n.ID()] = true
  108. if t.dead[n.ID()] {
  109. return errTimeout
  110. } else {
  111. return nil
  112. }
  113. }
  114. func (t *pingRecorder) lookupSelf() []*enode.Node {
  115. return nil
  116. }
  117. func (t *pingRecorder) lookupRandom() []*enode.Node {
  118. return nil
  119. }
  120. func (t *pingRecorder) close() {}
  121. func hasDuplicates(slice []*node) bool {
  122. seen := make(map[enode.ID]bool)
  123. for i, e := range slice {
  124. if e == nil {
  125. panic(fmt.Sprintf("nil *Node at %d", i))
  126. }
  127. if seen[e.ID()] {
  128. return true
  129. }
  130. seen[e.ID()] = true
  131. }
  132. return false
  133. }
  134. func sortedByDistanceTo(distbase enode.ID, slice []*node) bool {
  135. return sort.SliceIsSorted(slice, func(i, j int) bool {
  136. return enode.DistCmp(distbase, slice[i].ID(), slice[j].ID()) < 0
  137. })
  138. }
  139. func hexEncPrivkey(h string) *ecdsa.PrivateKey {
  140. b, err := hex.DecodeString(h)
  141. if err != nil {
  142. panic(err)
  143. }
  144. key, err := crypto.ToECDSA(b)
  145. if err != nil {
  146. panic(err)
  147. }
  148. return key
  149. }
  150. func hexEncPubkey(h string) (ret encPubkey) {
  151. b, err := hex.DecodeString(h)
  152. if err != nil {
  153. panic(err)
  154. }
  155. if len(b) != len(ret) {
  156. panic("invalid length")
  157. }
  158. copy(ret[:], b)
  159. return ret
  160. }