Fix NPE, ensure the listener is available (#11946)

* Fix NPE, ensure the listener is available

* Added test

---------

Co-authored-by: Nishant Das <nishdas93@gmail.com>
This commit is contained in:
Pedro Gomes 2023-02-22 06:53:09 +00:00 committed by GitHub
parent 9ccfe17227
commit 094ff877f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 3 deletions

View File

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

View File

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