mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 21:38:05 -05:00
Compare commits
19 Commits
develop2
...
beacon-p2p
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
23d278b7c5 | ||
|
|
a9c1911912 | ||
|
|
7fefa151be | ||
|
|
6c2508472e | ||
|
|
d9afce3861 | ||
|
|
9fd889e48f | ||
|
|
39b81ea555 | ||
|
|
f3eeab7d80 | ||
|
|
33d3f078e8 | ||
|
|
2e5b3c59fa | ||
|
|
bcc02ee913 | ||
|
|
b9064d704f | ||
|
|
17402180f8 | ||
|
|
4f00c0b487 | ||
|
|
f29f76cbf2 | ||
|
|
aab101a4bc | ||
|
|
82c3c11335 | ||
|
|
859b9d8a7e | ||
|
|
6c76232203 |
@@ -226,13 +226,6 @@ func TestService_BroadcastAttestationWithDiscoveryAttempts(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer bootListener.Close()
|
||||
|
||||
// Use shorter period for testing.
|
||||
currentPeriod := pollingPeriod
|
||||
pollingPeriod = 1 * time.Second
|
||||
defer func() {
|
||||
pollingPeriod = currentPeriod
|
||||
}()
|
||||
|
||||
bootNode := bootListener.Self()
|
||||
subnet := uint64(5)
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ func (s *Service) InterceptAddrDial(pid peer.ID, m multiaddr.Multiaddr) (allow b
|
||||
// InterceptAccept checks whether the incidental inbound connection is allowed.
|
||||
func (s *Service) InterceptAccept(n network.ConnMultiaddrs) (allow bool) {
|
||||
// Deny all incoming connections before we are ready
|
||||
if !s.started {
|
||||
if !s.Started() {
|
||||
return false
|
||||
}
|
||||
if !s.validateDial(n.RemoteMultiaddr()) {
|
||||
|
||||
@@ -40,7 +40,7 @@ func TestPeer_AtMaxLimit(t *testing.T) {
|
||||
s.cfg = &Config{MaxPeers: 0}
|
||||
s.addrFilter, err = configureFilter(&Config{})
|
||||
require.NoError(t, err)
|
||||
s.started = true
|
||||
s.started.Store(true)
|
||||
h1, err := libp2p.New([]libp2p.Option{privKeyOption(pkey), libp2p.ListenAddrs(listen), libp2p.ConnectionGater(s)}...)
|
||||
require.NoError(t, err)
|
||||
s.host = h1
|
||||
@@ -84,7 +84,7 @@ func TestService_InterceptBannedIP(t *testing.T) {
|
||||
ip := "212.67.10.122"
|
||||
multiAddress, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, 3000))
|
||||
require.NoError(t, err)
|
||||
s.started = true
|
||||
s.started.Store(true)
|
||||
|
||||
for i := 0; i < ipBurst; i++ {
|
||||
valid := s.validateDial(multiAddress)
|
||||
@@ -122,7 +122,7 @@ func TestService_RejectInboundConnectionBeforeStarted(t *testing.T) {
|
||||
t.Errorf("Expected multiaddress with ip %s to be rejected as p2p service is not ready", ip)
|
||||
}
|
||||
|
||||
s.started = true
|
||||
s.started.Store(true)
|
||||
valid = s.InterceptAccept(&maEndpoints{raddr: multiAddress})
|
||||
if !valid {
|
||||
t.Errorf("Expected multiaddress with ip %s to be accepted after service is started", ip)
|
||||
@@ -146,7 +146,7 @@ func TestService_RejectInboundPeersBeyondLimit(t *testing.T) {
|
||||
ip := "212.67.10.122"
|
||||
multiAddress, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, 3000))
|
||||
require.NoError(t, err)
|
||||
s.started = true
|
||||
s.started.Store(true)
|
||||
|
||||
valid := s.InterceptAccept(&maEndpoints{raddr: multiAddress})
|
||||
if !valid {
|
||||
@@ -191,7 +191,7 @@ func TestPeer_BelowMaxLimit(t *testing.T) {
|
||||
h1, err := libp2p.New([]libp2p.Option{privKeyOption(pkey), libp2p.ListenAddrs(listen), libp2p.ConnectionGater(s)}...)
|
||||
require.NoError(t, err)
|
||||
s.host = h1
|
||||
s.started = true
|
||||
s.started.Store(true)
|
||||
defer func() {
|
||||
err := h1.Close()
|
||||
require.NoError(t, err)
|
||||
@@ -237,7 +237,7 @@ func TestPeerAllowList(t *testing.T) {
|
||||
h1, err := libp2p.New([]libp2p.Option{privKeyOption(pkey), libp2p.ListenAddrs(listen), libp2p.ConnectionGater(s)}...)
|
||||
require.NoError(t, err)
|
||||
s.host = h1
|
||||
s.started = true
|
||||
s.started.Store(true)
|
||||
defer func() {
|
||||
err := h1.Close()
|
||||
require.NoError(t, err)
|
||||
@@ -284,7 +284,7 @@ func TestPeerDenyList(t *testing.T) {
|
||||
h1, err := libp2p.New([]libp2p.Option{privKeyOption(pkey), libp2p.ListenAddrs(listen), libp2p.ConnectionGater(s)}...)
|
||||
require.NoError(t, err)
|
||||
s.host = h1
|
||||
s.started = true
|
||||
s.started.Store(true)
|
||||
defer func() {
|
||||
err := h1.Close()
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||
@@ -54,7 +55,7 @@ var maxDialTimeout = params.BeaconConfig().RespTimeoutDuration()
|
||||
|
||||
// Service for managing peer to peer (p2p) networking.
|
||||
type Service struct {
|
||||
started bool
|
||||
started atomic.Bool
|
||||
isPreGenesis bool
|
||||
pingMethod func(ctx context.Context, id peer.ID) error
|
||||
pingMethodLock sync.RWMutex
|
||||
@@ -173,7 +174,7 @@ func NewService(ctx context.Context, cfg *Config) (*Service, error) {
|
||||
|
||||
// Start the p2p service.
|
||||
func (s *Service) Start() {
|
||||
if s.started {
|
||||
if s.Started() {
|
||||
log.Error("Attempted to start p2p service when it was already started")
|
||||
return
|
||||
}
|
||||
@@ -212,7 +213,7 @@ func (s *Service) Start() {
|
||||
go s.listenForNewNodes()
|
||||
}
|
||||
|
||||
s.started = true
|
||||
s.started.Store(true)
|
||||
|
||||
if len(s.cfg.StaticPeers) > 0 {
|
||||
addrs, err := PeersFromStringAddrs(s.cfg.StaticPeers)
|
||||
@@ -278,7 +279,7 @@ func (s *Service) Start() {
|
||||
// Stop the p2p service and terminate all peer connections.
|
||||
func (s *Service) Stop() error {
|
||||
defer s.cancel()
|
||||
s.started = false
|
||||
s.started.Store(false)
|
||||
if s.dv5Listener != nil {
|
||||
s.dv5Listener.Close()
|
||||
}
|
||||
@@ -291,7 +292,7 @@ func (s *Service) Status() error {
|
||||
if s.isPreGenesis {
|
||||
return nil
|
||||
}
|
||||
if !s.started {
|
||||
if !s.started.Load() {
|
||||
return errors.New("not running")
|
||||
}
|
||||
if s.startupErr != nil {
|
||||
@@ -305,7 +306,7 @@ func (s *Service) Status() error {
|
||||
|
||||
// Started returns true if the p2p service has successfully started.
|
||||
func (s *Service) Started() bool {
|
||||
return s.started
|
||||
return s.started.Load()
|
||||
}
|
||||
|
||||
// Encoding returns the configured networking encoding.
|
||||
|
||||
@@ -83,10 +83,10 @@ func TestService_Stop_SetsStartedToFalse(t *testing.T) {
|
||||
params.SetupTestConfigCleanup(t)
|
||||
s, err := NewService(context.Background(), &Config{StateNotifier: &mock.MockStateNotifier{}})
|
||||
require.NoError(t, err)
|
||||
s.started = true
|
||||
s.started.Store(true)
|
||||
s.dv5Listener = &mockListener{}
|
||||
assert.NoError(t, s.Stop())
|
||||
assert.Equal(t, false, s.started)
|
||||
assert.Equal(t, false, s.Started())
|
||||
}
|
||||
|
||||
func TestService_Stop_DontPanicIfDv5ListenerIsNotInited(t *testing.T) {
|
||||
@@ -118,7 +118,7 @@ func TestService_Start_OnlyStartsOnce(t *testing.T) {
|
||||
var vr [32]byte
|
||||
require.NoError(t, cs.SetClock(startup.NewClock(time.Now(), vr)))
|
||||
time.Sleep(time.Second * 2)
|
||||
assert.Equal(t, true, s.started, "Expected service to be started")
|
||||
assert.Equal(t, true, s.Started(), "Expected service to be started")
|
||||
s.Start()
|
||||
require.LogsContain(t, hook, "Attempted to start p2p service when it was already started")
|
||||
require.NoError(t, s.Stop())
|
||||
@@ -127,14 +127,15 @@ func TestService_Start_OnlyStartsOnce(t *testing.T) {
|
||||
|
||||
func TestService_Status_NotRunning(t *testing.T) {
|
||||
params.SetupTestConfigCleanup(t)
|
||||
s := &Service{started: false}
|
||||
s := &Service{}
|
||||
s.dv5Listener = &mockListener{}
|
||||
assert.ErrorContains(t, "not running", s.Status(), "Status returned wrong error")
|
||||
}
|
||||
|
||||
func TestService_Status_NoGenesisTimeSet(t *testing.T) {
|
||||
params.SetupTestConfigCleanup(t)
|
||||
s := &Service{started: true}
|
||||
s := &Service{}
|
||||
s.started.Store(true)
|
||||
s.dv5Listener = &mockListener{}
|
||||
assert.ErrorContains(t, "no genesis time set", s.Status(), "Status returned wrong error")
|
||||
|
||||
@@ -201,13 +202,6 @@ func TestListenForNewNodes(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer bootListener.Close()
|
||||
|
||||
// Use shorter period for testing.
|
||||
currentPeriod := pollingPeriod
|
||||
pollingPeriod = 1 * time.Second
|
||||
defer func() {
|
||||
pollingPeriod = currentPeriod
|
||||
}()
|
||||
|
||||
bootNode := bootListener.Self()
|
||||
|
||||
var listeners []*discover.UDPv5
|
||||
@@ -308,11 +302,16 @@ func TestService_JoinLeaveTopic(t *testing.T) {
|
||||
s, err := NewService(ctx, &Config{StateNotifier: &mock.MockStateNotifier{}, ClockWaiter: gs})
|
||||
require.NoError(t, err)
|
||||
|
||||
go s.awaitStateInitialized()
|
||||
wait := make(chan struct{})
|
||||
go func() {
|
||||
s.awaitStateInitialized()
|
||||
wait <- struct{}{}
|
||||
}()
|
||||
fd := initializeStateWithForkDigest(ctx, t, gs)
|
||||
|
||||
assert.Equal(t, 0, len(s.joinedTopics))
|
||||
|
||||
<-wait
|
||||
topic := fmt.Sprintf(AttestationSubnetTopicFormat, fd, 42) + "/" + encoder.ProtocolSuffixSSZSnappy
|
||||
topicHandle, err := s.JoinTopic(topic)
|
||||
assert.NoError(t, err)
|
||||
|
||||
@@ -14,11 +14,14 @@ go_library(
|
||||
"mainnet_config.go",
|
||||
"minimal_config.go",
|
||||
"network_config.go",
|
||||
"network_config_develop.go", # keep
|
||||
"network_config_prod.go",
|
||||
"testnet_e2e_config.go",
|
||||
"testnet_holesky_config.go",
|
||||
"testnet_sepolia_config.go",
|
||||
"testutils.go",
|
||||
"testutils_develop.go", # keep
|
||||
"testutils_prod.go",
|
||||
"values.go",
|
||||
],
|
||||
importpath = "github.com/prysmaticlabs/prysm/v5/config/params",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package params
|
||||
|
||||
import (
|
||||
"github.com/mohae/deepcopy"
|
||||
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
|
||||
)
|
||||
|
||||
@@ -20,27 +19,6 @@ type NetworkConfig struct {
|
||||
|
||||
var networkConfig = mainnetNetworkConfig
|
||||
|
||||
// BeaconNetworkConfig returns the current network config for
|
||||
// the beacon chain.
|
||||
func BeaconNetworkConfig() *NetworkConfig {
|
||||
return networkConfig
|
||||
}
|
||||
|
||||
// OverrideBeaconNetworkConfig will override the network
|
||||
// config with the added argument.
|
||||
func OverrideBeaconNetworkConfig(cfg *NetworkConfig) {
|
||||
networkConfig = cfg.Copy()
|
||||
}
|
||||
|
||||
// Copy returns Copy of the config object.
|
||||
func (c *NetworkConfig) Copy() *NetworkConfig {
|
||||
config, ok := deepcopy.Copy(*c).(NetworkConfig)
|
||||
if !ok {
|
||||
config = *networkConfig
|
||||
}
|
||||
return &config
|
||||
}
|
||||
|
||||
// MaxRequestBlock determines the maximum number of blocks that can be requested in a single
|
||||
// request for a given epoch. If the epoch is at or beyond config's `DenebForkEpoch`,
|
||||
// a special limit defined for Deneb is used.
|
||||
|
||||
38
config/params/network_config_develop.go
Normal file
38
config/params/network_config_develop.go
Normal file
@@ -0,0 +1,38 @@
|
||||
//go:build develop
|
||||
|
||||
package params
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/mohae/deepcopy"
|
||||
)
|
||||
|
||||
var networkConfigLock sync.Mutex
|
||||
|
||||
// BeaconNetworkConfig returns the current network config for
|
||||
// the beacon chain.
|
||||
func BeaconNetworkConfig() *NetworkConfig {
|
||||
networkConfigLock.Lock()
|
||||
defer networkConfigLock.Unlock()
|
||||
return networkConfig
|
||||
}
|
||||
|
||||
// OverrideBeaconNetworkConfig will override the network
|
||||
// config with the added argument.
|
||||
func OverrideBeaconNetworkConfig(cfg *NetworkConfig) {
|
||||
networkConfigLock.Lock()
|
||||
defer networkConfigLock.Unlock()
|
||||
networkConfig = cfg.Copy()
|
||||
}
|
||||
|
||||
// Copy returns Copy of the config object.
|
||||
func (c *NetworkConfig) Copy() *NetworkConfig {
|
||||
config, ok := deepcopy.Copy(*c).(NetworkConfig)
|
||||
if !ok {
|
||||
networkConfigLock.Lock()
|
||||
config = *networkConfig
|
||||
networkConfigLock.Unlock()
|
||||
}
|
||||
return &config
|
||||
}
|
||||
26
config/params/network_config_prod.go
Normal file
26
config/params/network_config_prod.go
Normal file
@@ -0,0 +1,26 @@
|
||||
//go:build !develop
|
||||
|
||||
package params
|
||||
|
||||
import "github.com/mohae/deepcopy"
|
||||
|
||||
// BeaconNetworkConfig returns the current network config for
|
||||
// the beacon chain.
|
||||
func BeaconNetworkConfig() *NetworkConfig {
|
||||
return networkConfig
|
||||
}
|
||||
|
||||
// OverrideBeaconNetworkConfig will override the network
|
||||
// config with the added argument.
|
||||
func OverrideBeaconNetworkConfig(cfg *NetworkConfig) {
|
||||
networkConfig = cfg.Copy()
|
||||
}
|
||||
|
||||
// Copy returns Copy of the config object.
|
||||
func (c *NetworkConfig) Copy() *NetworkConfig {
|
||||
config, ok := deepcopy.Copy(*c).(NetworkConfig)
|
||||
if !ok {
|
||||
config = *networkConfig
|
||||
}
|
||||
return &config
|
||||
}
|
||||
@@ -1,28 +1,6 @@
|
||||
package params
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// SetupTestConfigCleanup preserves configurations allowing to modify them within tests without any
|
||||
// restrictions, everything is restored after the test.
|
||||
func SetupTestConfigCleanup(t testing.TB) {
|
||||
prevDefaultBeaconConfig := mainnetBeaconConfig.Copy()
|
||||
temp := configs.getActive().Copy()
|
||||
undo, err := SetActiveWithUndo(temp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
prevNetworkCfg := networkConfig.Copy()
|
||||
t.Cleanup(func() {
|
||||
mainnetBeaconConfig = prevDefaultBeaconConfig
|
||||
err = undo()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
networkConfig = prevNetworkCfg
|
||||
})
|
||||
}
|
||||
import "testing"
|
||||
|
||||
// SetActiveTestCleanup sets an active config,
|
||||
// and adds a test cleanup hook to revert to the default config after the test completes.
|
||||
|
||||
@@ -4,13 +4,15 @@ package params
|
||||
|
||||
import "testing"
|
||||
|
||||
// SetupTestConfigCleanupWithLock preserves configurations allowing to modify them within tests without any
|
||||
// SetupTestConfigCleanup preserves configurations allowing to modify them within tests without any
|
||||
// restrictions, everything is restored after the test. This locks our config when undoing our config
|
||||
// change in order to satisfy the race detector.
|
||||
func SetupTestConfigCleanupWithLock(t testing.TB) {
|
||||
func SetupTestConfigCleanup(t testing.TB) {
|
||||
prevDefaultBeaconConfig := mainnetBeaconConfig.Copy()
|
||||
temp := configs.getActive().Copy()
|
||||
cfgrw.Lock()
|
||||
undo, err := SetActiveWithUndo(temp)
|
||||
cfgrw.Unlock()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -23,6 +25,8 @@ func SetupTestConfigCleanupWithLock(t testing.TB) {
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
networkConfigLock.Lock()
|
||||
networkConfig = prevNetworkCfg
|
||||
networkConfigLock.Unlock()
|
||||
})
|
||||
}
|
||||
|
||||
25
config/params/testutils_prod.go
Normal file
25
config/params/testutils_prod.go
Normal file
@@ -0,0 +1,25 @@
|
||||
//go:build !develop
|
||||
|
||||
package params
|
||||
|
||||
import "testing"
|
||||
|
||||
// SetupTestConfigCleanup preserves configurations allowing to modify them within tests without any
|
||||
// restrictions, everything is restored after the test.
|
||||
func SetupTestConfigCleanup(t testing.TB) {
|
||||
prevDefaultBeaconConfig := mainnetBeaconConfig.Copy()
|
||||
temp := configs.getActive().Copy()
|
||||
undo, err := SetActiveWithUndo(temp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
prevNetworkCfg := networkConfig.Copy()
|
||||
t.Cleanup(func() {
|
||||
mainnetBeaconConfig = prevDefaultBeaconConfig
|
||||
err = undo()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
networkConfig = prevNetworkCfg
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user