From ccb008141a854d8d4291537a580f8046e14414f5 Mon Sep 17 00:00:00 2001 From: likhita-809 <78951027+likhita-809@users.noreply.github.com> Date: Wed, 29 Jun 2022 15:23:59 +0530 Subject: [PATCH] feat: decouple `x/auth` from simapp (#12360) ## Description Closes: #12302 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) --- x/auth/ante/ante_test.go | 52 ++-- x/auth/ante/basic_test.go | 8 +- x/auth/ante/fee_test.go | 16 +- x/auth/ante/feegrant_test.go | 22 +- x/auth/ante/sigverify_test.go | 60 ++-- x/auth/ante/testutil_test.go | 75 +++-- x/auth/client/cli/encode_test.go | 56 ++-- x/auth/client/cli/query.go | 6 +- x/auth/client/cli/query_test.go | 5 +- x/auth/client/testutil/suite.go | 8 +- x/auth/client/tx_test.go | 26 +- x/auth/keeper/grpc_query_test.go | 28 +- x/auth/keeper/integration_test.go | 20 -- x/auth/keeper/keeper_bench_test.go | 27 +- x/auth/keeper/keeper_test.go | 134 ++++---- x/auth/keeper/querier_test.go | 39 ++- x/auth/migrations/legacytx/config_test.go | 2 +- x/auth/migrations/v043/store_test.go | 288 +++++++++--------- x/auth/migrations/v046/store_test.go | 22 +- x/auth/module_test.go | 24 +- x/auth/signing/verify_test.go | 40 +-- x/auth/simulation/decoder_test.go | 20 +- x/auth/testutil/app.yaml | 4 + .../{testutil => testutilhelpers}/tx/suite.go | 0 x/auth/tx/aux_test.go | 26 +- x/auth/tx/config_test.go | 2 +- x/auth/types/account_test.go | 17 +- x/auth/types/common_test.go | 10 - x/auth/types/genesis_test.go | 10 +- 29 files changed, 560 insertions(+), 487 deletions(-) delete mode 100644 x/auth/keeper/integration_test.go rename x/auth/{testutil => testutilhelpers}/tx/suite.go (100%) delete mode 100644 x/auth/types/common_test.go diff --git a/x/auth/ante/ante_test.go b/x/auth/ante/ante_test.go index f2bc8f83a726..a3bcc8779e1e 100644 --- a/x/auth/ante/ante_test.go +++ b/x/auth/ante/ante_test.go @@ -138,11 +138,11 @@ func (suite *AnteTestSuite) TestAnteHandlerSigErrors() { { "save the first account, but second is still unrecognized", func() { - acc1 := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr0) - suite.app.AccountKeeper.SetAccount(suite.ctx, acc1) - err := suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, feeAmount) + acc1 := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr0) + suite.accountKeeper.SetAccount(suite.ctx, acc1) + err := suite.bankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, feeAmount) suite.Require().NoError(err) - err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, addr0, feeAmount) + err = suite.bankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, addr0, feeAmount) suite.Require().NoError(err) }, false, @@ -445,8 +445,8 @@ func (suite *AnteTestSuite) TestAnteHandlerFees() { // Same data for every test cases priv0, _, addr0 := testdata.KeyTestPubAddr() - acc1 := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr0) - suite.app.AccountKeeper.SetAccount(suite.ctx, acc1) + acc1 := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr0) + suite.accountKeeper.SetAccount(suite.ctx, acc1) msgs := []sdk.Msg{testdata.NewTestMsg(addr0)} feeAmount := testdata.NewTestFeeAmount() gasLimit := testdata.NewTestGasLimit() @@ -471,7 +471,7 @@ func (suite *AnteTestSuite) TestAnteHandlerFees() { { "signer does not have enough funds to pay the fee", func() { - err := testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr0, sdk.NewCoins(sdk.NewInt64Coin("atom", 149))) + err := testutil.FundAccount(suite.bankKeeper, suite.ctx, addr0, sdk.NewCoins(sdk.NewInt64Coin("atom", 149))) suite.Require().NoError(err) }, false, @@ -483,12 +483,12 @@ func (suite *AnteTestSuite) TestAnteHandlerFees() { func() { accNums = []uint64{acc1.GetAccountNumber()} - modAcc := suite.app.AccountKeeper.GetModuleAccount(suite.ctx, types.FeeCollectorName) + modAcc := suite.accountKeeper.GetModuleAccount(suite.ctx, types.FeeCollectorName) - suite.Require().True(suite.app.BankKeeper.GetAllBalances(suite.ctx, modAcc.GetAddress()).Empty()) - require.True(sdk.IntEq(suite.T(), suite.app.BankKeeper.GetAllBalances(suite.ctx, addr0).AmountOf("atom"), sdk.NewInt(149))) + suite.Require().True(suite.bankKeeper.GetAllBalances(suite.ctx, modAcc.GetAddress()).Empty()) + require.True(sdk.IntEq(suite.T(), suite.bankKeeper.GetAllBalances(suite.ctx, addr0).AmountOf("atom"), sdk.NewInt(149))) - err := testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr0, sdk.NewCoins(sdk.NewInt64Coin("atom", 1))) + err := testutil.FundAccount(suite.bankKeeper, suite.ctx, addr0, sdk.NewCoins(sdk.NewInt64Coin("atom", 1))) suite.Require().NoError(err) }, false, @@ -498,10 +498,10 @@ func (suite *AnteTestSuite) TestAnteHandlerFees() { { "signer doesn't have any more funds", func() { - modAcc := suite.app.AccountKeeper.GetModuleAccount(suite.ctx, types.FeeCollectorName) + modAcc := suite.accountKeeper.GetModuleAccount(suite.ctx, types.FeeCollectorName) - require.True(sdk.IntEq(suite.T(), suite.app.BankKeeper.GetAllBalances(suite.ctx, modAcc.GetAddress()).AmountOf("atom"), sdk.NewInt(150))) - require.True(sdk.IntEq(suite.T(), suite.app.BankKeeper.GetAllBalances(suite.ctx, addr0).AmountOf("atom"), sdk.NewInt(0))) + require.True(sdk.IntEq(suite.T(), suite.bankKeeper.GetAllBalances(suite.ctx, modAcc.GetAddress()).AmountOf("atom"), sdk.NewInt(150))) + require.True(sdk.IntEq(suite.T(), suite.bankKeeper.GetAllBalances(suite.ctx, addr0).AmountOf("atom"), sdk.NewInt(0))) }, false, false, @@ -820,7 +820,7 @@ func (suite *AnteTestSuite) TestAnteHandlerSetPubKey() { "make sure public key has been set (tx itself should fail because of replay protection)", func() { // Make sure public key has been set from previous test. - acc0 := suite.app.AccountKeeper.GetAccount(suite.ctx, accounts[0].acc.GetAddress()) + acc0 := suite.accountKeeper.GetAccount(suite.ctx, accounts[0].acc.GetAddress()) suite.Require().Equal(acc0.GetPubKey(), accounts[0].priv.PubKey()) }, false, @@ -841,7 +841,7 @@ func (suite *AnteTestSuite) TestAnteHandlerSetPubKey() { "make sure public key is not set, when tx has no pubkey or signature", func() { // Make sure public key has not been set from previous test. - acc1 := suite.app.AccountKeeper.GetAccount(suite.ctx, accounts[1].acc.GetAddress()) + acc1 := suite.accountKeeper.GetAccount(suite.ctx, accounts[1].acc.GetAddress()) suite.Require().Nil(acc1.GetPubKey()) privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[1].priv}, []uint64{1}, []uint64{0} @@ -863,7 +863,7 @@ func (suite *AnteTestSuite) TestAnteHandlerSetPubKey() { suite.Require().True(errors.Is(err, sdkerrors.ErrNoSignatures)) // Make sure public key has not been set. - acc1 = suite.app.AccountKeeper.GetAccount(suite.ctx, accounts[1].acc.GetAddress()) + acc1 = suite.accountKeeper.GetAccount(suite.ctx, accounts[1].acc.GetAddress()) suite.Require().Nil(acc1.GetPubKey()) // Set incorrect accSeq, to generate incorrect signature. @@ -878,7 +878,7 @@ func (suite *AnteTestSuite) TestAnteHandlerSetPubKey() { func() { // Make sure public key has been set, as SetPubKeyDecorator // is called before all signature verification decorators. - acc1 := suite.app.AccountKeeper.GetAccount(suite.ctx, accounts[1].acc.GetAddress()) + acc1 := suite.accountKeeper.GetAccount(suite.ctx, accounts[1].acc.GetAddress()) suite.Require().Equal(acc1.GetPubKey(), accounts[1].priv.PubKey()) }, false, @@ -1011,9 +1011,9 @@ func (suite *AnteTestSuite) TestCustomSignatureVerificationGasConsumer() { // setup an ante handler that only accepts PubKeyEd25519 anteHandler, err := ante.NewAnteHandler( ante.HandlerOptions{ - AccountKeeper: suite.app.AccountKeeper, - BankKeeper: suite.app.BankKeeper, - FeegrantKeeper: suite.app.FeeGrantKeeper, + AccountKeeper: suite.accountKeeper, + BankKeeper: suite.bankKeeper, + FeegrantKeeper: suite.feeGrantKeeper, SignModeHandler: suite.clientCtx.TxConfig.SignModeHandler(), SigGasConsumer: func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error { switch pubkey := sig.PubKey.(type) { @@ -1116,14 +1116,14 @@ func (suite *AnteTestSuite) TestAnteHandlerReCheck() { } for _, tc := range testCases { // set testcase parameters - suite.app.AccountKeeper.SetParams(suite.ctx, tc.params) + suite.accountKeeper.SetParams(suite.ctx, tc.params) _, err := suite.anteHandler(suite.ctx, tx, false) suite.Require().NotNil(err, "tx does not fail on recheck with updated params in test case: %s", tc.name) // reset parameters to default values - suite.app.AccountKeeper.SetParams(suite.ctx, types.DefaultParams()) + suite.accountKeeper.SetParams(suite.ctx, types.DefaultParams()) } // require that local mempool fee check is still run on recheck since validator may change minFee between check and recheck @@ -1138,9 +1138,9 @@ func (suite *AnteTestSuite) TestAnteHandlerReCheck() { suite.ctx = suite.ctx.WithMinGasPrices(sdk.DecCoins{}) // remove funds for account so antehandler fails on recheck - suite.app.AccountKeeper.SetAccount(suite.ctx, accounts[0].acc) - balances := suite.app.BankKeeper.GetAllBalances(suite.ctx, accounts[0].acc.GetAddress()) - err = suite.app.BankKeeper.SendCoinsFromAccountToModule(suite.ctx, accounts[0].acc.GetAddress(), minttypes.ModuleName, balances) + suite.accountKeeper.SetAccount(suite.ctx, accounts[0].acc) + balances := suite.bankKeeper.GetAllBalances(suite.ctx, accounts[0].acc.GetAddress()) + err = suite.bankKeeper.SendCoinsFromAccountToModule(suite.ctx, accounts[0].acc.GetAddress(), minttypes.ModuleName, balances) suite.Require().NoError(err) _, err = suite.anteHandler(suite.ctx, tx, false) diff --git a/x/auth/ante/basic_test.go b/x/auth/ante/basic_test.go index 761e7a2b8e88..11e57f1ebdcb 100644 --- a/x/auth/ante/basic_test.go +++ b/x/auth/ante/basic_test.go @@ -73,7 +73,7 @@ func (suite *AnteTestSuite) TestValidateMemo() { suite.Require().NoError(err) // require that long memos get rejected - vmd := ante.NewValidateMemoDecorator(suite.app.AccountKeeper) + vmd := ante.NewValidateMemoDecorator(suite.accountKeeper) antehandler := sdk.ChainAnteDecorators(vmd) _, err = antehandler(suite.ctx, invalidTx, false) @@ -99,7 +99,7 @@ func (suite *AnteTestSuite) TestConsumeGasForTxSize() { feeAmount := testdata.NewTestFeeAmount() gasLimit := testdata.NewTestGasLimit() - cgtsd := ante.NewConsumeGasForTxSizeDecorator(suite.app.AccountKeeper) + cgtsd := ante.NewConsumeGasForTxSizeDecorator(suite.accountKeeper) antehandler := sdk.ChainAnteDecorators(cgtsd) testCases := []struct { @@ -125,7 +125,7 @@ func (suite *AnteTestSuite) TestConsumeGasForTxSize() { txBytes, err := suite.clientCtx.TxConfig.TxJSONEncoder()(tx) suite.Require().Nil(err, "Cannot marshal tx: %v", err) - params := suite.app.AccountKeeper.GetParams(suite.ctx) + params := suite.accountKeeper.GetParams(suite.ctx) expectedGas := sdk.Gas(len(txBytes)) * params.TxSizeCostPerByte // Set suite.ctx with TxBytes manually @@ -133,7 +133,7 @@ func (suite *AnteTestSuite) TestConsumeGasForTxSize() { // track how much gas is necessary to retrieve parameters beforeGas := suite.ctx.GasMeter().GasConsumed() - suite.app.AccountKeeper.GetParams(suite.ctx) + suite.accountKeeper.GetParams(suite.ctx) afterGas := suite.ctx.GasMeter().GasConsumed() expectedGas += afterGas - beforeGas diff --git a/x/auth/ante/fee_test.go b/x/auth/ante/fee_test.go index 21a0a2d7ff8e..357b5ca226d0 100644 --- a/x/auth/ante/fee_test.go +++ b/x/auth/ante/fee_test.go @@ -12,13 +12,13 @@ func (suite *AnteTestSuite) TestEnsureMempoolFees() { suite.SetupTest(true) // setup suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() - mfd := ante.NewDeductFeeDecorator(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.FeeGrantKeeper, nil) + mfd := ante.NewDeductFeeDecorator(suite.accountKeeper, suite.bankKeeper, suite.feeGrantKeeper, nil) antehandler := sdk.ChainAnteDecorators(mfd) // keys and addresses priv1, _, addr1 := testdata.KeyTestPubAddr() coins := sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(300))) - testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr1, coins) + testutil.FundAccount(suite.bankKeeper, suite.ctx, addr1, coins) // msg and signatures msg := testdata.NewTestMsg(addr1) @@ -85,13 +85,13 @@ func (suite *AnteTestSuite) TestDeductFees() { suite.Require().NoError(err) // Set account with insufficient funds - acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr1) - suite.app.AccountKeeper.SetAccount(suite.ctx, acc) + acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr1) + suite.accountKeeper.SetAccount(suite.ctx, acc) coins := sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(10))) - err = testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr1, coins) + err = testutil.FundAccount(suite.bankKeeper, suite.ctx, addr1, coins) suite.Require().NoError(err) - dfd := ante.NewDeductFeeDecorator(suite.app.AccountKeeper, suite.app.BankKeeper, nil, nil) + dfd := ante.NewDeductFeeDecorator(suite.accountKeeper, suite.bankKeeper, nil, nil) antehandler := sdk.ChainAnteDecorators(dfd) _, err = antehandler(suite.ctx, tx, false) @@ -99,8 +99,8 @@ func (suite *AnteTestSuite) TestDeductFees() { suite.Require().NotNil(err, "Tx did not error when fee payer had insufficient funds") // Set account with sufficient funds - suite.app.AccountKeeper.SetAccount(suite.ctx, acc) - err = testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200)))) + suite.accountKeeper.SetAccount(suite.ctx, acc) + err = testutil.FundAccount(suite.bankKeeper, suite.ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200)))) suite.Require().NoError(err) _, err = antehandler(suite.ctx, tx, false) diff --git a/x/auth/ante/feegrant_test.go b/x/auth/ante/feegrant_test.go index 5031a37cd312..1874945d857c 100644 --- a/x/auth/ante/feegrant_test.go +++ b/x/auth/ante/feegrant_test.go @@ -26,13 +26,11 @@ import ( func (suite *AnteTestSuite) TestDeductFeesNoDelegation() { suite.SetupTest(false) - // setup - app, ctx := suite.app, suite.ctx - protoTxCfg := tx.NewTxConfig(codec.NewProtoCodec(app.InterfaceRegistry()), tx.DefaultSignModes) + protoTxCfg := tx.NewTxConfig(codec.NewProtoCodec(suite.interfaceRegistry), tx.DefaultSignModes) // this just tests our handler - dfd := ante.NewDeductFeeDecorator(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, nil) + dfd := ante.NewDeductFeeDecorator(suite.accountKeeper, suite.bankKeeper, suite.feeGrantKeeper, nil) feeAnteHandler := sdk.ChainAnteDecorators(dfd) // this tests the whole stack @@ -46,21 +44,21 @@ func (suite *AnteTestSuite) TestDeductFeesNoDelegation() { priv5, _, addr5 := testdata.KeyTestPubAddr() // Set addr1 with insufficient funds - err := testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr1, []sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(10))}) + err := testutil.FundAccount(suite.bankKeeper, suite.ctx, addr1, []sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(10))}) suite.Require().NoError(err) // Set addr2 with more funds - err = testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr2, []sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(99999))}) + err = testutil.FundAccount(suite.bankKeeper, suite.ctx, addr2, []sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(99999))}) suite.Require().NoError(err) // grant fee allowance from `addr2` to `addr3` (plenty to pay) - err = app.FeeGrantKeeper.GrantAllowance(ctx, addr2, addr3, &feegrant.BasicAllowance{ + err = suite.feeGrantKeeper.GrantAllowance(suite.ctx, addr2, addr3, &feegrant.BasicAllowance{ SpendLimit: sdk.NewCoins(sdk.NewInt64Coin("atom", 500)), }) suite.Require().NoError(err) // grant low fee allowance (20atom), to check the tx requesting more than allowed. - err = app.FeeGrantKeeper.GrantAllowance(ctx, addr2, addr4, &feegrant.BasicAllowance{ + err = suite.feeGrantKeeper.GrantAllowance(suite.ctx, addr2, addr4, &feegrant.BasicAllowance{ SpendLimit: sdk.NewCoins(sdk.NewInt64Coin("atom", 20)), }) suite.Require().NoError(err) @@ -138,22 +136,22 @@ func (suite *AnteTestSuite) TestDeductFeesNoDelegation() { fee := sdk.NewCoins(sdk.NewInt64Coin("atom", tc.fee)) msgs := []sdk.Msg{testdata.NewTestMsg(tc.signer)} - acc := app.AccountKeeper.GetAccount(ctx, tc.signer) + acc := suite.accountKeeper.GetAccount(suite.ctx, tc.signer) privs, accNums, seqs := []cryptotypes.PrivKey{tc.signerKey}, []uint64{0}, []uint64{0} if acc != nil { accNums, seqs = []uint64{acc.GetAccountNumber()}, []uint64{acc.GetSequence()} } - tx, err := genTxWithFeeGranter(protoTxCfg, msgs, fee, simtestutil.DefaultGenTxGas, ctx.ChainID(), accNums, seqs, tc.feeAccount, privs...) + tx, err := genTxWithFeeGranter(protoTxCfg, msgs, fee, simtestutil.DefaultGenTxGas, suite.ctx.ChainID(), accNums, seqs, tc.feeAccount, privs...) suite.Require().NoError(err) - _, err = feeAnteHandler(ctx, tx, false) // tests only feegrant ante + _, err = feeAnteHandler(suite.ctx, tx, false) // tests only feegrant ante if tc.valid { suite.Require().NoError(err) } else { suite.Require().Error(err) } - _, err = anteHandlerStack(ctx, tx, false) // tests while stack + _, err = anteHandlerStack(suite.ctx, tx, false) // tests while stack if tc.valid { suite.Require().NoError(err) } else { diff --git a/x/auth/ante/sigverify_test.go b/x/auth/ante/sigverify_test.go index c002818d3b26..4f69c40a25b6 100644 --- a/x/auth/ante/sigverify_test.go +++ b/x/auth/ante/sigverify_test.go @@ -11,7 +11,6 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys/secp256r1" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/crypto/types/multisig" - "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/tx/signing" @@ -36,9 +35,9 @@ func (suite *AnteTestSuite) TestSetPubKey() { msgs := make([]sdk.Msg, len(addrs)) // set accounts and create msg for each address for i, addr := range addrs { - acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr) + acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr) require.NoError(acc.SetAccountNumber(uint64(i))) - suite.app.AccountKeeper.SetAccount(suite.ctx, acc) + suite.accountKeeper.SetAccount(suite.ctx, acc) msgs[i] = testdata.NewTestMsg(addr) } require.NoError(suite.txBuilder.SetMsgs(msgs...)) @@ -49,7 +48,7 @@ func (suite *AnteTestSuite) TestSetPubKey() { tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) require.NoError(err) - spkd := ante.NewSetPubKeyDecorator(suite.app.AccountKeeper) + spkd := ante.NewSetPubKeyDecorator(suite.accountKeeper) antehandler := sdk.ChainAnteDecorators(spkd) ctx, err := antehandler(suite.ctx, tx, false) @@ -57,7 +56,7 @@ func (suite *AnteTestSuite) TestSetPubKey() { // Require that all accounts have pubkey set after Decorator runs for i, addr := range addrs { - pk, err := suite.app.AccountKeeper.GetPubKey(ctx, addr) + pk, err := suite.accountKeeper.GetPubKey(ctx, addr) require.NoError(err, "Error on retrieving pubkey from account") require.True(pubs[i].Equals(pk), "Wrong Pubkey retrieved from AccountKeeper, idx=%d\nexpected=%s\n got=%s", i, pubs[i], pk) @@ -67,7 +66,6 @@ func (suite *AnteTestSuite) TestSetPubKey() { func (suite *AnteTestSuite) TestConsumeSignatureVerificationGas() { params := types.DefaultParams() msg := []byte{1, 2, 3, 4} - cdc := simapp.MakeTestEncodingConfig().Amino p := types.DefaultParams() skR1, _ := secp256r1.GenPrivKey() @@ -77,7 +75,7 @@ func (suite *AnteTestSuite) TestConsumeSignatureVerificationGas() { expectedCost1 := expectedGasCostByKeys(pkSet1) for i := 0; i < len(pkSet1); i++ { stdSig := legacytx.StdSignature{PubKey: pkSet1[i], Signature: sigSet1[i]} - sigV2, err := legacytx.StdSignatureToSignatureV2(cdc, stdSig) + sigV2, err := legacytx.StdSignatureToSignatureV2(suite.clientCtx.LegacyAmino, stdSig) suite.Require().NoError(err) err = multisig.AddSignatureV2(multisignature1, sigV2, pkSet1) suite.Require().NoError(err) @@ -135,17 +133,17 @@ func (suite *AnteTestSuite) TestSigVerification() { msgs := make([]sdk.Msg, len(addrs)) // set accounts and create msg for each address for i, addr := range addrs { - acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr) + acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr) suite.Require().NoError(acc.SetAccountNumber(uint64(i))) - suite.app.AccountKeeper.SetAccount(suite.ctx, acc) + suite.accountKeeper.SetAccount(suite.ctx, acc) msgs[i] = testdata.NewTestMsg(addr) } feeAmount := testdata.NewTestFeeAmount() gasLimit := testdata.NewTestGasLimit() - spkd := ante.NewSetPubKeyDecorator(suite.app.AccountKeeper) - svd := ante.NewSigVerificationDecorator(suite.app.AccountKeeper, suite.clientCtx.TxConfig.SignModeHandler()) + spkd := ante.NewSetPubKeyDecorator(suite.accountKeeper) + svd := ante.NewSigVerificationDecorator(suite.accountKeeper, suite.clientCtx.TxConfig.SignModeHandler()) antehandler := sdk.ChainAnteDecorators(spkd, svd) type testCase struct { @@ -208,8 +206,6 @@ func (suite *AnteTestSuite) TestSigVerification() { // In the meantime, we want to make double-sure amino compatibility works. // ref: https://github.com/cosmos/cosmos-sdk/issues/7229 func (suite *AnteTestSuite) TestSigVerification_ExplicitAmino() { - suite.app, suite.ctx = createTestApp(suite.T(), true) - suite.ctx = suite.ctx.WithBlockHeight(1) // Set up TxConfig. aminoCdc := codec.NewLegacyAmino() @@ -221,9 +217,9 @@ func (suite *AnteTestSuite) TestSigVerification_ExplicitAmino() { anteHandler, err := ante.NewAnteHandler( ante.HandlerOptions{ - AccountKeeper: suite.app.AccountKeeper, - BankKeeper: suite.app.BankKeeper, - FeegrantKeeper: suite.app.FeeGrantKeeper, + AccountKeeper: suite.accountKeeper, + BankKeeper: suite.bankKeeper, + FeegrantKeeper: suite.feeGrantKeeper, SignModeHandler: txConfig.SignModeHandler(), SigGasConsumer: ante.DefaultSigVerificationGasConsumer, }, @@ -247,17 +243,17 @@ func (suite *AnteTestSuite) TestSigVerification_ExplicitAmino() { msgs := make([]sdk.Msg, len(addrs)) // set accounts and create msg for each address for i, addr := range addrs { - acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr) + acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr) suite.Require().NoError(acc.SetAccountNumber(uint64(i))) - suite.app.AccountKeeper.SetAccount(suite.ctx, acc) + suite.accountKeeper.SetAccount(suite.ctx, acc) msgs[i] = testdata.NewTestMsg(addr) } feeAmount := testdata.NewTestFeeAmount() gasLimit := testdata.NewTestGasLimit() - spkd := ante.NewSetPubKeyDecorator(suite.app.AccountKeeper) - svd := ante.NewSigVerificationDecorator(suite.app.AccountKeeper, suite.clientCtx.TxConfig.SignModeHandler()) + spkd := ante.NewSetPubKeyDecorator(suite.accountKeeper) + svd := ante.NewSigVerificationDecorator(suite.accountKeeper, suite.clientCtx.TxConfig.SignModeHandler()) antehandler := sdk.ChainAnteDecorators(spkd, svd) type testCase struct { @@ -323,7 +319,7 @@ func (suite *AnteTestSuite) runSigDecorators(params types.Params, _ bool, privs // Make block-height non-zero to include accNum in SignBytes suite.ctx = suite.ctx.WithBlockHeight(1) - suite.app.AccountKeeper.SetParams(suite.ctx, params) + suite.accountKeeper.SetParams(suite.ctx, params) msgs := make([]sdk.Msg, len(privs)) accNums := make([]uint64, len(privs)) @@ -331,9 +327,9 @@ func (suite *AnteTestSuite) runSigDecorators(params types.Params, _ bool, privs // set accounts and create msg for each address for i, priv := range privs { addr := sdk.AccAddress(priv.PubKey().Address()) - acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr) + acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr) suite.Require().NoError(acc.SetAccountNumber(uint64(i))) - suite.app.AccountKeeper.SetAccount(suite.ctx, acc) + suite.accountKeeper.SetAccount(suite.ctx, acc) msgs[i] = testdata.NewTestMsg(addr) accNums[i] = uint64(i) accSeqs[i] = uint64(0) @@ -348,9 +344,9 @@ func (suite *AnteTestSuite) runSigDecorators(params types.Params, _ bool, privs tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) suite.Require().NoError(err) - spkd := ante.NewSetPubKeyDecorator(suite.app.AccountKeeper) - svgc := ante.NewSigGasConsumeDecorator(suite.app.AccountKeeper, ante.DefaultSigVerificationGasConsumer) - svd := ante.NewSigVerificationDecorator(suite.app.AccountKeeper, suite.clientCtx.TxConfig.SignModeHandler()) + spkd := ante.NewSetPubKeyDecorator(suite.accountKeeper) + svgc := ante.NewSigGasConsumeDecorator(suite.accountKeeper, ante.DefaultSigVerificationGasConsumer) + svd := ante.NewSigVerificationDecorator(suite.accountKeeper, suite.clientCtx.TxConfig.SignModeHandler()) antehandler := sdk.ChainAnteDecorators(spkd, svgc, svd) // Determine gas consumption of antehandler with default params @@ -366,15 +362,15 @@ func (suite *AnteTestSuite) TestIncrementSequenceDecorator() { suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() priv, _, addr := testdata.KeyTestPubAddr() - acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr) + acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr) suite.Require().NoError(acc.SetAccountNumber(uint64(50))) - suite.app.AccountKeeper.SetAccount(suite.ctx, acc) + suite.accountKeeper.SetAccount(suite.ctx, acc) msgs := []sdk.Msg{testdata.NewTestMsg(addr)} suite.Require().NoError(suite.txBuilder.SetMsgs(msgs...)) privs := []cryptotypes.PrivKey{priv} - accNums := []uint64{suite.app.AccountKeeper.GetAccount(suite.ctx, addr).GetAccountNumber()} - accSeqs := []uint64{suite.app.AccountKeeper.GetAccount(suite.ctx, addr).GetSequence()} + accNums := []uint64{suite.accountKeeper.GetAccount(suite.ctx, addr).GetAccountNumber()} + accSeqs := []uint64{suite.accountKeeper.GetAccount(suite.ctx, addr).GetSequence()} feeAmount := testdata.NewTestFeeAmount() gasLimit := testdata.NewTestGasLimit() suite.txBuilder.SetFeeAmount(feeAmount) @@ -383,7 +379,7 @@ func (suite *AnteTestSuite) TestIncrementSequenceDecorator() { tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) suite.Require().NoError(err) - isd := ante.NewIncrementSequenceDecorator(suite.app.AccountKeeper) + isd := ante.NewIncrementSequenceDecorator(suite.accountKeeper) antehandler := sdk.ChainAnteDecorators(isd) testCases := []struct { @@ -401,6 +397,6 @@ func (suite *AnteTestSuite) TestIncrementSequenceDecorator() { for i, tc := range testCases { _, err := antehandler(tc.ctx, tx, tc.simulate) suite.Require().NoError(err, "unexpected error; tc #%d, %v", i, tc) - suite.Require().Equal(tc.expectedSeq, suite.app.AccountKeeper.GetAccount(suite.ctx, addr).GetSequence()) + suite.Require().Equal(tc.expectedSeq, suite.accountKeeper.GetAccount(suite.ctx, addr).GetSequence()) } } diff --git a/x/auth/ante/testutil_test.go b/x/auth/ante/testutil_test.go index 15598b3b23b0..9c16f583c6e0 100644 --- a/x/auth/ante/testutil_test.go +++ b/x/auth/ante/testutil_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/cosmos/cosmos-sdk/codec" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" "github.com/stretchr/testify/suite" @@ -13,14 +14,20 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/tx" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/simapp" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/tx/signing" "github.com/cosmos/cosmos-sdk/x/auth/ante" + "github.com/cosmos/cosmos-sdk/x/auth/keeper" + "github.com/cosmos/cosmos-sdk/x/auth/testutil" + + codectypes "github.com/cosmos/cosmos-sdk/codec/types" xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" "github.com/cosmos/cosmos-sdk/x/auth/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + feegrantkeeper "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" ) // TestAccount represents an account used in the tests in x/auth/ante. @@ -33,42 +40,50 @@ type TestAccount struct { type AnteTestSuite struct { suite.Suite - app *simapp.SimApp - anteHandler sdk.AnteHandler - ctx sdk.Context - clientCtx client.Context - txBuilder client.TxBuilder -} - -// returns context and app with params set on account keeper -func createTestApp(t *testing.T, isCheckTx bool) (*simapp.SimApp, sdk.Context) { - app := simapp.Setup(t, isCheckTx) - ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{}) - app.AccountKeeper.SetParams(ctx, authtypes.DefaultParams()) - - return app, ctx + interfaceRegistry codectypes.InterfaceRegistry + anteHandler sdk.AnteHandler + ctx sdk.Context + clientCtx client.Context + txBuilder client.TxBuilder + accountKeeper keeper.AccountKeeper + bankKeeper bankkeeper.Keeper + feeGrantKeeper feegrantkeeper.Keeper } // SetupTest setups a new test, with new app, context, and anteHandler. func (suite *AnteTestSuite) SetupTest(isCheckTx bool) { - suite.app, suite.ctx = createTestApp(suite.T(), isCheckTx) - suite.ctx = suite.ctx.WithBlockHeight(1) + var ( + txConfig client.TxConfig + legacyAmino *codec.LegacyAmino + ) + + app, err := simtestutil.Setup( + testutil.AppConfig, + &suite.accountKeeper, + &suite.bankKeeper, + &suite.feeGrantKeeper, + &suite.interfaceRegistry, + &txConfig, + &legacyAmino, + ) + suite.Require().NoError(err) + + suite.ctx = app.BaseApp.NewContext(isCheckTx, tmproto.Header{}).WithBlockHeight(1) + suite.accountKeeper.SetParams(suite.ctx, authtypes.DefaultParams()) - // Set up TxConfig. - encodingConfig := simapp.MakeTestEncodingConfig() // We're using TestMsg encoding in some tests, so register it here. - encodingConfig.Amino.RegisterConcrete(&testdata.TestMsg{}, "testdata.TestMsg", nil) - testdata.RegisterInterfaces(encodingConfig.InterfaceRegistry) + legacyAmino.Amino.RegisterConcrete(&testdata.TestMsg{}, "testdata.TestMsg", nil) + testdata.RegisterInterfaces(suite.interfaceRegistry) suite.clientCtx = client.Context{}. - WithTxConfig(encodingConfig.TxConfig) + WithTxConfig(txConfig) anteHandler, err := ante.NewAnteHandler( ante.HandlerOptions{ - AccountKeeper: suite.app.AccountKeeper, - BankKeeper: suite.app.BankKeeper, - FeegrantKeeper: suite.app.FeeGrantKeeper, - SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), + AccountKeeper: suite.accountKeeper, + BankKeeper: suite.bankKeeper, + FeegrantKeeper: suite.feeGrantKeeper, + SignModeHandler: txConfig.SignModeHandler(), SigGasConsumer: ante.DefaultSigVerificationGasConsumer, }, ) @@ -84,17 +99,17 @@ func (suite *AnteTestSuite) CreateTestAccounts(numAccs int) []TestAccount { for i := 0; i < numAccs; i++ { priv, _, addr := testdata.KeyTestPubAddr() - acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr) + acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr) err := acc.SetAccountNumber(uint64(i)) suite.Require().NoError(err) - suite.app.AccountKeeper.SetAccount(suite.ctx, acc) + suite.accountKeeper.SetAccount(suite.ctx, acc) someCoins := sdk.Coins{ sdk.NewInt64Coin("atom", 10000000), } - err = suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, someCoins) + err = suite.bankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, someCoins) suite.Require().NoError(err) - err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, addr, someCoins) + err = suite.bankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, addr, someCoins) suite.Require().NoError(err) accounts = append(accounts, TestAccount{acc, priv}) diff --git a/x/auth/client/cli/encode_test.go b/x/auth/client/cli/encode_test.go index a0b5a7427ee8..e74d92a35d4e 100644 --- a/x/auth/client/cli/encode_test.go +++ b/x/auth/client/cli/encode_test.go @@ -1,4 +1,4 @@ -package cli +package cli_test import ( "context" @@ -8,23 +8,32 @@ import ( "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/client" - simappparams "github.com/cosmos/cosmos-sdk/simapp/params" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/depinject" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/cosmos/cosmos-sdk/x/auth/client/cli" + authtestutil "github.com/cosmos/cosmos-sdk/x/auth/testutil" ) func TestGetCommandEncode(t *testing.T) { - encodingConfig := simappparams.MakeTestEncodingConfig() + var ( + txCfg client.TxConfig + legacyAmino *codec.LegacyAmino + codec codec.Codec + ) + + err := depinject.Inject( + authtestutil.AppConfig, + &txCfg, + &legacyAmino, + &codec, + ) + require.NoError(t, err) - cmd := GetEncodeCommand() + cmd := cli.GetEncodeCommand() _ = testutil.ApplyMockIODiscardOutErr(cmd) - authtypes.RegisterLegacyAminoCodec(encodingConfig.Amino) - sdk.RegisterLegacyAminoCodec(encodingConfig.Amino) - - txCfg := encodingConfig.TxConfig - // Build a test transaction builder := txCfg.NewTxBuilder() builder.SetGasLimit(50000) @@ -38,8 +47,8 @@ func TestGetCommandEncode(t *testing.T) { ctx := context.Background() clientCtx := client.Context{}. - WithTxConfig(encodingConfig.TxConfig). - WithCodec(encodingConfig.Codec) + WithTxConfig(txCfg). + WithCodec(codec) ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) cmd.SetArgs([]string{txFileName}) @@ -48,18 +57,27 @@ func TestGetCommandEncode(t *testing.T) { } func TestGetCommandDecode(t *testing.T) { - encodingConfig := simappparams.MakeTestEncodingConfig() + var ( + txCfg client.TxConfig + legacyAmino *codec.LegacyAmino + codec codec.Codec + ) + + err := depinject.Inject( + authtestutil.AppConfig, + &txCfg, + &legacyAmino, + &codec, + ) + require.NoError(t, err) clientCtx := client.Context{}. - WithTxConfig(encodingConfig.TxConfig). - WithCodec(encodingConfig.Codec) + WithTxConfig(txCfg). + WithCodec(codec) - cmd := GetDecodeCommand() + cmd := cli.GetDecodeCommand() _ = testutil.ApplyMockIODiscardOutErr(cmd) - sdk.RegisterLegacyAminoCodec(encodingConfig.Amino) - - txCfg := encodingConfig.TxConfig clientCtx = clientCtx.WithTxConfig(txCfg) // Build a test transaction diff --git a/x/auth/client/cli/query.go b/x/auth/client/cli/query.go index c3eda5b2d03e..3c9dd000106c 100644 --- a/x/auth/client/cli/query.go +++ b/x/auth/client/cli/query.go @@ -333,7 +333,7 @@ $ %s query tx --%s=%s , } case typeSig: { - sigParts, err := parseSigArgs(args) + sigParts, err := ParseSigArgs(args) if err != nil { return err } @@ -391,8 +391,8 @@ $ %s query tx --%s=%s , return cmd } -// parseSigArgs parses comma-separated signatures from the CLI arguments. -func parseSigArgs(args []string) ([]string, error) { +// ParseSigArgs parses comma-separated signatures from the CLI arguments. +func ParseSigArgs(args []string) ([]string, error) { if len(args) != 1 || args[0] == "" { return nil, fmt.Errorf("argument should be comma-separated signatures") } diff --git a/x/auth/client/cli/query_test.go b/x/auth/client/cli/query_test.go index 0168d3008179..6258beed2e2d 100644 --- a/x/auth/client/cli/query_test.go +++ b/x/auth/client/cli/query_test.go @@ -1,8 +1,9 @@ -package cli +package cli_test import ( "testing" + "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/stretchr/testify/require" ) @@ -21,7 +22,7 @@ func TestParseSigs(t *testing.T) { } for _, tc := range cases { - sigs, err := parseSigArgs(tc.args) + sigs, err := cli.ParseSigArgs(tc.args) if tc.expErr { require.Error(t, err) } else { diff --git a/x/auth/client/testutil/suite.go b/x/auth/client/testutil/suite.go index 7e882df7d3ae..5bc6b735fc67 100644 --- a/x/auth/client/testutil/suite.go +++ b/x/auth/client/testutil/suite.go @@ -15,14 +15,16 @@ import ( tmcli "github.com/tendermint/tendermint/libs/cli" "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/depinject" + authtestutil "github.com/cosmos/cosmos-sdk/x/auth/testutil" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/testutil" clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" "github.com/cosmos/cosmos-sdk/testutil/network" @@ -1351,8 +1353,10 @@ func TestGetBroadcastCommandOfflineFlag(t *testing.T) { } func TestGetBroadcastCommandWithoutOfflineFlag(t *testing.T) { + var txCfg client.TxConfig + err := depinject.Inject(authtestutil.AppConfig, &txCfg) + require.NoError(t, err) clientCtx := client.Context{} - txCfg := simapp.MakeTestEncodingConfig().TxConfig clientCtx = clientCtx.WithTxConfig(txCfg) ctx := context.Background() diff --git a/x/auth/client/tx_test.go b/x/auth/client/tx_test.go index 4934e0febc91..13de490c67df 100644 --- a/x/auth/client/tx_test.go +++ b/x/auth/client/tx_test.go @@ -9,14 +9,16 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" "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" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" authclient "github.com/cosmos/cosmos-sdk/x/auth/client" "github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx" + authtestutil "github.com/cosmos/cosmos-sdk/x/auth/testutil" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -55,11 +57,19 @@ func TestDefaultTxEncoder(t *testing.T) { func TestReadTxFromFile(t *testing.T) { t.Parallel() - encodingConfig := simapp.MakeTestEncodingConfig() + var ( + txCfg client.TxConfig + interfaceRegistry codectypes.InterfaceRegistry + ) + err := depinject.Inject( + authtestutil.AppConfig, + &interfaceRegistry, + &txCfg, + ) + require.NoError(t, err) - txCfg := encodingConfig.TxConfig clientCtx := client.Context{} - clientCtx = clientCtx.WithInterfaceRegistry(encodingConfig.InterfaceRegistry) + clientCtx = clientCtx.WithInterfaceRegistry(interfaceRegistry) clientCtx = clientCtx.WithTxConfig(txCfg) feeAmount := sdk.Coins{sdk.NewInt64Coin("atom", 150)} @@ -89,9 +99,13 @@ func TestReadTxFromFile(t *testing.T) { func TestBatchScanner_Scan(t *testing.T) { t.Parallel() - encodingConfig := simapp.MakeTestEncodingConfig() + var txGen client.TxConfig + err := depinject.Inject( + authtestutil.AppConfig, + &txGen, + ) + require.NoError(t, err) - txGen := encodingConfig.TxConfig clientCtx := client.Context{} clientCtx = clientCtx.WithTxConfig(txGen) diff --git a/x/auth/keeper/grpc_query_test.go b/x/auth/keeper/grpc_query_test.go index 2b99a257aa1e..3ecdcfe86f16 100644 --- a/x/auth/keeper/grpc_query_test.go +++ b/x/auth/keeper/grpc_query_test.go @@ -30,10 +30,10 @@ func (suite *KeeperTestSuite) TestGRPCQueryAccounts() { { "success", func() { - suite.app.AccountKeeper.SetAccount(suite.ctx, - suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, first)) - suite.app.AccountKeeper.SetAccount(suite.ctx, - suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, second)) + suite.accountKeeper.SetAccount(suite.ctx, + suite.accountKeeper.NewAccountWithAddress(suite.ctx, first)) + suite.accountKeeper.SetAccount(suite.ctx, + suite.accountKeeper.NewAccountWithAddress(suite.ctx, second)) req = &types.QueryAccountsRequest{} }, true, @@ -41,7 +41,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryAccounts() { addresses := make([]sdk.AccAddress, len(res.Accounts)) for i, acc := range res.Accounts { var account types.AccountI - err := suite.app.InterfaceRegistry().UnpackAny(acc, &account) + err := suite.interfaceRegistry.UnpackAny(acc, &account) suite.Require().NoError(err) addresses[i] = account.GetAddress() } @@ -117,14 +117,14 @@ func (suite *KeeperTestSuite) TestGRPCQueryAccount() { { "success", func() { - suite.app.AccountKeeper.SetAccount(suite.ctx, - suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr)) + suite.accountKeeper.SetAccount(suite.ctx, + suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr)) req = &types.QueryAccountRequest{Address: addr.String()} }, true, func(res *types.QueryAccountResponse) { var newAccount types.AccountI - err := suite.app.InterfaceRegistry().UnpackAny(res.Account, &newAccount) + err := suite.interfaceRegistry.UnpackAny(res.Account, &newAccount) suite.Require().NoError(err) suite.Require().NotNil(newAccount) suite.Require().True(addr.Equals(newAccount.GetAddress())) @@ -183,8 +183,8 @@ func (suite *KeeperTestSuite) TestGRPCQueryAccountAddressByID() { { "valid request", func() { - account := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr) - suite.app.AccountKeeper.SetAccount(suite.ctx, account) + account := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr) + suite.accountKeeper.SetAccount(suite.ctx, account) req = &types.QueryAccountAddressByIDRequest{Id: int64(account.GetAccountNumber())} }, true, @@ -231,7 +231,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryParameters() { "success", func() { req = &types.QueryParamsRequest{} - expParams = suite.app.AccountKeeper.GetParams(suite.ctx) + expParams = suite.accountKeeper.GetParams(suite.ctx) }, true, }, @@ -277,7 +277,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryModuleAccounts() { mintModuleExists := false for _, acc := range res.Accounts { var account types.AccountI - err := suite.app.InterfaceRegistry().UnpackAny(acc, &account) + err := suite.interfaceRegistry.UnpackAny(acc, &account) suite.Require().NoError(err) moduleAccount, ok := account.(types.ModuleAccountI) @@ -300,7 +300,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryModuleAccounts() { mintModuleExists := false for _, acc := range res.Accounts { var account types.AccountI - err := suite.app.InterfaceRegistry().UnpackAny(acc, &account) + err := suite.interfaceRegistry.UnpackAny(acc, &account) suite.Require().NoError(err) moduleAccount, ok := account.(types.ModuleAccountI) @@ -331,7 +331,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryModuleAccounts() { var moduleNames []string for _, any := range res.Accounts { var account types.AccountI - err := suite.app.InterfaceRegistry().UnpackAny(any, &account) + err := suite.interfaceRegistry.UnpackAny(any, &account) suite.Require().NoError(err) moduleAccount, ok := account.(types.ModuleAccountI) suite.Require().True(ok) diff --git a/x/auth/keeper/integration_test.go b/x/auth/keeper/integration_test.go deleted file mode 100644 index cbb9223b6fd3..000000000000 --- a/x/auth/keeper/integration_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package keeper_test - -import ( - "testing" - - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - - "github.com/cosmos/cosmos-sdk/simapp" - sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" -) - -// returns context and app with params set on account keeper -func createTestApp(t *testing.T, isCheckTx bool) (*simapp.SimApp, sdk.Context) { - app := simapp.Setup(t, isCheckTx) - ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{}) - app.AccountKeeper.SetParams(ctx, authtypes.DefaultParams()) - - return app, ctx -} diff --git a/x/auth/keeper/keeper_bench_test.go b/x/auth/keeper/keeper_bench_test.go index 5c25bf11cc26..4177a8208512 100644 --- a/x/auth/keeper/keeper_bench_test.go +++ b/x/auth/keeper/keeper_bench_test.go @@ -3,31 +3,44 @@ package keeper_test import ( "testing" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth/keeper" + "github.com/cosmos/cosmos-sdk/x/auth/testutil" + "github.com/stretchr/testify/require" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" ) func BenchmarkAccountMapperGetAccountFound(b *testing.B) { b.ReportAllocs() - app, ctx := createTestApp(&testing.T{}, false) + var accountKeeper keeper.AccountKeeper + app, err := simtestutil.Setup(testutil.AppConfig, &accountKeeper) + require.NoError(b, err) + + ctx := app.BaseApp.NewContext(false, tmproto.Header{}) // assumes b.N < 2**24 for i := 0; i < b.N; i++ { arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} addr := sdk.AccAddress(arr) - acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr) - app.AccountKeeper.SetAccount(ctx, acc) + acc := accountKeeper.NewAccountWithAddress(ctx, addr) + accountKeeper.SetAccount(ctx, acc) } b.ResetTimer() for i := 0; i < b.N; i++ { arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} - app.AccountKeeper.GetAccount(ctx, sdk.AccAddress(arr)) + accountKeeper.GetAccount(ctx, sdk.AccAddress(arr)) } } func BenchmarkAccountMapperSetAccount(b *testing.B) { b.ReportAllocs() - app, ctx := createTestApp(&testing.T{}, false) + var accountKeeper keeper.AccountKeeper + app, err := simtestutil.Setup(testutil.AppConfig, &accountKeeper) + require.NoError(b, err) + + ctx := app.BaseApp.NewContext(false, tmproto.Header{}) b.ResetTimer() @@ -35,7 +48,7 @@ func BenchmarkAccountMapperSetAccount(b *testing.B) { for i := 0; i < b.N; i++ { arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} addr := sdk.AccAddress(arr) - acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr) - app.AccountKeeper.SetAccount(ctx, acc) + acc := accountKeeper.NewAccountWithAddress(ctx, addr) + accountKeeper.SetAccount(ctx, acc) } } diff --git a/x/auth/keeper/keeper_test.go b/x/auth/keeper/keeper_test.go index b63409b988ae..e0ff0509b260 100644 --- a/x/auth/keeper/keeper_test.go +++ b/x/auth/keeper/keeper_test.go @@ -3,14 +3,18 @@ package keeper_test import ( "testing" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" "github.com/cosmos/cosmos-sdk/baseapp" - "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" "github.com/cosmos/cosmos-sdk/x/auth/keeper" + "github.com/cosmos/cosmos-sdk/x/auth/testutil" "github.com/cosmos/cosmos-sdk/x/auth/types" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" ) const ( @@ -27,17 +31,30 @@ var ( type KeeperTestSuite struct { suite.Suite - app *simapp.SimApp ctx sdk.Context - queryClient types.QueryClient + app *runtime.App + queryClient types.QueryClient + legacyAmino *codec.LegacyAmino + interfaceRegistry codectypes.InterfaceRegistry + accountKeeper keeper.AccountKeeper } func (suite *KeeperTestSuite) SetupTest() { - suite.app, suite.ctx = createTestApp(suite.T(), true) + app, err := simtestutil.Setup( + testutil.AppConfig, + &suite.legacyAmino, + &suite.interfaceRegistry, + &suite.accountKeeper, + &suite.interfaceRegistry, + ) + suite.Require().NoError(err) + + suite.app = app + suite.ctx = app.BaseApp.NewContext(true, tmproto.Header{}) - queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry()) - types.RegisterQueryServer(queryHelper, suite.app.AccountKeeper) + queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.interfaceRegistry) + types.RegisterQueryServer(queryHelper, suite.accountKeeper) suite.queryClient = types.NewQueryClient(queryHelper) } @@ -45,104 +62,89 @@ func TestKeeperTestSuite(t *testing.T) { suite.Run(t, new(KeeperTestSuite)) } -func TestAccountMapperGetSet(t *testing.T) { - app, ctx := createTestApp(t, true) +func (suite *KeeperTestSuite) TestAccountMapperGetSet() { + ctx := suite.ctx addr := sdk.AccAddress([]byte("some---------address")) // no account before its created - acc := app.AccountKeeper.GetAccount(ctx, addr) - require.Nil(t, acc) + acc := suite.accountKeeper.GetAccount(ctx, addr) + suite.Require().Nil(acc) // create account and check default values - acc = app.AccountKeeper.NewAccountWithAddress(ctx, addr) - require.NotNil(t, acc) - require.Equal(t, addr, acc.GetAddress()) - require.EqualValues(t, nil, acc.GetPubKey()) - require.EqualValues(t, 0, acc.GetSequence()) + acc = suite.accountKeeper.NewAccountWithAddress(ctx, addr) + suite.Require().NotNil(acc) + suite.Require().Equal(addr, acc.GetAddress()) + suite.Require().EqualValues(nil, acc.GetPubKey()) + suite.Require().EqualValues(0, acc.GetSequence()) // NewAccount doesn't call Set, so it's still nil - require.Nil(t, app.AccountKeeper.GetAccount(ctx, addr)) + suite.Require().Nil(suite.accountKeeper.GetAccount(ctx, addr)) // set some values on the account and save it newSequence := uint64(20) err := acc.SetSequence(newSequence) - require.NoError(t, err) - app.AccountKeeper.SetAccount(ctx, acc) + suite.Require().NoError(err) + suite.accountKeeper.SetAccount(ctx, acc) // check the new values - acc = app.AccountKeeper.GetAccount(ctx, addr) - require.NotNil(t, acc) - require.Equal(t, newSequence, acc.GetSequence()) + acc = suite.accountKeeper.GetAccount(ctx, addr) + suite.Require().NotNil(acc) + suite.Require().Equal(newSequence, acc.GetSequence()) } -func TestAccountMapperRemoveAccount(t *testing.T) { - app, ctx := createTestApp(t, true) +func (suite *KeeperTestSuite) TestAccountMapperRemoveAccount() { + ctx := suite.ctx addr1 := sdk.AccAddress([]byte("addr1---------------")) addr2 := sdk.AccAddress([]byte("addr2---------------")) // create accounts - acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) - acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) + acc1 := suite.accountKeeper.NewAccountWithAddress(ctx, addr1) + acc2 := suite.accountKeeper.NewAccountWithAddress(ctx, addr2) accSeq1 := uint64(20) accSeq2 := uint64(40) err := acc1.SetSequence(accSeq1) - require.NoError(t, err) + suite.Require().NoError(err) err = acc2.SetSequence(accSeq2) - require.NoError(t, err) - app.AccountKeeper.SetAccount(ctx, acc1) - app.AccountKeeper.SetAccount(ctx, acc2) + suite.Require().NoError(err) + suite.accountKeeper.SetAccount(ctx, acc1) + suite.accountKeeper.SetAccount(ctx, acc2) - acc1 = app.AccountKeeper.GetAccount(ctx, addr1) - require.NotNil(t, acc1) - require.Equal(t, accSeq1, acc1.GetSequence()) + acc1 = suite.accountKeeper.GetAccount(ctx, addr1) + suite.Require().NotNil(acc1) + suite.Require().Equal(accSeq1, acc1.GetSequence()) // remove one account - app.AccountKeeper.RemoveAccount(ctx, acc1) - acc1 = app.AccountKeeper.GetAccount(ctx, addr1) - require.Nil(t, acc1) + suite.accountKeeper.RemoveAccount(ctx, acc1) + acc1 = suite.accountKeeper.GetAccount(ctx, addr1) + suite.Require().Nil(acc1) - acc2 = app.AccountKeeper.GetAccount(ctx, addr2) - require.NotNil(t, acc2) - require.Equal(t, accSeq2, acc2.GetSequence()) + acc2 = suite.accountKeeper.GetAccount(ctx, addr2) + suite.Require().NotNil(acc2) + suite.Require().Equal(accSeq2, acc2.GetSequence()) } -func TestGetSetParams(t *testing.T) { - app, ctx := createTestApp(t, true) +func (suite *KeeperTestSuite) TestGetSetParams() { + ctx := suite.ctx params := types.DefaultParams() - app.AccountKeeper.SetParams(ctx, params) + suite.accountKeeper.SetParams(ctx, params) - actualParams := app.AccountKeeper.GetParams(ctx) - require.Equal(t, params, actualParams) + actualParams := suite.accountKeeper.GetParams(ctx) + suite.Require().Equal(params, actualParams) } -func TestSupply_ValidatePermissions(t *testing.T) { - app, _ := createTestApp(t, true) - - // add module accounts to supply keeper - maccPerms := simapp.GetMaccPerms() - maccPerms[holder] = nil - maccPerms[types.Burner] = []string{types.Burner} - maccPerms[types.Minter] = []string{types.Minter} - maccPerms[multiPerm] = []string{types.Burner, types.Minter, types.Staking} - maccPerms[randomPerm] = []string{"random"} - - cdc := simapp.MakeTestEncodingConfig().Codec - keeper := keeper.NewAccountKeeper( - cdc, app.GetKey(types.StoreKey), app.GetSubspace(types.ModuleName), - types.ProtoBaseAccount, maccPerms, sdk.Bech32MainPrefix, - ) +func (suite *KeeperTestSuite) TestSupply_ValidatePermissions() { - err := keeper.ValidatePermissions(multiPermAcc) - require.NoError(t, err) + err := suite.accountKeeper.ValidatePermissions(multiPermAcc) + suite.Require().NoError(err) - err = keeper.ValidatePermissions(randomPermAcc) - require.NoError(t, err) + err = suite.accountKeeper.ValidatePermissions(randomPermAcc) + suite.Require().NoError(err) // unregistered permissions otherAcc := types.NewEmptyModuleAccount("other", "other") - err = app.AccountKeeper.ValidatePermissions(otherAcc) - require.Error(t, err) + err = suite.accountKeeper.ValidatePermissions(otherAcc) + suite.Require().Error(err) } diff --git a/x/auth/keeper/querier_test.go b/x/auth/keeper/querier_test.go index fb5b4e9f5144..7eef9c3b2e14 100644 --- a/x/auth/keeper/querier_test.go +++ b/x/auth/keeper/querier_test.go @@ -2,12 +2,9 @@ package keeper_test import ( "fmt" - "testing" "github.com/cosmos/cosmos-sdk/codec" - "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" "github.com/cosmos/cosmos-sdk/testutil/testdata" @@ -15,9 +12,9 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/types" ) -func TestQueryAccount(t *testing.T) { - app, ctx := createTestApp(t, true) - legacyQuerierCdc := codec.NewAminoCodec(app.LegacyAmino()) +func (suite *KeeperTestSuite) TestQueryAccount() { + ctx := suite.ctx + legacyQuerierCdc := codec.NewAminoCodec(suite.legacyAmino) req := abci.RequestQuery{ Path: "", @@ -25,41 +22,41 @@ func TestQueryAccount(t *testing.T) { } path := []string{types.QueryAccount} - querier := keep.NewQuerier(app.AccountKeeper, legacyQuerierCdc.LegacyAmino) + querier := keep.NewQuerier(suite.accountKeeper, legacyQuerierCdc.LegacyAmino) bz, err := querier(ctx, []string{"other"}, req) - require.Error(t, err) - require.Nil(t, bz) + suite.Require().Error(err) + suite.Require().Nil(bz) req = abci.RequestQuery{ Path: fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAccount), Data: []byte{}, } res, err := querier(ctx, path, req) - require.Error(t, err) - require.Nil(t, res) + suite.Require().Error(err) + suite.Require().Nil(res) req.Data = legacyQuerierCdc.MustMarshalJSON(&types.QueryAccountRequest{Address: ""}) res, err = querier(ctx, path, req) - require.Error(t, err) - require.Nil(t, res) + suite.Require().Error(err) + suite.Require().Nil(res) _, _, addr := testdata.KeyTestPubAddr() req.Data = legacyQuerierCdc.MustMarshalJSON(&types.QueryAccountRequest{Address: addr.String()}) res, err = querier(ctx, path, req) - require.Error(t, err) - require.Nil(t, res) + suite.Require().Error(err) + suite.Require().Nil(res) - app.AccountKeeper.SetAccount(ctx, app.AccountKeeper.NewAccountWithAddress(ctx, addr)) + suite.accountKeeper.SetAccount(ctx, suite.accountKeeper.NewAccountWithAddress(ctx, addr)) res, err = querier(ctx, path, req) - require.NoError(t, err) - require.NotNil(t, res) + suite.Require().NoError(err) + suite.Require().NotNil(res) res, err = querier(ctx, path, req) - require.NoError(t, err) - require.NotNil(t, res) + suite.Require().NoError(err) + suite.Require().NotNil(res) var account types.AccountI err2 := legacyQuerierCdc.LegacyAmino.UnmarshalJSON(res, &account) - require.Nil(t, err2) + suite.Require().Nil(err2) } diff --git a/x/auth/migrations/legacytx/config_test.go b/x/auth/migrations/legacytx/config_test.go index 6626b6e59a1f..5fd2fa3657e4 100644 --- a/x/auth/migrations/legacytx/config_test.go +++ b/x/auth/migrations/legacytx/config_test.go @@ -10,7 +10,7 @@ import ( "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx" - txtestutil "github.com/cosmos/cosmos-sdk/x/auth/testutil/tx" + txtestutil "github.com/cosmos/cosmos-sdk/x/auth/testutilhelpers/tx" ) func testCodec() *codec.LegacyAmino { diff --git a/x/auth/migrations/v043/store_test.go b/x/auth/migrations/v043/store_test.go index d1445386895b..afd2b9a67d82 100644 --- a/x/auth/migrations/v043/store_test.go +++ b/x/auth/migrations/v043/store_test.go @@ -8,10 +8,12 @@ import ( "github.com/stretchr/testify/require" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/cosmos/cosmos-sdk/simapp" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" - authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + "github.com/cosmos/cosmos-sdk/x/auth/keeper" + "github.com/cosmos/cosmos-sdk/x/auth/testutil" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported" "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" @@ -21,10 +23,26 @@ import ( ) func TestMigrateVestingAccounts(t *testing.T) { + var ( + accountKeeper keeper.AccountKeeper + bankKeeper bankkeeper.Keeper + stakingKeeper *stakingkeeper.Keeper + ) + app, err := simtestutil.Setup( + testutil.AppConfig, + &accountKeeper, + &bankKeeper, + &stakingKeeper, + ) + require.NoError(t, err) + + ctx := app.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()}) + stakingKeeper.SetParams(ctx, stakingtypes.DefaultParams()) + testCases := []struct { name string - prepareFunc func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) - garbageFunc func(ctx sdk.Context, vesting exported.VestingAccount, app *simapp.SimApp) error + prepareFunc func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) + garbageFunc func(ctx sdk.Context, vesting exported.VestingAccount, accounKeeper keeper.AccountKeeper) error tokenAmount int64 expVested int64 expFree int64 @@ -32,20 +50,22 @@ func TestMigrateVestingAccounts(t *testing.T) { }{ { "delayed vesting has vested, multiple delegations less than the total account balance", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(200))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(200))) delayedAccount := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.BlockTime().Unix()) ctx = ctx.WithBlockTime(ctx.BlockTime().AddDate(1, 0, 0)) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetParams(ctx, authtypes.DefaultParams()) + accountKeeper.SetAccount(ctx, delayedAccount) - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) require.NoError(t, err) - _, err = app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) require.NoError(t, err) - _, err = app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) require.NoError(t, err) }, cleartTrackingFields, @@ -56,16 +76,17 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "delayed vesting has vested, single delegations which exceed the vested amount", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + require.NoError(t, err) baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(200))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(200))) delayedAccount := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.BlockTime().Unix()) ctx = ctx.WithBlockTime(ctx.BlockTime().AddDate(1, 0, 0)) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(300), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(300), stakingtypes.Unbonded, validator, true) require.NoError(t, err) }, cleartTrackingFields, @@ -76,20 +97,20 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "delayed vesting has vested, multiple delegations which exceed the vested amount", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(200))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(200))) delayedAccount := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.BlockTime().Unix()) ctx = ctx.WithBlockTime(ctx.BlockTime().AddDate(1, 0, 0)) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) require.NoError(t, err) - _, err = app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) require.NoError(t, err) - _, err = app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) require.NoError(t, err) }, cleartTrackingFields, @@ -100,14 +121,14 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "delayed vesting has not vested, single delegations which exceed the vested amount", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(200))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(200))) delayedAccount := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.BlockTime().AddDate(1, 0, 0).Unix()) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(300), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(300), stakingtypes.Unbonded, validator, true) require.NoError(t, err) }, cleartTrackingFields, @@ -118,18 +139,18 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "delayed vesting has not vested, multiple delegations which exceed the vested amount", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(200))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(200))) delayedAccount := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.BlockTime().AddDate(1, 0, 0).Unix()) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) require.NoError(t, err) - _, err = app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) require.NoError(t, err) - _, err = app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) require.NoError(t, err) }, cleartTrackingFields, @@ -140,18 +161,18 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "not end time", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(300))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(300))) delayedAccount := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.BlockTime().AddDate(1, 0, 0).Unix()) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) require.NoError(t, err) - _, err = app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) require.NoError(t, err) - _, err = app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(100), stakingtypes.Unbonded, validator, true) require.NoError(t, err) }, cleartTrackingFields, @@ -162,14 +183,14 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "delayed vesting has not vested, single delegation greater than the total account balance", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(300))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(300))) delayedAccount := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.BlockTime().AddDate(1, 0, 0).Unix()) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(300), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(300), stakingtypes.Unbonded, validator, true) require.NoError(t, err) }, cleartTrackingFields, @@ -180,16 +201,16 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "delayed vesting has vested, single delegation greater than the total account balance", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(300))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(300))) delayedAccount := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.BlockTime().Unix()) ctx = ctx.WithBlockTime(ctx.BlockTime().AddDate(1, 0, 0)) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(300), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(300), stakingtypes.Unbonded, validator, true) require.NoError(t, err) }, cleartTrackingFields, @@ -200,18 +221,18 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "continuous vesting, start time after blocktime", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { startTime := ctx.BlockTime().AddDate(1, 0, 0).Unix() endTime := ctx.BlockTime().AddDate(2, 0, 0).Unix() baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(300))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(300))) delayedAccount := types.NewContinuousVestingAccount(baseAccount, vestedCoins, startTime, endTime) ctx = ctx.WithBlockTime(ctx.BlockTime().AddDate(1, 0, 0)) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(300), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(300), stakingtypes.Unbonded, validator, true) require.NoError(t, err) }, cleartTrackingFields, @@ -222,18 +243,18 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "continuous vesting, start time passed but not ended", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { startTime := ctx.BlockTime().AddDate(-1, 0, 0).Unix() endTime := ctx.BlockTime().AddDate(2, 0, 0).Unix() baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(300))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(300))) delayedAccount := types.NewContinuousVestingAccount(baseAccount, vestedCoins, startTime, endTime) ctx = ctx.WithBlockTime(ctx.BlockTime().AddDate(1, 0, 0)) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(300), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(300), stakingtypes.Unbonded, validator, true) require.NoError(t, err) }, cleartTrackingFields, @@ -244,18 +265,18 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "continuous vesting, start time and endtime passed", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { startTime := ctx.BlockTime().AddDate(-2, 0, 0).Unix() endTime := ctx.BlockTime().AddDate(-1, 0, 0).Unix() baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(300))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(300))) delayedAccount := types.NewContinuousVestingAccount(baseAccount, vestedCoins, startTime, endTime) ctx = ctx.WithBlockTime(ctx.BlockTime().AddDate(1, 0, 0)) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(300), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(300), stakingtypes.Unbonded, validator, true) require.NoError(t, err) }, cleartTrackingFields, @@ -266,9 +287,9 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "periodic vesting account, yet to be vested, some rewards delegated", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(100))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(100))) start := ctx.BlockTime().Unix() + int64(time.Hour/time.Second) @@ -281,9 +302,9 @@ func TestMigrateVestingAccounts(t *testing.T) { account := types.NewPeriodicVestingAccount(baseAccount, vestedCoins, start, periods) - app.AccountKeeper.SetAccount(ctx, account) + accountKeeper.SetAccount(ctx, account) - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(150), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(150), stakingtypes.Unbonded, validator, true) require.NoError(t, err) }, cleartTrackingFields, @@ -294,7 +315,7 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "periodic vesting account, nothing has vested yet", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { /* Test case: - periodic vesting account starts at time 1601042400 @@ -306,28 +327,28 @@ func TestMigrateVestingAccounts(t *testing.T) { */ startTime := int64(1601042400) baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(3666666670000))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(3666666670000))) periods := []types.Period{ { Length: 31536000, - Amount: sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(1833333335000))), + Amount: sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(1833333335000))), }, { Length: 15638400, - Amount: sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(916666667500))), + Amount: sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(916666667500))), }, { Length: 15897600, - Amount: sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(916666667500))), + Amount: sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(916666667500))), }, } delayedAccount := types.NewPeriodicVestingAccount(baseAccount, vestedCoins, startTime, periods) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) // delegation of the original vesting - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(3666666670000), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(3666666670000), stakingtypes.Unbonded, validator, true) require.NoError(t, err) }, cleartTrackingFields, @@ -338,7 +359,7 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "periodic vesting account, all has vested", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { /* Test case: - periodic vesting account starts at time 1601042400 @@ -350,19 +371,19 @@ func TestMigrateVestingAccounts(t *testing.T) { */ startTime := int64(1601042400) baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(3666666670000))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(3666666670000))) periods := []types.Period{ { Length: 31536000, - Amount: sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(1833333335000))), + Amount: sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(1833333335000))), }, { Length: 15638400, - Amount: sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(916666667500))), + Amount: sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(916666667500))), }, { Length: 15897600, - Amount: sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(916666667500))), + Amount: sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(916666667500))), }, } @@ -370,10 +391,10 @@ func TestMigrateVestingAccounts(t *testing.T) { ctx = ctx.WithBlockTime(time.Unix(1601042400+31536000+15897600+15897600+1, 0)) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) // delegation of the original vesting - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(3666666670000), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(3666666670000), stakingtypes.Unbonded, validator, true) require.NoError(t, err) }, cleartTrackingFields, @@ -384,7 +405,7 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "periodic vesting account, first period has vested", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { /* Test case: - periodic vesting account starts at time 1601042400 @@ -396,19 +417,19 @@ func TestMigrateVestingAccounts(t *testing.T) { */ startTime := int64(1601042400) baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(3666666670000))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(3666666670000))) periods := []types.Period{ { Length: 31536000, - Amount: sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(1833333335000))), + Amount: sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(1833333335000))), }, { Length: 15638400, - Amount: sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(916666667500))), + Amount: sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(916666667500))), }, { Length: 15897600, - Amount: sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(916666667500))), + Amount: sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(916666667500))), }, } @@ -416,10 +437,10 @@ func TestMigrateVestingAccounts(t *testing.T) { ctx = ctx.WithBlockTime(time.Unix(1601042400+31536000+1, 0)) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) // delegation of the original vesting - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(3666666670000), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(3666666670000), stakingtypes.Unbonded, validator, true) require.NoError(t, err) }, cleartTrackingFields, @@ -430,7 +451,7 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "periodic vesting account, first 2 period has vested", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { /* Test case: - periodic vesting account starts at time 1601042400 @@ -442,19 +463,19 @@ func TestMigrateVestingAccounts(t *testing.T) { */ startTime := int64(1601042400) baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(3666666670000))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(3666666670000))) periods := []types.Period{ { Length: 31536000, - Amount: sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(1833333335000))), + Amount: sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(1833333335000))), }, { Length: 15638400, - Amount: sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(916666667500))), + Amount: sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(916666667500))), }, { Length: 15897600, - Amount: sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(916666667500))), + Amount: sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(916666667500))), }, } @@ -462,10 +483,10 @@ func TestMigrateVestingAccounts(t *testing.T) { ctx = ctx.WithBlockTime(time.Unix(1601042400+31536000+15638400+1, 0)) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) // delegation of the original vesting - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(3666666670000), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(3666666670000), stakingtypes.Unbonded, validator, true) require.NoError(t, err) }, cleartTrackingFields, @@ -476,16 +497,16 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "vesting account has unbonding delegations in place", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(300))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(300))) delayedAccount := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.BlockTime().AddDate(10, 0, 0).Unix()) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) // delegation of the original vesting - _, err := app.StakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(300), stakingtypes.Unbonded, validator, true) + _, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdk.NewInt(300), stakingtypes.Unbonded, validator, true) require.NoError(t, err) ctx = ctx.WithBlockTime(ctx.BlockTime().AddDate(1, 0, 0)) @@ -494,7 +515,7 @@ func TestMigrateVestingAccounts(t *testing.T) { require.NoError(t, err) // un-delegation of the original vesting - _, err = app.StakingKeeper.Undelegate(ctx, delegatorAddr, valAddr, sdk.NewDecFromInt(sdk.NewInt(300))) + _, err = stakingKeeper.Undelegate(ctx, delegatorAddr, valAddr, sdk.NewDecFromInt(sdk.NewInt(300))) require.NoError(t, err) }, cleartTrackingFields, @@ -505,13 +526,13 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "vesting account has never delegated anything", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(300))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(300))) delayedAccount := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.BlockTime().AddDate(10, 0, 0).Unix()) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) }, cleartTrackingFields, 450, @@ -521,13 +542,13 @@ func TestMigrateVestingAccounts(t *testing.T) { }, { "vesting account has no delegation but dirty DelegatedFree and DelegatedVesting fields", - func(app *simapp.SimApp, ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { + func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) { baseAccount := authtypes.NewBaseAccountWithAddress(delegatorAddr) - vestedCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(300))) + vestedCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(300))) delayedAccount := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.BlockTime().AddDate(10, 0, 0).Unix()) - app.AccountKeeper.SetAccount(ctx, delayedAccount) + accountKeeper.SetAccount(ctx, delayedAccount) }, dirtyTrackingFields, 450, @@ -540,48 +561,45 @@ func TestMigrateVestingAccounts(t *testing.T) { for _, tc := range testCases { tc := tc t.Run(tc.name, func(t *testing.T) { - app := simapp.Setup(t, false) - ctx := app.BaseApp.NewContext(false, tmproto.Header{ - Time: time.Now(), - }) - addrs := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(tc.tokenAmount)) + accountKeeper.SetParams(ctx, authtypes.DefaultParams()) + addrs := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 1, sdk.NewInt(tc.tokenAmount)) delegatorAddr := addrs[0] - _, valAddr := createValidator(t, ctx, app, tc.tokenAmount*2) - validator, found := app.StakingKeeper.GetValidator(ctx, valAddr) + _, valAddr := createValidator(t, ctx, bankKeeper, stakingKeeper, tc.tokenAmount*2) + validator, found := stakingKeeper.GetValidator(ctx, valAddr) require.True(t, found) - tc.prepareFunc(app, ctx, validator, delegatorAddr) + tc.prepareFunc(ctx, validator, delegatorAddr) if tc.blockTime != 0 { ctx = ctx.WithBlockTime(time.Unix(tc.blockTime, 0)) } // We introduce the bug - savedAccount := app.AccountKeeper.GetAccount(ctx, delegatorAddr) + savedAccount := accountKeeper.GetAccount(ctx, delegatorAddr) vestingAccount, ok := savedAccount.(exported.VestingAccount) require.True(t, ok) - require.NoError(t, tc.garbageFunc(ctx, vestingAccount, app)) + require.NoError(t, tc.garbageFunc(ctx, vestingAccount, accountKeeper)) - m := authkeeper.NewMigrator(app.AccountKeeper, app.GRPCQueryRouter()) + m := keeper.NewMigrator(accountKeeper, app.GRPCQueryRouter()) require.NoError(t, m.Migrate1to2(ctx)) var expVested sdk.Coins var expFree sdk.Coins if tc.expVested != 0 { - expVested = sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(tc.expVested))) + expVested = sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(tc.expVested))) } if tc.expFree != 0 { - expFree = sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(tc.expFree))) + expFree = sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(tc.expFree))) } trackingCorrected( ctx, t, - app.AccountKeeper, + accountKeeper, savedAccount.GetAddress(), expVested, expFree, @@ -590,7 +608,7 @@ func TestMigrateVestingAccounts(t *testing.T) { } } -func trackingCorrected(ctx sdk.Context, t *testing.T, ak authkeeper.AccountKeeper, addr sdk.AccAddress, expDelVesting sdk.Coins, expDelFree sdk.Coins) { +func trackingCorrected(ctx sdk.Context, t *testing.T, ak keeper.AccountKeeper, addr sdk.AccAddress, expDelVesting sdk.Coins, expDelFree sdk.Coins) { t.Helper() baseAccount := ak.GetAccount(ctx, addr) vDA, ok := baseAccount.(exported.VestingAccount) @@ -602,20 +620,21 @@ func trackingCorrected(ctx sdk.Context, t *testing.T, ak authkeeper.AccountKeepe require.True(t, freeOk, vDA.GetDelegatedFree().String()) } -func cleartTrackingFields(ctx sdk.Context, vesting exported.VestingAccount, app *simapp.SimApp) error { +func cleartTrackingFields(ctx sdk.Context, vesting exported.VestingAccount, accountKeeper keeper.AccountKeeper) error { + switch t := vesting.(type) { case *types.DelayedVestingAccount: t.DelegatedFree = nil t.DelegatedVesting = nil - app.AccountKeeper.SetAccount(ctx, t) + accountKeeper.SetAccount(ctx, t) case *types.ContinuousVestingAccount: t.DelegatedFree = nil t.DelegatedVesting = nil - app.AccountKeeper.SetAccount(ctx, t) + accountKeeper.SetAccount(ctx, t) case *types.PeriodicVestingAccount: t.DelegatedFree = nil t.DelegatedVesting = nil - app.AccountKeeper.SetAccount(ctx, t) + accountKeeper.SetAccount(ctx, t) default: return fmt.Errorf("expected vesting account, found %t", t) } @@ -623,22 +642,23 @@ func cleartTrackingFields(ctx sdk.Context, vesting exported.VestingAccount, app return nil } -func dirtyTrackingFields(ctx sdk.Context, vesting exported.VestingAccount, app *simapp.SimApp) error { +func dirtyTrackingFields(ctx sdk.Context, vesting exported.VestingAccount, accountKeeper keeper.AccountKeeper) error { + dirt := sdk.NewCoins(sdk.NewInt64Coin("stake", 42)) switch t := vesting.(type) { case *types.DelayedVestingAccount: t.DelegatedFree = dirt t.DelegatedVesting = dirt - app.AccountKeeper.SetAccount(ctx, t) + accountKeeper.SetAccount(ctx, t) case *types.ContinuousVestingAccount: t.DelegatedFree = dirt t.DelegatedVesting = dirt - app.AccountKeeper.SetAccount(ctx, t) + accountKeeper.SetAccount(ctx, t) case *types.PeriodicVestingAccount: t.DelegatedFree = dirt t.DelegatedVesting = dirt - app.AccountKeeper.SetAccount(ctx, t) + accountKeeper.SetAccount(ctx, t) default: return fmt.Errorf("expected vesting account, found %t", t) } @@ -646,32 +666,24 @@ func dirtyTrackingFields(ctx sdk.Context, vesting exported.VestingAccount, app * return nil } -func createValidator(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers int64) (sdk.AccAddress, sdk.ValAddress) { +func createValidator(t *testing.T, ctx sdk.Context, bankKeeper bankkeeper.Keeper, stakingKeeper *stakingkeeper.Keeper, powers int64) (sdk.AccAddress, sdk.ValAddress) { + valTokens := sdk.TokensFromConsensusPower(powers, sdk.DefaultPowerReduction) - addrs := simapp.AddTestAddrsIncremental(app, ctx, 1, valTokens) + addrs := simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, ctx, 1, valTokens) valAddrs := simtestutil.ConvertAddrsToValAddrs(addrs) pks := simtestutil.CreateTestPubKeys(1) - cdc := simapp.MakeTestEncodingConfig().Codec - - app.StakingKeeper = stakingkeeper.NewKeeper( - cdc, - app.GetKey(stakingtypes.StoreKey), - app.AccountKeeper, - app.BankKeeper, - app.GetSubspace(stakingtypes.ModuleName), - ) val1, err := stakingtypes.NewValidator(valAddrs[0], pks[0], stakingtypes.Description{}) require.NoError(t, err) - app.StakingKeeper.SetValidator(ctx, val1) - require.NoError(t, app.StakingKeeper.SetValidatorByConsAddr(ctx, val1)) - app.StakingKeeper.SetNewValidatorByPowerIndex(ctx, val1) + stakingKeeper.SetValidator(ctx, val1) + require.NoError(t, stakingKeeper.SetValidatorByConsAddr(ctx, val1)) + stakingKeeper.SetNewValidatorByPowerIndex(ctx, val1) - _, err = app.StakingKeeper.Delegate(ctx, addrs[0], valTokens, stakingtypes.Unbonded, val1, true) + _, err = stakingKeeper.Delegate(ctx, addrs[0], valTokens, stakingtypes.Unbonded, val1, true) require.NoError(t, err) - _ = staking.EndBlocker(ctx, app.StakingKeeper) + _ = staking.EndBlocker(ctx, stakingKeeper) return addrs[0], valAddrs[0] } diff --git a/x/auth/migrations/v046/store_test.go b/x/auth/migrations/v046/store_test.go index 698cdbb8c24d..13db36689ac4 100644 --- a/x/auth/migrations/v046/store_test.go +++ b/x/auth/migrations/v046/store_test.go @@ -6,8 +6,10 @@ import ( "time" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" - "github.com/cosmos/cosmos-sdk/simapp" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" "github.com/cosmos/cosmos-sdk/x/auth/keeper" + "github.com/cosmos/cosmos-sdk/x/auth/testutil" + "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/stretchr/testify/require" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -15,19 +17,25 @@ import ( // TestMigrateMapAccAddressToAccNumberKey test cases for state migration of map to accAddr to accNum func TestMigrateMapAccAddressToAccNumberKey(t *testing.T) { - app := simapp.Setup(t, false) + var ( + accountKeeper keeper.AccountKeeper + ) + + app, err := simtestutil.Setup( + testutil.AppConfig, + &accountKeeper, + ) + require.NoError(t, err) // new base account senderPrivKey := secp256k1.GenPrivKey() randAccNumber := uint64(rand.Intn(100000-10000) + 10000) acc := types.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), randAccNumber, 0) - ctx := app.App.BaseApp.NewContext(false, tmproto.Header{ - Time: time.Now(), - }) + ctx := app.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()}) // migrator - m := keeper.NewMigrator(app.AccountKeeper, app.GRPCQueryRouter()) + m := keeper.NewMigrator(accountKeeper, app.GRPCQueryRouter()) // set the account to store with map acc addr to acc number require.NoError(t, m.V45_SetAccount(ctx, acc)) @@ -55,7 +63,7 @@ func TestMigrateMapAccAddressToAccNumberKey(t *testing.T) { } // get the account address by acc id - accAddr := app.AccountKeeper.GetAccountAddressByID(ctx, tc.accNum) + accAddr := accountKeeper.GetAccountAddressByID(ctx, tc.accNum) if tc.doMigration { require.Equal(t, accAddr, acc.Address) diff --git a/x/auth/module_test.go b/x/auth/module_test.go index 84d16cd54762..8e58792f44aa 100644 --- a/x/auth/module_test.go +++ b/x/auth/module_test.go @@ -4,34 +4,20 @@ import ( "testing" "github.com/stretchr/testify/require" - abcitypes "github.com/tendermint/tendermint/abci/types" - tmjson "github.com/tendermint/tendermint/libs/json" - "github.com/tendermint/tendermint/libs/log" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - dbm "github.com/tendermint/tm-db" - "github.com/cosmos/cosmos-sdk/simapp" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" + "github.com/cosmos/cosmos-sdk/x/auth/keeper" + "github.com/cosmos/cosmos-sdk/x/auth/testutil" "github.com/cosmos/cosmos-sdk/x/auth/types" ) func TestItCreatesModuleAccountOnInitBlock(t *testing.T) { - db := dbm.NewMemDB() - encCdc := simapp.MakeTestEncodingConfig() - app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, 5, encCdc, simtestutil.NewAppOptionsWithFlagHome(simapp.DefaultNodeHome)) - - genesisState := simapp.GenesisStateWithSingleValidator(t, app) - stateBytes, err := tmjson.Marshal(genesisState) + var accountKeeper keeper.AccountKeeper + app, err := simtestutil.SetupAtGenesis(testutil.AppConfig, &accountKeeper) require.NoError(t, err) - app.InitChain( - abcitypes.RequestInitChain{ - AppStateBytes: stateBytes, - ChainId: "test-chain-id", - }, - ) - ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - acc := app.AccountKeeper.GetAccount(ctx, types.NewModuleAddress(types.FeeCollectorName)) + acc := accountKeeper.GetAccount(ctx, types.NewModuleAddress(types.FeeCollectorName)) require.NotNil(t, acc) } diff --git a/x/auth/signing/verify_test.go b/x/auth/signing/verify_test.go index db91bc15e06d..6e5d8f4f9fe3 100644 --- a/x/auth/signing/verify_test.go +++ b/x/auth/signing/verify_test.go @@ -4,20 +4,24 @@ import ( "testing" "github.com/stretchr/testify/require" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/cosmos/cosmos-sdk/codec" kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/crypto/types/multisig" - "github.com/cosmos/cosmos-sdk/simapp" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth/ante" + + "github.com/cosmos/cosmos-sdk/x/auth/keeper" "github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx" "github.com/cosmos/cosmos-sdk/x/auth/signing" + authtestutil "github.com/cosmos/cosmos-sdk/x/auth/testutil" "github.com/cosmos/cosmos-sdk/x/auth/types" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" "github.com/cosmos/cosmos-sdk/x/bank/testutil" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" ) func TestVerifySignature(t *testing.T) { @@ -29,22 +33,29 @@ func TestVerifySignature(t *testing.T) { chainId = "test-chain" ) - app, ctx := createTestApp(t, false) - ctx = ctx.WithBlockHeight(1) + var ( + accountKeeper keeper.AccountKeeper + bankKeeper bankkeeper.Keeper + ) + + app, err := simtestutil.Setup(authtestutil.AppConfig, &accountKeeper, &bankKeeper) + require.NoError(t, err) + + ctx := app.BaseApp.NewContext(false, tmproto.Header{}).WithBlockHeight(1) cdc := codec.NewLegacyAmino() sdk.RegisterLegacyAminoCodec(cdc) types.RegisterLegacyAminoCodec(cdc) cdc.RegisterConcrete(testdata.TestMsg{}, "cosmos-sdk/Test", nil) - acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr) - _ = app.AccountKeeper.NewAccountWithAddress(ctx, addr1) - app.AccountKeeper.SetAccount(ctx, acc1) + acc1 := accountKeeper.NewAccountWithAddress(ctx, addr) + _ = accountKeeper.NewAccountWithAddress(ctx, addr1) + accountKeeper.SetAccount(ctx, acc1) balances := sdk.NewCoins(sdk.NewInt64Coin("atom", 200)) - require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr, balances)) - acc, err := ante.GetSignerAcc(ctx, app.AccountKeeper, addr) + require.NoError(t, testutil.FundAccount(bankKeeper, ctx, addr, balances)) + acc, err := ante.GetSignerAcc(ctx, accountKeeper, addr) require.NoError(t, err) - require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr, balances)) + require.NoError(t, testutil.FundAccount(bankKeeper, ctx, addr, balances)) msgs := []sdk.Msg{testdata.NewTestMsg(addr)} fee := legacytx.NewStdFee(50000, sdk.Coins{sdk.NewInt64Coin("atom", 150)}) @@ -98,12 +109,3 @@ func TestVerifySignature(t *testing.T) { err = signing.VerifySignature(multisigKey, signerData, multisignature, handler, stdTx) require.NoError(t, err) } - -// returns context and app with params set on account keeper -func createTestApp(t *testing.T, isCheckTx bool) (*simapp.SimApp, sdk.Context) { - app := simapp.Setup(t, isCheckTx) - ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{}) - app.AccountKeeper.SetParams(ctx, types.DefaultParams()) - - return app, ctx -} diff --git a/x/auth/simulation/decoder_test.go b/x/auth/simulation/decoder_test.go index c79f5c4595d9..3dfb6576c880 100644 --- a/x/auth/simulation/decoder_test.go +++ b/x/auth/simulation/decoder_test.go @@ -7,11 +7,16 @@ import ( gogotypes "github.com/gogo/protobuf/types" "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" - "github.com/cosmos/cosmos-sdk/simapp" + "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/auth/simulation" + "github.com/cosmos/cosmos-sdk/x/auth/testutil" + + authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -21,12 +26,17 @@ var ( ) func TestDecodeStore(t *testing.T) { - app := simapp.Setup(t, false) - cdc := simapp.MakeTestEncodingConfig().Codec + var ( + cdc codec.Codec + accountKeeper authkeeper.AccountKeeper + ) + err := depinject.Inject(testutil.AppConfig, &cdc, &accountKeeper) + require.NoError(t, err) + acc := types.NewBaseAccountWithAddress(delAddr1) - dec := simulation.NewDecodeStore(app.AccountKeeper) + dec := simulation.NewDecodeStore(accountKeeper) - accBz, err := app.AccountKeeper.MarshalAccount(acc) + accBz, err := accountKeeper.MarshalAccount(acc) require.NoError(t, err) globalAccNumber := gogotypes.UInt64Value{Value: 10} diff --git a/x/auth/testutil/app.yaml b/x/auth/testutil/app.yaml index 72b6f3e9466a..d8a7275fd5de 100644 --- a/x/auth/testutil/app.yaml +++ b/x/auth/testutil/app.yaml @@ -21,6 +21,10 @@ modules: permissions: [burner, staking] - account: not_bonded_tokens_pool permissions: [burner, staking] + - account: multiple permissions account # dummy permissions + permissions: [burner, minter, staking] + - account: random permission + permissions: [random] - name: bank config: diff --git a/x/auth/testutil/tx/suite.go b/x/auth/testutilhelpers/tx/suite.go similarity index 100% rename from x/auth/testutil/tx/suite.go rename to x/auth/testutilhelpers/tx/suite.go diff --git a/x/auth/tx/aux_test.go b/x/auth/tx/aux_test.go index 7e60300ed13f..2f86c9ec7177 100644 --- a/x/auth/tx/aux_test.go +++ b/x/auth/tx/aux_test.go @@ -5,15 +5,17 @@ import ( "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/tx" clienttx "github.com/cosmos/cosmos-sdk/client/tx" codectypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/depinject" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" txtypes "github.com/cosmos/cosmos-sdk/types/tx" "github.com/cosmos/cosmos-sdk/types/tx/signing" authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" + "github.com/cosmos/cosmos-sdk/x/auth/testutil" ) var ( @@ -37,8 +39,18 @@ var ( // Then it tests integrating the 2 AuxSignerData into a // client.TxBuilder created by the fee payer. func TestBuilderWithAux(t *testing.T) { - encCfg := simapp.MakeTestEncodingConfig() - testdata.RegisterInterfaces(encCfg.InterfaceRegistry) + var ( + interfaceRegistry codectypes.InterfaceRegistry + txConfig client.TxConfig + ) + + err := depinject.Inject(testutil.AppConfig, + &interfaceRegistry, + &txConfig, + ) + require.NoError(t, err) + + testdata.RegisterInterfaces(interfaceRegistry) // Create an AuxTxBuilder for tipper (1st signer) tipperBuilder, tipperSig := makeTipperTxBuilder(t) @@ -71,7 +83,7 @@ func TestBuilderWithAux(t *testing.T) { require.NoError(t, err) // Fee payer (3rd and last signer) creates a TxBuilder. - w := encCfg.TxConfig.NewTxBuilder() + w := txConfig.NewTxBuilder() // Note: we're testing calling AddAuxSignerData in the wrong order, i.e. // adding the aux2 signer data first before the tipper. err = w.AddAuxSignerData(aux2SignerData) @@ -128,7 +140,7 @@ func TestBuilderWithAux(t *testing.T) { PubKey: feepayerPk, Sequence: 15, }) - signBz, err = encCfg.TxConfig.SignModeHandler().GetSignBytes( + signBz, err = txConfig.SignModeHandler().GetSignBytes( signing.SignMode_SIGN_MODE_DIRECT, authsigning.SignerData{ Address: feepayerAddr.String(), @@ -153,9 +165,9 @@ func TestBuilderWithAux(t *testing.T) { }) // Make sure tx is correct. - txBz, err := encCfg.TxConfig.TxEncoder()(w.GetTx()) + txBz, err := txConfig.TxEncoder()(w.GetTx()) require.NoError(t, err) - tx, err := encCfg.TxConfig.TxDecoder()(txBz) + tx, err := txConfig.TxDecoder()(txBz) require.NoError(t, err) require.Equal(t, tx.(sdk.FeeTx).FeePayer(), feepayerAddr) require.Equal(t, tx.(sdk.FeeTx).GetFee(), fee) diff --git a/x/auth/tx/config_test.go b/x/auth/tx/config_test.go index a3524a9e02f4..22628bb5479c 100644 --- a/x/auth/tx/config_test.go +++ b/x/auth/tx/config_test.go @@ -10,7 +10,7 @@ import ( "github.com/cosmos/cosmos-sdk/std" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" - txtestutil "github.com/cosmos/cosmos-sdk/x/auth/testutil/tx" + txtestutil "github.com/cosmos/cosmos-sdk/x/auth/testutilhelpers/tx" ) func TestGenerator(t *testing.T) { diff --git a/x/auth/types/account_test.go b/x/auth/types/account_test.go index ad24595c3fcf..5289f4aeeb54 100644 --- a/x/auth/types/account_test.go +++ b/x/auth/types/account_test.go @@ -10,9 +10,11 @@ import ( "sigs.k8s.io/yaml" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" - "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/depinject" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" + authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + "github.com/cosmos/cosmos-sdk/x/auth/testutil" "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -60,26 +62,29 @@ func TestBaseSequence(t *testing.T) { } func TestBaseAccountMarshal(t *testing.T) { - app := simapp.Setup(t, false) + var accountKeeper authkeeper.AccountKeeper + + err := depinject.Inject(testutil.AppConfig, &accountKeeper) + require.NoError(t, err) _, pub, addr := testdata.KeyTestPubAddr() acc := types.NewBaseAccountWithAddress(addr) seq := uint64(7) // set everything on the account - err := acc.SetPubKey(pub) + err = acc.SetPubKey(pub) require.Nil(t, err) err = acc.SetSequence(seq) require.Nil(t, err) - bz, err := app.AccountKeeper.MarshalAccount(acc) + bz, err := accountKeeper.MarshalAccount(acc) require.Nil(t, err) - acc2, err := app.AccountKeeper.UnmarshalAccount(bz) + acc2, err := accountKeeper.UnmarshalAccount(bz) require.Nil(t, err) require.Equal(t, acc, acc2) // error on bad bytes - _, err = app.AccountKeeper.UnmarshalAccount(bz[:len(bz)/2]) + _, err = accountKeeper.UnmarshalAccount(bz[:len(bz)/2]) require.NotNil(t, err) } diff --git a/x/auth/types/common_test.go b/x/auth/types/common_test.go deleted file mode 100644 index c47914fc97fc..000000000000 --- a/x/auth/types/common_test.go +++ /dev/null @@ -1,10 +0,0 @@ -package types_test - -import ( - "github.com/cosmos/cosmos-sdk/simapp" -) - -var ( - ecdc = simapp.MakeTestEncodingConfig() - appCodec, legacyAmino = ecdc.Codec, ecdc.Amino -) diff --git a/x/auth/types/genesis_test.go b/x/auth/types/genesis_test.go index 7e96036053fc..b6f0bce62e7a 100644 --- a/x/auth/types/genesis_test.go +++ b/x/auth/types/genesis_test.go @@ -4,12 +4,15 @@ import ( "encoding/json" "testing" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/depinject" proto "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/require" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth/testutil" "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -51,6 +54,9 @@ func TestValidateGenesisDuplicateAccounts(t *testing.T) { } func TestGenesisAccountIterator(t *testing.T) { + var cdc codec.Codec + depinject.Inject(testutil.AppConfig, &cdc) + acc1 := types.NewBaseAccountWithAddress(sdk.AccAddress(addr1)) acc2 := types.NewBaseAccountWithAddress(sdk.AccAddress(addr2)) @@ -62,14 +68,14 @@ func TestGenesisAccountIterator(t *testing.T) { authGenState.Accounts = accounts appGenesis := make(map[string]json.RawMessage) - authGenStateBz, err := appCodec.MarshalJSON(authGenState) + authGenStateBz, err := cdc.MarshalJSON(authGenState) require.NoError(t, err) appGenesis[types.ModuleName] = authGenStateBz var addresses []sdk.AccAddress types.GenesisAccountIterator{}.IterateGenesisAccounts( - appCodec, appGenesis, func(acc types.AccountI) (stop bool) { + cdc, appGenesis, func(acc types.AccountI) (stop bool) { addresses = append(addresses, acc.GetAddress()) return false },