Skip to content

Commit

Permalink
test: improve send ethereum tx (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Jul 31, 2024
1 parent a50e131 commit a2cb4e9
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 730 deletions.
16 changes: 4 additions & 12 deletions x/crosschain/precompile/add_pending_pool_rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,7 @@ func (suite *PrecompileTestSuite) TestAddPendingPoolRewards() {
})
suite.Require().NoError(err)

tx, err := suite.PackEthereumTx(signer, crosschaintypes.GetAddress(), big.NewInt(0), crossChainPack)
suite.Require().NoError(err)
res, err := suite.app.EvmKeeper.EthereumTx(sdk.WrapSDKContext(suite.ctx), tx)
suite.Require().NoError(err)
res := suite.EthereumTx(signer, crosschaintypes.GetAddress(), big.NewInt(0), crossChainPack)
suite.Require().False(res.Failed(), res.VmError)

pendingTxBefore, found := suite.CrossChainKeepers()[moduleName].GetPendingPoolTxById(suite.ctx, 1)
Expand All @@ -171,13 +168,9 @@ func (suite *PrecompileTestSuite) TestAddPendingPoolRewards() {
packData, err := precompile.NewAddPendingPoolRewardsMethod(nil).PackInput(addPendingPoolRewards)
suite.Require().NoError(err)

tx, err = suite.PackEthereumTx(signer, crosschaintypes.GetAddress(), big.NewInt(0), packData)
if err == nil {
res, err = suite.app.EvmKeeper.EthereumTx(sdk.WrapSDKContext(suite.ctx), tx)
}
// check result
res = suite.EthereumTx(signer, crosschaintypes.GetAddress(), big.NewInt(0), packData)

if tc.result {
suite.Require().NoError(err)
suite.Require().False(res.Failed(), res.VmError)

pendingTxAfter, found := suite.CrossChainKeepers()[moduleName].GetPendingPoolTxById(suite.ctx, 1)
Expand All @@ -186,8 +179,7 @@ func (suite *PrecompileTestSuite) TestAddPendingPoolRewards() {
suite.Require().Equal(sdk.NewCoin(fxtypes.DefaultDenom, sdk.NewIntFromBigInt(addRewardFee)).String(),
sdk.NewCoins(pendingTxAfter.Rewards...).Sub(sdk.NewCoins(pendingTxBefore.Rewards...)...).String())
} else {
suite.Require().Error(err)
suite.Require().EqualError(err, errResult.Error())
suite.Error(res, errResult)
}
})
}
Expand Down
21 changes: 6 additions & 15 deletions x/crosschain/precompile/cancel_bridge_call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
tmrand "github.com/cometbft/cometbft/libs/rand"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/stretchr/testify/require"

"github.com/functionx/fx-core/v7/testutil/helpers"
Expand Down Expand Up @@ -116,32 +115,24 @@ func (suite *PrecompileTestSuite) TestCancelPendingBridgeCall() {
}
bridgeCallPack, err := precompile.NewBridgeCallMethod(nil).PackInput(args)
suite.Require().NoError(err)
tx, err := suite.PackEthereumTx(signer, crosschaintypes.GetAddress(), big.NewInt(0), bridgeCallPack)
suite.Require().NoError(err)
bridgeCallRes, err := suite.app.EvmKeeper.EthereumTx(sdk.WrapSDKContext(suite.ctx), tx)
suite.Require().NoError(err)
suite.Require().False(bridgeCallRes.Failed(), bridgeCallRes.VmError)
res := suite.EthereumTx(signer, crosschaintypes.GetAddress(), big.NewInt(0), bridgeCallPack)
suite.Require().False(res.Failed(), res.VmError)

balanceBefore := suite.BalanceOf(pair.GetERC20Contract(), signer.Address())

cancelArgs, resultErr := tc.malleate(moduleName)
cancelArgs, errResult := tc.malleate(moduleName)
packData, err := precompile.NewCancelPendingBridgeCallMethod(nil).PackInput(cancelArgs)
suite.Require().NoError(err)

tx, err = suite.PackEthereumTx(signer, crosschaintypes.GetAddress(), big.NewInt(0), packData)
var res *evmtypes.MsgEthereumTxResponse
if err == nil {
res, err = suite.app.EvmKeeper.EthereumTx(sdk.WrapSDKContext(suite.ctx), tx)
}
res = suite.EthereumTx(signer, crosschaintypes.GetAddress(), big.NewInt(0), packData)

if tc.result {
suite.Require().NoError(err)
suite.Require().False(res.Failed(), res.VmError)

balanceAfter := suite.BalanceOf(pair.GetERC20Contract(), signer.Address())
suite.Equal(big.NewInt(0).Add(balanceBefore, amount).String(), balanceAfter.String())
} else {
suite.Require().Error(err)
suite.Require().EqualError(err, resultErr.Error())
suite.Error(res, errResult)
}
})
}
Expand Down
30 changes: 8 additions & 22 deletions x/crosschain/precompile/cancel_send_to_external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package precompile_test

import (
"encoding/hex"
"errors"
"fmt"
"math/big"
"strings"
Expand All @@ -12,7 +13,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/ethereum/go-ethereum/common"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/stretchr/testify/require"

"github.com/functionx/fx-core/v7/contract"
Expand Down Expand Up @@ -49,10 +49,7 @@ func (suite *PrecompileTestSuite) TestCancelSendToExternal() {
)
suite.Require().NoError(err)

tx, err := suite.PackEthereumTx(signer, crosschaintypes.GetAddress(), value, data)
suite.Require().NoError(err)
res, err := suite.app.EvmKeeper.EthereumTx(sdk.WrapSDKContext(suite.ctx), tx)
suite.Require().NoError(err)
res := suite.EthereumTx(signer, crosschaintypes.GetAddress(), value, data)
suite.Require().False(res.Failed(), res.VmError)
}
transferCrossChainTxFunc := func(signer *helpers.Signer, contact common.Address, moduleName string, amount, fee, value *big.Int) {
Expand All @@ -64,10 +61,7 @@ func (suite *PrecompileTestSuite) TestCancelSendToExternal() {
fxtypes.MustStrToByte32(moduleName),
)
suite.Require().NoError(err)
tx, err := suite.PackEthereumTx(signer, contact, value, data)
suite.Require().NoError(err)
res, err := suite.app.EvmKeeper.EthereumTx(sdk.WrapSDKContext(suite.ctx), tx)
suite.Require().NoError(err)
res := suite.EthereumTx(signer, contact, value, data)
suite.Require().False(res.Failed(), res.VmError)
}
refundPackFunc := func(moduleName string, md Metadata, signer *helpers.Signer, randMint *big.Int) ([]byte, []string) {
Expand Down Expand Up @@ -384,14 +378,9 @@ func (suite *PrecompileTestSuite) TestCancelSendToExternal() {
totalBefore, err := suite.app.BankKeeper.TotalSupply(suite.ctx, &banktypes.QueryTotalSupplyRequest{})
suite.Require().NoError(err)

tx, err := suite.PackEthereumTx(signer, crosschaintypes.GetAddress(), big.NewInt(0), packData)
var res *evmtypes.MsgEthereumTxResponse
if err == nil {
res, err = suite.app.EvmKeeper.EthereumTx(sdk.WrapSDKContext(suite.ctx), tx)
}
// check result
res := suite.EthereumTx(signer, crosschaintypes.GetAddress(), big.NewInt(0), packData)

if tc.result {
suite.Require().NoError(err)
suite.Require().False(res.Failed(), res.VmError)
// check balance after tx
chainBalances := suite.app.BankKeeper.GetAllBalances(suite.ctx, signer.AccAddress())
Expand Down Expand Up @@ -458,8 +447,7 @@ func (suite *PrecompileTestSuite) TestCancelSendToExternal() {
}

} else {
suite.Require().Error(err)
suite.Require().EqualError(err, tc.error(errArgs))
suite.Error(res, errors.New(tc.error(errArgs)))
}
})
}
Expand Down Expand Up @@ -491,10 +479,8 @@ func (suite *PrecompileTestSuite) TestDeleteOutgoingTransferRelation() {
data, err := crosschaintypes.GetABI().Pack("crossChain", pair.GetERC20Contract(),
helpers.GenExternalAddr(moduleName), amount, fee, fxtypes.MustStrToByte32(moduleName), "")
suite.Require().NoError(err)
tx, err := suite.PackEthereumTx(signer, crosschaintypes.GetAddress(), big.NewInt(0), data)
suite.Require().NoError(err)
res, err := suite.app.EvmKeeper.EthereumTx(sdk.WrapSDKContext(suite.ctx), tx)
suite.Require().NoError(err)

res := suite.EthereumTx(signer, crosschaintypes.GetAddress(), big.NewInt(0), data)
suite.Require().False(res.Failed(), res.VmError)

// get crosschain pending tx
Expand Down
62 changes: 28 additions & 34 deletions x/crosschain/precompile/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ import (
ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
localhost "github.com/cosmos/ibc-go/v7/modules/light-clients/09-localhost"
ibctesting "github.com/cosmos/ibc-go/v7/testing"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/evmos/ethermint/crypto/ethsecp256k1"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/stretchr/testify/require"
Expand All @@ -47,7 +49,6 @@ import (
crosschaintypes "github.com/functionx/fx-core/v7/x/crosschain/types"
"github.com/functionx/fx-core/v7/x/erc20/types"
crossethtypes "github.com/functionx/fx-core/v7/x/eth/types"
fxevmtypes "github.com/functionx/fx-core/v7/x/evm/types"
tronkeeper "github.com/functionx/fx-core/v7/x/tron/keeper"
trontypes "github.com/functionx/fx-core/v7/x/tron/types"
)
Expand Down Expand Up @@ -95,51 +96,26 @@ func (suite *PrecompileTestSuite) SetupSubTest() {
suite.SetupTest()
}

func (suite *PrecompileTestSuite) PackEthereumTx(signer *helpers.Signer, to common.Address, amount *big.Int, data []byte) (*evmtypes.MsgEthereumTx, error) {
fromAddr := signer.Address()
value := hexutil.Big(*amount)
args, err := json.Marshal(&evmtypes.TransactionArgs{To: &to, From: &fromAddr, Data: (*hexutil.Bytes)(&data), Value: &value})
suite.Require().NoError(err)

queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
evmtypes.RegisterQueryServer(queryHelper, suite.app.EvmKeeper)
res, err := evmtypes.NewQueryClient(queryHelper).EstimateGas(sdk.WrapSDKContext(suite.ctx),
&evmtypes.EthCallRequest{
Args: args,
GasCap: contract.DefaultGasCap,
ChainId: suite.app.EvmKeeper.ChainID().Int64(),
},
)
if err != nil {
return nil, err
}

if len(res.VmError) > 0 {
if len(res.Ret) > 4 {
retError, err := fxevmtypes.UnpackRetError(res.Ret[4:])
if err != nil {
return nil, err
}
return nil, fmt.Errorf("%s: %s", res.VmError, retError)
}
return nil, fmt.Errorf(res.VmError)
}

func (suite *PrecompileTestSuite) EthereumTx(signer *helpers.Signer, to common.Address, amount *big.Int, data []byte) *evmtypes.MsgEthereumTxResponse {
ethTx := evmtypes.NewTx(
fxtypes.EIP155ChainID(),
suite.app.EvmKeeper.GetNonce(suite.ctx, signer.Address()),
&to,
amount,
res.Gas,
contract.DefaultGasCap,
nil,
nil,
nil,
data,
nil,
)
ethTx.From = signer.Address().Bytes()
err = ethTx.Sign(ethtypes.LatestSignerForChainID(fxtypes.EIP155ChainID()), signer)
return ethTx, err
err := ethTx.Sign(ethtypes.LatestSignerForChainID(fxtypes.EIP155ChainID()), signer)
suite.Require().NoError(err)

res, err := suite.app.EvmKeeper.EthereumTx(sdk.WrapSDKContext(suite.ctx), ethTx)
suite.Require().NoError(err)
return res
}

func (suite *PrecompileTestSuite) Commit() {
Expand Down Expand Up @@ -168,6 +144,24 @@ func (suite *PrecompileTestSuite) RandSigner() *helpers.Signer {
return signer
}

func (suite *PrecompileTestSuite) Error(res *evmtypes.MsgEthereumTxResponse, errResult error) {
suite.Require().True(res.Failed())
if res.VmError != vm.ErrExecutionReverted.Error() {
suite.Require().Equal(errResult.Error(), res.VmError)
return
}

if len(res.Ret) > 0 {
reason, err := abi.UnpackRevert(common.CopyBytes(res.Ret))
suite.Require().NoError(err)

suite.Require().Equal(errResult.Error(), reason)
return
}

suite.Require().Equal(errResult.Error(), vm.ErrExecutionReverted.Error())
}

func (suite *PrecompileTestSuite) MintFeeCollector(coins sdk.Coins) {
err := suite.app.BankKeeper.MintCoins(suite.ctx, types.ModuleName, coins)
suite.Require().NoError(err)
Expand Down
Loading

0 comments on commit a2cb4e9

Please sign in to comment.