Extract conditional slot processing to new function (#10114)

* Extract conditional slot processing to new function

* rename function
This commit is contained in:
Radosław Kapka 2022-01-25 15:52:18 +01:00 committed by GitHub
parent ae31eed013
commit 588658a649
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 20 deletions

View File

@ -157,11 +157,9 @@ func (s *Service) getSyncCommitteeHeadState(ctx context.Context, slot types.Slot
if headState == nil || headState.IsNil() { if headState == nil || headState.IsNil() {
return nil, errors.New("nil state") return nil, errors.New("nil state")
} }
if slot > headState.Slot() { headState, err = transition.ProcessSlotsIfPossible(ctx, headState, slot)
headState, err = transition.ProcessSlots(ctx, headState, slot) if err != nil {
if err != nil { return nil, err
return nil, err
}
} }
syncHeadStateMiss.Inc() syncHeadStateMiss.Inc()
err = syncCommitteeHeadStateCache.Put(slot, headState) err = syncCommitteeHeadStateCache.Put(slot, headState)

View File

@ -42,11 +42,9 @@ func (s *Service) getAttPreState(ctx context.Context, c *ethpb.Checkpoint) (stat
if err != nil { if err != nil {
return nil, err return nil, err
} }
if epochStartSlot > baseState.Slot() { baseState, err = transition.ProcessSlotsIfPossible(ctx, baseState, epochStartSlot)
baseState, err = transition.ProcessSlots(ctx, baseState, epochStartSlot) if err != nil {
if err != nil { return nil, errors.Wrapf(err, "could not process slots up to epoch %d", c.Epoch)
return nil, errors.Wrapf(err, "could not process slots up to epoch %d", c.Epoch)
}
} }
// Sharing the same state across caches is perfectly fine here, the fetching // Sharing the same state across caches is perfectly fine here, the fetching

View File

@ -151,15 +151,22 @@ func ProcessSlotsUsingNextSlotCache(
// Since next slot cache only advances state by 1 slot, // Since next slot cache only advances state by 1 slot,
// we check if there's more slots that need to process. // we check if there's more slots that need to process.
if slot > parentState.Slot() { parentState, err = ProcessSlotsIfPossible(ctx, parentState, slot)
parentState, err = ProcessSlots(ctx, parentState, slot) if err != nil {
if err != nil { return nil, errors.Wrap(err, "could not process slots")
return nil, errors.Wrap(err, "could not process slots")
}
} }
return parentState, nil return parentState, nil
} }
// ProcessSlotsIfPossible executes ProcessSlots on the input state when target slot is above the state's slot.
// Otherwise, it returns the input state unchanged.
func ProcessSlotsIfPossible(ctx context.Context, state state.BeaconState, targetSlot types.Slot) (state.BeaconState, error) {
if targetSlot > state.Slot() {
return ProcessSlots(ctx, state, targetSlot)
}
return state, nil
}
// ProcessSlots process through skip slots and apply epoch transition when it's needed // ProcessSlots process through skip slots and apply epoch transition when it's needed
// //
// Spec pseudocode definition: // Spec pseudocode definition:

View File

@ -610,3 +610,29 @@ func TestProcessSlotsUsingNextSlotCache(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, types.Slot(5), s.Slot()) require.Equal(t, types.Slot(5), s.Slot())
} }
func TestProcessSlotsConditionally(t *testing.T) {
ctx := context.Background()
s, _ := util.DeterministicGenesisState(t, 1)
t.Run("target slot below current slot", func(t *testing.T) {
require.NoError(t, s.SetSlot(5))
s, err := transition.ProcessSlotsIfPossible(ctx, s, 4)
require.NoError(t, err)
assert.Equal(t, types.Slot(5), s.Slot())
})
t.Run("target slot equal current slot", func(t *testing.T) {
require.NoError(t, s.SetSlot(5))
s, err := transition.ProcessSlotsIfPossible(ctx, s, 5)
require.NoError(t, err)
assert.Equal(t, types.Slot(5), s.Slot())
})
t.Run("target slot above current slot", func(t *testing.T) {
require.NoError(t, s.SetSlot(5))
s, err := transition.ProcessSlotsIfPossible(ctx, s, 6)
require.NoError(t, err)
assert.Equal(t, types.Slot(6), s.Slot())
})
}

View File

@ -666,11 +666,9 @@ func advanceState(ctx context.Context, s state.BeaconState, requestedEpoch, curr
return nil, errors.Wrap(err, "Could not obtain epoch's start slot") return nil, errors.Wrap(err, "Could not obtain epoch's start slot")
} }
} }
if s.Slot() < epochStartSlot { s, err = transition.ProcessSlotsIfPossible(ctx, s, epochStartSlot)
s, err = transition.ProcessSlots(ctx, s, epochStartSlot) if err != nil {
if err != nil { return nil, errors.Wrapf(err, "Could not process slots up to %d", epochStartSlot)
return nil, errors.Wrapf(err, "Could not process slots up to %d", epochStartSlot)
}
} }
return s, nil return s, nil