Skip to content

Commit

Permalink
test(crosschain): simplified testing (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code committed Jul 25, 2024
1 parent 156326f commit f90e62c
Show file tree
Hide file tree
Showing 26 changed files with 348 additions and 426 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ lint-install:
lint: lint-install
echo "--> Running linter"
@golangci-lint run --build-tags=$(GO_BUILD) --out-format=tab
@if [ $$(find . -name '*.go' -type f | xargs grep 'nolint\|#nosec' | wc -l) -ne 46 ]; then \
@if [ $$(find . -name '*.go' -type f | xargs grep 'nolint\|#nosec' | wc -l) -ne 44 ]; then \
echo "--> increase or decrease nolint, please recheck them"; \
echo "--> list nolint: \`find . -name '*.go' -type f | xargs grep 'nolint\|#nosec'\`"; exit 1;\
fi
Expand Down Expand Up @@ -181,7 +181,6 @@ test-nightly:
mocks:
@go install go.uber.org/mock/mockgen@v0.4.0
mockgen -source=x/crosschain/types/expected_keepers.go -package mock -destination x/crosschain/mock/expected_keepers_mocks.go
mockgen -source=x/crosschain/precompile/expected_keepers.go -package mock -destination x/crosschain/precompile/mock/expected_keepers_mocks.go

.PHONY: test test-count test-nightly mocks

Expand Down
251 changes: 125 additions & 126 deletions x/crosschain/tests/abci_test.go → x/crosschain/keeper/abci_test.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package tests_test
package keeper_test

import (
sdkmath "cosmossdk.io/math"
tmrand "github.com/cometbft/cometbft/libs/rand"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"

"github.com/functionx/fx-core/v7/testutil/helpers"
"github.com/functionx/fx-core/v7/x/crosschain/types"
Expand Down Expand Up @@ -52,7 +51,7 @@ func (suite *KeeperTestSuite) TestLastPendingBatchRequestByAddr() {
DestAddress: helpers.GenHexAddress().Hex(),
}},
})
require.NoError(suite.T(), err)
suite.Require().NoError(err)
}

wrapSDKContext := sdk.WrapSDKContext(suite.ctx)
Expand All @@ -70,10 +69,10 @@ func (suite *KeeperTestSuite) TestLastPendingBatchRequestByAddr() {
&types.QueryLastPendingBatchRequestByAddrRequest{
BridgerAddress: testCase.BridgerAddress.String(),
})
require.NoError(suite.T(), err, testCase.Name)
require.NotNil(suite.T(), response, testCase.Name)
require.NotNil(suite.T(), response.Batch, testCase.Name)
require.EqualValues(suite.T(), testCase.ExpectStartHeight, response.Batch.Block, testCase.Name)
suite.Require().NoError(err, testCase.Name)
suite.Require().NotNil(response, testCase.Name)
suite.Require().NotNil(response.Batch, testCase.Name)
suite.Require().EqualValues(testCase.ExpectStartHeight, response.Batch.Block, testCase.Name)
}
}

Expand Down
6 changes: 3 additions & 3 deletions x/crosschain/keeper/bridge_call_in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
erc20types "github.com/functionx/fx-core/v7/x/erc20/types"
)

func (s *KeeperTestSuite) TestBridgeCallHandler() {
func (s *KeeperMockSuite) TestBridgeCallHandler() {
callEvmMock := func(msg *types.MsgBridgeCallClaim, sender common.Address, getTokenPairTimes int) {
s.crosschainKeeper.SetLastObservedBlockHeight(s.ctx, 1000, msg.BlockHeight-1)

Expand Down Expand Up @@ -138,7 +138,7 @@ func (s *KeeperTestSuite) TestBridgeCallHandler() {
}
}

func (s *KeeperTestSuite) Test_CoinsToBridgeCallTokens() {
func (s *KeeperMockSuite) Test_CoinsToBridgeCallTokens() {
input := sdk.Coins{
sdk.NewCoin(fxtypes.DefaultDenom, sdk.NewInt(1e18)),
sdk.NewCoin("aaa", sdk.NewInt(2e18)),
Expand All @@ -159,7 +159,7 @@ func (s *KeeperTestSuite) Test_CoinsToBridgeCallTokens() {
s.Require().EqualValues(expectAmount, amounts)
}

func (s *KeeperTestSuite) MockBridgeCallToken(erc20Tokens []types.ERC20Token) {
func (s *KeeperMockSuite) MockBridgeCallToken(erc20Tokens []types.ERC20Token) {
if len(erc20Tokens) == 0 {
return
}
Expand Down
8 changes: 4 additions & 4 deletions x/crosschain/keeper/bridge_call_out_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/functionx/fx-core/v7/x/crosschain/types"
)

func (s *KeeperTestSuite) TestKeeper_BridgeCallResultHandler() {
func (s *KeeperMockSuite) TestKeeper_BridgeCallResultHandler() {
tests := []struct {
name string
initData func(msg *types.MsgBridgeCallResultClaim)
Expand Down Expand Up @@ -61,7 +61,7 @@ func (s *KeeperTestSuite) TestKeeper_BridgeCallResultHandler() {
}
}

func (s *KeeperTestSuite) TestKeeper_BridgeCallCoinsToERC20Token() {
func (s *KeeperMockSuite) TestKeeper_BridgeCallCoinsToERC20Token() {
type Data struct {
sender sdk.AccAddress
coin sdk.Coin
Expand Down Expand Up @@ -151,7 +151,7 @@ func (s *KeeperTestSuite) TestKeeper_BridgeCallCoinsToERC20Token() {
}
}

func (s *KeeperTestSuite) TestKeeper_DeleteOutgoingBridgeCall() {
func (s *KeeperMockSuite) TestKeeper_DeleteOutgoingBridgeCall() {
outCall := &types.OutgoingBridgeCall{
Sender: helpers.GenHexAddress().String(),
Nonce: tmrand.Uint64(),
Expand All @@ -168,7 +168,7 @@ func (s *KeeperTestSuite) TestKeeper_DeleteOutgoingBridgeCall() {
s.Require().False(s.crosschainKeeper.HasOutgoingBridgeCallAddressAndNonce(s.ctx, outCall.Sender, outCall.Nonce))
}

func (s *KeeperTestSuite) TestKeeper_IteratorBridgeCallNotLiquidsByDenom() {
func (s *KeeperMockSuite) TestKeeper_IteratorBridgeCallNotLiquidsByDenom() {
expectCoins := sdk.NewCoins()
for i := 0; i < int(tmrand.Int63n(10))+1; i++ {
newCoin := sdk.NewCoin(types.NewBridgeDenom(s.moduleName, helpers.GenExternalAddr(s.moduleName)), sdkmath.NewInt(int64(tmrand.Uint32())))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tests_test
package keeper_test

import (
"encoding/hex"
Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/keeper/bridge_call_refund_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/functionx/fx-core/v7/x/crosschain/types"
)

func (s *KeeperTestSuite) TestKeeper_HandleOutgoingBridgeCallRefund() {
func (s *KeeperMockSuite) TestKeeper_HandleOutgoingBridgeCallRefund() {
tests := []struct {
name string
initData func(outgoingBridgeCall *types.OutgoingBridgeCall)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tests_test
package keeper_test

import (
"github.com/functionx/fx-core/v7/testutil/helpers"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package tests_test
package keeper_test

import (
"sort"
"testing"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"

"github.com/functionx/fx-core/v7/testutil/helpers"
"github.com/functionx/fx-core/v7/x/crosschain/keeper"
Expand All @@ -26,17 +24,17 @@ func (suite *KeeperTestSuite) TestBatchAndTxImportExport() {
}
bridgeTokens[i] = bridgeToken
denom, err := suite.Keeper().SetIbcDenomTrace(suite.ctx, bridgeToken.Token, "")
require.NoError(suite.T(), err)
require.Equal(suite.T(), denom, bridgeToken.Denom)
suite.Require().NoError(err)
suite.Require().Equal(denom, bridgeToken.Denom)
suite.Keeper().AddBridgeToken(suite.ctx, bridgeToken.Token, denom) // nolint:staticcheck

for _, bridger := range suite.bridgerAddrs {
voucher := sdk.NewCoin(bridgeToken.Denom, sdkmath.NewInt(9990))
err := suite.app.BankKeeper.MintCoins(suite.ctx, suite.chainName, sdk.NewCoins(voucher))
require.NoError(suite.T(), err)
suite.Require().NoError(err)

err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, suite.chainName, bridger, sdk.NewCoins(voucher))
require.NoError(suite.T(), err)
suite.Require().NoError(err)
}
}

Expand All @@ -47,7 +45,7 @@ func (suite *KeeperTestSuite) TestBatchAndTxImportExport() {
fees := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
amounts := []int{51, 52, 53, 54, 55, 56, 57, 58, 59, 60}
for i := 0; i < numTxs; i++ {
// Pick fee, amount, sender, receiver, and contract for the ith transaction
// Pick fee, amoun sender, receiver, and contract for the ith transaction
// Sender and contract will always match up (they must since sender i controls the whole balance of the ith token)
// Receivers should get a balance of many token types since i % len(receivers) is usually different than i % len(contracts)
fee := fees[i%len(fees)] // fee for this transaction
Expand Down Expand Up @@ -77,7 +75,7 @@ func (suite *KeeperTestSuite) TestBatchAndTxImportExport() {
// CREATE BATCHES
// ==================
// Want to create batches for half of the transactions for each contract
// with 100 tx in each batch, 1000 txs per contract, we want 5 batches per contract to batch 500 txs per contract
// with 100 tx in each batch, 1000 txs per contrac we want 5 batches per contract to batch 500 txs per contract
for i, bridgeToken := range bridgeTokens {
suite.ctx = suite.ctx.WithBlockHeight(int64(50 + i))
batch, err := suite.Keeper().BuildOutgoingTxBatch(suite.ctx, bridgeToken.Token, bridgeToken.Token, 100, sdkmath.NewInt(1), sdkmath.NewInt(1))
Expand All @@ -95,7 +93,7 @@ func (suite *KeeperTestSuite) TestBatchAndTxImportExport() {
}

// export
checkAllTransactionsExist(suite.T(), suite.ctx, suite.Keeper(), txs)
suite.checkAllTransactionsExist(txs)
genesisState := keeper.ExportGenesis(suite.ctx, suite.Keeper())

// clear data
Expand All @@ -113,20 +111,20 @@ func (suite *KeeperTestSuite) TestBatchAndTxImportExport() {

// import
keeper.InitGenesis(suite.ctx, suite.Keeper(), genesisState)
checkAllTransactionsExist(suite.T(), suite.ctx, suite.Keeper(), txs)
suite.checkAllTransactionsExist(txs)
}

// Requires that all transactions in txs exist in keeper
func checkAllTransactionsExist(t *testing.T, ctx sdk.Context, keeper keeper.Keeper, txs types.OutgoingTransferTxs) {
unbatched := keeper.GetUnbatchedTransactions(ctx)
batches := keeper.GetOutgoingTxBatches(ctx)
func (suite *KeeperTestSuite) checkAllTransactionsExist(txs types.OutgoingTransferTxs) {
unbatched := suite.Keeper().GetUnbatchedTransactions(suite.ctx)
batches := suite.Keeper().GetOutgoingTxBatches(suite.ctx)
// Collect all txs into an array
var gotTxs types.OutgoingTransferTxs
gotTxs = append(gotTxs, unbatched...)
for _, batch := range batches {
gotTxs = append(gotTxs, batch.Transactions...)
}
require.Equal(t, len(txs), len(gotTxs))
suite.Require().Equal(len(txs), len(gotTxs))
// Sort both arrays for simple searching
sort.Slice(gotTxs, func(i, j int) bool {
return gotTxs[i].Id < gotTxs[j].Id
Expand All @@ -136,10 +134,10 @@ func checkAllTransactionsExist(t *testing.T, ctx sdk.Context, keeper keeper.Keep
})
// Actually check that the txs all exist, iterate on txs in case some got lost in the import/export step
for i, exp := range txs {
require.Equal(t, exp.Id, gotTxs[i].Id)
require.Equal(t, exp.Fee.String(), gotTxs[i].Fee.String())
require.Equal(t, exp.Token.String(), gotTxs[i].Token.String())
require.Equal(t, exp.DestAddress, gotTxs[i].DestAddress)
require.Equal(t, exp.Sender, gotTxs[i].Sender)
suite.Require().Equal(exp.Id, gotTxs[i].Id)
suite.Require().Equal(exp.Fee.String(), gotTxs[i].Fee.String())
suite.Require().Equal(exp.Token.String(), gotTxs[i].Token.String())
suite.Require().Equal(exp.DestAddress, gotTxs[i].DestAddress)
suite.Require().Equal(exp.Sender, gotTxs[i].Sender)
}
}
4 changes: 2 additions & 2 deletions x/crosschain/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/functionx/fx-core/v7/x/crosschain/types"
)

func (s *KeeperTestSuite) TestQueryServer_BridgeCalls() {
func (s *KeeperMockSuite) TestQueryServer_BridgeCalls() {
ctx := sdk.WrapSDKContext(s.ctx)
data1 := types.OutgoingBridgeCall{
Nonce: tmrand.Uint64(),
Expand Down Expand Up @@ -45,7 +45,7 @@ func (s *KeeperTestSuite) TestQueryServer_BridgeCalls() {
s.Equal(len(actual.BridgeCalls), 2)
}

func (s *KeeperTestSuite) TestQueryServer_PendingBridgeCalls() {
func (s *KeeperMockSuite) TestQueryServer_PendingBridgeCalls() {
ctx := sdk.WrapSDKContext(s.ctx)
data1 := types.PendingOutgoingBridgeCall{
OutgoinBridgeCall: &types.OutgoingBridgeCall{
Expand Down
Loading

0 comments on commit f90e62c

Please sign in to comment.