|
| 1 | +package db_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/NethermindEth/juno/core/felt" |
| 7 | + "github.com/NethermindEth/juno/db" |
| 8 | + "github.com/NethermindEth/juno/db/memory" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestHistoryLastUpdateBlock(t *testing.T) { |
| 14 | + addr := felt.NewFromUint64[felt.Felt](1) |
| 15 | + loc := felt.NewFromUint64[felt.Felt](2) |
| 16 | + prefix := db.ContractStorageHistoryKey(addr, loc) |
| 17 | + |
| 18 | + putAt := func(t *testing.T, database db.KeyValueStore, blockNum uint64) { |
| 19 | + t.Helper() |
| 20 | + key := db.ContractStorageHistoryAtBlockKey(addr, loc, blockNum) |
| 21 | + require.NoError(t, database.Put(key, []byte("value"))) |
| 22 | + } |
| 23 | + |
| 24 | + t.Run("empty db returns not found", func(t *testing.T) { |
| 25 | + database := memory.New() |
| 26 | + defer database.Close() |
| 27 | + |
| 28 | + blockNum, found, err := db.HistoryLastUpdateBlock(database, prefix, 10) |
| 29 | + require.NoError(t, err) |
| 30 | + assert.False(t, found) |
| 31 | + assert.Equal(t, uint64(0), blockNum) |
| 32 | + }) |
| 33 | + |
| 34 | + t.Run("exact match returns upToBlock", func(t *testing.T) { |
| 35 | + database := memory.New() |
| 36 | + defer database.Close() |
| 37 | + |
| 38 | + putAt(t, database, 5) |
| 39 | + |
| 40 | + blockNum, found, err := db.HistoryLastUpdateBlock(database, prefix, 5) |
| 41 | + require.NoError(t, err) |
| 42 | + assert.True(t, found) |
| 43 | + assert.Equal(t, uint64(5), blockNum) |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("returns latest block before upToBlock", func(t *testing.T) { |
| 47 | + database := memory.New() |
| 48 | + defer database.Close() |
| 49 | + |
| 50 | + putAt(t, database, 3) |
| 51 | + putAt(t, database, 7) |
| 52 | + putAt(t, database, 10) |
| 53 | + |
| 54 | + blockNum, found, err := db.HistoryLastUpdateBlock(database, prefix, 8) |
| 55 | + require.NoError(t, err) |
| 56 | + assert.True(t, found) |
| 57 | + assert.Equal(t, uint64(7), blockNum) |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("all entries are after upToBlock returns not found", func(t *testing.T) { |
| 61 | + database := memory.New() |
| 62 | + defer database.Close() |
| 63 | + |
| 64 | + putAt(t, database, 5) |
| 65 | + putAt(t, database, 10) |
| 66 | + |
| 67 | + blockNum, found, err := db.HistoryLastUpdateBlock(database, prefix, 3) |
| 68 | + require.NoError(t, err) |
| 69 | + assert.False(t, found) |
| 70 | + assert.Equal(t, uint64(0), blockNum) |
| 71 | + }) |
| 72 | + |
| 73 | + t.Run("multiple entries with exact match at upToBlock", func(t *testing.T) { |
| 74 | + database := memory.New() |
| 75 | + defer database.Close() |
| 76 | + |
| 77 | + putAt(t, database, 1) |
| 78 | + putAt(t, database, 5) |
| 79 | + putAt(t, database, 9) |
| 80 | + |
| 81 | + blockNum, found, err := db.HistoryLastUpdateBlock(database, prefix, 9) |
| 82 | + require.NoError(t, err) |
| 83 | + assert.True(t, found) |
| 84 | + assert.Equal(t, uint64(9), blockNum) |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("different contract prefix does not affect result", func(t *testing.T) { |
| 88 | + database := memory.New() |
| 89 | + defer database.Close() |
| 90 | + |
| 91 | + otherAddr := new(felt.Felt).SetUint64(99) |
| 92 | + otherPrefix := db.ContractStorageHistoryKey(otherAddr, loc) |
| 93 | + |
| 94 | + putAt(t, database, 5) |
| 95 | + |
| 96 | + // Write entries under a different prefix |
| 97 | + otherKey := db.ContractStorageHistoryAtBlockKey(otherAddr, loc, 3) |
| 98 | + require.NoError(t, database.Put(otherKey, []byte("other"))) |
| 99 | + |
| 100 | + blockNum, found, err := db.HistoryLastUpdateBlock(database, prefix, 10) |
| 101 | + require.NoError(t, err) |
| 102 | + assert.True(t, found) |
| 103 | + assert.Equal(t, uint64(5), blockNum) |
| 104 | + |
| 105 | + // Query the other prefix |
| 106 | + blockNum, found, err = db.HistoryLastUpdateBlock(database, otherPrefix, 10) |
| 107 | + require.NoError(t, err) |
| 108 | + assert.True(t, found) |
| 109 | + assert.Equal(t, uint64(3), blockNum) |
| 110 | + }) |
| 111 | +} |
0 commit comments