Skip to content

Commit

Permalink
refactor(duties): Simplify RequiredEpochDuties method
Browse files Browse the repository at this point in the history
  • Loading branch information
samcm committed Jul 20, 2023
1 parent 27a7907 commit d280491
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions pkg/sentry/ethereum/services/duties.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (m *DutiesService) Start(ctx context.Context) error {
}
}()

m.beacon.Wallclock().OnEpochChanged(func(epoch ethwallclock.Epoch) {
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")
}
Expand Down Expand Up @@ -104,25 +104,19 @@ func (m *DutiesService) Name() Name {
return "duties"
}

func (m *DutiesService) RequiredEpochDuties(ctx context.Context) []phase0.Epoch {
now := m.beacon.Wallclock().Epochs().Current()
func (m *DutiesService) RequiredEpochDuties() []phase0.Epoch {
now := m.metadata.Wallclock().Epochs().Current()

epochNumber := now.Number()

epochs := []phase0.Epoch{
phase0.Epoch(epochNumber - 3),
phase0.Epoch(epochNumber - 2),
phase0.Epoch(epochNumber - 1),
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.
Expand All @@ -139,7 +133,7 @@ func (m *DutiesService) RequiredEpochDuties(ctx context.Context) []phase0.Epoch
}

func (m *DutiesService) Ready(ctx context.Context) error {
for _, epoch := range m.RequiredEpochDuties(ctx) {
for _, epoch := range m.RequiredEpochDuties() {
if duties := m.beaconCommittees.Get(epoch); duties == nil {
return fmt.Errorf("duties for epoch %d are not ready", epoch)
}
Expand All @@ -153,7 +147,7 @@ func (m *DutiesService) backFillEpochDuties(ctx context.Context) error {
return fmt.Errorf("metadata service is not ready")
}

for _, epoch := range m.RequiredEpochDuties(ctx) {
for _, epoch := range m.RequiredEpochDuties() {
if duties := m.beaconCommittees.Get(epoch); duties == nil {
if err := m.fetchBeaconCommittee(ctx, epoch); err != nil {
return err
Expand Down

0 comments on commit d280491

Please sign in to comment.