Skip to content

Commit

Permalink
fix(sentry): Only require current epoch duties
Browse files Browse the repository at this point in the history
  • Loading branch information
samcm committed Jul 20, 2023
1 parent 32e4c26 commit bd977af
Showing 1 changed file with 52 additions and 12 deletions.
64 changes: 52 additions & 12 deletions pkg/sentry/ethereum/services/duties.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
Expand Down Expand Up @@ -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{}{}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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 {
Expand Down

0 comments on commit bd977af

Please sign in to comment.