From f202d2d202cf80deec38776a4659778be0120e96 Mon Sep 17 00:00:00 2001 From: amsanghi Date: Fri, 10 Nov 2023 15:04:42 +0530 Subject: [PATCH 1/2] Clean up old cache files --- staker/challenge-cache/cache.go | 38 ++++++++++++++++++++++++++-- staker/challenge-cache/cache_test.go | 12 ++++++--- staker/state_provider.go | 5 +++- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/staker/challenge-cache/cache.go b/staker/challenge-cache/cache.go index 1a79ff507c..a2d54afe3b 100644 --- a/staker/challenge-cache/cache.go +++ b/staker/challenge-cache/cache.go @@ -34,6 +34,7 @@ import ( "io" "os" "path/filepath" + "time" protocol "github.com/OffchainLabs/bold/chain-abstraction" "github.com/OffchainLabs/bold/containers/option" @@ -62,11 +63,44 @@ type Cache struct { baseDir string } +func isOlderThanFourteenDays(t time.Time) bool { + return time.Now().Sub(t) > 14*24*time.Hour +} + +func deleteFilesOlderThanFourteenDays(dir string) error { + files, err := os.ReadDir(dir) + if err != nil { + return err + } + for _, file := range files { + fileInfo, err := file.Info() + if err != nil { + return err + } + if fileInfo.IsDir() { + if err := deleteFilesOlderThanFourteenDays(filepath.Join(dir, fileInfo.Name())); err != nil { + return err + } + } else { + if isOlderThanFourteenDays(fileInfo.ModTime()) { + if err := os.Remove(filepath.Join(dir, fileInfo.Name())); err != nil { + return err + } + } + } + } + return nil +} + // New cache from a base directory path. -func New(baseDir string) *Cache { +func New(baseDir string) (*Cache, error) { + err := deleteFilesOlderThanFourteenDays(baseDir) + if err != nil { + return nil, err + } return &Cache{ baseDir: baseDir, - } + }, nil } // Key for cache lookups includes the wavm module root of a challenge, as well diff --git a/staker/challenge-cache/cache_test.go b/staker/challenge-cache/cache_test.go index b9fec74b9c..32dc219cf8 100644 --- a/staker/challenge-cache/cache_test.go +++ b/staker/challenge-cache/cache_test.go @@ -29,7 +29,10 @@ func TestCache(t *testing.T) { t.Fatal(err) } }) - cache := New(basePath) + cache, err := New(basePath) + if err != nil { + t.Fatal(err) + } key := &Key{ WavmModuleRoot: common.BytesToHash([]byte("foo")), MessageHeight: 0, @@ -51,7 +54,7 @@ func TestCache(t *testing.T) { common.BytesToHash([]byte("bar")), common.BytesToHash([]byte("baz")), } - err := cache.Put(key, want) + err = cache.Put(key, want) if err != nil { t.Fatal(err) } @@ -295,7 +298,10 @@ func BenchmarkCache_Read_32Mb(b *testing.B) { b.Fatal(err) } }) - cache := New(basePath) + cache, err := New(basePath) + if err != nil { + b.Fatal(err) + } key := &Key{ WavmModuleRoot: common.BytesToHash([]byte("foo")), MessageHeight: 0, diff --git a/staker/state_provider.go b/staker/state_provider.go index 8caaaa3bb4..6106884632 100644 --- a/staker/state_provider.go +++ b/staker/state_provider.go @@ -47,7 +47,10 @@ type StateManager struct { } func NewStateManager(val *StatelessBlockValidator, blockValidator *BlockValidator, numOpcodesPerBigStep uint64, maxWavmOpcodes uint64, cacheBaseDir string) (*StateManager, error) { - historyCache := challengecache.New(cacheBaseDir) + historyCache, err := challengecache.New(cacheBaseDir) + if err != nil { + return nil, err + } return &StateManager{ validator: val, blockValidator: blockValidator, From 60a17ff3cea4f5d18dd47315beab8939ec3e2fa6 Mon Sep 17 00:00:00 2001 From: amsanghi Date: Fri, 10 Nov 2023 15:21:44 +0530 Subject: [PATCH 2/2] fix lint --- staker/challenge-cache/cache.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/staker/challenge-cache/cache.go b/staker/challenge-cache/cache.go index 0b764192e1..b2690b7182 100644 --- a/staker/challenge-cache/cache.go +++ b/staker/challenge-cache/cache.go @@ -68,7 +68,7 @@ type Cache struct { } func isOlderThanFourteenDays(t time.Time) bool { - return time.Now().Sub(t) > 14*24*time.Hour + return time.Since(t) > 14*24*time.Hour } func deleteFilesOlderThanFourteenDays(dir string) error {