Skip to content

Commit ec3e2b4

Browse files
feat: Add amino support for x/authz and x/feegrant (cosmos#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 <[email protected]> * Address reviews Co-authored-by: likhita-809 <[email protected]>
1 parent fc6e3d6 commit ec3e2b4

File tree

11 files changed

+231
-56
lines changed

11 files changed

+231
-56
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ if input key is empty, or input data contains empty key.
5555
* [\#9133](https://github.com/cosmos/cosmos-sdk/pull/9133) Added hooks for governance actions.
5656
* (x/staking) [\#9214](https://github.com/cosmos/cosmos-sdk/pull/9214) Added `new_shares` attribute inside `EventTypeDelegate` event.
5757
* [\#9382](https://github.com/cosmos/cosmos-sdk/pull/9382) feat: add Dec.Float64() function.
58+
* [\#9457](https://github.com/cosmos/cosmos-sdk/pull/9457) Add amino support for x/authz and x/feegrant Msgs.
5859

5960
### Client Breaking Changes
6061

x/authz/client/testutil/tx.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func (s *IntegrationTestSuite) TearDownSuite() {
7979

8080
var typeMsgSend = bank.SendAuthorization{}.MsgTypeURL()
8181
var typeMsgVote = sdk.MsgTypeURL(&govtypes.MsgVote{})
82+
var typeMsgSubmitProposal = sdk.MsgTypeURL(&govtypes.MsgSubmitProposal{})
8283

8384
func (s *IntegrationTestSuite) TestCLITxGrantAuthorization() {
8485
val := s.network.Validators[0]
@@ -258,6 +259,22 @@ func (s *IntegrationTestSuite) TestCLITxGrantAuthorization() {
258259
0,
259260
false,
260261
},
262+
{
263+
"Valid tx with amino",
264+
[]string{
265+
grantee.String(),
266+
"generic",
267+
fmt.Sprintf("--%s=%s", cli.FlagMsgType, typeMsgVote),
268+
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
269+
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
270+
fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours),
271+
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
272+
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
273+
fmt.Sprintf("--%s=%s", flags.FlagSignMode, flags.SignModeLegacyAminoJSON),
274+
},
275+
0,
276+
false,
277+
},
261278
}
262279

263280
for _, tc := range testCases {
@@ -324,6 +341,23 @@ func (s *IntegrationTestSuite) TestCmdRevokeAuthorizations() {
324341
)
325342
s.Require().NoError(err)
326343

344+
// generic-authorization used for amino testing
345+
_, err = ExecGrant(
346+
val,
347+
[]string{
348+
grantee.String(),
349+
"generic",
350+
fmt.Sprintf("--%s=%s", cli.FlagMsgType, typeMsgSubmitProposal),
351+
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
352+
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address),
353+
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
354+
fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours),
355+
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
356+
fmt.Sprintf("--%s=%s", flags.FlagSignMode, flags.SignModeLegacyAminoJSON),
357+
},
358+
)
359+
s.Require().NoError(err)
360+
327361
testCases := []struct {
328362
name string
329363
args []string
@@ -381,6 +415,20 @@ func (s *IntegrationTestSuite) TestCmdRevokeAuthorizations() {
381415
&sdk.TxResponse{}, 0,
382416
false,
383417
},
418+
{
419+
"Valid tx with amino",
420+
[]string{
421+
grantee.String(),
422+
typeMsgSubmitProposal,
423+
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
424+
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
425+
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
426+
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
427+
fmt.Sprintf("--%s=%s", flags.FlagSignMode, flags.SignModeLegacyAminoJSON),
428+
},
429+
&sdk.TxResponse{}, 0,
430+
false,
431+
},
384432
}
385433
for _, tc := range testCases {
386434
tc := tc
@@ -509,6 +557,19 @@ func (s *IntegrationTestSuite) TestNewExecGenericAuthorized() {
509557
0,
510558
false,
511559
},
560+
{
561+
"valid tx with amino",
562+
[]string{
563+
execMsg.Name(),
564+
fmt.Sprintf("--%s=%s", flags.FlagFrom, grantee.String()),
565+
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
566+
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
567+
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
568+
fmt.Sprintf("--%s=%s", flags.FlagSignMode, flags.SignModeLegacyAminoJSON),
569+
},
570+
&sdk.TxResponse{}, 0,
571+
false,
572+
},
512573
}
513574

514575
for _, tc := range testCases {

x/authz/module/module.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,8 @@ func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
187187

188188
// WeightedOperations returns the all the gov module operations with their respective weights.
189189
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
190-
protoCdc := codec.NewProtoCodec(am.registry)
191190
return simulation.WeightedOperations(
192191
simState.AppParams, simState.Cdc,
193-
am.accountKeeper, am.bankKeeper, am.keeper, am.cdc, protoCdc,
192+
am.accountKeeper, am.bankKeeper, am.keeper, am.cdc,
194193
)
195194
}

x/authz/msgs.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@ import (
55

66
"github.com/gogo/protobuf/proto"
77

8+
"github.com/cosmos/cosmos-sdk/codec/legacy"
89
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
910
sdk "github.com/cosmos/cosmos-sdk/types"
1011
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
12+
"github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx"
1113
)
1214

1315
var (
1416
_ sdk.Msg = &MsgGrant{}
1517
_ sdk.Msg = &MsgRevoke{}
1618
_ sdk.Msg = &MsgExec{}
1719

20+
// For amino support.
21+
_ legacytx.LegacyMsg = &MsgGrant{}
22+
_ legacytx.LegacyMsg = &MsgRevoke{}
23+
_ legacytx.LegacyMsg = &MsgExec{}
24+
1825
_ cdctypes.UnpackInterfacesMessage = &MsgGrant{}
1926
_ cdctypes.UnpackInterfacesMessage = &MsgExec{}
2027
)
@@ -60,6 +67,21 @@ func (msg MsgGrant) ValidateBasic() error {
6067
return msg.Grant.ValidateBasic()
6168
}
6269

70+
// Type implements the LegacyMsg.Type method.
71+
func (msg MsgGrant) Type() string {
72+
return sdk.MsgTypeURL(&msg)
73+
}
74+
75+
// Route implements the LegacyMsg.Route method.
76+
func (msg MsgGrant) Route() string {
77+
return sdk.MsgTypeURL(&msg)
78+
}
79+
80+
// GetSignBytes implements the LegacyMsg.GetSignBytes method.
81+
func (msg MsgGrant) GetSignBytes() []byte {
82+
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
83+
}
84+
6385
// GetAuthorization returns the cache value from the MsgGrant.Authorization if present.
6486
func (msg *MsgGrant) GetAuthorization() Authorization {
6587
return msg.Grant.GetAuthorization()
@@ -138,6 +160,21 @@ func (msg MsgRevoke) ValidateBasic() error {
138160
return nil
139161
}
140162

163+
// Type implements the LegacyMsg.Type method.
164+
func (msg MsgRevoke) Type() string {
165+
return sdk.MsgTypeURL(&msg)
166+
}
167+
168+
// Route implements the LegacyMsg.Route method.
169+
func (msg MsgRevoke) Route() string {
170+
return sdk.MsgTypeURL(&msg)
171+
}
172+
173+
// GetSignBytes implements the LegacyMsg.GetSignBytes method.
174+
func (msg MsgRevoke) GetSignBytes() []byte {
175+
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
176+
}
177+
141178
// NewMsgExec creates a new MsgExecAuthorized
142179
//nolint:interfacer
143180
func NewMsgExec(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExec {
@@ -193,3 +230,18 @@ func (msg MsgExec) ValidateBasic() error {
193230

194231
return nil
195232
}
233+
234+
// Type implements the LegacyMsg.Type method.
235+
func (msg MsgExec) Type() string {
236+
return sdk.MsgTypeURL(&msg)
237+
}
238+
239+
// Route implements the LegacyMsg.Route method.
240+
func (msg MsgExec) Route() string {
241+
return sdk.MsgTypeURL(&msg)
242+
}
243+
244+
// GetSignBytes implements the LegacyMsg.GetSignBytes method.
245+
func (msg MsgExec) GetSignBytes() []byte {
246+
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
247+
}

x/authz/simulation/operations.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const (
4545

4646
// WeightedOperations returns all the operations from the module with their respective weights
4747
func WeightedOperations(
48-
appParams simtypes.AppParams, cdc codec.JSONCodec, ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keeper, appCdc cdctypes.AnyUnpacker, protoCdc *codec.ProtoCodec) simulation.WeightedOperations {
48+
appParams simtypes.AppParams, cdc codec.JSONCodec, ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keeper, appCdc cdctypes.AnyUnpacker) simulation.WeightedOperations {
4949

5050
var (
5151
weightMsgGrant int
@@ -74,22 +74,21 @@ func WeightedOperations(
7474
return simulation.WeightedOperations{
7575
simulation.NewWeightedOperation(
7676
weightMsgGrant,
77-
SimulateMsgGrant(ak, bk, k, protoCdc),
77+
SimulateMsgGrant(ak, bk, k),
7878
),
7979
simulation.NewWeightedOperation(
8080
weightRevoke,
81-
SimulateMsgRevoke(ak, bk, k, protoCdc),
81+
SimulateMsgRevoke(ak, bk, k),
8282
),
8383
simulation.NewWeightedOperation(
8484
weightExec,
85-
SimulateMsgExec(ak, bk, k, appCdc, protoCdc),
85+
SimulateMsgExec(ak, bk, k, appCdc),
8686
),
8787
}
8888
}
8989

9090
// SimulateMsgGrant generates a MsgGrant with random values.
91-
func SimulateMsgGrant(ak authz.AccountKeeper, bk authz.BankKeeper, _ keeper.Keeper,
92-
protoCdc *codec.ProtoCodec) simtypes.Operation {
91+
func SimulateMsgGrant(ak authz.AccountKeeper, bk authz.BankKeeper, _ keeper.Keeper) simtypes.Operation {
9392
return func(
9493
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
9594
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
@@ -136,7 +135,7 @@ func SimulateMsgGrant(ak authz.AccountKeeper, bk authz.BankKeeper, _ keeper.Keep
136135
if err != nil {
137136
return simtypes.NoOpMsg(authz.ModuleName, sdk.MsgTypeURL(msg), "unable to deliver tx"), nil, err
138137
}
139-
return simtypes.NewOperationMsg(msg, true, "", protoCdc), nil, err
138+
return simtypes.NewOperationMsg(msg, true, "", nil), nil, err
140139
}
141140
}
142141

@@ -149,7 +148,7 @@ func generateRandomAuthorization(r *rand.Rand, spendLimit sdk.Coins) authz.Autho
149148
}
150149

151150
// SimulateMsgRevoke generates a MsgRevoke with random values.
152-
func SimulateMsgRevoke(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keeper, protoCdc *codec.ProtoCodec) simtypes.Operation {
151+
func SimulateMsgRevoke(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keeper) simtypes.Operation {
153152
return func(
154153
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
155154
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
@@ -203,12 +202,12 @@ func SimulateMsgRevoke(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Kee
203202
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgRevoke, "unable to deliver tx"), nil, err
204203
}
205204

206-
return simtypes.NewOperationMsg(&msg, true, "", protoCdc), nil, nil
205+
return simtypes.NewOperationMsg(&msg, true, "", nil), nil, nil
207206
}
208207
}
209208

210209
// SimulateMsgExec generates a MsgExec with random values.
211-
func SimulateMsgExec(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keeper, cdc cdctypes.AnyUnpacker, protoCdc *codec.ProtoCodec) simtypes.Operation {
210+
func SimulateMsgExec(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keeper, cdc cdctypes.AnyUnpacker) simtypes.Operation {
212211
return func(
213212
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
214213
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
@@ -293,6 +292,6 @@ func SimulateMsgExec(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keepe
293292
if err != nil {
294293
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, "unmarshal error"), nil, err
295294
}
296-
return simtypes.NewOperationMsg(&msg, true, "success", protoCdc), nil, nil
295+
return simtypes.NewOperationMsg(&msg, true, "success", nil), nil, nil
297296
}
298297
}

x/authz/simulation/operations_test.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
abci "github.com/tendermint/tendermint/abci/types"
1111
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
1212

13-
"github.com/cosmos/cosmos-sdk/codec"
1413
"github.com/cosmos/cosmos-sdk/simapp"
1514
sdk "github.com/cosmos/cosmos-sdk/types"
1615
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
@@ -22,25 +21,23 @@ import (
2221
type SimTestSuite struct {
2322
suite.Suite
2423

25-
ctx sdk.Context
26-
app *simapp.SimApp
27-
protoCdc *codec.ProtoCodec
24+
ctx sdk.Context
25+
app *simapp.SimApp
2826
}
2927

3028
func (suite *SimTestSuite) SetupTest() {
3129
checkTx := false
3230
app := simapp.Setup(checkTx)
3331
suite.app = app
3432
suite.ctx = app.BaseApp.NewContext(checkTx, tmproto.Header{})
35-
suite.protoCdc = codec.NewProtoCodec(suite.app.InterfaceRegistry())
3633
}
3734

3835
func (suite *SimTestSuite) TestWeightedOperations() {
3936
cdc := suite.app.AppCodec()
4037
appParams := make(simtypes.AppParams)
4138

42-
weightesOps := simulation.WeightedOperations(appParams, cdc, suite.app.AccountKeeper,
43-
suite.app.BankKeeper, suite.app.AuthzKeeper, cdc, suite.protoCdc,
39+
weightedOps := simulation.WeightedOperations(appParams, cdc, suite.app.AccountKeeper,
40+
suite.app.BankKeeper, suite.app.AuthzKeeper, cdc,
4441
)
4542

4643
// setup 3 accounts
@@ -58,7 +55,7 @@ func (suite *SimTestSuite) TestWeightedOperations() {
5855
{simulation.WeightExec, authz.ModuleName, simulation.TypeMsgExec},
5956
}
6057

61-
for i, w := range weightesOps {
58+
for i, w := range weightedOps {
6259
operationMsg, _, _ := w.Op()(r, suite.app.BaseApp, suite.ctx, accs, "")
6360
// the following checks are very much dependent from the ordering of the output given
6461
// by WeightedOperations. if the ordering in WeightedOperations changes some tests
@@ -104,7 +101,7 @@ func (suite *SimTestSuite) TestSimulateGrant() {
104101
grantee := accounts[1]
105102

106103
// execute operation
107-
op := simulation.SimulateMsgGrant(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.AuthzKeeper, suite.protoCdc)
104+
op := simulation.SimulateMsgGrant(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.AuthzKeeper)
108105
operationMsg, futureOperations, err := op(r, suite.app.BaseApp, ctx, accounts, "")
109106
suite.Require().NoError(err)
110107

@@ -141,7 +138,7 @@ func (suite *SimTestSuite) TestSimulateRevoke() {
141138
suite.Require().NoError(err)
142139

143140
// execute operation
144-
op := simulation.SimulateMsgRevoke(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.AuthzKeeper, suite.protoCdc)
141+
op := simulation.SimulateMsgRevoke(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.AuthzKeeper)
145142
operationMsg, futureOperations, err := op(r, suite.app.BaseApp, suite.ctx, accounts, "")
146143
suite.Require().NoError(err)
147144

@@ -176,7 +173,7 @@ func (suite *SimTestSuite) TestSimulateExec() {
176173
suite.Require().NoError(err)
177174

178175
// execute operation
179-
op := simulation.SimulateMsgExec(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.AuthzKeeper, suite.app.AppCodec(), suite.protoCdc)
176+
op := simulation.SimulateMsgExec(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.AuthzKeeper, suite.app.AppCodec())
180177
operationMsg, futureOperations, err := op(r, suite.app.BaseApp, suite.ctx, accounts, "")
181178
suite.Require().NoError(err)
182179

0 commit comments

Comments
 (0)