Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 8 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ if input key is empty, or input data contains empty key.
* [\#9133](https://github.com/cosmos/cosmos-sdk/pull/9133) Added hooks for governance actions.
* (x/staking) [\#9214](https://github.com/cosmos/cosmos-sdk/pull/9214) Added `new_shares` attribute inside `EventTypeDelegate` event.
* [\#9382](https://github.com/cosmos/cosmos-sdk/pull/9382) feat: add Dec.Float64() function.
* [\#9457](https://github.com/cosmos/cosmos-sdk/pull/9457) Add amino support for x/authz and x/feegrant Msgs.

### Client Breaking Changes

Expand Down
60 changes: 60 additions & 0 deletions x/authz/client/testutil/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (s *IntegrationTestSuite) TearDownSuite() {

var typeMsgSend = bank.SendAuthorization{}.MsgTypeURL()
var typeMsgVote = sdk.MsgTypeURL(&govtypes.MsgVote{})
var typeMsgSubmitProposal = sdk.MsgTypeURL(&govtypes.MsgSubmitProposal{})

func (s *IntegrationTestSuite) TestCLITxGrantAuthorization() {
val := s.network.Validators[0]
Expand Down Expand Up @@ -258,6 +259,22 @@ func (s *IntegrationTestSuite) TestCLITxGrantAuthorization() {
0,
false,
},
{
"Valid tx with amino",
[]string{
grantee.String(),
"generic",
fmt.Sprintf("--%s=%s", cli.FlagMsgType, typeMsgVote),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
fmt.Sprintf("--%s=%s", flags.FlagSignMode, flags.SignModeLegacyAminoJSON),
},
0,
false,
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -324,6 +341,22 @@ func (s *IntegrationTestSuite) TestCmdRevokeAuthorizations() {
)
s.Require().NoError(err)

// generic-authorization used for amino testing
_, err = ExecGrant(
val,
[]string{
grantee.String(),
"generic",
fmt.Sprintf("--%s=%s", cli.FlagMsgType, typeMsgSubmitProposal),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
},
)
s.Require().NoError(err)

testCases := []struct {
name string
args []string
Expand Down Expand Up @@ -381,6 +414,20 @@ func (s *IntegrationTestSuite) TestCmdRevokeAuthorizations() {
&sdk.TxResponse{}, 0,
false,
},
{
"Valid tx with amino",
[]string{
grantee.String(),
typeMsgSubmitProposal,
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
fmt.Sprintf("--%s=%s", flags.FlagSignMode, flags.SignModeLegacyAminoJSON),
},
&sdk.TxResponse{}, 0,
false,
},
}
for _, tc := range testCases {
tc := tc
Expand Down Expand Up @@ -509,6 +556,19 @@ func (s *IntegrationTestSuite) TestNewExecGenericAuthorized() {
0,
false,
},
{
"valid tx with amino",
[]string{
execMsg.Name(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, grantee.String()),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagSignMode, flags.SignModeLegacyAminoJSON),
},
&sdk.TxResponse{}, 0,
false,
},
}

for _, tc := range testCases {
Expand Down
52 changes: 52 additions & 0 deletions x/authz/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ import (

"github.com/gogo/protobuf/proto"

"github.com/cosmos/cosmos-sdk/codec/legacy"
cdctypes "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 = &MsgGrant{}
_ sdk.Msg = &MsgRevoke{}
_ sdk.Msg = &MsgExec{}

// For amino support.
_ legacytx.LegacyMsg = &MsgGrant{}
_ legacytx.LegacyMsg = &MsgRevoke{}
_ legacytx.LegacyMsg = &MsgExec{}

_ cdctypes.UnpackInterfacesMessage = &MsgGrant{}
_ cdctypes.UnpackInterfacesMessage = &MsgExec{}
)
Expand Down Expand Up @@ -60,6 +67,21 @@ func (msg MsgGrant) ValidateBasic() error {
return msg.Grant.ValidateBasic()
}

// Type implements the LegacyMsg.Type method.
func (msg MsgGrant) Type() string {
return sdk.MsgTypeURL(&msg)
}

// Route implements the LegacyMsg.Type method.
func (msg MsgGrant) Route() string {
return sdk.MsgTypeURL(&msg)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Authz has no legacy msg handler, so this is dead code. I'm actually inclined to panic here with a user friendly message saying so. wdyt?

}

// Route implements the LegacyMsg.Type method.
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
func (msg MsgGrant) GetSignBytes() []byte {
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
}

// GetAuthorization returns the cache value from the MsgGrant.Authorization if present.
func (msg *MsgGrant) GetAuthorization() Authorization {
return msg.Grant.GetAuthorization()
Expand Down Expand Up @@ -138,6 +160,21 @@ func (msg MsgRevoke) ValidateBasic() error {
return nil
}

// Type implements the LegacyMsg.Type method.
func (msg MsgRevoke) Type() string {
return sdk.MsgTypeURL(&msg)
}

// Route implements the LegacyMsg.Type method.
func (msg MsgRevoke) Route() string {
return sdk.MsgTypeURL(&msg)
}

// Route implements the LegacyMsg.Type method.
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
func (msg MsgRevoke) GetSignBytes() []byte {
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
}

// NewMsgExec creates a new MsgExecAuthorized
//nolint:interfacer
func NewMsgExec(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExec {
Expand Down Expand Up @@ -193,3 +230,18 @@ func (msg MsgExec) ValidateBasic() error {

return nil
}

// Type implements the LegacyMsg.Type method.
func (msg MsgExec) Type() string {
return sdk.MsgTypeURL(&msg)
}

// Route implements the LegacyMsg.Type method.
func (msg MsgExec) Route() string {
return sdk.MsgTypeURL(&msg)
}

// Route implements the LegacyMsg.Type method.
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
func (msg MsgExec) GetSignBytes() []byte {
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
}
60 changes: 49 additions & 11 deletions x/feegrant/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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() {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
38 changes: 36 additions & 2 deletions x/feegrant/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.Type method.
func (msg MsgGrantAllowance) Route() string {
return sdk.MsgTypeURL(&msg)
}

// Route implements the LegacyMsg.Type method.
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down Expand Up @@ -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.Type method.
func (msg MsgRevokeAllowance) Route() string {
return sdk.MsgTypeURL(&msg)
}

// Route implements the LegacyMsg.Type method.
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
func (msg MsgRevokeAllowance) GetSignBytes() []byte {
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
}