2019-10-29 10:08:57 -05:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-02-27 04:36:26 -06:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2019-10-29 10:08:57 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
)
|
|
|
|
|
|
|
|
type crawler struct {
|
|
|
|
input nodeSet
|
|
|
|
output nodeSet
|
2020-04-08 02:57:23 -05:00
|
|
|
disc resolver
|
2019-10-29 10:08:57 -05:00
|
|
|
iters []enode.Iterator
|
|
|
|
inputIter enode.Iterator
|
|
|
|
ch chan *enode.Node
|
|
|
|
closed chan struct{}
|
|
|
|
|
|
|
|
// settings
|
|
|
|
revalidateInterval time.Duration
|
2023-02-27 04:36:26 -06:00
|
|
|
mu sync.RWMutex
|
2019-10-29 10:08:57 -05:00
|
|
|
}
|
|
|
|
|
2023-02-14 02:08:06 -06:00
|
|
|
const (
|
|
|
|
nodeRemoved = iota
|
|
|
|
nodeSkipRecent
|
|
|
|
nodeSkipIncompat
|
|
|
|
nodeAdded
|
|
|
|
nodeUpdated
|
|
|
|
)
|
|
|
|
|
2020-04-08 02:57:23 -05:00
|
|
|
type resolver interface {
|
|
|
|
RequestENR(*enode.Node) (*enode.Node, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCrawler(input nodeSet, disc resolver, iters ...enode.Iterator) *crawler {
|
2019-10-29 10:08:57 -05:00
|
|
|
c := &crawler{
|
|
|
|
input: input,
|
|
|
|
output: make(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
|
|
|
|
}
|
|
|
|
|
2023-02-27 04:36:26 -06:00
|
|
|
func (c *crawler) run(timeout time.Duration, nthreads int) nodeSet {
|
2019-10-29 10:08:57 -05:00
|
|
|
var (
|
|
|
|
timeoutTimer = time.NewTimer(timeout)
|
|
|
|
timeoutCh <-chan time.Time
|
2023-02-14 02:08:06 -06:00
|
|
|
statusTicker = time.NewTicker(time.Second * 8)
|
2019-10-29 10:08:57 -05:00
|
|
|
doneCh = make(chan enode.Iterator, len(c.iters))
|
|
|
|
liveIters = len(c.iters)
|
|
|
|
)
|
2023-02-27 04:36:26 -06:00
|
|
|
if nthreads < 1 {
|
|
|
|
nthreads = 1
|
|
|
|
}
|
2020-04-01 09:12:01 -05:00
|
|
|
defer timeoutTimer.Stop()
|
2023-02-14 02:08:06 -06:00
|
|
|
defer statusTicker.Stop()
|
2019-10-29 10:08:57 -05:00
|
|
|
for _, it := range c.iters {
|
|
|
|
go c.runIterator(doneCh, it)
|
|
|
|
}
|
2023-02-14 02:08:06 -06:00
|
|
|
var (
|
2023-02-27 04:36:26 -06:00
|
|
|
added uint64
|
|
|
|
updated uint64
|
|
|
|
skipped uint64
|
|
|
|
recent uint64
|
|
|
|
removed uint64
|
|
|
|
wg sync.WaitGroup
|
2023-02-14 02:08:06 -06:00
|
|
|
)
|
2023-02-27 04:36:26 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-10-29 10:08:57 -05:00
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case it := <-doneCh:
|
|
|
|
if it == c.inputIter {
|
|
|
|
// Enable timeout when we're done revalidating the input nodes.
|
|
|
|
log.Info("Revalidation of input set is done", "len", len(c.input))
|
|
|
|
if timeout > 0 {
|
|
|
|
timeoutCh = timeoutTimer.C
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if liveIters--; liveIters == 0 {
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
case <-timeoutCh:
|
|
|
|
break loop
|
2023-02-14 02:08:06 -06:00
|
|
|
case <-statusTicker.C:
|
|
|
|
log.Info("Crawling in progress",
|
2023-02-27 04:36:26 -06:00
|
|
|
"added", atomic.LoadUint64(&added),
|
|
|
|
"updated", atomic.LoadUint64(&updated),
|
|
|
|
"removed", atomic.LoadUint64(&removed),
|
2023-04-17 09:29:27 -05:00
|
|
|
"ignored(recent)", atomic.LoadUint64(&recent),
|
2023-02-27 04:36:26 -06:00
|
|
|
"ignored(incompatible)", atomic.LoadUint64(&skipped))
|
2019-10-29 10:08:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(c.closed)
|
|
|
|
for _, it := range c.iters {
|
|
|
|
it.Close()
|
|
|
|
}
|
|
|
|
for ; liveIters > 0; liveIters-- {
|
|
|
|
<-doneCh
|
|
|
|
}
|
2023-02-27 04:36:26 -06:00
|
|
|
wg.Wait()
|
2019-10-29 10:08:57 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 02:08:06 -06:00
|
|
|
// updateNode updates the info about the given node, and returns a status
|
|
|
|
// about what changed
|
|
|
|
func (c *crawler) updateNode(n *enode.Node) int {
|
2023-02-27 04:36:26 -06:00
|
|
|
c.mu.RLock()
|
2019-10-29 10:08:57 -05:00
|
|
|
node, ok := c.output[n.ID()]
|
2023-02-27 04:36:26 -06:00
|
|
|
c.mu.RUnlock()
|
2019-10-29 10:08:57 -05:00
|
|
|
|
|
|
|
// Skip validation of recently-seen nodes.
|
|
|
|
if ok && time.Since(node.LastCheck) < c.revalidateInterval {
|
2023-02-14 02:08:06 -06:00
|
|
|
return nodeSkipRecent
|
2019-10-29 10:08:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Request the node record.
|
2023-02-14 02:08:06 -06:00
|
|
|
status := nodeUpdated
|
2023-02-27 04:36:26 -06:00
|
|
|
node.LastCheck = truncNow()
|
|
|
|
if nn, err := c.disc.RequestENR(n); err != nil {
|
2019-10-29 10:08:57 -05:00
|
|
|
if node.Score == 0 {
|
|
|
|
// Node doesn't implement EIP-868.
|
|
|
|
log.Debug("Skipping node", "id", n.ID())
|
2023-02-14 02:08:06 -06:00
|
|
|
return nodeSkipIncompat
|
2019-10-29 10:08:57 -05:00
|
|
|
}
|
|
|
|
node.Score /= 2
|
|
|
|
} else {
|
|
|
|
node.N = nn
|
|
|
|
node.Seq = nn.Seq()
|
|
|
|
node.Score++
|
|
|
|
if node.FirstResponse.IsZero() {
|
|
|
|
node.FirstResponse = node.LastCheck
|
2023-02-14 02:08:06 -06:00
|
|
|
status = nodeAdded
|
2019-10-29 10:08:57 -05:00
|
|
|
}
|
|
|
|
node.LastResponse = node.LastCheck
|
|
|
|
}
|
|
|
|
// Store/update node in output set.
|
2023-02-27 04:36:26 -06:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2019-10-29 10:08:57 -05:00
|
|
|
if node.Score <= 0 {
|
2023-02-14 02:08:06 -06:00
|
|
|
log.Debug("Removing node", "id", n.ID())
|
2019-10-29 10:08:57 -05:00
|
|
|
delete(c.output, n.ID())
|
2023-02-14 02:08:06 -06:00
|
|
|
return nodeRemoved
|
2019-10-29 10:08:57 -05:00
|
|
|
}
|
2023-02-14 02:08:06 -06:00
|
|
|
log.Debug("Updating node", "id", n.ID(), "seq", n.Seq(), "score", node.Score)
|
|
|
|
c.output[n.ID()] = node
|
|
|
|
return status
|
2019-10-29 10:08:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func truncNow() time.Time {
|
|
|
|
return time.Now().UTC().Truncate(1 * time.Second)
|
|
|
|
}
|