Skip to content

Commit 26100e0

Browse files
Refactor slot by blockroot (#16040)
**Review after #16033 is merged** **What type of PR is this?** Other **What does this PR do? Why is it needed?** This PR refactors the code to find the corresponding slot of a given block root using state summary or the block itself, into its own function `SlotByBlockRoot(ctx, blockroot)`. Note that there exists a function `slotByBlockRoot(ctx context.Context, tx *bolt.Tx, blockRoot []byte)` immediately below the new function. Also note that this function has two drawbacks, which led to creation of the new function: - the old function requires a boltdb tx, which is not necessarily available to the caller. - the old function does NOT make use of the state summary cache. edit: - the old function also uses the state bucket to retrieve the state and it's slot. this is not something we want in the state diff feature, since there is no state bucket.
1 parent 768fa0e commit 26100e0

File tree

2 files changed

+38
-55
lines changed

2 files changed

+38
-55
lines changed

beacon-chain/db/kv/state.go

Lines changed: 35 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/OffchainLabs/prysm/v7/beacon-chain/state"
99
statenative "github.com/OffchainLabs/prysm/v7/beacon-chain/state/state-native"
1010
"github.com/OffchainLabs/prysm/v7/config/features"
11-
"github.com/OffchainLabs/prysm/v7/consensus-types/blocks"
1211
"github.com/OffchainLabs/prysm/v7/consensus-types/primitives"
1312
"github.com/OffchainLabs/prysm/v7/encoding/bytesutil"
1413
"github.com/OffchainLabs/prysm/v7/genesis"
@@ -481,7 +480,7 @@ func (s *Store) DeleteState(ctx context.Context, blockRoot [32]byte) error {
481480
return nil
482481
}
483482

484-
slot, err := s.slotByBlockRoot(ctx, tx, blockRoot[:])
483+
slot, err := s.SlotByBlockRoot(ctx, blockRoot)
485484
if err != nil {
486485
return err
487486
}
@@ -823,50 +822,45 @@ func (s *Store) stateBytes(ctx context.Context, blockRoot [32]byte) ([]byte, err
823822
return dst, err
824823
}
825824

826-
// slotByBlockRoot retrieves the corresponding slot of the input block root.
827-
func (s *Store) slotByBlockRoot(ctx context.Context, tx *bolt.Tx, blockRoot []byte) (primitives.Slot, error) {
828-
ctx, span := trace.StartSpan(ctx, "BeaconDB.slotByBlockRoot")
825+
// SlotByBlockRoot returns the slot of the input block root, based on state summary, block, or state.
826+
// Check for state is only done if state diff feature is not enabled.
827+
func (s *Store) SlotByBlockRoot(ctx context.Context, blockRoot [32]byte) (primitives.Slot, error) {
828+
ctx, span := trace.StartSpan(ctx, "BeaconDB.SlotByBlockRoot")
829829
defer span.End()
830830

831-
bkt := tx.Bucket(stateSummaryBucket)
832-
enc := bkt.Get(blockRoot)
831+
// check state summary first
832+
stateSummary, err := s.StateSummary(ctx, blockRoot)
833+
if err != nil {
834+
return 0, err
835+
}
836+
if stateSummary != nil {
837+
return stateSummary.Slot, nil
838+
}
833839

834-
if enc == nil {
835-
// Fall back to check the block.
836-
bkt := tx.Bucket(blocksBucket)
837-
enc := bkt.Get(blockRoot)
840+
// fall back to block if state summary is not found
841+
blk, err := s.Block(ctx, blockRoot)
842+
if err != nil {
843+
return 0, err
844+
}
845+
if blk != nil && !blk.IsNil() {
846+
return blk.Block().Slot(), nil
847+
}
838848

839-
if enc == nil {
840-
// Fallback and check the state.
841-
bkt = tx.Bucket(stateBucket)
842-
enc = bkt.Get(blockRoot)
843-
if enc == nil {
844-
return 0, errors.New("state enc can't be nil")
845-
}
846-
// no need to construct the validator entries as it is not used here.
847-
s, err := s.unmarshalState(ctx, enc, nil)
848-
if err != nil {
849-
return 0, errors.Wrap(err, "could not unmarshal state")
850-
}
851-
if s == nil || s.IsNil() {
852-
return 0, errors.New("state can't be nil")
853-
}
854-
return s.Slot(), nil
855-
}
856-
b, err := unmarshalBlock(ctx, enc)
857-
if err != nil {
858-
return 0, errors.Wrap(err, "could not unmarshal block")
859-
}
860-
if err := blocks.BeaconBlockIsNil(b); err != nil {
861-
return 0, err
862-
}
863-
return b.Block().Slot(), nil
849+
// fall back to state, only if state diff feature is not enabled
850+
if features.Get().EnableStateDiff {
851+
return 0, errors.New("neither state summary nor block found")
852+
}
853+
854+
st, err := s.State(ctx, blockRoot)
855+
if err != nil {
856+
return 0, err
864857
}
865-
stateSummary := &ethpb.StateSummary{}
866-
if err := decode(ctx, enc, stateSummary); err != nil {
867-
return 0, errors.Wrap(err, "could not unmarshal state summary")
858+
if st != nil && !st.IsNil() {
859+
return st.Slot(), nil
868860
}
869-
return stateSummary.Slot, nil
861+
862+
// neither state summary, block nor state found
863+
return 0, errors.New("neither state summary, block nor state found")
870864
}
871865

872866
// HighestSlotStatesBelow returns the states with the highest slot below the input slot
@@ -1044,24 +1038,10 @@ func (s *Store) isStateValidatorMigrationOver() (bool, error) {
10441038
}
10451039

10461040
func (s *Store) getStateUsingStateDiff(ctx context.Context, blockRoot [32]byte) (state.BeaconState, error) {
1047-
var slot primitives.Slot
1048-
1049-
stateSummary, err := s.StateSummary(ctx, blockRoot)
1041+
slot, err := s.SlotByBlockRoot(ctx, blockRoot)
10501042
if err != nil {
10511043
return nil, err
10521044
}
1053-
if stateSummary == nil {
1054-
blk, err := s.Block(ctx, blockRoot)
1055-
if err != nil {
1056-
return nil, err
1057-
}
1058-
if blk == nil || blk.IsNil() {
1059-
return nil, errors.New("neither state summary nor block found")
1060-
}
1061-
slot = blk.Block().Slot()
1062-
} else {
1063-
slot = stateSummary.Slot
1064-
}
10651045

10661046
st, err := s.stateByDiff(ctx, slot)
10671047
if err != nil {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Ignored
2+
3+
- Refactor finding slot by block root using state summary and block to its own function.

0 commit comments

Comments
 (0)