2019-08-13 21:12:00 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-08-21 06:08:30 +00:00
|
|
|
"time"
|
2019-08-13 21:12:00 +00:00
|
|
|
|
2019-08-21 06:08:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
2019-08-19 21:20:56 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-08-13 21:12:00 +00:00
|
|
|
"github.com/libp2p/go-libp2p"
|
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
2019-08-16 17:13:04 +00:00
|
|
|
network "github.com/libp2p/go-libp2p-core/network"
|
2019-08-16 20:03:11 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2019-08-16 17:13:04 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/protocol"
|
2019-08-13 21:12:00 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2019-08-21 06:08:30 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2019-08-13 21:12:00 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-08-16 17:13:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder"
|
2019-08-13 21:12:00 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared"
|
2019-08-19 21:20:56 +00:00
|
|
|
deprecatedp2p "github.com/prysmaticlabs/prysm/shared/deprecated-p2p"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
2019-08-13 21:12:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = shared.Service(&Service{})
|
2019-08-21 06:08:30 +00:00
|
|
|
var pollingPeriod = 1 * time.Second
|
2019-08-13 21:12:00 +00:00
|
|
|
|
|
|
|
// Service for managing peer to peer (p2p) networking.
|
|
|
|
type Service struct {
|
2019-08-21 06:08:30 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
started bool
|
|
|
|
cfg *Config
|
|
|
|
startupErr error
|
|
|
|
dv5Listener Listener
|
|
|
|
host host.Host
|
|
|
|
pubsub *pubsub.PubSub
|
2019-08-13 21:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService initializes a new p2p service compatible with shared.Service interface. No
|
|
|
|
// connections are made until the Start function is called during the service registry startup.
|
|
|
|
func NewService(cfg *Config) (*Service, error) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
return &Service{
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
cfg: cfg,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the p2p service.
|
|
|
|
func (s *Service) Start() {
|
|
|
|
if s.started {
|
|
|
|
log.Error("Attempted to start p2p service when it was already started")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-21 06:08:30 +00:00
|
|
|
ipAddr := ipAddr(s.cfg)
|
|
|
|
privKey, err := privKey(s.cfg)
|
|
|
|
if err != nil {
|
|
|
|
s.startupErr = err
|
2019-08-23 21:46:54 +00:00
|
|
|
log.WithError(err).Error("Failed to generate p2p private key")
|
2019-08-21 06:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-13 21:12:00 +00:00
|
|
|
// TODO(3147): Add host options
|
2019-08-21 06:08:30 +00:00
|
|
|
opts := buildOptions(s.cfg, ipAddr, privKey)
|
|
|
|
h, err := libp2p.New(s.ctx, opts...)
|
2019-08-13 21:12:00 +00:00
|
|
|
if err != nil {
|
|
|
|
s.startupErr = err
|
2019-08-23 21:46:54 +00:00
|
|
|
log.WithError(err).Error("Failed to create p2p host")
|
2019-08-13 21:12:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
s.host = h
|
2019-08-23 21:59:59 +00:00
|
|
|
if s.cfg.BootstrapNodeAddr != "" && !s.cfg.NoDiscovery {
|
2019-08-21 20:58:38 +00:00
|
|
|
listener, err := startDiscoveryV5(ipAddr, privKey, s.cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to start discovery")
|
|
|
|
s.startupErr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.dv5Listener = listener
|
2019-08-21 06:08:30 +00:00
|
|
|
|
2019-08-21 20:58:38 +00:00
|
|
|
go s.listenForNewNodes()
|
|
|
|
}
|
2019-08-13 21:12:00 +00:00
|
|
|
|
2019-08-22 15:23:16 +00:00
|
|
|
if len(s.cfg.StaticPeers) > 0 {
|
|
|
|
addrs, err := manyMultiAddrsFromString(s.cfg.StaticPeers)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not connect to static peer: %v", err)
|
|
|
|
}
|
|
|
|
s.connectWithAllPeers(addrs)
|
|
|
|
}
|
|
|
|
|
2019-08-13 21:12:00 +00:00
|
|
|
// TODO(3147): Add gossip sub options
|
|
|
|
gs, err := pubsub.NewGossipSub(s.ctx, s.host)
|
|
|
|
if err != nil {
|
|
|
|
s.startupErr = err
|
2019-08-21 20:58:38 +00:00
|
|
|
|
|
|
|
log.WithError(err).Error("Failed to start pubsub")
|
2019-08-13 21:12:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
s.pubsub = gs
|
2019-08-21 20:58:38 +00:00
|
|
|
|
|
|
|
s.started = true
|
2019-08-22 15:23:16 +00:00
|
|
|
|
2019-08-24 16:07:03 +00:00
|
|
|
registerMetrics(s)
|
|
|
|
|
2019-08-22 15:23:16 +00:00
|
|
|
multiAddrs := s.host.Network().ListenAddresses()
|
|
|
|
log.Infof("Node currently listening at %s", multiAddrs[1].String())
|
2019-08-13 21:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the p2p service and terminate all peer connections.
|
|
|
|
func (s *Service) Stop() error {
|
|
|
|
s.started = false
|
2019-08-21 06:08:30 +00:00
|
|
|
s.dv5Listener.Close()
|
2019-08-13 21:12:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status of the p2p service. Will return an error if the service is considered unhealthy to
|
|
|
|
// indicate that this node should not serve traffic until the issue has been resolved.
|
|
|
|
func (s *Service) Status() error {
|
|
|
|
if !s.started {
|
|
|
|
return errors.New("not running")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-16 17:13:04 +00:00
|
|
|
|
2019-08-21 20:58:38 +00:00
|
|
|
// Started returns true if the p2p service has successfully started.
|
|
|
|
func (s *Service) Started() bool {
|
|
|
|
return s.started
|
|
|
|
}
|
|
|
|
|
2019-08-16 17:13:04 +00:00
|
|
|
// Encoding returns the configured networking encoding.
|
|
|
|
func (s *Service) Encoding() encoder.NetworkEncoding {
|
2019-08-21 16:33:48 +00:00
|
|
|
encoding := s.cfg.Encoding
|
|
|
|
switch encoding {
|
|
|
|
case encoder.SSZ:
|
|
|
|
return &encoder.SszNetworkEncoder{}
|
|
|
|
case encoder.SSZSnappy:
|
|
|
|
return &encoder.SszNetworkEncoder{UseSnappyCompression: true}
|
|
|
|
default:
|
|
|
|
panic("Invalid Network Encoding Flag Provided")
|
|
|
|
}
|
2019-08-16 17:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PubSub returns the p2p pubsub framework.
|
|
|
|
func (s *Service) PubSub() *pubsub.PubSub {
|
|
|
|
return s.pubsub
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetStreamHandler sets the protocol handler on the p2p host multiplexer.
|
|
|
|
// This method is a pass through to libp2pcore.Host.SetStreamHandler.
|
|
|
|
func (s *Service) SetStreamHandler(topic string, handler network.StreamHandler) {
|
|
|
|
s.host.SetStreamHandler(protocol.ID(topic), handler)
|
|
|
|
}
|
2019-08-16 20:03:11 +00:00
|
|
|
|
|
|
|
// Disconnect from a peer.
|
|
|
|
func (s *Service) Disconnect(pid peer.ID) error {
|
2019-08-23 16:53:38 +00:00
|
|
|
return s.host.Network().ClosePeer(pid)
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|
2019-08-19 21:20:56 +00:00
|
|
|
|
2019-08-21 06:08:30 +00:00
|
|
|
// listen for new nodes watches for new nodes in the network and adds them to the peerstore.
|
|
|
|
func (s *Service) listenForNewNodes() {
|
|
|
|
node, err := discv5.ParseNode(s.cfg.BootstrapNodeAddr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not parse bootstrap address: %v", err)
|
|
|
|
}
|
|
|
|
nodeID := node.ID
|
|
|
|
ticker := time.NewTicker(pollingPeriod)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
nodes := s.dv5Listener.Lookup(nodeID)
|
|
|
|
multiAddresses := convertToMultiAddr(nodes)
|
|
|
|
s.connectWithAllPeers(multiAddresses)
|
|
|
|
// store furthest node as the next to lookup
|
|
|
|
nodeID = nodes[len(nodes)-1].ID
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
log.Debug("p2p context is closed, exiting routine")
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) connectWithAllPeers(multiAddrs []ma.Multiaddr) {
|
|
|
|
addrInfos, err := peer.AddrInfosFromP2pAddrs(multiAddrs...)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not convert to peer address info's from multiaddresses: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, info := range addrInfos {
|
|
|
|
if info.ID == s.host.ID() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := s.host.Connect(s.ctx, info); err != nil {
|
|
|
|
log.Errorf("Could not connect with peer: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 21:20:56 +00:00
|
|
|
// Subscribe to some topic.
|
|
|
|
// TODO(3147): Remove
|
|
|
|
// DEPRECATED: Do not use.
|
|
|
|
func (s *Service) Subscribe(_ proto.Message, _ chan deprecatedp2p.Message) event.Subscription {
|
|
|
|
return nil
|
|
|
|
}
|