|
| 1 | +package flatkv |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + |
| 8 | + "github.com/sei-protocol/sei-chain/sei-db/common/keys" |
| 9 | + "github.com/sei-protocol/sei-chain/sei-db/proto" |
| 10 | + "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv/ktype" |
| 11 | + "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv/vtype" |
| 12 | +) |
| 13 | + |
| 14 | +// rawRow is a physical key and the bytes a block is expected to leave under it. |
| 15 | +type rawRow struct { |
| 16 | + key []byte |
| 17 | + value []byte |
| 18 | +} |
| 19 | + |
| 20 | +// commitRowInEveryStore commits block 1 with one row in each of FlatKV's stores, and returns the |
| 21 | +// physical key and serialized bytes of each. |
| 22 | +func commitRowInEveryStore(t *testing.T, s *CommitStore) []rawRow { |
| 23 | + t.Helper() |
| 24 | + addr := addrN(1) |
| 25 | + slot := slotN(2) |
| 26 | + balance := balanceN(3) |
| 27 | + codeHash := codeHashN(4) |
| 28 | + bytecode := []byte{0x60, 0x00} |
| 29 | + evmMisc := evmMiscKey(0x05) |
| 30 | + bankKey := []byte{0x06} |
| 31 | + |
| 32 | + require.NoError(t, s.CommitStateChanges(1, []*proto.NamedChangeSet{ |
| 33 | + namedCS( |
| 34 | + noncePair(addr, 7), |
| 35 | + balancePair(addr, balance), |
| 36 | + codeHashPair(addr, codeHash), |
| 37 | + storagePair(addr, slot, []byte{0x08}), |
| 38 | + codePair(addr, bytecode), |
| 39 | + &proto.KVPair{Key: evmMisc, Value: []byte{0x09}}, |
| 40 | + ), |
| 41 | + {Name: "bank", Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{{Key: bankKey, Value: []byte{0x0a}}}}}, |
| 42 | + })) |
| 43 | + |
| 44 | + account := vtype.NewAccountData().SetBlockHeight(1).SetNonce(7).SetBalance(&balance).SetCodeHash(&codeHash) |
| 45 | + storage, err := vtype.SerializeStorage(1, padLeft32(0x08)) |
| 46 | + require.NoError(t, err) |
| 47 | + return []rawRow{ |
| 48 | + {key: accountPhysKey(addr), value: account.Serialize()}, |
| 49 | + {key: storagePhysKey(addr, slot), value: storage}, |
| 50 | + {key: ktype.EVMPhysicalKey(keys.EVMKeyCode, addr[:]), value: vtype.SerializeCode(1, bytecode)}, |
| 51 | + {key: ktype.ModulePhysicalKey(keys.EVMStoreKey, evmMisc), value: vtype.SerializeMisc(1, []byte{0x09})}, |
| 52 | + {key: ktype.ModulePhysicalKey("bank", bankKey), value: vtype.SerializeMisc(1, []byte{0x0a})}, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// GetRaw answers with the row exactly as stored, metadata included, in every store a key can route to. |
| 57 | +func TestGetRawReturnsStoredRows(t *testing.T) { |
| 58 | + s := setupTestStore(t) |
| 59 | + defer func() { require.NoError(t, s.Close()) }() |
| 60 | + rows := commitRowInEveryStore(t, s) |
| 61 | + |
| 62 | + stateView := s.OpenView() |
| 63 | + defer stateView.Close() |
| 64 | + |
| 65 | + for _, row := range rows { |
| 66 | + value, found := stateView.GetRaw(row.key) |
| 67 | + require.True(t, found, "key %q", row.key) |
| 68 | + require.Equal(t, row.value, value, "key %q", row.key) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// A physical key that holds nothing is reported as not found, whichever store it routes to. |
| 73 | +func TestGetRawMissingKey(t *testing.T) { |
| 74 | + s := setupTestStore(t) |
| 75 | + defer func() { require.NoError(t, s.Close()) }() |
| 76 | + commitRowInEveryStore(t, s) |
| 77 | + |
| 78 | + stateView := s.OpenView() |
| 79 | + defer stateView.Close() |
| 80 | + |
| 81 | + missing := addrN(0x20) |
| 82 | + for _, key := range [][]byte{ |
| 83 | + accountPhysKey(missing), |
| 84 | + storagePhysKey(missing, slotN(1)), |
| 85 | + ktype.EVMPhysicalKey(keys.EVMKeyCode, missing[:]), |
| 86 | + ktype.ModulePhysicalKey(keys.EVMStoreKey, evmMiscKey(0x20)), |
| 87 | + ktype.ModulePhysicalKey("bank", []byte{0x20}), |
| 88 | + } { |
| 89 | + value, found := stateView.GetRaw(key) |
| 90 | + require.False(t, found, "key %q", key) |
| 91 | + require.Nil(t, value, "key %q", key) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// One batch can span every store and include keys that hold nothing; only the found keys come back. |
| 96 | +func TestBatchGetRawMixesStoresAndMissingKeys(t *testing.T) { |
| 97 | + s := setupTestStore(t) |
| 98 | + defer func() { require.NoError(t, s.Close()) }() |
| 99 | + rows := commitRowInEveryStore(t, s) |
| 100 | + |
| 101 | + stateView := s.OpenView() |
| 102 | + defer stateView.Close() |
| 103 | + |
| 104 | + missing := addrN(0x20) |
| 105 | + requested := [][]byte{accountPhysKey(missing), storagePhysKey(missing, slotN(1))} |
| 106 | + expected := map[string][]byte{} |
| 107 | + for _, row := range rows { |
| 108 | + requested = append(requested, row.key) |
| 109 | + expected[string(row.key)] = row.value |
| 110 | + } |
| 111 | + |
| 112 | + require.Equal(t, expected, stateView.BatchGetRaw(requested)) |
| 113 | +} |
| 114 | + |
| 115 | +// An empty batch reads nothing and returns an empty map. |
| 116 | +func TestBatchGetRawEmpty(t *testing.T) { |
| 117 | + s := setupTestStore(t) |
| 118 | + defer func() { require.NoError(t, s.Close()) }() |
| 119 | + commitRowInEveryStore(t, s) |
| 120 | + |
| 121 | + stateView := s.OpenView() |
| 122 | + defer stateView.Close() |
| 123 | + |
| 124 | + require.Empty(t, stateView.BatchGetRaw(nil)) |
| 125 | +} |
| 126 | + |
| 127 | +// Raw reads are pinned to the view's block like every other read: a later block's write is invisible |
| 128 | +// to a view opened before it, and visible to one opened after. |
| 129 | +func TestRawReadsArePinnedToTheViewBlock(t *testing.T) { |
| 130 | + s := setupTestStore(t) |
| 131 | + defer func() { require.NoError(t, s.Close()) }() |
| 132 | + rows := commitRowInEveryStore(t, s) |
| 133 | + accountRow := rows[0] |
| 134 | + |
| 135 | + before := s.OpenView() |
| 136 | + defer before.Close() |
| 137 | + |
| 138 | + addr := addrN(1) |
| 139 | + commitNonce(t, s, 2, addr, 8) |
| 140 | + |
| 141 | + value, found := before.GetRaw(accountRow.key) |
| 142 | + require.True(t, found) |
| 143 | + require.Equal(t, accountRow.value, value) |
| 144 | + require.Equal(t, map[string][]byte{string(accountRow.key): accountRow.value}, |
| 145 | + before.BatchGetRaw([][]byte{accountRow.key})) |
| 146 | + |
| 147 | + after := s.OpenView() |
| 148 | + defer after.Close() |
| 149 | + |
| 150 | + balance := balanceN(3) |
| 151 | + codeHash := codeHashN(4) |
| 152 | + updated := vtype.NewAccountData().SetBlockHeight(2).SetNonce(8).SetBalance(&balance).SetCodeHash(&codeHash) |
| 153 | + value, found = after.GetRaw(accountRow.key) |
| 154 | + require.True(t, found) |
| 155 | + require.Equal(t, updated.Serialize(), value) |
| 156 | +} |
| 157 | + |
| 158 | +// A key with no module separator cannot be routed to any store. |
| 159 | +func TestGetRawRejectsUnroutableKey(t *testing.T) { |
| 160 | + s := setupTestStore(t) |
| 161 | + defer func() { require.NoError(t, s.Close()) }() |
| 162 | + commitRowInEveryStore(t, s) |
| 163 | + |
| 164 | + stateView := s.OpenView() |
| 165 | + defer stateView.Close() |
| 166 | + |
| 167 | + require.Panics(t, func() { stateView.GetRaw([]byte("no-separator")) }) |
| 168 | + require.Panics(t, func() { stateView.BatchGetRaw([][]byte{[]byte("no-separator")}) }) |
| 169 | +} |
| 170 | + |
| 171 | +// A key under the metadata prefix names the store's own bookkeeping, not block state. |
| 172 | +func TestGetRawRejectsMetaKey(t *testing.T) { |
| 173 | + s := setupTestStore(t) |
| 174 | + defer func() { require.NoError(t, s.Close()) }() |
| 175 | + rows := commitRowInEveryStore(t, s) |
| 176 | + |
| 177 | + stateView := s.OpenView() |
| 178 | + defer stateView.Close() |
| 179 | + |
| 180 | + metaKey := []byte(ktype.MetaKeyPrefix + "version") |
| 181 | + require.Panics(t, func() { stateView.GetRaw(metaKey) }) |
| 182 | + require.Panics(t, func() { stateView.BatchGetRaw([][]byte{rows[0].key, metaKey}) }) |
| 183 | +} |
| 184 | + |
| 185 | +// storeViewFor reports a key it cannot serve as an error rather than panicking. |
| 186 | +func TestStoreViewForRejectsMetaKey(t *testing.T) { |
| 187 | + s := setupTestStore(t) |
| 188 | + defer func() { require.NoError(t, s.Close()) }() |
| 189 | + commitRowInEveryStore(t, s) |
| 190 | + |
| 191 | + stateView := s.OpenView() |
| 192 | + defer stateView.Close() |
| 193 | + flatView := stateView.(*flatKVStateView) |
| 194 | + |
| 195 | + for _, key := range [][]byte{[]byte(ktype.MetaKeyPrefix + "version"), []byte("no-separator")} { |
| 196 | + var err error |
| 197 | + require.NotPanics(t, func() { _, err = flatView.storeViewFor(key) }) |
| 198 | + require.Error(t, err, "key %q", key) |
| 199 | + } |
| 200 | +} |
0 commit comments