Compare commits

...

6 Commits

Author SHA1 Message Date
terencechain
167525c465 Update beacon-chain/core/transition/transition.go
Co-authored-by: Potuz <potuz@prysmaticlabs.com>
2023-04-04 12:09:51 -07:00
terence tsao
b4d4ea6022 Rm bad change 2023-04-04 11:09:34 -07:00
terence tsao
dac9bf7bed Rm bad change 2023-04-04 11:09:02 -07:00
terence tsao
b0feda22b9 Add condition 2023-04-04 10:59:34 -07:00
terence tsao
5dcfeb8b54 Sync with develop 2023-04-04 10:55:24 -07:00
terence tsao
3498b2ca79 Use process slots next slot cache 2023-03-30 10:39:40 -07:00
6 changed files with 31 additions and 21 deletions

View File

@@ -264,7 +264,8 @@ func (s *Service) getPayloadAttribute(ctx context.Context, st state.BeaconState,
// Get previous randao.
st = st.Copy()
st, err := transition.ProcessSlotsIfPossible(ctx, st, slot)
headRoot := s.headRoot()
st, err := transition.ProcessSlotsUsingNextSlotCache(ctx, st, headRoot[:], slot)
if err != nil {
log.WithError(err).Error("Could not process slots to get payload attribute")
return false, emptyAttri, 0

View File

@@ -154,10 +154,14 @@ func (s *Service) getSyncCommitteeHeadState(ctx context.Context, slot primitives
if err != nil {
return nil, err
}
headRoot, err := s.HeadRoot(ctx)
if err != nil {
return nil, err
}
if headState == nil || headState.IsNil() {
return nil, errors.New("nil state")
}
headState, err = transition.ProcessSlotsIfPossible(ctx, headState, slot)
headState, err = transition.ProcessSlotsUsingNextSlotCache(ctx, headState, headRoot, slot)
if err != nil {
return nil, err
}

View File

@@ -42,7 +42,7 @@ func (s *Service) getAttPreState(ctx context.Context, c *ethpb.Checkpoint) (stat
if err != nil {
return nil, err
}
baseState, err = transition.ProcessSlotsIfPossible(ctx, baseState, epochStartSlot)
baseState, err = transition.ProcessSlotsUsingNextSlotCache(ctx, baseState, c.Root, epochStartSlot)
if err != nil {
return nil, errors.Wrapf(err, "could not process slots up to epoch %d", c.Epoch)
}

View File

@@ -156,22 +156,15 @@ func ProcessSlotsUsingNextSlotCache(
}
var err error
parentState, err = ProcessSlots(ctx, parentState, slot)
if err != nil {
return nil, errors.Wrap(err, "could not process slots")
if slot > parentState.Slot() {
parentState, err = ProcessSlots(ctx, parentState, slot)
if err != nil {
return nil, errors.Wrap(err, "could not process slots")
}
}
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 primitives.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
//
// Spec pseudocode definition:

View File

@@ -643,21 +643,21 @@ func TestProcessSlotsConditionally(t *testing.T) {
t.Run("target slot below current slot", func(t *testing.T) {
require.NoError(t, s.SetSlot(5))
s, err := transition.ProcessSlotsIfPossible(ctx, s, 4)
s, err := transition.ProcessSlotsUsingNextSlotCache(ctx, s, []byte{}, 4)
require.NoError(t, err)
assert.Equal(t, primitives.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)
s, err := transition.ProcessSlotsUsingNextSlotCache(ctx, s, []byte{}, 5)
require.NoError(t, err)
assert.Equal(t, primitives.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)
s, err := transition.ProcessSlotsUsingNextSlotCache(ctx, s, []byte{}, 6)
require.NoError(t, err)
assert.Equal(t, primitives.Slot(6), s.Slot())
})

View File

@@ -170,7 +170,11 @@ func (bs *Server) SubmitAttesterSlashing(ctx context.Context, req *ethpbv1.Attes
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get head state: %v", err)
}
headState, err = transition.ProcessSlotsIfPossible(ctx, headState, req.Attestation_1.Data.Slot)
headRoot, err := bs.ChainInfoFetcher.HeadRoot(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get head root: %v", err)
}
headState, err = transition.ProcessSlotsUsingNextSlotCache(ctx, headState, headRoot, req.Attestation_1.Data.Slot)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not process slots: %v", err)
}
@@ -226,7 +230,11 @@ func (bs *Server) SubmitProposerSlashing(ctx context.Context, req *ethpbv1.Propo
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get head state: %v", err)
}
headState, err = transition.ProcessSlotsIfPossible(ctx, headState, req.SignedHeader_1.Message.Slot)
headRoot, err := bs.ChainInfoFetcher.HeadRoot(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get head root: %v", err)
}
headState, err = transition.ProcessSlotsUsingNextSlotCache(ctx, headState, headRoot, req.SignedHeader_1.Message.Slot)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not process slots: %v", err)
}
@@ -284,7 +292,11 @@ func (bs *Server) SubmitVoluntaryExit(ctx context.Context, req *ethpbv1.SignedVo
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get epoch from message: %v", err)
}
headState, err = transition.ProcessSlotsIfPossible(ctx, headState, s)
headRoot, err := bs.ChainInfoFetcher.HeadRoot(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get head root: %v", err)
}
headState, err = transition.ProcessSlotsUsingNextSlotCache(ctx, headState, headRoot, s)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not process slots: %v", err)
}