From 0e75a33996b54555df874f27f4ed6e3e45156efb Mon Sep 17 00:00:00 2001 From: atheesh Date: Wed, 20 Apr 2022 12:59:16 +0530 Subject: [PATCH] chore: add more tests for code cov in `middleware` --- x/auth/middleware/basic_test.go | 31 +++++++++++++++++++++++++++++ x/auth/middleware/fee_test.go | 16 +++++++++++++-- x/auth/middleware/gas_test.go | 3 +++ x/auth/middleware/run_msgs_test.go | 5 +++++ x/auth/middleware/sigverify_test.go | 5 +++++ x/auth/middleware/tx_test.go | 7 +++++++ 6 files changed, 65 insertions(+), 2 deletions(-) diff --git a/x/auth/middleware/basic_test.go b/x/auth/middleware/basic_test.go index 1c44e292eb9..7bc583ed8d7 100644 --- a/x/auth/middleware/basic_test.go +++ b/x/auth/middleware/basic_test.go @@ -33,22 +33,37 @@ func (s *MWTestSuite) TestValidateBasic() { invalidTx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID()) s.Require().NoError(err) + // DeliverTx _, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: invalidTx}) s.Require().NotNil(err, "Did not error on invalid tx") + // SimulateTx + _, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: invalidTx}) + s.Require().NotNil(err, "Did not error on invalid tx") + privs, accNums, accSeqs = []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0} validTx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID()) s.Require().NoError(err) + // DeliverTx _, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: validTx}) s.Require().Nil(err, "ValidateBasicMiddleware returned error on valid tx. err: %v", err) + // SimulateTx + _, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: validTx}) + s.Require().Nil(err, "ValidateBasicMiddleware returned error on valid tx. err: %v", err) + // test middleware skips on recheck ctx = ctx.WithIsReCheckTx(true) // middleware should skip processing invalidTx on recheck and thus return nil-error + // DeliverTx _, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: invalidTx}) s.Require().Nil(err, "ValidateBasicMiddleware ran on ReCheck") + + // SimulateTx + _, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: invalidTx}) + s.Require().Nil(err, "ValidateBasicMiddleware ran on ReCheck") } func (s *MWTestSuite) TestValidateMemo() { @@ -73,8 +88,12 @@ func (s *MWTestSuite) TestValidateMemo() { s.Require().NoError(err) // require that long memos get rejected + // DeliverTx _, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: invalidTx}) + s.Require().NotNil(err, "Did not error on tx with high memo") + // SimulateTx + _, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: invalidTx}) s.Require().NotNil(err, "Did not error on tx with high memo") txBuilder.SetMemo(strings.Repeat("01234567890", 10)) @@ -82,8 +101,13 @@ func (s *MWTestSuite) TestValidateMemo() { s.Require().NoError(err) // require small memos pass ValidateMemo middleware + // DeliverTx _, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: validTx}) s.Require().Nil(err, "ValidateBasicMiddleware returned error on valid tx. err: %v", err) + + // SimulateTx + _, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: validTx}) + s.Require().Nil(err, "ValidateBasicMiddleware returned error on valid tx. err: %v", err) } func (s *MWTestSuite) TestConsumeGasForTxSize() { @@ -214,8 +238,15 @@ func (s *MWTestSuite) TestTxHeightTimeoutMiddleware() { s.Require().NoError(err) ctx := ctx.WithBlockHeight(tc.height) + + // DeliverTx + _, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx}) + s.Require().Equal(tc.expectErr, err != nil, err) + + // SimulateTx _, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx}) s.Require().Equal(tc.expectErr, err != nil, err) + }) } } diff --git a/x/auth/middleware/fee_test.go b/x/auth/middleware/fee_test.go index 7c1f54463ff..9899e2aa63c 100644 --- a/x/auth/middleware/fee_test.go +++ b/x/auth/middleware/fee_test.go @@ -100,15 +100,27 @@ func (s *MWTestSuite) TestDeductFees() { err = testutil.FundAccount(s.app.BankKeeper, ctx, addr1, coins) s.Require().NoError(err) + // DeliverTx _, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx}) - s.Require().NotNil(err, "Tx did not error when fee payer had insufficient funds") + s.Require().NotNil(err, "Tx errored when fee payer had insufficient funds") + + // SimulateTx + _, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx}) + s.Require().NotNil(err, "Tx errored when fee payer had insufficient funds") // Set account with sufficient funds s.app.AccountKeeper.SetAccount(ctx, acc) err = testutil.FundAccount(s.app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200)))) s.Require().NoError(err) + // DeliverTx _, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx}) + s.Require().Nil(err, "Tx did not error after account has been set with sufficient funds") + + err = testutil.FundAccount(s.app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200)))) + s.Require().NoError(err) - s.Require().Nil(err, "Tx errored after account has been set with sufficient funds") + // SimulateTx + _, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx}) + s.Require().Nil(err, "Tx did not error after account has been set with sufficient funds") } diff --git a/x/auth/middleware/gas_test.go b/x/auth/middleware/gas_test.go index 78685fa6c76..48007776214 100644 --- a/x/auth/middleware/gas_test.go +++ b/x/auth/middleware/gas_test.go @@ -65,10 +65,13 @@ func (s *MWTestSuite) TestSetup() { for _, tc := range testcases { s.Run(tc.name, func() { res, _, err := txHandler.CheckTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: tc.tx}, tx.RequestCheckTx{}) + _, simErr := txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: tc.tx}) if tc.expErr { s.Require().EqualError(err, tc.errorStr) + s.Require().EqualError(simErr, tc.errorStr) } else { s.Require().Nil(err, "SetUpContextMiddleware returned error") + s.Require().Nil(simErr, "SetUpContextMiddleware returned error") s.Require().Equal(tc.expGasLimit, uint64(res.GasWanted)) } }) diff --git a/x/auth/middleware/run_msgs_test.go b/x/auth/middleware/run_msgs_test.go index 6278d5c023d..143a2197538 100644 --- a/x/auth/middleware/run_msgs_test.go +++ b/x/auth/middleware/run_msgs_test.go @@ -28,8 +28,13 @@ func (s *MWTestSuite) TestRunMsgs() { txBytes, err := s.clientCtx.TxConfig.TxEncoder()(testTx) s.Require().NoError(err) + // DeliverTx res, err := txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx, TxBytes: txBytes}) s.Require().NoError(err) s.Require().Len(res.MsgResponses, 1) s.Require().Equal(fmt.Sprintf("/%s", proto.MessageName(&testdata.MsgCreateDogResponse{})), res.MsgResponses[0].TypeUrl) + + // SimulateTx + _, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx, TxBytes: txBytes}) + s.Require().NoError(err) } diff --git a/x/auth/middleware/sigverify_test.go b/x/auth/middleware/sigverify_test.go index a0c68062a96..fd4fc562204 100644 --- a/x/auth/middleware/sigverify_test.go +++ b/x/auth/middleware/sigverify_test.go @@ -55,6 +55,7 @@ func (s *MWTestSuite) TestSetPubKey() { testTx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID()) require.NoError(err) + // DeliverTx _, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx}) require.NoError(err) @@ -65,6 +66,10 @@ func (s *MWTestSuite) TestSetPubKey() { require.True(pubs[i].Equals(pk), "Wrong Pubkey retrieved from AccountKeeper, idx=%d\nexpected=%s\n got=%s", i, pubs[i], pk) } + + // SimulateTx + _, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx}) + require.NoError(err) } func (s *MWTestSuite) TestConsumeSignatureVerificationGas() { diff --git a/x/auth/middleware/tx_test.go b/x/auth/middleware/tx_test.go index 8624aadbfea..9e4ce65ed64 100644 --- a/x/auth/middleware/tx_test.go +++ b/x/auth/middleware/tx_test.go @@ -46,11 +46,18 @@ func (s *MWTestSuite) TestTxDecoderMiddleware() { txReqChecker, middleware.NewTxDecoderMiddleware(s.clientCtx.TxConfig.TxDecoder()), ) + + // DeliverTx _, err := txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tc.req) + + // SimulateTx + _, simErr := txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tc.req) if tc.expErr { require.Error(err) + require.Error(simErr) } else { require.NoError(err) + require.NoError(simErr) } }) }