Skip to content

Commit

Permalink
move code to validate outbound methods
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Jun 4, 2024
1 parent 833a253 commit 4abefdb
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 61 deletions.
62 changes: 2 additions & 60 deletions x/crosschain/keeper/cctx_gateway_zevm.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package keeper

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/zeta-chain/zetacore/x/crosschain/types"
Expand Down Expand Up @@ -38,63 +36,7 @@ Instead we use a temporary context to make changes and then commit the context o
New CCTX status after preprocessing is returned.
*/
func (c CCTXGatewayZEVM) InitiateOutbound(ctx sdk.Context, cctx *types.CrossChainTx) (newCCTXStatus types.CctxStatus) {
tmpCtx, commit := ctx.CacheContext()
isContractReverted, err := c.crosschainKeeper.HandleEVMDeposit(tmpCtx, cctx)

// TODO (https://github.com/zeta-chain/node/issues/2278): further processing will be in validateOutbound(...), for now keeping it here
if err != nil && !isContractReverted {
// exceptional case; internal error; should abort CCTX
cctx.SetAbort(err.Error())
return types.CctxStatus_Aborted
} else if err != nil && isContractReverted {
// contract call reverted; should refund via a revert tx
revertMessage := err.Error()
senderChain := c.crosschainKeeper.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 := c.crosschainKeeper.GetRevertGasLimit(ctx, *cctx)
if err != nil {
cctx.SetAbort(fmt.Sprintf("revert gas limit error: %s", 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
}
isContractReverted, err := c.crosschainKeeper.HandleEVMDeposit(ctx, cctx)

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 := c.crosschainKeeper.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 c.crosschainKeeper.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()
cctx.SetOutBoundMined("Remote omnichain contract call completed")
return types.CctxStatus_OutboundMined
return c.crosschainKeeper.ValidateOutboundZEVM(ctx, cctx, err, isContractReverted)
}
93 changes: 93 additions & 0 deletions x/crosschain/keeper/cctx_orchestrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package keeper

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/zeta-chain/zetacore/x/crosschain/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
)

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 {
// 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)
if err != nil {
cctx.SetAbort(fmt.Sprintf("revert gas limit error: %s", 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()
cctx.SetOutBoundMined("Remote omnichain contract call completed")
return types.CctxStatus_OutboundMined
}

func (k Keeper) ValidateOutboundObservers(ctx sdk.Context, cctx *types.CrossChainTx, ballotStatus observertypes.BallotStatus, valueReceived string) error {
tmpCtx, commit := ctx.CacheContext()
err := func() error {
switch ballotStatus {
case observertypes.BallotStatus_BallotFinalized_SuccessObservation:
k.ProcessSuccessfulOutbound(tmpCtx, cctx, valueReceived)
case observertypes.BallotStatus_BallotFinalized_FailureObservation:
err := k.ProcessFailedOutbound(tmpCtx, cctx, valueReceived)
if err != nil {
return err
}
}
return nil
}()
if err != nil {
return err
}
err = cctx.Validate()
if err != nil {
return err
}
commit()
return nil
}
2 changes: 1 addition & 1 deletion x/crosschain/keeper/msg_server_vote_outbound_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (k msgServer) VoteOutbound(
// Fund the gas stability pool with the remaining funds
k.FundStabilityPool(ctx, &cctx)

err = k.ProcessOutbound(ctx, &cctx, ballot.BallotStatus, msg.ValueReceived.String())
err = k.ValidateOutboundObservers(ctx, &cctx, ballot.BallotStatus, msg.ValueReceived.String())
if err != nil {
k.SaveFailedOutbound(ctx, &cctx, err.Error(), ballotIndex)
return &types.MsgVoteOutboundResponse{}, nil
Expand Down

0 comments on commit 4abefdb

Please sign in to comment.