@@ -24,7 +24,7 @@ const snapshotEntrySize = 16 // bytes per snapshotEntry
2424
2525// Cache tracks seen blocks and DA inclusion status.
2626type Cache struct {
27- mu sync.Mutex
27+ mu sync.RWMutex
2828
2929 hashes map [string ]bool
3030 daIncluded map [string ]uint64
@@ -53,8 +53,8 @@ func NewCache(s store.Store, keyPrefix string) *Cache {
5353}
5454
5555func (c * Cache ) isSeen (hash string ) bool {
56- c .mu .Lock ()
57- defer c .mu .Unlock ()
56+ c .mu .RLock ()
57+ defer c .mu .RUnlock ()
5858 return c .hashes [hash ]
5959}
6060
@@ -72,15 +72,15 @@ func (c *Cache) removeSeen(hash string) {
7272}
7373
7474func (c * Cache ) getDAIncluded (hash string ) (uint64 , bool ) {
75- c .mu .Lock ()
76- defer c .mu .Unlock ()
75+ c .mu .RLock ()
76+ defer c .mu .RUnlock ()
7777 v , ok := c .daIncluded [hash ]
7878 return v , ok
7979}
8080
8181func (c * Cache ) getDAIncludedByHeight (blockHeight uint64 ) (uint64 , bool ) {
82- c .mu .Lock ()
83- defer c .mu .Unlock ()
82+ c .mu .RLock ()
83+ defer c .mu .RUnlock ()
8484 hash , ok := c .hashByHeight [blockHeight ]
8585 if ! ok {
8686 return 0 , false
@@ -138,8 +138,8 @@ func (c *Cache) deleteAllForHeight(height uint64) {
138138}
139139
140140func (c * Cache ) daIncludedLen () int {
141- c .mu .Lock ()
142- defer c .mu .Unlock ()
141+ c .mu .RLock ()
142+ defer c .mu .RUnlock ()
143143 return len (c .daIncluded )
144144}
145145
@@ -150,7 +150,7 @@ func (c *Cache) persistSnapshot(ctx context.Context) error {
150150 return nil
151151 }
152152
153- c .mu .Lock ()
153+ c .mu .RLock ()
154154 entries := make ([]snapshotEntry , 0 , len (c .hashByHeight ))
155155 for h , hash := range c .hashByHeight {
156156 daH , ok := c .daIncluded [hash ]
@@ -159,7 +159,7 @@ func (c *Cache) persistSnapshot(ctx context.Context) error {
159159 }
160160 entries = append (entries , snapshotEntry {blockHeight : h , daHeight : daH })
161161 }
162- c .mu .Unlock ()
162+ c .mu .RUnlock ()
163163
164164 return c .store .SetMetadata (ctx , c .snapshotKey (), encodeSnapshot (entries ))
165165}
0 commit comments