From 42f294934fac19b7fe922231ea4bb9245608d5b7 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Thu, 25 Mar 2021 16:17:25 +0530 Subject: [PATCH 01/13] WIP --- x/authz/exported/authorizations.go | 8 ++-- x/authz/keeper/keeper.go | 2 +- x/authz/simulation/operations.go | 2 +- x/authz/types/generic_authorization.go | 26 +++++++++--- x/authz/types/generic_authorization_test.go | 21 +++++++++ x/bank/types/send_authorization.go | 16 +++++-- x/bank/types/send_authorization_test.go | 37 ++++++++++++++++ x/staking/types/authz.go | 18 +++++--- x/staking/types/authz_test.go | 47 +++++++++++++++------ 9 files changed, 143 insertions(+), 34 deletions(-) create mode 100644 x/authz/types/generic_authorization_test.go create mode 100644 x/bank/types/send_authorization_test.go diff --git a/x/authz/exported/authorizations.go b/x/authz/exported/authorizations.go index d7cca5ae79d0..32fea0b0b3a8 100644 --- a/x/authz/exported/authorizations.go +++ b/x/authz/exported/authorizations.go @@ -3,8 +3,6 @@ package exported import ( "github.com/gogo/protobuf/proto" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -17,5 +15,9 @@ type Authorization interface { // Accept determines whether this grant permits the provided sdk.ServiceMsg to be performed, and if // so provides an upgraded authorization instance. - Accept(msg sdk.ServiceMsg, block tmproto.Header) (updated Authorization, delete bool, err error) + Accept(ctx sdk.Context, msg sdk.ServiceMsg) (updated Authorization, delete bool, err error) + + // ValidateBasic does a simple validation check that + // doesn't require access to any other information. + ValidateBasic() error } diff --git a/x/authz/keeper/keeper.go b/x/authz/keeper/keeper.go index 33d6248f7123..dcbee170cb1e 100644 --- a/x/authz/keeper/keeper.go +++ b/x/authz/keeper/keeper.go @@ -87,7 +87,7 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, service if authorization == nil { return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "authorization not found") } - updated, del, err := authorization.Accept(serviceMsg, ctx.BlockHeader()) + updated, del, err := authorization.Accept(ctx, serviceMsg) if err != nil { return nil, err } diff --git a/x/authz/simulation/operations.go b/x/authz/simulation/operations.go index 959b0c06fe65..e13cbaa338dd 100644 --- a/x/authz/simulation/operations.go +++ b/x/authz/simulation/operations.go @@ -249,7 +249,7 @@ func SimulateMsgExecuteAuthorized(ak types.AccountKeeper, bk types.BankKeeper, k msg := types.NewMsgExecAuthorized(grantee.Address, []sdk.ServiceMsg{execMsg}) sendGrant := targetGrant.Authorization.GetCachedValue().(*banktype.SendAuthorization) - _, _, err = sendGrant.Accept(execMsg, ctx.BlockHeader()) + _, _, err = sendGrant.Accept(ctx, execMsg) if err != nil { return simtypes.NoOpMsg(types.ModuleName, TypeMsgExecDelegated, err.Error()), nil, nil } diff --git a/x/authz/types/generic_authorization.go b/x/authz/types/generic_authorization.go index 672260c67f34..7f92fc517b7c 100644 --- a/x/authz/types/generic_authorization.go +++ b/x/authz/types/generic_authorization.go @@ -1,9 +1,11 @@ package types import ( - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + "strings" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/errors" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/x/authz/exported" ) @@ -19,11 +21,25 @@ func NewGenericAuthorization(methodName string) *GenericAuthorization { } // MethodName implements Authorization.MethodName. -func (cap GenericAuthorization) MethodName() string { - return cap.MessageName +func (authorization GenericAuthorization) MethodName() string { + return authorization.MessageName } // Accept implements Authorization.Accept. -func (cap GenericAuthorization) Accept(msg sdk.ServiceMsg, block tmproto.Header) (updated exported.Authorization, delete bool, err error) { - return &cap, false, nil +func (authorization GenericAuthorization) Accept(ctx sdk.Context, msg sdk.ServiceMsg) (updated exported.Authorization, delete bool, err error) { + return &authorization, false, nil +} + +// ValidateBasic implements Authorization.ValidateBasic. +func (authorization GenericAuthorization) ValidateBasic() error { + if !isServiceMsg(authorization.MessageName) { + return sdkerrors.Wrapf(errors.ErrInvalidType, " %s is not a valid service msg", authorization.MessageName) + } + return nil +} + +// isServiceMsg checks if a type URL corresponds to a service method name, +// i.e. /cosmos.bank.Msg/Send vs /cosmos.bank.MsgSend +func isServiceMsg(typeURL string) bool { + return strings.Count(typeURL, "/") >= 2 } diff --git a/x/authz/types/generic_authorization_test.go b/x/authz/types/generic_authorization_test.go new file mode 100644 index 000000000000..da483050d170 --- /dev/null +++ b/x/authz/types/generic_authorization_test.go @@ -0,0 +1,21 @@ +package types_test + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/x/authz/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/stretchr/testify/require" +) + +func TestGenericAuthorization(t *testing.T) { + + // validateBasic returns error not a valida service msg + authorization := types.NewGenericAuthorization(banktypes.TypeMsgSend) + require.Error(t, authorization.ValidateBasic()) + + // valid service msg + authorization = types.NewGenericAuthorization(banktypes.SendAuthorization{}.MethodName()) + require.NoError(t, authorization.ValidateBasic()) + require.Equal(t, banktypes.SendAuthorization{}.MethodName(), authorization.MessageName) +} diff --git a/x/bank/types/send_authorization.go b/x/bank/types/send_authorization.go index ccd755c1fce5..7f53cd4c8856 100644 --- a/x/bank/types/send_authorization.go +++ b/x/bank/types/send_authorization.go @@ -3,9 +3,8 @@ package types import ( "reflect" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/errors" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" authz "github.com/cosmos/cosmos-sdk/x/authz/exported" ) @@ -27,7 +26,7 @@ func (authorization SendAuthorization) MethodName() string { } // Accept implements Authorization.Accept. -func (authorization SendAuthorization) Accept(msg sdk.ServiceMsg, block tmproto.Header) (updated authz.Authorization, delete bool, err error) { +func (authorization SendAuthorization) Accept(ctx sdk.Context, msg sdk.ServiceMsg) (updated authz.Authorization, delete bool, err error) { if reflect.TypeOf(msg.Request) == reflect.TypeOf(&MsgSend{}) { msg, ok := msg.Request.(*MsgSend) if ok { @@ -44,3 +43,14 @@ func (authorization SendAuthorization) Accept(msg sdk.ServiceMsg, block tmproto. } return nil, false, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "type mismatch") } + +// ValidateBasic implements Authorization.ValidateBasic. +func (authorization SendAuthorization) ValidateBasic() error { + if authorization.SpendLimit == nil { + return sdkerrors.Wrapf(errors.ErrInvalidCoins, "spend limit cannot be nil") + } + if !authorization.SpendLimit.IsAllPositive() { + return sdkerrors.Wrapf(errors.ErrInvalidCoins, "spend limit cannot be negitive") + } + return nil +} diff --git a/x/bank/types/send_authorization_test.go b/x/bank/types/send_authorization_test.go new file mode 100644 index 000000000000..b6448710b815 --- /dev/null +++ b/x/bank/types/send_authorization_test.go @@ -0,0 +1,37 @@ +package types_test + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/simapp" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/stretchr/testify/require" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" +) + +var ( + posCoins = sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000))) + fromAddr = sdk.AccAddress("_____from _____") + toAddr = sdk.AccAddress("_______to________") +) + +func TestSendAuthorization(t *testing.T) { + app := simapp.Setup(false) + ctx := app.BaseApp.NewContext(false, tmproto.Header{}) + + authorization := types.NewSendAuthorization(posCoins) + require.Equal(t, authorization.MethodName(), "/cosmos.bank.v1beta1.Msg/Send") + require.NoError(t, authorization.ValidateBasic()) + send := types.NewMsgSend(fromAddr, toAddr, posCoins) + srvMsg := sdk.ServiceMsg{ + MethodName: "/cosmos.bank.v1beta1.Msg/Send", + Request: send, + } + require.NoError(t, authorization.ValidateBasic()) + updated, del, err := authorization.Accept(ctx, srvMsg) + require.NoError(t, err) + require.NotNil(t, del) + require.Nil(t, updated) + +} diff --git a/x/staking/types/authz.go b/x/staking/types/authz.go index e88288da4e2d..05fac9835f8d 100644 --- a/x/staking/types/authz.go +++ b/x/staking/types/authz.go @@ -1,18 +1,17 @@ package types import ( - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" authz "github.com/cosmos/cosmos-sdk/x/authz/exported" ) var ( - _ authz.Authorization = &StakeAuthorization{} - TypeDelegate = "/cosmos.staking.v1beta1.Msg/Delegate" - TypeUndelegate = "/cosmos.staking.v1beta1.Msg/Undelegate" - TypeBeginRedelegate = "/cosmos.staking.v1beta1.Msg/BeginRedelegate" + _ authz.Authorization = &StakeAuthorization{} + + TypeDelegate = "/cosmos.staking.v1beta1.Msg/Delegate" + TypeUndelegate = "/cosmos.staking.v1beta1.Msg/Undelegate" + TypeBeginRedelegate = "/cosmos.staking.v1beta1.Msg/BeginRedelegate" ) // NewStakeAuthorization creates a new StakeAuthorization object. @@ -46,8 +45,13 @@ func (authorization StakeAuthorization) MethodName() string { return authzType } +func (authorization StakeAuthorization) ValidateBasic() error { + // TODO + return nil +} + // Accept implements Authorization.Accept. -func (authorization StakeAuthorization) Accept(msg sdk.ServiceMsg, block tmproto.Header) (updated authz.Authorization, delete bool, err error) { +func (authorization StakeAuthorization) Accept(ctx sdk.Context, msg sdk.ServiceMsg) (updated authz.Authorization, delete bool, err error) { var validatorAddress string var amount sdk.Coin diff --git a/x/staking/types/authz_test.go b/x/staking/types/authz_test.go index 18709d9ecd51..60d4ec2066ec 100644 --- a/x/staking/types/authz_test.go +++ b/x/staking/types/authz_test.go @@ -5,12 +5,31 @@ import ( tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) +type AuthzTestSuite struct { + suite.Suite + + app *simapp.SimApp + ctx sdk.Context +} + +func TestAuthzTestSuite(t *testing.T) { + suite.Run(t, new(AuthzTestSuite)) +} + +func (suite *AuthzTestSuite) SetupTest() { + app := simapp.Setup(false) + ctx := app.BaseApp.NewContext(false, tmproto.Header{}) + suite.ctx = ctx + +} + var ( coin100 = sdk.NewInt64Coin("steak", 100) coin50 = sdk.NewInt64Coin("steak", 50) @@ -20,23 +39,23 @@ var ( val3 = sdk.ValAddress("_____validator3_____") ) -func TestAuthzAuthorizations(t *testing.T) { +func (suite *AuthzTestSuite) TestAuthzAuthorizations() { // verify MethodName delAuth, _ := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100) - require.Equal(t, delAuth.MethodName(), stakingtypes.TypeDelegate) + suite.Require().Equal(delAuth.MethodName(), stakingtypes.TypeDelegate) // error both allow & deny list _, err := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{val1}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100) - require.Error(t, err) + suite.Require().Error(err) // verify MethodName undelAuth, _ := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_UNDELEGATE, &coin100) - require.Equal(t, undelAuth.MethodName(), stakingtypes.TypeUndelegate) + suite.Require().Equal(undelAuth.MethodName(), stakingtypes.TypeUndelegate) // verify MethodName beginRedelAuth, _ := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_REDELEGATE, &coin100) - require.Equal(t, beginRedelAuth.MethodName(), stakingtypes.TypeBeginRedelegate) + suite.Require().Equal(beginRedelAuth.MethodName(), stakingtypes.TypeBeginRedelegate) validators1_2 := []string{val1.String(), val2.String()} @@ -240,18 +259,18 @@ func TestAuthzAuthorizations(t *testing.T) { for _, tc := range testCases { tc := tc - t.Run(tc.msg, func(t *testing.T) { + suite.Run(tc.msg, func() { delAuth, err := stakingtypes.NewStakeAuthorization(tc.allowed, tc.denied, tc.msgType, tc.limit) - require.NoError(t, err) - updated, del, err := delAuth.Accept(tc.srvMsg, tmproto.Header{}) + suite.Require().NoError(err) + updated, del, err := delAuth.Accept(suite.ctx, tc.srvMsg) if tc.expectErr { - require.Error(t, err) - require.Equal(t, tc.isDelete, del) + suite.Require().Error(err) + suite.Require().Equal(tc.isDelete, del) } else { - require.NoError(t, err) - require.Equal(t, tc.isDelete, del) + suite.Require().NoError(err) + suite.Require().Equal(tc.isDelete, del) if tc.updatedAuthorization != nil { - require.Equal(t, tc.updatedAuthorization.String(), updated.String()) + suite.Require().Equal(tc.updatedAuthorization.String(), updated.String()) } } }) From 5b616c1269a8a2c1dea6631642610e30882496a8 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Thu, 25 Mar 2021 22:20:08 +0530 Subject: [PATCH 02/13] Add consume gas --- x/authz/client/cli/tx.go | 4 +++ x/authz/client/cli/tx_test.go | 6 ++-- x/authz/types/generic_authorization.go | 3 +- x/bank/types/send_authorization.go | 5 ++- x/bank/types/send_authorization_test.go | 39 ++++++++++++++++---- x/staking/types/authz.go | 12 ++++++- x/staking/types/authz_test.go | 48 +++++++++---------------- 7 files changed, 69 insertions(+), 48 deletions(-) diff --git a/x/authz/client/cli/tx.go b/x/authz/client/cli/tx.go index 44df6f5e9aa4..dbebb946c6bf 100644 --- a/x/authz/client/cli/tx.go +++ b/x/authz/client/cli/tx.go @@ -160,6 +160,10 @@ Examples: return fmt.Errorf("invalid authorization type, %s", args[1]) } + if err = authorization.ValidateBasic(); err != nil { + return err + } + msg, err := types.NewMsgGrantAuthorization(clientCtx.GetFromAddress(), grantee, authorization, time.Unix(exp, 0)) if err != nil { return err diff --git a/x/authz/client/cli/tx_test.go b/x/authz/client/cli/tx_test.go index 179feca333d6..298b5167b7f4 100644 --- a/x/authz/client/cli/tx_test.go +++ b/x/authz/client/cli/tx_test.go @@ -1,5 +1,3 @@ -// +build norace - package cli_test import ( @@ -156,8 +154,8 @@ func (s *IntegrationTestSuite) TestCLITxGrantAuthorization() { fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours), }, - &sdk.TxResponse{}, 29, - false, + nil, 0, + true, }, { "failed with error both validators not allowed", diff --git a/x/authz/types/generic_authorization.go b/x/authz/types/generic_authorization.go index 7f92fc517b7c..48bc179685a1 100644 --- a/x/authz/types/generic_authorization.go +++ b/x/authz/types/generic_authorization.go @@ -4,7 +4,6 @@ import ( "strings" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/errors" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/x/authz/exported" ) @@ -33,7 +32,7 @@ func (authorization GenericAuthorization) Accept(ctx sdk.Context, msg sdk.Servic // ValidateBasic implements Authorization.ValidateBasic. func (authorization GenericAuthorization) ValidateBasic() error { if !isServiceMsg(authorization.MessageName) { - return sdkerrors.Wrapf(errors.ErrInvalidType, " %s is not a valid service msg", authorization.MessageName) + return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, " %s is not a valid service msg", authorization.MessageName) } return nil } diff --git a/x/bank/types/send_authorization.go b/x/bank/types/send_authorization.go index 7f53cd4c8856..82d1bf1e630b 100644 --- a/x/bank/types/send_authorization.go +++ b/x/bank/types/send_authorization.go @@ -4,7 +4,6 @@ import ( "reflect" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/errors" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" authz "github.com/cosmos/cosmos-sdk/x/authz/exported" ) @@ -47,10 +46,10 @@ func (authorization SendAuthorization) Accept(ctx sdk.Context, msg sdk.ServiceMs // ValidateBasic implements Authorization.ValidateBasic. func (authorization SendAuthorization) ValidateBasic() error { if authorization.SpendLimit == nil { - return sdkerrors.Wrapf(errors.ErrInvalidCoins, "spend limit cannot be nil") + return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "spend limit cannot be nil") } if !authorization.SpendLimit.IsAllPositive() { - return sdkerrors.Wrapf(errors.ErrInvalidCoins, "spend limit cannot be negitive") + return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "spend limit cannot be negitive") } return nil } diff --git a/x/bank/types/send_authorization_test.go b/x/bank/types/send_authorization_test.go index b6448710b815..055a0b1fb0aa 100644 --- a/x/bank/types/send_authorization_test.go +++ b/x/bank/types/send_authorization_test.go @@ -11,27 +11,54 @@ import ( ) var ( - posCoins = sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000))) - fromAddr = sdk.AccAddress("_____from _____") - toAddr = sdk.AccAddress("_______to________") + coins1000 = sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000))) + coins500 = sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(500))) + fromAddr = sdk.AccAddress("_____from _____") + toAddr = sdk.AccAddress("_______to________") ) func TestSendAuthorization(t *testing.T) { app := simapp.Setup(false) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) + authorization := types.NewSendAuthorization(coins1000) - authorization := types.NewSendAuthorization(posCoins) + t.Log("verify authorization returns valid method name") require.Equal(t, authorization.MethodName(), "/cosmos.bank.v1beta1.Msg/Send") require.NoError(t, authorization.ValidateBasic()) - send := types.NewMsgSend(fromAddr, toAddr, posCoins) + send := types.NewMsgSend(fromAddr, toAddr, coins1000) srvMsg := sdk.ServiceMsg{ MethodName: "/cosmos.bank.v1beta1.Msg/Send", Request: send, } require.NoError(t, authorization.ValidateBasic()) + + t.Log("verify updated authorization returns nil") updated, del, err := authorization.Accept(ctx, srvMsg) require.NoError(t, err) - require.NotNil(t, del) + require.True(t, del) require.Nil(t, updated) + authorization = types.NewSendAuthorization(coins1000) + require.Equal(t, authorization.MethodName(), "/cosmos.bank.v1beta1.Msg/Send") + require.NoError(t, authorization.ValidateBasic()) + send = types.NewMsgSend(fromAddr, toAddr, coins500) + srvMsg = sdk.ServiceMsg{ + MethodName: "/cosmos.bank.v1beta1.Msg/Send", + Request: send, + } + require.NoError(t, authorization.ValidateBasic()) + updated, del, err = authorization.Accept(ctx, srvMsg) + + t.Log("verify updated authorization returns remaining spent limit") + require.NoError(t, err) + require.False(t, del) + require.NotNil(t, updated) + sendAuth := types.NewSendAuthorization(coins500) + require.Equal(t, sendAuth.String(), updated.String()) + + t.Log("expect updated authorization nil after spending remaining amount") + updated, del, err = updated.Accept(ctx, srvMsg) + require.NoError(t, err) + require.True(t, del) + require.Nil(t, updated) } diff --git a/x/staking/types/authz.go b/x/staking/types/authz.go index 05fac9835f8d..d46ee6dd701e 100644 --- a/x/staking/types/authz.go +++ b/x/staking/types/authz.go @@ -6,6 +6,8 @@ import ( authz "github.com/cosmos/cosmos-sdk/x/authz/exported" ) +const gasCostPerIteration = uint64(1000) + var ( _ authz.Authorization = &StakeAuthorization{} @@ -46,7 +48,12 @@ func (authorization StakeAuthorization) MethodName() string { } func (authorization StakeAuthorization) ValidateBasic() error { - // TODO + if authorization.MaxTokens != nil && authorization.MaxTokens.IsNegative() { + return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "negative coin amount: %v", authorization.MaxTokens) + } + if authorization.GetValidators().Size() == 0 { + return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "validator set is empty") + } return nil } @@ -72,13 +79,16 @@ func (authorization StakeAuthorization) Accept(ctx sdk.Context, msg sdk.ServiceM isValidatorExists := false allowedList := authorization.GetAllowList().GetAddress() for _, validator := range allowedList { + ctx.GasMeter().ConsumeGas(gasCostPerIteration, "iteration: stake authorization allow list") if validator == validatorAddress { isValidatorExists = true break } } + denyList := authorization.GetDenyList().GetAddress() for _, validator := range denyList { + ctx.GasMeter().ConsumeGas(gasCostPerIteration, "iteration: stake authorization deny list") if validator == validatorAddress { return nil, false, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, " cannot delegate/undelegate to %s validator", validator) } diff --git a/x/staking/types/authz_test.go b/x/staking/types/authz_test.go index 60d4ec2066ec..d1489a8b6e76 100644 --- a/x/staking/types/authz_test.go +++ b/x/staking/types/authz_test.go @@ -5,31 +5,13 @@ import ( tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/stretchr/testify/suite" + "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) -type AuthzTestSuite struct { - suite.Suite - - app *simapp.SimApp - ctx sdk.Context -} - -func TestAuthzTestSuite(t *testing.T) { - suite.Run(t, new(AuthzTestSuite)) -} - -func (suite *AuthzTestSuite) SetupTest() { - app := simapp.Setup(false) - ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - suite.ctx = ctx - -} - var ( coin100 = sdk.NewInt64Coin("steak", 100) coin50 = sdk.NewInt64Coin("steak", 50) @@ -39,23 +21,25 @@ var ( val3 = sdk.ValAddress("_____validator3_____") ) -func (suite *AuthzTestSuite) TestAuthzAuthorizations() { +func TestAuthzAuthorizations(t *testing.T) { + app := simapp.Setup(false) + ctx := app.BaseApp.NewContext(false, tmproto.Header{}) // verify MethodName delAuth, _ := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100) - suite.Require().Equal(delAuth.MethodName(), stakingtypes.TypeDelegate) + require.Equal(t, delAuth.MethodName(), stakingtypes.TypeDelegate) // error both allow & deny list _, err := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{val1}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100) - suite.Require().Error(err) + require.Error(t, err) // verify MethodName undelAuth, _ := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_UNDELEGATE, &coin100) - suite.Require().Equal(undelAuth.MethodName(), stakingtypes.TypeUndelegate) + require.Equal(t, undelAuth.MethodName(), stakingtypes.TypeUndelegate) // verify MethodName beginRedelAuth, _ := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_REDELEGATE, &coin100) - suite.Require().Equal(beginRedelAuth.MethodName(), stakingtypes.TypeBeginRedelegate) + require.Equal(t, beginRedelAuth.MethodName(), stakingtypes.TypeBeginRedelegate) validators1_2 := []string{val1.String(), val2.String()} @@ -259,18 +243,18 @@ func (suite *AuthzTestSuite) TestAuthzAuthorizations() { for _, tc := range testCases { tc := tc - suite.Run(tc.msg, func() { + t.Run(tc.msg, func(t *testing.T) { delAuth, err := stakingtypes.NewStakeAuthorization(tc.allowed, tc.denied, tc.msgType, tc.limit) - suite.Require().NoError(err) - updated, del, err := delAuth.Accept(suite.ctx, tc.srvMsg) + require.NoError(t, err) + updated, del, err := delAuth.Accept(ctx, tc.srvMsg) if tc.expectErr { - suite.Require().Error(err) - suite.Require().Equal(tc.isDelete, del) + require.Error(t, err) + require.Equal(t, tc.isDelete, del) } else { - suite.Require().NoError(err) - suite.Require().Equal(tc.isDelete, del) + require.NoError(t, err) + require.Equal(t, tc.isDelete, del) if tc.updatedAuthorization != nil { - suite.Require().Equal(tc.updatedAuthorization.String(), updated.String()) + require.Equal(t, tc.updatedAuthorization.String(), updated.String()) } } }) From 6d40f037c22087ad3838125c4606eb08517ae904 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Thu, 25 Mar 2021 22:32:11 +0530 Subject: [PATCH 03/13] update authz.go --- x/staking/types/authz.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/x/staking/types/authz.go b/x/staking/types/authz.go index d46ee6dd701e..ff3407c7d504 100644 --- a/x/staking/types/authz.go +++ b/x/staking/types/authz.go @@ -51,9 +51,6 @@ func (authorization StakeAuthorization) ValidateBasic() error { if authorization.MaxTokens != nil && authorization.MaxTokens.IsNegative() { return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "negative coin amount: %v", authorization.MaxTokens) } - if authorization.GetValidators().Size() == 0 { - return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "validator set is empty") - } return nil } @@ -112,9 +109,9 @@ func (authorization StakeAuthorization) Accept(ctx sdk.Context, msg sdk.ServiceM } func validateAndBech32fy(allowed []sdk.ValAddress, denied []sdk.ValAddress) ([]string, []string, error) { - if len(allowed) == 0 && len(denied) == 0 { - return nil, nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "both allowed & deny list cannot be empty") - } + // if len(allowed) == 0 && len(denied) == 0 { + // return nil, nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "both allowed & deny list cannot be empty") + // } if len(allowed) > 0 && len(denied) > 0 { return nil, nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "cannot set both allowed & deny list") From e7a3636723596a95812cafc8bba634e42a6d2d93 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Thu, 25 Mar 2021 22:41:36 +0530 Subject: [PATCH 04/13] add build flag --- x/authz/client/cli/tx_test.go | 2 ++ x/staking/types/authz.go | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/x/authz/client/cli/tx_test.go b/x/authz/client/cli/tx_test.go index 298b5167b7f4..b7dad15e65b2 100644 --- a/x/authz/client/cli/tx_test.go +++ b/x/authz/client/cli/tx_test.go @@ -1,3 +1,5 @@ +// +build norace + package cli_test import ( diff --git a/x/staking/types/authz.go b/x/staking/types/authz.go index ff3407c7d504..c1d3a3f700d7 100644 --- a/x/staking/types/authz.go +++ b/x/staking/types/authz.go @@ -109,9 +109,9 @@ func (authorization StakeAuthorization) Accept(ctx sdk.Context, msg sdk.ServiceM } func validateAndBech32fy(allowed []sdk.ValAddress, denied []sdk.ValAddress) ([]string, []string, error) { - // if len(allowed) == 0 && len(denied) == 0 { - // return nil, nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "both allowed & deny list cannot be empty") - // } + if len(allowed) == 0 && len(denied) == 0 { + return nil, nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "both allowed & deny list cannot be empty") + } if len(allowed) > 0 && len(denied) > 0 { return nil, nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "cannot set both allowed & deny list") From be5c12fd5ada7674ac3ff619e3bc8bbe828b510f Mon Sep 17 00:00:00 2001 From: MD Aleem <72057206+aleem1314@users.noreply.github.com> Date: Thu, 25 Mar 2021 22:43:53 +0530 Subject: [PATCH 05/13] Update x/staking/types/authz.go --- x/staking/types/authz.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/staking/types/authz.go b/x/staking/types/authz.go index c1d3a3f700d7..36dc63c446ce 100644 --- a/x/staking/types/authz.go +++ b/x/staking/types/authz.go @@ -76,7 +76,7 @@ func (authorization StakeAuthorization) Accept(ctx sdk.Context, msg sdk.ServiceM isValidatorExists := false allowedList := authorization.GetAllowList().GetAddress() for _, validator := range allowedList { - ctx.GasMeter().ConsumeGas(gasCostPerIteration, "iteration: stake authorization allow list") + ctx.GasMeter().ConsumeGas(gasCostPerIteration, "stake authorization") if validator == validatorAddress { isValidatorExists = true break From bdfd3eeb81e13d81d329e281d8ab64fe04eeee0a Mon Sep 17 00:00:00 2001 From: MD Aleem <72057206+aleem1314@users.noreply.github.com> Date: Thu, 25 Mar 2021 22:44:20 +0530 Subject: [PATCH 06/13] Update x/staking/types/authz.go --- x/staking/types/authz.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/staking/types/authz.go b/x/staking/types/authz.go index 36dc63c446ce..6b34d00e47e9 100644 --- a/x/staking/types/authz.go +++ b/x/staking/types/authz.go @@ -85,7 +85,7 @@ func (authorization StakeAuthorization) Accept(ctx sdk.Context, msg sdk.ServiceM denyList := authorization.GetDenyList().GetAddress() for _, validator := range denyList { - ctx.GasMeter().ConsumeGas(gasCostPerIteration, "iteration: stake authorization deny list") + ctx.GasMeter().ConsumeGas(gasCostPerIteration, "stake authorization") if validator == validatorAddress { return nil, false, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, " cannot delegate/undelegate to %s validator", validator) } From f40136b1e433d2d9501c96e075bee2ee8298b03b Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Thu, 25 Mar 2021 22:55:22 +0530 Subject: [PATCH 07/13] add tests docs --- x/authz/types/generic_authorization_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/x/authz/types/generic_authorization_test.go b/x/authz/types/generic_authorization_test.go index da483050d170..e3330d1c6594 100644 --- a/x/authz/types/generic_authorization_test.go +++ b/x/authz/types/generic_authorization_test.go @@ -9,12 +9,11 @@ import ( ) func TestGenericAuthorization(t *testing.T) { - - // validateBasic returns error not a valida service msg + t.Log("verify ValidateBasic returns error for non-service msg") authorization := types.NewGenericAuthorization(banktypes.TypeMsgSend) require.Error(t, authorization.ValidateBasic()) - // valid service msg + t.Log("verify ValidateBasic returns nil for service msg") authorization = types.NewGenericAuthorization(banktypes.SendAuthorization{}.MethodName()) require.NoError(t, authorization.ValidateBasic()) require.Equal(t, banktypes.SendAuthorization{}.MethodName(), authorization.MessageName) From 733ef553c4c777a4cf96effd6ca4312302640cbf Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Fri, 26 Mar 2021 20:46:21 +0530 Subject: [PATCH 08/13] review changes --- types/tx/types.go | 8 ++++---- x/authz/client/cli/tx.go | 4 ---- x/authz/types/generic_authorization.go | 11 ++--------- x/authz/types/msgs.go | 8 ++++++++ 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/types/tx/types.go b/types/tx/types.go index a0b05b5dcea0..08301e422aa0 100644 --- a/types/tx/types.go +++ b/types/tx/types.go @@ -27,7 +27,7 @@ func (t *Tx) GetMsgs() []sdk.Msg { res := make([]sdk.Msg, len(anys)) for i, any := range anys { var msg sdk.Msg - if isServiceMsg(any.TypeUrl) { + if IsServiceMsg(any.TypeUrl) { req := any.GetCachedValue() if req == nil { panic("Any cached value is nil. Transaction messages must be correctly packed Any values.") @@ -183,7 +183,7 @@ func (m *TxBody) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { for _, any := range m.Messages { // If the any's typeUrl contains 2 slashes, then we unpack the any into // a ServiceMsg struct as per ADR-031. - if isServiceMsg(any.TypeUrl) { + if IsServiceMsg(any.TypeUrl) { var req sdk.MsgRequest err := unpacker.UnpackAny(any, &req) if err != nil { @@ -223,8 +223,8 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { registry.RegisterImplementations((*sdk.Tx)(nil), &Tx{}) } -// isServiceMsg checks if a type URL corresponds to a service method name, +// IsServiceMsg checks if a type URL corresponds to a service method name, // i.e. /cosmos.bank.Msg/Send vs /cosmos.bank.MsgSend -func isServiceMsg(typeURL string) bool { +func IsServiceMsg(typeURL string) bool { return strings.Count(typeURL, "/") >= 2 } diff --git a/x/authz/client/cli/tx.go b/x/authz/client/cli/tx.go index dbebb946c6bf..44df6f5e9aa4 100644 --- a/x/authz/client/cli/tx.go +++ b/x/authz/client/cli/tx.go @@ -160,10 +160,6 @@ Examples: return fmt.Errorf("invalid authorization type, %s", args[1]) } - if err = authorization.ValidateBasic(); err != nil { - return err - } - msg, err := types.NewMsgGrantAuthorization(clientCtx.GetFromAddress(), grantee, authorization, time.Unix(exp, 0)) if err != nil { return err diff --git a/x/authz/types/generic_authorization.go b/x/authz/types/generic_authorization.go index 48bc179685a1..05ea59f1145d 100644 --- a/x/authz/types/generic_authorization.go +++ b/x/authz/types/generic_authorization.go @@ -1,10 +1,9 @@ package types import ( - "strings" - sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/cosmos/cosmos-sdk/types/tx" "github.com/cosmos/cosmos-sdk/x/authz/exported" ) @@ -31,14 +30,8 @@ func (authorization GenericAuthorization) Accept(ctx sdk.Context, msg sdk.Servic // ValidateBasic implements Authorization.ValidateBasic. func (authorization GenericAuthorization) ValidateBasic() error { - if !isServiceMsg(authorization.MessageName) { + if !tx.IsServiceMsg(authorization.MessageName) { return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, " %s is not a valid service msg", authorization.MessageName) } return nil } - -// isServiceMsg checks if a type URL corresponds to a service method name, -// i.e. /cosmos.bank.Msg/Send vs /cosmos.bank.MsgSend -func isServiceMsg(typeURL string) bool { - return strings.Count(typeURL, "/") >= 2 -} diff --git a/x/authz/types/msgs.go b/x/authz/types/msgs.go index 8fa3bdd1f0a4..f8f938556abd 100644 --- a/x/authz/types/msgs.go +++ b/x/authz/types/msgs.go @@ -63,6 +63,14 @@ func (msg MsgGrantAuthorizationRequest) ValidateBasic() error { return sdkerrors.Wrap(ErrInvalidExpirationTime, "Time can't be in the past") } + authorization, ok := msg.Authorization.GetCachedValue().(exported.Authorization) + if !ok { + return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "expected %T, got %T", (exported.Authorization)(nil), msg.Authorization.GetCachedValue()) + } + if err = authorization.ValidateBasic(); err != nil { + return err + } + return nil } From d706b8e2671a71197c345a203a4e84dfb134a362 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Fri, 26 Mar 2021 21:37:11 +0530 Subject: [PATCH 09/13] fix operations --- x/authz/simulation/operations.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/x/authz/simulation/operations.go b/x/authz/simulation/operations.go index e13cbaa338dd..8fef468b06db 100644 --- a/x/authz/simulation/operations.go +++ b/x/authz/simulation/operations.go @@ -96,8 +96,12 @@ func SimulateMsgGrantAuthorization(ak types.AccountKeeper, bk types.BankKeeper, } blockTime := ctx.BlockTime() + spendLimit := spendableCoins.Sub(fees) + if spendLimit == nil { + return simtypes.NoOpMsg(types.ModuleName, TypeMsgGrantAuthorization, "spend limit is nil"), nil, nil + } msg, err := types.NewMsgGrantAuthorization(granter.Address, grantee.Address, - banktype.NewSendAuthorization(spendableCoins.Sub(fees)), blockTime.AddDate(1, 0, 0)) + banktype.NewSendAuthorization(spendLimit), blockTime.AddDate(1, 0, 0)) if err != nil { return simtypes.NoOpMsg(types.ModuleName, TypeMsgGrantAuthorization, err.Error()), nil, err From 82aed7737681416f5a0943e201daa7a467b985ee Mon Sep 17 00:00:00 2001 From: MD Aleem <72057206+aleem1314@users.noreply.github.com> Date: Mon, 29 Mar 2021 19:21:56 +0530 Subject: [PATCH 10/13] Update x/authz/types/msgs.go Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> --- x/authz/types/msgs.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/x/authz/types/msgs.go b/x/authz/types/msgs.go index f8f938556abd..0b72d1a7e869 100644 --- a/x/authz/types/msgs.go +++ b/x/authz/types/msgs.go @@ -67,11 +67,7 @@ func (msg MsgGrantAuthorizationRequest) ValidateBasic() error { if !ok { return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "expected %T, got %T", (exported.Authorization)(nil), msg.Authorization.GetCachedValue()) } - if err = authorization.ValidateBasic(); err != nil { - return err - } - - return nil + return authorization.ValidateBasic() } // GetGrantAuthorization returns the cache value from the MsgGrantAuthorization.Authorization if present. From 71900e768fb28de493d5472dffed1353271dc550 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Mon, 29 Mar 2021 19:23:01 +0530 Subject: [PATCH 11/13] review changes --- types/msgservice/msg_service.go | 7 +++++++ types/tx/types.go | 12 +++--------- x/authz/types/generic_authorization.go | 4 ++-- x/staking/types/authz.go | 4 ++++ x/staking/types/authz_test.go | 6 +++++- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/types/msgservice/msg_service.go b/types/msgservice/msg_service.go index 277ee3d157c9..ddffed943e8a 100644 --- a/types/msgservice/msg_service.go +++ b/types/msgservice/msg_service.go @@ -3,6 +3,7 @@ package msgservice import ( "context" "fmt" + "strings" "github.com/gogo/protobuf/proto" "google.golang.org/grpc" @@ -42,3 +43,9 @@ func RegisterMsgServiceDesc(registry codectypes.InterfaceRegistry, sd *grpc.Serv func noopInterceptor(_ context.Context, _ interface{}, _ *grpc.UnaryServerInfo, _ grpc.UnaryHandler) (interface{}, error) { return nil, nil } + +// IsServiceMsg checks if a type URL corresponds to a service method name, +// i.e. /cosmos.bank.Msg/Send vs /cosmos.bank.MsgSend +func IsServiceMsg(typeURL string) bool { + return strings.Count(typeURL, "/") >= 2 +} diff --git a/types/tx/types.go b/types/tx/types.go index 08301e422aa0..66fbb193ad96 100644 --- a/types/tx/types.go +++ b/types/tx/types.go @@ -2,12 +2,12 @@ package tx import ( "fmt" - "strings" codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/cosmos/cosmos-sdk/types/msgservice" ) // MaxGasWanted defines the max gas allowed. @@ -27,7 +27,7 @@ func (t *Tx) GetMsgs() []sdk.Msg { res := make([]sdk.Msg, len(anys)) for i, any := range anys { var msg sdk.Msg - if IsServiceMsg(any.TypeUrl) { + if msgservice.IsServiceMsg(any.TypeUrl) { req := any.GetCachedValue() if req == nil { panic("Any cached value is nil. Transaction messages must be correctly packed Any values.") @@ -183,7 +183,7 @@ func (m *TxBody) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { for _, any := range m.Messages { // If the any's typeUrl contains 2 slashes, then we unpack the any into // a ServiceMsg struct as per ADR-031. - if IsServiceMsg(any.TypeUrl) { + if msgservice.IsServiceMsg(any.TypeUrl) { var req sdk.MsgRequest err := unpacker.UnpackAny(any, &req) if err != nil { @@ -222,9 +222,3 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { registry.RegisterInterface("cosmos.tx.v1beta1.Tx", (*sdk.Tx)(nil)) registry.RegisterImplementations((*sdk.Tx)(nil), &Tx{}) } - -// IsServiceMsg checks if a type URL corresponds to a service method name, -// i.e. /cosmos.bank.Msg/Send vs /cosmos.bank.MsgSend -func IsServiceMsg(typeURL string) bool { - return strings.Count(typeURL, "/") >= 2 -} diff --git a/x/authz/types/generic_authorization.go b/x/authz/types/generic_authorization.go index 05ea59f1145d..101f3acf1664 100644 --- a/x/authz/types/generic_authorization.go +++ b/x/authz/types/generic_authorization.go @@ -3,7 +3,7 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/cosmos/cosmos-sdk/types/tx" + "github.com/cosmos/cosmos-sdk/types/msgservice" "github.com/cosmos/cosmos-sdk/x/authz/exported" ) @@ -30,7 +30,7 @@ func (authorization GenericAuthorization) Accept(ctx sdk.Context, msg sdk.Servic // ValidateBasic implements Authorization.ValidateBasic. func (authorization GenericAuthorization) ValidateBasic() error { - if !tx.IsServiceMsg(authorization.MessageName) { + if !msgservice.IsServiceMsg(authorization.MessageName) { return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, " %s is not a valid service msg", authorization.MessageName) } return nil diff --git a/x/staking/types/authz.go b/x/staking/types/authz.go index 6b34d00e47e9..eb3117723057 100644 --- a/x/staking/types/authz.go +++ b/x/staking/types/authz.go @@ -51,6 +51,10 @@ func (authorization StakeAuthorization) ValidateBasic() error { if authorization.MaxTokens != nil && authorization.MaxTokens.IsNegative() { return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "negative coin amount: %v", authorization.MaxTokens) } + if authorization.AuthorizationType == AuthorizationType_AUTHORIZATION_TYPE_UNSPECIFIED { + return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "unknown authorization type") + } + return nil } diff --git a/x/staking/types/authz_test.go b/x/staking/types/authz_test.go index d1489a8b6e76..ffe6ba78947d 100644 --- a/x/staking/types/authz_test.go +++ b/x/staking/types/authz_test.go @@ -25,8 +25,12 @@ func TestAuthzAuthorizations(t *testing.T) { app := simapp.Setup(false) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) + // verify ValidateBasic returns error for the AUTHORIZATION_TYPE_UNSPECIFIED authorization type + delAuth, _ := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_UNSPECIFIED, &coin100) + require.Error(t, delAuth.ValidateBasic()) + // verify MethodName - delAuth, _ := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100) + delAuth, _ = stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100) require.Equal(t, delAuth.MethodName(), stakingtypes.TypeDelegate) // error both allow & deny list From f497bccf61b98f2aa01236b6e442eadc95ad668d Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Thu, 8 Apr 2021 11:58:12 +0530 Subject: [PATCH 12/13] update gas cost --- x/staking/types/authz.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x/staking/types/authz.go b/x/staking/types/authz.go index eb3117723057..32e8b719d1f2 100644 --- a/x/staking/types/authz.go +++ b/x/staking/types/authz.go @@ -6,7 +6,9 @@ import ( authz "github.com/cosmos/cosmos-sdk/x/authz/exported" ) -const gasCostPerIteration = uint64(1000) +// TODO: Revisit this once we have propoer gas fee framework. +// Tracking issues https://github.com/cosmos/cosmos-sdk/issues/9054, https://github.com/cosmos/cosmos-sdk/discussions/9072 +const gasCostPerIteration = uint64(10) var ( _ authz.Authorization = &StakeAuthorization{} From 71b7093070abae8f0bd8d0b5f8272c03e2d910b7 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Thu, 8 Apr 2021 20:14:34 +0530 Subject: [PATCH 13/13] review changes --- x/staking/types/authz_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/x/staking/types/authz_test.go b/x/staking/types/authz_test.go index ffe6ba78947d..c43ef9e1fb48 100644 --- a/x/staking/types/authz_test.go +++ b/x/staking/types/authz_test.go @@ -26,15 +26,17 @@ func TestAuthzAuthorizations(t *testing.T) { ctx := app.BaseApp.NewContext(false, tmproto.Header{}) // verify ValidateBasic returns error for the AUTHORIZATION_TYPE_UNSPECIFIED authorization type - delAuth, _ := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_UNSPECIFIED, &coin100) + delAuth, err := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_UNSPECIFIED, &coin100) + require.NoError(t, err) require.Error(t, delAuth.ValidateBasic()) // verify MethodName - delAuth, _ = stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100) + delAuth, err = stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100) + require.NoError(t, err) require.Equal(t, delAuth.MethodName(), stakingtypes.TypeDelegate) // error both allow & deny list - _, err := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{val1}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100) + _, err = stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{val1}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100) require.Error(t, err) // verify MethodName