fillset.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/p2p/enode"
  20. "github.com/ethereum/go-ethereum/p2p/nodestate"
  21. )
  22. // FillSet tries to read nodes from an input iterator and add them to a node set by
  23. // setting the specified node state flag(s) until the size of the set reaches the target.
  24. // Note that other mechanisms (like other FillSet instances reading from different inputs)
  25. // can also set the same flag(s) and FillSet will always care about the total number of
  26. // nodes having those flags.
  27. type FillSet struct {
  28. lock sync.Mutex
  29. cond *sync.Cond
  30. ns *nodestate.NodeStateMachine
  31. input enode.Iterator
  32. closed bool
  33. flags nodestate.Flags
  34. count, target int
  35. }
  36. // NewFillSet creates a new FillSet
  37. func NewFillSet(ns *nodestate.NodeStateMachine, input enode.Iterator, flags nodestate.Flags) *FillSet {
  38. fs := &FillSet{
  39. ns: ns,
  40. input: input,
  41. flags: flags,
  42. }
  43. fs.cond = sync.NewCond(&fs.lock)
  44. ns.SubscribeState(flags, func(n *enode.Node, oldState, newState nodestate.Flags) {
  45. fs.lock.Lock()
  46. if oldState.Equals(flags) {
  47. fs.count--
  48. }
  49. if newState.Equals(flags) {
  50. fs.count++
  51. }
  52. if fs.target > fs.count {
  53. fs.cond.Signal()
  54. }
  55. fs.lock.Unlock()
  56. })
  57. go fs.readLoop()
  58. return fs
  59. }
  60. // readLoop keeps reading nodes from the input and setting the specified flags for them
  61. // whenever the node set size is under the current target
  62. func (fs *FillSet) readLoop() {
  63. for {
  64. fs.lock.Lock()
  65. for fs.target <= fs.count && !fs.closed {
  66. fs.cond.Wait()
  67. }
  68. fs.lock.Unlock()
  69. if !fs.input.Next() {
  70. return
  71. }
  72. fs.ns.SetState(fs.input.Node(), fs.flags, nodestate.Flags{}, 0)
  73. }
  74. }
  75. // SetTarget sets the current target for node set size. If the previous target was not
  76. // reached and FillSet was still waiting for the next node from the input then the next
  77. // incoming node will be added to the set regardless of the target. This ensures that
  78. // all nodes coming from the input are eventually added to the set.
  79. func (fs *FillSet) SetTarget(target int) {
  80. fs.lock.Lock()
  81. defer fs.lock.Unlock()
  82. fs.target = target
  83. if fs.target > fs.count {
  84. fs.cond.Signal()
  85. }
  86. }
  87. // Close shuts FillSet down and closes the input iterator
  88. func (fs *FillSet) Close() {
  89. fs.lock.Lock()
  90. defer fs.lock.Unlock()
  91. fs.closed = true
  92. fs.input.Close()
  93. fs.cond.Signal()
  94. }