From 8c5d0a128f523abe994333ba74c88ee285274414 Mon Sep 17 00:00:00 2001 From: trung2891 Date: Wed, 22 Nov 2023 01:14:10 +0700 Subject: [PATCH] feeburn test --- x/feeburn/ante/antetest/fee_test.go | 44 +++++++++++++++++++++ x/feeburn/ante/antetest/fee_test_setup.go | 9 +---- x/feeburn/ante/fee.go | 2 + x/feeburn/types/params_test.go | 48 +++++++++++++++++++++++ 4 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 x/feeburn/ante/antetest/fee_test.go create mode 100644 x/feeburn/types/params_test.go diff --git a/x/feeburn/ante/antetest/fee_test.go b/x/feeburn/ante/antetest/fee_test.go new file mode 100644 index 00000000..a545906f --- /dev/null +++ b/x/feeburn/ante/antetest/fee_test.go @@ -0,0 +1,44 @@ +package antetest + +import ( + "testing" + + feeburnante "github.com/White-Whale-Defi-Platform/migaloo-chain/v3/x/feeburn/ante" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/testutil/testdata" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/bank/testutil" + "github.com/stretchr/testify/suite" +) + +func TestIntegrationTestSuite(t *testing.T) { + suite.Run(t, new(IntegrationTestSuite)) +} + +func (s *IntegrationTestSuite) TestFeeBurnAnteHandler() { + // setup test + s.SetupTest() + s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder() + + // keys and addresses + priv1, _, addr1 := testdata.KeyTestPubAddr() + + // msg and signatures + msg := testdata.NewTestMsg(addr1) + feeAmount := testdata.NewTestFeeAmount() + gasLimit := testdata.NewTestGasLimit() + s.Require().NoError(s.txBuilder.SetMsgs(msg)) + s.txBuilder.SetFeeAmount(feeAmount) + s.txBuilder.SetGasLimit(gasLimit) + + privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0} + tx, err := s.CreateTestTx(privs, accNums, accSeqs, s.ctx.ChainID()) + s.Require().NoError(err) + + mfd := feeburnante.NewDeductFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, nil, s.app.FeeburnKeeper) + antehandler := sdk.ChainAnteDecorators(mfd) + err = testutil.FundAccount(s.app.BankKeeper, s.ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200)))) + s.Require().NoError(err) + _, err = antehandler(s.ctx, tx, false) + s.Require().Error(err) +} diff --git a/x/feeburn/ante/antetest/fee_test_setup.go b/x/feeburn/ante/antetest/fee_test_setup.go index 5ddf4c6f..fea7bb0b 100644 --- a/x/feeburn/ante/antetest/fee_test_setup.go +++ b/x/feeburn/ante/antetest/fee_test_setup.go @@ -11,7 +11,6 @@ import ( "github.com/cosmos/cosmos-sdk/types/tx/signing" xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" "github.com/cosmos/cosmos-sdk/x/params/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/stretchr/testify/suite" tmrand "github.com/tendermint/tendermint/libs/rand" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -46,19 +45,13 @@ func (s *IntegrationTestSuite) SetupTest() { s.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig) } -func (s *IntegrationTestSuite) SetupTestBurnFeeStore(minGasPrice []sdk.DecCoin, feeBurnParams *feeburnmoduletypes.Params) types.Subspace { +func (s *IntegrationTestSuite) SetupTestBurnFeeStore(feeBurnParams *feeburnmoduletypes.Params) types.Subspace { subspace := s.app.GetSubspace(feeburn.ModuleName) subspace.SetParamSet(s.ctx, feeBurnParams) return subspace } -// SetupTestStakingSubspace sets uatom as bond denom for the fee tests. -func (s *IntegrationTestSuite) SetupTestStakingSubspace(params stakingtypes.Params) types.Subspace { - s.app.GetSubspace(stakingtypes.ModuleName).SetParamSet(s.ctx, ¶ms) - return s.app.GetSubspace(stakingtypes.ModuleName) -} - func (s *IntegrationTestSuite) CreateTestTx(privs []cryptotypes.PrivKey, accNums []uint64, accSeqs []uint64, chainID string) (xauthsigning.Tx, error) { var sigsV2 []signing.SignatureV2 for i, priv := range privs { diff --git a/x/feeburn/ante/fee.go b/x/feeburn/ante/fee.go index 295faa3a..aca6b5a7 100644 --- a/x/feeburn/ante/fee.go +++ b/x/feeburn/ante/fee.go @@ -41,6 +41,7 @@ func NewDeductFeeDecorator(ak ante.AccountKeeper, bk BankKeeper, fk ante.Feegran func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { feeTx, ok := tx.(sdk.FeeTx) + if !ok { return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx") } @@ -72,6 +73,7 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo func (dfd DeductFeeDecorator) checkDeductFee(ctx sdk.Context, sdkTx sdk.Tx, fee sdk.Coins) error { feeTx, ok := sdkTx.(sdk.FeeTx) + if !ok { return errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx") } diff --git a/x/feeburn/types/params_test.go b/x/feeburn/types/params_test.go new file mode 100644 index 00000000..94c5e1a5 --- /dev/null +++ b/x/feeburn/types/params_test.go @@ -0,0 +1,48 @@ +package types + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestDefaultParams(t *testing.T) { + p := DefaultParams() + require.EqualValues(t, p.TxFeeBurnPercent, "0") +} + +func TestValidateParams(t *testing.T) { + tests := map[string]struct { + percent interface{} + expectErr bool + }{ + "DafaultParams, pass": { + DefaultParams().TxFeeBurnPercent, + false, + }, + "lower boundary testing, pass": { + "0", + false, + }, + "upper boundary testing, pass": { + "100", + false, + }, + "greater 100%, fail": { + "101", + true, + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + err := validateTxFeeBurnPercent(test.percent) + if test.expectErr { + require.Error(t, err) + return + } + require.NoError(t, err) + }) + } + +}