Skip to content

Commit

Permalink
refactor(gov): migrate gov module to v8 (#735)
Browse files Browse the repository at this point in the history
Co-authored-by: fx0x55 <80245546+fx0x55@users.noreply.github.com>
  • Loading branch information
zakir-code and fx0x55 authored Oct 12, 2024
1 parent 8c2c51a commit 09bceb4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 14 deletions.
26 changes: 21 additions & 5 deletions app/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
dbm "github.com/cosmos/cosmos-db"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -22,6 +24,8 @@ import (
nextversion "github.com/functionx/fx-core/v8/app/upgrades/v8"
"github.com/functionx/fx-core/v8/testutil/helpers"
fxtypes "github.com/functionx/fx-core/v8/types"
fxgovv8 "github.com/functionx/fx-core/v8/x/gov/migrations/v8"
fxgovtypes "github.com/functionx/fx-core/v8/x/gov/types"
fxstakingv8 "github.com/functionx/fx-core/v8/x/staking/migrations/v8"
)

Expand Down Expand Up @@ -91,14 +95,26 @@ func newContext(t *testing.T, myApp *app.App, chainId string, deliveState bool)

func checkAppUpgrade(t *testing.T, ctx sdk.Context, myApp *app.App) {
checkStakingMigrationDelete(t, ctx, myApp)

checkGovCustomParams(t, ctx, myApp)
}

func checkGovCustomParams(t *testing.T, ctx sdk.Context, myApp *app.App) {
egfCustomParams, found := myApp.GovKeeper.GetCustomParams(ctx, sdk.MsgTypeURL(&distributiontypes.MsgCommunityPoolSpend{}))
require.True(t, found)
expectEGFParams := fxgovtypes.NewCustomParams(fxgovtypes.EGFCustomParamDepositRatio.String(), fxgovtypes.DefaultEGFCustomParamVotingPeriod, fxgovtypes.DefaultCustomParamQuorum40.String())
assert.Equal(t, expectEGFParams, egfCustomParams)

checkKeysIsDelete(t, ctx.KVStore(myApp.GetKey(govtypes.StoreKey)), fxgovv8.GetRemovedStoreKeys())
}

func checkStakingMigrationDelete(t *testing.T, ctx sdk.Context, myApp *app.App) {
storeKey := myApp.GetKey(stakingtypes.StoreKey)
kvStore := ctx.KVStore(storeKey)
removeKeys := fxstakingv8.GetRemovedValidatorStoreKeys()
require.Greater(t, len(removeKeys), 0)
for _, removeKey := range removeKeys {
checkKeysIsDelete(t, ctx.KVStore(myApp.GetKey(stakingtypes.StoreKey)), fxstakingv8.GetRemovedStoreKeys())
}

func checkKeysIsDelete(t *testing.T, kvStore storetypes.KVStore, keys [][]byte) {
require.Greater(t, len(keys), 0)
for _, removeKey := range keys {
iterator := storetypes.KVStorePrefixIterator(kvStore, removeKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down
16 changes: 16 additions & 0 deletions app/upgrades/v8/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package v8
import (
"context"

storetypes "cosmossdk.io/store/types"
upgradetypes "cosmossdk.io/x/upgrade/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/functionx/fx-core/v8/app/keepers"
"github.com/functionx/fx-core/v8/x/gov/keeper"
fxgovv8 "github.com/functionx/fx-core/v8/x/gov/migrations/v8"
fxstakingv8 "github.com/functionx/fx-core/v8/x/staking/migrations/v8"
)

Expand All @@ -24,8 +28,20 @@ func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator,

fxstakingv8.DeleteMigrationValidatorStore(cacheCtx, app.GetKey(stakingtypes.StoreKey))

if err = migrationGovCustomParam(cacheCtx, app.GovKeeper, app.GetKey(govtypes.StoreKey)); err != nil {
return fromVM, err
}

commit()
cacheCtx.Logger().Info("upgrade complete", "module", "upgrade")
return toVM, nil
}
}

func migrationGovCustomParam(ctx sdk.Context, keeper *keeper.Keeper, storeKey *storetypes.KVStoreKey) error {
// 1. delete fxParams key
fxgovv8.DeleteOldParamsStore(ctx, storeKey)

// 2. init custom params
return keeper.InitCustomParams(ctx)
}
31 changes: 31 additions & 0 deletions x/gov/migrations/v8/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package v8

import (
storetypes "cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

var (
// Deprecated: do not use, remove in v8
FxBaseParamsKeyPrefix = []byte("0x90")
// Deprecated: do not use, remove in v8
FxEGFParamsKey = []byte("0x91")
)

func GetRemovedStoreKeys() [][]byte {
return [][]byte{FxBaseParamsKeyPrefix, FxEGFParamsKey}
}

func DeleteOldParamsStore(
ctx sdk.Context,
storeKey storetypes.StoreKey,
) {
store := ctx.KVStore(storeKey)
removeKeys := GetRemovedStoreKeys()
for _, key := range removeKeys {
iterator := storetypes.KVStorePrefixIterator(store, key)
for ; iterator.Valid(); iterator.Next() {
store.Delete(iterator.Key())
}
}
}
7 changes: 0 additions & 7 deletions x/gov/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ var (
)

var (
// todo delete this store
// FxBaseParamsKeyPrefix is the key to query all base params
FxBaseParamsKeyPrefix = []byte("0x90")
// todo delete this store
// FxEGFParamsKey is the key to query all EGF params
FxEGFParamsKey = []byte("0x91")

FxSwitchParamsKey = []byte{0x92}
CustomParamsKey = []byte{0x93}
)
Expand Down
4 changes: 2 additions & 2 deletions x/staking/migrations/v8/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
ConsensusProcessKey = []byte{0x93}
)

func GetRemovedValidatorStoreKeys() [][]byte {
func GetRemovedStoreKeys() [][]byte {
return [][]byte{ValidatorOperatorKey, ConsensusPubKey, ConsensusProcessKey}
}

Expand All @@ -23,7 +23,7 @@ func DeleteMigrationValidatorStore(
storeKey storetypes.StoreKey,
) {
store := ctx.KVStore(storeKey)
removeKeys := GetRemovedValidatorStoreKeys()
removeKeys := GetRemovedStoreKeys()
for _, key := range removeKeys {
iterator := storetypes.KVStorePrefixIterator(store, key)
for ; iterator.Valid(); iterator.Next() {
Expand Down

0 comments on commit 09bceb4

Please sign in to comment.