-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initialize cctx gateway interface (#2291)
- Loading branch information
Showing
25 changed files
with
388 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/zeta-chain/zetacore/x/crosschain/types" | ||
) | ||
|
||
// CCTXGatewayObservers is implementation of CCTXGateway interface for observers | ||
type CCTXGatewayObservers struct { | ||
crosschainKeeper Keeper | ||
} | ||
|
||
// NewCCTXGatewayObservers returns new instance of CCTXGatewayObservers | ||
func NewCCTXGatewayObservers(crosschainKeeper Keeper) CCTXGatewayObservers { | ||
return CCTXGatewayObservers{ | ||
crosschainKeeper: crosschainKeeper, | ||
} | ||
} | ||
|
||
/* | ||
InitiateOutbound updates the store so observers can use the PendingCCTX query: | ||
- If preprocessing of outbound is successful, the CCTX status is changed to PendingOutbound. | ||
- if preprocessing of outbound, such as paying the gas fee for the destination fails, the state is reverted to aborted | ||
We do not return an error from this function, as all changes need to be persisted to the state. | ||
Instead, we use a temporary context to make changes and then commit the context on for the happy path, i.e cctx is set to PendingOutbound. | ||
New CCTX status after preprocessing is returned. | ||
*/ | ||
func (c CCTXGatewayObservers) InitiateOutbound( | ||
ctx sdk.Context, | ||
cctx *types.CrossChainTx, | ||
) (newCCTXStatus types.CctxStatus) { | ||
tmpCtx, commit := ctx.CacheContext() | ||
outboundReceiverChainID := cctx.GetCurrentOutboundParam().ReceiverChainId | ||
err := func() error { | ||
err := c.crosschainKeeper.PayGasAndUpdateCctx( | ||
tmpCtx, | ||
outboundReceiverChainID, | ||
cctx, | ||
cctx.InboundParams.Amount, | ||
false, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
return c.crosschainKeeper.UpdateNonce(tmpCtx, outboundReceiverChainID, cctx) | ||
}() | ||
if err != nil { | ||
// do not commit anything here as the CCTX should be aborted | ||
cctx.SetAbort(err.Error()) | ||
return types.CctxStatus_Aborted | ||
} | ||
commit() | ||
cctx.SetPendingOutbound("") | ||
return types.CctxStatus_PendingOutbound | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/zeta-chain/zetacore/x/crosschain/types" | ||
) | ||
|
||
// CCTXGatewayZEVM is implementation of CCTXGateway interface for ZEVM | ||
type CCTXGatewayZEVM struct { | ||
crosschainKeeper Keeper | ||
} | ||
|
||
// NewCCTXGatewayZEVM returns new instance of CCTXGatewayZEVM | ||
func NewCCTXGatewayZEVM(crosschainKeeper Keeper) CCTXGatewayZEVM { | ||
return CCTXGatewayZEVM{ | ||
crosschainKeeper: crosschainKeeper, | ||
} | ||
} | ||
|
||
/* | ||
InitiateOutbound handles evm deposit and call ValidateOutbound. | ||
TODO (https://github.com/zeta-chain/node/issues/2278): move remaining of this comment to ValidateOutbound once it's added. | ||
- If the deposit is successful, the CCTX status is changed to OutboundMined. | ||
- If the deposit returns an internal error i.e if HandleEVMDeposit() returns an error, but isContractReverted is false, the CCTX status is changed to Aborted. | ||
- If the deposit is reverted, the function tries to create a revert cctx with status PendingRevert. | ||
- If the creation of revert tx also fails it changes the status to Aborted. | ||
Note : Aborted CCTXs are not refunded in this function. The refund is done using a separate refunding mechanism. | ||
We do not return an error from this function , as all changes need to be persisted to the state. | ||
Instead we use a temporary context to make changes and then commit the context on for the happy path ,i.e cctx is set to OutboundMined. | ||
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 | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
cosmoserrors "cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/zeta-chain/zetacore/pkg/chains" | ||
"github.com/zeta-chain/zetacore/x/crosschain/types" | ||
) | ||
|
||
// InitiateOutbound initiates the outbound for the CCTX depending on the CCTX gateway. | ||
// It does a conditional dispatch to correct CCTX gateway based on the receiver chain | ||
// which handles the state changes and error handling. | ||
func (k Keeper) InitiateOutbound(ctx sdk.Context, cctx *types.CrossChainTx) (types.CctxStatus, error) { | ||
receiverChainID := cctx.GetCurrentOutboundParam().ReceiverChainId | ||
chainInfo := chains.GetChainFromChainID(receiverChainID) | ||
if chainInfo == nil { | ||
return cctx.CctxStatus.Status, cosmoserrors.Wrap( | ||
types.ErrInitiatitingOutbound, | ||
fmt.Sprintf( | ||
"chain info not found for %d", receiverChainID, | ||
), | ||
) | ||
} | ||
|
||
cctxGateway, ok := k.cctxGateways[chainInfo.CctxGateway] | ||
if !ok { | ||
return cctx.CctxStatus.Status, cosmoserrors.Wrap( | ||
types.ErrInitiatitingOutbound, | ||
fmt.Sprintf( | ||
"CCTXGateway not defined for receiver chain %d", receiverChainID, | ||
), | ||
) | ||
} | ||
|
||
return cctxGateway.InitiateOutbound(ctx, cctx), nil | ||
} |
Oops, something went wrong.