mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 13:28:01 -05:00
Compare commits
23 Commits
ba2333069a
...
revamp-ser
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2361dff95e | ||
|
|
2b81953117 | ||
|
|
f597366760 | ||
|
|
91d1bf0f0f | ||
|
|
40983b291a | ||
|
|
dd74e82e56 | ||
|
|
a7d7052d5d | ||
|
|
0c86e7bee5 | ||
|
|
a37eb3a00b | ||
|
|
34985f2d12 | ||
|
|
dc38ed5f5f | ||
|
|
f8bfe7c564 | ||
|
|
2bf9473005 | ||
|
|
be1780ebe9 | ||
|
|
b3a08148e0 | ||
|
|
5609c71e0d | ||
|
|
4134b55380 | ||
|
|
3884f85b20 | ||
|
|
1e8c7e7795 | ||
|
|
7ae997bfbc | ||
|
|
f8cddcd626 | ||
|
|
5b49e169d7 | ||
|
|
7d81c7515a |
@@ -26,7 +26,6 @@ go_library(
|
||||
"//beacon-chain/forkchoice:go_default_library",
|
||||
"//beacon-chain/forkchoice/protoarray:go_default_library",
|
||||
"//beacon-chain/gateway:go_default_library",
|
||||
"//beacon-chain/node/registration:go_default_library",
|
||||
"//beacon-chain/operations/attestations:go_default_library",
|
||||
"//beacon-chain/operations/slashings:go_default_library",
|
||||
"//beacon-chain/operations/synccommittee:go_default_library",
|
||||
@@ -42,9 +41,9 @@ go_library(
|
||||
"//beacon-chain/sync/initial-sync:go_default_library",
|
||||
"//cmd:go_default_library",
|
||||
"//cmd/beacon-chain/flags:go_default_library",
|
||||
"//cmd/beacon-chain/p2p:go_default_library",
|
||||
"//config/features:go_default_library",
|
||||
"//config/params:go_default_library",
|
||||
"//container/slice:go_default_library",
|
||||
"//encoding/bytesutil:go_default_library",
|
||||
"//monitoring/backup:go_default_library",
|
||||
"//monitoring/prometheus:go_default_library",
|
||||
@@ -74,6 +73,7 @@ go_test(
|
||||
"//beacon-chain/core/feed/state:go_default_library",
|
||||
"//cmd:go_default_library",
|
||||
"//cmd/beacon-chain/flags:go_default_library",
|
||||
"//cmd/beacon-chain/p2p:go_default_library",
|
||||
"//config/params:go_default_library",
|
||||
"//testing/assert:go_default_library",
|
||||
"//testing/require:go_default_library",
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
types "github.com/prysmaticlabs/eth2-types"
|
||||
"github.com/prysmaticlabs/prysm/cmd"
|
||||
"github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags"
|
||||
p2pcmd "github.com/prysmaticlabs/prysm/cmd/beacon-chain/p2p"
|
||||
"github.com/prysmaticlabs/prysm/config/params"
|
||||
tracing2 "github.com/prysmaticlabs/prysm/monitoring/tracing"
|
||||
"github.com/urfave/cli/v2"
|
||||
@@ -72,9 +73,9 @@ func configureEth1Config(cliCtx *cli.Context) {
|
||||
}
|
||||
|
||||
func configureNetwork(cliCtx *cli.Context) {
|
||||
if cliCtx.IsSet(cmd.BootstrapNode.Name) {
|
||||
if cliCtx.IsSet(p2pcmd.BootstrapNode.Name) {
|
||||
c := params.BeaconNetworkConfig()
|
||||
c.BootstrapNodes = cliCtx.StringSlice(cmd.BootstrapNode.Name)
|
||||
c.BootstrapNodes = cliCtx.StringSlice(p2pcmd.BootstrapNode.Name)
|
||||
params.OverrideBeaconNetworkConfig(c)
|
||||
}
|
||||
if cliCtx.IsSet(flags.ContractDeploymentBlock.Name) {
|
||||
|
||||
@@ -8,8 +8,8 @@ import (
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
types "github.com/prysmaticlabs/eth2-types"
|
||||
"github.com/prysmaticlabs/prysm/cmd"
|
||||
"github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags"
|
||||
p2pcmd "github.com/prysmaticlabs/prysm/cmd/beacon-chain/p2p"
|
||||
"github.com/prysmaticlabs/prysm/config/params"
|
||||
"github.com/prysmaticlabs/prysm/testing/assert"
|
||||
"github.com/prysmaticlabs/prysm/testing/require"
|
||||
@@ -100,10 +100,10 @@ func TestConfigureNetwork(t *testing.T) {
|
||||
app := cli.App{}
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
bootstrapNodes := cli.StringSlice{}
|
||||
set.Var(&bootstrapNodes, cmd.BootstrapNode.Name, "")
|
||||
set.Var(&bootstrapNodes, p2pcmd.BootstrapNode.Name, "")
|
||||
set.Int(flags.ContractDeploymentBlock.Name, 0, "")
|
||||
require.NoError(t, set.Set(cmd.BootstrapNode.Name, "node1"))
|
||||
require.NoError(t, set.Set(cmd.BootstrapNode.Name, "node2"))
|
||||
require.NoError(t, set.Set(p2pcmd.BootstrapNode.Name, "node1"))
|
||||
require.NoError(t, set.Set(p2pcmd.BootstrapNode.Name, "node2"))
|
||||
require.NoError(t, set.Set(flags.ContractDeploymentBlock.Name, strconv.Itoa(100)))
|
||||
cliCtx := cli.NewContext(&app, set, nil)
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@ import (
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/forkchoice"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/gateway"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/node/registration"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/operations/slashings"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/operations/synccommittee"
|
||||
@@ -46,7 +45,6 @@ import (
|
||||
"github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags"
|
||||
"github.com/prysmaticlabs/prysm/config/features"
|
||||
"github.com/prysmaticlabs/prysm/config/params"
|
||||
"github.com/prysmaticlabs/prysm/container/slice"
|
||||
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
||||
"github.com/prysmaticlabs/prysm/monitoring/backup"
|
||||
"github.com/prysmaticlabs/prysm/monitoring/prometheus"
|
||||
@@ -69,6 +67,7 @@ const debugGrpcMaxMsgSize = 1 << 27
|
||||
type serviceFlagOpts struct {
|
||||
blockchainFlagOpts []blockchain.Option
|
||||
powchainFlagOpts []powchain.Option
|
||||
p2pFlagOpts []p2p.Option
|
||||
}
|
||||
|
||||
// BeaconNode defines a struct that handles the services running a random beacon chain
|
||||
@@ -163,7 +162,7 @@ func New(cliCtx *cli.Context, opts ...Option) (*BeaconNode, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := beacon.registerP2P(cliCtx); err != nil {
|
||||
if err := beacon.registerP2P(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -464,33 +463,13 @@ func (b *BeaconNode) startStateGen() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *BeaconNode) registerP2P(cliCtx *cli.Context) error {
|
||||
bootstrapNodeAddrs, dataDir, err := registration.P2PPreregistration(cliCtx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
svc, err := p2p.NewService(b.ctx, &p2p.Config{
|
||||
NoDiscovery: cliCtx.Bool(cmd.NoDiscovery.Name),
|
||||
StaticPeers: slice.SplitCommaSeparated(cliCtx.StringSlice(cmd.StaticPeers.Name)),
|
||||
BootstrapNodeAddr: bootstrapNodeAddrs,
|
||||
RelayNodeAddr: cliCtx.String(cmd.RelayNode.Name),
|
||||
DataDir: dataDir,
|
||||
LocalIP: cliCtx.String(cmd.P2PIP.Name),
|
||||
HostAddress: cliCtx.String(cmd.P2PHost.Name),
|
||||
HostDNS: cliCtx.String(cmd.P2PHostDNS.Name),
|
||||
PrivateKey: cliCtx.String(cmd.P2PPrivKey.Name),
|
||||
MetaDataDir: cliCtx.String(cmd.P2PMetadata.Name),
|
||||
TCPPort: cliCtx.Uint(cmd.P2PTCPPort.Name),
|
||||
UDPPort: cliCtx.Uint(cmd.P2PUDPPort.Name),
|
||||
MaxPeers: cliCtx.Uint(cmd.P2PMaxPeers.Name),
|
||||
AllowListCIDR: cliCtx.String(cmd.P2PAllowList.Name),
|
||||
DenyListCIDR: slice.SplitCommaSeparated(cliCtx.StringSlice(cmd.P2PDenyList.Name)),
|
||||
EnableUPnP: cliCtx.Bool(cmd.EnableUPnPFlag.Name),
|
||||
DisableDiscv5: cliCtx.Bool(flags.DisableDiscv5.Name),
|
||||
StateNotifier: b,
|
||||
DB: b.db,
|
||||
})
|
||||
func (b *BeaconNode) registerP2P() error {
|
||||
opts := append(
|
||||
b.serviceFlagOpts.p2pFlagOpts,
|
||||
p2p.WithDatabase(b.db),
|
||||
p2p.WithStateNotifier(b),
|
||||
)
|
||||
svc, err := p2p.NewService(b.ctx, opts...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package node
|
||||
|
||||
import (
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
|
||||
)
|
||||
|
||||
@@ -23,3 +24,11 @@ func WithPowchainFlagOptions(opts []powchain.Option) Option {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithP2PFlagOptions includes functional options for the p2p service related to CLI flags.
|
||||
func WithP2PFlagOptions(opts []p2p.Option) Option {
|
||||
return func(bn *BeaconNode) error {
|
||||
bn.serviceFlagOpts.p2pFlagOpts = opts
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,10 @@ go_library(
|
||||
"p2p.go",
|
||||
],
|
||||
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/node/registration",
|
||||
visibility = ["//beacon-chain/node:__subpackages__"],
|
||||
visibility = [
|
||||
"//beacon-chain/node:__subpackages__",
|
||||
"//cmd/beacon-chain:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
"//cmd:go_default_library",
|
||||
"//config/params:go_default_library",
|
||||
|
||||
@@ -5,7 +5,6 @@ go_library(
|
||||
srcs = [
|
||||
"addr_factory.go",
|
||||
"broadcaster.go",
|
||||
"config.go",
|
||||
"connection_gater.go",
|
||||
"dial_relay_node.go",
|
||||
"discovery.go",
|
||||
@@ -35,6 +34,7 @@ go_library(
|
||||
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/p2p",
|
||||
visibility = [
|
||||
"//beacon-chain:__subpackages__",
|
||||
"//cmd/beacon-chain:__subpackages__",
|
||||
"//testing/endtoend/evaluators:__pkg__",
|
||||
"//testing/fuzz:__pkg__",
|
||||
"//tools:__subpackages__",
|
||||
|
||||
@@ -41,7 +41,7 @@ func TestService_Broadcast(t *testing.T) {
|
||||
host: p1.BHost,
|
||||
pubsub: p1.PubSub(),
|
||||
joinedTopics: map[string]*pubsub.Topic{},
|
||||
cfg: &Config{},
|
||||
cfg: &flagConfig{},
|
||||
genesisTime: time.Now(),
|
||||
genesisValidatorsRoot: bytesutil.PadTo([]byte{'A'}, 32),
|
||||
}
|
||||
@@ -154,7 +154,7 @@ func TestService_BroadcastAttestation(t *testing.T) {
|
||||
host: p1.BHost,
|
||||
pubsub: p1.PubSub(),
|
||||
joinedTopics: map[string]*pubsub.Topic{},
|
||||
cfg: &Config{},
|
||||
cfg: &flagConfig{},
|
||||
genesisTime: time.Now(),
|
||||
genesisValidatorsRoot: bytesutil.PadTo([]byte{'A'}, 32),
|
||||
subnetsLock: make(map[uint64]*sync.RWMutex),
|
||||
@@ -207,7 +207,7 @@ func TestService_BroadcastAttestation(t *testing.T) {
|
||||
|
||||
func TestService_BroadcastAttestationWithDiscoveryAttempts(t *testing.T) {
|
||||
// Setup bootnode.
|
||||
cfg := &Config{}
|
||||
cfg := &flagConfig{}
|
||||
port := 2000
|
||||
cfg.UDPPort = uint(port)
|
||||
_, pkey := createAddrAndPrivKey(t)
|
||||
@@ -236,7 +236,7 @@ func TestService_BroadcastAttestationWithDiscoveryAttempts(t *testing.T) {
|
||||
var listeners []*discover.UDPv5
|
||||
var hosts []host.Host
|
||||
// setup other nodes.
|
||||
cfg = &Config{
|
||||
cfg = &flagConfig{
|
||||
BootstrapNodeAddr: []string{bootNode.String()},
|
||||
Discv5BootStrapAddr: []string{bootNode.String()},
|
||||
MaxPeers: 30,
|
||||
@@ -297,7 +297,7 @@ func TestService_BroadcastAttestationWithDiscoveryAttempts(t *testing.T) {
|
||||
pubsub: ps1,
|
||||
dv5Listener: listeners[0],
|
||||
joinedTopics: map[string]*pubsub.Topic{},
|
||||
cfg: &Config{},
|
||||
cfg: &flagConfig{},
|
||||
genesisTime: time.Now(),
|
||||
genesisValidatorsRoot: bytesutil.PadTo([]byte{'A'}, 32),
|
||||
subnetsLock: make(map[uint64]*sync.RWMutex),
|
||||
@@ -313,7 +313,7 @@ func TestService_BroadcastAttestationWithDiscoveryAttempts(t *testing.T) {
|
||||
pubsub: ps2,
|
||||
dv5Listener: listeners[1],
|
||||
joinedTopics: map[string]*pubsub.Topic{},
|
||||
cfg: &Config{},
|
||||
cfg: &flagConfig{},
|
||||
genesisTime: time.Now(),
|
||||
genesisValidatorsRoot: bytesutil.PadTo([]byte{'A'}, 32),
|
||||
subnetsLock: make(map[uint64]*sync.RWMutex),
|
||||
@@ -378,7 +378,7 @@ func TestService_BroadcastSyncCommittee(t *testing.T) {
|
||||
host: p1.BHost,
|
||||
pubsub: p1.PubSub(),
|
||||
joinedTopics: map[string]*pubsub.Topic{},
|
||||
cfg: &Config{},
|
||||
cfg: &flagConfig{},
|
||||
genesisTime: time.Now(),
|
||||
genesisValidatorsRoot: bytesutil.PadTo([]byte{'A'}, 32),
|
||||
subnetsLock: make(map[uint64]*sync.RWMutex),
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
||||
)
|
||||
|
||||
// Config for the p2p service. These parameters are set from application level flags
|
||||
// to initialize the p2p service.
|
||||
type Config struct {
|
||||
NoDiscovery bool
|
||||
EnableUPnP bool
|
||||
DisableDiscv5 bool
|
||||
StaticPeers []string
|
||||
BootstrapNodeAddr []string
|
||||
Discv5BootStrapAddr []string
|
||||
RelayNodeAddr string
|
||||
LocalIP string
|
||||
HostAddress string
|
||||
HostDNS string
|
||||
PrivateKey string
|
||||
DataDir string
|
||||
MetaDataDir string
|
||||
TCPPort uint
|
||||
UDPPort uint
|
||||
MaxPeers uint
|
||||
AllowListCIDR string
|
||||
DenyListCIDR []string
|
||||
StateNotifier statefeed.Notifier
|
||||
DB db.ReadOnlyDatabase
|
||||
}
|
||||
@@ -97,7 +97,7 @@ var privateCIDRList = []string{
|
||||
|
||||
// configureFilter looks at the provided allow lists and
|
||||
// deny lists to appropriately create a filter.
|
||||
func configureFilter(cfg *Config) (*multiaddr.Filters, error) {
|
||||
func configureFilter(cfg *flagConfig) (*multiaddr.Filters, error) {
|
||||
addrFilter := multiaddr.NewFilters()
|
||||
var privErr error
|
||||
switch {
|
||||
|
||||
@@ -36,8 +36,8 @@ func TestPeer_AtMaxLimit(t *testing.T) {
|
||||
},
|
||||
},
|
||||
})
|
||||
s.cfg = &Config{MaxPeers: 0}
|
||||
s.addrFilter, err = configureFilter(&Config{})
|
||||
s.cfg = &flagConfig{MaxPeers: 0}
|
||||
s.addrFilter, err = configureFilter(&flagConfig{})
|
||||
require.NoError(t, err)
|
||||
h1, err := libp2p.New(context.Background(), []libp2p.Option{privKeyOption(pkey), libp2p.ListenAddrs(listen), libp2p.ConnectionGater(s)}...)
|
||||
require.NoError(t, err)
|
||||
@@ -77,7 +77,7 @@ func TestService_InterceptBannedIP(t *testing.T) {
|
||||
}),
|
||||
}
|
||||
var err error
|
||||
s.addrFilter, err = configureFilter(&Config{})
|
||||
s.addrFilter, err = configureFilter(&flagConfig{})
|
||||
require.NoError(t, err)
|
||||
ip := "212.67.10.122"
|
||||
multiAddress, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, 3000))
|
||||
@@ -104,10 +104,10 @@ func TestService_RejectInboundPeersBeyondLimit(t *testing.T) {
|
||||
ScorerParams: &scorers.Config{},
|
||||
}),
|
||||
host: mockp2p.NewTestP2P(t).BHost,
|
||||
cfg: &Config{MaxPeers: uint(limit)},
|
||||
cfg: &flagConfig{MaxPeers: uint(limit)},
|
||||
}
|
||||
var err error
|
||||
s.addrFilter, err = configureFilter(&Config{})
|
||||
s.addrFilter, err = configureFilter(&flagConfig{})
|
||||
require.NoError(t, err)
|
||||
ip := "212.67.10.122"
|
||||
multiAddress, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, 3000))
|
||||
@@ -150,8 +150,8 @@ func TestPeer_BelowMaxLimit(t *testing.T) {
|
||||
},
|
||||
},
|
||||
})
|
||||
s.cfg = &Config{MaxPeers: 1}
|
||||
s.addrFilter, err = configureFilter(&Config{})
|
||||
s.cfg = &flagConfig{MaxPeers: 1}
|
||||
s.addrFilter, err = configureFilter(&flagConfig{})
|
||||
require.NoError(t, err)
|
||||
h1, err := libp2p.New(context.Background(), []libp2p.Option{privKeyOption(pkey), libp2p.ListenAddrs(listen), libp2p.ConnectionGater(s)}...)
|
||||
require.NoError(t, err)
|
||||
@@ -196,7 +196,7 @@ func TestPeerAllowList(t *testing.T) {
|
||||
ScorerParams: &scorers.Config{},
|
||||
}),
|
||||
}
|
||||
s.addrFilter, err = configureFilter(&Config{AllowListCIDR: cidr})
|
||||
s.addrFilter, err = configureFilter(&flagConfig{AllowListCIDR: cidr})
|
||||
require.NoError(t, err)
|
||||
h1, err := libp2p.New(context.Background(), []libp2p.Option{privKeyOption(pkey), libp2p.ListenAddrs(listen), libp2p.ConnectionGater(s)}...)
|
||||
require.NoError(t, err)
|
||||
@@ -242,7 +242,7 @@ func TestPeerDenyList(t *testing.T) {
|
||||
ScorerParams: &scorers.Config{},
|
||||
}),
|
||||
}
|
||||
s.addrFilter, err = configureFilter(&Config{DenyListCIDR: []string{cidr}})
|
||||
s.addrFilter, err = configureFilter(&flagConfig{DenyListCIDR: []string{cidr}})
|
||||
require.NoError(t, err)
|
||||
h1, err := libp2p.New(context.Background(), []libp2p.Option{privKeyOption(pkey), libp2p.ListenAddrs(listen), libp2p.ConnectionGater(s)}...)
|
||||
require.NoError(t, err)
|
||||
@@ -279,7 +279,7 @@ func TestService_InterceptAddrDial_Allow(t *testing.T) {
|
||||
}
|
||||
var err error
|
||||
cidr := "212.67.89.112/16"
|
||||
s.addrFilter, err = configureFilter(&Config{AllowListCIDR: cidr})
|
||||
s.addrFilter, err = configureFilter(&flagConfig{AllowListCIDR: cidr})
|
||||
require.NoError(t, err)
|
||||
ip := "212.67.10.122"
|
||||
multiAddress, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, 3000))
|
||||
@@ -301,7 +301,7 @@ func TestService_InterceptAddrDial_Public(t *testing.T) {
|
||||
//test with public filter
|
||||
cidr := "public"
|
||||
ip := "212.67.10.122"
|
||||
s.addrFilter, err = configureFilter(&Config{AllowListCIDR: cidr})
|
||||
s.addrFilter, err = configureFilter(&flagConfig{AllowListCIDR: cidr})
|
||||
require.NoError(t, err)
|
||||
multiAddress, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, 3000))
|
||||
require.NoError(t, err)
|
||||
@@ -321,7 +321,7 @@ func TestService_InterceptAddrDial_Public(t *testing.T) {
|
||||
//test with public allow filter, with a public address added to the deny list
|
||||
invalidPublicIp := "212.67.10.122"
|
||||
validPublicIp := "91.65.69.69"
|
||||
s.addrFilter, err = configureFilter(&Config{AllowListCIDR: "public", DenyListCIDR: []string{"212.67.89.112/16"}})
|
||||
s.addrFilter, err = configureFilter(&flagConfig{AllowListCIDR: "public", DenyListCIDR: []string{"212.67.89.112/16"}})
|
||||
require.NoError(t, err)
|
||||
multiAddress, err = ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", validPublicIp, 3000))
|
||||
require.NoError(t, err)
|
||||
@@ -348,7 +348,7 @@ func TestService_InterceptAddrDial_Private(t *testing.T) {
|
||||
var err error
|
||||
//test with private filter
|
||||
cidr := "private"
|
||||
s.addrFilter, err = configureFilter(&Config{DenyListCIDR: []string{cidr}})
|
||||
s.addrFilter, err = configureFilter(&flagConfig{DenyListCIDR: []string{cidr}})
|
||||
require.NoError(t, err)
|
||||
ip := "212.67.10.122"
|
||||
multiAddress, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, 3000))
|
||||
@@ -377,7 +377,7 @@ func TestService_InterceptAddrDial_AllowPrivate(t *testing.T) {
|
||||
var err error
|
||||
//test with private filter
|
||||
cidr := "private"
|
||||
s.addrFilter, err = configureFilter(&Config{AllowListCIDR: cidr})
|
||||
s.addrFilter, err = configureFilter(&flagConfig{AllowListCIDR: cidr})
|
||||
require.NoError(t, err)
|
||||
ip := "212.67.10.122"
|
||||
multiAddress, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, 3000))
|
||||
@@ -406,7 +406,7 @@ func TestService_InterceptAddrDial_DenyPublic(t *testing.T) {
|
||||
var err error
|
||||
//test with private filter
|
||||
cidr := "public"
|
||||
s.addrFilter, err = configureFilter(&Config{DenyListCIDR: []string{cidr}})
|
||||
s.addrFilter, err = configureFilter(&flagConfig{DenyListCIDR: []string{cidr}})
|
||||
require.NoError(t, err)
|
||||
ip := "212.67.10.122"
|
||||
multiAddress, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, 3000))
|
||||
@@ -435,7 +435,7 @@ func TestService_InterceptAddrDial_AllowConflict(t *testing.T) {
|
||||
var err error
|
||||
//test with private filter
|
||||
cidr := "public"
|
||||
s.addrFilter, err = configureFilter(&Config{DenyListCIDR: []string{cidr, "192.168.0.0/16"}})
|
||||
s.addrFilter, err = configureFilter(&flagConfig{DenyListCIDR: []string{cidr, "192.168.0.0/16"}})
|
||||
require.NoError(t, err)
|
||||
ip := "212.67.10.122"
|
||||
multiAddress, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, 3000))
|
||||
|
||||
@@ -55,7 +55,7 @@ func createAddrAndPrivKey(t *testing.T) (net.IP, *ecdsa.PrivateKey) {
|
||||
randNum := rand.Int()
|
||||
tempPath := path.Join(temp, strconv.Itoa(randNum))
|
||||
require.NoError(t, os.Mkdir(tempPath, 0700))
|
||||
pkey, err := privKey(&Config{DataDir: tempPath})
|
||||
pkey, err := privKey(&flagConfig{DataDir: tempPath})
|
||||
require.NoError(t, err, "Could not get private key")
|
||||
return ipAddr, pkey
|
||||
}
|
||||
@@ -66,7 +66,7 @@ func TestCreateListener(t *testing.T) {
|
||||
s := &Service{
|
||||
genesisTime: time.Now(),
|
||||
genesisValidatorsRoot: bytesutil.PadTo([]byte{'A'}, 32),
|
||||
cfg: &Config{UDPPort: uint(port)},
|
||||
cfg: &flagConfig{UDPPort: uint(port)},
|
||||
}
|
||||
listener, err := s.createListener(ipAddr, pkey)
|
||||
require.NoError(t, err)
|
||||
@@ -90,7 +90,7 @@ func TestStartDiscV5_DiscoverAllPeers(t *testing.T) {
|
||||
genesisTime := time.Now()
|
||||
genesisValidatorsRoot := make([]byte, 32)
|
||||
s := &Service{
|
||||
cfg: &Config{UDPPort: uint(port)},
|
||||
cfg: &flagConfig{UDPPort: uint(port)},
|
||||
genesisTime: genesisTime,
|
||||
genesisValidatorsRoot: genesisValidatorsRoot,
|
||||
}
|
||||
@@ -103,7 +103,7 @@ func TestStartDiscV5_DiscoverAllPeers(t *testing.T) {
|
||||
var listeners []*discover.UDPv5
|
||||
for i := 1; i <= 5; i++ {
|
||||
port = 3000 + i
|
||||
cfg := &Config{
|
||||
cfg := &flagConfig{
|
||||
Discv5BootStrapAddr: []string{bootNode.String()},
|
||||
UDPPort: uint(port),
|
||||
}
|
||||
@@ -152,7 +152,7 @@ func TestMultiAddrConversion_OK(t *testing.T) {
|
||||
hook := logTest.NewGlobal()
|
||||
ipAddr, pkey := createAddrAndPrivKey(t)
|
||||
s := &Service{
|
||||
cfg: &Config{
|
||||
cfg: &flagConfig{
|
||||
TCPPort: 0,
|
||||
UDPPort: 0,
|
||||
},
|
||||
@@ -170,7 +170,7 @@ func TestMultiAddrConversion_OK(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStaticPeering_PeersAreAdded(t *testing.T) {
|
||||
cfg := &Config{
|
||||
cfg := &flagConfig{
|
||||
MaxPeers: 30,
|
||||
}
|
||||
port := 6000
|
||||
@@ -194,10 +194,11 @@ func TestStaticPeering_PeersAreAdded(t *testing.T) {
|
||||
cfg.TCPPort = 14500
|
||||
cfg.UDPPort = 14501
|
||||
cfg.StaticPeers = staticPeers
|
||||
cfg.StateNotifier = &mock.MockStateNotifier{}
|
||||
cfg.NoDiscovery = true
|
||||
s, err := NewService(context.Background(), cfg)
|
||||
s, err := NewService(context.Background())
|
||||
require.NoError(t, err)
|
||||
s.cfg = cfg
|
||||
s.cfg.stateNotifier = &mock.MockStateNotifier{}
|
||||
|
||||
exitRoutine := make(chan bool)
|
||||
go func() {
|
||||
@@ -207,7 +208,7 @@ func TestStaticPeering_PeersAreAdded(t *testing.T) {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
// Send in a loop to ensure it is delivered (busy wait for the service to subscribe to the state feed).
|
||||
for sent := 0; sent == 0; {
|
||||
sent = s.stateNotifier.StateFeed().Send(&feed.Event{
|
||||
sent = s.cfg.stateNotifier.StateFeed().Send(&feed.Event{
|
||||
Type: statefeed.Initialized,
|
||||
Data: &statefeed.InitializedData{
|
||||
StartTime: time.Now(),
|
||||
@@ -229,7 +230,7 @@ func TestHostIsResolved(t *testing.T) {
|
||||
exampleIP := "93.184.216.34"
|
||||
|
||||
s := &Service{
|
||||
cfg: &Config{
|
||||
cfg: &flagConfig{
|
||||
HostDNS: exampleHost,
|
||||
},
|
||||
genesisTime: time.Now(),
|
||||
@@ -246,7 +247,7 @@ func TestHostIsResolved(t *testing.T) {
|
||||
func TestInboundPeerLimit(t *testing.T) {
|
||||
fakePeer := testp2p.NewTestP2P(t)
|
||||
s := &Service{
|
||||
cfg: &Config{MaxPeers: 30},
|
||||
cfg: &flagConfig{MaxPeers: 30},
|
||||
ipLimiter: leakybucket.NewCollector(ipLimit, ipBurst, false),
|
||||
peers: peers.NewStatus(context.Background(), &peers.StatusConfig{
|
||||
PeerLimit: 30,
|
||||
@@ -275,7 +276,7 @@ func TestUDPMultiAddress(t *testing.T) {
|
||||
genesisTime := time.Now()
|
||||
genesisValidatorsRoot := make([]byte, 32)
|
||||
s := &Service{
|
||||
cfg: &Config{UDPPort: uint(port)},
|
||||
cfg: &flagConfig{UDPPort: uint(port)},
|
||||
genesisTime: genesisTime,
|
||||
genesisValidatorsRoot: genesisValidatorsRoot,
|
||||
}
|
||||
@@ -362,7 +363,7 @@ func TestRefreshENR_ForkBoundaries(t *testing.T) {
|
||||
s := &Service{
|
||||
genesisTime: time.Now(),
|
||||
genesisValidatorsRoot: bytesutil.PadTo([]byte{'A'}, 32),
|
||||
cfg: &Config{UDPPort: uint(port)},
|
||||
cfg: &flagConfig{UDPPort: uint(port)},
|
||||
}
|
||||
listener, err := s.createListener(ipAddr, pkey)
|
||||
assert.NoError(t, err)
|
||||
@@ -383,7 +384,7 @@ func TestRefreshENR_ForkBoundaries(t *testing.T) {
|
||||
s := &Service{
|
||||
genesisTime: time.Now(),
|
||||
genesisValidatorsRoot: bytesutil.PadTo([]byte{'A'}, 32),
|
||||
cfg: &Config{UDPPort: uint(port)},
|
||||
cfg: &flagConfig{UDPPort: uint(port)},
|
||||
}
|
||||
listener, err := s.createListener(ipAddr, pkey)
|
||||
assert.NoError(t, err)
|
||||
@@ -405,7 +406,7 @@ func TestRefreshENR_ForkBoundaries(t *testing.T) {
|
||||
s := &Service{
|
||||
genesisTime: time.Now().Add(-5 * oneEpochDuration()),
|
||||
genesisValidatorsRoot: bytesutil.PadTo([]byte{'A'}, 32),
|
||||
cfg: &Config{UDPPort: uint(port)},
|
||||
cfg: &flagConfig{UDPPort: uint(port)},
|
||||
}
|
||||
listener, err := s.createListener(ipAddr, pkey)
|
||||
assert.NoError(t, err)
|
||||
@@ -436,7 +437,7 @@ func TestRefreshENR_ForkBoundaries(t *testing.T) {
|
||||
s := &Service{
|
||||
genesisTime: time.Now().Add(-5 * oneEpochDuration()),
|
||||
genesisValidatorsRoot: bytesutil.PadTo([]byte{'A'}, 32),
|
||||
cfg: &Config{UDPPort: uint(port)},
|
||||
cfg: &flagConfig{UDPPort: uint(port)},
|
||||
}
|
||||
listener, err := s.createListener(ipAddr, pkey)
|
||||
assert.NoError(t, err)
|
||||
@@ -466,7 +467,7 @@ func TestRefreshENR_ForkBoundaries(t *testing.T) {
|
||||
s := &Service{
|
||||
genesisTime: time.Now().Add(-6 * oneEpochDuration()),
|
||||
genesisValidatorsRoot: bytesutil.PadTo([]byte{'A'}, 32),
|
||||
cfg: &Config{UDPPort: uint(port)},
|
||||
cfg: &flagConfig{UDPPort: uint(port)},
|
||||
}
|
||||
listener, err := s.createListener(ipAddr, pkey)
|
||||
assert.NoError(t, err)
|
||||
|
||||
@@ -32,9 +32,9 @@ func TestStartDiscv5_DifferentForkDigests(t *testing.T) {
|
||||
genesisTime := time.Now()
|
||||
genesisValidatorsRoot := make([]byte, 32)
|
||||
s := &Service{
|
||||
cfg: &Config{
|
||||
cfg: &flagConfig{
|
||||
UDPPort: uint(port),
|
||||
StateNotifier: &mock.MockStateNotifier{},
|
||||
stateNotifier: &mock.MockStateNotifier{},
|
||||
},
|
||||
genesisTime: genesisTime,
|
||||
genesisValidatorsRoot: genesisValidatorsRoot,
|
||||
@@ -44,10 +44,9 @@ func TestStartDiscv5_DifferentForkDigests(t *testing.T) {
|
||||
defer bootListener.Close()
|
||||
|
||||
bootNode := bootListener.Self()
|
||||
cfg := &Config{
|
||||
cfg := &flagConfig{
|
||||
Discv5BootStrapAddr: []string{bootNode.String()},
|
||||
UDPPort: uint(port),
|
||||
StateNotifier: &mock.MockStateNotifier{},
|
||||
}
|
||||
|
||||
var listeners []*discover.UDPv5
|
||||
@@ -66,6 +65,7 @@ func TestStartDiscv5_DifferentForkDigests(t *testing.T) {
|
||||
genesisTime: genesisTime,
|
||||
genesisValidatorsRoot: root,
|
||||
}
|
||||
s.cfg.stateNotifier = &mock.MockStateNotifier{}
|
||||
listener, err := s.startDiscoveryV5(ipAddr, pkey)
|
||||
assert.NoError(t, err, "Could not start discovery for node")
|
||||
listeners = append(listeners, listener)
|
||||
@@ -92,8 +92,9 @@ func TestStartDiscv5_DifferentForkDigests(t *testing.T) {
|
||||
cfg.UDPPort = 14000
|
||||
cfg.TCPPort = 14001
|
||||
cfg.MaxPeers = 30
|
||||
s, err = NewService(context.Background(), cfg)
|
||||
s, err = NewService(context.Background())
|
||||
require.NoError(t, err)
|
||||
s.cfg = cfg
|
||||
s.genesisTime = genesisTime
|
||||
s.genesisValidatorsRoot = make([]byte, 32)
|
||||
s.dv5Listener = lastListener
|
||||
@@ -120,17 +121,19 @@ func TestStartDiscv5_SameForkDigests_DifferentNextForkData(t *testing.T) {
|
||||
genesisTime := time.Now()
|
||||
genesisValidatorsRoot := make([]byte, 32)
|
||||
s := &Service{
|
||||
cfg: &Config{UDPPort: uint(port)},
|
||||
cfg: &flagConfig{
|
||||
UDPPort: uint(port),
|
||||
stateNotifier: &mock.MockStateNotifier{},
|
||||
},
|
||||
genesisTime: genesisTime,
|
||||
genesisValidatorsRoot: genesisValidatorsRoot,
|
||||
stateNotifier: &mock.MockStateNotifier{},
|
||||
}
|
||||
bootListener, err := s.createListener(ipAddr, pkey)
|
||||
require.NoError(t, err)
|
||||
defer bootListener.Close()
|
||||
|
||||
bootNode := bootListener.Self()
|
||||
cfg := &Config{
|
||||
cfg := &flagConfig{
|
||||
Discv5BootStrapAddr: []string{bootNode.String()},
|
||||
UDPPort: uint(port),
|
||||
}
|
||||
@@ -154,8 +157,8 @@ func TestStartDiscv5_SameForkDigests_DifferentNextForkData(t *testing.T) {
|
||||
cfg: cfg,
|
||||
genesisTime: genesisTime,
|
||||
genesisValidatorsRoot: genesisValidatorsRoot,
|
||||
stateNotifier: &mock.MockStateNotifier{},
|
||||
}
|
||||
s.cfg.stateNotifier = &mock.MockStateNotifier{}
|
||||
listener, err := s.startDiscoveryV5(ipAddr, pkey)
|
||||
assert.NoError(t, err, "Could not start discovery for node")
|
||||
listeners = append(listeners, listener)
|
||||
@@ -182,9 +185,10 @@ func TestStartDiscv5_SameForkDigests_DifferentNextForkData(t *testing.T) {
|
||||
cfg.UDPPort = 14000
|
||||
cfg.TCPPort = 14001
|
||||
cfg.MaxPeers = 30
|
||||
cfg.StateNotifier = &mock.MockStateNotifier{}
|
||||
s, err = NewService(context.Background(), cfg)
|
||||
cfg.stateNotifier = &mock.MockStateNotifier{}
|
||||
s, err = NewService(context.Background())
|
||||
require.NoError(t, err)
|
||||
s.cfg = cfg
|
||||
|
||||
s.genesisTime = genesisTime
|
||||
s.genesisValidatorsRoot = make([]byte, 32)
|
||||
@@ -235,7 +239,7 @@ func TestDiscv5_AddRetrieveForkEntryENR(t *testing.T) {
|
||||
randNum := rand.Int()
|
||||
tempPath := path.Join(temp, strconv.Itoa(randNum))
|
||||
require.NoError(t, os.Mkdir(tempPath, 0700))
|
||||
pkey, err := privKey(&Config{DataDir: tempPath})
|
||||
pkey, err := privKey(&flagConfig{DataDir: tempPath})
|
||||
require.NoError(t, err, "Could not get private key")
|
||||
db, err := enode.OpenDB("")
|
||||
require.NoError(t, err)
|
||||
@@ -258,7 +262,7 @@ func TestAddForkEntry_Genesis(t *testing.T) {
|
||||
randNum := rand.Int()
|
||||
tempPath := path.Join(temp, strconv.Itoa(randNum))
|
||||
require.NoError(t, os.Mkdir(tempPath, 0700))
|
||||
pkey, err := privKey(&Config{DataDir: tempPath})
|
||||
pkey, err := privKey(&flagConfig{DataDir: tempPath})
|
||||
require.NoError(t, err, "Could not get private key")
|
||||
db, err := enode.OpenDB("")
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -125,9 +125,9 @@ func (s *Service) retrieveActiveValidators() (uint64, error) {
|
||||
if s.activeValidatorCount != 0 {
|
||||
return s.activeValidatorCount, nil
|
||||
}
|
||||
rt := s.cfg.DB.LastArchivedRoot(s.ctx)
|
||||
rt := s.cfg.db.LastArchivedRoot(s.ctx)
|
||||
if rt == params.BeaconConfig().ZeroHash {
|
||||
genState, err := s.cfg.DB.GenesisState(s.ctx)
|
||||
genState, err := s.cfg.db.GenesisState(s.ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -142,7 +142,7 @@ func (s *Service) retrieveActiveValidators() (uint64, error) {
|
||||
s.activeValidatorCount = activeVals
|
||||
return activeVals, nil
|
||||
}
|
||||
bState, err := s.cfg.DB.State(s.ctx, rt)
|
||||
bState, err := s.cfg.db.State(s.ctx, rt)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
@@ -23,7 +23,9 @@ func TestCorrect_ActiveValidatorsCount(t *testing.T) {
|
||||
db := dbutil.SetupDB(t)
|
||||
s := &Service{
|
||||
ctx: context.Background(),
|
||||
cfg: &Config{DB: db},
|
||||
cfg: &flagConfig{
|
||||
db: db,
|
||||
},
|
||||
}
|
||||
bState, err := util.NewBeaconState(func(state *ethpb.BeaconState) error {
|
||||
validators := make([]*ethpb.Validator, params.BeaconConfig().MinGenesisActiveValidatorCount)
|
||||
|
||||
@@ -11,9 +11,166 @@ import (
|
||||
"github.com/libp2p/go-tcp-transport"
|
||||
ma "github.com/multiformats/go-multiaddr"
|
||||
"github.com/pkg/errors"
|
||||
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
||||
"github.com/prysmaticlabs/prysm/runtime/version"
|
||||
)
|
||||
|
||||
// Option for p2p configurations.
|
||||
type Option func(s *Service) error
|
||||
|
||||
// WithDatabase for beacon chain db access.
|
||||
func WithDatabase(beaconDB db.ReadOnlyDatabase) Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.db = beaconDB
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithStateNotifier for subscribing to beacon state events.
|
||||
func WithStateNotifier(notifier statefeed.Notifier) Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.stateNotifier = notifier
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithEnableUPnP for p2p.
|
||||
func WithEnableUPnP() Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.EnableUPnP = true
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithNoDiscovery for p2p.
|
||||
func WithNoDiscovery() Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.NoDiscovery = true
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithStaticPeers for p2p.
|
||||
func WithStaticPeers(peers []string) Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.StaticPeers = peers
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithBootstrapNodeAddr for p2p.
|
||||
func WithBootstrapNodeAddr(addresses []string) Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.BootstrapNodeAddr = addresses
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithRelayNodeAddr for p2p.
|
||||
func WithRelayNodeAddr(addr string) Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.RelayNodeAddr = addr
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithDataDir for the beacon node data directory.
|
||||
func WithDataDir(dir string) Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.DataDir = dir
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithLocalIP for p2p.
|
||||
func WithLocalIP(ip string) Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.LocalIP = ip
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithHostAddr for p2p.
|
||||
func WithHostAddr(addr string) Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.HostAddress = addr
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithHostDNS for p2p.
|
||||
func WithHostDNS(host string) Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.HostDNS = host
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithPrivateKey for p2p.
|
||||
func WithPrivateKey(privKey string) Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.PrivateKey = privKey
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithMetadataDir for p2p.
|
||||
func WithMetadataDir(dir string) Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.MetaDataDir = dir
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithTCPPort for p2p.
|
||||
func WithTCPPort(port uint) Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.TCPPort = port
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithUDPPort for p2p.
|
||||
func WithUDPPort(port uint) Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.UDPPort = port
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithMaxPeers for p2p.
|
||||
func WithMaxPeers(maxPeers uint) Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.MaxPeers = maxPeers
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithAllowListCIDR for p2p.
|
||||
func WithAllowListCIDR(allowList string) Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.AllowListCIDR = allowList
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithDenyListCIDR for p2p.
|
||||
func WithDenyListCIDR(denyList []string) Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.DenyListCIDR = denyList
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithDisableDiscv5 for p2p.
|
||||
func WithDisableDiscv5() Option {
|
||||
return func(s *Service) error {
|
||||
s.cfg.DisableDiscv5 = true
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// buildOptions for the libp2p host.
|
||||
func (s *Service) buildOptions(ip net.IP, priKey *ecdsa.PrivateKey) []libp2p.Option {
|
||||
cfg := s.cfg
|
||||
|
||||
@@ -30,7 +30,7 @@ func TestPrivateKeyLoading(t *testing.T) {
|
||||
err = ioutil.WriteFile(file.Name(), []byte(out), params.BeaconIoConfig().ReadWritePermissions)
|
||||
require.NoError(t, err, "Could not write key to file")
|
||||
log.WithField("file", file.Name()).WithField("key", out).Info("Wrote key to file")
|
||||
cfg := &Config{
|
||||
cfg := &flagConfig{
|
||||
PrivateKey: file.Name(),
|
||||
}
|
||||
pKey, err := privKey(cfg)
|
||||
|
||||
@@ -331,9 +331,7 @@ func TestService_MonitorsStateForkUpdates(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
notifier := &mock.MockStateNotifier{}
|
||||
s, err := NewService(ctx, &Config{
|
||||
StateNotifier: notifier,
|
||||
})
|
||||
s, err := NewService(ctx, WithStateNotifier(&mock.MockStateNotifier{}))
|
||||
require.NoError(t, err)
|
||||
|
||||
require.False(t, s.isInitialized())
|
||||
|
||||
@@ -16,15 +16,14 @@ import (
|
||||
)
|
||||
|
||||
func TestService_PublishToTopicConcurrentMapWrite(t *testing.T) {
|
||||
s, err := NewService(context.Background(), &Config{
|
||||
StateNotifier: &mock.MockStateNotifier{},
|
||||
})
|
||||
s, err := NewService(context.Background())
|
||||
require.NoError(t, err)
|
||||
s.cfg.stateNotifier = &mock.MockStateNotifier{}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
go s.awaitStateInitialized()
|
||||
fd := initializeStateWithForkDigest(ctx, t, s.stateNotifier.StateFeed())
|
||||
fd := initializeStateWithForkDigest(ctx, t, s.cfg.stateNotifier.StateFeed())
|
||||
|
||||
if !s.isInitialized() {
|
||||
t.Fatal("service was not initialized")
|
||||
|
||||
@@ -23,7 +23,7 @@ func TestService_Send(t *testing.T) {
|
||||
|
||||
svc := &Service{
|
||||
host: p1.BHost,
|
||||
cfg: &Config{},
|
||||
cfg: &flagConfig{},
|
||||
}
|
||||
|
||||
msg := ðpb.Fork{
|
||||
|
||||
@@ -25,6 +25,7 @@ import (
|
||||
"github.com/prysmaticlabs/prysm/async"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/core/feed"
|
||||
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/peers"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/peers/scorers"
|
||||
@@ -58,13 +59,36 @@ const pubsubQueueSize = 600
|
||||
// maxDialTimeout is the timeout for a single peer dial.
|
||||
var maxDialTimeout = params.BeaconNetworkConfig().RespTimeout
|
||||
|
||||
type flagConfig struct {
|
||||
NoDiscovery bool
|
||||
EnableUPnP bool
|
||||
DisableDiscv5 bool
|
||||
StaticPeers []string
|
||||
BootstrapNodeAddr []string
|
||||
Discv5BootStrapAddr []string
|
||||
RelayNodeAddr string
|
||||
LocalIP string
|
||||
HostAddress string
|
||||
HostDNS string
|
||||
PrivateKey string
|
||||
DataDir string
|
||||
MetaDataDir string
|
||||
TCPPort uint
|
||||
UDPPort uint
|
||||
MaxPeers uint
|
||||
AllowListCIDR string
|
||||
DenyListCIDR []string
|
||||
db db.ReadOnlyDatabase
|
||||
stateNotifier statefeed.Notifier
|
||||
}
|
||||
|
||||
// Service for managing peer to peer (p2p) networking.
|
||||
type Service struct {
|
||||
started bool
|
||||
isPreGenesis bool
|
||||
pingMethod func(ctx context.Context, id peer.ID) error
|
||||
cancel context.CancelFunc
|
||||
cfg *Config
|
||||
cfg *flagConfig
|
||||
peers *peers.Status
|
||||
addrFilter *multiaddr.Filters
|
||||
ipLimiter *leakybucket.Collector
|
||||
@@ -78,7 +102,6 @@ type Service struct {
|
||||
initializationLock sync.Mutex
|
||||
dv5Listener Listener
|
||||
startupErr error
|
||||
stateNotifier statefeed.Notifier
|
||||
ctx context.Context
|
||||
host host.Host
|
||||
genesisTime time.Time
|
||||
@@ -88,24 +111,29 @@ type Service struct {
|
||||
|
||||
// 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(ctx context.Context, cfg *Config) (*Service, error) {
|
||||
func NewService(ctx context.Context, opts ...Option) (*Service, error) {
|
||||
var err error
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
_ = cancel // govet fix for lost cancel. Cancel is handled in service.Stop().
|
||||
|
||||
s := &Service{
|
||||
ctx: ctx,
|
||||
stateNotifier: cfg.StateNotifier,
|
||||
cancel: cancel,
|
||||
cfg: cfg,
|
||||
isPreGenesis: true,
|
||||
joinedTopics: make(map[string]*pubsub.Topic, len(gossipTopicMappings)),
|
||||
subnetsLock: make(map[uint64]*sync.RWMutex),
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
cfg: &flagConfig{},
|
||||
isPreGenesis: true,
|
||||
joinedTopics: make(map[string]*pubsub.Topic, len(gossipTopicMappings)),
|
||||
subnetsLock: make(map[uint64]*sync.RWMutex),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
if err := opt(s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
dv5Nodes := parseBootStrapAddrs(s.cfg.BootstrapNodeAddr)
|
||||
|
||||
cfg.Discv5BootStrapAddr = dv5Nodes
|
||||
s.cfg.Discv5BootStrapAddr = dv5Nodes
|
||||
|
||||
ipAddr := ipAddr()
|
||||
s.privKey, err = privKey(s.cfg)
|
||||
@@ -125,8 +153,8 @@ func NewService(ctx context.Context, cfg *Config) (*Service, error) {
|
||||
}
|
||||
s.ipLimiter = leakybucket.NewCollector(ipLimit, ipBurst, true /* deleteEmptyBuckets */)
|
||||
|
||||
opts := s.buildOptions(ipAddr, s.privKey)
|
||||
h, err := libp2p.New(s.ctx, opts...)
|
||||
libp2pOpts := s.buildOptions(ipAddr, s.privKey)
|
||||
h, err := libp2p.New(s.ctx, libp2pOpts...)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to create p2p host")
|
||||
return nil, err
|
||||
@@ -401,7 +429,7 @@ func (s *Service) awaitStateInitialized() {
|
||||
}
|
||||
|
||||
stateChannel := make(chan *feed.Event, 1)
|
||||
stateSub := s.stateNotifier.StateFeed().Subscribe(stateChannel)
|
||||
stateSub := s.cfg.stateNotifier.StateFeed().Subscribe(stateChannel)
|
||||
cleanup := stateSub.Unsubscribe
|
||||
defer cleanup()
|
||||
for {
|
||||
|
||||
@@ -81,7 +81,7 @@ func createHost(t *testing.T, port int) (host.Host, *ecdsa.PrivateKey, net.IP) {
|
||||
}
|
||||
|
||||
func TestService_Stop_SetsStartedToFalse(t *testing.T) {
|
||||
s, err := NewService(context.Background(), &Config{StateNotifier: &mock.MockStateNotifier{}})
|
||||
s, err := NewService(context.Background(), WithStateNotifier(&mock.MockStateNotifier{}))
|
||||
require.NoError(t, err)
|
||||
s.started = true
|
||||
s.dv5Listener = &mockListener{}
|
||||
@@ -90,7 +90,7 @@ func TestService_Stop_SetsStartedToFalse(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestService_Stop_DontPanicIfDv5ListenerIsNotInited(t *testing.T) {
|
||||
s, err := NewService(context.Background(), &Config{StateNotifier: &mock.MockStateNotifier{}})
|
||||
s, err := NewService(context.Background(), WithStateNotifier(&mock.MockStateNotifier{}))
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, s.Stop())
|
||||
}
|
||||
@@ -98,14 +98,14 @@ func TestService_Stop_DontPanicIfDv5ListenerIsNotInited(t *testing.T) {
|
||||
func TestService_Start_OnlyStartsOnce(t *testing.T) {
|
||||
hook := logTest.NewGlobal()
|
||||
|
||||
cfg := &Config{
|
||||
cfg := &flagConfig{
|
||||
TCPPort: 2000,
|
||||
UDPPort: 2000,
|
||||
StateNotifier: &mock.MockStateNotifier{},
|
||||
stateNotifier: &mock.MockStateNotifier{},
|
||||
}
|
||||
s, err := NewService(context.Background(), cfg)
|
||||
s, err := NewService(context.Background(), WithStateNotifier(&mock.MockStateNotifier{}))
|
||||
require.NoError(t, err)
|
||||
s.stateNotifier = &mock.MockStateNotifier{}
|
||||
s.cfg = cfg
|
||||
s.dv5Listener = &mockListener{}
|
||||
exitRoutine := make(chan bool)
|
||||
go func() {
|
||||
@@ -114,7 +114,7 @@ func TestService_Start_OnlyStartsOnce(t *testing.T) {
|
||||
}()
|
||||
// Send in a loop to ensure it is delivered (busy wait for the service to subscribe to the state feed).
|
||||
for sent := 0; sent == 0; {
|
||||
sent = s.stateNotifier.StateFeed().Send(&feed.Event{
|
||||
sent = s.cfg.stateNotifier.StateFeed().Send(&feed.Event{
|
||||
Type: statefeed.Initialized,
|
||||
Data: &statefeed.InitializedData{
|
||||
StartTime: time.Now(),
|
||||
@@ -149,9 +149,10 @@ func TestService_Status_NoGenesisTimeSet(t *testing.T) {
|
||||
func TestListenForNewNodes(t *testing.T) {
|
||||
// Setup bootnode.
|
||||
notifier := &mock.MockStateNotifier{}
|
||||
cfg := &Config{StateNotifier: notifier}
|
||||
cfg := &flagConfig{}
|
||||
port := 2000
|
||||
cfg.UDPPort = uint(port)
|
||||
cfg.stateNotifier = notifier
|
||||
_, pkey := createAddrAndPrivKey(t)
|
||||
ipAddr := net.ParseIP("127.0.0.1")
|
||||
genesisTime := prysmTime.Now()
|
||||
@@ -177,16 +178,16 @@ func TestListenForNewNodes(t *testing.T) {
|
||||
var listeners []*discover.UDPv5
|
||||
var hosts []host.Host
|
||||
// setup other nodes.
|
||||
cfg = &Config{
|
||||
cfg = &flagConfig{
|
||||
BootstrapNodeAddr: []string{bootNode.String()},
|
||||
Discv5BootStrapAddr: []string{bootNode.String()},
|
||||
MaxPeers: 30,
|
||||
StateNotifier: notifier,
|
||||
}
|
||||
for i := 1; i <= 5; i++ {
|
||||
h, pkey, ipAddr := createHost(t, port+i)
|
||||
cfg.UDPPort = uint(port + i)
|
||||
cfg.TCPPort = uint(port + i)
|
||||
cfg.stateNotifier = notifier
|
||||
s := &Service{
|
||||
cfg: cfg,
|
||||
genesisTime: genesisTime,
|
||||
@@ -215,9 +216,11 @@ func TestListenForNewNodes(t *testing.T) {
|
||||
|
||||
cfg.UDPPort = 14000
|
||||
cfg.TCPPort = 14001
|
||||
cfg.stateNotifier = &mock.MockStateNotifier{}
|
||||
|
||||
s, err = NewService(context.Background(), cfg)
|
||||
s, err = NewService(context.Background())
|
||||
require.NoError(t, err)
|
||||
s.cfg = cfg
|
||||
exitRoutine := make(chan bool)
|
||||
go func() {
|
||||
s.Start()
|
||||
@@ -226,7 +229,7 @@ func TestListenForNewNodes(t *testing.T) {
|
||||
time.Sleep(1 * time.Second)
|
||||
// Send in a loop to ensure it is delivered (busy wait for the service to subscribe to the state feed).
|
||||
for sent := 0; sent == 0; {
|
||||
sent = s.stateNotifier.StateFeed().Send(&feed.Event{
|
||||
sent = s.cfg.stateNotifier.StateFeed().Send(&feed.Event{
|
||||
Type: statefeed.Initialized,
|
||||
Data: &statefeed.InitializedData{
|
||||
StartTime: genesisTime,
|
||||
@@ -273,11 +276,11 @@ func TestPeer_Disconnect(t *testing.T) {
|
||||
func TestService_JoinLeaveTopic(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
s, err := NewService(ctx, &Config{StateNotifier: &mock.MockStateNotifier{}})
|
||||
s, err := NewService(ctx, WithStateNotifier(&mock.MockStateNotifier{}))
|
||||
require.NoError(t, err)
|
||||
|
||||
go s.awaitStateInitialized()
|
||||
fd := initializeStateWithForkDigest(ctx, t, s.stateNotifier.StateFeed())
|
||||
fd := initializeStateWithForkDigest(ctx, t, s.cfg.stateNotifier.StateFeed())
|
||||
|
||||
assert.Equal(t, 0, len(s.joinedTopics))
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ func TestStartDiscV5_DiscoverPeersWithSubnets(t *testing.T) {
|
||||
genesisTime := time.Now()
|
||||
genesisValidatorsRoot := make([]byte, 32)
|
||||
s := &Service{
|
||||
cfg: &Config{UDPPort: uint(port)},
|
||||
cfg: &flagConfig{UDPPort: uint(port)},
|
||||
genesisTime: genesisTime,
|
||||
genesisValidatorsRoot: genesisValidatorsRoot,
|
||||
}
|
||||
@@ -55,7 +55,7 @@ func TestStartDiscV5_DiscoverPeersWithSubnets(t *testing.T) {
|
||||
var listeners []*discover.UDPv5
|
||||
for i := 1; i <= 3; i++ {
|
||||
port = 3000 + i
|
||||
cfg := &Config{
|
||||
cfg := &flagConfig{
|
||||
BootstrapNodeAddr: []string{bootNode.String()},
|
||||
Discv5BootStrapAddr: []string{bootNode.String()},
|
||||
MaxPeers: 30,
|
||||
@@ -85,14 +85,15 @@ func TestStartDiscV5_DiscoverPeersWithSubnets(t *testing.T) {
|
||||
|
||||
// Make one service on port 4001.
|
||||
port = 4001
|
||||
cfg := &Config{
|
||||
cfg := &flagConfig{
|
||||
BootstrapNodeAddr: []string{bootNode.String()},
|
||||
Discv5BootStrapAddr: []string{bootNode.String()},
|
||||
MaxPeers: 30,
|
||||
UDPPort: uint(port),
|
||||
stateNotifier: &mock.MockStateNotifier{},
|
||||
}
|
||||
cfg.StateNotifier = &mock.MockStateNotifier{}
|
||||
s, err = NewService(context.Background(), cfg)
|
||||
s, err = NewService(context.Background())
|
||||
s.cfg = cfg
|
||||
require.NoError(t, err)
|
||||
exitRoutine := make(chan bool)
|
||||
go func() {
|
||||
@@ -102,7 +103,7 @@ func TestStartDiscV5_DiscoverPeersWithSubnets(t *testing.T) {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
// Send in a loop to ensure it is delivered (busy wait for the service to subscribe to the state feed).
|
||||
for sent := 0; sent == 0; {
|
||||
sent = s.stateNotifier.StateFeed().Send(&feed.Event{
|
||||
sent = s.cfg.stateNotifier.StateFeed().Send(&feed.Event{
|
||||
Type: statefeed.Initialized,
|
||||
Data: &statefeed.InitializedData{
|
||||
StartTime: time.Now(),
|
||||
|
||||
@@ -63,7 +63,7 @@ func convertToInterfacePubkey(pubkey *ecdsa.PublicKey) crypto.PubKey {
|
||||
|
||||
// Determines a private key for p2p networking from the p2p service's
|
||||
// configuration struct. If no key is found, it generates a new one.
|
||||
func privKey(cfg *Config) (*ecdsa.PrivateKey, error) {
|
||||
func privKey(cfg *flagConfig) (*ecdsa.PrivateKey, error) {
|
||||
defaultKeyPath := path.Join(cfg.DataDir, keyPath)
|
||||
privateKeyPath := cfg.PrivateKey
|
||||
|
||||
@@ -109,7 +109,7 @@ func privKeyFromFile(path string) (*ecdsa.PrivateKey, error) {
|
||||
// Retrieves node p2p metadata from a set of configuration values
|
||||
// from the p2p service.
|
||||
// TODO: Figure out how to do a v1/v2 check.
|
||||
func metaDataFromConfig(cfg *Config) (metadata.Metadata, error) {
|
||||
func metaDataFromConfig(cfg *flagConfig) (metadata.Metadata, error) {
|
||||
defaultKeyPath := path.Join(cfg.DataDir, metaDataPath)
|
||||
metaDataPath := cfg.MetaDataDir
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ go_library(
|
||||
"//cmd/beacon-chain/blockchain:go_default_library",
|
||||
"//cmd/beacon-chain/db:go_default_library",
|
||||
"//cmd/beacon-chain/flags:go_default_library",
|
||||
"//cmd/beacon-chain/p2p:go_default_library",
|
||||
"//cmd/beacon-chain/powchain:go_default_library",
|
||||
"//config/features:go_default_library",
|
||||
"//io/file:go_default_library",
|
||||
|
||||
@@ -17,7 +17,6 @@ go_library(
|
||||
"//testing/endtoend:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
"//cmd:go_default_library",
|
||||
"//config/params:go_default_library",
|
||||
"@com_github_sirupsen_logrus//:go_default_library",
|
||||
"@com_github_urfave_cli_v2//:go_default_library",
|
||||
|
||||
@@ -190,6 +190,12 @@ var (
|
||||
Usage: "Sets the minimum number of peers that a node will attempt to peer with that are subscribed to a subnet.",
|
||||
Value: 6,
|
||||
}
|
||||
// P2PMaxPeers defines a flag to specify the max number of peers in libp2p.
|
||||
P2PMaxPeers = &cli.IntFlag{
|
||||
Name: "p2p-max-peers",
|
||||
Usage: "The max number of p2p peers to maintain.",
|
||||
Value: 45,
|
||||
}
|
||||
// TerminalTotalDifficultyOverride specifies the total difficulty to manual overrides the `TERMINAL_TOTAL_DIFFICULTY` parameter.
|
||||
TerminalTotalDifficultyOverride = &cli.Uint64Flag{
|
||||
Name: "terminal-total-difficulty-override",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package flags
|
||||
|
||||
import (
|
||||
"github.com/prysmaticlabs/prysm/cmd"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
@@ -60,7 +59,7 @@ func ConfigureGlobalFlags(ctx *cli.Context) {
|
||||
|
||||
func configureMinimumPeers(ctx *cli.Context, cfg *GlobalFlags) {
|
||||
cfg.MinimumSyncPeers = ctx.Int(MinSyncPeers.Name)
|
||||
maxPeers := ctx.Int(cmd.P2PMaxPeers.Name)
|
||||
maxPeers := ctx.Int(P2PMaxPeers.Name)
|
||||
if cfg.MinimumSyncPeers > maxPeers {
|
||||
log.Warnf("Changing Minimum Sync Peers to %d", maxPeers)
|
||||
cfg.MinimumSyncPeers = maxPeers
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
blockchaincmd "github.com/prysmaticlabs/prysm/cmd/beacon-chain/blockchain"
|
||||
dbcommands "github.com/prysmaticlabs/prysm/cmd/beacon-chain/db"
|
||||
"github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags"
|
||||
p2pcmd "github.com/prysmaticlabs/prysm/cmd/beacon-chain/p2p"
|
||||
powchaincmd "github.com/prysmaticlabs/prysm/cmd/beacon-chain/powchain"
|
||||
"github.com/prysmaticlabs/prysm/config/features"
|
||||
"github.com/prysmaticlabs/prysm/io/file"
|
||||
@@ -65,6 +66,7 @@ var appFlags = []cli.Flag{
|
||||
flags.Eth1HeaderReqLimit,
|
||||
flags.GenesisStatePath,
|
||||
flags.MinPeersPerSubnet,
|
||||
flags.P2PMaxPeers,
|
||||
flags.TerminalTotalDifficultyOverride,
|
||||
flags.TerminalBlockHashOverride,
|
||||
flags.TerminalBlockHashActivationEpochOverride,
|
||||
@@ -74,20 +76,20 @@ var appFlags = []cli.Flag{
|
||||
cmd.MinimalConfigFlag,
|
||||
cmd.E2EConfigFlag,
|
||||
cmd.RPCMaxPageSizeFlag,
|
||||
cmd.BootstrapNode,
|
||||
cmd.NoDiscovery,
|
||||
cmd.StaticPeers,
|
||||
cmd.RelayNode,
|
||||
cmd.P2PUDPPort,
|
||||
cmd.P2PTCPPort,
|
||||
cmd.P2PIP,
|
||||
cmd.P2PHost,
|
||||
cmd.P2PHostDNS,
|
||||
cmd.P2PMaxPeers,
|
||||
cmd.P2PPrivKey,
|
||||
cmd.P2PMetadata,
|
||||
cmd.P2PAllowList,
|
||||
cmd.P2PDenyList,
|
||||
p2pcmd.BootstrapNode,
|
||||
p2pcmd.NoDiscovery,
|
||||
p2pcmd.StaticPeers,
|
||||
p2pcmd.RelayNode,
|
||||
p2pcmd.P2PUDPPort,
|
||||
p2pcmd.P2PTCPPort,
|
||||
p2pcmd.P2PIP,
|
||||
p2pcmd.P2PHost,
|
||||
p2pcmd.P2PHostDNS,
|
||||
p2pcmd.P2PPrivKey,
|
||||
p2pcmd.P2PMetadata,
|
||||
p2pcmd.P2PAllowList,
|
||||
p2pcmd.P2PDenyList,
|
||||
p2pcmd.EnableUPnPFlag,
|
||||
cmd.DataDirFlag,
|
||||
cmd.VerbosityFlag,
|
||||
cmd.EnableTracingFlag,
|
||||
@@ -110,7 +112,6 @@ var appFlags = []cli.Flag{
|
||||
debug.BlockProfileRateFlag,
|
||||
debug.MutexProfileFractionFlag,
|
||||
cmd.LogFileName,
|
||||
cmd.EnableUPnPFlag,
|
||||
cmd.ConfigFileFlag,
|
||||
cmd.ChainConfigFileFlag,
|
||||
cmd.GrpcMaxCallRecvMsgSizeFlag,
|
||||
@@ -234,12 +235,17 @@ func startNode(ctx *cli.Context) error {
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
p2pFlagOpts, err := p2pcmd.FlagOptions(ctx)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
powchainFlagOpts, err := powchaincmd.FlagOptions(ctx)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
opts := []node.Option{
|
||||
node.WithBlockchainFlagOptions(blockchainFlagOpts),
|
||||
node.WithP2PFlagOptions(p2pFlagOpts),
|
||||
node.WithPowchainFlagOptions(powchainFlagOpts),
|
||||
}
|
||||
beacon, err := node.New(ctx, opts...)
|
||||
|
||||
19
cmd/beacon-chain/p2p/BUILD.bazel
Normal file
19
cmd/beacon-chain/p2p/BUILD.bazel
Normal file
@@ -0,0 +1,19 @@
|
||||
load("@prysm//tools/go:def.bzl", "go_library")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"flags.go",
|
||||
"options.go",
|
||||
],
|
||||
importpath = "github.com/prysmaticlabs/prysm/cmd/beacon-chain/p2p",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//beacon-chain/node/registration:go_default_library",
|
||||
"//beacon-chain/p2p:go_default_library",
|
||||
"//cmd/beacon-chain/flags:go_default_library",
|
||||
"//config/params:go_default_library",
|
||||
"//container/slice:go_default_library",
|
||||
"@com_github_urfave_cli_v2//:go_default_library",
|
||||
],
|
||||
)
|
||||
101
cmd/beacon-chain/p2p/flags.go
Normal file
101
cmd/beacon-chain/p2p/flags.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package p2pcmd
|
||||
|
||||
import (
|
||||
"github.com/prysmaticlabs/prysm/config/params"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
// NoDiscovery specifies whether we are running a local network and have no need for connecting
|
||||
// to the bootstrap nodes in the cloud
|
||||
NoDiscovery = &cli.BoolFlag{
|
||||
Name: "no-discovery",
|
||||
Usage: "Enable only local network p2p and do not connect to cloud bootstrap nodes.",
|
||||
}
|
||||
// StaticPeers specifies a set of peers to connect to explicitly.
|
||||
StaticPeers = &cli.StringSliceFlag{
|
||||
Name: "peer",
|
||||
Usage: "Connect with this peer. This flag may be used multiple times.",
|
||||
}
|
||||
// BootstrapNode tells the beacon node which bootstrap node to connect to
|
||||
BootstrapNode = &cli.StringSliceFlag{
|
||||
Name: "bootstrap-node",
|
||||
Usage: "The address of bootstrap node. Beacon node will connect for peer discovery via DHT. Multiple nodes can be passed by using the flag multiple times but not comma-separated. You can also pass YAML files containing multiple nodes.",
|
||||
Value: cli.NewStringSlice(params.BeaconNetworkConfig().BootstrapNodes...),
|
||||
}
|
||||
// RelayNode tells the beacon node which relay node to connect to.
|
||||
RelayNode = &cli.StringFlag{
|
||||
Name: "relay-node",
|
||||
Usage: "The address of relay node. The beacon node will connect to the " +
|
||||
"relay node and advertise their address via the relay node to other peers",
|
||||
Value: "",
|
||||
}
|
||||
// P2PUDPPort defines the port to be used by discv5.
|
||||
P2PUDPPort = &cli.IntFlag{
|
||||
Name: "p2p-udp-port",
|
||||
Usage: "The port used by discv5.",
|
||||
Value: 12000,
|
||||
}
|
||||
// P2PTCPPort defines the port to be used by libp2p.
|
||||
P2PTCPPort = &cli.IntFlag{
|
||||
Name: "p2p-tcp-port",
|
||||
Usage: "The port used by libp2p.",
|
||||
Value: 13000,
|
||||
}
|
||||
// P2PIP defines the local IP to be used by libp2p.
|
||||
P2PIP = &cli.StringFlag{
|
||||
Name: "p2p-local-ip",
|
||||
Usage: "The local ip address to listen for incoming data.",
|
||||
Value: "",
|
||||
}
|
||||
// P2PHost defines the host IP to be used by libp2p.
|
||||
P2PHost = &cli.StringFlag{
|
||||
Name: "p2p-host-ip",
|
||||
Usage: "The IP address advertised by libp2p. This may be used to advertise an external IP.",
|
||||
Value: "",
|
||||
}
|
||||
// P2PHostDNS defines the host DNS to be used by libp2p.
|
||||
P2PHostDNS = &cli.StringFlag{
|
||||
Name: "p2p-host-dns",
|
||||
Usage: "The DNS address advertised by libp2p. This may be used to advertise an external DNS.",
|
||||
Value: "",
|
||||
}
|
||||
// P2PPrivKey defines a flag to specify the location of the private key file for libp2p.
|
||||
P2PPrivKey = &cli.StringFlag{
|
||||
Name: "p2p-priv-key",
|
||||
Usage: "The file containing the private key to use in communications with other peers.",
|
||||
Value: "",
|
||||
}
|
||||
// P2PMetadata defines a flag to specify the location of the peer metadata file.
|
||||
P2PMetadata = &cli.StringFlag{
|
||||
Name: "p2p-metadata",
|
||||
Usage: "The file containing the metadata to communicate with other peers.",
|
||||
Value: "",
|
||||
}
|
||||
// P2PAllowList defines a CIDR subnet to exclusively allow connections.
|
||||
P2PAllowList = &cli.StringFlag{
|
||||
Name: "p2p-allowlist",
|
||||
Usage: "The CIDR subnet for allowing only certain peer connections. " +
|
||||
"Using \"public\" would allow only public subnets. Example: " +
|
||||
"192.168.0.0/16 would permit connections to peers on your local network only. The " +
|
||||
"default is to accept all connections.",
|
||||
}
|
||||
// P2PDenyList defines a list of CIDR subnets to disallow connections from them.
|
||||
P2PDenyList = &cli.StringSliceFlag{
|
||||
Name: "p2p-denylist",
|
||||
Usage: "The CIDR subnets for denying certainy peer connections. " +
|
||||
"Using \"private\" would deny all private subnets. Example: " +
|
||||
"192.168.0.0/16 would deny connections from peers on your local network only. The " +
|
||||
"default is to accept all connections.",
|
||||
}
|
||||
// EnableUPnPFlag specifies if UPnP should be enabled or not. The default value is false.
|
||||
EnableUPnPFlag = &cli.BoolFlag{
|
||||
Name: "enable-upnp",
|
||||
Usage: "Enable the service (Beacon chain or Validator) to use UPnP when possible.",
|
||||
}
|
||||
// DisableDiscv5 disables running discv5.
|
||||
DisableDiscv5 = &cli.BoolFlag{
|
||||
Name: "disable-discv5",
|
||||
Usage: "Does not run the discoveryV5 dht.",
|
||||
}
|
||||
)
|
||||
47
cmd/beacon-chain/p2p/options.go
Normal file
47
cmd/beacon-chain/p2p/options.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package p2pcmd
|
||||
|
||||
import (
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/node/registration"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
||||
"github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags"
|
||||
"github.com/prysmaticlabs/prysm/container/slice"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// FlagOptions for peer-to-peer networking configurations.
|
||||
func FlagOptions(c *cli.Context) ([]p2p.Option, error) {
|
||||
bootstrapNodeAddrs, dataDir, err := registration.P2PPreregistration(c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opts := []p2p.Option{
|
||||
p2p.WithStaticPeers(
|
||||
slice.SplitCommaSeparated(c.StringSlice(StaticPeers.Name)),
|
||||
),
|
||||
p2p.WithBootstrapNodeAddr(bootstrapNodeAddrs),
|
||||
p2p.WithRelayNodeAddr(c.String(RelayNode.Name)),
|
||||
p2p.WithDataDir(dataDir),
|
||||
p2p.WithLocalIP(c.String(P2PIP.Name)),
|
||||
p2p.WithHostAddr(c.String(P2PHost.Name)),
|
||||
p2p.WithHostDNS(c.String(P2PHostDNS.Name)),
|
||||
p2p.WithPrivateKey(c.String(P2PPrivKey.Name)),
|
||||
p2p.WithMetadataDir(c.String(P2PMetadata.Name)),
|
||||
p2p.WithTCPPort(c.Uint(P2PTCPPort.Name)),
|
||||
p2p.WithUDPPort(c.Uint(P2PUDPPort.Name)),
|
||||
p2p.WithMaxPeers(c.Uint(flags.P2PMaxPeers.Name)),
|
||||
p2p.WithAllowListCIDR(c.String(P2PAllowList.Name)),
|
||||
p2p.WithDenyListCIDR(
|
||||
slice.SplitCommaSeparated(c.StringSlice(P2PDenyList.Name)),
|
||||
),
|
||||
}
|
||||
if c.Bool(NoDiscovery.Name) {
|
||||
opts = append(opts, p2p.WithNoDiscovery())
|
||||
}
|
||||
if c.Bool(EnableUPnPFlag.Name) {
|
||||
opts = append(opts, p2p.WithEnableUPnP())
|
||||
}
|
||||
if c.Bool(DisableDiscv5.Name) {
|
||||
opts = append(opts, p2p.WithDisableDiscv5())
|
||||
}
|
||||
return opts, nil
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/prysmaticlabs/prysm/cmd"
|
||||
"github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags"
|
||||
p2pcmd "github.com/prysmaticlabs/prysm/cmd/beacon-chain/p2p"
|
||||
"github.com/prysmaticlabs/prysm/config/features"
|
||||
"github.com/prysmaticlabs/prysm/runtime/debug"
|
||||
"github.com/urfave/cli/v2"
|
||||
@@ -47,11 +48,11 @@ var appHelpFlagGroups = []flagGroup{
|
||||
cmd.MinimalConfigFlag,
|
||||
cmd.E2EConfigFlag,
|
||||
cmd.RPCMaxPageSizeFlag,
|
||||
cmd.NoDiscovery,
|
||||
cmd.BootstrapNode,
|
||||
cmd.RelayNode,
|
||||
cmd.P2PUDPPort,
|
||||
cmd.P2PTCPPort,
|
||||
p2pcmd.NoDiscovery,
|
||||
p2pcmd.BootstrapNode,
|
||||
p2pcmd.RelayNode,
|
||||
p2pcmd.P2PUDPPort,
|
||||
p2pcmd.P2PTCPPort,
|
||||
cmd.DataDirFlag,
|
||||
cmd.VerbosityFlag,
|
||||
cmd.EnableTracingFlag,
|
||||
@@ -136,16 +137,16 @@ var appHelpFlagGroups = []flagGroup{
|
||||
{
|
||||
Name: "p2p",
|
||||
Flags: []cli.Flag{
|
||||
cmd.P2PIP,
|
||||
cmd.P2PHost,
|
||||
cmd.P2PHostDNS,
|
||||
cmd.P2PMaxPeers,
|
||||
cmd.P2PPrivKey,
|
||||
cmd.P2PMetadata,
|
||||
cmd.P2PAllowList,
|
||||
cmd.P2PDenyList,
|
||||
cmd.StaticPeers,
|
||||
cmd.EnableUPnPFlag,
|
||||
p2pcmd.P2PIP,
|
||||
p2pcmd.P2PHost,
|
||||
p2pcmd.P2PHostDNS,
|
||||
flags.P2PMaxPeers,
|
||||
p2pcmd.P2PPrivKey,
|
||||
p2pcmd.P2PMetadata,
|
||||
p2pcmd.P2PAllowList,
|
||||
p2pcmd.P2PDenyList,
|
||||
p2pcmd.StaticPeers,
|
||||
p2pcmd.EnableUPnPFlag,
|
||||
flags.MinSyncPeers,
|
||||
},
|
||||
},
|
||||
|
||||
94
cmd/flags.go
94
cmd/flags.go
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/prysmaticlabs/prysm/config/params"
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/urfave/cli/v2/altsrc"
|
||||
)
|
||||
@@ -83,94 +82,6 @@ var (
|
||||
Name: "disable-monitoring",
|
||||
Usage: "Disable monitoring service.",
|
||||
}
|
||||
// NoDiscovery specifies whether we are running a local network and have no need for connecting
|
||||
// to the bootstrap nodes in the cloud
|
||||
NoDiscovery = &cli.BoolFlag{
|
||||
Name: "no-discovery",
|
||||
Usage: "Enable only local network p2p and do not connect to cloud bootstrap nodes.",
|
||||
}
|
||||
// StaticPeers specifies a set of peers to connect to explicitly.
|
||||
StaticPeers = &cli.StringSliceFlag{
|
||||
Name: "peer",
|
||||
Usage: "Connect with this peer. This flag may be used multiple times.",
|
||||
}
|
||||
// BootstrapNode tells the beacon node which bootstrap node to connect to
|
||||
BootstrapNode = &cli.StringSliceFlag{
|
||||
Name: "bootstrap-node",
|
||||
Usage: "The address of bootstrap node. Beacon node will connect for peer discovery via DHT. Multiple nodes can be passed by using the flag multiple times but not comma-separated. You can also pass YAML files containing multiple nodes.",
|
||||
Value: cli.NewStringSlice(params.BeaconNetworkConfig().BootstrapNodes...),
|
||||
}
|
||||
// RelayNode tells the beacon node which relay node to connect to.
|
||||
RelayNode = &cli.StringFlag{
|
||||
Name: "relay-node",
|
||||
Usage: "The address of relay node. The beacon node will connect to the " +
|
||||
"relay node and advertise their address via the relay node to other peers",
|
||||
Value: "",
|
||||
}
|
||||
// P2PUDPPort defines the port to be used by discv5.
|
||||
P2PUDPPort = &cli.IntFlag{
|
||||
Name: "p2p-udp-port",
|
||||
Usage: "The port used by discv5.",
|
||||
Value: 12000,
|
||||
}
|
||||
// P2PTCPPort defines the port to be used by libp2p.
|
||||
P2PTCPPort = &cli.IntFlag{
|
||||
Name: "p2p-tcp-port",
|
||||
Usage: "The port used by libp2p.",
|
||||
Value: 13000,
|
||||
}
|
||||
// P2PIP defines the local IP to be used by libp2p.
|
||||
P2PIP = &cli.StringFlag{
|
||||
Name: "p2p-local-ip",
|
||||
Usage: "The local ip address to listen for incoming data.",
|
||||
Value: "",
|
||||
}
|
||||
// P2PHost defines the host IP to be used by libp2p.
|
||||
P2PHost = &cli.StringFlag{
|
||||
Name: "p2p-host-ip",
|
||||
Usage: "The IP address advertised by libp2p. This may be used to advertise an external IP.",
|
||||
Value: "",
|
||||
}
|
||||
// P2PHostDNS defines the host DNS to be used by libp2p.
|
||||
P2PHostDNS = &cli.StringFlag{
|
||||
Name: "p2p-host-dns",
|
||||
Usage: "The DNS address advertised by libp2p. This may be used to advertise an external DNS.",
|
||||
Value: "",
|
||||
}
|
||||
// P2PPrivKey defines a flag to specify the location of the private key file for libp2p.
|
||||
P2PPrivKey = &cli.StringFlag{
|
||||
Name: "p2p-priv-key",
|
||||
Usage: "The file containing the private key to use in communications with other peers.",
|
||||
Value: "",
|
||||
}
|
||||
// P2PMetadata defines a flag to specify the location of the peer metadata file.
|
||||
P2PMetadata = &cli.StringFlag{
|
||||
Name: "p2p-metadata",
|
||||
Usage: "The file containing the metadata to communicate with other peers.",
|
||||
Value: "",
|
||||
}
|
||||
// P2PMaxPeers defines a flag to specify the max number of peers in libp2p.
|
||||
P2PMaxPeers = &cli.IntFlag{
|
||||
Name: "p2p-max-peers",
|
||||
Usage: "The max number of p2p peers to maintain.",
|
||||
Value: 45,
|
||||
}
|
||||
// P2PAllowList defines a CIDR subnet to exclusively allow connections.
|
||||
P2PAllowList = &cli.StringFlag{
|
||||
Name: "p2p-allowlist",
|
||||
Usage: "The CIDR subnet for allowing only certain peer connections. " +
|
||||
"Using \"public\" would allow only public subnets. Example: " +
|
||||
"192.168.0.0/16 would permit connections to peers on your local network only. The " +
|
||||
"default is to accept all connections.",
|
||||
}
|
||||
// P2PDenyList defines a list of CIDR subnets to disallow connections from them.
|
||||
P2PDenyList = &cli.StringSliceFlag{
|
||||
Name: "p2p-denylist",
|
||||
Usage: "The CIDR subnets for denying certainy peer connections. " +
|
||||
"Using \"private\" would deny all private subnets. Example: " +
|
||||
"192.168.0.0/16 would deny connections from peers on your local network only. The " +
|
||||
"default is to accept all connections.",
|
||||
}
|
||||
// ForceClearDB removes any previously stored data at the data directory.
|
||||
ForceClearDB = &cli.BoolFlag{
|
||||
Name: "force-clear-db",
|
||||
@@ -198,11 +109,6 @@ var (
|
||||
Name: "log-file",
|
||||
Usage: "Specify log file name, relative or absolute",
|
||||
}
|
||||
// EnableUPnPFlag specifies if UPnP should be enabled or not. The default value is false.
|
||||
EnableUPnPFlag = &cli.BoolFlag{
|
||||
Name: "enable-upnp",
|
||||
Usage: "Enable the service (Beacon chain or Validator) to use UPnP when possible.",
|
||||
}
|
||||
// ConfigFileFlag specifies the filepath to load flag values.
|
||||
ConfigFileFlag = &cli.StringFlag{
|
||||
Name: "config-file",
|
||||
|
||||
@@ -16,6 +16,7 @@ go_library(
|
||||
deps = [
|
||||
"//cmd:go_default_library",
|
||||
"//cmd/beacon-chain/flags:go_default_library",
|
||||
"//cmd/beacon-chain/p2p:go_default_library",
|
||||
"//cmd/validator/flags:go_default_library",
|
||||
"//config/features:go_default_library",
|
||||
"//config/params:go_default_library",
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/bazelbuild/rules_go/go/tools/bazel"
|
||||
cmdshared "github.com/prysmaticlabs/prysm/cmd"
|
||||
"github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags"
|
||||
p2pcmd "github.com/prysmaticlabs/prysm/cmd/beacon-chain/p2p"
|
||||
"github.com/prysmaticlabs/prysm/config/features"
|
||||
"github.com/prysmaticlabs/prysm/config/params"
|
||||
"github.com/prysmaticlabs/prysm/testing/endtoend/helpers"
|
||||
@@ -110,14 +111,14 @@ func (node *BeaconNode) Start(ctx context.Context) error {
|
||||
fmt.Sprintf("--%s=%d", flags.RPCPort.Name, e2e.TestParams.BeaconNodeRPCPort+index),
|
||||
fmt.Sprintf("--%s=http://127.0.0.1:%d", flags.HTTPWeb3ProviderFlag.Name, e2e.TestParams.Eth1RPCPort),
|
||||
fmt.Sprintf("--%s=%d", flags.MinSyncPeers.Name, e2e.TestParams.BeaconNodeCount-1),
|
||||
fmt.Sprintf("--%s=%d", cmdshared.P2PUDPPort.Name, e2e.TestParams.BeaconNodeRPCPort+index+10),
|
||||
fmt.Sprintf("--%s=%d", cmdshared.P2PTCPPort.Name, e2e.TestParams.BeaconNodeRPCPort+index+20),
|
||||
fmt.Sprintf("--%s=%d", p2pcmd.P2PUDPPort.Name, e2e.TestParams.BeaconNodeRPCPort+index+10),
|
||||
fmt.Sprintf("--%s=%d", p2pcmd.P2PTCPPort.Name, e2e.TestParams.BeaconNodeRPCPort+index+20),
|
||||
fmt.Sprintf("--%s=%d", flags.MonitoringPortFlag.Name, e2e.TestParams.BeaconNodeMetricsPort+index),
|
||||
fmt.Sprintf("--%s=%d", flags.GRPCGatewayPort.Name, e2e.TestParams.BeaconNodeRPCPort+index+40),
|
||||
fmt.Sprintf("--%s=%d", flags.ContractDeploymentBlock.Name, 0),
|
||||
fmt.Sprintf("--%s=%d", flags.MinPeersPerSubnet.Name, 0),
|
||||
fmt.Sprintf("--%s=%d", cmdshared.RPCMaxPageSizeFlag.Name, params.BeaconConfig().MinGenesisActiveValidatorCount),
|
||||
fmt.Sprintf("--%s=%s", cmdshared.BootstrapNode.Name, enr),
|
||||
fmt.Sprintf("--%s=%s", p2pcmd.BootstrapNode.Name, enr),
|
||||
fmt.Sprintf("--%s=%s", cmdshared.VerbosityFlag.Name, "debug"),
|
||||
"--" + cmdshared.ForceClearDB.Name,
|
||||
"--" + cmdshared.E2EConfigFlag.Name,
|
||||
|
||||
@@ -24,9 +24,7 @@ func init() {
|
||||
logrus.SetLevel(logrus.PanicLevel)
|
||||
|
||||
var err error
|
||||
p, err = p2p.NewService(context.Background(), &p2p.Config{
|
||||
NoDiscovery: true,
|
||||
})
|
||||
p, err = p2p.NewService(context.Background(), p2p.WithNoDiscovery())
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "could not create new p2p service"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user