Skip to content

Commit

Permalink
clear expired codes
Browse files Browse the repository at this point in the history
  • Loading branch information
sin3A authored and cyilong committed Jul 16, 2024
1 parent 4441ad5 commit 936a181
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
8 changes: 2 additions & 6 deletions modules/upgrade/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
store "github.com/cosmos/cosmos-sdk/store/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
modulev1 "iritamod.bianjie.ai/api/iritamod/upgrade/module/v1"
"iritamod.bianjie.ai/modules/upgrade/keeper"
)
Expand All @@ -29,7 +28,6 @@ func (am AppModule) IsAppModule() {}

type UpgradeInputs struct {
depinject.In
//upgrade.UpgradeInputs
Key *store.KVStoreKey
Cdc codec.Codec
}
Expand All @@ -43,10 +41,8 @@ type UpgradeOutputs struct {
func ProvideModule(in UpgradeInputs) UpgradeOutputs {
skipUpgradeHeights := make(map[int64]bool)
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
cosmosupgradekeeper := upgradekeeper.NewKeeper(skipUpgradeHeights, in.Key, in.Cdc, "/", nil, authority.String())
keeper := keeper.NewKeeper(
cosmosupgradekeeper,
)
//cosmosupgradekeeper := upgradekeeper.NewKeeper(skipUpgradeHeights, in.Key, in.Cdc, "/", nil, authority.String())
keeper := keeper.NewKeeper(skipUpgradeHeights, in.Key, in.Cdc, "/", nil, authority.String())
m := NewAppModule(keeper)
return UpgradeOutputs{UpgradeKeeper: keeper, Module: m}
}
37 changes: 20 additions & 17 deletions modules/upgrade/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
package keeper

import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
xp "github.com/cosmos/cosmos-sdk/x/upgrade/exported"
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

"iritamod.bianjie.ai/modules/upgrade/types"
)

type Keeper struct {
uk *upgradekeeper.Keeper
*upgradekeeper.Keeper
}

func NewKeeper(uk *upgradekeeper.Keeper) Keeper {
func NewKeeper(skipUpgradeHeights map[int64]bool, storeKey storetypes.StoreKey, cdc codec.BinaryCodec, homePath string, vs xp.ProtocolVersionSetter, authority string) Keeper {
return Keeper{
uk: uk,
upgradekeeper.NewKeeper(skipUpgradeHeights, storeKey, cdc, homePath, vs, authority),
}
}

// ScheduleUpgrade schedules an upgrade based on the specified plan.
func (k Keeper) ScheduleUpgrade(ctx sdk.Context, msg *types.MsgUpgradeSoftware) error {
p, has := k.uk.GetUpgradePlan(ctx)
p, has := k.GetUpgradePlan(ctx)
if has {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest,
Expand All @@ -35,40 +38,40 @@ func (k Keeper) ScheduleUpgrade(ctx sdk.Context, msg *types.MsgUpgradeSoftware)
Height: msg.Height,
Info: msg.Info,
}
return k.uk.ScheduleUpgrade(ctx, plan)
return k.Keeper.ScheduleUpgrade(ctx, plan)
}

// ClearUpgradePlan clears currently schedule upgrade
func (k Keeper) ClearUpgradePlan(ctx sdk.Context) error {
_, has := k.uk.GetUpgradePlan(ctx)
_, has := k.GetUpgradePlan(ctx)
if !has {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "there is currently no upgrade plan")
}

k.uk.ClearUpgradePlan(ctx)
k.Keeper.ClearUpgradePlan(ctx)
return nil
}

func (k Keeper) UpgradeKeeper() *upgradekeeper.Keeper {
return k.uk
return k.Keeper
}

// SetUpgradeHandler sets an UpgradeHandler for the upgrade specified by name. This handler will be called when the upgrade
// with this name is applied. In order for an upgrade with the given name to proceed, a handler for this upgrade
// must be set even if it is a no-op function.
func (k *Keeper) SetUpgradeHandler(name string, upgradeHandler upgradetypes.UpgradeHandler) {
k.uk.SetUpgradeHandler(name, upgradeHandler)
}
//func (k *Keeper) SetUpgradeHandler(name string, upgradeHandler upgradetypes.UpgradeHandler) {
// k.Keeper.SetUpgradeHandler(name, upgradeHandler)
//}

// ReadUpgradeInfoFromDisk returns the name and height of the upgrade
// which is written to disk by the old binary when panic'ing
// if there's an error in reading the info,
// it assumes that the upgrade info is not available
func (k Keeper) ReadUpgradeInfoFromDisk() (upgradetypes.Plan, error) {
return k.uk.ReadUpgradeInfoFromDisk()
}
//func (k Keeper) ReadUpgradeInfoFromDisk() (upgradetypes.Plan, error) {
// return k.Keeper.ReadUpgradeInfoFromDisk()
//}

// IsSkipHeight checks if the given height is part of skipUpgradeHeights
func (k Keeper) IsSkipHeight(height int64) bool {
return k.uk.IsSkipHeight(height)
}
//func (k Keeper) IsSkipHeight(height int64) bool {
// return k.Keeper.IsSkipHeight(height)
//}
13 changes: 13 additions & 0 deletions modules/upgrade/test/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkupgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/stretchr/testify/suite"
"iritamod.bianjie.ai/modules/upgrade/keeper"
uktype "iritamod.bianjie.ai/modules/upgrade/types"
Expand Down Expand Up @@ -43,6 +44,18 @@ func (suite *KeeperTestSuite) TestUpgrade() {
}
err := suite.keeper.ScheduleUpgrade(suite.ctx, msg)
suite.NoError(err)
plan, has := suite.keeper.GetUpgradePlan(suite.ctx)
suite.True(has)
suite.Equal(msg.Name, plan.Name)
suite.Equal(msg.Height, plan.Height)
plan, err = suite.keeper.ReadUpgradeInfoFromDisk()
suite.NoError(err)
resp, err := suite.keeper.CurrentPlan(suite.ctx, &sdkupgradetypes.QueryCurrentPlanRequest{})
suite.NoError(err)
suite.NotNil(resp)
suite.Equal(msg.Name, resp.Plan.Name)
suite.Equal(msg.Height, resp.Plan.Height)
err = suite.keeper.ClearUpgradePlan(suite.ctx)
suite.NoError(err)

}

0 comments on commit 936a181

Please sign in to comment.