Skip to content

Commit

Permalink
add e2e test for revert
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Apr 16, 2024
1 parent a7623fc commit 1b81398
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 52 deletions.
36 changes: 19 additions & 17 deletions x/crosschain/keeper/cctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
12 changes: 1 addition & 11 deletions x/crosschain/keeper/evm_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 11 additions & 7 deletions x/crosschain/keeper/msg_server_vote_inbound_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
17 changes: 1 addition & 16 deletions x/crosschain/keeper/process_inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion zetaclient/evm/evm_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Check failure on line 465 in zetaclient/evm/evm_signer.go

View workflow job for this annotation

GitHub Actions / build-and-test

(*github.com/rs/zerolog.Event).Msgf call has arguments but no formatting directives

Check failure on line 465 in zetaclient/evm/evm_signer.go

View workflow job for this annotation

GitHub Actions / build-and-test

(*github.com/rs/zerolog.Event).Msgf call has arguments but no formatting directives

Check failure on line 465 in zetaclient/evm/evm_signer.go

View workflow job for this annotation

GitHub Actions / lint

printf: (*github.com/rs/zerolog.Event).Msgf call has arguments but no formatting directives (govet)
}
// Try to broadcast transaction
if tx != nil {
outTxHash := tx.Hash().Hex()
Expand Down

0 comments on commit 1b81398

Please sign in to comment.