Skip to content

Commit

Permalink
refactor migration
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverHappened committed Aug 17, 2023
1 parent 1f61b01 commit 6cfda75
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 70 deletions.
145 changes: 78 additions & 67 deletions app/upgrades/nextupgrade/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package nextupgrade
import (
"errors"

"github.com/cosmos/cosmos-sdk/codec"
ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
ibcchanneltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
Expand All @@ -16,7 +16,83 @@ import (
contractmanagertypes "github.com/neutron-org/neutron/x/contractmanager/types"
)

func MigrateFailures(ctx sdk.Context, storeKeys upgrades.StoreKeys, cdc codec.Codec) error {
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
keepers *upgrades.UpgradeKeepers,
storeKeys upgrades.StoreKeys,
cdc codec.Codec,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Starting module migrations...")
vm, err := mm.RunMigrations(ctx, configurator, vm)
if err != nil {
return vm, err
}

err = migrateGlobalFees(ctx, keepers)
if err != nil {
ctx.Logger().Error("failed to migrate GlobalFees", "err", err)
return vm, err
}

err = migrateFailures(ctx, storeKeys, cdc)
if err != nil {
ctx.Logger().Error("failed to migrate Failures", "err", err)
return vm, err
}

ctx.Logger().Info("Upgrade complete")
return vm, err
}
}

func migrateGlobalFees(ctx sdk.Context, keepers *upgrades.UpgradeKeepers) error {
ctx.Logger().Info("Implementing GlobalFee Params...")

if !keepers.GlobalFeeSubspace.Has(ctx, types.ParamStoreKeyMinGasPrices) {
return errors.New("minimum_gas_prices param not found")
}

if !keepers.GlobalFeeSubspace.Has(ctx, types.ParamStoreKeyBypassMinFeeMsgTypes) {
return errors.New("bypass_min_fee_msg_types param not found")
}

if !keepers.GlobalFeeSubspace.Has(ctx, types.ParamStoreKeyMaxTotalBypassMinFeeMsgGasUsage) {
return errors.New("max_total_bypass_min_fee_msg_gas_usage param not found")
}

// global fee is empty set, set global fee to equal to 0.05 USD (for 200k of gas) in appropriate coin
// As of June 22nd, 2023 this is
// 0.9untrn,0.026ibc/C4CFF46FD6DE35CA4CF4CE031E643C8FDC9BA4B99AE598E9B0ED98FE3A2319F9,0.25ibc/F082B65C88E4B6D5EF1DB243CDA1D331D002759E938A0F5CD3FFDC5D53B3E349
requiredGlobalFees := sdk.DecCoins{
sdk.NewDecCoinFromDec("untrn", sdk.MustNewDecFromStr("0.9")),
sdk.NewDecCoinFromDec("ibc/C4CFF46FD6DE35CA4CF4CE031E643C8FDC9BA4B99AE598E9B0ED98FE3A2319F9", sdk.MustNewDecFromStr("0.026")),
sdk.NewDecCoinFromDec("ibc/F082B65C88E4B6D5EF1DB243CDA1D331D002759E938A0F5CD3FFDC5D53B3E349", sdk.MustNewDecFromStr("0.25")),
}
requiredGlobalFees = requiredGlobalFees.Sort()

keepers.GlobalFeeSubspace.Set(ctx, types.ParamStoreKeyMinGasPrices, &requiredGlobalFees)

ctx.Logger().Info("Global fees was set successfully")

defaultBypassFeeMessages := []string{
sdk.MsgTypeURL(&ibcchanneltypes.MsgRecvPacket{}),
sdk.MsgTypeURL(&ibcchanneltypes.MsgAcknowledgement{}),
sdk.MsgTypeURL(&ibcclienttypes.MsgUpdateClient{}),
}
keepers.GlobalFeeSubspace.Set(ctx, types.ParamStoreKeyBypassMinFeeMsgTypes, &defaultBypassFeeMessages)

ctx.Logger().Info("Bypass min fee msg types was set successfully")

keepers.GlobalFeeSubspace.Set(ctx, types.ParamStoreKeyMaxTotalBypassMinFeeMsgGasUsage, types.DefaultmaxTotalBypassMinFeeMsgGasUsage)

ctx.Logger().Info("Max total bypass min fee msg gas usage set successfully")

return nil
}

func migrateFailures(ctx sdk.Context, storeKeys upgrades.StoreKeys, cdc codec.Codec) error {
ctx.Logger().Info("Migrating failures...")

// fetch list of all old failures
Expand Down Expand Up @@ -53,68 +129,3 @@ func MigrateFailures(ctx sdk.Context, storeKeys upgrades.StoreKeys, cdc codec.Co

return nil
}

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
keepers *upgrades.UpgradeKeepers,
storeKeys upgrades.StoreKeys,
cdc codec.Codec,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Starting module migrations...")
vm, err := mm.RunMigrations(ctx, configurator, vm)
if err != nil {
return vm, err
}

ctx.Logger().Info("Implementing GlobalFee Params...")

if !keepers.GlobalFeeSubspace.Has(ctx, types.ParamStoreKeyMinGasPrices) {
return vm, errors.New("minimum_gas_prices param not found")
}

if !keepers.GlobalFeeSubspace.Has(ctx, types.ParamStoreKeyBypassMinFeeMsgTypes) {
return vm, errors.New("bypass_min_fee_msg_types param not found")
}

if !keepers.GlobalFeeSubspace.Has(ctx, types.ParamStoreKeyMaxTotalBypassMinFeeMsgGasUsage) {
return vm, errors.New("max_total_bypass_min_fee_msg_gas_usage param not found")
}

// global fee is empty set, set global fee to equal to 0.05 USD (for 200k of gas) in appropriate coin
// As of June 22nd, 2023 this is
// 0.9untrn,0.026ibc/C4CFF46FD6DE35CA4CF4CE031E643C8FDC9BA4B99AE598E9B0ED98FE3A2319F9,0.25ibc/F082B65C88E4B6D5EF1DB243CDA1D331D002759E938A0F5CD3FFDC5D53B3E349
requiredGlobalFees := sdk.DecCoins{
sdk.NewDecCoinFromDec("untrn", sdk.MustNewDecFromStr("0.9")),
sdk.NewDecCoinFromDec("ibc/C4CFF46FD6DE35CA4CF4CE031E643C8FDC9BA4B99AE598E9B0ED98FE3A2319F9", sdk.MustNewDecFromStr("0.026")),
sdk.NewDecCoinFromDec("ibc/F082B65C88E4B6D5EF1DB243CDA1D331D002759E938A0F5CD3FFDC5D53B3E349", sdk.MustNewDecFromStr("0.25")),
}
requiredGlobalFees = requiredGlobalFees.Sort()

keepers.GlobalFeeSubspace.Set(ctx, types.ParamStoreKeyMinGasPrices, &requiredGlobalFees)

ctx.Logger().Info("Global fees was set successfully")

defaultBypassFeeMessages := []string{
sdk.MsgTypeURL(&ibcchanneltypes.MsgRecvPacket{}),
sdk.MsgTypeURL(&ibcchanneltypes.MsgAcknowledgement{}),
sdk.MsgTypeURL(&ibcclienttypes.MsgUpdateClient{}),
}
keepers.GlobalFeeSubspace.Set(ctx, types.ParamStoreKeyBypassMinFeeMsgTypes, &defaultBypassFeeMessages)

ctx.Logger().Info("Bypass min fee msg types was set successfully")

keepers.GlobalFeeSubspace.Set(ctx, types.ParamStoreKeyMaxTotalBypassMinFeeMsgGasUsage, types.DefaultmaxTotalBypassMinFeeMsgGasUsage)

ctx.Logger().Info("Max total bypass min fee msg gas usage set successfully")

err = MigrateFailures(ctx, storeKeys, cdc)
if err != nil {
ctx.Logger().Error("failed to migrate failures", "err", err)
}

ctx.Logger().Info("Upgrade complete")
return vm, err
}
}
6 changes: 3 additions & 3 deletions app/upgrades/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package upgrades

import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
store "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/types/module"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
Expand All @@ -29,7 +29,7 @@ type Upgrade struct {
CreateUpgradeHandler func(*module.Manager, module.Configurator, *UpgradeKeepers, StoreKeys, codec.Codec) upgradetypes.UpgradeHandler

// Store upgrades, should be used for any new modules introduced, new modules deleted, or store names renamed.
StoreUpgrades storetypes.StoreUpgrades
StoreUpgrades store.StoreUpgrades
}

type UpgradeKeepers struct {
Expand All @@ -47,5 +47,5 @@ type UpgradeKeepers struct {
}

type StoreKeys interface {
GetKey(string) *storetypes.KVStoreKey
GetKey(string) *store.KVStoreKey
}

0 comments on commit 6cfda75

Please sign in to comment.