Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Jun 28, 2024
1 parent 54d6135 commit 66e2b77
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 113 deletions.
8 changes: 4 additions & 4 deletions pkg/chains/chains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func TestCombineDefaultChainsList(t *testing.T) {
// chain IDs are 11000 - 11009 to not conflict with the default chains
var chainList = make([]chains.Chain, 0, 10)
for i := int64(11000); i < 10; i++ {
chainList = append(chainList, *sample.Chain(i))
chainList = append(chainList, sample.Chain(i))
}

bitcoinMainnetChainID := chains.BitcoinMainnet.ChainId
Expand All @@ -289,7 +289,7 @@ func TestCombineDefaultChainsList(t *testing.T) {
chains.DefaultChainsList()[0].ChainId,
"Bitcoin mainnet be the first in the default chain list for TestCombineDefaultChainsList tests",
)
alternativeBitcoinMainnet := *sample.Chain(bitcoinMainnetChainID)
alternativeBitcoinMainnet := sample.Chain(bitcoinMainnetChainID)

tests := []struct {
name string
Expand Down Expand Up @@ -324,13 +324,13 @@ func TestCombineChainList(t *testing.T) {
// prepare array containing pre-defined chains
var chainList = make([]chains.Chain, 0, 10)
for i := int64(0); i < 10; i++ {
chainList = append(chainList, *sample.Chain(i))
chainList = append(chainList, sample.Chain(i))
}

// prepare second array for duplicated chain IDs
var duplicatedChainList = make([]chains.Chain, 0, 10)
for i := int64(0); i < 10; i++ {
duplicatedChainList = append(duplicatedChainList, *sample.Chain(i))
duplicatedChainList = append(duplicatedChainList, sample.Chain(i))
}

tests := []struct {
Expand Down
26 changes: 8 additions & 18 deletions testutil/keeper/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,24 +261,14 @@ func GetCrosschainFungibleMock(t testing.TB, keeper *keeper.Keeper) *crosschainm
return cfk
}

func MockGetSupportedChainFromChainID(m *crosschainmocks.CrosschainObserverKeeper, senderChain *chains.Chain) {
if senderChain != nil {
m.On("GetSupportedChainFromChainID", mock.Anything, senderChain.ChainId).
Return(senderChain).Once()
} else {
m.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).
Return(&chains.Chain{}).Once()
}
func MockGetSupportedChainFromChainID(m *crosschainmocks.CrosschainObserverKeeper, senderChain chains.Chain) {
m.On("GetSupportedChainFromChainID", mock.Anything, senderChain.ChainId).
Return(senderChain, true).Once()
}

func MockFailedGetSupportedChainFromChainID(m *crosschainmocks.CrosschainObserverKeeper, senderChain *chains.Chain) {
if senderChain != nil {
m.On("GetSupportedChainFromChainID", mock.Anything, senderChain.ChainId).
Return(nil).Once()
} else {
m.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).
Return(nil).Once()
}
func MockFailedGetSupportedChainFromChainID(m *crosschainmocks.CrosschainObserverKeeper, senderChain chains.Chain) {
m.On("GetSupportedChainFromChainID", mock.Anything, senderChain.ChainId).
Return(chains.Chain{}, false).Once()
}

func MockGetRevertGasLimitForERC20(
Expand All @@ -304,7 +294,7 @@ func MockPayGasAndUpdateCCTX(
asset string,
) {
m2.On("GetSupportedChainFromChainID", mock.Anything, senderChain.ChainId).
Return(&senderChain).Twice()
Return(senderChain, true).Twice()
m.On("GetForeignCoinFromAsset", mock.Anything, asset, senderChain.ChainId).
Return(fungibletypes.ForeignCoins{
Zrc20ContractAddress: sample.EthAddress().String(),
Expand Down Expand Up @@ -340,7 +330,7 @@ func MockUpdateNonce(m *crosschainmocks.CrosschainObserverKeeper, senderChain ch
nonce = uint64(1)
tss := sample.Tss()
m.On("GetSupportedChainFromChainID", mock.Anything, senderChain.ChainId).
Return(senderChain)
Return(senderChain, true).Once()
m.On("GetChainNonces", mock.Anything, senderChain.ChainName.String()).
Return(observertypes.ChainNonces{Nonce: nonce}, true)
m.On("GetTSS", mock.Anything).
Expand Down
15 changes: 3 additions & 12 deletions x/crosschain/keeper/cctx_orchestrator_validate_outbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,6 @@ func TestKeeper_ValidateFailedOutbound(t *testing.T) {
// mock successful PayGasAndUpdateCctx
keepertest.MockPayGasAndUpdateCCTX(fungibleMock, observerMock, ctx, *k, senderChain, asset)

// mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, &senderChain)

// mock successful UpdateNonce
_ = keepertest.MockUpdateNonce(observerMock, senderChain)

Expand Down Expand Up @@ -293,9 +290,6 @@ func TestKeeper_ValidateFailedOutbound(t *testing.T) {
// mock successful PayGasAndUpdateCctx
keepertest.MockPayGasAndUpdateCCTX(fungibleMock, observerMock, ctx, *k, senderChain, asset)

// mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, &senderChain)

// mock successful UpdateNonce
_ = keepertest.MockUpdateNonce(observerMock, senderChain)

Expand Down Expand Up @@ -334,7 +328,7 @@ func TestKeeper_ValidateFailedOutbound(t *testing.T) {
keepertest.MockPayGasAndUpdateCCTX(fungibleMock, observerMock, ctx, *k, senderChain, asset)

// mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, &senderChain)
keepertest.MockGetSupportedChainFromChainID(observerMock, senderChain)

// mock failed UpdateNonce
observerMock.On("GetChainNonces", mock.Anything, senderChain.ChainName.String()).
Expand Down Expand Up @@ -370,10 +364,10 @@ func TestKeeper_ValidateFailedOutbound(t *testing.T) {
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, senderChain, 100)

// mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, &senderChain)
keepertest.MockGetSupportedChainFromChainID(observerMock, senderChain)

// mock failed failed GetSupportedChainFromChainID to fail PayGasAndUpdateCctx
keepertest.MockFailedGetSupportedChainFromChainID(observerMock, &senderChain)
keepertest.MockFailedGetSupportedChainFromChainID(observerMock, senderChain)

cctx := GetERC20Cctx(t, receiver, senderChain, asset, amount)
cctx.CctxStatus.Status = types.CctxStatus_PendingOutbound
Expand Down Expand Up @@ -563,9 +557,6 @@ func TestKeeper_ValidateOutboundObservers(t *testing.T) {
// mock successful PayGasAndUpdateCctx
keepertest.MockPayGasAndUpdateCCTX(fungibleMock, observerMock, ctx, *k, senderChain, asset)

// mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, &senderChain)

// mock successful UpdateNonce
_ = keepertest.MockUpdateNonce(observerMock, senderChain)

Expand Down
12 changes: 6 additions & 6 deletions x/crosschain/keeper/cctx_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func TestKeeper_SetObserverOutboundInfo(t *testing.T) {

observerMock := keepertest.GetCrosschainObserverMock(t, k)
// mock failed GetSupportedChainFromChainID
keepertest.MockFailedGetSupportedChainFromChainID(observerMock, nil)
keepertest.MockFailedGetSupportedChainFromChainID(observerMock, sample.Chain(5))

err := k.SetObserverOutboundInfo(ctx, 5, nil)
require.Error(t, err)
Expand All @@ -248,7 +248,7 @@ func TestKeeper_SetObserverOutboundInfo(t *testing.T) {
observerMock := keepertest.GetCrosschainObserverMock(t, k)

// mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, &chains.Chain{
keepertest.MockGetSupportedChainFromChainID(observerMock, chains.Chain{
ChainName: 5,
ChainId: 5,
})
Expand All @@ -274,7 +274,7 @@ func TestKeeper_SetObserverOutboundInfo(t *testing.T) {
observerMock := keepertest.GetCrosschainObserverMock(t, k)

// mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, &chains.Chain{
keepertest.MockGetSupportedChainFromChainID(observerMock, chains.Chain{
ChainName: 5,
ChainId: 5,
})
Expand Down Expand Up @@ -304,7 +304,7 @@ func TestKeeper_SetObserverOutboundInfo(t *testing.T) {
observerMock := keepertest.GetCrosschainObserverMock(t, k)

// mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, &chains.Chain{
keepertest.MockGetSupportedChainFromChainID(observerMock, chains.Chain{
ChainName: 5,
ChainId: 5,
})
Expand Down Expand Up @@ -336,7 +336,7 @@ func TestKeeper_SetObserverOutboundInfo(t *testing.T) {
observerMock := keepertest.GetCrosschainObserverMock(t, k)

// mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, &chains.Chain{
keepertest.MockGetSupportedChainFromChainID(observerMock, chains.Chain{
ChainName: 5,
ChainId: 5,
})
Expand Down Expand Up @@ -370,7 +370,7 @@ func TestKeeper_SetObserverOutboundInfo(t *testing.T) {
observerMock := keepertest.GetCrosschainObserverMock(t, k)

// mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, &chains.Chain{
keepertest.MockGetSupportedChainFromChainID(observerMock, chains.Chain{
ChainName: 5,
ChainId: 5,
})
Expand Down
34 changes: 20 additions & 14 deletions x/crosschain/keeper/initiate_outbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestKeeper_InitiateOutboundZEVMDeposit(t *testing.T) {
keepertest.MockRevertForHandleEVMDeposit(fungibleMock, receiver, amount, senderChain.ChainId, errDeposit)

// mock unsuccessful GetSupportedChainFromChainID
keepertest.MockFailedGetSupportedChainFromChainID(observerMock, &senderChain)
keepertest.MockFailedGetSupportedChainFromChainID(observerMock, senderChain)

// call InitiateOutbound
cctx := GetERC20Cctx(t, receiver, senderChain, "", amount)
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestKeeper_InitiateOutboundZEVMDeposit(t *testing.T) {
keepertest.MockRevertForHandleEVMDeposit(fungibleMock, receiver, amount, senderChain.ChainId, errDeposit)

// Mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, &senderChain)
keepertest.MockGetSupportedChainFromChainID(observerMock, senderChain)

// mock unsuccessful GetRevertGasLimit for ERC20
fungibleMock.On("GetForeignCoinFromAsset", mock.Anything, asset, senderChain.ChainId).
Expand Down Expand Up @@ -177,13 +177,13 @@ func TestKeeper_InitiateOutboundZEVMDeposit(t *testing.T) {
observerMock := keepertest.GetCrosschainObserverMock(t, k)

// Mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, &senderChain)
keepertest.MockGetSupportedChainFromChainID(observerMock, senderChain)

// mock successful GetRevertGasLimit for ERC20
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, senderChain, 100)

// mock unsuccessful PayGasInERC20AndUpdateCctx
keepertest.MockFailedGetSupportedChainFromChainID(observerMock, &senderChain)
keepertest.MockFailedGetSupportedChainFromChainID(observerMock, senderChain)

// call InitiateOutbound
cctx := GetERC20Cctx(t, receiver, senderChain, asset, amount)
Expand Down Expand Up @@ -222,13 +222,13 @@ func TestKeeper_InitiateOutboundZEVMDeposit(t *testing.T) {
observerMock := keepertest.GetCrosschainObserverMock(t, k)

// Mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, &senderChain)
keepertest.MockGetSupportedChainFromChainID(observerMock, senderChain)

// mock successful GetRevertGasLimit for ERC20
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, senderChain, 0)

// mock unsuccessful PayGasInERC20AndUpdateCctx
keepertest.MockFailedGetSupportedChainFromChainID(observerMock, &senderChain)
keepertest.MockFailedGetSupportedChainFromChainID(observerMock, senderChain)

// call InitiateOutbound
cctx := GetERC20Cctx(t, receiver, senderChain, asset, amount)
Expand Down Expand Up @@ -265,7 +265,7 @@ func TestKeeper_InitiateOutboundZEVMDeposit(t *testing.T) {
keepertest.MockRevertForHandleEVMDeposit(fungibleMock, receiver, amount, senderChain.ChainId, errDeposit)

// Mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, &senderChain)
keepertest.MockGetSupportedChainFromChainID(observerMock, senderChain)

// mock successful GetRevertGasLimit for ERC20
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, senderChain, 100)
Expand Down Expand Up @@ -306,9 +306,6 @@ func TestKeeper_InitiateOutboundZEVMDeposit(t *testing.T) {
// mock unsuccessful HandleEVMDeposit which reverts , i.e returns err and isContractReverted = true
keepertest.MockRevertForHandleEVMDeposit(fungibleMock, receiver, amount, senderChain.ChainId, errDeposit)

// Mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, &senderChain)

// mock successful GetRevertGasLimit for ERC20
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, senderChain, 100)

Expand Down Expand Up @@ -349,7 +346,7 @@ func TestKeeper_InitiateOutboundZEVMDeposit(t *testing.T) {
keepertest.MockRevertForHandleEVMDeposit(fungibleMock, receiver, amount, senderChain.ChainId, errDeposit)

// Mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, &senderChain)
keepertest.MockGetSupportedChainFromChainID(observerMock, senderChain)

// mock successful GetRevertGasLimit for ERC20
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, senderChain, 100)
Expand Down Expand Up @@ -389,15 +386,24 @@ func TestKeeper_InitiateOutboundProcessCrosschainMsgPassing(t *testing.T) {
keepertest.MockPayGasAndUpdateCCTX(fungibleMock, observerMock, ctx, *k, receiverChain, "")

// mock successful UpdateNonce
updatedNonce := keepertest.MockUpdateNonce(observerMock, receiverChain)
nonce := uint64(1)
tss := sample.Tss()
observerMock.On("GetChainNonces", mock.Anything, receiverChain.ChainName.String()).
Return(observertypes.ChainNonces{Nonce: nonce}, true)
observerMock.On("GetTSS", mock.Anything).
Return(tss, true)
observerMock.On("GetPendingNonces", mock.Anything, tss.TssPubkey, mock.Anything).
Return(observertypes.PendingNonces{NonceHigh: int64(nonce)}, true)
observerMock.On("SetChainNonces", mock.Anything, mock.Anything)
observerMock.On("SetPendingNonces", mock.Anything, mock.Anything)

// call InitiateOutbound
cctx := GetERC20Cctx(t, receiver, receiverChain, "", amount)
newStatus, err := k.InitiateOutbound(ctx, keeper.InitiateOutboundConfig{CCTX: cctx, ShouldPayGas: true})
require.NoError(t, err)
require.Equal(t, types.CctxStatus_PendingOutbound, cctx.CctxStatus.Status)
require.Equal(t, types.CctxStatus_PendingOutbound, newStatus)
require.Equal(t, updatedNonce, cctx.GetCurrentOutboundParam().TssNonce)
require.Equal(t, nonce, cctx.GetCurrentOutboundParam().TssNonce)
})

t.Run("unable to process crosschain msg passing PayGasAndUpdateCctx fails", func(t *testing.T) {
Expand All @@ -413,7 +419,7 @@ func TestKeeper_InitiateOutboundProcessCrosschainMsgPassing(t *testing.T) {
receiverChain := getValidEthChain()

// mock unsuccessful PayGasAndUpdateCctx
keepertest.MockFailedGetSupportedChainFromChainID(observerMock, nil)
keepertest.MockFailedGetSupportedChainFromChainID(observerMock, receiverChain)

// call InitiateOutbound
cctx := GetERC20Cctx(t, receiver, receiverChain, "", amount)
Expand Down
18 changes: 9 additions & 9 deletions x/crosschain/keeper/msg_server_add_inbound_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestMsgServer_AddToInboundTracker(t *testing.T) {
txHash := "string"
chainID := getValidEthChainID()

observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(chains.Chain{}, true)
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)

msg := types.MsgAddInboundTracker{
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestMsgServer_AddToInboundTracker(t *testing.T) {
chainID := getValidEthChainID()

observerMock := keepertest.GetCrosschainObserverMock(t, k)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(nil)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(chains.Chain{}, false)

msg := types.MsgAddInboundTracker{
Creator: sample.AccAddress(),
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestMsgServer_AddToInboundTracker(t *testing.T) {
txHash := "string"
chainID := getValidEthChainID()

observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(chains.Chain{}, true)
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)

setSupportedChain(ctx, zk, chainID)
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestMsgServer_AddToInboundTracker(t *testing.T) {
txHash := "string"
chainID := getValidEthChainID()

observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(chains.Chain{}, true)
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(true)

msg := types.MsgAddInboundTracker{
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestMsgServer_AddToInboundTracker(t *testing.T) {
txHash := "string"
chainID := getValidEthChainID()

observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(chains.Chain{}, true)
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)
lightclientMock.On("VerifyProof", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(nil, errors.New("error"))
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestMsgServer_AddToInboundTracker(t *testing.T) {
txHash := "string"
chainID := getValidEthChainID()

observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(chains.Chain{}, true)
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)
lightclientMock.On("VerifyProof", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(sample.Bytes(), nil)
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestMsgServer_AddToInboundTracker(t *testing.T) {
txHash := "string"
chainID := getValidEthChainID()

observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(chains.Chain{}, true)
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)
lightclientMock.On("VerifyProof", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(sample.Bytes(), nil)
Expand Down Expand Up @@ -271,7 +271,7 @@ func TestMsgServer_AddToInboundTracker(t *testing.T) {
txHash := "string"
chainID := getValidEthChainID()

observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(chains.Chain{}, true)
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)
observerMock.On("GetChainParamsByChainID", mock.Anything, mock.Anything).
Return(sample.ChainParams(chains.Ethereum.ChainId), true)
Expand Down Expand Up @@ -317,7 +317,7 @@ func TestMsgServer_AddToInboundTracker(t *testing.T) {
observerMock := keepertest.GetCrosschainObserverMock(t, k)
lightclientMock := keepertest.GetCrosschainLightclientMock(t, k)

observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(chains.Chain{}, true)
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)
observerMock.On("GetChainParamsByChainID", mock.Anything, mock.Anything).
Return(sample.ChainParams(chains.Ethereum.ChainId), true)
Expand Down
Loading

0 comments on commit 66e2b77

Please sign in to comment.