Compare commits

...

19 Commits

Author SHA1 Message Date
Preston Van Loon
23d278b7c5 bazel run //:gazelle 2024-06-03 09:57:06 -05:00
jsvisa
a9c1911912 config/params: update build.bazel
Signed-off-by: jsvisa <delweng@gmail.com>
2024-06-02 00:54:51 +00:00
jsvisa
7fefa151be config/params: split testutils into normal,prod,dev
Signed-off-by: jsvisa <delweng@gmail.com>
2024-06-02 00:54:32 +00:00
Delweng
6c2508472e Merge branch 'develop' into beacon-p2p-datarace 2024-06-02 08:23:14 +08:00
jsvisa
d9afce3861 config/params: move SetActiveTestCleanup to develop only
Signed-off-by: jsvisa <delweng@gmail.com>
2024-05-08 03:18:42 +00:00
Delweng
9fd889e48f Merge branch 'develop' into beacon-p2p-datarace 2024-05-08 08:42:16 +08:00
Preston Van Loon
39b81ea555 remove go:test constraints, gocheckcompilerdirectives 2024-05-07 16:52:46 -05:00
Preston Van Loon
f3eeab7d80 Merge branch 'develop' of github.com:prysmaticlabs/prysm into beacon-p2p-datarace 2024-05-07 15:51:35 -05:00
jsvisa
33d3f078e8 cofig/params: bazel build
Signed-off-by: jsvisa <delweng@gmail.com>
2023-12-14 05:10:13 +00:00
jsvisa
2e5b3c59fa config/params: add network_config_{develop,prod} to bazel build
Signed-off-by: jsvisa <delweng@gmail.com>
2023-12-14 02:44:16 +00:00
Delweng
bcc02ee913 Merge branch 'develop' into beacon-p2p-datarace 2023-12-13 21:15:59 +08:00
Delweng
b9064d704f Merge branch 'develop' into beacon-p2p-datarace 2023-12-13 20:29:09 +08:00
Delweng
17402180f8 Merge branch 'develop' into beacon-p2p-datarace 2023-12-05 21:26:06 +08:00
jsvisa
4f00c0b487 beacon-chainn/p2p: avoid datarate
Signed-off-by: jsvisa <delweng@gmail.com>
2023-12-02 12:11:14 +00:00
jsvisa
f29f76cbf2 beacon-chain/p2p: don't modify pollingPeriod
Signed-off-by: jsvisa <delweng@gmail.com>
2023-12-02 12:11:14 +00:00
jsvisa
aab101a4bc config/params: rw network config lock or not
Signed-off-by: jsvisa <delweng@gmail.com>
2023-12-02 12:11:12 +00:00
jsvisa
82c3c11335 config/params: setActive with lock
Signed-off-by: jsvisa <delweng@gmail.com>
2023-12-02 12:10:23 +00:00
jsvisa
859b9d8a7e config/params: apply develop tag for go:test
Signed-off-by: jsvisa <delweng@gmail.com>
2023-12-02 12:10:22 +00:00
jsvisa
6c76232203 beacon-chain/p2p: use atomic.Bool instead of bool for concurrent rw
Signed-off-by: jsvisa <delweng@gmail.com>
2023-12-02 12:10:22 +00:00
12 changed files with 126 additions and 81 deletions

View File

@@ -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)

View File

@@ -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()) {

View File

@@ -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)

View File

@@ -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.

View File

@@ -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)

View File

@@ -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",

View File

@@ -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.

View 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
}

View 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
}

View File

@@ -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.

View File

@@ -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()
})
}

View 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
})
}