diff --git a/x/authz/CHANGELOG.md b/x/authz/CHANGELOG.md index e6de2c18baca..0963382c9e03 100644 --- a/x/authz/CHANGELOG.md +++ b/x/authz/CHANGELOG.md @@ -31,6 +31,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* [#19783](https://github.com/cosmos/cosmos-sdk/pull/19783) Removes the use of Accouts String() method + * `NewMsgExec`, `NewMsgGrant` and `NewMsgRevoke` now takes strings as arguments instead of `sdk.AccAddress`. + * `ExportGenesis` also returns an error. + * `IterateGrants` returns an error, its handler function also returns an error. * [#19637](https://github.com/cosmos/cosmos-sdk/pull/19637) `NewKeeper` doesn't take a message router anymore. Set the message router in the `appmodule.Environment` instead. * [#19490](https://github.com/cosmos/cosmos-sdk/pull/19490) `appmodule.Environment` is received on the Keeper to get access to different application services. * [#18737](https://github.com/cosmos/cosmos-sdk/pull/18737) Update the keeper method `DequeueAndDeleteExpiredGrants` to take a limit argument for the number of grants to prune. diff --git a/x/authz/client/cli/tx.go b/x/authz/client/cli/tx.go index 6227785b067a..904b210d88f6 100644 --- a/x/authz/client/cli/tx.go +++ b/x/authz/client/cli/tx.go @@ -67,7 +67,10 @@ func NewCmdExecAuthorization() *cobra.Command { if err != nil { return err } - grantee := clientCtx.GetFromAddress() + grantee, err := clientCtx.AddressCodec.BytesToString(clientCtx.GetFromAddress()) + if err != nil { + return err + } if offline, _ := cmd.Flags().GetBool(flags.FlagOffline); offline { return errors.New("cannot broadcast tx during offline mode") @@ -106,15 +109,20 @@ Examples: return err } - if strings.EqualFold(args[0], clientCtx.GetFromAddress().String()) { - return errors.New("grantee and granter should be different") + grantee := args[0] + if _, err := clientCtx.AddressCodec.StringToBytes(grantee); err != nil { + return err } - grantee, err := clientCtx.AddressCodec.StringToBytes(args[0]) + granter, err := clientCtx.AddressCodec.BytesToString(clientCtx.GetFromAddress()) if err != nil { return err } + if strings.EqualFold(grantee, granter) { + return errors.New("grantee and granter should be different") + } + var authorization authz.Authorization switch args[1] { case "send": @@ -230,7 +238,7 @@ Examples: return err } - msg, err := authz.NewMsgGrant(clientCtx.GetFromAddress(), grantee, authorization, expire) + msg, err := authz.NewMsgGrant(granter, grantee, authorization, expire) if err != nil { return err } diff --git a/x/authz/client/cli/tx_test.go b/x/authz/client/cli/tx_test.go index a9c4d87a96f6..bdb963a102a2 100644 --- a/x/authz/client/cli/tx_test.go +++ b/x/authz/client/cli/tx_test.go @@ -80,6 +80,8 @@ func (s *CLITestSuite) SetupSuite() { val := testutil.CreateKeyringAccounts(s.T(), s.kr, 1) s.grantee = make([]sdk.AccAddress, 6) + valAddr, err := s.baseCtx.AddressCodec.BytesToString(val[0].Address) + s.Require().NoError(err) s.addrs = make([]sdk.AccAddress, 1) s.addrs[0] = s.createAccount("validator address") @@ -93,13 +95,15 @@ func (s *CLITestSuite) SetupSuite() { s.grantee[1] = s.createAccount("grantee2") // Send some funds to the new account. s.msgSendExec(s.grantee[1]) + grantee1Addr, err := s.baseCtx.AddressCodec.BytesToString(s.grantee[1]) + s.Require().NoError(err) // grant send authorization to grantee2 out, err := authzclitestutil.CreateGrant(s.clientCtx, []string{ - s.grantee[1].String(), + grantee1Addr, "send", fmt.Sprintf("--%s=100stake", cli.FlagSpendLimit), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, valAddr), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(10))).String()), @@ -112,13 +116,15 @@ func (s *CLITestSuite) SetupSuite() { // Create new account in the keyring. s.grantee[2] = s.createAccount("grantee3") + grantee2Addr, err := s.baseCtx.AddressCodec.BytesToString(s.grantee[2]) + s.Require().NoError(err) // grant send authorization to grantee3 _, err = authzclitestutil.CreateGrant(s.clientCtx, []string{ - s.grantee[2].String(), + grantee2Addr, "send", fmt.Sprintf("--%s=100stake", cli.FlagSpendLimit), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, valAddr), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(10))).String()), @@ -129,6 +135,8 @@ func (s *CLITestSuite) SetupSuite() { // Create new accounts in the keyring. s.grantee[3] = s.createAccount("grantee4") s.msgSendExec(s.grantee[3]) + grantee3Addr, err := s.baseCtx.AddressCodec.BytesToString(s.grantee[3]) + s.Require().NoError(err) s.grantee[4] = s.createAccount("grantee5") s.grantee[5] = s.createAccount("grantee6") @@ -136,11 +144,11 @@ func (s *CLITestSuite) SetupSuite() { // grant send authorization with allow list to grantee4 out, err = authzclitestutil.CreateGrant(s.clientCtx, []string{ - s.grantee[3].String(), + grantee3Addr, "send", fmt.Sprintf("--%s=100stake", cli.FlagSpendLimit), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, valAddr), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=%d", cli.FlagExpiration, time.Now().Add(time.Minute*time.Duration(120)).Unix()), fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(10))).String()), @@ -170,9 +178,13 @@ func (s *CLITestSuite) msgSendExec(grantee sdk.AccAddress) { coins := sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(200))) from := val[0].Address + fromAddr, err := s.clientCtx.AddressCodec.BytesToString(from) + s.Require().NoError(err) + granteeAddr, err := s.clientCtx.AddressCodec.BytesToString(grantee) + s.Require().NoError(err) msgSend := &banktypes.MsgSend{ - FromAddress: from.String(), - ToAddress: grantee.String(), + FromAddress: fromAddr, + ToAddress: granteeAddr, Amount: coins, } @@ -182,8 +194,14 @@ func (s *CLITestSuite) msgSendExec(grantee sdk.AccAddress) { func (s *CLITestSuite) TestCLITxGrantAuthorization() { val := testutil.CreateKeyringAccounts(s.T(), s.kr, 1) + valAddress, err := s.clientCtx.ValidatorAddressCodec.BytesToString(s.addrs[0]) + s.Require().NoError(err) + fromAddr, err := s.baseCtx.AddressCodec.BytesToString(val[0].Address) + s.Require().NoError(err) grantee := s.grantee[0] + granteeAddr, err := s.baseCtx.AddressCodec.BytesToString(grantee) + s.Require().NoError(err) twoHours := time.Now().Add(time.Minute * 120).Unix() pastHour := time.Now().Add(-time.Minute * 60).Unix() @@ -213,7 +231,7 @@ func (s *CLITestSuite) TestCLITxGrantAuthorization() { "grantee_addr", "send", fmt.Sprintf("--%s=100stake", cli.FlagSpendLimit), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, fromAddr), fmt.Sprintf("--%s=true", flags.FlagGenerateOnly), fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours), }, @@ -223,10 +241,10 @@ func (s *CLITestSuite) TestCLITxGrantAuthorization() { { "Invalid spend limit", []string{ - grantee.String(), + granteeAddr, "send", fmt.Sprintf("--%s=0stake", cli.FlagSpendLimit), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, fromAddr), fmt.Sprintf("--%s=true", flags.FlagGenerateOnly), fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours), }, @@ -236,10 +254,10 @@ func (s *CLITestSuite) TestCLITxGrantAuthorization() { { "Invalid expiration time", []string{ - grantee.String(), + granteeAddr, "send", fmt.Sprintf("--%s=100stake", cli.FlagSpendLimit), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, fromAddr), fmt.Sprintf("--%s=true", flags.FlagBroadcastMode), fmt.Sprintf("--%s=%d", cli.FlagExpiration, pastHour), }, @@ -249,10 +267,10 @@ func (s *CLITestSuite) TestCLITxGrantAuthorization() { { "fail with error invalid msg-type", []string{ - grantee.String(), + granteeAddr, "generic", fmt.Sprintf("--%s=invalid-msg-type", cli.FlagMsgType), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, fromAddr), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(10))).String()), @@ -264,14 +282,14 @@ func (s *CLITestSuite) TestCLITxGrantAuthorization() { { "invalid bond denom for tx delegate authorization allowed validators", []string{ - grantee.String(), + granteeAddr, "delegate", fmt.Sprintf("--%s=100xyz", cli.FlagSpendLimit), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, fromAddr), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours), - fmt.Sprintf("--%s=%s", cli.FlagAllowedValidators, sdk.ValAddress(s.addrs[0]).String()), + fmt.Sprintf("--%s=%s", cli.FlagAllowedValidators, valAddress), fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(10))).String()), }, true, @@ -280,14 +298,14 @@ func (s *CLITestSuite) TestCLITxGrantAuthorization() { { "invalid bond denom for tx delegate authorization deny validators", []string{ - grantee.String(), + granteeAddr, "delegate", fmt.Sprintf("--%s=100xyz", cli.FlagSpendLimit), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, fromAddr), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours), - fmt.Sprintf("--%s=%s", cli.FlagDenyValidators, sdk.ValAddress(s.addrs[0]).String()), + fmt.Sprintf("--%s=%s", cli.FlagDenyValidators, valAddress), fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(10))).String()), }, true, @@ -296,14 +314,14 @@ func (s *CLITestSuite) TestCLITxGrantAuthorization() { { "invalid bond denom for tx undelegate authorization", []string{ - grantee.String(), + granteeAddr, "unbond", fmt.Sprintf("--%s=100xyz", cli.FlagSpendLimit), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, fromAddr), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours), - fmt.Sprintf("--%s=%s", cli.FlagAllowedValidators, sdk.ValAddress(s.addrs[0]).String()), + fmt.Sprintf("--%s=%s", cli.FlagAllowedValidators, valAddress), fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(10))).String()), }, true, @@ -312,14 +330,14 @@ func (s *CLITestSuite) TestCLITxGrantAuthorization() { { "invalid bond denom for tx redelegate authorization", []string{ - grantee.String(), + granteeAddr, "redelegate", fmt.Sprintf("--%s=100xyz", cli.FlagSpendLimit), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, fromAddr), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours), - fmt.Sprintf("--%s=%s", cli.FlagAllowedValidators, sdk.ValAddress(s.addrs[0]).String()), + fmt.Sprintf("--%s=%s", cli.FlagAllowedValidators, valAddress), fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(10))).String()), }, true, @@ -328,14 +346,14 @@ func (s *CLITestSuite) TestCLITxGrantAuthorization() { { "invalid decimal coin expression with more than single coin", []string{ - grantee.String(), + granteeAddr, "delegate", fmt.Sprintf("--%s=100stake,20xyz", cli.FlagSpendLimit), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, fromAddr), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours), - fmt.Sprintf("--%s=%s", cli.FlagAllowedValidators, sdk.ValAddress(s.addrs[0]).String()), + fmt.Sprintf("--%s=%s", cli.FlagAllowedValidators, valAddress), fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(10))).String()), }, true, @@ -344,7 +362,7 @@ func (s *CLITestSuite) TestCLITxGrantAuthorization() { { "invalid authorization type", []string{ - grantee.String(), + granteeAddr, "invalid authz type", }, true, @@ -353,10 +371,10 @@ func (s *CLITestSuite) TestCLITxGrantAuthorization() { { "Valid tx send authorization", []string{ - grantee.String(), + granteeAddr, "send", fmt.Sprintf("--%s=100stake", cli.FlagSpendLimit), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, fromAddr), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), @@ -368,10 +386,10 @@ func (s *CLITestSuite) TestCLITxGrantAuthorization() { { "Valid tx send authorization with allow list", []string{ - grantee.String(), + granteeAddr, "send", fmt.Sprintf("--%s=100stake", cli.FlagSpendLimit), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, fromAddr), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), @@ -384,10 +402,10 @@ func (s *CLITestSuite) TestCLITxGrantAuthorization() { { "Invalid tx send authorization with duplicate allow list", []string{ - grantee.String(), + granteeAddr, "send", fmt.Sprintf("--%s=100stake", cli.FlagSpendLimit), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, fromAddr), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), @@ -400,10 +418,10 @@ func (s *CLITestSuite) TestCLITxGrantAuthorization() { { "Valid tx generic authorization", []string{ - grantee.String(), + granteeAddr, "generic", fmt.Sprintf("--%s=%s", cli.FlagMsgType, typeMsgVote), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, fromAddr), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), @@ -415,10 +433,10 @@ func (s *CLITestSuite) TestCLITxGrantAuthorization() { { "fail when granter = grantee", []string{ - grantee.String(), + granteeAddr, "generic", fmt.Sprintf("--%s=%s", cli.FlagMsgType, typeMsgVote), - fmt.Sprintf("--%s=%s", flags.FlagFrom, grantee.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, granteeAddr), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), @@ -430,10 +448,10 @@ func (s *CLITestSuite) TestCLITxGrantAuthorization() { { "Valid tx with amino", []string{ - grantee.String(), + granteeAddr, "generic", fmt.Sprintf("--%s=%s", cli.FlagMsgType, typeMsgVote), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, fromAddr), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), diff --git a/x/authz/keeper/genesis.go b/x/authz/keeper/genesis.go index d5de1d741b09..909c54dc1421 100644 --- a/x/authz/keeper/genesis.go +++ b/x/authz/keeper/genesis.go @@ -41,17 +41,27 @@ func (k Keeper) InitGenesis(ctx context.Context, data *authz.GenesisState) error } // ExportGenesis returns a GenesisState for a given context. -func (k Keeper) ExportGenesis(ctx context.Context) *authz.GenesisState { +func (k Keeper) ExportGenesis(ctx context.Context) (*authz.GenesisState, error) { var entries []authz.GrantAuthorization - k.IterateGrants(ctx, func(granter, grantee sdk.AccAddress, grant authz.Grant) bool { + err := k.IterateGrants(ctx, func(granter, grantee sdk.AccAddress, grant authz.Grant) (bool, error) { + granterAddr, err := k.authKeeper.AddressCodec().BytesToString(granter) + if err != nil { + return false, err + } + granteeAddr, err := k.authKeeper.AddressCodec().BytesToString(grantee) + if err != nil { + return false, err + } entries = append(entries, authz.GrantAuthorization{ - Granter: granter.String(), - Grantee: grantee.String(), + Granter: granterAddr, + Grantee: granteeAddr, Expiration: grant.Expiration, Authorization: grant.Authorization, }) - return false + return false, nil }) - - return authz.NewGenesisState(entries) + if err != nil { + return nil, err + } + return authz.NewGenesisState(entries), nil } diff --git a/x/authz/keeper/genesis_test.go b/x/authz/keeper/genesis_test.go index d913c3bef304..a4a40033514d 100644 --- a/x/authz/keeper/genesis_test.go +++ b/x/authz/keeper/genesis_test.go @@ -81,18 +81,20 @@ func (suite *GenesisTestSuite) TestImportExportGenesis() { grant := &bank.SendAuthorization{SpendLimit: coins} err := suite.keeper.SaveGrant(suite.ctx, granteeAddr, granterAddr, grant, &expires) suite.Require().NoError(err) - genesis := suite.keeper.ExportGenesis(suite.ctx) - + genesis, err := suite.keeper.ExportGenesis(suite.ctx) + suite.Require().NoError(err) // Clear keeper err = suite.keeper.DeleteGrant(suite.ctx, granteeAddr, granterAddr, grant.MsgTypeURL()) suite.Require().NoError(err) - newGenesis := suite.keeper.ExportGenesis(suite.ctx) + newGenesis, err := suite.keeper.ExportGenesis(suite.ctx) + suite.Require().NoError(err) suite.Require().NotEqual(genesis, newGenesis) suite.Require().Empty(newGenesis) err = suite.keeper.InitGenesis(suite.ctx, genesis) suite.Require().NoError(err) - newGenesis = suite.keeper.ExportGenesis(suite.ctx) + newGenesis, err = suite.keeper.ExportGenesis(suite.ctx) + suite.Require().NoError(err) suite.Require().Equal(genesis, newGenesis) } diff --git a/x/authz/keeper/grpc_query_test.go b/x/authz/keeper/grpc_query_test.go index be53c1df8e3f..6cb51dbae0f7 100644 --- a/x/authz/keeper/grpc_query_test.go +++ b/x/authz/keeper/grpc_query_test.go @@ -21,6 +21,11 @@ func (suite *TestSuite) TestGRPCQueryAuthorization() { expAuthorization authz.Authorization ) + addr0, err := suite.accountKeeper.AddressCodec().BytesToString(addrs[0]) + suite.Require().NoError(err) + addr1, err := suite.accountKeeper.AddressCodec().BytesToString(addrs[1]) + suite.Require().NoError(err) + testCases := []struct { msg string malleate func(require *require.Assertions) @@ -39,7 +44,7 @@ func (suite *TestSuite) TestGRPCQueryAuthorization() { "fail invalid grantee addr", func(require *require.Assertions) { req = &authz.QueryGrantsRequest{ - Granter: addrs[0].String(), + Granter: addr0, } }, "empty address string is not allowed", @@ -49,8 +54,8 @@ func (suite *TestSuite) TestGRPCQueryAuthorization() { "fail invalid msg-type", func(require *require.Assertions) { req = &authz.QueryGrantsRequest{ - Granter: addrs[0].String(), - Grantee: addrs[1].String(), + Granter: addr0, + Grantee: addr1, MsgTypeUrl: "unknown", } }, @@ -61,8 +66,8 @@ func (suite *TestSuite) TestGRPCQueryAuthorization() { "authorization not found", func(require *require.Assertions) { req = &authz.QueryGrantsRequest{ - Granter: addrs[1].String(), - Grantee: addrs[0].String(), + Granter: addr1, + Grantee: addr0, MsgTypeUrl: banktypes.SendAuthorization{}.MsgTypeURL(), } }, @@ -74,8 +79,8 @@ func (suite *TestSuite) TestGRPCQueryAuthorization() { func(require *require.Assertions) { expAuthorization = suite.createSendAuthorization(addrs[0], addrs[1]) req = &authz.QueryGrantsRequest{ - Granter: addrs[1].String(), - Grantee: addrs[0].String(), + Granter: addr1, + Grantee: addr0, MsgTypeUrl: expAuthorization.MsgTypeURL(), } }, @@ -95,8 +100,8 @@ func (suite *TestSuite) TestGRPCQueryAuthorization() { expAuthorization = suite.createSendAuthorizationWithAllowList(addrs[0], addrs[1]) require.Len(expAuthorization.(*banktypes.SendAuthorization).GetAllowList(), 1) req = &authz.QueryGrantsRequest{ - Granter: addrs[1].String(), - Grantee: addrs[0].String(), + Granter: addr1, + Grantee: addr0, MsgTypeUrl: expAuthorization.MsgTypeURL(), } }, @@ -132,6 +137,9 @@ func (suite *TestSuite) TestGRPCQueryGranterGrants() { require := suite.Require() queryClient, addrs := suite.queryClient, suite.addrs + addr0, err := suite.accountKeeper.AddressCodec().BytesToString(addrs[0]) + suite.Require().NoError(err) + testCases := []struct { msg string preRun func() @@ -153,7 +161,7 @@ func (suite *TestSuite) TestGRPCQueryGranterGrants() { }, false, authz.QueryGranterGrantsRequest{ - Granter: addrs[0].String(), + Granter: addr0, }, 1, }, @@ -164,7 +172,7 @@ func (suite *TestSuite) TestGRPCQueryGranterGrants() { }, false, authz.QueryGranterGrantsRequest{ - Granter: addrs[0].String(), + Granter: addr0, }, 2, }, @@ -174,7 +182,7 @@ func (suite *TestSuite) TestGRPCQueryGranterGrants() { }, false, authz.QueryGranterGrantsRequest{ - Granter: addrs[0].String(), + Granter: addr0, Pagination: &query.PageRequest{ Limit: 1, }, @@ -202,6 +210,11 @@ func (suite *TestSuite) TestGRPCQueryGranteeGrants() { require := suite.Require() queryClient, addrs := suite.queryClient, suite.addrs + addr0, err := suite.accountKeeper.AddressCodec().BytesToString(addrs[0]) + suite.Require().NoError(err) + addr2, err := suite.accountKeeper.AddressCodec().BytesToString(addrs[2]) + suite.Require().NoError(err) + testCases := []struct { msg string preRun func() @@ -223,7 +236,7 @@ func (suite *TestSuite) TestGRPCQueryGranteeGrants() { }, false, authz.QueryGranteeGrantsRequest{ - Grantee: addrs[0].String(), + Grantee: addr0, }, 1, }, @@ -232,7 +245,7 @@ func (suite *TestSuite) TestGRPCQueryGranteeGrants() { func() {}, false, authz.QueryGranteeGrantsRequest{ - Grantee: addrs[2].String(), + Grantee: addr2, }, 0, }, @@ -243,7 +256,7 @@ func (suite *TestSuite) TestGRPCQueryGranteeGrants() { }, false, authz.QueryGranteeGrantsRequest{ - Grantee: addrs[0].String(), + Grantee: addr0, }, 2, }, @@ -252,7 +265,7 @@ func (suite *TestSuite) TestGRPCQueryGranteeGrants() { func() {}, false, authz.QueryGranteeGrantsRequest{ - Grantee: addrs[0].String(), + Grantee: addr0, Pagination: &query.PageRequest{ Limit: 1, }, @@ -289,8 +302,10 @@ func (suite *TestSuite) createSendAuthorization(grantee, granter sdk.AccAddress) func (suite *TestSuite) createSendAuthorizationWithAllowList(grantee, granter sdk.AccAddress) authz.Authorization { exp := suite.ctx.HeaderInfo().Time.Add(time.Hour) newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) - authorization := &banktypes.SendAuthorization{SpendLimit: newCoins, AllowList: []string{suite.addrs[5].String()}} - err := suite.authzKeeper.SaveGrant(suite.ctx, grantee, granter, authorization, &exp) + addr, err := suite.accountKeeper.AddressCodec().BytesToString(suite.addrs[5]) + suite.Require().NoError(err) + authorization := &banktypes.SendAuthorization{SpendLimit: newCoins, AllowList: []string{addr}} + err = suite.authzKeeper.SaveGrant(suite.ctx, grantee, granter, authorization, &exp) suite.Require().NoError(err) return authorization } diff --git a/x/authz/keeper/keeper.go b/x/authz/keeper/keeper.go index 2613e4085327..a4347e6132d2 100644 --- a/x/authz/keeper/keeper.go +++ b/x/authz/keeper/keeper.go @@ -197,10 +197,19 @@ func (k Keeper) SaveGrant(ctx context.Context, grantee, granter sdk.AccAddress, return err } + granterAddr, err := k.authKeeper.AddressCodec().BytesToString(granter) + if err != nil { + return err + } + granteeAddr, err := k.authKeeper.AddressCodec().BytesToString(grantee) + if err != nil { + return err + } + return k.environment.EventService.EventManager(ctx).Emit(&authz.EventGrant{ MsgTypeUrl: authorization.MsgTypeURL(), - Granter: granter.String(), - Grantee: grantee.String(), + Granter: granterAddr, + Grantee: granteeAddr, }) } @@ -226,10 +235,18 @@ func (k Keeper) DeleteGrant(ctx context.Context, grantee, granter sdk.AccAddress return err } + granterAddr, err := k.authKeeper.AddressCodec().BytesToString(granter) + if err != nil { + return err + } + granteeAddr, err := k.authKeeper.AddressCodec().BytesToString(grantee) + if err != nil { + return err + } return k.environment.EventService.EventManager(ctx).Emit(&authz.EventRevoke{ MsgTypeUrl: msgType, - Granter: granter.String(), - Grantee: grantee.String(), + Granter: granterAddr, + Grantee: granteeAddr, }) } @@ -282,8 +299,8 @@ func (k Keeper) GetAuthorization(ctx context.Context, grantee, granter sdk.AccAd // It should not be used in query or msg services without charging additional gas. // The iteration stops when the handler function returns true or the iterator exhaust. func (k Keeper) IterateGrants(ctx context.Context, - handler func(granterAddr, granteeAddr sdk.AccAddress, grant authz.Grant) bool, -) { + handler func(granterAddr, granteeAddr sdk.AccAddress, grant authz.Grant) (bool, error), +) error { store := runtime.KVStoreAdapter(k.environment.KVStoreService.OpenKVStore(ctx)) iter := storetypes.KVStorePrefixIterator(store, GrantKey) defer iter.Close() @@ -291,10 +308,15 @@ func (k Keeper) IterateGrants(ctx context.Context, var grant authz.Grant granterAddr, granteeAddr, _ := parseGrantStoreKey(iter.Key()) k.cdc.MustUnmarshal(iter.Value(), &grant) - if handler(granterAddr, granteeAddr, grant) { + ok, err := handler(granterAddr, granteeAddr, grant) + if err != nil { + return err + } + if ok { break } } + return nil } func (k Keeper) getGrantQueueItem(ctx context.Context, expiration time.Time, granter, grantee sdk.AccAddress) (*authz.GrantQueueItem, error) { diff --git a/x/authz/keeper/keeper_test.go b/x/authz/keeper/keeper_test.go index b0c77f497dae..c790b6fde446 100644 --- a/x/authz/keeper/keeper_test.go +++ b/x/authz/keeper/keeper_test.go @@ -155,10 +155,10 @@ func (s *TestSuite) TestKeeperIter() { err = s.authzKeeper.SaveGrant(ctx, granteeAddr, granter2Addr, sendAuthz, &e) s.Require().NoError(err) - s.authzKeeper.IterateGrants(ctx, func(granter, grantee sdk.AccAddress, grant authz.Grant) bool { + _ = s.authzKeeper.IterateGrants(ctx, func(granter, grantee sdk.AccAddress, grant authz.Grant) (bool, error) { s.Require().Equal(granteeAddr, grantee) s.Require().Contains([]sdk.AccAddress{granterAddr, granter2Addr}, granter) - return true + return true, nil }) } @@ -169,7 +169,12 @@ func (s *TestSuite) TestDispatchAction() { granterAddr := addrs[0] granteeAddr := addrs[1] - recipientAddr := addrs[2] + granterStrAddr, err := s.accountKeeper.AddressCodec().BytesToString(addrs[0]) + s.Require().NoError(err) + granteeStrAddr, err := s.accountKeeper.AddressCodec().BytesToString(addrs[1]) + s.Require().NoError(err) + recipientStrAddr, err := s.accountKeeper.AddressCodec().BytesToString(addrs[2]) + s.Require().NoError(err) a := banktypes.NewSendAuthorization(coins100, nil) testCases := []struct { @@ -182,11 +187,11 @@ func (s *TestSuite) TestDispatchAction() { }{ { "expect error authorization not found", - authz.NewMsgExec(granteeAddr, []sdk.Msg{ + authz.NewMsgExec(granteeStrAddr, []sdk.Msg{ &banktypes.MsgSend{ Amount: coins10, - FromAddress: granterAddr.String(), - ToAddress: recipientAddr.String(), + FromAddress: granterStrAddr, + ToAddress: recipientStrAddr, }, }), true, @@ -201,11 +206,11 @@ func (s *TestSuite) TestDispatchAction() { }, { "expect error expired authorization", - authz.NewMsgExec(granteeAddr, []sdk.Msg{ + authz.NewMsgExec(granteeStrAddr, []sdk.Msg{ &banktypes.MsgSend{ Amount: coins10, - FromAddress: granterAddr.String(), - ToAddress: recipientAddr.String(), + FromAddress: granterStrAddr, + ToAddress: recipientStrAddr, }, }), true, @@ -220,11 +225,11 @@ func (s *TestSuite) TestDispatchAction() { }, { "expect error over spent limit", - authz.NewMsgExec(granteeAddr, []sdk.Msg{ + authz.NewMsgExec(granteeStrAddr, []sdk.Msg{ &banktypes.MsgSend{ Amount: coins1000, - FromAddress: granterAddr.String(), - ToAddress: recipientAddr.String(), + FromAddress: granterStrAddr, + ToAddress: recipientStrAddr, }, }), true, @@ -239,11 +244,11 @@ func (s *TestSuite) TestDispatchAction() { }, { "valid test verify amount left", - authz.NewMsgExec(granteeAddr, []sdk.Msg{ + authz.NewMsgExec(granteeStrAddr, []sdk.Msg{ &banktypes.MsgSend{ Amount: coins10, - FromAddress: granterAddr.String(), - ToAddress: recipientAddr.String(), + FromAddress: granterStrAddr, + ToAddress: recipientStrAddr, }, }), false, @@ -265,11 +270,11 @@ func (s *TestSuite) TestDispatchAction() { }, { "valid test verify authorization is removed when it is used up", - authz.NewMsgExec(granteeAddr, []sdk.Msg{ + authz.NewMsgExec(granteeStrAddr, []sdk.Msg{ &banktypes.MsgSend{ Amount: coins100, - FromAddress: granterAddr.String(), - ToAddress: recipientAddr.String(), + FromAddress: granterStrAddr, + ToAddress: recipientStrAddr, }, }), false, @@ -314,19 +319,24 @@ func (s *TestSuite) TestDispatchedEvents() { addrs := s.addrs granterAddr := addrs[0] granteeAddr := addrs[1] - recipientAddr := addrs[2] + granterStrAddr, err := s.accountKeeper.AddressCodec().BytesToString(addrs[0]) + s.Require().NoError(err) + granteeStrAddr, err := s.accountKeeper.AddressCodec().BytesToString(addrs[1]) + s.Require().NoError(err) + recipientStrAddr, err := s.accountKeeper.AddressCodec().BytesToString(addrs[2]) + s.Require().NoError(err) expiration := s.ctx.HeaderInfo().Time.Add(1 * time.Second) // must be in the future - msgs := authz.NewMsgExec(granteeAddr, []sdk.Msg{ + msgs := authz.NewMsgExec(granteeStrAddr, []sdk.Msg{ &banktypes.MsgSend{ Amount: coins10, - FromAddress: granterAddr.String(), - ToAddress: recipientAddr.String(), + FromAddress: granterStrAddr, + ToAddress: recipientStrAddr, }, }) // grant authorization - err := s.authzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, &banktypes.SendAuthorization{SpendLimit: coins10}, &expiration) + err = s.authzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, &banktypes.SendAuthorization{SpendLimit: coins10}, &expiration) require.NoError(err) authorizations, err := s.authzKeeper.GetAuthorizations(s.ctx, granteeAddr, granterAddr) require.NoError(err) diff --git a/x/authz/keeper/msg_server_test.go b/x/authz/keeper/msg_server_test.go index 879061943b14..6fb013bb7fba 100644 --- a/x/authz/keeper/msg_server_test.go +++ b/x/authz/keeper/msg_server_test.go @@ -36,6 +36,10 @@ func (suite *TestSuite) TestGrant() { coins := sdk.NewCoins(sdk.NewCoin("steak", sdkmath.NewInt(10))) grantee, granter := addrs[0], addrs[1] + granterStrAddr, err := suite.accountKeeper.AddressCodec().BytesToString(granter) + suite.Require().NoError(err) + granteeStrAddr, err := suite.accountKeeper.AddressCodec().BytesToString(grantee) + suite.Require().NoError(err) testCases := []struct { name string @@ -49,8 +53,8 @@ func (suite *TestSuite) TestGrant() { grant, err := authz.NewGrant(curBlockTime, banktypes.NewSendAuthorization(coins, nil), &oneYear) suite.Require().NoError(err) return &authz.MsgGrant{ - Granter: grantee.String(), - Grantee: grantee.String(), + Granter: granteeStrAddr, + Grantee: granteeStrAddr, Grant: grant, } }, @@ -64,7 +68,7 @@ func (suite *TestSuite) TestGrant() { suite.Require().NoError(err) return &authz.MsgGrant{ Granter: "invalid", - Grantee: grantee.String(), + Grantee: granteeStrAddr, Grant: grant, } }, @@ -77,7 +81,7 @@ func (suite *TestSuite) TestGrant() { grant, err := authz.NewGrant(curBlockTime, banktypes.NewSendAuthorization(coins, nil), &oneYear) suite.Require().NoError(err) return &authz.MsgGrant{ - Granter: granter.String(), + Granter: granterStrAddr, Grantee: "invalid", Grant: grant, } @@ -89,8 +93,8 @@ func (suite *TestSuite) TestGrant() { name: "invalid grant", malleate: func() *authz.MsgGrant { return &authz.MsgGrant{ - Granter: granter.String(), - Grantee: grantee.String(), + Granter: granterStrAddr, + Grantee: granteeStrAddr, Grant: authz.Grant{ Expiration: &oneYear, }, @@ -106,8 +110,8 @@ func (suite *TestSuite) TestGrant() { grant, err := authz.NewGrant(curBlockTime, banktypes.NewSendAuthorization(coins, nil), &oneHour) // we only need the authorization suite.Require().NoError(err) return &authz.MsgGrant{ - Granter: granter.String(), - Grantee: grantee.String(), + Granter: granterStrAddr, + Grantee: granteeStrAddr, Grant: authz.Grant{ Authorization: grant.Authorization, Expiration: &pTime, @@ -127,9 +131,13 @@ func (suite *TestSuite) TestGrant() { grant, err := authz.NewGrant(curBlockTime, banktypes.NewSendAuthorization(coins, nil), &oneYear) suite.Require().NoError(err) + + addr, err := suite.accountKeeper.AddressCodec().BytesToString(newAcc) + suite.Require().NoError(err) + return &authz.MsgGrant{ - Granter: granter.String(), - Grantee: newAcc.String(), + Granter: granterStrAddr, + Grantee: addr, Grant: grant, } }, @@ -140,8 +148,8 @@ func (suite *TestSuite) TestGrant() { grant, err := authz.NewGrant(curBlockTime, banktypes.NewSendAuthorization(coins, nil), &oneYear) suite.Require().NoError(err) return &authz.MsgGrant{ - Granter: granter.String(), - Grantee: grantee.String(), + Granter: granterStrAddr, + Grantee: granteeStrAddr, Grant: grant, } }, @@ -152,8 +160,8 @@ func (suite *TestSuite) TestGrant() { g, err := authz.NewGrant(curBlockTime, banktypes.NewSendAuthorization(coins, nil), &oneHour) suite.Require().NoError(err) _, err = suite.msgSrvr.Grant(suite.ctx, &authz.MsgGrant{ - Granter: granter.String(), - Grantee: grantee.String(), + Granter: granterStrAddr, + Grantee: granteeStrAddr, Grant: g, }) suite.Require().NoError(err) @@ -161,8 +169,8 @@ func (suite *TestSuite) TestGrant() { grant, err := authz.NewGrant(curBlockTime, authz.NewGenericAuthorization("/cosmos.bank.v1beta1.MsgUpdateParams"), &oneHour) suite.Require().NoError(err) return &authz.MsgGrant{ - Granter: granter.String(), - Grantee: grantee.String(), + Granter: granterStrAddr, + Grantee: granteeStrAddr, Grant: grant, } }, @@ -173,8 +181,8 @@ func (suite *TestSuite) TestGrant() { grant, err := authz.NewGrant(curBlockTime, banktypes.NewSendAuthorization(coins, []sdk.AccAddress{granter}), &oneYear) suite.Require().NoError(err) return &authz.MsgGrant{ - Granter: granter.String(), - Grantee: grantee.String(), + Granter: granterStrAddr, + Grantee: granteeStrAddr, Grant: grant, } }, @@ -185,8 +193,8 @@ func (suite *TestSuite) TestGrant() { grant, err := authz.NewGrant(curBlockTime, banktypes.NewSendAuthorization(coins, nil), nil) suite.Require().NoError(err) return &authz.MsgGrant{ - Granter: granter.String(), - Grantee: grantee.String(), + Granter: granterStrAddr, + Grantee: granteeStrAddr, Grant: grant, } }, @@ -210,6 +218,10 @@ func (suite *TestSuite) TestRevoke() { addrs := suite.createAccounts() grantee, granter := addrs[0], addrs[1] + granterStrAddr, err := suite.accountKeeper.AddressCodec().BytesToString(granter) + suite.Require().NoError(err) + granteeStrAddr, err := suite.accountKeeper.AddressCodec().BytesToString(grantee) + suite.Require().NoError(err) testCases := []struct { name string @@ -221,8 +233,8 @@ func (suite *TestSuite) TestRevoke() { name: "identical grantee and granter", malleate: func() *authz.MsgRevoke { return &authz.MsgRevoke{ - Granter: grantee.String(), - Grantee: grantee.String(), + Granter: granteeStrAddr, + Grantee: granteeStrAddr, MsgTypeUrl: bankSendAuthMsgType, } }, @@ -234,7 +246,7 @@ func (suite *TestSuite) TestRevoke() { malleate: func() *authz.MsgRevoke { return &authz.MsgRevoke{ Granter: "invalid", - Grantee: grantee.String(), + Grantee: granteeStrAddr, MsgTypeUrl: bankSendAuthMsgType, } }, @@ -245,7 +257,7 @@ func (suite *TestSuite) TestRevoke() { name: "invalid grantee", malleate: func() *authz.MsgRevoke { return &authz.MsgRevoke{ - Granter: granter.String(), + Granter: granterStrAddr, Grantee: "invalid", MsgTypeUrl: bankSendAuthMsgType, } @@ -257,8 +269,8 @@ func (suite *TestSuite) TestRevoke() { name: "no msg given", malleate: func() *authz.MsgRevoke { return &authz.MsgRevoke{ - Granter: granter.String(), - Grantee: grantee.String(), + Granter: granterStrAddr, + Grantee: granteeStrAddr, MsgTypeUrl: "", } }, @@ -271,8 +283,8 @@ func (suite *TestSuite) TestRevoke() { suite.createSendAuthorization(grantee, granter) return &authz.MsgRevoke{ - Granter: granter.String(), - Grantee: grantee.String(), + Granter: granterStrAddr, + Grantee: granteeStrAddr, MsgTypeUrl: bankSendAuthMsgType, } }, @@ -281,8 +293,8 @@ func (suite *TestSuite) TestRevoke() { name: "no existing grant to revoke", malleate: func() *authz.MsgRevoke { return &authz.MsgRevoke{ - Granter: granter.String(), - Grantee: grantee.String(), + Granter: granterStrAddr, + Grantee: granteeStrAddr, MsgTypeUrl: bankSendAuthMsgType, } }, @@ -308,11 +320,15 @@ func (suite *TestSuite) TestExec() { addrs := suite.createAccounts() grantee, granter := addrs[0], addrs[1] + granterStrAddr, err := suite.accountKeeper.AddressCodec().BytesToString(granter) + suite.Require().NoError(err) + granteeStrAddr, err := suite.accountKeeper.AddressCodec().BytesToString(grantee) + suite.Require().NoError(err) coins := sdk.NewCoins(sdk.NewCoin("steak", sdkmath.NewInt(10))) msg := &banktypes.MsgSend{ - FromAddress: granter.String(), - ToAddress: grantee.String(), + FromAddress: granterStrAddr, + ToAddress: granteeStrAddr, Amount: coins, } @@ -325,7 +341,7 @@ func (suite *TestSuite) TestExec() { { name: "invalid grantee (empty)", malleate: func() authz.MsgExec { - return authz.NewMsgExec(sdk.AccAddress{}, []sdk.Msg{msg}) + return authz.NewMsgExec("", []sdk.Msg{msg}) }, expErr: true, errMsg: "empty address string is not allowed", @@ -333,7 +349,7 @@ func (suite *TestSuite) TestExec() { { name: "non existing grant", malleate: func() authz.MsgExec { - return authz.NewMsgExec(grantee, []sdk.Msg{msg}) + return authz.NewMsgExec(granteeStrAddr, []sdk.Msg{msg}) }, expErr: true, errMsg: "authorization not found", @@ -341,7 +357,7 @@ func (suite *TestSuite) TestExec() { { name: "no message case", malleate: func() authz.MsgExec { - return authz.NewMsgExec(grantee, []sdk.Msg{}) + return authz.NewMsgExec(granteeStrAddr, []sdk.Msg{}) }, expErr: true, errMsg: "messages cannot be empty", @@ -350,7 +366,7 @@ func (suite *TestSuite) TestExec() { name: "valid case", malleate: func() authz.MsgExec { suite.createSendAuthorization(grantee, granter) - return authz.NewMsgExec(grantee, []sdk.Msg{msg}) + return authz.NewMsgExec(granteeStrAddr, []sdk.Msg{msg}) }, }, } @@ -372,6 +388,11 @@ func (suite *TestSuite) TestExec() { func (suite *TestSuite) TestPruneExpiredGrants() { addrs := suite.createAccounts() + addr0, err := suite.accountKeeper.AddressCodec().BytesToString(addrs[0]) + suite.Require().NoError(err) + addr1, err := suite.accountKeeper.AddressCodec().BytesToString(addrs[1]) + suite.Require().NoError(err) + timeNow := suite.ctx.BlockTime() expiration := timeNow.Add(time.Hour) coins := sdk.NewCoins(sdk.NewCoin("steak", sdkmath.NewInt(10))) @@ -379,23 +400,23 @@ func (suite *TestSuite) TestPruneExpiredGrants() { suite.Require().NoError(err) _, err = suite.msgSrvr.Grant(suite.ctx, &authz.MsgGrant{ - Granter: addrs[0].String(), - Grantee: addrs[1].String(), + Granter: addr0, + Grantee: addr1, Grant: grant, }) suite.Require().NoError(err) _, err = suite.msgSrvr.Grant(suite.ctx, &authz.MsgGrant{ - Granter: addrs[1].String(), - Grantee: addrs[0].String(), + Granter: addr1, + Grantee: addr0, Grant: grant, }) suite.Require().NoError(err) totalGrants := 0 - suite.authzKeeper.IterateGrants(suite.ctx, func(sdk.AccAddress, sdk.AccAddress, authz.Grant) bool { + _ = suite.authzKeeper.IterateGrants(suite.ctx, func(sdk.AccAddress, sdk.AccAddress, authz.Grant) (bool, error) { totalGrants++ - return false + return false, nil }) suite.Require().Equal(len(addrs), totalGrants) @@ -404,13 +425,13 @@ func (suite *TestSuite) TestPruneExpiredGrants() { headerInfo.Time = headerInfo.Time.Add(2 * time.Hour) suite.ctx = suite.ctx.WithHeaderInfo(headerInfo) - _, err = suite.authzKeeper.PruneExpiredGrants(suite.ctx, &authz.MsgPruneExpiredGrants{Pruner: addrs[0].String()}) + _, err = suite.authzKeeper.PruneExpiredGrants(suite.ctx, &authz.MsgPruneExpiredGrants{Pruner: addr0}) suite.Require().NoError(err) totalGrants = 0 - suite.authzKeeper.IterateGrants(suite.ctx, func(sdk.AccAddress, sdk.AccAddress, authz.Grant) bool { + _ = suite.authzKeeper.IterateGrants(suite.ctx, func(sdk.AccAddress, sdk.AccAddress, authz.Grant) (bool, error) { totalGrants++ - return false + return false, nil }) suite.Require().Equal(0, totalGrants) } diff --git a/x/authz/module/abci_test.go b/x/authz/module/abci_test.go index ba2ae1914a85..b8765e1fbe0d 100644 --- a/x/authz/module/abci_test.go +++ b/x/authz/module/abci_test.go @@ -71,7 +71,8 @@ func TestExpiredGrantsQueue(t *testing.T) { save := func(grantee sdk.AccAddress, exp *time.Time) { err := authzKeeper.SaveGrant(ctx, grantee, granter, sendAuthz, exp) - require.NoError(t, err, "Grant from %s", grantee.String()) + addr, _ := accountKeeper.AddressCodec().BytesToString(grantee) + require.NoError(t, err, "Grant from %s", addr) } save(grantee1, &expiration) save(grantee2, &expiration) @@ -86,8 +87,10 @@ func TestExpiredGrantsQueue(t *testing.T) { err := authzmodule.BeginBlocker(ctx, authzKeeper) require.NoError(t, err) + addr, err := accountKeeper.AddressCodec().BytesToString(granter) + require.NoError(t, err) res, err := queryClient.GranterGrants(ctx.Context(), &authz.QueryGranterGrantsRequest{ - Granter: granter.String(), + Granter: addr, }) require.NoError(t, err) require.NotNil(t, res) diff --git a/x/authz/module/module.go b/x/authz/module/module.go index a19447ba36c2..bf39198c6412 100644 --- a/x/authz/module/module.go +++ b/x/authz/module/module.go @@ -134,7 +134,10 @@ func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) error // ExportGenesis returns the exported genesis state as raw bytes for the authz module. func (am AppModule) ExportGenesis(ctx context.Context) (json.RawMessage, error) { - gs := am.keeper.ExportGenesis(ctx) + gs, err := am.keeper.ExportGenesis(ctx) + if err != nil { + return nil, err + } return am.cdc.MarshalJSON(gs) } diff --git a/x/authz/msgs.go b/x/authz/msgs.go index ad7b34fbf1c7..0964f45cb34b 100644 --- a/x/authz/msgs.go +++ b/x/authz/msgs.go @@ -20,10 +20,10 @@ var ( ) // NewMsgGrant creates a new MsgGrant -func NewMsgGrant(granter, grantee sdk.AccAddress, a Authorization, expiration *time.Time) (*MsgGrant, error) { +func NewMsgGrant(granter, grantee string, a Authorization, expiration *time.Time) (*MsgGrant, error) { m := &MsgGrant{ - Granter: granter.String(), - Grantee: grantee.String(), + Granter: granter, + Grantee: grantee, Grant: Grant{Expiration: expiration}, } err := m.SetAuthorization(a) @@ -71,16 +71,16 @@ func (msg MsgGrant) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error { } // NewMsgRevoke creates a new MsgRevoke -func NewMsgRevoke(granter, grantee sdk.AccAddress, msgTypeURL string) MsgRevoke { +func NewMsgRevoke(granter, grantee, msgTypeURL string) MsgRevoke { return MsgRevoke{ - Granter: granter.String(), - Grantee: grantee.String(), + Granter: granter, + Grantee: grantee, MsgTypeUrl: msgTypeURL, } } // NewMsgExec creates a new MsgExecAuthorized -func NewMsgExec(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExec { +func NewMsgExec(grantee string, msgs []sdk.Msg) MsgExec { msgsAny := make([]*cdctypes.Any, len(msgs)) for i, msg := range msgs { any, err := cdctypes.NewAnyWithValue(msg) @@ -92,7 +92,7 @@ func NewMsgExec(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExec { } return MsgExec{ - Grantee: grantee.String(), + Grantee: grantee, Msgs: msgsAny, } } diff --git a/x/authz/msgs_test.go b/x/authz/msgs_test.go index bd0a230be281..194f93d417f3 100644 --- a/x/authz/msgs_test.go +++ b/x/authz/msgs_test.go @@ -72,7 +72,7 @@ func TestAminoJSON(t *testing.T) { sendAuthz := banktypes.NewSendAuthorization(sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(1000))), nil) sendGrant, err := authz.NewGrant(blockTime, sendAuthz, &expiresAt) require.NoError(t, err) - valAddr, err := sdk.ValAddressFromBech32("cosmosvaloper1xcy3els9ua75kdm783c3qu0rfa2eples6eavqq") + valAddr, err := valAddressCodec.StringToBytes("cosmosvaloper1xcy3els9ua75kdm783c3qu0rfa2eples6eavqq") require.NoError(t, err) stakingAuth, err := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{valAddr}, nil, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &sdk.Coin{Denom: "stake", Amount: sdkmath.NewInt(1000)}, valAddressCodec) require.NoError(t, err) diff --git a/x/authz/simulation/genesis.go b/x/authz/simulation/genesis.go index 0fc5c757cf71..073bdffabc8e 100644 --- a/x/authz/simulation/genesis.go +++ b/x/authz/simulation/genesis.go @@ -5,6 +5,7 @@ import ( "time" v1 "cosmossdk.io/api/cosmos/gov/v1" + "cosmossdk.io/core/address" sdkmath "cosmossdk.io/math" "cosmossdk.io/x/authz" banktypes "cosmossdk.io/x/bank/types" @@ -16,7 +17,7 @@ import ( ) // genGrant returns a slice of authorization grants. -func genGrant(r *rand.Rand, accounts []simtypes.Account, genT time.Time) []authz.GrantAuthorization { +func genGrant(r *rand.Rand, accounts []simtypes.Account, genT time.Time, cdc address.Codec) []authz.GrantAuthorization { authorizations := make([]authz.GrantAuthorization, len(accounts)-1) for i := 0; i < len(accounts)-1; i++ { granter := accounts[i] @@ -26,9 +27,11 @@ func genGrant(r *rand.Rand, accounts []simtypes.Account, genT time.Time) []authz e := genT.AddDate(1, 0, 0) expiration = &e } + granterAddr, _ := cdc.BytesToString(granter.Address) + granteeAddr, _ := cdc.BytesToString(grantee.Address) authorizations[i] = authz.GrantAuthorization{ - Granter: granter.Address.String(), - Grantee: grantee.Address.String(), + Granter: granterAddr, + Grantee: granteeAddr, Authorization: generateRandomGrant(r), Expiration: expiration, } @@ -59,7 +62,7 @@ func newAnyAuthorization(a authz.Authorization) *codectypes.Any { func RandomizedGenState(simState *module.SimulationState) { var grants []authz.GrantAuthorization simState.AppParams.GetOrGenerate("authz", &grants, simState.Rand, func(r *rand.Rand) { - grants = genGrant(r, simState.Accounts, simState.GenTimestamp) + grants = genGrant(r, simState.Accounts, simState.GenTimestamp, simState.AddressCodec) }) authzGrantsGenesis := authz.NewGenesisState(grants) diff --git a/x/authz/simulation/operations.go b/x/authz/simulation/operations.go index c73c562b5f37..657383d37b5e 100644 --- a/x/authz/simulation/operations.go +++ b/x/authz/simulation/operations.go @@ -123,7 +123,15 @@ func SimulateMsgGrant( } randomAuthz := generateRandomAuthorization(r, spendLimit) - msg, err := authz.NewMsgGrant(granter.Address, grantee.Address, randomAuthz, expiration) + granterAddr, err := ak.AddressCodec().BytesToString(granter.Address) + if err != nil { + return simtypes.NoOpMsg(authz.ModuleName, TypeMsgGrant, "could not get granter address"), nil, nil + } + granteeAddr, err := ak.AddressCodec().BytesToString(grantee.Address) + if err != nil { + return simtypes.NoOpMsg(authz.ModuleName, TypeMsgGrant, "could not get grantee address"), nil, nil + } + msg, err := authz.NewMsgGrant(granterAddr, granteeAddr, randomAuthz, expiration) if err != nil { return simtypes.NoOpMsg(authz.ModuleName, TypeMsgGrant, err.Error()), nil, err } @@ -174,13 +182,16 @@ func SimulateMsgRevoke( var grant authz.Grant hasGrant := false - k.IterateGrants(ctx, func(granter, grantee sdk.AccAddress, g authz.Grant) bool { + err := k.IterateGrants(ctx, func(granter, grantee sdk.AccAddress, g authz.Grant) (bool, error) { grant = g granterAddr = granter granteeAddr = grantee hasGrant = true - return true + return true, nil }) + if err != nil { + return simtypes.NoOpMsg(authz.ModuleName, TypeMsgRevoke, err.Error()), nil, err + } if !hasGrant { return simtypes.NoOpMsg(authz.ModuleName, TypeMsgRevoke, "no grants"), nil, nil @@ -202,7 +213,15 @@ func SimulateMsgRevoke( return simtypes.NoOpMsg(authz.ModuleName, TypeMsgRevoke, "authorization error"), nil, err } - msg := authz.NewMsgRevoke(granterAddr, granteeAddr, a.MsgTypeURL()) + granterStrAddr, err := ak.AddressCodec().BytesToString(granterAddr) + if err != nil { + return simtypes.NoOpMsg(authz.ModuleName, TypeMsgRevoke, "could not get granter address"), nil, nil + } + granteeStrAddr, err := ak.AddressCodec().BytesToString(granteeAddr) + if err != nil { + return simtypes.NoOpMsg(authz.ModuleName, TypeMsgRevoke, "could not get grantee address"), nil, nil + } + msg := authz.NewMsgRevoke(granterStrAddr, granteeStrAddr, a.MsgTypeURL()) account := ak.GetAccount(ctx, granterAddr) tx, err := simtestutil.GenSignedMockTx( r, @@ -244,19 +263,18 @@ func SimulateMsgExec( var granteeAddr sdk.AccAddress var sendAuth *banktype.SendAuthorization var err error - k.IterateGrants(ctx, func(granter, grantee sdk.AccAddress, grant authz.Grant) bool { + err = k.IterateGrants(ctx, func(granter, grantee sdk.AccAddress, grant authz.Grant) (bool, error) { granterAddr = granter granteeAddr = grantee var a authz.Authorization a, err = grant.GetAuthorization() if err != nil { - return true + return true, err } var ok bool sendAuth, ok = a.(*banktype.SendAuthorization) - return ok + return ok, nil }) - if err != nil { return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, err.Error()), nil, err } @@ -305,7 +323,7 @@ func SimulateMsgExec( } - msgExec := authz.NewMsgExec(granteeAddr, msg) + msgExec := authz.NewMsgExec(greStr, msg) granteeSpendableCoins := bk.SpendableCoins(ctx, granteeAddr) fees, err := simtypes.RandomFees(r, granteeSpendableCoins) if err != nil {