mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-28 14:17:17 +00:00
61026103c6
* initial validator attesthead rewrite based on proposer rewrite * proceed with fetching committees and tree hashing the canonical head at assigned attester slot * complete filling the properties of attestation data and all associated root hashes * add when to attest todo * finish entire attester client logic * tests with mocks checked in * tests passing in client * stubbed out server implementation * fixed build due to old property * regen mocks with new mockgen version * fixed broken tests * complete bazel build fix * address some review comments * deep proto test * tests passing after checking for empty committee and crosslink root * address nishant comments
75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
package p2p
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
iaddr "github.com/ipfs/go-ipfs-addr"
|
|
host "github.com/libp2p/go-libp2p-host"
|
|
ps "github.com/libp2p/go-libp2p-peerstore"
|
|
mdns "github.com/libp2p/go-libp2p/p2p/discovery"
|
|
"github.com/sirupsen/logrus"
|
|
"go.opencensus.io/trace"
|
|
)
|
|
|
|
var log = logrus.WithField("prefix", "p2p")
|
|
|
|
// Discovery interval for multicast DNS querying.
|
|
var discoveryInterval = 1 * time.Minute
|
|
|
|
// mDNSTag is the name of the mDNS service.
|
|
var mDNSTag = mdns.ServiceTag
|
|
|
|
// TODO(287): add other discovery protocols such as DHT, etc.
|
|
// startmDNSDiscovery supports discovery via multicast DNS peer discovery.
|
|
func startmDNSDiscovery(ctx context.Context, host host.Host) error {
|
|
mdnsService, err := mdns.NewMdnsService(ctx, host, discoveryInterval, mDNSTag)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
mdnsService.RegisterNotifee(&discovery{ctx, host})
|
|
return nil
|
|
}
|
|
|
|
// startDHTDiscovery supports discovery via DHT.
|
|
func startDHTDiscovery(ctx context.Context, host host.Host, bootstrapAddr string) error {
|
|
ctx, span := trace.StartSpan(ctx, "p2p_startDHTDiscovery")
|
|
defer span.End()
|
|
|
|
addr, err := iaddr.ParseString(bootstrapAddr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
peerinfo, err := ps.InfoFromP2pAddr(addr.Multiaddr())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = host.Connect(ctx, *peerinfo)
|
|
return err
|
|
}
|
|
|
|
// Discovery implements mDNS notifee interface.
|
|
type discovery struct {
|
|
ctx context.Context
|
|
host host.Host
|
|
}
|
|
|
|
// HandlePeerFound registers the peer with the host.
|
|
func (d *discovery) HandlePeerFound(pi ps.PeerInfo) {
|
|
log.WithFields(logrus.Fields{
|
|
"peer addrs": pi.Addrs,
|
|
"peer id": pi.ID,
|
|
}).Debug("Attempting to connect to a peer")
|
|
|
|
d.host.Peerstore().AddAddrs(pi.ID, pi.Addrs, ps.PermanentAddrTTL)
|
|
if err := d.host.Connect(d.ctx, pi); err != nil {
|
|
log.Warnf("Failed to connect to peer: %v", err)
|
|
}
|
|
|
|
log.WithFields(logrus.Fields{
|
|
"peers": d.host.Peerstore().Peers(),
|
|
}).Debug("Peers are now")
|
|
}
|