Skip to content

Commit

Permalink
refactor(staking): remove unused of validator store when migrate (#733)
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 7fd9cc7 commit 8c2c51a
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 28 deletions.
3 changes: 0 additions & 3 deletions app/app_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package app_test
import (
"testing"

"github.com/stretchr/testify/require"

"github.com/functionx/fx-core/v8/testutil/helpers"
fxtypes "github.com/functionx/fx-core/v8/types"
)
Expand All @@ -14,7 +12,6 @@ func TestAppDB(t *testing.T) {

chainId := fxtypes.MainnetChainId
myApp := buildApp(t)
require.NoError(t, myApp.LoadLatestVersion())
ctx := newContext(t, myApp, chainId, false)

_ = ctx
Expand Down
4 changes: 2 additions & 2 deletions app/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func Test_ExportGenesisAndRunNode(t *testing.T) {
ctx := context.Background()
group, ctx := errgroup.WithContext(ctx)
myAppConstructor := func(appConfig *fxcfg.Config, ctx *server.Context) servertypes.Application {
return helpers.NewApp(func(opts helpers.AppOpts) {
return helpers.NewApp(func(opts *helpers.AppOpts) {
opts.Home = ctx.Config.RootDir
})
}
Expand All @@ -110,7 +110,7 @@ func exportGenesisDoc(t *testing.T, home string) *types.GenesisDoc {
db, err := dbm.NewDB("application", dbm.GoLevelDBBackend, filepath.Join(home, "data"))
require.NoError(t, err)

myApp := helpers.NewApp(func(opts helpers.AppOpts) {
myApp := helpers.NewApp(func(opts *helpers.AppOpts) {
opts.Logger = log.NewTestLogger(t)
opts.Home = home
opts.DB = db
Expand Down
9 changes: 7 additions & 2 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import (
ibctransfer "github.com/cosmos/ibc-go/v8/modules/apps/transfer"
ibctransferkeeper "github.com/cosmos/ibc-go/v8/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
ibcconnectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"
Expand Down Expand Up @@ -587,8 +589,11 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(slashingtypes.ModuleName).WithKeyTable(slashingtypes.ParamKeyTable())
paramsKeeper.Subspace(govtypes.ModuleName).WithKeyTable(govv1.ParamKeyTable())
paramsKeeper.Subspace(crisistypes.ModuleName).WithKeyTable(crisistypes.ParamKeyTable())
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
paramsKeeper.Subspace(ibcexported.ModuleName)

paramsKeeper.Subspace(ibctransfertypes.ModuleName).WithKeyTable(ibctransfertypes.ParamKeyTable())
ibcKeyTable := ibcclienttypes.ParamKeyTable()
ibcKeyTable.RegisterParamSet(&ibcconnectiontypes.Params{})
paramsKeeper.Subspace(ibcexported.ModuleName).WithKeyTable(ibcKeyTable)

paramsKeeper.Subspace(evmtypes.ModuleName).WithKeyTable(v0evmtypes.ParamKeyTable())
paramsKeeper.Subspace(feemarkettypes.ModuleName).WithKeyTable(feemarkettypes.ParamKeyTable())
Expand Down
51 changes: 39 additions & 12 deletions app/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,47 @@ import (
"path/filepath"
"testing"

coreheader "cosmossdk.io/core/header"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/upgrade"
upgradetypes "cosmossdk.io/x/upgrade/types"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtime "github.com/cometbft/cometbft/types/time"
dbm "github.com/cosmos/cosmos-db"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/functionx/fx-core/v8/app"
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"
fxstakingv8 "github.com/functionx/fx-core/v8/x/staking/migrations/v8"
)

func Test_UpgradeAndMigrate(t *testing.T) {
helpers.SkipTest(t, "Skipping local test:", t.Name())

chainId := fxtypes.MainnetChainId
myApp := buildApp(t)
myApp.SetStoreLoader(upgradetypes.UpgradeStoreLoader(myApp.LastBlockHeight()+1, nextversion.Upgrade.StoreUpgrades()))
require.NoError(t, myApp.LoadLatestVersion())

ctx := newContext(t, myApp, chainId, false)

// 1. set upgrade plan
require.NoError(t, myApp.UpgradeKeeper.ScheduleUpgrade(ctx, upgradetypes.Plan{
Name: nextversion.Upgrade.UpgradeName,
Height: ctx.BlockHeight() + 1,
Height: ctx.BlockHeight(),
}))

header := ctx.BlockHeader()
header.Height = header.Height + 1

var err error
_, err = myApp.BeginBlocker(ctx)
// 2. execute upgrade
responsePreBlock, err := upgrade.PreBlocker(ctx, myApp.UpgradeKeeper)
require.NoError(t, err)
require.True(t, responsePreBlock.IsConsensusParamsChanged())

_, err = myApp.EndBlocker(ctx)
require.NoError(t, err)
// 3. check the status after the upgrade
checkAppUpgrade(t, ctx, myApp)
}

func buildApp(t *testing.T) *app.App {
Expand All @@ -52,8 +55,9 @@ func buildApp(t *testing.T) *app.App {
db, err := dbm.NewDB("application", dbm.GoLevelDBBackend, filepath.Join(home, "data"))
require.NoError(t, err)

myApp := helpers.NewApp(func(opts helpers.AppOpts) {
myApp := helpers.NewApp(func(opts *helpers.AppOpts) {
opts.DB = db
opts.Home = home
})
return myApp
}
Expand All @@ -66,10 +70,15 @@ func newContext(t *testing.T, myApp *app.App, chainId string, deliveState bool)
}
var ctx sdk.Context
if deliveState {
ctx = myApp.NewContext(false)
ctx = myApp.NewContextLegacy(false, header)
} else {
ctx = myApp.NewUncachedContext(false, header)
}
ctx = ctx.WithHeaderInfo(coreheader.Info{
Height: header.Height,
Time: header.Time,
ChainID: header.ChainID,
})
// set the first validator to proposer
validators, err := myApp.StakingKeeper.GetAllValidators(ctx)
require.NoError(t, err)
Expand All @@ -79,3 +88,21 @@ func newContext(t *testing.T, myApp *app.App, chainId string, deliveState bool)
ctx = ctx.WithProposer(pubKey.Address().Bytes())
return ctx
}

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

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 {
iterator := storetypes.KVStorePrefixIterator(kvStore, removeKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
require.Failf(t, "key is not deleted", "prefix:%x, key:%x", removeKey, iterator.Key())
}
}
}
5 changes: 5 additions & 0 deletions app/upgrades/v8/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
upgradetypes "cosmossdk.io/x/upgrade/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

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

func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator, app *keepers.AppKeepers) upgradetypes.UpgradeHandler {
Expand All @@ -19,6 +21,9 @@ func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator,
if err != nil {
return fromVM, err
}

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

commit()
cacheCtx.Logger().Info("upgrade complete", "module", "upgrade")
return toVM, nil
Expand Down
4 changes: 2 additions & 2 deletions testutil/helpers/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type AppOpts struct {
DB dbm.DB
}

func NewApp(opts ...func(AppOpts)) *app.App {
defOpts := AppOpts{
func NewApp(opts ...func(*AppOpts)) *app.App {
defOpts := &AppOpts{
Logger: log.NewNopLogger(),
Home: fxtypes.GetDefaultNodeHome(),
DB: dbm.NewMemDB(),
Expand Down
18 changes: 18 additions & 0 deletions x/gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
govv1betal.RegisterQueryServer(cfg.QueryServer(), legacyQueryServer)
govv1.RegisterQueryServer(cfg.QueryServer(), queryServer)
types.RegisterQueryServer(cfg.QueryServer(), queryServer)

// register migration for x/gov
m := govkeeper.NewMigrator(am.keeper.Keeper, am.legacySubspace)
if err := cfg.RegisterMigration(govtypes.ModuleName, 1, m.Migrate1to2); err != nil {
panic(fmt.Sprintf("failed to migrate x/gov from version 1 to 2: %v", err))
}

if err := cfg.RegisterMigration(govtypes.ModuleName, 2, m.Migrate2to3); err != nil {
panic(fmt.Sprintf("failed to migrate x/gov from version 2 to 3: %v", err))
}

if err := cfg.RegisterMigration(govtypes.ModuleName, 3, m.Migrate3to4); err != nil {
panic(fmt.Sprintf("failed to migrate x/gov from version 3 to 4: %v", err))
}

if err := cfg.RegisterMigration(govtypes.ModuleName, 4, m.Migrate4to5); err != nil {
panic(fmt.Sprintf("failed to migrate x/gov from version 4 to 5: %v", err))
}
}

// InitGenesis performs genesis initialization for the gov module. It returns
Expand Down
33 changes: 33 additions & 0 deletions x/staking/migrations/v8/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package v8

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

var (
// Deprecated: do not use, remove in v8
ValidatorOperatorKey = []byte{0x91}
// Deprecated: do not use, remove in v8
ConsensusPubKey = []byte{0x92}
// Deprecated: do not use, remove in v8
ConsensusProcessKey = []byte{0x93}
)

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

func DeleteMigrationValidatorStore(
ctx sdk.Context,
storeKey storetypes.StoreKey,
) {
store := ctx.KVStore(storeKey)
removeKeys := GetRemovedValidatorStoreKeys()
for _, key := range removeKeys {
iterator := storetypes.KVStorePrefixIterator(store, key)
for ; iterator.Valid(); iterator.Next() {
store.Delete(iterator.Key())
}
}
}
8 changes: 1 addition & 7 deletions x/staking/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

var (
AllowanceKey = []byte{0x90}
// todo: remove this key in migrate v8
ValidatorOperatorKey = []byte{0x91}
ConsensusPubKey = []byte{0x92}
ConsensusProcessKey = []byte{0x93}
)
var AllowanceKey = []byte{0x90}

func GetAllowanceKey(valAddr sdk.ValAddress, owner, spender sdk.AccAddress) []byte {
// key is of the form AllowanceKey || valAddrLen (1 byte) || valAddr || ownerAddrLen (1 byte) || ownerAddr || spenderAddrLen (1 byte) || spenderAddr
Expand Down

0 comments on commit 8c2c51a

Please sign in to comment.