Skip to content

Commit

Permalink
chore: change peers caps to []string and change DNS discovery logic (#…
Browse files Browse the repository at this point in the history
…411)

* chore: cleanup

* fix import

* fix logic

* fix: simplify logic
  • Loading branch information
minhd-vu authored Oct 24, 2024
1 parent 43b4e81 commit 1fa089c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
40 changes: 19 additions & 21 deletions cmd/p2p/sensor/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,26 +251,30 @@ var SensorCmd = &cobra.Command{
sub := server.SubscribeEvents(events)
defer sub.Unsubscribe()

ticker := time.NewTicker(2 * time.Second) // Ticker for recurring tasks every 2 seconds
hourlyTicker := time.NewTicker(time.Hour) // Ticker for running DNS discovery every hour
ticker := time.NewTicker(2 * time.Second) // Ticker for recurring tasks every 2 seconds.
hourlyTicker := time.NewTicker(time.Hour) // Ticker for running DNS discovery every hour.
defer ticker.Stop()
defer hourlyTicker.Stop()

signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)

// peers represents the peer map that is used to write to the nodes.json
// file. This is helpful when restarting the node with the --quickstart flag
// enabled. This map does not represent the peers that are currently
// connected to the sensor. To do that use `server.Peers()` instead.
peers := make(map[enode.ID]string)
var peersMutex sync.Mutex

for _, node := range inputSensorParams.nodes {
// Map node URLs to node IDs to avoid duplicates
// Map node URLs to node IDs to avoid duplicates.
peers[node.ID()] = node.URLv4()
}

go handleAPI(&server, msgCounter)

// Run DNS discovery immediately at startup
go handleDNSDiscovery(&server, peers, &peersMutex)
// Run DNS discovery immediately at startup.
go handleDNSDiscovery(&server)

for {
select {
Expand All @@ -282,9 +286,9 @@ var SensorCmd = &cobra.Command{

db.WritePeers(context.Background(), server.Peers())
case peer := <-opts.Peers:
// Lock the peers map before modifying it
// Lock the peers map before modifying it.
peersMutex.Lock()
// Update the peer list and the nodes file
// Update the peer list and the nodes file.
if _, ok := peers[peer.ID()]; !ok {
peers[peer.ID()] = peer.URLv4()

Expand All @@ -294,7 +298,7 @@ var SensorCmd = &cobra.Command{
}
peersMutex.Unlock()
case <-hourlyTicker.C:
go handleDNSDiscovery(&server, peers, &peersMutex)
go handleDNSDiscovery(&server)
case <-signals:
// This gracefully stops the sensor so that the peers can be written to
// the nodes file.
Expand Down Expand Up @@ -389,7 +393,7 @@ func handleAPI(server *ethp2p.Server, counter *prometheus.CounterVec) {
// handleDNSDiscovery performs DNS-based peer discovery and adds new peers to
// the p2p server. It syncs the DNS discovery tree and adds any newly discovered
// peers not already in the peers map.
func handleDNSDiscovery(server *ethp2p.Server, peers map[enode.ID]string, peersMutex *sync.Mutex) {
func handleDNSDiscovery(server *ethp2p.Server) {
if len(inputSensorParams.DiscoveryDNS) == 0 {
return
}
Expand All @@ -405,26 +409,20 @@ func handleDNSDiscovery(server *ethp2p.Server, peers map[enode.ID]string, peersM
return
}

// Log the number of nodes in the tree
// Log the number of nodes in the tree.
log.Info().
Int("unique_nodes", len(tree.Nodes())).
Msg("Successfully synced DNS discovery tree")

// Lock the peers map and server operations
peersMutex.Lock()
defer peersMutex.Unlock()

// Add DNS-discovered peers
// Add DNS-discovered peers.
for _, node := range tree.Nodes() {
if _, ok := peers[node.ID()]; ok {
continue
}

log.Debug().
Str("enode", node.URLv4()).
Msg("Discovered new peer through DNS")
Msg("Discovered peer through DNS")

// Instruct server to connect to the new peer
// Add the peer to the static node set. The server itself handles whether to
// connect to the peer if it's already connected. If a node is part of the
// static peer set, the server will handle reconnecting after disconnects.
server.AddPeer(node)
}

Expand Down
5 changes: 2 additions & 3 deletions p2p/database/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"math/big"
"strings"
"time"

"cloud.google.com/go/datastore"
Expand Down Expand Up @@ -107,7 +106,7 @@ type DatastoreTransaction struct {

type DatastorePeer struct {
Name string
Caps string
Caps []string
URL string
LastSeenBy string
TimeLastSeen time.Time
Expand Down Expand Up @@ -266,7 +265,7 @@ func (d *Datastore) WritePeers(ctx context.Context, peers []*p2p.Peer) {
keys = append(keys, datastore.NameKey(PeersKind, peer.ID().String(), nil))
dsPeers = append(dsPeers, &DatastorePeer{
Name: peer.Fullname(),
Caps: strings.Join(peer.Info().Caps, ","),
Caps: peer.Info().Caps,
URL: peer.Node().URLv4(),
LastSeenBy: d.sensorID,
TimeLastSeen: now,
Expand Down

0 comments on commit 1fa089c

Please sign in to comment.