feat: Add amino support for x/authz and x/feegrant (#9457)

* add amino for authz

* Add amion for feegrant

* add cl

* Remove protoCdc from simulations

* Update x/authz/client/testutil/tx.go

Co-authored-by: likhita-809 <78951027+likhita-809@users.noreply.github.com>

* Address reviews

Co-authored-by: likhita-809 <78951027+likhita-809@users.noreply.github.com>
This commit is contained in:
Amaury
2021-06-04 09:29:58 +00:00
committed by GitHub
co-authored by likhita-809
parent fc6e3d6056
commit ec3e2b4d0d
11 changed files with 231 additions and 56 deletions
+49 -11
View File
@@ -59,6 +59,22 @@ func (s *IntegrationTestSuite) SetupSuite() {
granter := val.Address
grantee := s.network.Validators[1].Address
s.createGrant(granter, grantee)
grant, err := feegrant.NewGrant(granter, grantee, &feegrant.BasicAllowance{
SpendLimit: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100))),
})
s.Require().NoError(err)
s.addedGrant = grant
s.addedGranter = granter
s.addedGrantee = grantee
}
// createGrant creates a new basic allowance fee grant from granter to grantee.
func (s *IntegrationTestSuite) createGrant(granter, grantee sdk.Address) {
val := s.network.Validators[0]
clientCtx := val.ClientCtx
commonFlags := []string{
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
@@ -81,20 +97,10 @@ func (s *IntegrationTestSuite) SetupSuite() {
cmd := cli.NewCmdFeeGrant()
_, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
_, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
s.Require().NoError(err)
_, err = s.network.WaitForHeight(1)
s.Require().NoError(err)
s.addedGranter = granter
s.addedGrantee = grantee
grant, err := feegrant.NewGrant(granter, grantee, &feegrant.BasicAllowance{
SpendLimit: sdk.NewCoins(fee),
})
s.Require().NoError(err)
s.addedGrant = grant
}
func (s *IntegrationTestSuite) TearDownSuite() {
@@ -302,6 +308,20 @@ func (s *IntegrationTestSuite) TestNewCmdFeeGrant() {
),
false, 0, &sdk.TxResponse{},
},
{
"valid basic fee grant with amino",
append(
[]string{
granter.String(),
"cosmos1v57fx2l2rt6ehujuu99u2fw05779m5e2ux4z2h",
fmt.Sprintf("--%s=%s", cli.FlagSpendLimit, "100stake"),
fmt.Sprintf("--%s=%s", flags.FlagFrom, granter),
fmt.Sprintf("--%s=%s", flags.FlagSignMode, flags.SignModeLegacyAminoJSON),
},
commonFlags...,
),
false, 0, &sdk.TxResponse{},
},
{
"valid basic fee grant without spend limit",
append(
@@ -507,6 +527,11 @@ func (s *IntegrationTestSuite) TestNewCmdRevokeFeegrant() {
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
}
// Create new fee grant specifically to test amino.
aminoGrantee, err := sdk.AccAddressFromBech32("cosmos16ydaqh0fcnh4qt7a3jme4mmztm2qel5axcpw00")
s.Require().NoError(err)
s.createGrant(granter, aminoGrantee)
testCases := []struct {
name string
args []string
@@ -562,6 +587,19 @@ func (s *IntegrationTestSuite) TestNewCmdRevokeFeegrant() {
),
false, 0, &sdk.TxResponse{},
},
{
"Valid revoke with amino",
append(
[]string{
granter.String(),
aminoGrantee.String(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, granter),
fmt.Sprintf("--%s=%s", flags.FlagSignMode, flags.SignModeLegacyAminoJSON),
},
commonFlags...,
),
false, 0, &sdk.TxResponse{},
},
}
for _, tc := range testCases {
+1 -2
View File
@@ -207,8 +207,7 @@ func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
// WeightedOperations returns all the feegrant module operations with their respective weights.
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
protoCdc := codec.NewProtoCodec(am.registry)
return simulation.WeightedOperations(
simState.AppParams, simState.Cdc, am.accountKeeper, am.bankKeeper, am.keeper, protoCdc,
simState.AppParams, simState.Cdc, am.accountKeeper, am.bankKeeper, am.keeper,
)
}
+36 -2
View File
@@ -3,14 +3,18 @@ package feegrant
import (
"github.com/gogo/protobuf/proto"
"github.com/cosmos/cosmos-sdk/codec/legacy"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx"
)
var (
_, _ sdk.Msg = &MsgGrantAllowance{}, &MsgRevokeAllowance{}
_ types.UnpackInterfacesMessage = &MsgGrantAllowance{}
_, _ sdk.Msg = &MsgGrantAllowance{}, &MsgRevokeAllowance{}
_, _ legacytx.LegacyMsg = &MsgGrantAllowance{}, &MsgRevokeAllowance{} // For amino support.
_ types.UnpackInterfacesMessage = &MsgGrantAllowance{}
)
// NewMsgGrantAllowance creates a new MsgGrantAllowance.
@@ -61,6 +65,21 @@ func (msg MsgGrantAllowance) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{granter}
}
// Type implements the LegacyMsg.Type method.
func (msg MsgGrantAllowance) Type() string {
return sdk.MsgTypeURL(&msg)
}
// Route implements the LegacyMsg.Route method.
func (msg MsgGrantAllowance) Route() string {
return sdk.MsgTypeURL(&msg)
}
// GetSignBytes implements the LegacyMsg.GetSignBytes method.
func (msg MsgGrantAllowance) GetSignBytes() []byte {
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
}
// GetFeeAllowanceI returns unpacked FeeAllowance
func (msg MsgGrantAllowance) GetFeeAllowanceI() (FeeAllowanceI, error) {
allowance, ok := msg.Allowance.GetCachedValue().(FeeAllowanceI)
@@ -108,3 +127,18 @@ func (msg MsgRevokeAllowance) GetSigners() []sdk.AccAddress {
}
return []sdk.AccAddress{granter}
}
// Type implements the LegacyMsg.Type method.
func (msg MsgRevokeAllowance) Type() string {
return sdk.MsgTypeURL(&msg)
}
// Route implements the LegacyMsg.Route method.
func (msg MsgRevokeAllowance) Route() string {
return sdk.MsgTypeURL(&msg)
}
// GetSignBytes implements the LegacyMsg.GetSignBytes method.
func (msg MsgRevokeAllowance) GetSignBytes() []byte {
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
}
+6 -7
View File
@@ -28,7 +28,6 @@ var (
func WeightedOperations(
appParams simtypes.AppParams, cdc codec.JSONCodec,
ak feegrant.AccountKeeper, bk feegrant.BankKeeper, k keeper.Keeper,
protoCdc *codec.ProtoCodec,
) simulation.WeightedOperations {
var (
@@ -51,17 +50,17 @@ func WeightedOperations(
return simulation.WeightedOperations{
simulation.NewWeightedOperation(
weightMsgGrantAllowance,
SimulateMsgGrantAllowance(ak, bk, k, protoCdc),
SimulateMsgGrantAllowance(ak, bk, k),
),
simulation.NewWeightedOperation(
weightMsgRevokeAllowance,
SimulateMsgRevokeAllowance(ak, bk, k, protoCdc),
SimulateMsgRevokeAllowance(ak, bk, k),
),
}
}
// SimulateMsgGrantAllowance generates MsgGrantAllowance with random values.
func SimulateMsgGrantAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeeper, k keeper.Keeper, protoCdc *codec.ProtoCodec) simtypes.Operation {
func SimulateMsgGrantAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeeper, k keeper.Keeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
@@ -118,12 +117,12 @@ func SimulateMsgGrantAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeeper
if err != nil {
return simtypes.NoOpMsg(feegrant.ModuleName, sdk.MsgTypeURL(msg), "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, "", protoCdc), nil, err
return simtypes.NewOperationMsg(msg, true, "", nil), nil, err
}
}
// SimulateMsgRevokeAllowance generates a MsgRevokeAllowance with random values.
func SimulateMsgRevokeAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeeper, k keeper.Keeper, protoCdc *codec.ProtoCodec) simtypes.Operation {
func SimulateMsgRevokeAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeeper, k keeper.Keeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
@@ -182,6 +181,6 @@ func SimulateMsgRevokeAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeepe
}
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
return simtypes.NewOperationMsg(&msg, true, "", protoCdc), nil, err
return simtypes.NewOperationMsg(&msg, true, "", nil), nil, err
}
}
+6 -10
View File
@@ -10,7 +10,6 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -22,9 +21,8 @@ import (
type SimTestSuite struct {
suite.Suite
ctx sdk.Context
app *simapp.SimApp
protoCdc *codec.ProtoCodec
ctx sdk.Context
app *simapp.SimApp
}
func (suite *SimTestSuite) SetupTest() {
@@ -34,7 +32,6 @@ func (suite *SimTestSuite) SetupTest() {
suite.ctx = app.BaseApp.NewContext(checkTx, tmproto.Header{
Time: time.Now(),
})
suite.protoCdc = codec.NewProtoCodec(suite.app.InterfaceRegistry())
}
@@ -62,10 +59,9 @@ func (suite *SimTestSuite) TestWeightedOperations() {
cdc := app.AppCodec()
appParams := make(simtypes.AppParams)
weightesOps := simulation.WeightedOperations(
weightedOps := simulation.WeightedOperations(
appParams, cdc, app.AccountKeeper,
app.BankKeeper, app.FeeGrantKeeper,
suite.protoCdc,
)
s := rand.NewSource(1)
@@ -89,7 +85,7 @@ func (suite *SimTestSuite) TestWeightedOperations() {
},
}
for i, w := range weightesOps {
for i, w := range weightedOps {
operationMsg, _, _ := w.Op()(r, app.BaseApp, ctx, accs, ctx.ChainID())
// the following checks are very much dependent from the ordering of the output given
// by WeightedOperations. if the ordering in WeightedOperations changes some tests
@@ -112,7 +108,7 @@ func (suite *SimTestSuite) TestSimulateMsgGrantAllowance() {
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1, AppHash: app.LastCommitID().Hash}})
// execute operation
op := simulation.SimulateMsgGrantAllowance(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, suite.protoCdc)
op := simulation.SimulateMsgGrantAllowance(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper)
operationMsg, futureOperations, err := op(r, app.BaseApp, ctx, accounts, "")
require.NoError(err)
@@ -154,7 +150,7 @@ func (suite *SimTestSuite) TestSimulateMsgRevokeAllowance() {
require.NoError(err)
// execute operation
op := simulation.SimulateMsgRevokeAllowance(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, suite.protoCdc)
op := simulation.SimulateMsgRevokeAllowance(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper)
operationMsg, futureOperations, err := op(r, app.BaseApp, ctx, accounts, "")
require.NoError(err)