diff --git a/x/crosschain/keeper/cctx_orchestrator.go b/x/crosschain/keeper/cctx_orchestrator.go index 9144cf6521..b57349c5a5 100644 --- a/x/crosschain/keeper/cctx_orchestrator.go +++ b/x/crosschain/keeper/cctx_orchestrator.go @@ -9,64 +9,73 @@ import ( ) func (k Keeper) ValidateOutboundZEVM(ctx sdk.Context, cctx *types.CrossChainTx, zevmError error, isContractReverted bool) (newCCTXStatus types.CctxStatus) { - _, commit := ctx.CacheContext() if zevmError != nil && !isContractReverted { // exceptional case; internal error; should abort CCTX cctx.SetAbort(zevmError.Error()) return types.CctxStatus_Aborted - } else if zevmError != nil && isContractReverted { + } + + if zevmError != nil && isContractReverted { // contract call reverted; should refund via a revert tx - revertMessage := zevmError.Error() - senderChain := k.zetaObserverKeeper.GetSupportedChainFromChainID(ctx, cctx.InboundParams.SenderChainId) - if senderChain == nil { - cctx.SetAbort(fmt.Sprintf("invalid sender chain id %d", cctx.InboundParams.SenderChainId)) - return types.CctxStatus_Aborted - } - gasLimit, err := k.GetRevertGasLimit(ctx, *cctx) + err := k.tryRevertOutbound(ctx, cctx, zevmError) if err != nil { - cctx.SetAbort(fmt.Sprintf("revert gas limit error: %s", err.Error())) + cctx.SetAbort(err.Error()) return types.CctxStatus_Aborted } - if gasLimit == 0 { - // use same gas limit of outbound as a fallback -- should not be required - gasLimit = cctx.GetCurrentOutboundParam().GasLimit - } - err = cctx.AddRevertOutbound(gasLimit) - if err != nil { - cctx.SetAbort(fmt.Sprintf("revert outbound error: %s", err.Error())) - return types.CctxStatus_Aborted - } - // we create a new cached context, and we don't commit the previous one with EVM deposit - tmpCtxRevert, commitRevert := ctx.CacheContext() - err = func() error { - err := k.PayGasAndUpdateCctx( - tmpCtxRevert, - senderChain.ChainId, - cctx, - cctx.InboundParams.Amount, - false, - ) - if err != nil { - return err - } - // Update nonce using senderchain id as this is a revert tx and would go back to the original sender - return k.UpdateNonce(tmpCtxRevert, senderChain.ChainId, cctx) - }() - if err != nil { - cctx.SetAbort(fmt.Sprintf("deposit revert message: %s err : %s", revertMessage, err.Error())) - return types.CctxStatus_Aborted - } - commitRevert() - cctx.SetPendingRevert(revertMessage) return types.CctxStatus_PendingRevert } - // successful HandleEVMDeposit; + _, commit := ctx.CacheContext() commit() cctx.SetOutBoundMined("Remote omnichain contract call completed") return types.CctxStatus_OutboundMined } +func (k Keeper) tryRevertOutbound(ctx sdk.Context, cctx *types.CrossChainTx, zevmError error) error { + senderChain := k.zetaObserverKeeper.GetSupportedChainFromChainID(ctx, cctx.InboundParams.SenderChainId) + if senderChain == nil { + return fmt.Errorf("invalid sender chain id %d", cctx.InboundParams.SenderChainId) + } + // use same gas limit of outbound as a fallback -- should not be required + gasLimit, err := k.GetRevertGasLimit(ctx, *cctx) + if err != nil { + return fmt.Errorf("revert gas limit error: %s", err.Error()) + } + if gasLimit == 0 { + gasLimit = cctx.GetCurrentOutboundParam().GasLimit + } + + revertMessage := zevmError.Error() + err = cctx.AddRevertOutbound(gasLimit) + if err != nil { + return fmt.Errorf("revert outbound error: %s", err.Error()) + } + + // we create a new cached context, and we don't commit the previous one with EVM deposit + tmpCtxRevert, commitRevert := ctx.CacheContext() + err = func() error { + err := k.PayGasAndUpdateCctx( + tmpCtxRevert, + senderChain.ChainId, + cctx, + cctx.InboundParams.Amount, + false, + ) + if err != nil { + return err + } + + // update nonce using senderchain id as this is a revert tx and would go back to the original sender + return k.UpdateNonce(tmpCtxRevert, senderChain.ChainId, cctx) + }() + if err != nil { + return fmt.Errorf("deposit revert message: %s err : %s", revertMessage, err.Error()) + } + commitRevert() + cctx.SetPendingRevert(revertMessage) + return nil +} + func (k Keeper) ValidateOutboundObservers(ctx sdk.Context, cctx *types.CrossChainTx, ballotStatus observertypes.BallotStatus, valueReceived string) error { tmpCtx, commit := ctx.CacheContext() err := func() error {