-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate feegrant module (#936)
- Loading branch information
1 parent
69b553f
commit 1cb185c
Showing
4 changed files
with
185 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters