Skip to content

Commit

Permalink
use storeupgrade throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed Aug 12, 2024
1 parent 810a542 commit ffd238c
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 56 deletions.
5 changes: 3 additions & 2 deletions server/mock/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mock
import (
"io"

corestore "cosmossdk.io/core/store"
dbm "github.com/cosmos/cosmos-db"
protoio "github.com/cosmos/gogoproto/io"

Expand Down Expand Up @@ -98,11 +99,11 @@ func (ms multiStore) LoadLatestVersion() error {
return nil
}

func (ms multiStore) LoadLatestVersionAndUpgrade(upgrades *storetypes.StoreUpgrades) error {
func (ms multiStore) LoadLatestVersionAndUpgrade(upgrades *corestore.StoreUpgrades) error {
return nil
}

func (ms multiStore) LoadVersionAndUpgrade(ver int64, upgrades *storetypes.StoreUpgrades) error {
func (ms multiStore) LoadVersionAndUpgrade(ver int64, upgrades *corestore.StoreUpgrades) error {
panic("not implemented")
}

Expand Down
3 changes: 3 additions & 0 deletions store/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module cosmossdk.io/store

go 1.22.2

replace cosmossdk.io/core => ../core

require (
cosmossdk.io/core v0.0.0-00010101000000-000000000000
cosmossdk.io/errors v1.0.1
cosmossdk.io/log v1.4.0
cosmossdk.io/math v1.3.0
Expand Down
30 changes: 5 additions & 25 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"sync"

corestore "cosmossdk.io/core/store"
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
dbm "github.com/cosmos/cosmos-db"
protoio "github.com/cosmos/gogoproto/io"
Expand Down Expand Up @@ -178,13 +179,13 @@ func (rs *Store) StoreKeysByName() map[string]types.StoreKey {
}

// LoadLatestVersionAndUpgrade implements CommitMultiStore
func (rs *Store) LoadLatestVersionAndUpgrade(upgrades *types.StoreUpgrades) error {
func (rs *Store) LoadLatestVersionAndUpgrade(upgrades *corestore.StoreUpgrades) error {
ver := GetLatestVersion(rs.db)
return rs.loadVersion(ver, upgrades)
}

// LoadVersionAndUpgrade allows us to rename substores while loading an older version
func (rs *Store) LoadVersionAndUpgrade(ver int64, upgrades *types.StoreUpgrades) error {
func (rs *Store) LoadVersionAndUpgrade(ver int64, upgrades *corestore.StoreUpgrades) error {
return rs.loadVersion(ver, upgrades)
}

Expand All @@ -199,7 +200,7 @@ func (rs *Store) LoadVersion(ver int64) error {
return rs.loadVersion(ver, nil)
}

func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error {
func (rs *Store) loadVersion(ver int64, upgrades *corestore.StoreUpgrades) error {
infos := make(map[string]types.StoreInfo)

rs.logger.Debug("loadVersion", "ver", ver)
Expand Down Expand Up @@ -243,7 +244,7 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error {
rs.logger.Debug("loadVersion commitID", "key", key, "ver", ver, "hash", fmt.Sprintf("%x", commitID.Hash))

// If it has been added, set the initial version
if upgrades.IsAdded(key.Name()) || upgrades.RenamedFrom(key.Name()) != "" {
if upgrades.IsAdded(key.Name()) {
storeParams.initialVersion = uint64(ver) + 1
} else if commitID.Version != ver && storeParams.typ == types.StoreTypeIAVL {
return fmt.Errorf("version of store %q mismatch root store's version; expected %d got %d; new stores should be added using StoreUpgrades", key.Name(), ver, commitID.Version)
Expand All @@ -262,27 +263,6 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error {
return errorsmod.Wrapf(err, "failed to delete store %s", key.Name())
}
rs.removalMap[key] = true
} else if oldName := upgrades.RenamedFrom(key.Name()); oldName != "" {
// handle renames specially
// make an unregistered key to satisfy loadCommitStore params
oldKey := types.NewKVStoreKey(oldName)
oldParams := newStoreParams(oldKey, storeParams.db, storeParams.typ, 0)

// load from the old name
oldStore, err := rs.loadCommitStoreFromParams(oldKey, rs.getCommitID(infos, oldName), oldParams)
if err != nil {
return errorsmod.Wrapf(err, "failed to load old store %s", oldName)
}

// move all data
if err := moveKVStoreData(oldStore.(types.KVStore), store.(types.KVStore)); err != nil {
return errorsmod.Wrapf(err, "failed to move store %s -> %s", oldName, key.Name())
}

// add the old key so its deletion is committed
newStores[oldKey] = oldStore
// this will ensure it's not perpetually stored in commitInfo
rs.removalMap[oldKey] = true
}
}

Expand Down
15 changes: 6 additions & 9 deletions store/rootmulti/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

corestore "cosmossdk.io/core/store"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -103,7 +104,7 @@ func TestCacheMultiStoreWithVersion(t *testing.T) {
key4, key5 := types.NewKVStoreKey("store4"), types.NewKVStoreKey("store5")
ms.MountStoreWithDB(key4, types.StoreTypeIAVL, nil)
ms.MountStoreWithDB(key5, types.StoreTypeIAVL, nil)
err = ms.LoadLatestVersionAndUpgrade(&types.StoreUpgrades{Added: []string{"store4", "store5"}})
err = ms.LoadLatestVersionAndUpgrade(&corestore.StoreUpgrades{Added: []string{"store4", "store5"}})
require.NoError(t, err)
ms.Commit()

Expand Down Expand Up @@ -684,7 +685,7 @@ func TestUnevenStoresHeightCheck(t *testing.T) {
require.Error(t, err)

// now, let's load with upgrades...
upgrades := &types.StoreUpgrades{
upgrades := &corestore.StoreUpgrades{
Added: []string{"store4"},
}
err = store.LoadLatestVersionAndUpgrade(upgrades)
Expand Down Expand Up @@ -843,7 +844,7 @@ func newMultiStoreWithMounts(db dbm.DB, pruningOpts pruningtypes.PruningOptions)
return store
}

func newMultiStoreWithModifiedMounts(db dbm.DB, pruningOpts pruningtypes.PruningOptions) (*Store, *types.StoreUpgrades) {
func newMultiStoreWithModifiedMounts(db dbm.DB, pruningOpts pruningtypes.PruningOptions) (*Store, *corestore.StoreUpgrades) {
store := NewStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
store.SetPruning(pruningOpts)

Expand All @@ -852,12 +853,8 @@ func newMultiStoreWithModifiedMounts(db dbm.DB, pruningOpts pruningtypes.Pruning
store.MountStoreWithDB(types.NewKVStoreKey("store3"), types.StoreTypeIAVL, nil)
store.MountStoreWithDB(types.NewKVStoreKey("store4"), types.StoreTypeIAVL, nil)

upgrades := &types.StoreUpgrades{
Added: []string{"store4"},
Renamed: []types.StoreRename{{
OldKey: "store2",
NewKey: "restore2",
}},
upgrades := &corestore.StoreUpgrades{
Added: []string{"store4"},
Deleted: []string{"store3"},
}

Expand Down
8 changes: 6 additions & 2 deletions store/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
crypto "github.com/cometbft/cometbft/api/cometbft/crypto/v1"
dbm "github.com/cosmos/cosmos-db"

corestore "cosmossdk.io/core/store"

"cosmossdk.io/store/metrics"
pruningtypes "cosmossdk.io/store/pruning/types"
snapshottypes "cosmossdk.io/store/snapshots/types"
Expand Down Expand Up @@ -76,6 +78,7 @@ type ResponseQuery struct {
// MultiStore

// StoreUpgrades defines a series of transformations to apply the multistore db upon load
// Deprecated: Use store.StoreUpgrades instead.
type StoreUpgrades struct {
Added []string `json:"added"`
Renamed []StoreRename `json:"renamed"`
Expand All @@ -85,6 +88,7 @@ type StoreUpgrades struct {
// StoreRename defines a name change of a sub-store.
// All data previously under a PrefixStore with OldKey will be copied
// to a PrefixStore with NewKey, then deleted from OldKey store.
// Deprecated: Use store.StoreUpgrades instead. Store renaming was removed in core v1.0.0
type StoreRename struct {
OldKey string `json:"old_key"`
NewKey string `json:"new_key"`
Expand Down Expand Up @@ -193,12 +197,12 @@ type CommitMultiStore interface {
// LoadLatestVersionAndUpgrade will load the latest version, but also
// rename/delete/create sub-store keys, before registering all the keys
// in order to handle breaking formats in migrations
LoadLatestVersionAndUpgrade(upgrades *StoreUpgrades) error
LoadLatestVersionAndUpgrade(upgrades *corestore.StoreUpgrades) error

// LoadVersionAndUpgrade will load the named version, but also
// rename/delete/create sub-store keys, before registering all the keys
// in order to handle breaking formats in migrations
LoadVersionAndUpgrade(ver int64, upgrades *StoreUpgrades) error
LoadVersionAndUpgrade(ver int64, upgrades *corestore.StoreUpgrades) error

// LoadVersion load a specific persisted version. When you load an old version, or when
// the last commit attempt didn't complete, the next commit after loading
Expand Down
5 changes: 3 additions & 2 deletions x/upgrade/types/storeloader.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package types

import (
corestore "cosmossdk.io/core/store"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/baseapp"
)

// UpgradeStoreLoader is used to prepare baseapp with a fixed StoreLoader
// pattern. This is useful for custom upgrade loading logic.
func UpgradeStoreLoader(upgradeHeight int64, storeUpgrades *storetypes.StoreUpgrades) baseapp.StoreLoader {
func UpgradeStoreLoader(upgradeHeight int64, storeUpgrades *corestore.StoreUpgrades) baseapp.StoreLoader {
return func(ms storetypes.CommitMultiStore) error {
if upgradeHeight == ms.LastCommitID().Version+1 {
// Check if the current commit version and upgrade height matches
if len(storeUpgrades.Renamed) > 0 || len(storeUpgrades.Deleted) > 0 || len(storeUpgrades.Added) > 0 {
if len(storeUpgrades.Deleted) > 0 || len(storeUpgrades.Added) > 0 {
return ms.LoadLatestVersionAndUpgrade(storeUpgrades)
}
}
Expand Down
16 changes: 0 additions & 16 deletions x/upgrade/types/storeloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
)

func useUpgradeLoader(height int64, upgrades *storetypes.StoreUpgrades) func(*baseapp.BaseApp) {
return func(app *baseapp.BaseApp) {
app.SetStoreLoader(UpgradeStoreLoader(height, upgrades))
}
}

func initStore(t *testing.T, db dbm.DB, storeKey string, k, v []byte) {
t.Helper()
rs := rootmulti.NewStore(db, coretesting.NewNopLogger(), metrics.NewNoOpMetrics())
Expand Down Expand Up @@ -93,16 +87,6 @@ func TestSetLoader(t *testing.T) {
origStoreKey: "foo",
loadStoreKey: "foo",
},
"rename with inline opts": {
setLoader: useUpgradeLoader(upgradeHeight, &storetypes.StoreUpgrades{
Renamed: []storetypes.StoreRename{{
OldKey: "foo",
NewKey: "bar",
}},
}),
origStoreKey: "foo",
loadStoreKey: "bar",
},
}

k := []byte("key")
Expand Down

0 comments on commit ffd238c

Please sign in to comment.