github.com/Finschia/finschia-sdk@v0.49.1/x/auth/ante/fee_test.go (about)

     1  package ante_test
     2  
     3  import (
     4  	cryptotypes "github.com/Finschia/finschia-sdk/crypto/types"
     5  	"github.com/Finschia/finschia-sdk/simapp"
     6  	"github.com/Finschia/finschia-sdk/testutil/testdata"
     7  	sdk "github.com/Finschia/finschia-sdk/types"
     8  	"github.com/Finschia/finschia-sdk/x/auth/ante"
     9  )
    10  
    11  func (suite *AnteTestSuite) TestEnsureMempoolFees() {
    12  	suite.SetupTest(true) // setup
    13  	suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()
    14  
    15  	mfd := ante.NewMempoolFeeDecorator()
    16  	antehandler := sdk.ChainAnteDecorators(mfd)
    17  
    18  	// keys and addresses
    19  	priv1, _, addr1 := testdata.KeyTestPubAddr()
    20  
    21  	// msg and signatures
    22  	msg := testdata.NewTestMsg(addr1)
    23  	feeAmount := testdata.NewTestFeeAmount()
    24  	gasLimit := testdata.NewTestGasLimit()
    25  	suite.Require().NoError(suite.txBuilder.SetMsgs(msg))
    26  	suite.txBuilder.SetFeeAmount(feeAmount)
    27  	suite.txBuilder.SetGasLimit(gasLimit)
    28  
    29  	privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
    30  	tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID())
    31  	suite.Require().NoError(err)
    32  
    33  	// Set high gas price so standard test fee fails
    34  	atomPrice := sdk.NewDecCoinFromDec("atom", sdk.NewDec(200).Quo(sdk.NewDec(100000)))
    35  	highGasPrice := []sdk.DecCoin{atomPrice}
    36  	suite.ctx = suite.ctx.WithMinGasPrices(highGasPrice)
    37  
    38  	// Set IsCheckTx to true
    39  	suite.ctx = suite.ctx.WithIsCheckTx(true)
    40  
    41  	// antehandler errors with insufficient fees
    42  	_, err = antehandler(suite.ctx, tx, false)
    43  	suite.Require().NotNil(err, "Decorator should have errored on too low fee for local gasPrice")
    44  
    45  	// Set IsCheckTx to false
    46  	suite.ctx = suite.ctx.WithIsCheckTx(false)
    47  
    48  	// antehandler should not error since we do not check minGasPrice in DeliverTx
    49  	_, err = antehandler(suite.ctx, tx, false)
    50  	suite.Require().Nil(err, "MempoolFeeDecorator returned error in DeliverTx")
    51  
    52  	// Set IsCheckTx back to true for testing sufficient mempool fee
    53  	suite.ctx = suite.ctx.WithIsCheckTx(true)
    54  
    55  	atomPrice = sdk.NewDecCoinFromDec("atom", sdk.NewDec(0).Quo(sdk.NewDec(100000)))
    56  	lowGasPrice := []sdk.DecCoin{atomPrice}
    57  	suite.ctx = suite.ctx.WithMinGasPrices(lowGasPrice)
    58  
    59  	_, err = antehandler(suite.ctx, tx, false)
    60  	suite.Require().Nil(err, "Decorator should not have errored on fee higher than local gasPrice")
    61  }
    62  
    63  func (suite *AnteTestSuite) TestDeductFees() {
    64  	suite.SetupTest(false) // setup
    65  	suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()
    66  
    67  	// keys and addresses
    68  	priv1, _, addr1 := testdata.KeyTestPubAddr()
    69  
    70  	// msg and signatures
    71  	msg := testdata.NewTestMsg(addr1)
    72  	feeAmount := testdata.NewTestFeeAmount()
    73  	gasLimit := testdata.NewTestGasLimit()
    74  	suite.Require().NoError(suite.txBuilder.SetMsgs(msg))
    75  	suite.txBuilder.SetFeeAmount(feeAmount)
    76  	suite.txBuilder.SetGasLimit(gasLimit)
    77  
    78  	privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
    79  	tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID())
    80  	suite.Require().NoError(err)
    81  
    82  	// Set account with insufficient funds
    83  	acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr1)
    84  	suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
    85  	coins := sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(10)))
    86  	err = simapp.FundAccount(suite.app, suite.ctx, addr1, coins)
    87  	suite.Require().NoError(err)
    88  
    89  	dfd := ante.NewDeductFeeDecorator(suite.app.AccountKeeper, suite.app.BankKeeper, nil)
    90  	antehandler := sdk.ChainAnteDecorators(dfd)
    91  
    92  	_, err = antehandler(suite.ctx, tx, false)
    93  
    94  	suite.Require().NotNil(err, "Tx did not error when fee payer had insufficient funds")
    95  
    96  	// Set account with sufficient funds
    97  	suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
    98  	err = simapp.FundAccount(suite.app, suite.ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200))))
    99  	suite.Require().NoError(err)
   100  
   101  	_, err = antehandler(suite.ctx, tx, false)
   102  
   103  	suite.Require().Nil(err, "Tx errored after account has been set with sufficient funds")
   104  }