@@ -73,6 +73,29 @@ func NewCache[T any](s store.Store, keyPrefix string) *Cache[T] {
7373 }
7474}
7575
76+ // storeKey returns the store key for a given hash.
77+ func (c * Cache [T ]) storeKey (hash string ) string {
78+ return c .storeKeyPrefix + hash
79+ }
80+
81+ // encodeDAInclusion encodes daHeight and blockHeight into a 16-byte value.
82+ func encodeDAInclusion (daHeight , blockHeight uint64 ) []byte {
83+ value := make ([]byte , 16 ) // 8 bytes for daHeight + 8 bytes for blockHeight
84+ binary .LittleEndian .PutUint64 (value [0 :8 ], daHeight )
85+ binary .LittleEndian .PutUint64 (value [8 :16 ], blockHeight )
86+ return value
87+ }
88+
89+ // decodeDAInclusion decodes a 16-byte value into daHeight and blockHeight.
90+ func decodeDAInclusion (value []byte ) (daHeight , blockHeight uint64 , ok bool ) {
91+ if len (value ) != 16 {
92+ return 0 , 0 , false
93+ }
94+ daHeight = binary .LittleEndian .Uint64 (value [0 :8 ])
95+ blockHeight = binary .LittleEndian .Uint64 (value [8 :16 ])
96+ return daHeight , blockHeight , true
97+ }
98+
7699// getItem returns an item from the cache by height.
77100// Returns nil if not found or type mismatch.
78101func (c * Cache [T ]) getItem (height uint64 ) * T {
@@ -131,13 +154,21 @@ func (c *Cache[T]) setDAIncluded(hash string, daHeight uint64, blockHeight uint6
131154 c .daIncluded .Add (hash , daHeight )
132155 c .hashByHeight .Add (blockHeight , hash )
133156
157+ // Persist to store if configured (for SIGKILL protection)
158+ if c .store != nil {
159+ _ = c .store .SetMetadata (context .Background (), c .storeKey (hash ), encodeDAInclusion (daHeight , blockHeight ))
160+ }
161+
134162 // Update max DA height if necessary
135163 c .setMaxDAHeight (daHeight )
136164}
137165
138- // removeDAIncluded removes the DA-included status of the hash
166+ // removeDAIncluded removes the DA-included status of the hash from cache and store.
139167func (c * Cache [T ]) removeDAIncluded (hash string ) {
140168 c .daIncluded .Remove (hash )
169+ if c .store != nil {
170+ _ = c .store .DeleteMetadata (context .Background (), c .storeKey (hash ))
171+ }
141172}
142173
143174// daHeight returns the maximum DA height from all DA-included items.
@@ -165,7 +196,7 @@ func (c *Cache[T]) removeSeen(hash string) {
165196 c .hashes .Remove (hash )
166197}
167198
168- // deleteAllForHeight removes all items and their associated data from the cache at the given height.
199+ // deleteAllForHeight removes all items and their associated data from the cache and store at the given height.
169200func (c * Cache [T ]) deleteAllForHeight (height uint64 ) {
170201 c .itemsByHeight .Remove (height )
171202
@@ -178,7 +209,7 @@ func (c *Cache[T]) deleteAllForHeight(height uint64) {
178209
179210 if ok {
180211 c .hashes .Remove (hash )
181- c .daIncluded . Remove (hash )
212+ c .removeDAIncluded (hash )
182213 }
183214}
184215
@@ -191,19 +222,17 @@ func (c *Cache[T]) RestoreFromStore(ctx context.Context, hashes []string) error
191222 }
192223
193224 for _ , hash := range hashes {
194- key := c .storeKeyPrefix + hash
195- value , err := c .store .GetMetadata (ctx , key )
225+ value , err := c .store .GetMetadata (ctx , c .storeKey (hash ))
196226 if err != nil {
197227 // Key not found is not an error - the hash may not have been DA included yet
198228 continue
199229 }
200- if len (value ) != 16 {
230+
231+ daHeight , blockHeight , ok := decodeDAInclusion (value )
232+ if ! ok {
201233 continue // Invalid data, skip
202234 }
203235
204- daHeight := binary .LittleEndian .Uint64 (value [0 :8 ])
205- blockHeight := binary .LittleEndian .Uint64 (value [8 :16 ])
206-
207236 c .daIncluded .Add (hash , daHeight )
208237 c .hashByHeight .Add (blockHeight , hash )
209238
@@ -242,12 +271,7 @@ func (c *Cache[T]) SaveToStore(ctx context.Context) error {
242271 }
243272 }
244273
245- key := c .storeKeyPrefix + hash
246- value := make ([]byte , 16 )
247- binary .LittleEndian .PutUint64 (value [0 :8 ], daHeight )
248- binary .LittleEndian .PutUint64 (value [8 :16 ], blockHeight )
249-
250- if err := c .store .SetMetadata (ctx , key , value ); err != nil {
274+ if err := c .store .SetMetadata (ctx , c .storeKey (hash ), encodeDAInclusion (daHeight , blockHeight )); err != nil {
251275 return fmt .Errorf ("failed to save DA inclusion for hash %s: %w" , hash , err )
252276 }
253277 }
@@ -262,8 +286,7 @@ func (c *Cache[T]) ClearFromStore(ctx context.Context, hashes []string) error {
262286 }
263287
264288 for _ , hash := range hashes {
265- key := c .storeKeyPrefix + hash
266- if err := c .store .DeleteMetadata (ctx , key ); err != nil {
289+ if err := c .store .DeleteMetadata (ctx , c .storeKey (hash )); err != nil {
267290 return fmt .Errorf ("failed to delete DA inclusion for hash %s: %w" , hash , err )
268291 }
269292 }
0 commit comments