@@ -15,6 +15,8 @@ import (
1515// beacon block root. The envelope is stored in blinded form: the full execution payload is replaced
1616// with its block hash. The full payload can later be retrieved from the EL via
1717// engine_getPayloadBodiesByHash.
18+ // A secondary index from BlockHash → BeaconBlockRoot is maintained so that
19+ // envelopes can be looked up by execution block hash.
1820func (s * Store ) SaveExecutionPayloadEnvelope (ctx context.Context , env * ethpb.SignedExecutionPayloadEnvelope ) error {
1921 _ , span := trace .StartSpan (ctx , "BeaconDB.SaveExecutionPayloadEnvelope" )
2022 defer span .End ()
@@ -24,6 +26,7 @@ func (s *Store) SaveExecutionPayloadEnvelope(ctx context.Context, env *ethpb.Sig
2426 }
2527
2628 blockRoot := bytesutil .ToBytes32 (env .Message .BeaconBlockRoot )
29+ blockHash := bytesutil .ToBytes32 (env .Message .Payload .BlockHash )
2730 blinded := blindEnvelope (env )
2831
2932 enc , err := encodeBlindedEnvelope (blinded )
@@ -32,8 +35,10 @@ func (s *Store) SaveExecutionPayloadEnvelope(ctx context.Context, env *ethpb.Sig
3235 }
3336
3437 return s .db .Update (func (tx * bolt.Tx ) error {
35- bkt := tx .Bucket (executionPayloadEnvelopesBucket )
36- return bkt .Put (blockRoot [:], enc )
38+ if err := tx .Bucket (executionPayloadEnvelopesBucket ).Put (blockRoot [:], enc ); err != nil {
39+ return err
40+ }
41+ return tx .Bucket (executionPayloadEnvelopeBlockHashBucket ).Put (blockHash [:], blockRoot [:])
3742 })
3843}
3944
@@ -56,6 +61,30 @@ func (s *Store) ExecutionPayloadEnvelope(ctx context.Context, blockRoot [32]byte
5661 return decodeBlindedEnvelope (enc )
5762}
5863
64+ // ExecutionPayloadEnvelopeByBlockHash retrieves the blinded signed execution payload envelope
65+ // by execution block hash. It uses the secondary BlockHash → BeaconBlockRoot index and then
66+ // fetches the envelope from the primary bucket.
67+ func (s * Store ) ExecutionPayloadEnvelopeByBlockHash (ctx context.Context , blockHash [32 ]byte ) (* ethpb.SignedBlindedExecutionPayloadEnvelope , error ) {
68+ _ , span := trace .StartSpan (ctx , "BeaconDB.ExecutionPayloadEnvelopeByBlockHash" )
69+ defer span .End ()
70+
71+ var enc []byte
72+ if err := s .db .View (func (tx * bolt.Tx ) error {
73+ blockRoot := tx .Bucket (executionPayloadEnvelopeBlockHashBucket ).Get (blockHash [:])
74+ if blockRoot == nil {
75+ return nil
76+ }
77+ enc = tx .Bucket (executionPayloadEnvelopesBucket ).Get (blockRoot )
78+ return nil
79+ }); err != nil {
80+ return nil , err
81+ }
82+ if enc == nil {
83+ return nil , errors .Wrap (ErrNotFound , "execution payload envelope not found for block hash" )
84+ }
85+ return decodeBlindedEnvelope (enc )
86+ }
87+
5988// HasExecutionPayloadEnvelope checks whether an execution payload envelope exists for the given beacon block root.
6089func (s * Store ) HasExecutionPayloadEnvelope (ctx context.Context , blockRoot [32 ]byte ) bool {
6190 _ , span := trace .StartSpan (ctx , "BeaconDB.HasExecutionPayloadEnvelope" )
@@ -72,13 +101,25 @@ func (s *Store) HasExecutionPayloadEnvelope(ctx context.Context, blockRoot [32]b
72101 return exists
73102}
74103
75- // DeleteExecutionPayloadEnvelope removes a signed execution payload envelope by beacon block root.
104+ // DeleteExecutionPayloadEnvelope removes a signed execution payload envelope by beacon block root
105+ // and cleans up the BlockHash index entry.
76106func (s * Store ) DeleteExecutionPayloadEnvelope (ctx context.Context , blockRoot [32 ]byte ) error {
77107 _ , span := trace .StartSpan (ctx , "BeaconDB.DeleteExecutionPayloadEnvelope" )
78108 defer span .End ()
79109
80110 return s .db .Update (func (tx * bolt.Tx ) error {
81111 bkt := tx .Bucket (executionPayloadEnvelopesBucket )
112+ // Read the existing entry to find the BlockHash for index cleanup.
113+ enc := bkt .Get (blockRoot [:])
114+ if enc != nil {
115+ blinded , err := decodeBlindedEnvelope (enc )
116+ if err == nil && blinded .Message != nil {
117+ blockHash := bytesutil .ToBytes32 (blinded .Message .BlockHash )
118+ if err := tx .Bucket (executionPayloadEnvelopeBlockHashBucket ).Delete (blockHash [:]); err != nil {
119+ return err
120+ }
121+ }
122+ }
82123 return bkt .Delete (blockRoot [:])
83124 })
84125}
@@ -95,6 +136,7 @@ func blindEnvelope(env *ethpb.SignedExecutionPayloadEnvelope) *ethpb.SignedBlind
95136 BeaconBlockRoot : env .Message .BeaconBlockRoot ,
96137 Slot : env .Message .Slot ,
97138 StateRoot : env .Message .StateRoot ,
139+ ParentBlockHash : env .Message .Payload .ParentHash ,
98140 },
99141 Signature : env .Signature ,
100142 }
0 commit comments