|
| 1 | +// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package blockdb |
| 5 | + |
| 6 | +import ( |
| 7 | + "slices" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestCacheOnMiss(t *testing.T) { |
| 14 | + db, _ := newTestDatabase(t, DefaultConfig()) |
| 15 | + height := uint64(20) |
| 16 | + block := randomBlock(t) |
| 17 | + require.NoError(t, db.Put(height, block)) |
| 18 | + |
| 19 | + // Evict the entry from cache to simulate a cache miss |
| 20 | + db.entryCache.Evict(height) |
| 21 | + |
| 22 | + // Read the block - should populate the cache |
| 23 | + _, err := db.Get(height) |
| 24 | + require.NoError(t, err) |
| 25 | + |
| 26 | + // Verify block is now in cache |
| 27 | + _, ok := db.entryCache.Get(height) |
| 28 | + require.True(t, ok) |
| 29 | +} |
| 30 | + |
| 31 | +func TestCacheHas(t *testing.T) { |
| 32 | + db, _ := newTestDatabase(t, DefaultConfig()) |
| 33 | + height := uint64(30) |
| 34 | + block := randomBlock(t) |
| 35 | + require.NoError(t, db.Put(height, block)) |
| 36 | + |
| 37 | + has, err := db.Has(height) |
| 38 | + require.NoError(t, err) |
| 39 | + require.True(t, has) |
| 40 | + |
| 41 | + // Verify block is in cache |
| 42 | + cached, ok := db.entryCache.Get(height) |
| 43 | + require.True(t, ok) |
| 44 | + require.Equal(t, block, cached) |
| 45 | +} |
| 46 | + |
| 47 | +func TestCachePutStoresClone(t *testing.T) { |
| 48 | + db, _ := newTestDatabase(t, DefaultConfig()) |
| 49 | + height := uint64(40) |
| 50 | + block := randomBlock(t) |
| 51 | + copy := slices.Clone(block) |
| 52 | + require.NoError(t, db.Put(height, copy)) |
| 53 | + |
| 54 | + // Modify the original block after Put |
| 55 | + copy[0] = 99 |
| 56 | + |
| 57 | + // Cache should have the original unmodified data |
| 58 | + cached, ok := db.entryCache.Get(height) |
| 59 | + require.True(t, ok) |
| 60 | + require.Equal(t, block, cached) |
| 61 | +} |
| 62 | + |
| 63 | +func TestCacheGetReturnsClone(t *testing.T) { |
| 64 | + db, _ := newTestDatabase(t, DefaultConfig()) |
| 65 | + height := uint64(50) |
| 66 | + block := randomBlock(t) |
| 67 | + require.NoError(t, db.Put(height, block)) |
| 68 | + |
| 69 | + // Get the block and modify the returned data |
| 70 | + data, err := db.Get(height) |
| 71 | + require.NoError(t, err) |
| 72 | + data[0] = 99 |
| 73 | + |
| 74 | + // Cache should still have the original unmodified data |
| 75 | + cached, ok := db.entryCache.Get(height) |
| 76 | + require.True(t, ok) |
| 77 | + require.Equal(t, block, cached) |
| 78 | + |
| 79 | + // Second Get should also return original data |
| 80 | + data, err = db.Get(height) |
| 81 | + require.NoError(t, err) |
| 82 | + require.Equal(t, block, data) |
| 83 | +} |
| 84 | + |
| 85 | +func TestCachePutOverridesSameHeight(t *testing.T) { |
| 86 | + db, _ := newTestDatabase(t, DefaultConfig()) |
| 87 | + height := uint64(60) |
| 88 | + b1 := randomBlock(t) |
| 89 | + require.NoError(t, db.Put(height, b1)) |
| 90 | + |
| 91 | + // Verify first block is in cache |
| 92 | + cached, ok := db.entryCache.Get(height) |
| 93 | + require.True(t, ok) |
| 94 | + require.Equal(t, b1, cached) |
| 95 | + |
| 96 | + // Put second block at same height and verify it overrides the first one |
| 97 | + b2 := randomBlock(t) |
| 98 | + require.NoError(t, db.Put(height, b2)) |
| 99 | + cached, ok = db.entryCache.Get(height) |
| 100 | + require.True(t, ok) |
| 101 | + require.Equal(t, b2, cached) |
| 102 | + require.NotEqual(t, b1, cached) |
| 103 | + |
| 104 | + // Get should also return the new block |
| 105 | + data, err := db.Get(height) |
| 106 | + require.NoError(t, err) |
| 107 | + require.Equal(t, b2, data) |
| 108 | +} |
0 commit comments