|
8 | 8 | "github.com/OffchainLabs/prysm/v7/beacon-chain/state" |
9 | 9 | statenative "github.com/OffchainLabs/prysm/v7/beacon-chain/state/state-native" |
10 | 10 | "github.com/OffchainLabs/prysm/v7/config/features" |
11 | | - "github.com/OffchainLabs/prysm/v7/consensus-types/blocks" |
12 | 11 | "github.com/OffchainLabs/prysm/v7/consensus-types/primitives" |
13 | 12 | "github.com/OffchainLabs/prysm/v7/encoding/bytesutil" |
14 | 13 | "github.com/OffchainLabs/prysm/v7/genesis" |
@@ -481,7 +480,7 @@ func (s *Store) DeleteState(ctx context.Context, blockRoot [32]byte) error { |
481 | 480 | return nil |
482 | 481 | } |
483 | 482 |
|
484 | | - slot, err := s.slotByBlockRoot(ctx, tx, blockRoot[:]) |
| 483 | + slot, err := s.SlotByBlockRoot(ctx, blockRoot) |
485 | 484 | if err != nil { |
486 | 485 | return err |
487 | 486 | } |
@@ -823,50 +822,45 @@ func (s *Store) stateBytes(ctx context.Context, blockRoot [32]byte) ([]byte, err |
823 | 822 | return dst, err |
824 | 823 | } |
825 | 824 |
|
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") |
829 | 829 | defer span.End() |
830 | 830 |
|
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 | + } |
833 | 839 |
|
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 | + } |
838 | 848 |
|
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 |
864 | 857 | } |
865 | | - stateSummary := ðpb.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 |
868 | 860 | } |
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") |
870 | 864 | } |
871 | 865 |
|
872 | 866 | // HighestSlotStatesBelow returns the states with the highest slot below the input slot |
@@ -1044,24 +1038,10 @@ func (s *Store) isStateValidatorMigrationOver() (bool, error) { |
1044 | 1038 | } |
1045 | 1039 |
|
1046 | 1040 | 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) |
1050 | 1042 | if err != nil { |
1051 | 1043 | return nil, err |
1052 | 1044 | } |
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 | | - } |
1065 | 1045 |
|
1066 | 1046 | st, err := s.stateByDiff(ctx, slot) |
1067 | 1047 | if err != nil { |
|
0 commit comments