Skip to content

Commit

Permalink
add revert support
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Aug 13, 2024
1 parent 43467c4 commit a2164a5
Show file tree
Hide file tree
Showing 23 changed files with 512 additions and 211 deletions.
3 changes: 3 additions & 0 deletions docs/openapi/openapi.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57561,6 +57561,9 @@ definitions:
type: boolean
abort_address:
type: string
revert_message:
type: string
format: byte
title: RevertOptions represents the options for reverting a cctx
crosschainTxFinalizationStatus:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ func TestV2ETHWithdrawAndCallRevertWithCall(r *runner.E2ERunner, args []string)
r.Logger.CCTX(*cctx, "withdraw")
require.Equal(r, crosschaintypes.CctxStatus_Reverted, cctx.CctxStatus.Status)

r.AssertTestDAppZEVMCalled(true, payloadMessageWithdrawOnRevertETH, amount)
r.AssertTestDAppZEVMCalled(true, payloadMessageWithdrawOnRevertETH, big.NewInt(0))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/rakyll/statik v0.1.7
github.com/zeta-chain/keystone/keys v0.0.0-20231105174229-903bc9405da2
github.com/zeta-chain/protocol-contracts v1.0.2-athens3.0.20240813115206-2d79414c9ec0
github.com/zeta-chain/protocol-contracts v1.0.2-athens3.0.20240813155303-bc9246d9c711
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0
gopkg.in/yaml.v2 v2.4.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1673,8 +1673,8 @@ github.com/zeta-chain/go-tss v0.0.0-20240729195411-9f5ae8189449 h1:4U+4g2QQjbrme
github.com/zeta-chain/go-tss v0.0.0-20240729195411-9f5ae8189449/go.mod h1:LN1IBRN8xQkKgdgLhl5BDGZyPm70QOTbVLejdS2FVpo=
github.com/zeta-chain/keystone/keys v0.0.0-20231105174229-903bc9405da2 h1:gd2uE0X+ZbdFJ8DubxNqLbOVlCB12EgWdzSNRAR82tM=
github.com/zeta-chain/keystone/keys v0.0.0-20231105174229-903bc9405da2/go.mod h1:x7Bkwbzt2W2lQfjOirnff0Dj+tykdbTG1FMJPVPZsvE=
github.com/zeta-chain/protocol-contracts v1.0.2-athens3.0.20240813115206-2d79414c9ec0 h1:Wp67+TdHrHaetpc2HANrIR/IIcvqK+uykW/3V/m9kVw=
github.com/zeta-chain/protocol-contracts v1.0.2-athens3.0.20240813115206-2d79414c9ec0/go.mod h1:SjT7QirtJE8stnAe1SlNOanxtfSfijJm3MGJ+Ax7w7w=
github.com/zeta-chain/protocol-contracts v1.0.2-athens3.0.20240813155303-bc9246d9c711 h1:0PW6j5EDKsR0+LjGZsz3UVrOPk14iUgmwKP2J633388=
github.com/zeta-chain/protocol-contracts v1.0.2-athens3.0.20240813155303-bc9246d9c711/go.mod h1:SjT7QirtJE8stnAe1SlNOanxtfSfijJm3MGJ+Ax7w7w=
github.com/zondax/hid v0.9.0/go.mod h1:l5wttcP0jwtdLjqjMMWFVEE7d1zO0jvSPA9OPZxWpEM=
github.com/zondax/hid v0.9.2 h1:WCJFnEDMiqGF64nlZz28E9qLVZ0KSJ7xpc5DLEyma2U=
github.com/zondax/hid v0.9.2/go.mod h1:l5wttcP0jwtdLjqjMMWFVEE7d1zO0jvSPA9OPZxWpEM=
Expand Down
1 change: 1 addition & 0 deletions proto/zetachain/zetacore/crosschain/cross_chain_tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ message RevertOptions {
string revert_address = 1;
bool call_on_revert = 2;
string abort_address = 3;
bytes revert_message = 4;
}

message CrossChainTx {
Expand Down
18 changes: 18 additions & 0 deletions testutil/keeper/mocks/crosschain/fungible.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ export declare class RevertOptions extends Message<RevertOptions> {
*/
abortAddress: string;

/**
* @generated from field: bytes revert_message = 4;
*/
revertMessage: Uint8Array;

constructor(data?: PartialMessage<RevertOptions>);

static readonly runtime: typeof proto3;
Expand Down
21 changes: 16 additions & 5 deletions x/crosschain/keeper/cctx_orchestrator_validate_outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
tmtypes "github.com/cometbft/cometbft/types"
sdk "github.com/cosmos/cosmos-sdk/types"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"

"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/coin"
Expand Down Expand Up @@ -118,7 +119,7 @@ func (k Keeper) processFailedOutboundObservers(ctx sdk.Context, cctx *types.Cros
// For all other transactions we need to create a revert tx and set the status to pending revert

if cctx.ProtocolContractVersion == types.ProtocolContractVersion_V2 {
return processFailedOutboundV2(ctx, cctx)
return k.processFailedOutboundV2(ctx, cctx)
}

if cctx.InboundParams.CoinType == coin.CoinType_Cmd {
Expand Down Expand Up @@ -311,23 +312,33 @@ func (k Keeper) processFailedZETAOutboundOnZEVM(ctx sdk.Context, cctx *types.Cro
// - all coin type use the same workflow
// TODO: consolidate logic with above function
// https://github.com/zeta-chain/node/issues/2627
func processFailedOutboundV2(ctx sdk.Context, cctx *types.CrossChainTx) error {
func (k Keeper) processFailedOutboundV2(ctx sdk.Context, cctx *types.CrossChainTx) error {
switch cctx.CctxStatus.Status {
case types.CctxStatus_PendingOutbound:

// add revert outbound
err := cctx.AddRevertOutbound(fungiblekeeper.ZEVMGasLimitDepositAndCall.Uint64())
if err != nil {
// Return err to save the failed outbound ad set to aborted
return fmt.Errorf("failed AddRevertOutbound: %s", err.Error())
return errors.Wrap(err, "failed AddRevertOutbound")
}

// update status
cctx.SetPendingRevert("Outbound failed, trying revert")

// process the revert on ZEVM
// TODO: add ProcessV2RevertDeposit call
// https://github.com/zeta-chain/node/issues/2660
if err := k.fungibleKeeper.ProcessV2RevertDeposit(
ctx,
cctx.GetCurrentOutboundParam().Amount.BigInt(),
cctx.GetCurrentOutboundParam().ReceiverChainId,
cctx.InboundParams.CoinType,
cctx.InboundParams.Asset,
ethcommon.HexToAddress(cctx.GetCurrentOutboundParam().Receiver),
cctx.RevertOptions.CallOnRevert,
cctx.RevertOptions.RevertMessage,
); err != nil {
return errors.Wrap(err, "failed ProcessV2RevertDeposit")
}

// tx is reverted
cctx.SetReverted("Outbound failed, revert executed")
Expand Down
30 changes: 15 additions & 15 deletions x/crosschain/keeper/v2_zevm_inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ func (k Keeper) parseGatewayCallEvent(
// currently inbound data is represented with a MsgVoteInbound message
// TODO: replace with a more appropriate object
// https://github.com/zeta-chain/node/issues/2658
// TODO: include revert options
// https://github.com/zeta-chain/node/issues/2660
func (k Keeper) newWithdrawalInbound(
ctx sdk.Context,
from ethcommon.Address,
Expand All @@ -183,13 +181,8 @@ func (k Keeper) newWithdrawalInbound(
return nil, errors.Wrapf(err, "cannot encode address %v", event.Receiver)
}

// temporary hardcoded gas limit for withdraw and call
// TODO: use gas limit specified by user
// https://github.com/zeta-chain/node/issues/2668
gasLimit := uint64(1000000)

// for simple withdraw without call, we use the specified gas limit in the zrc20 contract
if len(event.Message) == 0 {
gasLimit := event.GasLimit.Uint64()
if gasLimit == 0 {
gasLimitQueried, err := k.fungibleKeeper.QueryGasLimit(
ctx,
ethcommon.HexToAddress(foreignCoin.Zrc20ContractAddress),
Expand All @@ -216,15 +209,14 @@ func (k Keeper) newWithdrawalInbound(
foreignCoin.Asset,
event.Raw.Index,
types.ProtocolContractVersion_V2,
types.WithZEVMRevertOptions(event.RevertOptions),
), nil
}

// newCallInbound creates a new inbound object for a call
// currently inbound data is represented with a MsgVoteInbound message
// TODO: replace with a more appropriate object
// https://github.com/zeta-chain/node/issues/2658
// TODO: include revert options
// https://github.com/zeta-chain/node/issues/2660
func (k Keeper) newCallInbound(
ctx sdk.Context,
from ethcommon.Address,
Expand All @@ -251,10 +243,17 @@ func (k Keeper) newCallInbound(
return nil, errors.Wrapf(err, "cannot encode address %v", event.Receiver)
}

// temporary hardcoded gas limit for withdraw and call
// TODO: use gas limit specified by user
// https://github.com/zeta-chain/node/issues/2668
gasLimit := uint64(1000000)
gasLimit := event.GasLimit.Uint64()
if gasLimit == 0 {
gasLimitQueried, err := k.fungibleKeeper.QueryGasLimit(
ctx,
ethcommon.HexToAddress(foreignCoin.Zrc20ContractAddress),
)
if err != nil {
return nil, errors.Wrap(err, "cannot query gas limit")
}
gasLimit = gasLimitQueried.Uint64()
}

return types.NewMsgVoteInbound(
"",
Expand All @@ -272,5 +271,6 @@ func (k Keeper) newCallInbound(
"",
event.Raw.Index,
types.ProtocolContractVersion_V2,
types.WithZEVMRevertOptions(event.RevertOptions),
), nil
}
14 changes: 13 additions & 1 deletion x/crosschain/types/cctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,19 @@ func (m *CrossChainTx) AddRevertOutbound(gasLimit uint64) error {
return fmt.Errorf("cannot revert before trying to process an outbound tx")
}

// in protocol contract V2, developers can specify a specific address to receive the revert
// if not specified, the sender address is used
// note: this option is current only support for EVM type chains
revertReceiver := m.InboundParams.Sender
if m.ProtocolContractVersion == ProtocolContractVersion_V2 {
revertAddress, valid := m.RevertOptions.GetEVMRevertAddress()
if valid {
revertReceiver = revertAddress.Hex()
}
}

revertTxParams := &OutboundParams{
Receiver: m.InboundParams.Sender,
Receiver: revertReceiver,
ReceiverChainId: m.InboundParams.SenderChainId,
Amount: m.GetCurrentOutboundParam().Amount,
GasLimit: gasLimit,
Expand Down Expand Up @@ -242,6 +253,7 @@ func NewCCTX(ctx sdk.Context, msg MsgVoteInbound, tssPubkey string) (CrossChainT
InboundParams: inboundParams,
OutboundParams: []*OutboundParams{outboundParams},
ProtocolContractVersion: msg.ProtocolContractVersion,
RevertOptions: msg.RevertOptions,
}

// TODO: remove this validate call
Expand Down
Loading

0 comments on commit a2164a5

Please sign in to comment.