Skip to content

Commit

Permalink
cmd/devp2p: less output in node crawler
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Feb 13, 2023
1 parent 7d29fff commit 3015005
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions cmd/devp2p/crawl.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ type crawler struct {
revalidateInterval time.Duration
}

const (
nodeRemoved = iota
nodeIgnoredRecent
nodeSkipped
nodeUpdated
)

type resolver interface {
RequestENR(*enode.Node) (*enode.Node, error)
}
Expand Down Expand Up @@ -63,19 +70,36 @@ func (c *crawler) run(timeout time.Duration) 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)
)
defer timeoutTimer.Stop()
defer statusTicker.Stop()
for _, it := range c.iters {
go c.runIterator(doneCh, it)
}

var (
updates int
ignored int
skips int
removals int
)
loop:
for {
select {
case n := <-c.ch:
c.updateNode(n)
switch c.updateNode(n) {
case nodeIgnoredRecent:
ignored++
case nodeSkipped:
skips++
case nodeRemoved:
removals++
default:
updates++
}
case it := <-doneCh:
if it == c.inputIter {
// Enable timeout when we're done revalidating the input nodes.
Expand All @@ -89,6 +113,10 @@ loop:
}
case <-timeoutCh:
break loop
case <-statusTicker.C:
log.Info("Crawling in progress",
"updated", updates, "removals", removals,
"ignored (recently checked)", ignored, "ignored (no EIP-868)", skips)
}
}

Expand All @@ -113,12 +141,14 @@ func (c *crawler) runIterator(done chan<- enode.Iterator, it enode.Iterator) {
}
}

func (c *crawler) updateNode(n *enode.Node) {
// updateNode updates the info about the given node, and returns a status
// about what changed
func (c *crawler) updateNode(n *enode.Node) int {
node, ok := c.output[n.ID()]

// Skip validation of recently-seen nodes.
if ok && time.Since(node.LastCheck) < c.revalidateInterval {
return
return nodeIgnoredRecent
}

// Request the node record.
Expand All @@ -128,7 +158,7 @@ func (c *crawler) updateNode(n *enode.Node) {
if node.Score == 0 {
// Node doesn't implement EIP-868.
log.Debug("Skipping node", "id", n.ID())
return
return nodeSkipped
}
node.Score /= 2
} else {
Expand All @@ -143,12 +173,14 @@ func (c *crawler) updateNode(n *enode.Node) {

// Store/update node in output set.
if node.Score <= 0 {
log.Info("Removing node", "id", n.ID())
log.Debug("Removing node", "id", n.ID())
delete(c.output, n.ID())
} else {
log.Info("Updating node", "id", n.ID(), "seq", n.Seq(), "score", node.Score)
c.output[n.ID()] = node
return nodeRemoved
}
log.Debug("Updating node", "id", n.ID(), "seq", n.Seq(), "score", node.Score)
c.output[n.ID()] = node
return nodeUpdated

}

func truncNow() time.Time {
Expand Down

0 comments on commit 3015005

Please sign in to comment.