|
| 1 | +package prefix_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "cmp" |
| 5 | + cryptorand "crypto/rand" |
| 6 | + "iter" |
| 7 | + "maps" |
| 8 | + "math/rand/v2" |
| 9 | + "slices" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/NethermindEth/juno/core/felt" |
| 13 | + "github.com/NethermindEth/juno/db" |
| 14 | + "github.com/NethermindEth/juno/db/memory" |
| 15 | + "github.com/NethermindEth/juno/db/typed" |
| 16 | + "github.com/NethermindEth/juno/db/typed/key" |
| 17 | + "github.com/NethermindEth/juno/db/typed/prefix" |
| 18 | + "github.com/NethermindEth/juno/db/typed/value" |
| 19 | + "github.com/stretchr/testify/require" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + layerKeyCount = 50 |
| 24 | + testPerLayer = 5 |
| 25 | +) |
| 26 | + |
| 27 | +type testKey struct { |
| 28 | + number uint64 |
| 29 | + hash felt.Felt |
| 30 | + slot []byte |
| 31 | +} |
| 32 | + |
| 33 | +type testEntry struct { |
| 34 | + key testKey |
| 35 | + value felt.Felt |
| 36 | +} |
| 37 | + |
| 38 | +func (k testKey) Marshal() []byte { |
| 39 | + return slices.Concat( |
| 40 | + key.Uint64.Marshal(k.number), |
| 41 | + key.Felt.Marshal(&k.hash), |
| 42 | + key.Bytes.Marshal(k.slot), |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +var bucket = prefix.NewPrefixedBucket( |
| 47 | + typed.NewBucket( |
| 48 | + db.Bucket(0), |
| 49 | + key.Marshal[testKey](), |
| 50 | + value.Felt, |
| 51 | + ), |
| 52 | + prefix.Prefix( |
| 53 | + key.Uint64, |
| 54 | + prefix.Prefix( |
| 55 | + key.Felt, |
| 56 | + prefix.Prefix( |
| 57 | + key.Bytes, |
| 58 | + prefix.End[felt.Felt](), |
| 59 | + ), |
| 60 | + ), |
| 61 | + ), |
| 62 | +) |
| 63 | + |
| 64 | +func extractExpectedEntries( |
| 65 | + entries map[uint64]map[felt.Felt]map[string]testEntry, |
| 66 | + filterBlockNumber *uint64, |
| 67 | + filterHash *felt.Felt, |
| 68 | + filterSlot *string, |
| 69 | +) []testEntry { |
| 70 | + res := make([]testEntry, 0) |
| 71 | + for blockNumber := range filter(filterBlockNumber, cmp.Compare, entries) { |
| 72 | + for hash := range filter(filterHash, compareFelt, entries[blockNumber]) { |
| 73 | + for slot := range filter(filterSlot, cmp.Compare, entries[blockNumber][hash]) { |
| 74 | + res = append(res, entries[blockNumber][hash][slot]) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + return res |
| 79 | +} |
| 80 | + |
| 81 | +func compareFelt(a, b felt.Felt) int { |
| 82 | + return a.Cmp(&b) |
| 83 | +} |
| 84 | + |
| 85 | +func filter[K comparable, V any](filter *K, cmp func(K, K) int, entries map[K]V) iter.Seq[K] { |
| 86 | + if filter != nil { |
| 87 | + return func(yield func(K) bool) { |
| 88 | + yield(*filter) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return func(yield func(K) bool) { |
| 93 | + for _, key := range slices.SortedFunc(maps.Keys(entries), cmp) { |
| 94 | + if !yield(key) { |
| 95 | + return |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func randomEntry[K comparable, V any](t *testing.T, entries map[K]V) (K, V) { |
| 102 | + t.Helper() |
| 103 | + for key, value := range entries { |
| 104 | + return key, value |
| 105 | + } |
| 106 | + require.FailNow(t, "no entries") |
| 107 | + return *new(K), *new(V) |
| 108 | +} |
| 109 | + |
| 110 | +func validateResult( |
| 111 | + t *testing.T, |
| 112 | + expected []testEntry, |
| 113 | + actual iter.Seq2[prefix.Entry[felt.Felt], error], |
| 114 | +) { |
| 115 | + t.Helper() |
| 116 | + count := 0 |
| 117 | + for entry, err := range actual { |
| 118 | + require.NoError(t, err) |
| 119 | + require.Greater(t, len(expected), count) |
| 120 | + require.Equal(t, bucket.Key(expected[count].key.Marshal()), entry.Key) |
| 121 | + require.Equal(t, expected[count].value, entry.Value) |
| 122 | + count++ |
| 123 | + } |
| 124 | + require.Equal(t, len(expected), count) |
| 125 | +} |
| 126 | + |
| 127 | +func TestPrefixedBucket(t *testing.T) { |
| 128 | + database := memory.New() |
| 129 | + content := make(map[uint64]map[felt.Felt]map[string]testEntry) |
| 130 | + |
| 131 | + t.Run("Populate database", func(t *testing.T) { |
| 132 | + for range layerKeyCount { |
| 133 | + blockNumber := rand.Uint64() |
| 134 | + content[blockNumber] = make(map[felt.Felt]map[string]testEntry) |
| 135 | + |
| 136 | + for range layerKeyCount { |
| 137 | + hash := felt.Random[felt.Felt]() |
| 138 | + content[blockNumber][hash] = make(map[string]testEntry) |
| 139 | + |
| 140 | + for range layerKeyCount { |
| 141 | + slot := cryptorand.Text() |
| 142 | + value := felt.Random[felt.Felt]() |
| 143 | + |
| 144 | + key := testKey{ |
| 145 | + number: blockNumber, |
| 146 | + hash: hash, |
| 147 | + slot: []byte(slot), |
| 148 | + } |
| 149 | + entry := testEntry{ |
| 150 | + key: key, |
| 151 | + value: value, |
| 152 | + } |
| 153 | + |
| 154 | + content[blockNumber][hash][slot] = entry |
| 155 | + require.NoError(t, bucket.Put(database, key, &value)) |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + }) |
| 160 | + |
| 161 | + t.Run("Full scan", func(t *testing.T) { |
| 162 | + validateResult( |
| 163 | + t, |
| 164 | + extractExpectedEntries(content, nil, nil, nil), |
| 165 | + bucket.Prefix().Scan(database), |
| 166 | + ) |
| 167 | + }) |
| 168 | + |
| 169 | + t.Run("1 layer scan", func(t *testing.T) { |
| 170 | + for range testPerLayer { |
| 171 | + blockNumber, _ := randomEntry(t, content) |
| 172 | + validateResult( |
| 173 | + t, |
| 174 | + extractExpectedEntries(content, &blockNumber, nil, nil), |
| 175 | + bucket.Prefix().Add(blockNumber).Scan(database), |
| 176 | + ) |
| 177 | + } |
| 178 | + }) |
| 179 | + |
| 180 | + t.Run("2 layer scan", func(t *testing.T) { |
| 181 | + for range testPerLayer * testPerLayer { |
| 182 | + blockNumber, map1 := randomEntry(t, content) |
| 183 | + hash, _ := randomEntry(t, map1) |
| 184 | + validateResult( |
| 185 | + t, |
| 186 | + extractExpectedEntries(content, &blockNumber, &hash, nil), |
| 187 | + bucket.Prefix().Add(blockNumber).Add(&hash).Scan(database), |
| 188 | + ) |
| 189 | + } |
| 190 | + }) |
| 191 | + |
| 192 | + t.Run("3 layer scan", func(t *testing.T) { |
| 193 | + for range testPerLayer * testPerLayer * testPerLayer { |
| 194 | + blockNumber, map1 := randomEntry(t, content) |
| 195 | + hash, map2 := randomEntry(t, map1) |
| 196 | + slot, _ := randomEntry(t, map2) |
| 197 | + validateResult( |
| 198 | + t, |
| 199 | + extractExpectedEntries(content, &blockNumber, &hash, &slot), |
| 200 | + bucket.Prefix().Add(blockNumber).Add(&hash).Add([]byte(slot)).Scan(database), |
| 201 | + ) |
| 202 | + } |
| 203 | + }) |
| 204 | +} |
0 commit comments