Skip to content

Commit 38a57ef

Browse files
authored
fix: prevent cache size underflow (#4986)
1 parent 4940ef8 commit 38a57ef

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

pkg/storer/cachestore.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"fmt"
1111
"time"
1212

13-
storage "github.com/ethersphere/bee/v2/pkg/storage"
13+
"github.com/ethersphere/bee/v2/pkg/storage"
1414
"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
1515
"github.com/ethersphere/bee/v2/pkg/swarm"
1616
)
@@ -39,7 +39,7 @@ func (db *DB) cacheWorker(ctx context.Context) {
3939
continue
4040
}
4141

42-
evict := size - capc
42+
evict := uint64(size - capc)
4343
if evict < db.reserveOptions.cacheMinEvictCount { // evict at least a min count
4444
evict = db.reserveOptions.cacheMinEvictCount
4545
}

pkg/storer/internal/cache/cache.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"sync/atomic"
1515
"time"
1616

17-
storage "github.com/ethersphere/bee/v2/pkg/storage"
17+
"github.com/ethersphere/bee/v2/pkg/storage"
1818
"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
1919
"github.com/ethersphere/bee/v2/pkg/swarm"
2020
"golang.org/x/sync/errgroup"
@@ -61,12 +61,14 @@ func New(ctx context.Context, store storage.Reader, capacity uint64) (*Cache, er
6161
}
6262

6363
// Size returns the current size of the cache.
64-
func (c *Cache) Size() uint64 {
65-
return uint64(c.size.Load())
64+
func (c *Cache) Size() int64 {
65+
return c.size.Load()
6666
}
6767

6868
// Capacity returns the capacity of the cache.
69-
func (c *Cache) Capacity() uint64 { return uint64(c.capacity) }
69+
func (c *Cache) Capacity() int64 {
70+
return int64(c.capacity)
71+
}
7072

7173
// Putter returns a Storage.Putter instance which adds the chunk to the underlying
7274
// chunkstore and also adds a Cache entry for the chunk.

pkg/storer/internal/cache/export_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"fmt"
1010
"time"
1111

12-
storage "github.com/ethersphere/bee/v2/pkg/storage"
12+
"github.com/ethersphere/bee/v2/pkg/storage"
1313
"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
1414
"github.com/ethersphere/bee/v2/pkg/swarm"
1515
)
@@ -43,7 +43,7 @@ func (c *Cache) RemoveOldestMaxBatch(ctx context.Context, st transaction.Storage
4343

4444
func (c *Cache) State(store storage.Reader) CacheState {
4545
state := CacheState{}
46-
state.Size = c.Size()
46+
state.Size = uint64(c.Size())
4747
runner := swarm.ZeroAddress
4848

4949
err := store.Iterate(

0 commit comments

Comments
 (0)