Skip to content

Commit

Permalink
Switch to DHT for peer address lookup
Browse files Browse the repository at this point in the history
Replaced the multiaddress-based peer information retrieval with a DHT lookup to simplify the process. Removed unnecessary imports and optimized the code for finding and connecting to peers in the Distributed Hash Table (DHT).
  • Loading branch information
restevens402 committed Aug 23, 2024
1 parent 039dc8d commit b49a1e8
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions pkg/workers/worker_selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"math/rand/v2"
"time"

"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/sirupsen/logrus"

masa "github.com/masa-finance/masa-oracle/pkg"
Expand All @@ -33,27 +31,22 @@ func GetEligibleWorkers(node *masa.OracleNode, category pubsub.WorkerCategory, c
continue
}

addr, err := multiaddr.NewMultiaddr(eligible.MultiaddrsString)
// Use the DHT to find the peer's address information
peerInfo, err := node.DHT.FindPeer(context.Background(), eligible.PeerId)
if err != nil {
logrus.Warnf("Error creating multiaddress for peer %s: %v", eligible.PeerId.String(), err)
continue
}

peerInfo, err := peer.AddrInfoFromP2pAddr(addr)
if err != nil {
logrus.Warnf("Failed to get peer info for %s: %v", eligible.PeerId.String(), err)
logrus.Warnf("Failed to find peer %s in DHT: %v", eligible.PeerId.String(), err)
continue
}

ctxWithTimeout, cancel := context.WithTimeout(context.Background(), config.ConnectionTimeout)
err = node.Host.Connect(ctxWithTimeout, *peerInfo)
err = node.Host.Connect(ctxWithTimeout, peerInfo)
cancel()
if err != nil {
logrus.Warnf("Failed to connect to peer %s: %v", eligible.PeerId.String(), err)
continue
}

workers = append(workers, data_types.Worker{IsLocal: false, NodeData: eligible, AddrInfo: peerInfo})
workers = append(workers, data_types.Worker{IsLocal: false, NodeData: eligible, AddrInfo: &peerInfo})
dur := time.Since(start).Milliseconds()
logrus.Infof("Worker selection took %v milliseconds", dur)
break
Expand Down

0 comments on commit b49a1e8

Please sign in to comment.