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

[DVT-819][DVT-903] Use p2p.Server and refactor #93

Merged
merged 32 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8e80a3e
add key file flag
minhd-vu Jun 26, 2023
25fc999
fix shadowed error
minhd-vu Jun 27, 2023
d17df57
fix database nil
minhd-vu Jun 28, 2023
bbab297
add ip and port flags
minhd-vu Jun 29, 2023
c58fca9
add server
minhd-vu Aug 1, 2023
8c6e6b9
fetch latest block
minhd-vu Aug 1, 2023
84a4e21
update message codes
minhd-vu Aug 1, 2023
e93abba
update params
minhd-vu Aug 2, 2023
b6bcdf4
add nat
minhd-vu Aug 2, 2023
f6a527b
remove ip flag
minhd-vu Aug 2, 2023
b503cfe
fix ci
minhd-vu Aug 2, 2023
6d93205
move logic to protocol.go
minhd-vu Aug 3, 2023
f2da6e5
use any nat
minhd-vu Aug 3, 2023
877ef20
behold!
minhd-vu Aug 9, 2023
11a0ba2
remove port for p2p.Listen
minhd-vu Aug 9, 2023
5dd2861
fix lint and docs
minhd-vu Aug 9, 2023
ff5748f
fix lint
minhd-vu Aug 9, 2023
278ad9d
update nodeset
minhd-vu Aug 10, 2023
85aa6a6
fix lint
minhd-vu Aug 10, 2023
c5158df
make gen-doc
minhd-vu Aug 10, 2023
0b2aa84
remove truncNow
minhd-vu Aug 10, 2023
0f97715
address comments
minhd-vu Aug 11, 2023
eade1fb
keep track of head block and improve logging
minhd-vu Aug 11, 2023
a89905a
update docs
minhd-vu Aug 11, 2023
d3b11e3
update when the nodes file gets written
minhd-vu Aug 11, 2023
01672da
update docs
minhd-vu Aug 11, 2023
3a0e2b2
fix protocol msg codes
minhd-vu Aug 14, 2023
56f0b14
add quick start flag
minhd-vu Aug 14, 2023
b898915
update docs
minhd-vu Aug 14, 2023
b627cc7
add trusted peers flag
minhd-vu Aug 14, 2023
6203cc7
update docs
minhd-vu Aug 14, 2023
7d0c0f9
set head block differently
minhd-vu Aug 15, 2023
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
9 changes: 5 additions & 4 deletions cmd/p2p/crawl/crawl.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type (
NodesFile string
Database string
RevalidationInterval string

revalidationInterval time.Duration
}
)
Expand All @@ -36,7 +37,7 @@ var (
var CrawlCmd = &cobra.Command{
Use: "crawl [nodes file]",
Short: "Crawl a network on the devp2p layer and generate a nodes JSON file.",
Long: "If no nodes.json file exists, run `echo \"{}\" >> nodes.json` to get started.",
Long: "If no nodes.json file exists, run `echo \"[]\" >> nodes.json` to get started.",
minhd-vu marked this conversation as resolved.
Show resolved Hide resolved
Args: cobra.MinimumNArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) (err error) {
inputCrawlParams.NodesFile = args[0]
Expand All @@ -54,7 +55,7 @@ var CrawlCmd = &cobra.Command{
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
inputSet, err := p2p.LoadNodesJSON(inputCrawlParams.NodesFile)
nodes, err := p2p.ReadNodeSet(inputCrawlParams.NodesFile)
if err != nil {
return err
}
Expand Down Expand Up @@ -84,13 +85,13 @@ var CrawlCmd = &cobra.Command{
}
defer disc.Close()

c := newCrawler(inputSet, disc, disc.RandomNodes())
c := newCrawler(nodes, disc, disc.RandomNodes())
c.revalidateInterval = inputCrawlParams.revalidationInterval

log.Info().Msg("Starting crawl")

output := c.run(inputCrawlParams.timeout, inputCrawlParams.Threads)
return p2p.WriteNodesJSON(inputCrawlParams.NodesFile, output)
return p2p.WriteNodeSet(inputCrawlParams.NodesFile, output)
},
}

Expand Down
66 changes: 14 additions & 52 deletions cmd/p2p/crawl/crawl_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type crawler struct {
input p2p.NodeSet
input []*enode.Node
output p2p.NodeSet
disc resolver
iters []enode.Iterator
Expand All @@ -37,21 +37,21 @@ type resolver interface {
RequestENR(*enode.Node) (*enode.Node, error)
}

func newCrawler(input p2p.NodeSet, disc resolver, iters ...enode.Iterator) *crawler {
func newCrawler(input []*enode.Node, 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()),
inputIter: enode.IterNodes(input),
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
for _, n := range input {
c.output[n.ID()] = n.URLv4()
}
return c
}
Expand All @@ -74,10 +74,8 @@ func (c *crawler) run(timeout time.Duration, nthreads int) p2p.NodeSet {
}
var (
added uint64
updated uint64
skipped uint64
recent uint64
removed uint64
wg sync.WaitGroup
)
wg.Add(nthreads)
Expand All @@ -92,12 +90,8 @@ func (c *crawler) run(timeout time.Duration, nthreads int) p2p.NodeSet {
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
Expand Down Expand Up @@ -125,9 +119,7 @@ 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(recent)", atomic.LoadUint64(&recent)).
Uint64("ignored(incompatible)", atomic.LoadUint64(&skipped)).
Msg("Crawling in progress")
}
Expand Down Expand Up @@ -184,12 +176,10 @@ func shouldSkipNode(n *enode.Node) bool {
// what changed.
func (c *crawler) updateNode(n *enode.Node) int {
c.mu.RLock()
node, ok := c.output[n.ID()]
_, 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")
if ok {
return nodeSkipRecent
}

Expand All @@ -198,43 +188,15 @@ func (c *crawler) updateNode(n *enode.Node) int {
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
nn, err := c.disc.RequestENR(n)
if err != nil {
return nodeSkipIncompat
}

// 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
}
c.output[nn.ID()] = nn.URLv4()
c.mu.Unlock()

func truncNow() time.Time {
return time.Now().UTC().Truncate(1 * time.Second)
return nodeAdded
}
19 changes: 12 additions & 7 deletions cmd/p2p/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ var PingCmd = &cobra.Command{
Long: `Ping nodes by either giving a single enode/enr or an entire nodes file.

This command will establish a handshake and status exchange to get the Hello and
Status messages and output JSON. If providing a enode/enr rather than a node file,
then the connection will remain open by default (--listen=true), and you can see
other messages the peer sends (e.g. blocks, transactions, etc.).`,
Status messages and output JSON. If providing a enode/enr rather than a nodes
file, then the connection will remain open by default (--listen=true), and you
can see other messages the peer sends (e.g. blocks, transactions, etc.).`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
nodes := []*enode.Node{}
if inputSet, err := p2p.LoadNodesJSON(args[0]); err == nil {
nodes = inputSet.Nodes()
if input, err := p2p.ReadNodeSet(args[0]); err == nil {
nodes = input
inputPingParams.Listen = false
} else if node, err := p2p.ParseNode(args[0]); err == nil {
nodes = append(nodes, node)
Expand Down Expand Up @@ -98,14 +98,19 @@ other messages the peer sends (e.g. blocks, transactions, etc.).`,
errStr = err.Error()
} else if inputPingParams.Listen {
// If the dial and peering were successful, listen to the peer for messages.
if err := conn.ReadAndServe(nil, count); err != nil {
if err := conn.ReadAndServe(count); err != nil {
log.Error().Err(err).Msg("Received error")
}
}

// Save the results to the output map.
mutex.Lock()
output[node.ID()] = pingNodeJSON{node, hello, status, errStr}
output[node.ID()] = pingNodeJSON{
Record: node,
Hello: hello,
Status: status,
Error: errStr,
}
mutex.Unlock()
}(n)
}
Expand Down
Loading