Skip to content

Commit

Permalink
authz: Update MsgGrant proto (#9280)
Browse files Browse the repository at this point in the history
* adding GetAuthorization test

* update MsgGrant proto

* update comment
  • Loading branch information
robert-zaremba authored May 10, 2021
1 parent b635ae2 commit 9038dfe
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 148 deletions.
3 changes: 1 addition & 2 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1323,8 +1323,7 @@ account with the provided expiration time.
| ----- | ---- | ----- | ----------- |
| `granter` | [string](#string) | | |
| `grantee` | [string](#string) | | |
| `authorization` | [google.protobuf.Any](#google.protobuf.Any) | | |
| `expiration` | [google.protobuf.Timestamp](#google.protobuf.Timestamp) | | |
| `grant` | [Grant](#cosmos.authz.v1beta1.Grant) | | |



Expand Down
4 changes: 2 additions & 2 deletions proto/cosmos/authz/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/any.proto";
import "cosmos/base/abci/v1beta1/abci.proto";
import "cosmos/authz/v1beta1/authz.proto";

option go_package = "github.com/cosmos/cosmos-sdk/x/authz";
option (gogoproto.goproto_getters_all) = false;
Expand All @@ -32,8 +33,7 @@ message MsgGrant {
string granter = 1;
string grantee = 2;

google.protobuf.Any authorization = 3 [(cosmos_proto.accepts_interface) = "Authorization"];
google.protobuf.Timestamp expiration = 4 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
cosmos.authz.v1beta1.Grant grant = 3 [(gogoproto.nullable) = false];
}

// MsgExecResponse defines the Msg/MsgExecResponse response type.
Expand Down
41 changes: 28 additions & 13 deletions x/authz/authorization_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,56 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// NewGrant returns new AuthrizationGrant
func NewGrant(authorization Authorization, expiration time.Time) (Grant, error) {
auth := Grant{
// NewGrant returns new Grant
func NewGrant(a Authorization, expiration time.Time) (Grant, error) {
g := Grant{
Expiration: expiration,
}
msg, ok := authorization.(proto.Message)
msg, ok := a.(proto.Message)
if !ok {
return Grant{}, sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", authorization)
return Grant{}, sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", a)
}

any, err := cdctypes.NewAnyWithValue(msg)
if err != nil {
return Grant{}, err
}
g.Authorization = any

auth.Authorization = any

return auth, nil
return g, nil
}

var (
_ cdctypes.UnpackInterfacesMessage = &Grant{}
)

// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (auth Grant) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error {
func (g Grant) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error {
var authorization Authorization
return unpacker.UnpackAny(auth.Authorization, &authorization)
return unpacker.UnpackAny(g.Authorization, &authorization)
}

// GetAuthorization returns the cached value from the Grant.Authorization if present.
func (auth Grant) GetAuthorization() Authorization {
authorization, ok := auth.Authorization.GetCachedValue().(Authorization)
func (g Grant) GetAuthorization() Authorization {
if g.Authorization == nil {
return nil
}
a, ok := g.Authorization.GetCachedValue().(Authorization)
if !ok {
return nil
}
return authorization
return a
}

func (g Grant) ValidateBasic() error {
if g.Expiration.Unix() < time.Now().Unix() {
return sdkerrors.Wrap(ErrInvalidExpirationTime, "Time can't be in the past")
}

av := g.Authorization.GetCachedValue()
a, ok := av.(Authorization)
if !ok {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "expected %T, got %T", (Authorization)(nil), av)
}
return a.ValidateBasic()
}
2 changes: 1 addition & 1 deletion x/authz/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (k Keeper) Grant(goCtx context.Context, msg *authz.MsgGrant) (*authz.MsgGra
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "%s doesn't exist.", t)
}

err = k.SaveGrant(ctx, grantee, granter, authorization, msg.Expiration)
err = k.SaveGrant(ctx, grantee, granter, authorization, msg.Grant.Expiration)
if err != nil {
return nil, err
}
Expand Down
29 changes: 7 additions & 22 deletions x/authz/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ var (
//nolint:interfacer
func NewMsgGrant(granter sdk.AccAddress, grantee sdk.AccAddress, a Authorization, expiration time.Time) (*MsgGrant, error) {
m := &MsgGrant{
Granter: granter.String(),
Grantee: grantee.String(),
Expiration: expiration,
Granter: granter.String(),
Grantee: grantee.String(),
Grant: Grant{Expiration: expiration},
}
err := m.SetAuthorization(a)
if err != nil {
Expand Down Expand Up @@ -57,26 +57,12 @@ func (msg MsgGrant) ValidateBasic() error {
if granter.Equals(grantee) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "granter and grantee cannot be same")
}

if msg.Expiration.Unix() < time.Now().Unix() {
return sdkerrors.Wrap(ErrInvalidExpirationTime, "Time can't be in the past")
}

av := msg.Authorization.GetCachedValue()
a, ok := av.(Authorization)
if !ok {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "expected %T, got %T", (Authorization)(nil), av)
}
return a.ValidateBasic()
return msg.Grant.ValidateBasic()
}

// GetAuthorization returns the cache value from the MsgGrant.Authorization if present.
func (msg *MsgGrant) GetAuthorization() Authorization {
a, ok := msg.Authorization.GetCachedValue().(Authorization)
if !ok {
return nil
}
return a
return msg.Grant.GetAuthorization()
}

// SetAuthorization converts Authorization to any and adds it to MsgGrant.Authorization.
Expand All @@ -89,7 +75,7 @@ func (msg *MsgGrant) SetAuthorization(a Authorization) error {
if err != nil {
return err
}
msg.Authorization = any
msg.Grant.Authorization = any
return nil
}

Expand All @@ -108,8 +94,7 @@ func (msg MsgExec) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error {

// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (msg MsgGrant) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error {
var a Authorization
return unpacker.UnpackAny(msg.Authorization, &a)
return msg.Grant.UnpackInterfaces(unpacker)
}

// NewMsgRevoke creates a new MsgRevoke
Expand Down
18 changes: 18 additions & 0 deletions x/authz/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/stretchr/testify/require"

cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand Down Expand Up @@ -97,3 +98,20 @@ func TestMsgGrantAuthorization(t *testing.T) {
}
}
}

func TestMsgGrantGetAuthorization(t *testing.T) {
require := require.New(t)

m := authz.MsgGrant{}
require.Nil(m.GetAuthorization())

g := authz.GenericAuthorization{Msg: "some_type"}
var err error
m.Grant.Authorization, err = cdctypes.NewAnyWithValue(&g)
require.NoError(err)
require.Equal(m.GetAuthorization(), &g)

g = authz.GenericAuthorization{Msg: "some_type2"}
m.SetAuthorization(&g)
require.Equal(m.GetAuthorization(), &g)
}
Loading

0 comments on commit 9038dfe

Please sign in to comment.