Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

p2p: dial using node iterator #20132

Merged
merged 17 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
cmd/devp2p: improve crawl timestamp handling
  • Loading branch information
fjl committed Oct 18, 2019
commit 1098aef26a2018853dfd6d217ab7f950f9fab379
28 changes: 15 additions & 13 deletions cmd/devp2p/crawl.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,38 +111,40 @@ func (c *crawler) runIterator(done chan<- enode.Iterator, it enode.Iterator) {
}

func (c *crawler) updateNode(n *enode.Node) {
existing, ok := c.output[n.ID()]
node, ok := c.output[n.ID()]

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

// Request the node record.
nn, err := c.disc.RequestENR(n)
node.LastCheck = truncNow()
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we penalize nodes that happen to be refreshed during shutdown? I.e. are we sure err != nil only if ENR is offline and not in other scenarios that might be our fault?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shutdown can happen in two ways: when you cancel a cmd/devp2p run, the output file is not updated. Otherwise the command terminates when the crawl is done.

if existing.Checks == 0 {
if node.Score == 0 {
// Node doesn't implement EIP-868.
log.Debug("Skipping node", "id", n.ID())
return
}
existing.Checks /= 2
node.Score /= 2
} else {
if !ok {
existing.FirstSeen = truncNow()
node.N = nn
node.Seq = nn.Seq()
node.Score++
if node.FirstResponse.IsZero() {
node.FirstResponse = node.LastCheck
}
existing.N = nn
existing.Seq = nn.Seq()
existing.LastSeen = truncNow()
existing.Checks++
node.LastResponse = node.LastCheck
}

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

Expand Down
18 changes: 12 additions & 6 deletions cmd/devp2p/nodeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ const jsonIndent = " "
type nodeSet map[enode.ID]nodeJSON

type nodeJSON struct {
Seq uint64 `json:"seq"`
N *enode.Node `json:"record"`
FirstSeen time.Time `json:"firstSeen,omitempty"`
LastSeen time.Time `json:"lastSeen,omitempty"`
Checks int `json:"checks"`
Seq uint64 `json:"seq"`
N *enode.Node `json:"record"`

// The score tracks how many liveness checks were performed. It is incremented by one
// every time the node passes a check, and halved every time it doesn't.
Score int `json:"score,omitempty"`
// These two track the time of last successful contact.
FirstResponse time.Time `json:"firstResponse,omitempty"`
LastResponse time.Time `json:"lastResponse,omitempty"`
// This one tracks the time of our last attempt to contact the node.
LastCheck time.Time `json:"lastCheck,omitempty"`
}

func loadNodesJSON(file string) nodeSet {
Expand Down Expand Up @@ -79,7 +85,7 @@ func (ns nodeSet) nodes() []*enode.Node {

func (ns nodeSet) add(nodes ...*enode.Node) {
for _, n := range nodes {
ns[n.ID()] = nodeJSON{Seq: n.Seq(), N: n, FirstSeen: truncNow()}
ns[n.ID()] = nodeJSON{Seq: n.Seq(), N: n}
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/devp2p/nodesetcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func minAgeFilter(args []string) (nodeFilter, error) {
return nil, err
}
f := func(n nodeJSON) bool {
age := n.LastSeen.Sub(n.FirstSeen)
age := n.LastResponse.Sub(n.FirstResponse)
return age >= minage
}
return f, nil
Expand Down