New p2p package (#3196)

This commit is contained in:
Preston Van Loon
2019-08-13 17:12:00 -04:00
committed by terence tsao
parent 82efca9b6f
commit d2186726a3
6 changed files with 177 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"config.go",
"doc.go",
"log.go",
"service.go",
],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/p2p",
visibility = ["//beacon-chain:__subpackages__"],
deps = [
"//shared:go_default_library",
"@com_github_libp2p_go_libp2p//:go_default_library",
"@com_github_libp2p_go_libp2p_core//host:go_default_library",
"@com_github_libp2p_go_libp2p_pubsub//:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)
go_test(
name = "go_default_test",
srcs = ["service_test.go"],
embed = [":go_default_library"],
deps = [
"//shared/testutil:go_default_library",
"@com_github_sirupsen_logrus//hooks/test:go_default_library",
],
)

View File

@@ -0,0 +1,16 @@
package p2p
// Config for the p2p service. These parameters are set from application level flags
// to initialize the p2p service.
type Config struct {
NoDiscovery bool
StaticPeers []string
BootstrapNodeAddr string
RelayNodeAddr string
HostAddress string
PrivateKey string
Port uint
MaxPeers uint
WhitelistCIDR string
EnableUPnP bool
}

11
beacon-chain/p2p/doc.go Normal file
View File

@@ -0,0 +1,11 @@
/*
Package p2p implements the Ethereum 2.0 networking specification.
Canonical spec reference: https://github.com/ethereum/eth2.0-specs/blob/dev/specs/networking/p2p-interface.md
Prysm specific implementation design docs
- Networking Design Doc: https://docs.google.com/document/d/1VyhobQRkEjEkEPxmmdWvaHfKWn0j6dEae_wLZlrFtfU/view
This package is heavily utilizes the libp2p go implementation by Protocol Labs.
*/
package p2p

5
beacon-chain/p2p/log.go Normal file
View File

@@ -0,0 +1,5 @@
package p2p
import "github.com/sirupsen/logrus"
var log = logrus.WithField("prefix", "p2p")

View File

@@ -0,0 +1,77 @@
package p2p
import (
"context"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/host"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/shared"
)
var _ = shared.Service(&Service{})
// Service for managing peer to peer (p2p) networking.
type Service struct {
ctx context.Context
cancel context.CancelFunc
started bool
cfg *Config
startupErr error
host host.Host
pubsub *pubsub.PubSub
}
// 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
}
s.started = true
// TODO(3147): Add host options
h, err := libp2p.New(s.ctx)
if err != nil {
s.startupErr = err
return
}
s.host = h
// TODO(3147): Add gossip sub options
gs, err := pubsub.NewGossipSub(s.ctx, s.host)
if err != nil {
s.startupErr = err
return
}
s.pubsub = gs
}
// Stop the p2p service and terminate all peer connections.
func (s *Service) Stop() error {
s.started = false
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
}

View File

@@ -0,0 +1,37 @@
package p2p
import (
"testing"
"github.com/prysmaticlabs/prysm/shared/testutil"
logTest "github.com/sirupsen/logrus/hooks/test"
)
func TestService_Stop_SetsStartedToFalse(t *testing.T) {
s, _ := NewService(nil)
s.started = true
_ = s.Stop()
if s.started != false {
t.Error("Expected Service.started to be false, got true")
}
}
func TestService_Start_OnlyStartsOnce(t *testing.T) {
hook := logTest.NewGlobal()
s, _ := NewService(nil)
defer s.Stop()
s.Start()
if s.started != true {
t.Error("Expected service to be started")
}
s.Start()
testutil.AssertLogsContain(t, hook, "Attempted to start p2p service when it was already started")
}
func TestService_Status_NotRunning(t *testing.T) {
s := &Service{started: false}
if s.Status().Error() != "not running" {
t.Errorf("Status returned wrong error, got %v", s.Status())
}
}