diff --git a/x/authz/keeper/genesis_test.go b/x/authz/keeper/genesis_test.go index 077d2a162bd1..d651aa68c5b3 100644 --- a/x/authz/keeper/genesis_test.go +++ b/x/authz/keeper/genesis_test.go @@ -8,9 +8,10 @@ import ( tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" - "github.com/cosmos/cosmos-sdk/simapp" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/authz/keeper" + "github.com/cosmos/cosmos-sdk/x/authz/testutil" bank "github.com/cosmos/cosmos-sdk/x/bank/types" ) @@ -22,11 +23,13 @@ type GenesisTestSuite struct { } func (suite *GenesisTestSuite) SetupTest() { - checkTx := false - app := simapp.Setup(suite.T(), checkTx) + app, err := simtestutil.Setup( + testutil.AppConfig, + &suite.keeper, + ) + suite.Require().NoError(err) - suite.ctx = app.BaseApp.NewContext(checkTx, tmproto.Header{Height: 1}) - suite.keeper = app.AuthzKeeper + suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{Height: 1}) } var ( diff --git a/x/authz/keeper/grpc_query_test.go b/x/authz/keeper/grpc_query_test.go index d2f0b09e8b19..8b05ff8ce265 100644 --- a/x/authz/keeper/grpc_query_test.go +++ b/x/authz/keeper/grpc_query_test.go @@ -69,7 +69,7 @@ func (suite *TestSuite) TestGRPCQueryAuthorization() { func(require *require.Assertions, res *authz.QueryGrantsResponse) { var auth authz.Authorization require.Equal(1, len(res.Grants)) - err := suite.app.InterfaceRegistry().UnpackAny(res.Grants[0].Authorization, &auth) + err := suite.interfaceRegistry.UnpackAny(res.Grants[0].Authorization, &auth) require.NoError(err) require.NotNil(auth) require.Equal(auth.String(), expAuthorization.String()) @@ -135,7 +135,7 @@ func (suite *TestSuite) TestGRPCQueryAuthorizations() { func(res *authz.QueryGrantsResponse) { var auth authz.Authorization suite.Require().Equal(1, len(res.Grants)) - err := suite.app.InterfaceRegistry().UnpackAny(res.Grants[0].Authorization, &auth) + err := suite.interfaceRegistry.UnpackAny(res.Grants[0].Authorization, &auth) suite.Require().NoError(err) suite.Require().NotNil(auth) suite.Require().Equal(auth.String(), expAuthorization.String()) @@ -296,7 +296,7 @@ func (suite *TestSuite) createSendAuthorization(a1, a2 sdk.AccAddress) authz.Aut exp := suite.ctx.BlockHeader().Time.Add(time.Hour) newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) authorization := &banktypes.SendAuthorization{SpendLimit: newCoins} - err := suite.app.AuthzKeeper.SaveGrant(suite.ctx, a1, a2, authorization, &exp) + err := suite.authzKeeper.SaveGrant(suite.ctx, a1, a2, authorization, &exp) suite.Require().NoError(err) return authorization } diff --git a/x/authz/keeper/keeper_test.go b/x/authz/keeper/keeper_test.go index b9f41bc74919..dcb2496331aa 100644 --- a/x/authz/keeper/keeper_test.go +++ b/x/authz/keeper/keeper_test.go @@ -9,11 +9,16 @@ import ( tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/simapp" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/authz" - "github.com/cosmos/cosmos-sdk/x/bank/testutil" + authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper" + "github.com/cosmos/cosmos-sdk/x/authz/testutil" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" ) var ( @@ -26,30 +31,41 @@ var ( type TestSuite struct { suite.Suite - app *simapp.SimApp - ctx sdk.Context - addrs []sdk.AccAddress - queryClient authz.QueryClient + ctx sdk.Context + addrs []sdk.AccAddress + authzKeeper authzkeeper.Keeper + bankKeeper bankkeeper.Keeper + interfaceRegistry codectypes.InterfaceRegistry + queryClient authz.QueryClient } func (s *TestSuite) SetupTest() { - app := simapp.Setup(s.T(), false) + var stakingKeeper *stakingkeeper.Keeper + + app, err := simtestutil.Setup( + testutil.AppConfig, + &s.bankKeeper, + &stakingKeeper, + &s.authzKeeper, + &s.interfaceRegistry, + ) + s.Require().NoError(err) + ctx := app.BaseApp.NewContext(false, tmproto.Header{}) now := tmtime.Now() ctx = ctx.WithBlockHeader(tmproto.Header{Time: now}) - queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry()) - authz.RegisterQueryServer(queryHelper, app.AuthzKeeper) + queryHelper := baseapp.NewQueryServerTestHelper(ctx, s.interfaceRegistry) + authz.RegisterQueryServer(queryHelper, s.authzKeeper) queryClient := authz.NewQueryClient(queryHelper) s.queryClient = queryClient - s.app = app s.ctx = ctx s.queryClient = queryClient - s.addrs = simapp.AddTestAddrsIncremental(app, ctx, 3, sdk.NewInt(30000000)) + s.addrs = simtestutil.AddTestAddrsIncremental(s.bankKeeper, stakingKeeper, ctx, 3, sdk.NewInt(30000000)) } func (s *TestSuite) TestKeeper() { - app, ctx, addrs := s.app, s.ctx, s.addrs + ctx, addrs := s.ctx, s.addrs now := ctx.BlockTime() require := s.Require() @@ -57,39 +73,39 @@ func (s *TestSuite) TestKeeper() { granteeAddr := addrs[1] s.T().Log("verify that no authorization returns nil") - authorizations, err := app.AuthzKeeper.GetAuthorizations(ctx, granteeAddr, granterAddr) + authorizations, err := s.authzKeeper.GetAuthorizations(ctx, granteeAddr, granterAddr) require.NoError(err) require.Len(authorizations, 0) s.T().Log("verify save, get and delete") sendAutz := &banktypes.SendAuthorization{SpendLimit: coins100} expire := now.AddDate(1, 0, 0) - err = app.AuthzKeeper.SaveGrant(ctx, granteeAddr, granterAddr, sendAutz, &expire) + err = s.authzKeeper.SaveGrant(ctx, granteeAddr, granterAddr, sendAutz, &expire) require.NoError(err) - authorizations, err = app.AuthzKeeper.GetAuthorizations(ctx, granteeAddr, granterAddr) + authorizations, err = s.authzKeeper.GetAuthorizations(ctx, granteeAddr, granterAddr) require.NoError(err) require.Len(authorizations, 1) - err = app.AuthzKeeper.DeleteGrant(ctx, granteeAddr, granterAddr, sendAutz.MsgTypeURL()) + err = s.authzKeeper.DeleteGrant(ctx, granteeAddr, granterAddr, sendAutz.MsgTypeURL()) require.NoError(err) - authorizations, err = app.AuthzKeeper.GetAuthorizations(ctx, granteeAddr, granterAddr) + authorizations, err = s.authzKeeper.GetAuthorizations(ctx, granteeAddr, granterAddr) require.NoError(err) require.Len(authorizations, 0) s.T().Log("verify granting same authorization overwrite existing authorization") - err = app.AuthzKeeper.SaveGrant(ctx, granteeAddr, granterAddr, sendAutz, &expire) + err = s.authzKeeper.SaveGrant(ctx, granteeAddr, granterAddr, sendAutz, &expire) require.NoError(err) - authorizations, err = app.AuthzKeeper.GetAuthorizations(ctx, granteeAddr, granterAddr) + authorizations, err = s.authzKeeper.GetAuthorizations(ctx, granteeAddr, granterAddr) require.NoError(err) require.Len(authorizations, 1) sendAutz = &banktypes.SendAuthorization{SpendLimit: coins1000} - err = app.AuthzKeeper.SaveGrant(ctx, granteeAddr, granterAddr, sendAutz, &expire) + err = s.authzKeeper.SaveGrant(ctx, granteeAddr, granterAddr, sendAutz, &expire) require.NoError(err) - authorizations, err = app.AuthzKeeper.GetAuthorizations(ctx, granteeAddr, granterAddr) + authorizations, err = s.authzKeeper.GetAuthorizations(ctx, granteeAddr, granterAddr) require.NoError(err) require.Len(authorizations, 1) authorization := authorizations[0] @@ -98,22 +114,22 @@ func (s *TestSuite) TestKeeper() { require.Equal(sendAuth.MsgTypeURL(), sendAutz.MsgTypeURL()) s.T().Log("verify removing non existing authorization returns error") - err = app.AuthzKeeper.DeleteGrant(ctx, granterAddr, granteeAddr, "abcd") + err = s.authzKeeper.DeleteGrant(ctx, granterAddr, granteeAddr, "abcd") s.Require().Error(err) } func (s *TestSuite) TestKeeperIter() { - app, ctx, addrs := s.app, s.ctx, s.addrs + ctx, addrs := s.ctx, s.addrs granterAddr := addrs[0] granteeAddr := addrs[1] granter2Addr := addrs[2] e := ctx.BlockTime().AddDate(1, 0, 0) - s.app.AuthzKeeper.SaveGrant(ctx, granteeAddr, granterAddr, banktypes.NewSendAuthorization(coins100), &e) - s.app.AuthzKeeper.SaveGrant(ctx, granteeAddr, granter2Addr, banktypes.NewSendAuthorization(coins100), &e) + s.authzKeeper.SaveGrant(ctx, granteeAddr, granterAddr, banktypes.NewSendAuthorization(coins100), &e) + s.authzKeeper.SaveGrant(ctx, granteeAddr, granter2Addr, banktypes.NewSendAuthorization(coins100), &e) - app.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 { s.Require().Equal(granteeAddr, grantee) s.Require().Contains([]sdk.AccAddress{granterAddr, granter2Addr}, granter) return true @@ -121,7 +137,7 @@ func (s *TestSuite) TestKeeperIter() { } func (s *TestSuite) TestDispatchAction() { - app, addrs := s.app, s.addrs + addrs := s.addrs require := s.Require() now := s.ctx.BlockTime() @@ -130,7 +146,7 @@ func (s *TestSuite) TestDispatchAction() { recipientAddr := addrs[2] a := banktypes.NewSendAuthorization(coins100) - require.NoError(testutil.FundAccount(app.BankKeeper, s.ctx, granterAddr, coins1000)) + require.NoError(banktestutil.FundAccount(s.bankKeeper, s.ctx, granterAddr, coins1000)) testCases := []struct { name string @@ -153,7 +169,7 @@ func (s *TestSuite) TestDispatchAction() { "authorization not found", func() sdk.Context { // remove any existing authorizations - app.AuthzKeeper.DeleteGrant(s.ctx, granteeAddr, granterAddr, bankSendAuthMsgType) + s.authzKeeper.DeleteGrant(s.ctx, granteeAddr, granterAddr, bankSendAuthMsgType) return s.ctx }, func() {}, @@ -171,7 +187,7 @@ func (s *TestSuite) TestDispatchAction() { "authorization expired", func() sdk.Context { e := now.AddDate(0, 0, 1) - err := app.AuthzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, a, &e) + err := s.authzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, a, &e) require.NoError(err) return s.ctx.WithBlockTime(s.ctx.BlockTime().AddDate(0, 0, 2)) }, @@ -190,7 +206,7 @@ func (s *TestSuite) TestDispatchAction() { "requested amount is more than spend limit", func() sdk.Context { e := now.AddDate(0, 1, 0) - err := app.AuthzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, a, &e) + err := s.authzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, a, &e) require.NoError(err) return s.ctx }, @@ -209,12 +225,12 @@ func (s *TestSuite) TestDispatchAction() { "", func() sdk.Context { e := now.AddDate(0, 1, 0) - err := app.AuthzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, a, &e) + err := s.authzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, a, &e) require.NoError(err) return s.ctx }, func() { - authzs, err := app.AuthzKeeper.GetAuthorizations(s.ctx, granteeAddr, granterAddr) + authzs, err := s.authzKeeper.GetAuthorizations(s.ctx, granteeAddr, granterAddr) require.NoError(err) require.Len(authzs, 1) authorization := authzs[0].(*banktypes.SendAuthorization) @@ -235,12 +251,12 @@ func (s *TestSuite) TestDispatchAction() { "", func() sdk.Context { e := now.AddDate(0, 1, 0) - err := app.AuthzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, a, &e) + err := s.authzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, a, &e) require.NoError(err) return s.ctx }, func() { - authzs, err := app.AuthzKeeper.GetAuthorizations(s.ctx, granteeAddr, granterAddr) + authzs, err := s.authzKeeper.GetAuthorizations(s.ctx, granteeAddr, granterAddr) require.NoError(err) require.Len(authzs, 0) }, @@ -252,7 +268,7 @@ func (s *TestSuite) TestDispatchAction() { ctx := tc.preRun() executeMsgs, err := tc.req.GetMessages() require.NoError(err) - result, err := app.AuthzKeeper.DispatchActions(ctx, granteeAddr, executeMsgs) + result, err := s.authzKeeper.DispatchActions(ctx, granteeAddr, executeMsgs) if tc.expectErr { require.Error(err) require.Nil(result) @@ -270,11 +286,11 @@ func (s *TestSuite) TestDispatchAction() { // Ref: https://github.com/cosmos/cosmos-sdk/issues/9501 func (s *TestSuite) TestDispatchedEvents() { require := s.Require() - app, addrs := s.app, s.addrs + addrs := s.addrs granterAddr := addrs[0] granteeAddr := addrs[1] recipientAddr := addrs[2] - require.NoError(testutil.FundAccount(app.BankKeeper, s.ctx, granterAddr, coins1000)) + require.NoError(banktestutil.FundAccount(s.bankKeeper, s.ctx, granterAddr, coins1000)) expiration := s.ctx.BlockTime().Add(1 * time.Second) // must be in the future msgs := authz.NewMsgExec(granteeAddr, []sdk.Msg{ @@ -286,9 +302,9 @@ func (s *TestSuite) TestDispatchedEvents() { }) // grant authorization - err := app.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 := app.AuthzKeeper.GetAuthorizations(s.ctx, granteeAddr, granterAddr) + authorizations, err := s.authzKeeper.GetAuthorizations(s.ctx, granteeAddr, granterAddr) require.NoError(err) require.Len(authorizations, 1) authorization := authorizations[0].(*banktypes.SendAuthorization) @@ -297,7 +313,7 @@ func (s *TestSuite) TestDispatchedEvents() { executeMsgs, err := msgs.GetMessages() require.NoError(err) - result, err := app.AuthzKeeper.DispatchActions(s.ctx, granteeAddr, executeMsgs) + result, err := s.authzKeeper.DispatchActions(s.ctx, granteeAddr, executeMsgs) require.NoError(err) require.NotNil(result) events := s.ctx.EventManager().Events() @@ -319,7 +335,7 @@ func (s *TestSuite) TestDispatchedEvents() { func (s *TestSuite) TestDequeueAllGrantsQueue() { require := s.Require() - app, addrs := s.app, s.addrs + addrs := s.addrs granter := addrs[0] grantee := addrs[1] grantee1 := addrs[2] @@ -327,38 +343,38 @@ func (s *TestSuite) TestDequeueAllGrantsQueue() { a := banktypes.SendAuthorization{SpendLimit: coins100} // create few authorizations - err := app.AuthzKeeper.SaveGrant(s.ctx, grantee, granter, &a, &exp) + err := s.authzKeeper.SaveGrant(s.ctx, grantee, granter, &a, &exp) require.NoError(err) - err = app.AuthzKeeper.SaveGrant(s.ctx, grantee1, granter, &a, &exp) + err = s.authzKeeper.SaveGrant(s.ctx, grantee1, granter, &a, &exp) require.NoError(err) exp2 := exp.AddDate(0, 1, 0) - err = app.AuthzKeeper.SaveGrant(s.ctx, granter, grantee1, &a, &exp2) + err = s.authzKeeper.SaveGrant(s.ctx, granter, grantee1, &a, &exp2) require.NoError(err) exp2 = exp.AddDate(2, 0, 0) - err = app.AuthzKeeper.SaveGrant(s.ctx, granter, grantee, &a, &exp2) + err = s.authzKeeper.SaveGrant(s.ctx, granter, grantee, &a, &exp2) require.NoError(err) newCtx := s.ctx.WithBlockTime(exp.AddDate(1, 0, 0)) - err = app.AuthzKeeper.DequeueAndDeleteExpiredGrants(newCtx) + err = s.authzKeeper.DequeueAndDeleteExpiredGrants(newCtx) require.NoError(err) s.T().Log("verify expired grants are pruned from the state") - authzs, err := app.AuthzKeeper.GetAuthorizations(newCtx, grantee, granter) + authzs, err := s.authzKeeper.GetAuthorizations(newCtx, grantee, granter) require.NoError(err) require.Len(authzs, 0) - authzs, err = app.AuthzKeeper.GetAuthorizations(newCtx, granter, grantee1) + authzs, err = s.authzKeeper.GetAuthorizations(newCtx, granter, grantee1) require.NoError(err) require.Len(authzs, 0) - authzs, err = app.AuthzKeeper.GetAuthorizations(newCtx, grantee1, granter) + authzs, err = s.authzKeeper.GetAuthorizations(newCtx, grantee1, granter) require.NoError(err) require.Len(authzs, 0) - authzs, err = app.AuthzKeeper.GetAuthorizations(newCtx, granter, grantee) + authzs, err = s.authzKeeper.GetAuthorizations(newCtx, granter, grantee) require.NoError(err) require.Len(authzs, 1) } diff --git a/x/authz/migrations/v046/store_test.go b/x/authz/migrations/v046/store_test.go index 1142c115cb0d..922b21b30b36 100644 --- a/x/authz/migrations/v046/store_test.go +++ b/x/authz/migrations/v046/store_test.go @@ -4,21 +4,24 @@ import ( "testing" "time" + "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" - "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/depinject" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/authz" v046 "github.com/cosmos/cosmos-sdk/x/authz/migrations/v046" + authztestutil "github.com/cosmos/cosmos-sdk/x/authz/testutil" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" "github.com/stretchr/testify/require" ) func TestMigration(t *testing.T) { - encCfg := simapp.MakeTestEncodingConfig() - cdc := encCfg.Codec + var cdc codec.Codec + depinject.Inject(authztestutil.AppConfig, &cdc) + authzKey := sdk.NewKVStoreKey("authz") ctx := testutil.DefaultContext(authzKey, sdk.NewTransientStoreKey("transient_test")) granter1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address()) diff --git a/x/authz/module/abci_test.go b/x/authz/module/abci_test.go index 9b58c5eadbc7..f1c447739a83 100644 --- a/x/authz/module/abci_test.go +++ b/x/authz/module/abci_test.go @@ -5,19 +5,37 @@ import ( "time" "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/simapp" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/authz" + "github.com/cosmos/cosmos-sdk/x/authz/keeper" authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module" + "github.com/cosmos/cosmos-sdk/x/authz/testutil" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/proto/tendermint/types" ) func TestExpiredGrantsQueue(t *testing.T) { - app := simapp.Setup(t, false) + var interfaceRegistry codectypes.InterfaceRegistry + var authzKeeper keeper.Keeper + var bankKeeper bankkeeper.Keeper + var stakingKeeper *stakingkeeper.Keeper + + app, err := simtestutil.Setup( + testutil.AppConfig, + &interfaceRegistry, + &authzKeeper, + &bankKeeper, + &stakingKeeper, + ) + require.NoError(t, err) + ctx := app.BaseApp.NewContext(false, types.Header{}) - addrs := simapp.AddTestAddrsIncremental(app, ctx, 5, sdk.NewInt(30000000)) + addrs := simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, ctx, 5, sdk.NewInt(30000000)) granter := addrs[0] grantee1 := addrs[1] grantee2 := addrs[2] @@ -28,7 +46,7 @@ func TestExpiredGrantsQueue(t *testing.T) { smallCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 10)) save := func(grantee sdk.AccAddress, exp *time.Time) { - err := app.AuthzKeeper.SaveGrant(ctx, grantee, granter, banktypes.NewSendAuthorization(smallCoins), exp) + err := authzKeeper.SaveGrant(ctx, grantee, granter, banktypes.NewSendAuthorization(smallCoins), exp) require.NoError(t, err, "Grant from %s", grantee.String()) } save(grantee1, &expiration) @@ -36,12 +54,12 @@ func TestExpiredGrantsQueue(t *testing.T) { save(grantee3, &expiration2) save(grantee4, nil) - queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry()) - authz.RegisterQueryServer(queryHelper, app.AuthzKeeper) + queryHelper := baseapp.NewQueryServerTestHelper(ctx, interfaceRegistry) + authz.RegisterQueryServer(queryHelper, authzKeeper) queryClient := authz.NewQueryClient(queryHelper) checkGrants := func(ctx sdk.Context, expectedNum int) { - authzmodule.BeginBlocker(ctx, app.AuthzKeeper) + authzmodule.BeginBlocker(ctx, authzKeeper) res, err := queryClient.GranterGrants(ctx.Context(), &authz.QueryGranterGrantsRequest{ Granter: granter.String(), diff --git a/x/authz/module/module.go b/x/authz/module/module.go index 1fd7bf658c08..0ee9a8a2ddc9 100644 --- a/x/authz/module/module.go +++ b/x/authz/module/module.go @@ -193,16 +193,23 @@ type authzInputs struct { Key *store.KVStoreKey Cdc codec.Codec - AccountKeeper authz.AccountKeeper `key:"cosmos.auth.v1.AccountKeeper"` - BankKeeper authz.BankKeeper `key:"cosmos.bank.v1.Keeper"` + AccountKeeper authz.AccountKeeper + BankKeeper authz.BankKeeper Registry cdctypes.InterfaceRegistry MsgServiceRouter *baseapp.MsgServiceRouter } -func provideModule(in authzInputs) (keeper.Keeper, runtime.AppModuleWrapper) { +type authzOutputs struct { + depinject.Out + + AuthzKeeper keeper.Keeper + Module runtime.AppModuleWrapper +} + +func provideModule(in authzInputs) authzOutputs { k := keeper.NewKeeper(in.Key, in.Cdc, in.MsgServiceRouter, in.AccountKeeper) m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.Registry) - return k, runtime.WrapAppModule(m) + return authzOutputs{AuthzKeeper: k, Module: runtime.WrapAppModule(m)} } // ____________________________________________________________________________ @@ -233,7 +240,8 @@ func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { // WeightedOperations returns the all the gov module operations with their respective weights. func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { return simulation.WeightedOperations( + am.registry, simState.AppParams, simState.Cdc, - am.accountKeeper, am.bankKeeper, am.keeper, am.cdc, + am.accountKeeper, am.bankKeeper, am.keeper, ) } diff --git a/x/authz/simulation/decoder_test.go b/x/authz/simulation/decoder_test.go index c169e84137f6..e212a860a179 100644 --- a/x/authz/simulation/decoder_test.go +++ b/x/authz/simulation/decoder_test.go @@ -7,17 +7,21 @@ import ( "github.com/stretchr/testify/require" - "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/depinject" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/kv" "github.com/cosmos/cosmos-sdk/x/authz" "github.com/cosmos/cosmos-sdk/x/authz/keeper" "github.com/cosmos/cosmos-sdk/x/authz/simulation" + "github.com/cosmos/cosmos-sdk/x/authz/testutil" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" ) func TestDecodeStore(t *testing.T) { - cdc := simapp.MakeTestEncodingConfig().Codec + var cdc codec.Codec + depinject.Inject(testutil.AppConfig, &cdc) + dec := simulation.NewDecodeStore(cdc) now := time.Now().UTC() diff --git a/x/authz/simulation/genesis_test.go b/x/authz/simulation/genesis_test.go index 8f313be8eab1..272186197214 100644 --- a/x/authz/simulation/genesis_test.go +++ b/x/authz/simulation/genesis_test.go @@ -8,22 +8,25 @@ import ( "github.com/stretchr/testify/require" sdkmath "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/depinject" "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/cosmos/cosmos-sdk/x/authz" "github.com/cosmos/cosmos-sdk/x/authz/simulation" + "github.com/cosmos/cosmos-sdk/x/authz/testutil" ) func TestRandomizedGenState(t *testing.T) { - app := simapp.Setup(t, false) + var cdc codec.Codec + depinject.Inject(testutil.AppConfig, &cdc) s := rand.NewSource(1) r := rand.New(s) simState := module.SimulationState{ AppParams: make(simtypes.AppParams), - Cdc: app.AppCodec(), + Cdc: cdc, Rand: r, NumBonded: 3, Accounts: simtypes.RandomAccounts(r, 3), diff --git a/x/authz/simulation/operations.go b/x/authz/simulation/operations.go index 5209ca034306..d78744a1bd49 100644 --- a/x/authz/simulation/operations.go +++ b/x/authz/simulation/operations.go @@ -7,11 +7,11 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" cdctypes "github.com/cosmos/cosmos-sdk/codec/types" - simappparams "github.com/cosmos/cosmos-sdk/simapp/params" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "github.com/cosmos/cosmos-sdk/x/auth/tx" "github.com/cosmos/cosmos-sdk/x/authz" "github.com/cosmos/cosmos-sdk/x/authz/keeper" banktype "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -42,7 +42,12 @@ const ( // WeightedOperations returns all the operations from the module with their respective weights func WeightedOperations( - appParams simtypes.AppParams, cdc codec.JSONCodec, ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keeper, appCdc cdctypes.AnyUnpacker, + registry cdctypes.InterfaceRegistry, + appParams simtypes.AppParams, + cdc codec.JSONCodec, + ak authz.AccountKeeper, + bk authz.BankKeeper, + k keeper.Keeper, ) simulation.WeightedOperations { var ( weightMsgGrant int @@ -71,21 +76,21 @@ func WeightedOperations( return simulation.WeightedOperations{ simulation.NewWeightedOperation( weightMsgGrant, - SimulateMsgGrant(ak, bk, k), + SimulateMsgGrant(codec.NewProtoCodec(registry), ak, bk, k), ), simulation.NewWeightedOperation( weightExec, - SimulateMsgExec(ak, bk, k, appCdc), + SimulateMsgExec(codec.NewProtoCodec(registry), ak, bk, k, registry), ), simulation.NewWeightedOperation( weightRevoke, - SimulateMsgRevoke(ak, bk, k), + SimulateMsgRevoke(codec.NewProtoCodec(registry), ak, bk, k), ), } } // SimulateMsgGrant generates a MsgGrant with random values. -func SimulateMsgGrant(ak authz.AccountKeeper, bk authz.BankKeeper, _ keeper.Keeper) simtypes.Operation { +func SimulateMsgGrant(cdc *codec.ProtoCodec, ak authz.AccountKeeper, bk authz.BankKeeper, _ keeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { @@ -117,7 +122,7 @@ func SimulateMsgGrant(ak authz.AccountKeeper, bk authz.BankKeeper, _ keeper.Keep if err != nil { return simtypes.NoOpMsg(authz.ModuleName, TypeMsgGrant, err.Error()), nil, err } - txCfg := simappparams.MakeTestEncodingConfig().TxConfig + txCfg := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( txCfg, []sdk.Msg{msg}, @@ -149,7 +154,7 @@ func generateRandomAuthorization(r *rand.Rand, spendLimit sdk.Coins) authz.Autho } // SimulateMsgRevoke generates a MsgRevoke with random values. -func SimulateMsgRevoke(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keeper) simtypes.Operation { +func SimulateMsgRevoke(cdc *codec.ProtoCodec, ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { @@ -186,7 +191,7 @@ func SimulateMsgRevoke(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Kee } msg := authz.NewMsgRevoke(granterAddr, granteeAddr, a.MsgTypeURL()) - txCfg := simappparams.MakeTestEncodingConfig().TxConfig + txCfg := tx.NewTxConfig(cdc, tx.DefaultSignModes) account := ak.GetAccount(ctx, granterAddr) tx, err := simtestutil.GenSignedMockTx( txCfg, @@ -212,7 +217,7 @@ func SimulateMsgRevoke(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Kee } // SimulateMsgExec generates a MsgExec with random values. -func SimulateMsgExec(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keeper, cdc cdctypes.AnyUnpacker) simtypes.Operation { +func SimulateMsgExec(cdc *codec.ProtoCodec, ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keeper, unpacker cdctypes.AnyUnpacker) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { @@ -275,7 +280,7 @@ func SimulateMsgExec(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keepe return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, "fee error"), nil, err } - txCfg := simappparams.MakeTestEncodingConfig().TxConfig + txCfg := tx.NewTxConfig(cdc, tx.DefaultSignModes) granteeAcc := ak.GetAccount(ctx, granteeAddr) tx, err := simtestutil.GenSignedMockTx( txCfg, @@ -296,7 +301,7 @@ func SimulateMsgExec(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keepe return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, err.Error()), nil, err } - err = msgExec.UnpackInterfaces(cdc) + err = msgExec.UnpackInterfaces(unpacker) if err != nil { return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, "unmarshal error"), nil, err } diff --git a/x/authz/simulation/operations_test.go b/x/authz/simulation/operations_test.go index 00735b175db0..b599a7d88068 100644 --- a/x/authz/simulation/operations_test.go +++ b/x/authz/simulation/operations_test.go @@ -9,35 +9,60 @@ import ( abci "github.com/tendermint/tendermint/abci/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/runtime" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" "github.com/cosmos/cosmos-sdk/x/authz" + authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper" "github.com/cosmos/cosmos-sdk/x/authz/simulation" - "github.com/cosmos/cosmos-sdk/x/bank/testutil" + "github.com/cosmos/cosmos-sdk/x/authz/testutil" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" ) type SimTestSuite struct { suite.Suite ctx sdk.Context - app *simapp.SimApp + + app *runtime.App + legacyAmino *codec.LegacyAmino + codec codec.Codec + interfaceRegistry codectypes.InterfaceRegistry + accountKeeper authkeeper.AccountKeeper + bankKeeper bankkeeper.Keeper + stakingKeeper *stakingkeeper.Keeper + authzKeeper authzkeeper.Keeper } func (suite *SimTestSuite) SetupTest() { - checkTx := false - app := simapp.Setup(suite.T(), checkTx) + app, err := simtestutil.Setup( + testutil.AppConfig, + &suite.legacyAmino, + &suite.codec, + &suite.interfaceRegistry, + &suite.accountKeeper, + &suite.bankKeeper, + &suite.stakingKeeper, + &suite.authzKeeper, + ) + suite.Require().NoError(err) suite.app = app - suite.ctx = app.BaseApp.NewContext(checkTx, tmproto.Header{}) + suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{}) } func (suite *SimTestSuite) TestWeightedOperations() { - cdc := suite.app.AppCodec() + cdc := suite.codec appParams := make(simtypes.AppParams) - weightedOps := simulation.WeightedOperations(appParams, cdc, suite.app.AccountKeeper, - suite.app.BankKeeper, suite.app.AuthzKeeper, cdc) + weightedOps := simulation.WeightedOperations(suite.interfaceRegistry, appParams, cdc, suite.accountKeeper, + suite.bankKeeper, suite.authzKeeper) s := rand.NewSource(3) r := rand.New(s) @@ -73,14 +98,14 @@ func (suite *SimTestSuite) TestWeightedOperations() { func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account { accounts := simtypes.RandomAccounts(r, n) - initAmt := suite.app.StakingKeeper.TokensFromConsensusPower(suite.ctx, 200000) + initAmt := suite.stakingKeeper.TokensFromConsensusPower(suite.ctx, 200000) initCoins := sdk.NewCoins(sdk.NewCoin("stake", initAmt)) // add coins to the accounts for _, account := range accounts { - acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, account.Address) - suite.app.AccountKeeper.SetAccount(suite.ctx, acc) - suite.Require().NoError(testutil.FundAccount(suite.app.BankKeeper, suite.ctx, account.Address, initCoins)) + acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, account.Address) + suite.accountKeeper.SetAccount(suite.ctx, acc) + suite.Require().NoError(banktestutil.FundAccount(suite.bankKeeper, suite.ctx, account.Address, initCoins)) } return accounts @@ -105,12 +130,12 @@ func (suite *SimTestSuite) TestSimulateGrant() { grantee := accounts[1] // execute operation - op := simulation.SimulateMsgGrant(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.AuthzKeeper) + op := simulation.SimulateMsgGrant(codec.NewProtoCodec(suite.interfaceRegistry), suite.accountKeeper, suite.bankKeeper, suite.authzKeeper) operationMsg, futureOperations, err := op(r, suite.app.BaseApp, ctx, accounts, "") suite.Require().NoError(err) var msg authz.MsgGrant - suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) + suite.legacyAmino.UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) suite.Require().Equal(granter.Address.String(), msg.Granter) suite.Require().Equal(grantee.Address.String(), msg.Grantee) @@ -131,7 +156,7 @@ func (suite *SimTestSuite) TestSimulateRevoke() { }, }) - initAmt := suite.app.StakingKeeper.TokensFromConsensusPower(suite.ctx, 200000) + initAmt := suite.stakingKeeper.TokensFromConsensusPower(suite.ctx, 200000) initCoins := sdk.NewCoins(sdk.NewCoin("stake", initAmt)) granter := accounts[0] @@ -139,16 +164,16 @@ func (suite *SimTestSuite) TestSimulateRevoke() { a := banktypes.NewSendAuthorization(initCoins) expire := time.Now().Add(30 * time.Hour) - err := suite.app.AuthzKeeper.SaveGrant(suite.ctx, grantee.Address, granter.Address, a, &expire) + err := suite.authzKeeper.SaveGrant(suite.ctx, grantee.Address, granter.Address, a, &expire) suite.Require().NoError(err) // execute operation - op := simulation.SimulateMsgRevoke(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.AuthzKeeper) + op := simulation.SimulateMsgRevoke(codec.NewProtoCodec(suite.interfaceRegistry), suite.accountKeeper, suite.bankKeeper, suite.authzKeeper) operationMsg, futureOperations, err := op(r, suite.app.BaseApp, suite.ctx, accounts, "") suite.Require().NoError(err) var msg authz.MsgRevoke - suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) + suite.legacyAmino.UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) suite.Require().Equal(granter.Address.String(), msg.Granter) @@ -166,7 +191,7 @@ func (suite *SimTestSuite) TestSimulateExec() { // begin a new block suite.app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: suite.app.LastBlockHeight() + 1, AppHash: suite.app.LastCommitID().Hash}}) - initAmt := suite.app.StakingKeeper.TokensFromConsensusPower(suite.ctx, 200000) + initAmt := suite.stakingKeeper.TokensFromConsensusPower(suite.ctx, 200000) initCoins := sdk.NewCoins(sdk.NewCoin("stake", initAmt)) granter := accounts[0] @@ -174,17 +199,17 @@ func (suite *SimTestSuite) TestSimulateExec() { a := banktypes.NewSendAuthorization(initCoins) expire := suite.ctx.BlockTime().Add(1 * time.Hour) - err := suite.app.AuthzKeeper.SaveGrant(suite.ctx, grantee.Address, granter.Address, a, &expire) + err := suite.authzKeeper.SaveGrant(suite.ctx, grantee.Address, granter.Address, a, &expire) suite.Require().NoError(err) // execute operation - op := simulation.SimulateMsgExec(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.AuthzKeeper, suite.app.AppCodec()) + op := simulation.SimulateMsgExec(codec.NewProtoCodec(suite.interfaceRegistry), suite.accountKeeper, suite.bankKeeper, suite.authzKeeper, suite.codec) operationMsg, futureOperations, err := op(r, suite.app.BaseApp, suite.ctx, accounts, "") suite.Require().NoError(err) var msg authz.MsgExec - suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) + suite.legacyAmino.UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) suite.Require().Equal(grantee.Address.String(), msg.Grantee) diff --git a/x/authz/spec/06_app_wiring.md b/x/authz/spec/06_app_wiring.md new file mode 100644 index 000000000000..25246c847ce1 --- /dev/null +++ b/x/authz/spec/06_app_wiring.md @@ -0,0 +1,5 @@ +# App Wiring + +The minimal app-wiring configuration for `x/authz` is as follows: + ++++ https://github.com/cosmos/cosmos-sdk/blob/main/x/authz/testutil/app.yaml \ No newline at end of file diff --git a/x/authz/spec/README.md b/x/authz/spec/README.md index ac73d76dba3a..7b8816da4efd 100644 --- a/x/authz/spec/README.md +++ b/x/authz/spec/README.md @@ -28,3 +28,4 @@ granting arbitrary privileges from one account (the granter) to another account * [CLI](05_client.md#cli) * [gRPC](05_client.md#grpc) * [REST](05_client.md#rest) +6. **[App Wiring](06_app_wiring.md)** diff --git a/x/authz/testutil/app.yaml b/x/authz/testutil/app.yaml new file mode 100644 index 000000000000..1cc530e7de2d --- /dev/null +++ b/x/authz/testutil/app.yaml @@ -0,0 +1,51 @@ +modules: + - name: runtime + config: + "@type": cosmos.app.runtime.v1alpha1.Module + app_name: AuthzApp + begin_blockers: [mint, staking, auth, bank, genutil, authz, params] + end_blockers: [mint, staking, auth, bank, genutil, authz, params] + init_genesis: [auth, bank, staking, mint, genutil, authz, params] + + - name: auth + config: + "@type": cosmos.auth.module.v1.Module + bech32_prefix: cosmos + module_account_permissions: + - account: fee_collector + - account: mint + permissions: [minter] + - account: bonded_tokens_pool + permissions: [burner, staking] + - account: not_bonded_tokens_pool + permissions: [burner, staking] + - account: gov + permissions: [burner] + + - name: bank + config: + "@type": cosmos.bank.module.v1.Module + + - name: params + config: + "@type": cosmos.params.module.v1.Module + + - name: tx + config: + "@type": cosmos.tx.module.v1.Module + + - name: staking + config: + "@type": cosmos.staking.module.v1.Module + + - name: authz + config: + "@type": cosmos.authz.module.v1.Module + + - name: genutil + config: + "@type": cosmos.genutil.module.v1.Module + + - name: mint + config: + "@type": cosmos.mint.module.v1.Module \ No newline at end of file diff --git a/x/authz/testutil/app_config.go b/x/authz/testutil/app_config.go new file mode 100644 index 000000000000..104dbc12a93d --- /dev/null +++ b/x/authz/testutil/app_config.go @@ -0,0 +1,22 @@ +package testutil + +import ( + _ "embed" + + _ "github.com/cosmos/cosmos-sdk/x/auth" + _ "github.com/cosmos/cosmos-sdk/x/auth/tx/module" + _ "github.com/cosmos/cosmos-sdk/x/authz/module" + _ "github.com/cosmos/cosmos-sdk/x/bank" + _ "github.com/cosmos/cosmos-sdk/x/genutil" + _ "github.com/cosmos/cosmos-sdk/x/gov" + _ "github.com/cosmos/cosmos-sdk/x/mint" + _ "github.com/cosmos/cosmos-sdk/x/params" + _ "github.com/cosmos/cosmos-sdk/x/staking" + + "cosmossdk.io/core/appconfig" +) + +//go:embed app.yaml +var appConfig []byte + +var AppConfig = appconfig.LoadYAML(appConfig)