From bd977afcf90f5951b8ae7b3ae6c5f89a41979c6b Mon Sep 17 00:00:00 2001 From: Sam Calder-Mason Date: Thu, 20 Jul 2023 15:38:41 +1000 Subject: [PATCH 1/2] fix(sentry): Only require current epoch duties --- pkg/sentry/ethereum/services/duties.go | 64 +++++++++++++++++++++----- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/pkg/sentry/ethereum/services/duties.go b/pkg/sentry/ethereum/services/duties.go index 3bc1093c..6ca4982d 100644 --- a/pkg/sentry/ethereum/services/duties.go +++ b/pkg/sentry/ethereum/services/duties.go @@ -53,10 +53,13 @@ func NewDutiesService(log logrus.FieldLogger, sbeacon beacon.Node, metadata *Met func (m *DutiesService) Start(ctx context.Context) error { go func() { operation := func() error { - if err := m.backFillEpochDuties(ctx); err != nil { + if err := m.fetchRequiredEpochDuties(ctx); err != nil { return err } + //nolint:errcheck // We don't care about the error here + m.fetchNiceToHaveEpochDuties(ctx) + if err := m.Ready(ctx); err != nil { return err } @@ -76,9 +79,12 @@ func (m *DutiesService) Start(ctx context.Context) error { }() m.metadata.Wallclock().OnEpochChanged(func(epoch ethwallclock.Epoch) { - if err := m.backFillEpochDuties(ctx); err != nil { - m.log.WithError(err).Warn("Failed to fetch epoch duties") + if err := m.fetchRequiredEpochDuties(ctx); err != nil { + m.log.WithError(err).Warn("Failed to fetch required epoch duties") } + + //nolint:errcheck // We don't care about the error here + m.fetchNiceToHaveEpochDuties(ctx) }) go m.beaconCommittees.Start() @@ -111,16 +117,34 @@ func (m *DutiesService) RequiredEpochDuties(ctx context.Context) []phase0.Epoch epochs := []phase0.Epoch{ phase0.Epoch(epochNumber), - phase0.Epoch(epochNumber + 1), } - // Lodestar does not support fetching beacon committees for older epochs. - if m.metadata.Client(ctx) != string(ClientLodestar) { - epochs = append(epochs, - phase0.Epoch(epochNumber-1), - phase0.Epoch(epochNumber-2), - phase0.Epoch(epochNumber-3), - ) + final := map[phase0.Epoch]struct{}{} + + // Deduplicate in case the current epoch is below epoch 3. + for _, epoch := range epochs { + final[epoch] = struct{}{} + } + + epochs = make([]phase0.Epoch, 0, len(final)) + for epoch := range final { + epochs = append(epochs, epoch) + } + + return epochs +} + +func (m *DutiesService) NiceToHaveEpochDuties(ctx context.Context) []phase0.Epoch { + now := m.metadata.Wallclock().Epochs().Current() + + epochNumber := now.Number() + + epochs := []phase0.Epoch{ + phase0.Epoch(epochNumber - 1), + phase0.Epoch(epochNumber - 2), + phase0.Epoch(epochNumber - 3), + + phase0.Epoch(epochNumber + 1), } final := map[phase0.Epoch]struct{}{} @@ -148,7 +172,7 @@ func (m *DutiesService) Ready(ctx context.Context) error { return nil } -func (m *DutiesService) backFillEpochDuties(ctx context.Context) error { +func (m *DutiesService) fetchRequiredEpochDuties(ctx context.Context) error { if m.metadata.Wallclock() == nil { return fmt.Errorf("metadata service is not ready") } @@ -164,6 +188,22 @@ func (m *DutiesService) backFillEpochDuties(ctx context.Context) error { return nil } +func (m *DutiesService) fetchNiceToHaveEpochDuties(ctx context.Context) error { + if m.metadata.Wallclock() == nil { + return fmt.Errorf("metadata service is not ready") + } + + for _, epoch := range m.NiceToHaveEpochDuties(ctx) { + if duties := m.beaconCommittees.Get(epoch); duties == nil { + if err := m.fetchBeaconCommittee(ctx, epoch); err != nil { + m.log.WithError(err).Debugf("Failed to fetch beacon committee for epoch %d", epoch) + } + } + } + + return nil +} + func (m *DutiesService) fireOnBeaconCommitteeSubscriptions(epoch phase0.Epoch, committees []*v1.BeaconCommittee) { for _, fn := range m.onBeaconCommitteeSubscriptions { if err := fn(epoch, committees); err != nil { From 5a4575f58d4c5e65a797d7d0ceab40d938658fa3 Mon Sep 17 00:00:00 2001 From: Sam Calder-Mason Date: Thu, 20 Jul 2023 15:39:40 +1000 Subject: [PATCH 2/2] refactor: Remove unnecessary code for deduplicating epochs --- pkg/sentry/ethereum/services/duties.go | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/pkg/sentry/ethereum/services/duties.go b/pkg/sentry/ethereum/services/duties.go index 6ca4982d..e6957f13 100644 --- a/pkg/sentry/ethereum/services/duties.go +++ b/pkg/sentry/ethereum/services/duties.go @@ -119,18 +119,6 @@ func (m *DutiesService) RequiredEpochDuties(ctx context.Context) []phase0.Epoch phase0.Epoch(epochNumber), } - final := map[phase0.Epoch]struct{}{} - - // Deduplicate in case the current epoch is below epoch 3. - for _, epoch := range epochs { - final[epoch] = struct{}{} - } - - epochs = make([]phase0.Epoch, 0, len(final)) - for epoch := range final { - epochs = append(epochs, epoch) - } - return epochs }