Skip to content

Commit

Permalink
test: refactor tests to use BaseSuite (#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Sep 24, 2024
1 parent c7c6a86 commit 95279c1
Show file tree
Hide file tree
Showing 13 changed files with 346 additions and 397 deletions.
38 changes: 0 additions & 38 deletions testutil/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
abci "github.com/cometbft/cometbft/abci/types"
tmed25519 "github.com/cometbft/cometbft/crypto/ed25519"
tmtypes "github.com/cometbft/cometbft/types"
tmtime "github.com/cometbft/cometbft/types/time"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
Expand Down Expand Up @@ -169,40 +168,3 @@ func AddTestAddr(myApp *app.App, ctx sdk.Context, addr sdk.AccAddress, coins sdk
panic(err)
}
}

// Deprecated: please use BaseSuite.Commit
func MintBlock(myApp *app.App, ctx sdk.Context, block ...int64) sdk.Context {
lastBlockHeight := ctx.BlockHeight()
nextHeight := lastBlockHeight + 1
if len(block) > 0 {
nextHeight = lastBlockHeight + block[0]
}
for i := lastBlockHeight; i < nextHeight; i++ {
// 1. try to finalize the block + commit finalizeBlockState
if _, err := myApp.FinalizeBlock(&abci.RequestFinalizeBlock{
Height: i,
Time: tmtime.Now(),
ProposerAddress: ctx.BlockHeader().ProposerAddress,
}); err != nil {
panic(err)
}

// 2. commit lastCommitInfo
if _, err := myApp.Commit(); err != nil {
panic(err)
}

// 3. prepare to process new blocks (myApp.GetContextForFinalizeBlock(nil))
if _, err := myApp.ProcessProposal(&abci.RequestProcessProposal{
Height: i + 1,
Time: tmtime.Now(),
ProposerAddress: ctx.BlockHeader().ProposerAddress,
}); err != nil {
panic(err)
}

// 4. get new ctx for finalizeBlockState
ctx = myApp.GetContextForFinalizeBlock(nil)
}
return ctx
}
6 changes: 4 additions & 2 deletions testutil/helpers/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ func (s *BaseSuite) SetupTest() {
}
valSet, valAccounts, valBalances := GenerateGenesisValidator(valNumber, sdk.Coins{})
s.ValSet = valSet
for _, account := range valAccounts {
s.ValAddr = append(s.ValAddr, account.GetAddress().Bytes())
s.ValAddr = make([]sdk.ValAddress, valNumber)
for i := 0; i < valNumber; i++ {
s.ValAddr[i] = valAccounts[i].GetAddress().Bytes()
}

s.App = SetupWithGenesisValSet(s.T(), valSet, valAccounts, valBalances...)
s.Ctx = s.App.GetContextForFinalizeBlock(nil)
s.Ctx = s.Ctx.WithProposer(s.ValSet.Proposer.Address.Bytes())
Expand Down
162 changes: 81 additions & 81 deletions x/crosschain/keeper/abci_test.go

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions x/crosschain/keeper/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func (suite *KeeperTestSuite) TestLastPendingBatchRequestByAddr() {
},
}
for i := uint64(1); i <= 3; i++ {
suite.ctx = suite.ctx.WithBlockHeight(int64(i))
err := suite.Keeper().StoreBatch(suite.ctx, &types.OutgoingTxBatch{
suite.Ctx = suite.Ctx.WithBlockHeight(int64(i))
err := suite.Keeper().StoreBatch(suite.Ctx, &types.OutgoingTxBatch{
Block: i,
BatchNonce: i,
Transactions: types.OutgoingTransferTxs{{
Expand All @@ -54,16 +54,16 @@ func (suite *KeeperTestSuite) TestLastPendingBatchRequestByAddr() {
suite.Require().NoError(err)
}

wrapSDKContext := suite.ctx
wrapSDKContext := suite.Ctx
for _, testCase := range testCases {
oracle := types.Oracle{
OracleAddress: testCase.OracleAddress.String(),
BridgerAddress: testCase.BridgerAddress.String(),
StartHeight: testCase.StartHeight,
}
// save oracle
suite.Keeper().SetOracle(suite.ctx, oracle)
suite.Keeper().SetOracleAddrByBridgerAddr(suite.ctx, testCase.BridgerAddress, oracle.GetOracle())
suite.Keeper().SetOracle(suite.Ctx, oracle)
suite.Keeper().SetOracleAddrByBridgerAddr(suite.Ctx, testCase.BridgerAddress, oracle.GetOracle())

response, err := suite.QueryClient().LastPendingBatchRequestByAddr(wrapSDKContext,
&types.QueryLastPendingBatchRequestByAddrRequest{
Expand Down Expand Up @@ -100,10 +100,10 @@ func (suite *KeeperTestSuite) TestKeeper_DeleteBatchConfig() {
Block: 100,
FeeReceive: helpers.GenHexAddress().Hex(),
}
suite.NoError(suite.Keeper().StoreBatch(suite.ctx, batch))
suite.NoError(suite.Keeper().StoreBatch(suite.Ctx, batch))

suite.Equal(uint64(0), suite.Keeper().GetLastSlashedBatchBlock(suite.ctx))
batches := suite.Keeper().GetUnSlashedBatches(suite.ctx, batch.Block+1)
suite.Equal(uint64(0), suite.Keeper().GetLastSlashedBatchBlock(suite.Ctx))
batches := suite.Keeper().GetUnSlashedBatches(suite.Ctx, batch.Block+1)
suite.Equal(1, len(batches))

msgConfirmBatch := &types.MsgConfirmBatch{
Expand All @@ -114,14 +114,14 @@ func (suite *KeeperTestSuite) TestKeeper_DeleteBatchConfig() {
for i, oracle := range suite.oracleAddrs {
msgConfirmBatch.BridgerAddress = suite.bridgerAddrs[i].String()
msgConfirmBatch.ExternalAddress = crypto.PubkeyToAddress(suite.externalPris[i].PublicKey).String()
suite.Keeper().SetBatchConfirm(suite.ctx, oracle, msgConfirmBatch)
suite.Keeper().SetBatchConfirm(suite.Ctx, oracle, msgConfirmBatch)
}
suite.Keeper().OutgoingTxBatchExecuted(suite.ctx, batch.TokenContract, batch.BatchNonce)
suite.Keeper().OutgoingTxBatchExecuted(suite.Ctx, batch.TokenContract, batch.BatchNonce)

for _, oracle := range suite.oracleAddrs {
suite.Nil(suite.Keeper().GetBatchConfirm(suite.ctx, batch.TokenContract, batch.BatchNonce, oracle))
suite.Nil(suite.Keeper().GetBatchConfirm(suite.Ctx, batch.TokenContract, batch.BatchNonce, oracle))
}
suite.Nil(suite.Keeper().GetOutgoingTxBatch(suite.ctx, batch.TokenContract, batch.BatchNonce))
suite.Nil(suite.Keeper().GetOutgoingTxBatch(suite.Ctx, batch.TokenContract, batch.BatchNonce))
}

func (suite *KeeperTestSuite) TestKeeper_IterateBatchBySlashedBatchBlock() {
Expand Down Expand Up @@ -150,10 +150,10 @@ func (suite *KeeperTestSuite) TestKeeper_IterateBatchBySlashedBatchBlock() {
Block: uint64(100 + i),
FeeReceive: helpers.GenHexAddress().Hex(),
}
suite.NoError(suite.Keeper().StoreBatch(suite.ctx, batch))
suite.NoError(suite.Keeper().StoreBatch(suite.Ctx, batch))
}
var batchs []*types.OutgoingTxBatch
suite.Keeper().IterateBatchByBlockHeight(suite.ctx, 100+1, uint64(100+index+1),
suite.Keeper().IterateBatchByBlockHeight(suite.Ctx, 100+1, uint64(100+index+1),
func(batch *types.OutgoingTxBatch) bool {
batchs = append(batchs, batch)
return false
Expand Down
24 changes: 12 additions & 12 deletions x/crosschain/keeper/bridge_call_out_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (

func (suite *KeeperTestSuite) TestKeeper_BridgeCallRefund() {
suite.bondedOracle()
oracleAddr, found := suite.Keeper().GetOracleAddrByBridgerAddr(suite.ctx, suite.bridgerAddrs[0])
oracleAddr, found := suite.Keeper().GetOracleAddrByBridgerAddr(suite.Ctx, suite.bridgerAddrs[0])
suite.Require().True(found)
suite.Require().EqualValues(oracleAddr.Bytes(), suite.oracleAddrs[0].Bytes())
suite.Commit()

oracleAddr, found = suite.Keeper().GetOracleAddrByBridgerAddr(suite.ctx, suite.bridgerAddrs[0])
oracleAddr, found = suite.Keeper().GetOracleAddrByBridgerAddr(suite.Ctx, suite.bridgerAddrs[0])
suite.Require().True(found)
suite.Require().EqualValues(oracleAddr.Bytes(), suite.oracleAddrs[0].Bytes())

Expand All @@ -36,7 +36,7 @@ func (suite *KeeperTestSuite) TestKeeper_BridgeCallRefund() {
randomBlock := tmrand.Int63n(1000000000)
randomAmount := tmrand.Int63n(1000000000)
claim := &types.MsgSendToFxClaim{
EventNonce: suite.Keeper().GetLastEventNonceByOracle(suite.ctx, suite.oracleAddrs[0]) + 1,
EventNonce: suite.Keeper().GetLastEventNonceByOracle(suite.Ctx, suite.oracleAddrs[0]) + 1,
BlockHeight: uint64(randomBlock),
TokenContract: bridgeTokenStr,
Amount: sdkmath.NewInt(randomAmount),
Expand All @@ -47,12 +47,12 @@ func (suite *KeeperTestSuite) TestKeeper_BridgeCallRefund() {
}
suite.SendClaim(claim)

pair, b := suite.app.Erc20Keeper.GetTokenPair(suite.ctx, "ttt")
pair, b := suite.App.Erc20Keeper.GetTokenPair(suite.Ctx, "ttt")
suite.True(b)
suite.Equal(sdkmath.NewInt(randomAmount), suite.app.BankKeeper.GetBalance(suite.ctx, fxAddr1.Bytes(), pair.Denom).Amount)
suite.Equal(sdkmath.NewInt(randomAmount), suite.App.BankKeeper.GetBalance(suite.Ctx, fxAddr1.Bytes(), pair.Denom).Amount)

bridgeCallRefundAddr := helpers.GenAccAddress()
_, err := suite.MsgServer().BridgeCall(suite.ctx, &types.MsgBridgeCall{
_, err := suite.MsgServer().BridgeCall(suite.Ctx, &types.MsgBridgeCall{
ChainName: suite.chainName,
Sender: sdk.AccAddress(fxAddr1.Bytes()).String(),
Refund: bridgeCallRefundAddr.String(),
Expand All @@ -61,18 +61,18 @@ func (suite *KeeperTestSuite) TestKeeper_BridgeCallRefund() {
})
suite.NoError(err)

suite.Equal(sdkmath.NewInt(0), suite.app.BankKeeper.GetBalance(suite.ctx, fxAddr1.Bytes(), pair.Denom).Amount)
suite.Equal(sdkmath.NewInt(0), suite.App.BankKeeper.GetBalance(suite.Ctx, fxAddr1.Bytes(), pair.Denom).Amount)

var outgoingBridgeCall *types.OutgoingBridgeCall
suite.Keeper().IterateOutgoingBridgeCallsByAddress(suite.ctx, types.ExternalAddrToStr(suite.chainName, fxAddr1.Bytes()), func(value *types.OutgoingBridgeCall) bool {
suite.Keeper().IterateOutgoingBridgeCallsByAddress(suite.Ctx, types.ExternalAddrToStr(suite.chainName, fxAddr1.Bytes()), func(value *types.OutgoingBridgeCall) bool {
outgoingBridgeCall = value
return true
})
suite.NotNil(outgoingBridgeCall)

// Triggering the SendtoFx claim once is just to trigger timeout
sendToFxClaim := &types.MsgSendToFxClaim{
EventNonce: suite.Keeper().GetLastEventNonceByOracle(suite.ctx, suite.oracleAddrs[0]) + 1,
EventNonce: suite.Keeper().GetLastEventNonceByOracle(suite.Ctx, suite.oracleAddrs[0]) + 1,
BlockHeight: outgoingBridgeCall.Timeout,
TokenContract: bridgeTokenStr,
Amount: sdkmath.NewInt(randomAmount),
Expand All @@ -84,15 +84,15 @@ func (suite *KeeperTestSuite) TestKeeper_BridgeCallRefund() {
suite.SendClaim(sendToFxClaim)
// expect balance = sendToFx value + outgointBridgeCallRefund value
suite.checkBalanceOf(pair.GetERC20Contract(), fxAddr1, big.NewInt(randomAmount))
suite.Equal(sdkmath.NewInt(0), suite.app.BankKeeper.GetBalance(suite.ctx, fxAddr1.Bytes(), pair.Denom).Amount)
suite.Equal(sdkmath.NewInt(randomAmount), suite.app.BankKeeper.GetBalance(suite.ctx, bridgeCallRefundAddr, pair.Denom).Amount)
suite.Equal(sdkmath.NewInt(0), suite.App.BankKeeper.GetBalance(suite.Ctx, fxAddr1.Bytes(), pair.Denom).Amount)
suite.Equal(sdkmath.NewInt(randomAmount), suite.App.BankKeeper.GetBalance(suite.Ctx, bridgeCallRefundAddr, pair.Denom).Amount)
}

func (suite *KeeperTestSuite) checkBalanceOf(contractAddr, address common.Address, expectBalance *big.Int) {
var balanceRes struct {
Value *big.Int
}
err := suite.app.EvmKeeper.QueryContract(suite.ctx, contractAddr, contractAddr, contract.GetFIP20().ABI, "balanceOf", &balanceRes, address)
err := suite.App.EvmKeeper.QueryContract(suite.Ctx, contractAddr, contractAddr, contract.GetFIP20().ABI, "balanceOf", &balanceRes, address)
suite.Require().NoError(err)
suite.EqualValuesf(expectBalance.Cmp(balanceRes.Value), 0, "expect balance %s, got %s", expectBalance.String(), balanceRes.Value.String())
}
10 changes: 5 additions & 5 deletions x/crosschain/keeper/bridge_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (

func (suite *KeeperTestSuite) TestKeeper_BridgeToken() {
tokenContract := helpers.GenHexAddress().Hex()
denom, err := suite.Keeper().SetIbcDenomTrace(suite.ctx, tokenContract, "")
denom, err := suite.Keeper().SetIbcDenomTrace(suite.Ctx, tokenContract, "")
suite.NoError(err)
suite.Equal(types.NewBridgeDenom(suite.chainName, tokenContract), denom)

suite.Keeper().AddBridgeToken(suite.ctx, tokenContract, denom)
suite.Keeper().AddBridgeToken(suite.Ctx, tokenContract, denom)

bridgeToken := &types.BridgeToken{Token: tokenContract, Denom: denom}
suite.EqualValues(bridgeToken, suite.Keeper().GetBridgeTokenDenom(suite.ctx, tokenContract))
suite.EqualValues(bridgeToken, suite.Keeper().GetBridgeTokenDenom(suite.Ctx, tokenContract))

suite.EqualValues(bridgeToken, suite.Keeper().GetDenomBridgeToken(suite.ctx, denom))
suite.EqualValues(bridgeToken, suite.Keeper().GetDenomBridgeToken(suite.Ctx, denom))

suite.Keeper().IterateBridgeTokenToDenom(suite.ctx, func(bt *types.BridgeToken) bool {
suite.Keeper().IterateBridgeTokenToDenom(suite.Ctx, func(bt *types.BridgeToken) bool {
suite.Equal(bt.Token, tokenContract)
suite.Equal(bt.Denom, denom)
return false
Expand Down
32 changes: 16 additions & 16 deletions x/crosschain/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ func (suite *KeeperTestSuite) TestBatchAndTxImportExport() {
Denom: types.NewBridgeDenom(suite.chainName, contractAddress),
}
bridgeTokens[i] = bridgeToken
denom, err := suite.Keeper().SetIbcDenomTrace(suite.ctx, bridgeToken.Token, "")
denom, err := suite.Keeper().SetIbcDenomTrace(suite.Ctx, bridgeToken.Token, "")
suite.Require().NoError(err)
suite.Require().Equal(denom, bridgeToken.Denom)
suite.Keeper().AddBridgeToken(suite.ctx, bridgeToken.Token, denom)
suite.Keeper().AddBridgeToken(suite.Ctx, bridgeToken.Token, denom)

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))
err := suite.App.BankKeeper.MintCoins(suite.Ctx, suite.chainName, sdk.NewCoins(voucher))
suite.Require().NoError(err)

err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, suite.chainName, bridger, sdk.NewCoins(voucher))
err = suite.App.BankKeeper.SendCoinsFromModuleToAccount(suite.Ctx, suite.chainName, bridger, sdk.NewCoins(voucher))
suite.Require().NoError(err)
}
}
Expand All @@ -58,7 +58,7 @@ func (suite *KeeperTestSuite) TestBatchAndTxImportExport() {
feeToken := sdk.NewCoin(bridgeToken.Denom, sdkmath.NewInt(int64(fee)))

// add transaction to the pool
nextTxID, err := suite.Keeper().AddToOutgoingPool(suite.ctx, sender, receiver, amountToken, feeToken)
nextTxID, err := suite.Keeper().AddToOutgoingPool(suite.Ctx, sender, receiver, amountToken, feeToken)
suite.Require().NoError(err)

txs[i] = &types.OutgoingTransferTx{
Expand All @@ -70,15 +70,15 @@ func (suite *KeeperTestSuite) TestBatchAndTxImportExport() {
}
}

suite.Keeper().SetLastObservedBlockHeight(suite.ctx, 10, 10)
suite.Keeper().SetLastObservedBlockHeight(suite.Ctx, 10, 10)

// CREATE BATCHES
// ==================
// Want to create batches for half of the transactions for each 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))
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))
suite.Require().NoError(err)
suite.Require().EqualValues(100, len(batch.Transactions))
suite.Require().EqualValues(50+i, batch.Block)
Expand All @@ -94,30 +94,30 @@ func (suite *KeeperTestSuite) TestBatchAndTxImportExport() {

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

// clear data
storeKey := suite.app.GetKey(suite.chainName)
store := suite.ctx.KVStore(storeKey)
storeKey := suite.App.GetKey(suite.chainName)
store := suite.Ctx.KVStore(storeKey)
iterator := store.Iterator(nil, nil)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
store.Delete(iterator.Key())
}
unbatched := suite.Keeper().GetUnbatchedTransactions(suite.ctx)
unbatched := suite.Keeper().GetUnbatchedTransactions(suite.Ctx)
suite.Require().Empty(unbatched)
batches := suite.Keeper().GetOutgoingTxBatches(suite.ctx)
batches := suite.Keeper().GetOutgoingTxBatches(suite.Ctx)
suite.Require().Empty(batches)

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

// Requires that all transactions in txs exist in keeper
func (suite *KeeperTestSuite) checkAllTransactionsExist(txs types.OutgoingTransferTxs) {
unbatched := suite.Keeper().GetUnbatchedTransactions(suite.ctx)
batches := suite.Keeper().GetOutgoingTxBatches(suite.ctx)
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...)
Expand Down
Loading

0 comments on commit 95279c1

Please sign in to comment.