-
Notifications
You must be signed in to change notification settings - Fork 21
/
crawl_util.go
240 lines (215 loc) · 5.3 KB
/
crawl_util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package crawl
import (
"sync"
"sync/atomic"
"time"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/rs/zerolog/log"
"github.com/maticnetwork/polygon-cli/p2p"
)
type crawler struct {
input p2p.NodeSet
output p2p.NodeSet
disc resolver
iters []enode.Iterator
inputIter enode.Iterator
ch chan *enode.Node
closed chan struct{}
// settings
revalidateInterval time.Duration
mu sync.RWMutex
}
const (
nodeRemoved = iota
nodeSkipRecent
nodeSkipIncompat
nodeAdded
nodeUpdated
)
type resolver interface {
RequestENR(*enode.Node) (*enode.Node, error)
}
func newCrawler(input p2p.NodeSet, disc resolver, iters ...enode.Iterator) *crawler {
c := &crawler{
input: input,
output: make(p2p.NodeSet, len(input)),
disc: disc,
iters: iters,
inputIter: enode.IterNodes(input.Nodes()),
ch: make(chan *enode.Node),
closed: make(chan struct{}),
}
c.iters = append(c.iters, c.inputIter)
// Copy input to output initially. Any nodes that fail validation
// will be dropped from output during the run.
for id, n := range input {
c.output[id] = n
}
return c
}
func (c *crawler) run(timeout time.Duration, nthreads int) p2p.NodeSet {
var (
timeoutTimer = time.NewTimer(timeout)
timeoutCh <-chan time.Time
statusTicker = time.NewTicker(time.Second * 8)
doneCh = make(chan enode.Iterator, len(c.iters))
liveIters = len(c.iters)
)
if nthreads < 1 {
nthreads = 1
}
defer timeoutTimer.Stop()
defer statusTicker.Stop()
for _, it := range c.iters {
go c.runIterator(doneCh, it)
}
var (
added uint64
updated uint64
skipped uint64
recent uint64
removed uint64
wg sync.WaitGroup
)
wg.Add(nthreads)
for i := 0; i < nthreads; i++ {
go func() {
defer wg.Done()
for {
select {
case n := <-c.ch:
switch c.updateNode(n) {
case nodeSkipIncompat:
atomic.AddUint64(&skipped, 1)
case nodeSkipRecent:
atomic.AddUint64(&recent, 1)
case nodeRemoved:
atomic.AddUint64(&removed, 1)
case nodeAdded:
atomic.AddUint64(&added, 1)
default:
atomic.AddUint64(&updated, 1)
}
case <-c.closed:
return
}
}
}()
}
loop:
for {
select {
case it := <-doneCh:
if it == c.inputIter {
// Enable timeout when we're done revalidating the input nodes.
log.Info().Int("len", len(c.input)).Msg("Revalidation of input set is done")
if timeout > 0 {
timeoutCh = timeoutTimer.C
}
}
if liveIters--; liveIters == 0 {
break loop
}
case <-timeoutCh:
break loop
case <-statusTicker.C:
log.Info().
Uint64("added", atomic.LoadUint64(&added)).
Uint64("updated", atomic.LoadUint64(&updated)).
Uint64("removed", atomic.LoadUint64(&removed)).
Uint64("ignored(recent)", atomic.LoadUint64(&removed)).
Uint64("ignored(incompatible)", atomic.LoadUint64(&skipped)).
Msg("Crawling in progress")
}
}
close(c.closed)
for _, it := range c.iters {
it.Close()
}
for ; liveIters > 0; liveIters-- {
<-doneCh
}
wg.Wait()
return c.output
}
func (c *crawler) runIterator(done chan<- enode.Iterator, it enode.Iterator) {
defer func() { done <- it }()
for it.Next() {
select {
case c.ch <- it.Node():
case <-c.closed:
return
}
}
}
// shouldSkipNode filters out nodes by their network id. If there is a status
// message, skip nodes that don't have the correct network id. Otherwise, skip
// nodes that are unable to peer.
func shouldSkipNode(n *enode.Node) bool {
if inputCrawlParams.NetworkID == 0 {
return false
}
conn, err := p2p.Dial(n)
if err != nil {
log.Error().Err(err).Msg("Dial failed")
return true
}
defer conn.Close()
hello, status, err := conn.Peer()
if err != nil {
log.Error().Err(err).Msg("Peer failed")
return true
}
log.Debug().Interface("hello", hello).Interface("status", status).Msg("Message received")
return inputCrawlParams.NetworkID != status.NetworkID
}
// updateNode updates the info about the given node, and returns a status about
// what changed.
func (c *crawler) updateNode(n *enode.Node) int {
c.mu.RLock()
node, ok := c.output[n.ID()]
c.mu.RUnlock()
// Skip validation of recently-seen nodes.
if ok && time.Since(node.LastCheck) < c.revalidateInterval {
log.Debug().Str("id", n.ID().String()).Msg("Skipping node")
return nodeSkipRecent
}
// Filter out incompatible nodes.
if shouldSkipNode(n) {
return nodeSkipIncompat
}
// Request the node record.
status := nodeUpdated
node.LastCheck = truncNow()
if nn, err := c.disc.RequestENR(n); err != nil {
if node.Score == 0 {
// Node doesn't implement EIP-868.
log.Debug().Str("id", n.ID().String()).Msg("Skipping node")
return nodeSkipIncompat
}
node.Score /= 2
} else {
node.N = nn
node.Seq = nn.Seq()
node.Score++
if node.FirstResponse.IsZero() {
node.FirstResponse = node.LastCheck
status = nodeAdded
}
node.LastResponse = node.LastCheck
}
// Store/update node in output set.
c.mu.Lock()
defer c.mu.Unlock()
if node.Score <= 0 {
log.Debug().Str("id", n.ID().String()).Msg("Removing node")
delete(c.output, n.ID())
return nodeRemoved
}
log.Debug().Str("id", n.ID().String()).Uint64("seq", n.Seq()).Int("score", node.Score).Msg("Updating node")
c.output[n.ID()] = node
return status
}
func truncNow() time.Time {
return time.Now().UTC().Truncate(1 * time.Second)
}