From 1b813985b67dc8df7401dadade6d97a18feb92f0 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Tue, 16 Apr 2024 11:46:27 -0400 Subject: [PATCH] add e2e test for revert --- x/crosschain/keeper/cctx.go | 36 ++++++++++--------- x/crosschain/keeper/evm_deposit.go | 12 +------ .../keeper/msg_server_vote_inbound_tx.go | 18 ++++++---- x/crosschain/keeper/process_inbound.go | 17 +-------- zetaclient/evm/evm_signer.go | 9 ++++- 5 files changed, 40 insertions(+), 52 deletions(-) diff --git a/x/crosschain/keeper/cctx.go b/x/crosschain/keeper/cctx.go index 65e9af850b..17396d53bf 100644 --- a/x/crosschain/keeper/cctx.go +++ b/x/crosschain/keeper/cctx.go @@ -31,28 +31,30 @@ func (k Keeper) SetCctxAndNonceToCctxAndInTxHashToCctx(ctx sdk.Context, cctx typ }) } if cctx.InboundTxParams.CoinType == coin.CoinType_Zeta && cctx.CctxStatus.Status != types.CctxStatus_OutboundMined { - ctx.Logger().Error(fmt.Sprintf("set mapping nonce => cctxIndex: cctx: %s", cctx.Index)) + ctx.Logger().Error(fmt.Sprintf("SetNonceToCctx: cctx: %s", cctx.Index)) } k.SetCrossChainTx(ctx, cctx) if cctx.InboundTxParams.CoinType == coin.CoinType_Zeta && cctx.CctxStatus.Status != types.CctxStatus_OutboundMined { - ctx.Logger().Error(fmt.Sprintf("set cctx: cctx: %s", cctx.Index)) + ctx.Logger().Error(fmt.Sprintf("SetCrossChainTx: cctx: %s", cctx.Index)) + } + //set mapping inTxHash -> cctxIndex + in, _ := k.GetInTxHashToCctx(ctx, cctx.InboundTxParams.InboundTxObservedHash) + in.InTxHash = cctx.InboundTxParams.InboundTxObservedHash + found = false + for _, cctxIndex := range in.CctxIndex { + if cctxIndex == cctx.Index { + found = true + break + } + } + if !found { + in.CctxIndex = append(in.CctxIndex, cctx.Index) + } + k.SetInTxHashToCctx(ctx, in) + if cctx.InboundTxParams.CoinType == coin.CoinType_Zeta && cctx.CctxStatus.Status != types.CctxStatus_OutboundMined { + ctx.Logger().Error(fmt.Sprintf("SetInTxHashToCctx: cctx: %s", cctx.Index)) } - // set mapping inTxHash -> cctxIndex - //in, _ := k.GetInTxHashToCctx(ctx, cctx.InboundTxParams.InboundTxObservedHash) - //in.InTxHash = cctx.InboundTxParams.InboundTxObservedHash - //found = false - //for _, cctxIndex := range in.CctxIndex { - // if cctxIndex == cctx.Index { - // found = true - // break - // } - //} - //if !found { - // in.CctxIndex = append(in.CctxIndex, cctx.Index) - //} - //k.SetInTxHashToCctx(ctx, in) - // //if cctx.InboundTxParams.CoinType == coin.CoinType_Zeta && cctx.CctxStatus.Status != types.CctxStatus_OutboundMined { // ctx.Logger().Error(fmt.Sprintf("found tss: cctx: %s", cctx.Index)) // ctx.Logger().Error(fmt.Sprintf("found tss: tss: %s", tss.TssPubkey)) diff --git a/x/crosschain/keeper/evm_deposit.go b/x/crosschain/keeper/evm_deposit.go index fe20b9b61e..1d4684f744 100644 --- a/x/crosschain/keeper/evm_deposit.go +++ b/x/crosschain/keeper/evm_deposit.go @@ -47,22 +47,12 @@ func (k Keeper) HandleEVMDeposit(ctx sdk.Context, cctx *types.CrossChainTx) (boo if err != nil { return false, errors.Wrap(types.ErrUnableToDecodeMessageString, err.Error()) } - ctx.Logger().Info(fmt.Sprintf("HandleEVMDeposit: Zeta deposit to ZEVM cctx: %s ", cctx.Index)) // if coin type is Zeta, this is a deposit ZETA to zEVM cctx. evmTxResponse, err := k.fungibleKeeper.ZEVMDepositAndCallContract(ctx, sender, to, inboundSenderChainID, inboundAmount, data, indexBytes) - if evmTxResponse != nil { - ctx.Logger().Error(fmt.Sprintf("HandleEVMDeposit: ZEVMDepositAndCallContract response: %s ", evmTxResponse.String())) - ctx.Logger().Error(fmt.Sprintf("HandleEVMDeposit: ZEVMDepositAndCallContract response: %s", evmTxResponse.Logs)) - } - if err != nil { - ctx.Logger().Error(fmt.Sprintf("HandleEVMDeposit: ZEVMDepositAndCallContract error: %s", err.Error())) - } - if fungibletypes.IsContractReverted(evmTxResponse, err) || errShouldRevertCctx(err) { - ctx.Logger().Error(fmt.Sprintf("HandleEVMDeposit: ZEVMDepositAndCallContract reverted and error: %s", err.Error())) + ctx.Logger().Error(fmt.Sprintf("####HandleEVMDeposit: ZEVMDepositAndCallContract reverted and error: %s", err.Error())) return true, err // contract reverted; should refunding automatically } else if err != nil { - ctx.Logger().Error(fmt.Sprintf("HandleEVMDeposit: ZEVMDepositAndCallContract not reverted but has error: %s", err.Error())) return false, err // internal error; should abort } } else { diff --git a/x/crosschain/keeper/msg_server_vote_inbound_tx.go b/x/crosschain/keeper/msg_server_vote_inbound_tx.go index 14ec45b947..3378720bed 100644 --- a/x/crosschain/keeper/msg_server_vote_inbound_tx.go +++ b/x/crosschain/keeper/msg_server_vote_inbound_tx.go @@ -116,19 +116,23 @@ func (k msgServer) VoteOnObservedInboundTx(goCtx context.Context, msg *types.Msg func (k Keeper) SaveInbound(ctx sdk.Context, cctx *types.CrossChainTx, eventIndex uint64) { if cctx.InboundTxParams.CoinType == coin.CoinType_Zeta && cctx.CctxStatus.Status != types.CctxStatus_OutboundMined { - ctx.Logger().Info(fmt.Sprintf("SaveInbound: cctx: %s", cctx.Index)) + ctx.Logger().Info(fmt.Sprintf("SaveInbound Before: cctx: %s", cctx.Index)) } + + cctx.InboundTxParams.InboundTxFinalizedZetaHeight = uint64(ctx.BlockHeight()) + cctx.InboundTxParams.TxFinalizationStatus = types.TxFinalizationStatus_Executed + k.SetCctxAndNonceToCctxAndInTxHashToCctx(ctx, *cctx) + + if cctx.InboundTxParams.CoinType == coin.CoinType_Zeta && cctx.CctxStatus.Status != types.CctxStatus_OutboundMined { + ctx.Logger().Info(fmt.Sprintf("SaveInbound After: cctx: %s", cctx.Index)) + } + EmitEventInboundFinalized(ctx, cctx) k.AddFinalizedInbound(ctx, cctx.GetInboundTxParams().InboundTxObservedHash, cctx.GetInboundTxParams().SenderChainId, eventIndex) // #nosec G701 always positive - cctx.InboundTxParams.InboundTxFinalizedZetaHeight = uint64(ctx.BlockHeight()) - cctx.InboundTxParams.TxFinalizationStatus = types.TxFinalizationStatus_Executed + k.RemoveInTxTrackerIfExists(ctx, cctx.InboundTxParams.SenderChainId, cctx.InboundTxParams.InboundTxObservedHash) - if cctx.InboundTxParams.CoinType == coin.CoinType_Zeta && cctx.CctxStatus.Status != types.CctxStatus_OutboundMined { - ctx.Logger().Info(fmt.Sprintf("SaveInbound: cctx: %s", cctx.Index)) - } - k.SetCctxAndNonceToCctxAndInTxHashToCctx(ctx, *cctx) } diff --git a/x/crosschain/keeper/process_inbound.go b/x/crosschain/keeper/process_inbound.go index 980867cf85..2d5ee6205e 100644 --- a/x/crosschain/keeper/process_inbound.go +++ b/x/crosschain/keeper/process_inbound.go @@ -46,32 +46,19 @@ func (k Keeper) processZEVMDeposit(ctx sdk.Context, cctx *types.CrossChainTx) { } gasLimit, err := k.GetRevertGasLimit(ctx, *cctx) if err != nil { - ctx.Logger().Error(fmt.Sprintf("HandleEVMDeposit: ZEVMDepositAndCallContract Error in GetRevertGasLimit: %s", cctx.Index)) cctx.SetAbort(fmt.Sprintf("revert gas limit error: %s", err.Error())) return } if gasLimit == 0 { - // use same gas limit of outbound as a fallback -- should not happen + // use same gas limit of outbound as a fallback -- should not be required gasLimit = cctx.GetCurrentOutTxParam().OutboundTxGasLimit } - ctx.Logger().Error(fmt.Sprintf("HandleEVMDeposit: ZEVMDepositAndCallContract Before Adding Revert: %d", len(cctx.OutboundTxParams))) - err = cctx.AddRevertOutbound(gasLimit) if err != nil { cctx.SetAbort(fmt.Sprintf("revert outbound error: %s", err.Error())) return } - - ctx.Logger().Error(fmt.Sprintf("HandleEVMDeposit: ZEVMDepositAndCallContract Add revert: %s", cctx.Index)) - ctx.Logger().Error(fmt.Sprintf("HandleEVMDeposit: ZEVMDepositAndCallContract After Adding Revert: %d", len(cctx.OutboundTxParams))) - ctx.Logger().Error(fmt.Sprintf("HandleEVMDeposit: ZEVMDepositAndCallContract Receiver: %s", cctx.GetCurrentOutTxParam().Receiver)) - ctx.Logger().Error(fmt.Sprintf("HandleEVMDeposit: ZEVMDepositAndCallContract ReceiverChainId: %d", cctx.GetCurrentOutTxParam().ReceiverChainId)) - ctx.Logger().Error(fmt.Sprintf("HandleEVMDeposit: ZEVMDepositAndCallContract Amount: %s", cctx.GetCurrentOutTxParam().Amount.String())) - - ctx.Logger().Error(fmt.Sprintf("HandleEVMDeposit: ZEVMDepositAndCallContract Receiver Original: %s", cctx.OutboundTxParams[0].Receiver)) - ctx.Logger().Error(fmt.Sprintf("HandleEVMDeposit: ZEVMDepositAndCallContract ReceiverChainId Original: %d", cctx.OutboundTxParams[0].ReceiverChainId)) - // we create a new cached context, and we don't commit the previous one with EVM deposit tmpCtxRevert, commitRevert := ctx.CacheContext() err = func() error { @@ -83,7 +70,6 @@ func (k Keeper) processZEVMDeposit(ctx sdk.Context, cctx *types.CrossChainTx) { false, ) if err != nil { - ctx.Logger().Error(fmt.Sprintf("HandleEVMDeposit: ZEVMDepositAndCallContract error in PayGasAndUpdateCctx: %s", cctx.Index)) return err } // Update nonce using senderchain id as this is a revert tx and would go back to the original sender @@ -95,7 +81,6 @@ func (k Keeper) processZEVMDeposit(ctx sdk.Context, cctx *types.CrossChainTx) { } commitRevert() cctx.SetPendingRevert(revertMessage) - ctx.Logger().Error(fmt.Sprintf("HandleEVMDeposit: ZEVMDepositAndCallContract set to pending revert: %s", cctx.Index)) return } // successful HandleEVMDeposit; diff --git a/zetaclient/evm/evm_signer.go b/zetaclient/evm/evm_signer.go index e077d19fe2..9dd86b9f73 100644 --- a/zetaclient/evm/evm_signer.go +++ b/zetaclient/evm/evm_signer.go @@ -410,6 +410,11 @@ func (signer *Signer) TryProcessOutTx( } } else if cctx.CctxStatus.Status == types.CctxStatus_PendingRevert && cctx.OutboundTxParams[0].ReceiverChainId == zetaBridge.ZetaChain().ChainId { switch cctx.InboundTxParams.CoinType { + case coin.CoinType_Zeta: + logger.Info().Msgf("SignRevertTx: %d => %s, nonce %d, gasPrice %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, txData.gasPrice) + txData.srcChainID = big.NewInt(cctx.OutboundTxParams[0].ReceiverChainId) + txData.toChainID = big.NewInt(cctx.GetCurrentOutTxParam().ReceiverChainId) + tx, err = signer.SignRevertTx(txData) case coin.CoinType_Gas: logger.Info().Msgf("SignWithdrawTx: %d => %s, nonce %d, gasPrice %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, txData.gasPrice) tx, err = signer.SignWithdrawTx(txData) @@ -456,7 +461,9 @@ func (signer *Signer) BroadcastOutTx( txData *OutBoundTransactionData) { // Get destination chain for logging toChain := chains.GetChainFromChainID(txData.toChainID.Int64()) - + if tx == nil { + logger.Warn().Msgf("BroadcastOutTx: no tx to broadcast", cctx.Index) + } // Try to broadcast transaction if tx != nil { outTxHash := tx.Hash().Hex()