@@ -3,6 +3,7 @@ package cache
33import (
44 "context"
55 "encoding/binary"
6+ "fmt"
67 "sync"
78 "sync/atomic"
89
@@ -148,14 +149,14 @@ func (c *Cache[T]) setDAIncluded(hash string, daHeight uint64, blockHeight uint6
148149 c .daIncluded .Add (hash , daHeight )
149150 c .hashByHeight .Add (blockHeight , hash )
150151 c .setMaxDAHeight (daHeight )
151- c .persistSnapshot (context .Background ())
152+ _ = c .persistSnapshot (context .Background ())
152153}
153154
154155// removeDAIncluded removes the DA-included status of the hash from the cache
155156// and rewrites the window snapshot.
156157func (c * Cache [T ]) removeDAIncluded (hash string ) {
157158 c .daIncluded .Remove (hash )
158- c .persistSnapshot (context .Background ())
159+ _ = c .persistSnapshot (context .Background ())
159160}
160161
161162// daHeight returns the maximum DA height from all DA-included items.
@@ -200,15 +201,15 @@ func (c *Cache[T]) deleteAllForHeight(height uint64) {
200201 c .daIncluded .Remove (hash )
201202 }
202203
203- c .persistSnapshot (context .Background ())
204+ _ = c .persistSnapshot (context .Background ())
204205}
205206
206207// persistSnapshot writes all current in-flight [blockHeight, daHeight] pairs
207208// to the store under a single key. Called on every mutation; payload is tiny
208209// (typically <10 entries × 16 bytes).
209- func (c * Cache [T ]) persistSnapshot (ctx context.Context ) {
210+ func (c * Cache [T ]) persistSnapshot (ctx context.Context ) error {
210211 if c .store == nil || c .storeKeyPrefix == "" {
211- return
212+ return nil
212213 }
213214
214215 heights := c .hashByHeight .Keys ()
@@ -225,7 +226,7 @@ func (c *Cache[T]) persistSnapshot(ctx context.Context) {
225226 entries = append (entries , snapshotEntry {blockHeight : h , daHeight : daH })
226227 }
227228
228- _ = c .store .SetMetadata (ctx , c .snapshotKey (), encodeSnapshot (entries ))
229+ return c .store .SetMetadata (ctx , c .snapshotKey (), encodeSnapshot (entries ))
229230}
230231
231232// encodeSnapshot serialises a slice of snapshotEntry values into a byte slice.
@@ -298,7 +299,9 @@ func (c *Cache[T]) SaveToStore(ctx context.Context) error {
298299 if c .store == nil {
299300 return nil
300301 }
301- c .persistSnapshot (ctx )
302+ if err := c .persistSnapshot (ctx ); err != nil {
303+ return fmt .Errorf ("saving cache snapshot: %w" , err )
304+ }
302305 return nil
303306}
304307
@@ -307,6 +310,8 @@ func (c *Cache[T]) ClearFromStore(ctx context.Context) error {
307310 if c .store == nil {
308311 return nil
309312 }
310- _ = c .store .DeleteMetadata (ctx , c .snapshotKey ())
313+ if err := c .store .DeleteMetadata (ctx , c .snapshotKey ()); err != nil {
314+ return fmt .Errorf ("clearing cache snapshot: %w" , err )
315+ }
311316 return nil
312317}
0 commit comments