Skip to content

Commit

Permalink
refactor: migrate feegrant module (#936)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Jan 22, 2025
1 parent 69b553f commit 1cb185c
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 7 deletions.
76 changes: 76 additions & 0 deletions app/upgrades/v8/feegrant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package v8

import (
"cosmossdk.io/core/store"
"cosmossdk.io/x/feegrant"
feegrantkeeper "cosmossdk.io/x/feegrant/keeper"
"github.com/cosmos/cosmos-sdk/codec"
types1 "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"

fxtypes "github.com/pundiai/fx-core/v8/types"
)

func MigrateFeegrant(ctx sdk.Context, cdc codec.BinaryCodec, storeService store.KVStoreService, authKeeper authkeeper.AccountKeeper) error {
var err error
keeper := feegrantkeeper.NewKeeper(cdc, storeService, authKeeper)
iterErr := keeper.IterateAllFeeAllowances(ctx, func(grant feegrant.Grant) bool {
grant.Allowance, err = swapAllowance(grant.Allowance)
if err != nil {
return true
}
err = updateAllowance(ctx, cdc, storeService, authKeeper, grant)
return err != nil
})
if iterErr != nil {
return iterErr
}
if err != nil {
return err
}
return err
}

func swapAllowance(allowanceAny *types1.Any) (*types1.Any, error) {
switch allowance := allowanceAny.GetCachedValue().(type) {
case *feegrant.BasicAllowance:
allowance.SpendLimit = fxtypes.SwapCoins(allowance.SpendLimit)
return types1.NewAnyWithValue(allowance)
case *feegrant.PeriodicAllowance:
allowance.Basic.SpendLimit = fxtypes.SwapCoins(allowance.Basic.SpendLimit)
allowance.PeriodCanSpend = fxtypes.SwapCoins(allowance.PeriodCanSpend)
allowance.PeriodSpendLimit = fxtypes.SwapCoins(allowance.PeriodSpendLimit)
return types1.NewAnyWithValue(allowance)
case *feegrant.AllowedMsgAllowance:
newAny, err := swapAllowance(allowance.Allowance)
if err != nil {
return nil, err
}
allowance.Allowance = newAny
return types1.NewAnyWithValue(allowance)
default:
return nil, sdkerrors.ErrPackAny.Wrap("failed to swap allowance")
}
}

func updateAllowance(ctx sdk.Context, cdc codec.BinaryCodec, storeService store.KVStoreService, authKeeper authkeeper.AccountKeeper, grant feegrant.Grant) error {
kvStore := storeService.OpenKVStore(ctx)

granter, err := authKeeper.AddressCodec().StringToBytes(grant.Granter)
if err != nil {
return err
}
grantee, err := authKeeper.AddressCodec().StringToBytes(grant.Grantee)
if err != nil {
return err
}
key := feegrant.FeeAllowanceKey(granter, grantee)

bz, err := cdc.Marshal(&grant)
if err != nil {
return err
}
return kvStore.Set(key, bz)
}
83 changes: 83 additions & 0 deletions app/upgrades/v8/feegrant_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package v8_test

import (
"testing"
"time"

sdkmath "cosmossdk.io/math"
"cosmossdk.io/x/feegrant"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/require"

"github.com/pundiai/fx-core/v8/app/upgrades/v8"
"github.com/pundiai/fx-core/v8/testutil/helpers"
fxtypes "github.com/pundiai/fx-core/v8/types"
)

func Test_migrateFeegrant(t *testing.T) {
app, ctx := helpers.NewAppWithValNumber(t, 1)

now := time.Now()
coins := sdk.NewCoins(sdk.NewCoin(fxtypes.LegacyFXDenom, sdkmath.NewInt(1e18)))
allowanceList := []feegrant.FeeAllowanceI{
&feegrant.BasicAllowance{
SpendLimit: coins,
Expiration: &now,
},
&feegrant.PeriodicAllowance{
Basic: feegrant.BasicAllowance{
SpendLimit: coins,
Expiration: &now,
},
Period: 1,
PeriodSpendLimit: coins,
PeriodCanSpend: coins,
PeriodReset: now,
},
}

allowanceListLen := len(allowanceList)
for i := 0; i < allowanceListLen; i++ {
msg, ok := allowanceList[i].(proto.Message)
require.True(t, ok)
value, err := codectypes.NewAnyWithValue(msg)
require.NoError(t, err)
allowanceList = append(allowanceList,
&feegrant.AllowedMsgAllowance{
Allowance: value,
AllowedMessages: []string{
codectypes.MsgTypeURL(msg),
},
},
)
}

granters := make([]sdk.AccAddress, 0, len(allowanceList))
grantees := make([]sdk.AccAddress, 0, len(allowanceList))
for i, allowance := range allowanceList {
granters = append(granters, helpers.GenAccAddress())
grantees = append(grantees, helpers.GenAccAddress())
app.AccountKeeper.SetAccount(ctx, app.AccountKeeper.NewAccountWithAddress(ctx, granters[i]))
app.AccountKeeper.SetAccount(ctx, app.AccountKeeper.NewAccountWithAddress(ctx, grantees[i]))
require.NoError(t,
app.FeeGrantKeeper.GrantAllowance(
ctx,
granters[i],
grantees[i],
allowance,
),
)
}

err := v8.MigrateFeegrant(ctx, app.AppCodec(), runtime.NewKVStoreService(app.GetKey(feegrant.StoreKey)), app.AccountKeeper)
require.NoError(t, err)

for i, allowance := range allowanceList {
getAllowance, err := app.FeeGrantKeeper.GetAllowance(ctx, granters[i], grantees[i])
require.NoError(t, err)
require.NotEqual(t, allowance, getAllowance)
}
}
20 changes: 13 additions & 7 deletions app/upgrades/v8/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"cosmossdk.io/collections"
sdkmath "cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/feegrant"
upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/module"
Expand Down Expand Up @@ -47,19 +49,19 @@ import (
fxstakingv8 "github.com/pundiai/fx-core/v8/x/staking/migrations/v8"
)

func CreateUpgradeHandler(_ codec.Codec, mm *module.Manager, configurator module.Configurator, app *keepers.AppKeepers) upgradetypes.UpgradeHandler {
func CreateUpgradeHandler(codec codec.Codec, mm *module.Manager, configurator module.Configurator, app *keepers.AppKeepers) upgradetypes.UpgradeHandler {
return func(ctx context.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
cacheCtx, commit := sdk.UnwrapSDKContext(ctx).CacheContext()

var err error
var toVM module.VersionMap
if cacheCtx.ChainID() == fxtypes.TestnetChainId {
if err = upgradeTestnet(cacheCtx, app); err != nil {
if err = upgradeTestnet(cacheCtx, codec, app); err != nil {
return fromVM, err
}
toVM = fromVM
} else {
toVM, err = upgradeMainnet(cacheCtx, mm, configurator, app, fromVM, plan)
toVM, err = upgradeMainnet(cacheCtx, codec, mm, configurator, app, fromVM, plan)
if err != nil {
return fromVM, err
}
Expand All @@ -70,7 +72,7 @@ func CreateUpgradeHandler(_ codec.Codec, mm *module.Manager, configurator module
}
}

func upgradeTestnet(ctx sdk.Context, app *keepers.AppKeepers) error {
func upgradeTestnet(ctx sdk.Context, codec codec.Codec, app *keepers.AppKeepers) error {
fixBaseOracleStatus(ctx, app.CrosschainKeepers.Layer2Keeper)

if err := fixPundixCoin(ctx, app.EvmKeeper, app.Erc20Keeper, app.BankKeeper); err != nil {
Expand Down Expand Up @@ -104,7 +106,7 @@ func upgradeTestnet(ctx sdk.Context, app *keepers.AppKeepers) error {
migrateWFXToWPUNDIAI(ctx, app.EvmKeeper)
migrateOracleDelegateAmount(ctx, app.CrosschainKeepers)

if err := migrateModulesData(ctx, app); err != nil {
if err := migrateModulesData(ctx, codec, app); err != nil {
return err
}

Expand All @@ -114,6 +116,7 @@ func upgradeTestnet(ctx sdk.Context, app *keepers.AppKeepers) error {
//nolint:gocyclo // mainnet
func upgradeMainnet(
ctx sdk.Context,
codec codec.Codec,
mm *module.Manager,
configurator module.Configurator,
app *keepers.AppKeepers,
Expand All @@ -135,7 +138,7 @@ func upgradeMainnet(

migrateWFXToWPUNDIAI(ctx, app.EvmKeeper)

if err = migrateModulesData(ctx, app); err != nil {
if err = migrateModulesData(ctx, codec, app); err != nil {
return fromVM, err
}

Expand Down Expand Up @@ -206,7 +209,7 @@ func upgradeMainnet(
return toVM, nil
}

func migrateModulesData(ctx sdk.Context, app *keepers.AppKeepers) error {
func migrateModulesData(ctx sdk.Context, codec codec.Codec, app *keepers.AppKeepers) error {
if err := migrateBankModule(ctx, app.BankKeeper); err != nil {
return err
}
Expand All @@ -219,6 +222,9 @@ func migrateModulesData(ctx sdk.Context, app *keepers.AppKeepers) error {
if err := migrateMintParams(ctx, app.MintKeeper); err != nil {
return err
}
if err := MigrateFeegrant(ctx, codec, runtime.NewKVStoreService(app.GetKey(feegrant.StoreKey)), app.AccountKeeper); err != nil {
return err
}

migrateTransferTokenInEscrow(ctx, app.IBCTransferKeeper)

Expand Down
13 changes: 13 additions & 0 deletions testutil/helpers/app.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package helpers

import (
"testing"

"cosmossdk.io/log"
dbm "github.com/cosmos/cosmos-db"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/viper"

"github.com/pundiai/fx-core/v8/app"
Expand Down Expand Up @@ -34,3 +37,13 @@ func NewApp(opts ...func(*AppOpts)) *app.App {
viper.New(),
)
}

func NewAppWithValNumber(t *testing.T, valNumber int) (*app.App, sdk.Context) {
t.Helper()

valSet, valPrivs := generateGenesisValidator(valNumber)
myApp := setupWithGenesisValSet(t, valSet, valPrivs)
ctx := myApp.GetContextForFinalizeBlock(nil)
ctx = ctx.WithProposer(valSet.Proposer.Address.Bytes())
return myApp, ctx
}

0 comments on commit 1cb185c

Please sign in to comment.