chore: prep store v1.0.0 (#18318)

Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
Marko
2023-11-01 08:20:56 +01:00
committed by GitHub
co-authored by Julien Robert
parent 1788e32d11
commit 35955817c1
27 changed files with 276 additions and 166 deletions
+14 -4
View File
@@ -33,7 +33,10 @@ func newMultiStoreWithGeneratedData(db dbm.DB, stores uint8, storeKeys uint64) *
multiStore.MountStoreWithDB(key, types.StoreTypeIAVL, nil)
keys = append(keys, key)
}
multiStore.LoadLatestVersion()
err := multiStore.LoadLatestVersion()
if err != nil {
panic(err)
}
for _, key := range keys {
store := multiStore.GetCommitKVStore(key).(*iavl.Store)
@@ -50,7 +53,10 @@ func newMultiStoreWithGeneratedData(db dbm.DB, stores uint8, storeKeys uint64) *
}
multiStore.Commit()
multiStore.LoadLatestVersion()
err = multiStore.LoadLatestVersion()
if err != nil {
panic(err)
}
return multiStore
}
@@ -61,8 +67,9 @@ func newMultiStoreWithMixedMounts(db dbm.DB) *rootmulti.Store {
store.MountStoreWithDB(types.NewKVStoreKey("iavl2"), types.StoreTypeIAVL, nil)
store.MountStoreWithDB(types.NewKVStoreKey("iavl3"), types.StoreTypeIAVL, nil)
store.MountStoreWithDB(types.NewTransientStoreKey("trans1"), types.StoreTypeTransient, nil)
store.LoadLatestVersion()
if err := store.LoadLatestVersion(); err != nil {
panic(err)
}
return store
}
@@ -93,6 +100,7 @@ func newMultiStoreWithMixedMountsAndBasicData(db dbm.DB) *rootmulti.Store {
}
func assertStoresEqual(t *testing.T, expect, actual types.CommitKVStore, msgAndArgs ...interface{}) {
t.Helper()
assert.Equal(t, expect.LastCommitID(), actual.LastCommitID())
expectIter := expect.Iterator(nil, nil)
expectMap := map[string][]byte{}
@@ -226,6 +234,7 @@ func TestMultistoreSnapshotRestore(t *testing.T) {
}
func benchmarkMultistoreSnapshot(b *testing.B, stores uint8, storeKeys uint64) {
b.Helper()
b.Skip("Noisy with slow setup time, please see https://github.com/cosmos/cosmos-sdk/issues/8855.")
b.ReportAllocs()
@@ -261,6 +270,7 @@ func benchmarkMultistoreSnapshot(b *testing.B, stores uint8, storeKeys uint64) {
}
func benchmarkMultistoreSnapshotRestore(b *testing.B, stores uint8, storeKeys uint64) {
b.Helper()
b.Skip("Noisy with slow setup time, please see https://github.com/cosmos/cosmos-sdk/issues/8855.")
b.ReportAllocs()
+26 -12
View File
@@ -314,7 +314,9 @@ func deleteKVStore(kv types.KVStore) error {
keys = append(keys, itr.Key())
itr.Next()
}
itr.Close()
if err := itr.Close(); err != nil {
return err
}
for _, k := range keys {
kv.Delete(k)
@@ -330,17 +332,19 @@ func moveKVStoreData(oldDB, newDB types.KVStore) error {
newDB.Set(itr.Key(), itr.Value())
itr.Next()
}
itr.Close()
if err := itr.Close(); err != nil {
return err
}
// then delete the old store
return deleteKVStore(oldDB)
}
// PruneSnapshotHeight prunes the given height according to the prune strategy.
// If PruneNothing, this is a no-op.
// If other strategy, this height is persisted until the snapshot is operated.
// If the strategy is PruneNothing, this is a no-op.
// For other strategies, this height is persisted until the snapshot is operated.
func (rs *Store) PruneSnapshotHeight(height int64) {
rs.pruningManager.HandleHeightSnapshot(height)
rs.pruningManager.HandleSnapshotHeight(height)
}
// SetInterBlockCache sets the Store's internal inter-block (persistent) cache.
@@ -646,15 +650,15 @@ func (rs *Store) GetKVStore(key types.StoreKey) types.KVStore {
func (rs *Store) handlePruning(version int64) error {
pruneHeight := rs.pruningManager.GetPruningHeight(version)
rs.logger.Info("prune start", "height", version)
defer rs.logger.Info("prune end", "height", version)
rs.logger.Debug("prune start", "height", version)
defer rs.logger.Debug("prune end", "height", version)
return rs.PruneStores(pruneHeight)
}
// PruneStores prunes all history upto the specific height of the multi store.
func (rs *Store) PruneStores(pruningHeight int64) (err error) {
if pruningHeight <= 0 {
rs.logger.Debug("pruning skipped, height is smaller than 0")
rs.logger.Debug("pruning skipped, height is less than or equal to 0")
return nil
}
@@ -676,9 +680,11 @@ func (rs *Store) PruneStores(pruningHeight int64) (err error) {
continue
}
if errors.Is(err, iavltree.ErrVersionDoesNotExist) && err != nil {
if errors.Is(err, iavltree.ErrVersionDoesNotExist) {
return err
}
rs.logger.Error("failed to prune store", "key", key, "err", err)
}
return nil
}
@@ -1104,7 +1110,9 @@ func (rs *Store) GetCommitInfo(ver int64) (*types.CommitInfo, error) {
func (rs *Store) flushMetadata(db dbm.DB, version int64, cInfo *types.CommitInfo) {
rs.logger.Debug("flushing metadata", "height", version)
batch := db.NewBatch()
defer batch.Close()
defer func() {
_ = batch.Close()
}()
if cInfo != nil {
flushCommitInfo(batch, version, cInfo)
@@ -1203,7 +1211,10 @@ func flushCommitInfo(batch dbm.Batch, version int64, cInfo *types.CommitInfo) {
}
cInfoKey := fmt.Sprintf(commitInfoKeyFmt, version)
batch.Set([]byte(cInfoKey), bz)
err = batch.Set([]byte(cInfoKey), bz)
if err != nil {
panic(err)
}
}
func flushLatestVersion(batch dbm.Batch, version int64) {
@@ -1212,5 +1223,8 @@ func flushLatestVersion(batch dbm.Batch, version int64) {
panic(err)
}
batch.Set([]byte(latestVersionKey), bz)
err = batch.Set([]byte(latestVersionKey), bz)
if err != nil {
panic(err)
}
}
+16 -11
View File
@@ -648,7 +648,8 @@ func TestSetInitialVersion(t *testing.T) {
require.NoError(t, multi.LoadLatestVersion())
multi.SetInitialVersion(5)
err := multi.SetInitialVersion(5)
require.NoError(t, err)
require.Equal(t, int64(5), multi.initialVersion)
multi.Commit()
@@ -820,26 +821,27 @@ func unmountStore(rootStore *Store, storeKeyName string) {
}
func checkStore(t *testing.T, store *Store, expect, got types.CommitID) {
t.Helper()
require.Equal(t, expect, got)
require.Equal(t, expect, store.LastCommitID())
}
func checkContains(t testing.TB, info []types.StoreInfo, wanted []string) {
t.Helper()
func checkContains(tb testing.TB, info []types.StoreInfo, wanted []string) {
tb.Helper()
for _, want := range wanted {
checkHas(t, info, want)
checkHas(tb, info, want)
}
}
func checkHas(t testing.TB, info []types.StoreInfo, want string) {
t.Helper()
func checkHas(tb testing.TB, info []types.StoreInfo, want string) {
tb.Helper()
for _, i := range info {
if i.Name == want {
return
}
}
t.Fatalf("storeInfo doesn't contain %s", want)
tb.Fatalf("storeInfo doesn't contain %s", want)
}
func getExpectedCommitID(store *Store, ver int64) types.CommitID {
@@ -915,13 +917,15 @@ func (stub *commitKVStoreStub) Commit() types.CommitID {
return commitID
}
func prepareStoreMap() map[types.StoreKey]types.CommitKVStore {
func prepareStoreMap() (map[types.StoreKey]types.CommitKVStore, error) {
var db dbm.DB = dbm.NewMemDB()
store := NewStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
store.MountStoreWithDB(types.NewKVStoreKey("iavl1"), types.StoreTypeIAVL, nil)
store.MountStoreWithDB(types.NewKVStoreKey("iavl2"), types.StoreTypeIAVL, nil)
store.MountStoreWithDB(types.NewTransientStoreKey("trans1"), types.StoreTypeTransient, nil)
store.LoadLatestVersion()
if err := store.LoadLatestVersion(); err != nil {
return nil, err
}
return map[types.StoreKey]types.CommitKVStore{
testStoreKey1: &commitKVStoreStub{
CommitKVStore: store.GetStoreByName("iavl1").(types.CommitKVStore),
@@ -932,7 +936,7 @@ func prepareStoreMap() map[types.StoreKey]types.CommitKVStore {
testStoreKey3: &commitKVStoreStub{
CommitKVStore: store.GetStoreByName("trans1").(types.CommitKVStore),
},
}
}, nil
}
func TestCommitStores(t *testing.T) {
@@ -959,7 +963,8 @@ func TestCommitStores(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
storeMap := prepareStoreMap()
storeMap, err := prepareStoreMap()
require.NoError(t, err)
store := storeMap[testStoreKey1].(*commitKVStoreStub)
for i := tc.committed; i > 0; i-- {
store.Commit()