2021-06-16 10:14:22 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2021-11-09 23:34:04 +00:00
|
|
|
"context"
|
2021-06-16 10:14:22 +00:00
|
|
|
"errors"
|
2021-11-16 14:22:01 +00:00
|
|
|
"fmt"
|
2021-10-28 19:26:25 +00:00
|
|
|
"math/rand"
|
2021-11-09 23:34:04 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2021-06-16 10:14:22 +00:00
|
|
|
|
2021-11-16 14:22:01 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2021-06-16 10:14:22 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2021-11-09 23:34:04 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
|
2021-06-16 10:14:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("utils")
|
|
|
|
|
2021-11-09 23:34:04 +00:00
|
|
|
var ErrNoPeersAvailable = errors.New("no suitable peers found")
|
|
|
|
var PingServiceNotAvailable = errors.New("ping service not available")
|
|
|
|
|
2021-10-28 19:26:25 +00:00
|
|
|
// SelectPeer is used to return a random peer that supports a given protocol.
|
2021-06-16 10:14:22 +00:00
|
|
|
func SelectPeer(host host.Host, protocolId string) (*peer.ID, error) {
|
|
|
|
// @TODO We need to be more strategic about which peers we dial. Right now we just set one on the service.
|
|
|
|
// Ideally depending on the query and our set of peers we take a subset of ideal peers.
|
|
|
|
// This will require us to check for various factors such as:
|
|
|
|
// - which topics they track
|
|
|
|
// - latency?
|
|
|
|
// - default store peer?
|
|
|
|
var peers peer.IDSlice
|
|
|
|
for _, peer := range host.Peerstore().Peers() {
|
|
|
|
protocols, err := host.Peerstore().SupportsProtocols(peer, protocolId)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("error obtaining the protocols supported by peers", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(protocols) > 0 {
|
|
|
|
peers = append(peers, peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(peers) >= 1 {
|
2021-10-28 19:26:25 +00:00
|
|
|
// TODO: proper heuristic here that compares peer scores and selects "best" one. For now a random peer for the given protocol is returned
|
|
|
|
return &peers[rand.Intn(len(peers))], nil // nolint: gosec
|
2021-06-16 10:14:22 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 23:34:04 +00:00
|
|
|
return nil, ErrNoPeersAvailable
|
|
|
|
}
|
|
|
|
|
|
|
|
type pingResult struct {
|
|
|
|
p peer.ID
|
|
|
|
rtt time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func SelectPeerWithLowestRTT(ctx context.Context, host host.Host, protocolId string) (*peer.ID, error) {
|
|
|
|
var peers peer.IDSlice
|
|
|
|
for _, peer := range host.Peerstore().Peers() {
|
|
|
|
protocols, err := host.Peerstore().SupportsProtocols(peer, protocolId)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("error obtaining the protocols supported by peers", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(protocols) > 0 {
|
|
|
|
peers = append(peers, peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
waitCh := make(chan struct{})
|
|
|
|
pingCh := make(chan pingResult, 1000)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for _, p := range peers {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(p peer.ID) {
|
|
|
|
defer wg.Done()
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
result := <-ping.Ping(ctx, host, p)
|
|
|
|
if result.Error == nil {
|
|
|
|
pingCh <- pingResult{
|
|
|
|
p: p,
|
|
|
|
rtt: result.RTT,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(p)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
close(waitCh)
|
|
|
|
close(pingCh)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-waitCh:
|
|
|
|
var min *pingResult
|
|
|
|
for p := range pingCh {
|
|
|
|
if min == nil {
|
|
|
|
min = &p
|
|
|
|
} else {
|
|
|
|
if p.rtt < min.rtt {
|
|
|
|
min = &p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if min == nil {
|
|
|
|
return nil, ErrNoPeersAvailable
|
|
|
|
} else {
|
|
|
|
return &min.p, nil
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ErrNoPeersAvailable
|
|
|
|
}
|
2021-06-16 10:14:22 +00:00
|
|
|
}
|
2021-11-16 14:22:01 +00:00
|
|
|
|
|
|
|
func EnodeToMultiAddr(node *enode.Node) (ma.Multiaddr, error) {
|
|
|
|
peerID, err := peer.IDFromPublicKey(&ECDSAPublicKey{node.Pubkey()})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", node.IP(), node.TCP(), peerID))
|
|
|
|
}
|
|
|
|
|
|
|
|
func EnodeToPeerInfo(node *enode.Node) (*peer.AddrInfo, error) {
|
|
|
|
address, err := EnodeToMultiAddr(node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return peer.AddrInfoFromP2pAddr(address)
|
|
|
|
}
|