Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sentry): Only require current epoch duties #151

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 40 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,22 @@ 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),
)
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 +160,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 +176,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