diff --git a/das/bigcache_storage_service.go b/das/bigcache_storage_service.go deleted file mode 100644 index 21b70f86a9..0000000000 --- a/das/bigcache_storage_service.go +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright 2022, Offchain Labs, Inc. -// For license information, see https://github.com/nitro/blob/master/LICENSE - -package das - -import ( - "context" - "fmt" - - "github.com/offchainlabs/nitro/arbstate" - "github.com/offchainlabs/nitro/das/dastree" - "github.com/offchainlabs/nitro/util/pretty" - flag "github.com/spf13/pflag" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/lru" - "github.com/ethereum/go-ethereum/log" -) - -type BigCacheConfig struct { - Enable bool `koanf:"enable"` - Capacity int `koanf:"capacity"` -} - -var DefaultBigCacheConfig = BigCacheConfig{ - Capacity: 20_000, -} - -var TestBigCacheConfig = BigCacheConfig{ - Capacity: 1_000, -} - -func BigCacheConfigAddOptions(prefix string, f *flag.FlagSet) { - f.Bool(prefix+".enable", DefaultBigCacheConfig.Enable, "Enable local in-memory caching of sequencer batch data") - f.Int(prefix+".capacity", DefaultBigCacheConfig.Capacity, "Maximum number of entries (up to 64KB each) to store in the cache.") -} - -type BigCacheStorageService struct { - baseStorageService StorageService - cache *lru.Cache[common.Hash, []byte] -} - -func NewBigCacheStorageService(bigCacheConfig BigCacheConfig, baseStorageService StorageService) *BigCacheStorageService { - return &BigCacheStorageService{ - baseStorageService: baseStorageService, - cache: lru.NewCache[common.Hash, []byte](bigCacheConfig.Capacity), - } -} - -func (bcs *BigCacheStorageService) GetByHash(ctx context.Context, key common.Hash) ([]byte, error) { - log.Trace("das.BigCacheStorageService.GetByHash", "key", pretty.PrettyHash(key), "this", bcs) - - if val, wasCached := bcs.cache.Get(key); wasCached { - return val, nil - } - - val, err := bcs.baseStorageService.GetByHash(ctx, key) - if err != nil { - return nil, err - } - - bcs.cache.Add(key, val) - - return val, nil -} - -func (bcs *BigCacheStorageService) Put(ctx context.Context, value []byte, timeout uint64) error { - logPut("das.BigCacheStorageService.Put", value, timeout, bcs) - err := bcs.baseStorageService.Put(ctx, value, timeout) - if err != nil { - return err - } - bcs.cache.Add(common.Hash(dastree.Hash(value)), value) - return nil -} - -func (bcs *BigCacheStorageService) Sync(ctx context.Context) error { - return bcs.baseStorageService.Sync(ctx) -} - -func (bcs *BigCacheStorageService) Close(ctx context.Context) error { - return bcs.baseStorageService.Close(ctx) -} - -func (bcs *BigCacheStorageService) ExpirationPolicy(ctx context.Context) (arbstate.ExpirationPolicy, error) { - return bcs.baseStorageService.ExpirationPolicy(ctx) -} - -func (bcs *BigCacheStorageService) String() string { - return fmt.Sprintf("BigCacheStorageService(size:%+v)", len(bcs.cache.Keys())) -} - -func (bcs *BigCacheStorageService) HealthCheck(ctx context.Context) error { - return bcs.baseStorageService.HealthCheck(ctx) -} diff --git a/das/cache_storage_service.go b/das/cache_storage_service.go new file mode 100644 index 0000000000..13bdb189d3 --- /dev/null +++ b/das/cache_storage_service.go @@ -0,0 +1,95 @@ +// Copyright 2022, Offchain Labs, Inc. +// For license information, see https://github.com/nitro/blob/master/LICENSE + +package das + +import ( + "context" + "fmt" + + "github.com/offchainlabs/nitro/arbstate" + "github.com/offchainlabs/nitro/das/dastree" + "github.com/offchainlabs/nitro/util/pretty" + flag "github.com/spf13/pflag" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/lru" + "github.com/ethereum/go-ethereum/log" +) + +type CacheConfig struct { + Enable bool `koanf:"enable"` + Capacity int `koanf:"capacity"` +} + +var DefaultCacheConfig = CacheConfig{ + Capacity: 20_000, +} + +var TestCacheConfig = CacheConfig{ + Capacity: 1_000, +} + +func CacheConfigAddOptions(prefix string, f *flag.FlagSet) { + f.Bool(prefix+".enable", DefaultCacheConfig.Enable, "Enable local in-memory caching of sequencer batch data") + f.Int(prefix+".capacity", DefaultCacheConfig.Capacity, "Maximum number of entries (up to 64KB each) to store in the cache.") +} + +type CacheStorageService struct { + baseStorageService StorageService + cache *lru.Cache[common.Hash, []byte] +} + +func NewCacheStorageService(cacheConfig CacheConfig, baseStorageService StorageService) *CacheStorageService { + return &CacheStorageService{ + baseStorageService: baseStorageService, + cache: lru.NewCache[common.Hash, []byte](cacheConfig.Capacity), + } +} + +func (c *CacheStorageService) GetByHash(ctx context.Context, key common.Hash) ([]byte, error) { + log.Trace("das.CacheStorageService.GetByHash", "key", pretty.PrettyHash(key), "this", c) + + if val, wasCached := c.cache.Get(key); wasCached { + return val, nil + } + + val, err := c.baseStorageService.GetByHash(ctx, key) + if err != nil { + return nil, err + } + + c.cache.Add(key, val) + + return val, nil +} + +func (c *CacheStorageService) Put(ctx context.Context, value []byte, timeout uint64) error { + logPut("das.CacheStorageService.Put", value, timeout, c) + err := c.baseStorageService.Put(ctx, value, timeout) + if err != nil { + return err + } + c.cache.Add(common.Hash(dastree.Hash(value)), value) + return nil +} + +func (c *CacheStorageService) Sync(ctx context.Context) error { + return c.baseStorageService.Sync(ctx) +} + +func (c *CacheStorageService) Close(ctx context.Context) error { + return c.baseStorageService.Close(ctx) +} + +func (c *CacheStorageService) ExpirationPolicy(ctx context.Context) (arbstate.ExpirationPolicy, error) { + return c.baseStorageService.ExpirationPolicy(ctx) +} + +func (c *CacheStorageService) String() string { + return fmt.Sprintf("CacheStorageService(size:%+v)", len(c.cache.Keys())) +} + +func (c *CacheStorageService) HealthCheck(ctx context.Context) error { + return c.baseStorageService.HealthCheck(ctx) +} diff --git a/das/bigcache_storage_service_test.go b/das/cache_storage_service_test.go similarity index 68% rename from das/bigcache_storage_service_test.go rename to das/cache_storage_service_test.go index 6fe8cbc709..8b4203dab5 100644 --- a/das/bigcache_storage_service_test.go +++ b/das/cache_storage_service_test.go @@ -12,28 +12,28 @@ import ( "github.com/offchainlabs/nitro/das/dastree" ) -func TestBigCacheStorageService(t *testing.T) { +func TestCacheStorageService(t *testing.T) { ctx := context.Background() baseStorageService := NewMemoryBackedStorageService(ctx) - bigCacheService := NewBigCacheStorageService(TestBigCacheConfig, baseStorageService) + cacheService := NewCacheStorageService(TestCacheConfig, baseStorageService) val1 := []byte("The first value") val1CorrectKey := dastree.Hash(val1) val1IncorrectKey := dastree.Hash(append(val1, 0)) - _, err := bigCacheService.GetByHash(ctx, val1CorrectKey) + _, err := cacheService.GetByHash(ctx, val1CorrectKey) if !errors.Is(err, ErrNotFound) { t.Fatal(err) } - err = bigCacheService.Put(ctx, val1, 1) + err = cacheService.Put(ctx, val1, 1) Require(t, err) - _, err = bigCacheService.GetByHash(ctx, val1IncorrectKey) + _, err = cacheService.GetByHash(ctx, val1IncorrectKey) if !errors.Is(err, ErrNotFound) { t.Fatal(err) } - val, err := bigCacheService.GetByHash(ctx, val1CorrectKey) + val, err := cacheService.GetByHash(ctx, val1CorrectKey) Require(t, err) if !bytes.Equal(val, val1) { t.Fatal(val, val1) @@ -47,11 +47,11 @@ func TestBigCacheStorageService(t *testing.T) { err = baseStorageService.Put(ctx, val2, 1) Require(t, err) - _, err = bigCacheService.GetByHash(ctx, val2IncorrectKey) + _, err = cacheService.GetByHash(ctx, val2IncorrectKey) if !errors.Is(err, ErrNotFound) { t.Fatal(err) } - val, err = bigCacheService.GetByHash(ctx, val2CorrectKey) + val, err = cacheService.GetByHash(ctx, val2CorrectKey) Require(t, err) if !bytes.Equal(val, val2) { t.Fatal(val, val2) @@ -59,18 +59,18 @@ func TestBigCacheStorageService(t *testing.T) { // For Case where the value is present in the cache storage but not present in the base. emptyBaseStorageService := NewMemoryBackedStorageService(ctx) - bigCacheServiceWithEmptyBaseStorage := &BigCacheStorageService{ + cacheServiceWithEmptyBaseStorage := &CacheStorageService{ baseStorageService: emptyBaseStorageService, - cache: bigCacheService.cache, + cache: cacheService.cache, } - val, err = bigCacheServiceWithEmptyBaseStorage.GetByHash(ctx, val1CorrectKey) + val, err = cacheServiceWithEmptyBaseStorage.GetByHash(ctx, val1CorrectKey) Require(t, err) if !bytes.Equal(val, val1) { t.Fatal(val, val1) } // Closes the base storage properly. - err = bigCacheService.Close(ctx) + err = cacheService.Close(ctx) Require(t, err) _, err = baseStorageService.GetByHash(ctx, val1CorrectKey) if !errors.Is(err, ErrClosed) { diff --git a/das/das.go b/das/das.go index 910e511083..dd8e43a34d 100644 --- a/das/das.go +++ b/das/das.go @@ -40,8 +40,8 @@ type DataAvailabilityConfig struct { RequestTimeout time.Duration `koanf:"request-timeout"` - LocalCache BigCacheConfig `koanf:"local-cache"` - RedisCache RedisConfig `koanf:"redis-cache"` + LocalCache CacheConfig `koanf:"local-cache"` + RedisCache RedisConfig `koanf:"redis-cache"` LocalDBStorage LocalDBStorageConfig `koanf:"local-db-storage"` LocalFileStorage LocalFileStorageConfig `koanf:"local-file-storage"` @@ -109,7 +109,7 @@ func dataAvailabilityConfigAddOptions(prefix string, f *flag.FlagSet, r role) { f.Bool(prefix+".disable-signature-checking", DefaultDataAvailabilityConfig.DisableSignatureChecking, "disables signature checking on Data Availability Store requests (DANGEROUS, FOR TESTING ONLY)") // Cache options - BigCacheConfigAddOptions(prefix+".local-cache", f) + CacheConfigAddOptions(prefix+".local-cache", f) RedisConfigAddOptions(prefix+".redis-cache", f) // Storage options diff --git a/das/factory.go b/das/factory.go index c4223b8be2..d5f103e548 100644 --- a/das/factory.go +++ b/das/factory.go @@ -112,7 +112,7 @@ func WrapStorageWithCache( return nil, nil } - // Enable caches, Redis and (local) BigCache. Local is the outermost, so it will be tried first. + // Enable caches, Redis and (local) Cache. Local is the outermost, so it will be tried first. var err error if config.RedisCache.Enable { storageService, err = NewRedisStorageService(config.RedisCache, storageService) @@ -130,7 +130,7 @@ func WrapStorageWithCache( } } if config.LocalCache.Enable { - storageService = NewBigCacheStorageService(config.LocalCache, storageService) + storageService = NewCacheStorageService(config.LocalCache, storageService) lifecycleManager.Register(storageService) } return storageService, nil diff --git a/system_tests/das_test.go b/system_tests/das_test.go index 8edd91e1ec..e0d0ad6a11 100644 --- a/system_tests/das_test.go +++ b/system_tests/das_test.go @@ -256,7 +256,7 @@ func TestDASComplexConfigAndRestMirror(t *testing.T) { serverConfig := das.DataAvailabilityConfig{ Enable: true, - LocalCache: das.TestBigCacheConfig, + LocalCache: das.TestCacheConfig, LocalFileStorage: das.LocalFileStorageConfig{ Enable: true,