wrsiterator.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2020 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 client
  17. import (
  18. "sync"
  19. "github.com/ethereum/go-ethereum/les/utils"
  20. "github.com/ethereum/go-ethereum/p2p/enode"
  21. "github.com/ethereum/go-ethereum/p2p/nodestate"
  22. )
  23. // WrsIterator returns nodes from the specified selectable set with a weighted random
  24. // selection. Selection weights are provided by a callback function.
  25. type WrsIterator struct {
  26. lock sync.Mutex
  27. cond *sync.Cond
  28. ns *nodestate.NodeStateMachine
  29. wrs *utils.WeightedRandomSelect
  30. nextNode *enode.Node
  31. closed bool
  32. }
  33. // NewWrsIterator creates a new WrsIterator. Nodes are selectable if they have all the required
  34. // and none of the disabled flags set. When a node is selected the selectedFlag is set which also
  35. // disables further selectability until it is removed or times out.
  36. func NewWrsIterator(ns *nodestate.NodeStateMachine, requireFlags, disableFlags nodestate.Flags, weightField nodestate.Field) *WrsIterator {
  37. wfn := func(i interface{}) uint64 {
  38. n := ns.GetNode(i.(enode.ID))
  39. if n == nil {
  40. return 0
  41. }
  42. wt, _ := ns.GetField(n, weightField).(uint64)
  43. return wt
  44. }
  45. w := &WrsIterator{
  46. ns: ns,
  47. wrs: utils.NewWeightedRandomSelect(wfn),
  48. }
  49. w.cond = sync.NewCond(&w.lock)
  50. ns.SubscribeField(weightField, func(n *enode.Node, state nodestate.Flags, oldValue, newValue interface{}) {
  51. if state.HasAll(requireFlags) && state.HasNone(disableFlags) {
  52. w.lock.Lock()
  53. w.wrs.Update(n.ID())
  54. w.lock.Unlock()
  55. w.cond.Signal()
  56. }
  57. })
  58. ns.SubscribeState(requireFlags.Or(disableFlags), func(n *enode.Node, oldState, newState nodestate.Flags) {
  59. oldMatch := oldState.HasAll(requireFlags) && oldState.HasNone(disableFlags)
  60. newMatch := newState.HasAll(requireFlags) && newState.HasNone(disableFlags)
  61. if newMatch == oldMatch {
  62. return
  63. }
  64. w.lock.Lock()
  65. if newMatch {
  66. w.wrs.Update(n.ID())
  67. } else {
  68. w.wrs.Remove(n.ID())
  69. }
  70. w.lock.Unlock()
  71. w.cond.Signal()
  72. })
  73. return w
  74. }
  75. // Next selects the next node.
  76. func (w *WrsIterator) Next() bool {
  77. w.nextNode = w.chooseNode()
  78. return w.nextNode != nil
  79. }
  80. func (w *WrsIterator) chooseNode() *enode.Node {
  81. w.lock.Lock()
  82. defer w.lock.Unlock()
  83. for {
  84. for !w.closed && w.wrs.IsEmpty() {
  85. w.cond.Wait()
  86. }
  87. if w.closed {
  88. return nil
  89. }
  90. // Choose the next node at random. Even though w.wrs is guaranteed
  91. // non-empty here, Choose might return nil if all items have weight
  92. // zero.
  93. if c := w.wrs.Choose(); c != nil {
  94. id := c.(enode.ID)
  95. w.wrs.Remove(id)
  96. return w.ns.GetNode(id)
  97. }
  98. }
  99. }
  100. // Close ends the iterator.
  101. func (w *WrsIterator) Close() {
  102. w.lock.Lock()
  103. w.closed = true
  104. w.lock.Unlock()
  105. w.cond.Signal()
  106. }
  107. // Node returns the current node.
  108. func (w *WrsIterator) Node() *enode.Node {
  109. w.lock.Lock()
  110. defer w.lock.Unlock()
  111. return w.nextNode
  112. }