Skip to content

Commit

Permalink
fix evm hooks tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Jun 12, 2024
1 parent f675251 commit 0ef2093
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 45 deletions.
8 changes: 4 additions & 4 deletions x/crosschain/keeper/cctx_gateway_observers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ InitiateOutbound updates the store so observers can use the PendingCCTX query:
func (c CCTXGatewayObservers) InitiateOutbound(
ctx sdk.Context,
config InitiateOutboundConfig,
) (newCCTXStatus types.CctxStatus) {
) (newCCTXStatus types.CctxStatus, err error) {
tmpCtx, commit := ctx.CacheContext()
outboundReceiverChainID := config.CCTX.GetCurrentOutboundParam().ReceiverChainId
// TODO: does this condition make sense?
Expand All @@ -45,7 +45,7 @@ func (c CCTXGatewayObservers) InitiateOutbound(
noEthereumTxEvent = true
}

err := func() error {
err = func() error {
if config.PayGas {
err := c.crosschainKeeper.PayGasAndUpdateCctx(
tmpCtx,
Expand All @@ -70,9 +70,9 @@ func (c CCTXGatewayObservers) InitiateOutbound(
if err != nil {
// do not commit anything here as the CCTX should be aborted
config.CCTX.SetAbort(err.Error())
return types.CctxStatus_Aborted
return types.CctxStatus_Aborted, err
}
commit()
config.CCTX.SetPendingOutbound("")
return types.CctxStatus_PendingOutbound
return types.CctxStatus_PendingOutbound, nil
}
6 changes: 3 additions & 3 deletions x/crosschain/keeper/cctx_gateway_zevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ func NewCCTXGatewayZEVM(crosschainKeeper Keeper) CCTXGatewayZEVM {
func (c CCTXGatewayZEVM) InitiateOutbound(
ctx sdk.Context,
config InitiateOutboundConfig,
) (newCCTXStatus types.CctxStatus) {
) (newCCTXStatus types.CctxStatus, err error) {
tmpCtx, commit := ctx.CacheContext()
isContractReverted, err := c.crosschainKeeper.HandleEVMDeposit(tmpCtx, config.CCTX)

if err != nil && !isContractReverted {
// exceptional case; internal error; should abort CCTX
config.CCTX.SetAbort(err.Error())
return types.CctxStatus_Aborted
return types.CctxStatus_Aborted, err

Check warning on line 32 in x/crosschain/keeper/cctx_gateway_zevm.go

View check run for this annotation

Codecov / codecov/patch

x/crosschain/keeper/cctx_gateway_zevm.go#L31-L32

Added lines #L31 - L32 were not covered by tests
}

config.CCTX.SetPendingOutbound("")
Expand All @@ -38,5 +38,5 @@ func (c CCTXGatewayZEVM) InitiateOutbound(
commit()
}

return newCCTXStatus
return newCCTXStatus, nil
}
9 changes: 7 additions & 2 deletions x/crosschain/keeper/cctx_gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type CCTXGateway interface {
// Initiate a new outbound, this tells the CCTXGateway to carry out the action to execute the outbound.
// It is the only entry point to initiate an outbound and it returns new CCTX status after it is completed.
InitiateOutbound(ctx sdk.Context, config InitiateOutboundConfig) (newCCTXStatus types.CctxStatus)
InitiateOutbound(ctx sdk.Context, config InitiateOutboundConfig) (newCCTXStatus types.CctxStatus, err error)
}

var cctxGateways map[chains.CCTXGateway]CCTXGateway
Expand All @@ -26,7 +26,12 @@ func InitCCTXGateways(keeper Keeper) {
}
}

func ResolveCCTXGateway(c chains.CCTXGateway) (CCTXGateway, bool) {
func ResolveCCTXGateway(c chains.CCTXGateway, keeper Keeper) (CCTXGateway, bool) {
cctxGateways = map[chains.CCTXGateway]CCTXGateway{
chains.CCTXGateway_observers: NewCCTXGatewayObservers(keeper),
chains.CCTXGateway_zevm: NewCCTXGatewayZEVM(keeper),
}

cctxGateway, ok := cctxGateways[c]
return cctxGateway, ok
}
1 change: 0 additions & 1 deletion x/crosschain/keeper/evm_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ func (k Keeper) ProcessZRC20WithdrawalEvent(
emittingContract ethcommon.Address,
txOrigin string,
) error {

ctx.Logger().Info(fmt.Sprintf("ZRC20 withdrawal to %s amount %d", hex.EncodeToString(event.To), event.Value))
foreignCoin, found := k.fungibleKeeper.GetForeignCoins(ctx, event.Raw.Address.Hex())
if !found {
Expand Down
56 changes: 23 additions & 33 deletions x/crosschain/keeper/evm_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,8 @@ func TestKeeper_ProcessZRC20WithdrawalEvent(t *testing.T) {
event.Raw.Address = zrc20
emittingContract := sample.EthAddress()
txOrigin := sample.EthAddress()
tss := sample.Tss()

err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex(), tss)
err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex())
require.NoError(t, err)
cctxList := k.GetAllCrossChainTx(ctx)
require.Len(t, cctxList, 1)
Expand All @@ -237,9 +236,8 @@ func TestKeeper_ProcessZRC20WithdrawalEvent(t *testing.T) {
event.Raw.Address = zrc20
emittingContract := sample.EthAddress()
txOrigin := sample.EthAddress()
tss := sample.Tss()

err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex(), tss)
err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex())
require.NoError(t, err)
cctxList := k.GetAllCrossChainTx(ctx)
require.Len(t, cctxList, 1)
Expand All @@ -262,9 +260,8 @@ func TestKeeper_ProcessZRC20WithdrawalEvent(t *testing.T) {
setupGasCoin(t, ctx, zk.FungibleKeeper, sdkk.EvmKeeper, chainID, "ethereum", "ETH")
emittingContract := sample.EthAddress()
txOrigin := sample.EthAddress()
tss := sample.Tss()

err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex(), tss)
err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex())
require.ErrorContains(t, err, "cannot find foreign coin with emittingContract address")
require.Empty(t, k.GetAllCrossChainTx(ctx))
})
Expand All @@ -283,9 +280,8 @@ func TestKeeper_ProcessZRC20WithdrawalEvent(t *testing.T) {
event.Raw.Address = zrc20
emittingContract := sample.EthAddress()
txOrigin := sample.EthAddress()
tss := sample.Tss()

err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex(), tss)
err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex())
require.ErrorContains(t, err, "chain not supported")
require.Empty(t, k.GetAllCrossChainTx(ctx))
})
Expand All @@ -305,10 +301,10 @@ func TestKeeper_ProcessZRC20WithdrawalEvent(t *testing.T) {
event.Raw.Address = zrc20
emittingContract := sample.EthAddress()
txOrigin := sample.EthAddress()
tss := sample.Tss()

ctx = ctx.WithChainID("test_21-1")

err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex(), tss)
err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex())
require.ErrorContains(t, err, "failed to convert chainID: chain 21 not found")
require.Empty(t, k.GetAllCrossChainTx(ctx))
})
Expand All @@ -329,9 +325,8 @@ func TestKeeper_ProcessZRC20WithdrawalEvent(t *testing.T) {
event.To = ethcommon.Address{}.Bytes()
emittingContract := sample.EthAddress()
txOrigin := sample.EthAddress()
tss := sample.Tss()

err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex(), tss)
err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex())
require.ErrorContains(t, err, "cannot encode address")
require.Empty(t, k.GetAllCrossChainTx(ctx))
})
Expand All @@ -354,13 +349,13 @@ func TestKeeper_ProcessZRC20WithdrawalEvent(t *testing.T) {
event.Raw.Address = zrc20
emittingContract := sample.EthAddress()
txOrigin := sample.EthAddress()
tss := sample.Tss()

fc, _ := zk.FungibleKeeper.GetForeignCoins(ctx, zrc20.Hex())

fungibleMock.On("GetForeignCoins", mock.Anything, mock.Anything).Return(fc, true)
fungibleMock.On("QueryGasLimit", mock.Anything, mock.Anything).
Return(big.NewInt(0), fmt.Errorf("error querying gas limit"))
err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex(), tss)
err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex())
require.ErrorContains(t, err, "error querying gas limit")
require.Empty(t, k.GetAllCrossChainTx(ctx))
})
Expand All @@ -381,9 +376,8 @@ func TestKeeper_ProcessZRC20WithdrawalEvent(t *testing.T) {
event.Raw.Address = zrc20
emittingContract := sample.EthAddress()
txOrigin := sample.EthAddress()
tss := sample.Tss()

err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex(), tss)
err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex())
require.ErrorContains(t, err, "gasprice not found")
require.Empty(t, k.GetAllCrossChainTx(ctx))
})
Expand All @@ -408,10 +402,9 @@ func TestKeeper_ProcessZRC20WithdrawalEvent(t *testing.T) {
event.Raw.Address = zrc20
emittingContract := sample.EthAddress()
txOrigin := sample.EthAddress()
tss := sample.Tss()

err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex(), tss)
require.ErrorContains(t, err, "ProcessWithdrawalEvent: update nonce failed")
err = k.ProcessZRC20WithdrawalEvent(ctx, event, emittingContract, txOrigin.Hex())
require.ErrorContains(t, err, "nonce mismatch")
require.Empty(t, k.GetAllCrossChainTx(ctx))
})
}
Expand Down Expand Up @@ -489,9 +482,8 @@ func TestKeeper_ProcessZetaSentEvent(t *testing.T) {
require.NoError(t, err)
emittingContract := sample.EthAddress()
txOrigin := sample.EthAddress()
tss := sample.Tss()

err = k.ProcessZetaSentEvent(ctx, event, emittingContract, txOrigin.Hex(), tss)
err = k.ProcessZetaSentEvent(ctx, event, emittingContract, txOrigin.Hex())
require.NoError(t, err)
cctxList := k.GetAllCrossChainTx(ctx)
require.Len(t, cctxList, 1)
Expand Down Expand Up @@ -526,9 +518,8 @@ func TestKeeper_ProcessZetaSentEvent(t *testing.T) {
require.NoError(t, err)
emittingContract := sample.EthAddress()
txOrigin := sample.EthAddress()
tss := sample.Tss()

err = k.ProcessZetaSentEvent(ctx, event, emittingContract, txOrigin.Hex(), tss)
err = k.ProcessZetaSentEvent(ctx, event, emittingContract, txOrigin.Hex())
require.ErrorContains(t, err, "ProcessZetaSentEvent: failed to burn coins from fungible")
})

Expand Down Expand Up @@ -557,8 +548,8 @@ func TestKeeper_ProcessZetaSentEvent(t *testing.T) {
require.NoError(t, err)
emittingContract := sample.EthAddress()
txOrigin := sample.EthAddress()
tss := sample.Tss()
err = k.ProcessZetaSentEvent(ctx, event, emittingContract, txOrigin.Hex(), tss)

err = k.ProcessZetaSentEvent(ctx, event, emittingContract, txOrigin.Hex())
require.ErrorContains(t, err, "chain not supported")
})

Expand Down Expand Up @@ -589,9 +580,9 @@ func TestKeeper_ProcessZetaSentEvent(t *testing.T) {
require.NoError(t, err)
emittingContract := sample.EthAddress()
txOrigin := sample.EthAddress()
tss := sample.Tss()

ctx = ctx.WithChainID("test-21-1")
err = k.ProcessZetaSentEvent(ctx, event, emittingContract, txOrigin.Hex(), tss)
err = k.ProcessZetaSentEvent(ctx, event, emittingContract, txOrigin.Hex())
require.ErrorContains(t, err, "ProcessZetaSentEvent: failed to convert chainID")
})

Expand Down Expand Up @@ -619,10 +610,9 @@ func TestKeeper_ProcessZetaSentEvent(t *testing.T) {
require.NoError(t, err)
emittingContract := sample.EthAddress()
txOrigin := sample.EthAddress()
tss := sample.Tss()

err = k.ProcessZetaSentEvent(ctx, event, emittingContract, txOrigin.Hex(), tss)
require.ErrorContains(t, err, "ProcessWithdrawalEvent: pay gas failed")
err = k.ProcessZetaSentEvent(ctx, event, emittingContract, txOrigin.Hex())
require.ErrorContains(t, err, "gas coin contract invalid address")
})

t.Run("unable to process ZetaSentEvent if process cctx fails", func(t *testing.T) {
Expand Down Expand Up @@ -658,9 +648,9 @@ func TestKeeper_ProcessZetaSentEvent(t *testing.T) {
require.NoError(t, err)
emittingContract := sample.EthAddress()
txOrigin := sample.EthAddress()
tss := sample.Tss()
err = k.ProcessZetaSentEvent(ctx, event, emittingContract, txOrigin.Hex(), tss)
require.ErrorContains(t, err, "ProcessWithdrawalEvent: update nonce failed")

err = k.ProcessZetaSentEvent(ctx, event, emittingContract, txOrigin.Hex())
require.ErrorContains(t, err, "nonce mismatch")
})
}

Expand Down
4 changes: 2 additions & 2 deletions x/crosschain/keeper/initiate_outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (k Keeper) InitiateOutbound(ctx sdk.Context, config InitiateOutboundConfig)
)
}

cctxGateway, found := ResolveCCTXGateway(chainInfo.CctxGateway)
cctxGateway, found := ResolveCCTXGateway(chainInfo.CctxGateway, k)
if !found {
return config.CCTX.CctxStatus.Status, cosmoserrors.Wrap(

Check warning on line 39 in x/crosschain/keeper/initiate_outbound.go

View check run for this annotation

Codecov / codecov/patch

x/crosschain/keeper/initiate_outbound.go#L39

Added line #L39 was not covered by tests
types.ErrInitiatitingOutbound,
Expand All @@ -44,5 +44,5 @@ func (k Keeper) InitiateOutbound(ctx sdk.Context, config InitiateOutboundConfig)
)
}

return cctxGateway.InitiateOutbound(ctx, config), nil
return cctxGateway.InitiateOutbound(ctx, config)
}

0 comments on commit 0ef2093

Please sign in to comment.