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