github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/auth/ante/fee_test.go (about)

     1  package ante_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/ante"
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types"
    12  )
    13  
    14  func TestEnsureMempoolFees(t *testing.T) {
    15  	// setup
    16  	_, ctx := createTestApp(true)
    17  
    18  	mfd := ante.NewMempoolFeeDecorator()
    19  	antehandler := sdk.ChainAnteDecorators(mfd)
    20  
    21  	// keys and addresses
    22  	priv1, _, addr1 := types.KeyTestPubAddr()
    23  
    24  	// msg and signatures
    25  	msg1 := types.NewTestMsg(addr1)
    26  	fee := types.NewTestStdFee()
    27  
    28  	msgs := []sdk.Msg{msg1}
    29  
    30  	privs, accNums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0}
    31  	tx := types.NewTestTx(ctx, msgs, privs, accNums, seqs, fee)
    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  	ctx.SetMinGasPrices(highGasPrice)
    37  
    38  	// Set IsCheckTx to true
    39  	ctx.SetIsCheckTx(true)
    40  
    41  	// antehandler errors with insufficient fees
    42  	_, err := antehandler(ctx, tx, false)
    43  	require.NotNil(t, err, "Decorator should have errored on too low fee for local gasPrice")
    44  
    45  	// Set IsCheckTx to false
    46  	ctx.SetIsCheckTx(false)
    47  
    48  	// antehandler should not error since we do not check minGasPrice in DeliverTx
    49  	_, err = antehandler(ctx, tx, false)
    50  	require.Nil(t, err, "MempoolFeeDecorator returned error in DeliverTx")
    51  
    52  	// Set IsCheckTx back to true for testing sufficient mempool fee
    53  	ctx.SetIsCheckTx(true)
    54  
    55  	atomPrice = sdk.NewDecCoinFromDec("atom", sdk.NewDec(0).Quo(sdk.NewDec(100000)))
    56  	lowGasPrice := []sdk.DecCoin{atomPrice}
    57  	ctx.SetMinGasPrices(lowGasPrice)
    58  
    59  	_, err = antehandler(ctx, tx, false)
    60  	require.Nil(t, err, "Decorator should not have errored on fee higher than local gasPrice")
    61  }
    62  
    63  func TestDeductFees(t *testing.T) {
    64  	// setup
    65  	app, ctx := createTestApp(true)
    66  
    67  	// keys and addresses
    68  	priv1, _, addr1 := types.KeyTestPubAddr()
    69  
    70  	// msg and signatures
    71  	msg1 := types.NewTestMsg(addr1)
    72  	fee := types.NewTestStdFee()
    73  
    74  	msgs := []sdk.Msg{msg1}
    75  
    76  	privs, accNums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0}
    77  	tx := types.NewTestTx(ctx, msgs, privs, accNums, seqs, fee)
    78  
    79  	// Set account with insufficient funds
    80  	acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
    81  	acc.SetCoins([]sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(10))})
    82  	app.AccountKeeper.SetAccount(ctx, acc)
    83  
    84  	dfd := ante.NewDeductFeeDecorator(app.AccountKeeper, app.SupplyKeeper)
    85  	antehandler := sdk.ChainAnteDecorators(dfd)
    86  
    87  	_, err := antehandler(ctx, tx, false)
    88  
    89  	require.NotNil(t, err, "Tx did not error when fee payer had insufficient funds")
    90  
    91  	// Set account with sufficient funds
    92  	acc.SetCoins([]sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(200))})
    93  	app.AccountKeeper.SetAccount(ctx, acc)
    94  
    95  	_, err = antehandler(ctx, tx, false)
    96  
    97  	require.Nil(t, err, "Tx errored after account has been set with sufficient funds")
    98  }