From 094ff877f6ac18eca6c3b096e3b8a92e43bce309 Mon Sep 17 00:00:00 2001 From: Pedro Gomes Date: Wed, 22 Feb 2023 06:53:09 +0000 Subject: [PATCH] Fix NPE, ensure the listener is available (#11946) * Fix NPE, ensure the listener is available * Added test --------- Co-authored-by: Nishant Das --- beacon-chain/p2p/fork_watcher.go | 8 +++--- beacon-chain/p2p/service_test.go | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/beacon-chain/p2p/fork_watcher.go b/beacon-chain/p2p/fork_watcher.go index c36dec8cd..6db6c02f1 100644 --- a/beacon-chain/p2p/fork_watcher.go +++ b/beacon-chain/p2p/fork_watcher.go @@ -22,9 +22,11 @@ func (s *Service) forkWatcher() { // the updated fork digest. These repeatedly does // this over the epoch, which might be slightly wasteful // but is fine nonetheless. - _, err := addForkEntry(s.dv5Listener.LocalNode(), s.genesisTime, s.genesisValidatorsRoot) - if err != nil { - log.WithError(err).Error("Could not add fork entry") + if s.dv5Listener != nil { // make sure it's not a local network + _, err := addForkEntry(s.dv5Listener.LocalNode(), s.genesisTime, s.genesisValidatorsRoot) + if err != nil { + log.WithError(err).Error("Could not add fork entry") + } } // from Bellatrix Epoch, the MaxGossipSize and the MaxChunkSize is changed to 10Mb. diff --git a/beacon-chain/p2p/service_test.go b/beacon-chain/p2p/service_test.go index 6b0f8f5f9..2e451942b 100644 --- a/beacon-chain/p2p/service_test.go +++ b/beacon-chain/p2p/service_test.go @@ -152,6 +152,51 @@ func TestService_Status_NoGenesisTimeSet(t *testing.T) { assert.NoError(t, s.Status(), "Status returned error") } +func TestService_Start_NoDiscoverFlag(t *testing.T) { + params.SetupTestConfigCleanup(t) + + cfg := &Config{ + TCPPort: 2000, + UDPPort: 2000, + StateNotifier: &mock.MockStateNotifier{}, + NoDiscovery: true, // <-- no s.dv5Listener is created + } + s, err := NewService(context.Background(), cfg) + require.NoError(t, err) + + s.stateNotifier = &mock.MockStateNotifier{} + + // required params to addForkEntry in s.forkWatcher + s.genesisTime = time.Now() + beaconCfg := params.BeaconConfig().Copy() + beaconCfg.AltairForkEpoch = 0 + beaconCfg.BellatrixForkEpoch = 0 + beaconCfg.CapellaForkEpoch = 0 + beaconCfg.SecondsPerSlot = 1 + params.OverrideBeaconConfig(beaconCfg) + + exitRoutine := make(chan bool) + go func() { + s.Start() + <-exitRoutine + }() + + // 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{ + Type: statefeed.Initialized, + Data: &statefeed.InitializedData{ + StartTime: time.Now(), + GenesisValidatorsRoot: make([]byte, 32), + }, + }) + } + + time.Sleep(time.Second * 2) + + exitRoutine <- true +} + func TestListenForNewNodes(t *testing.T) { params.SetupTestConfigCleanup(t) // Setup bootnode.