diff --git a/x/auth/middleware/middleware_test.go b/x/auth/middleware/middleware_test.go index e3b04983b6f..23def99427f 100644 --- a/x/auth/middleware/middleware_test.go +++ b/x/auth/middleware/middleware_test.go @@ -29,7 +29,7 @@ func (s *MWTestSuite) TestSimulateGasCost() { txBuilder := s.clientCtx.TxConfig.NewTxBuilder() // Same data for every test cases - accounts := s.createTestAccounts(ctx, 3) + accounts := s.createTestAccounts(ctx, 3, sdk.Coins{sdk.NewInt64Coin("atom", 10000000)}) msgs := []sdk.Msg{ testdata.NewTestMsg(accounts[0].acc.GetAddress(), accounts[1].acc.GetAddress()), testdata.NewTestMsg(accounts[2].acc.GetAddress(), accounts[0].acc.GetAddress()), @@ -166,7 +166,7 @@ func (s *MWTestSuite) TestTxHandlerAccountNumbers() { txBuilder := s.clientCtx.TxConfig.NewTxBuilder() // Same data for every test cases - accounts := s.createTestAccounts(ctx, 2) + accounts := s.createTestAccounts(ctx, 2, sdk.Coins{sdk.NewInt64Coin("atom", 10000000)}) feeAmount := testdata.NewTestFeeAmount() gasLimit := testdata.NewTestGasLimit() @@ -248,7 +248,7 @@ func (s *MWTestSuite) TestTxHandlerAccountNumbersAtBlockHeightZero() { txBuilder := s.clientCtx.TxConfig.NewTxBuilder() // Same data for every test cases - accounts := s.createTestAccounts(ctx, 2) + accounts := s.createTestAccounts(ctx, 2, sdk.Coins{sdk.NewInt64Coin("atom", 10000000)}) feeAmount := testdata.NewTestFeeAmount() gasLimit := testdata.NewTestGasLimit() @@ -331,7 +331,7 @@ func (s *MWTestSuite) TestTxHandlerSequences() { txBuilder := s.clientCtx.TxConfig.NewTxBuilder() // Same data for every test cases - accounts := s.createTestAccounts(ctx, 3) + accounts := s.createTestAccounts(ctx, 3, sdk.Coins{sdk.NewInt64Coin("atom", 10000000)}) feeAmount := testdata.NewTestFeeAmount() gasLimit := testdata.NewTestGasLimit() @@ -524,7 +524,7 @@ func (s *MWTestSuite) TestTxHandlerMemoGas() { txBuilder := s.clientCtx.TxConfig.NewTxBuilder() // Same data for every test cases - accounts := s.createTestAccounts(ctx, 1) + accounts := s.createTestAccounts(ctx, 1, sdk.Coins{sdk.NewInt64Coin("atom", 10000000)}) msgs := []sdk.Msg{testdata.NewTestMsg(accounts[0].acc.GetAddress())} privs, accNums, accSeqs := []cryptotypes.PrivKey{accounts[0].priv}, []uint64{0}, []uint64{0} @@ -594,7 +594,7 @@ func (s *MWTestSuite) TestTxHandlerMultiSigner() { txBuilder := s.clientCtx.TxConfig.NewTxBuilder() // Same data for every test cases - accounts := s.createTestAccounts(ctx, 3) + accounts := s.createTestAccounts(ctx, 3, sdk.Coins{sdk.NewInt64Coin("atom", 10000000)}) msg1 := testdata.NewTestMsg(accounts[0].acc.GetAddress(), accounts[1].acc.GetAddress()) msg2 := testdata.NewTestMsg(accounts[2].acc.GetAddress(), accounts[0].acc.GetAddress()) msg3 := testdata.NewTestMsg(accounts[1].acc.GetAddress(), accounts[2].acc.GetAddress()) @@ -667,7 +667,7 @@ func (s *MWTestSuite) TestTxHandlerBadSignBytes() { txBuilder := s.clientCtx.TxConfig.NewTxBuilder() // Same data for every test cases - accounts := s.createTestAccounts(ctx, 2) + accounts := s.createTestAccounts(ctx, 2, sdk.Coins{sdk.NewInt64Coin("atom", 10000000)}) msg0 := testdata.NewTestMsg(accounts[0].acc.GetAddress()) // Variable data per test case @@ -793,7 +793,7 @@ func (s *MWTestSuite) TestTxHandlerSetPubKey() { txBuilder := s.clientCtx.TxConfig.NewTxBuilder() // Same data for every test cases - accounts := s.createTestAccounts(ctx, 2) + accounts := s.createTestAccounts(ctx, 2, sdk.Coins{sdk.NewInt64Coin("atom", 10000000)}) feeAmount := testdata.NewTestFeeAmount() gasLimit := testdata.NewTestGasLimit() @@ -971,7 +971,7 @@ func (s *MWTestSuite) TestTxHandlerSigLimitExceeded() { txBuilder := s.clientCtx.TxConfig.NewTxBuilder() // Same data for every test cases - accounts := s.createTestAccounts(ctx, 8) + accounts := s.createTestAccounts(ctx, 8, sdk.Coins{sdk.NewInt64Coin("atom", 10000000)}) var addrs []sdk.AccAddress var privs []cryptotypes.PrivKey for i := 0; i < 8; i++ { @@ -1029,7 +1029,7 @@ func (s *MWTestSuite) TestCustomSignatureVerificationGasConsumer() { s.Require().NoError(err) // Same data for every test cases - accounts := s.createTestAccounts(ctx, 1) + accounts := s.createTestAccounts(ctx, 1, sdk.Coins{sdk.NewInt64Coin("atom", 10000000)}) txBuilder.SetFeeAmount(testdata.NewTestFeeAmount()) txBuilder.SetGasLimit(testdata.NewTestGasLimit()) txBuilder.SetMsgs(testdata.NewTestMsg(accounts[0].acc.GetAddress())) @@ -1073,7 +1073,7 @@ func (s *MWTestSuite) TestTxHandlerReCheck() { txBuilder := s.clientCtx.TxConfig.NewTxBuilder() // Same data for every test cases - accounts := s.createTestAccounts(ctx, 1) + accounts := s.createTestAccounts(ctx, 1, sdk.Coins{sdk.NewInt64Coin("atom", 10000000)}) feeAmount := testdata.NewTestFeeAmount() gasLimit := testdata.NewTestGasLimit() diff --git a/x/auth/middleware/testutil_test.go b/x/auth/middleware/testutil_test.go index 11cfaf72c37..845c3d9e310 100644 --- a/x/auth/middleware/testutil_test.go +++ b/x/auth/middleware/testutil_test.go @@ -89,7 +89,7 @@ func (s *MWTestSuite) SetupTest(isCheckTx bool) sdk.Context { // createTestAccounts creates `numAccs` accounts, and return all relevant // information about them including their private keys. -func (s *MWTestSuite) createTestAccounts(ctx sdk.Context, numAccs int) []testAccount { +func (s *MWTestSuite) createTestAccounts(ctx sdk.Context, numAccs int, coins sdk.Coins) []testAccount { var accounts []testAccount for i := 0; i < numAccs; i++ { @@ -98,13 +98,11 @@ func (s *MWTestSuite) createTestAccounts(ctx sdk.Context, numAccs int) []testAcc err := acc.SetAccountNumber(uint64(i)) s.Require().NoError(err) s.app.AccountKeeper.SetAccount(ctx, acc) - someCoins := sdk.Coins{ - sdk.NewInt64Coin("atom", 10000000), - } - err = s.app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, someCoins) + + err = s.app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, coins) s.Require().NoError(err) - err = s.app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, someCoins) + err = s.app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, coins) s.Require().NoError(err) accounts = append(accounts, testAccount{acc, priv}) diff --git a/x/auth/middleware/tips_test.go b/x/auth/middleware/tips_test.go index 4ebe3ef9dc6..b7c9bd2867a 100644 --- a/x/auth/middleware/tips_test.go +++ b/x/auth/middleware/tips_test.go @@ -2,14 +2,81 @@ package middleware_test import ( "testing" + + "github.com/cosmos/cosmos-sdk/client" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/tx" + minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" + "github.com/stretchr/testify/suite" ) func (s *MWTestSuite) TestMetaTransactions() { - s.SetupTest(false) // reset + ctx := s.SetupTest(false) // reset + + // Setup 2 accounts: + // - tipper has 1000 regens + // - feePayer has 1000 atoms and 1000 regens + regens := sdk.NewCoins(sdk.NewCoin("regen", sdk.NewInt(1000))) + atoms := sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(1000))) + accts := s.createTestAccounts(ctx, 1, regens) + tipper, feePayer := accts[0], accts[1] + err := s.app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(1000)))) + s.Require().NoError(err) + err = s.app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, feePayer.acc.GetAddress(), atoms) + s.Require().NoError(err) + + testcases := []struct { + name string + tip sdk.Coins + fee sdk.Coins + gasLimit uint64 + expErr bool + expErrStr string + }{ + { + name: "", + tip: sdk.NewCoins(sdk.NewCoin("regen", sdk.NewInt(1000))), + fee: sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(1000))), + }, + } + for _, tc := range testcases { + s.Run(tc.name, func() { + tipperTxBuilder := s.mkTipperTxBuilder(tipper.acc.String(), tc.tip) + feePayerTxBuilder := s.mkFeePayerTxBuilder(feePayer.acc.String(), tx.Fee{Amount: tc.fee, GasLimit: tc.gasLimit}, tipperTxBuilder) + tx, _, err := s.createTestTx(feePayerTxBuilder, []cryptotypes.PrivKey{feePayer.priv}, []uint64{1}, []uint64{0}, ctx.ChainID()) + s.Require().NoError(err) + _, res, err := s.app.SimDeliver(s.clientCtx.TxConfig.TxEncoder(), tx) + if tc.expErr { + s.Require().EqualError(err, tc.expErrStr) + } else { + s.Require().NoError(err) + s.Require().NotNil(res) + } + }) + } +} + +func (s *MWTestSuite) mkTipperTxBuilder(tipper string, tip sdk.Coins) client.TxBuilder { + txBuilder := s.clientCtx.TxConfig.NewTxBuilder() + txBuilder.SetTip(&tx.Tip{ + Amount: tip, + Tipper: tipper, + }) + txBuilder.SetMsgs() + + return txBuilder +} + +func (s *MWTestSuite) mkFeePayerTxBuilder(feePayer string, fee tx.Fee, tipperTxBuilder client.TxBuilder) client.TxBuilder { + txBuilder := s.clientCtx.TxConfig.NewTxBuilder() + txBuilder.SetMsgs(tipperTxBuilder.GetTx().GetMsgs()...) + txBuilder.SetFeeAmount(fee.Amount) + txBuilder.SetGasLimit(fee.GasLimit) - tipper := s.createTestAccounts(1, "regen")[0] + return txBuilder } func TestMWTestSuite2(t *testing.T) { - s.Run(t, new(MWTestSuite)) + suite.Run(t, new(MWTestSuite)) }