Skip to content

Commit

Permalink
feat: divide messages into status and error
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Oct 2, 2024
1 parent 2580332 commit 64217ae
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 247 deletions.
2 changes: 1 addition & 1 deletion docs/cli/zetacored/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -9441,7 +9441,7 @@ zetacored tx crosschain vote-gas-price [chain] [price] [priorityFee] [blockNumbe
Broadcast message to vote an inbound

```
zetacored tx crosschain vote-inbound [sender] [senderChainID] [txOrigin] [receiver] [receiverChainID] [amount] [message] [inboundHash] [inBlockHeight] [coinType] [asset] [eventIndex] [protocolContractVersion] [isArbitraryCall] [flags]
zetacored tx crosschain vote-inbound [sender] [senderChainID] [txOrigin] [receiver] [receiverChainID] [amount] [message] [inboundHash] [inBlockHeight] [coinType] [asset] [eventIndex] [protocolContractVersion] [flags]
```

### Options
Expand Down
2 changes: 2 additions & 0 deletions docs/openapi/openapi.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58446,6 +58446,8 @@ definitions:
$ref: '#/definitions/crosschainCctxStatus'
status_message:
type: string
error_message:
type: string
lastUpdate_timestamp:
type: string
format: int64
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ require (
github.com/bnb-chain/tss-lib v1.5.0
github.com/showa-93/go-mask v0.6.2
github.com/tonkeeper/tongo v1.9.3
gotest.tools v2.2.0+incompatible
)

require (
Expand Down
2 changes: 1 addition & 1 deletion proto/zetachain/zetacore/crosschain/cross_chain_tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ message OutboundParams {
message Status {
CctxStatus status = 1;
string status_message = 2;
string error_message = 6;
int64 lastUpdate_timestamp = 3;
bool isAbortRefunded = 4;
// when the CCTX was created. only populated on new transactions.
Expand Down Expand Up @@ -135,5 +136,4 @@ message CrossChainTx {
repeated OutboundParams outbound_params = 10;
ProtocolContractVersion protocol_contract_version = 11;
RevertOptions revert_options = 12 [ (gogoproto.nullable) = false ];
string error = 13;
}
1 change: 0 additions & 1 deletion testutil/sample/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ func CrossChainTx(t *testing.T, index string) *types.CrossChainTx {
OutboundParams: []*types.OutboundParams{OutboundParams(r), OutboundParams(r)},
ProtocolContractVersion: types.ProtocolContractVersion_V1,
RevertOptions: types.NewEmptyRevertOptions(),
Error: "",
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ export declare class Status extends Message<Status> {
*/
statusMessage: string;

/**
* @generated from field: string error_message = 6;
*/
errorMessage: string;

/**
* @generated from field: int64 lastUpdate_timestamp = 3;
*/
Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/keeper/cctx_gateway_observers.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (c CCTXGatewayObservers) InitiateOutbound(
}()
if err != nil {
// do not commit anything here as the CCTX should be aborted
config.CCTX.SetAbort(true, err.Error())
config.CCTX.SetAbort("", err.Error())
return types.CctxStatus_Aborted, err
}
commit()
Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/keeper/cctx_gateway_zevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (c CCTXGatewayZEVM) InitiateOutbound(

if err != nil && !isContractReverted {
// exceptional case; internal error; should abort CCTX
config.CCTX.SetAbort(true, err.Error())
config.CCTX.SetAbort("", err.Error())
return types.CctxStatus_Aborted, err
}

Expand Down
24 changes: 12 additions & 12 deletions x/crosschain/keeper/cctx_orchestrator_validate_outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (k Keeper) ValidateOutboundZEVM(
cctx.InboundParams.Amount,
)
if err != nil {
cctx.SetAbort(true, fmt.Sprintf("%s : %s", depositErr, err.Error()))
cctx.SetAbort("", fmt.Sprintf("deposit error %s, got error: %s", depositErr, err.Error()))
return types.CctxStatus_Aborted
}

Expand Down Expand Up @@ -122,7 +122,7 @@ func (k Keeper) processFailedOutboundObservers(ctx sdk.Context, cctx *types.Cros
if cctx.InboundParams.CoinType == coin.CoinType_Cmd {
// if the cctx is of coin type cmd or the sender chain is zeta chain, then we do not revert, the cctx is aborted
cctx.GetCurrentOutboundParam().TxFinalizationStatus = types.TxFinalizationStatus_Executed
cctx.SetAbort(true, "Outbound failed: cmd cctx reverted")
cctx.SetAbort("", "outbound failed, cmd cctx reverted")
} else if chains.IsZetaChain(cctx.InboundParams.SenderChainId, k.GetAuthorityKeeper().GetAdditionalChainList(ctx)) {
switch cctx.InboundParams.CoinType {
// Try revert if the coin-type is ZETA
Expand All @@ -137,7 +137,7 @@ func (k Keeper) processFailedOutboundObservers(ctx sdk.Context, cctx *types.Cros
default:
{
cctx.GetCurrentOutboundParam().TxFinalizationStatus = types.TxFinalizationStatus_Executed
cctx.SetAbort(true, "Outbound failed for non-ZETA cctx")
cctx.SetAbort("", "outbound failed for non-ZETA cctx")
}
}
} else {
Expand Down Expand Up @@ -195,10 +195,10 @@ func (k Keeper) processFailedOutboundOnExternalChain(
return err
}
// Not setting the finalization status here, the required changes have been made while creating the revert tx
cctx.SetPendingRevert(true, revertMsg)
cctx.SetPendingRevert("", revertMsg)
case types.CctxStatus_PendingRevert:
cctx.GetCurrentOutboundParam().TxFinalizationStatus = types.TxFinalizationStatus_Executed
cctx.SetAbort(true, "Outbound failed: revert failed; abort TX")
cctx.SetAbort("", "outbound and revert failed")
}
return nil
}
Expand All @@ -225,9 +225,9 @@ func (k Keeper) processSuccessfulOutbound(
oldStatus := cctx.CctxStatus.Status
switch oldStatus {
case types.CctxStatus_PendingRevert:
cctx.SetReverted(false, "Outbound succeeded: revert executed")
cctx.SetReverted("", "revert executed")
case types.CctxStatus_PendingOutbound:
cctx.SetOutboundMined(false, "Outbound succeeded: mined")
cctx.SetOutboundMined("", "")
default:
return
}
Expand Down Expand Up @@ -256,7 +256,7 @@ func (k Keeper) processFailedZETAOutboundOnZEVM(ctx sdk.Context, cctx *types.Cro
}

// Trying to revert the transaction this would get set to a finalized status in the same block as this does not need a TSS singing
cctx.SetPendingRevert(true, "Outbound failed: trying to revert")
cctx.SetPendingRevert("", "outbound failed")
data, err := base64.StdEncoding.DecodeString(cctx.RelayedMessage)
if err != nil {
return fmt.Errorf("failed decoding relayed message: %s", err.Error())
Expand Down Expand Up @@ -290,7 +290,7 @@ func (k Keeper) processFailedZETAOutboundOnZEVM(ctx sdk.Context, cctx *types.Cro
return fmt.Errorf("failed ZETARevertAndCallContract: %s", err.Error())
}

cctx.SetReverted(true, "Outbound failed: revert executed")
cctx.SetReverted("", "outbound failed")
if len(ctx.TxBytes()) > 0 {
// add event for tendermint transaction hash format
hash := tmbytes.HexBytes(tmtypes.Tx(ctx.TxBytes()).Hash())
Expand Down Expand Up @@ -336,7 +336,7 @@ func (k Keeper) processFailedOutboundV2(ctx sdk.Context, cctx *types.CrossChainT
}

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

// process the revert on ZEVM
if err := k.fungibleKeeper.ProcessV2RevertDeposit(
Expand All @@ -354,7 +354,7 @@ func (k Keeper) processFailedOutboundV2(ctx sdk.Context, cctx *types.CrossChainT
}

// tx is reverted
cctx.SetReverted(true, "Outbound failed: revert executed")
cctx.SetReverted("", "outbound failed")

// add event for tendermint transaction hash format
if len(ctx.TxBytes()) > 0 {
Expand All @@ -367,7 +367,7 @@ func (k Keeper) processFailedOutboundV2(ctx sdk.Context, cctx *types.CrossChainT
cctx.GetCurrentOutboundParam().TxFinalizationStatus = types.TxFinalizationStatus_Executed
case types.CctxStatus_PendingRevert:
cctx.GetCurrentOutboundParam().TxFinalizationStatus = types.TxFinalizationStatus_Executed
cctx.SetAbort(true, "Outbound failed: revert failed; abort TX")
cctx.SetAbort("", "outbound and revert failed")
}
return nil
}
2 changes: 1 addition & 1 deletion x/crosschain/keeper/initiate_outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ func (k Keeper) InitiateOutbound(ctx sdk.Context, config InitiateOutboundConfig)
)
}

config.CCTX.SetPendingOutbound(false, "")
config.CCTX.SetPendingOutbound("", "")
return cctxGateway.InitiateOutbound(ctx, config)
}
4 changes: 2 additions & 2 deletions x/crosschain/keeper/msg_server_vote_inbound_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func TestKeeper_VoteInbound(t *testing.T) {
})
}

func TestStatus_ChangeStatus(t *testing.T) {
func TestStatus_UpdateCctxStatus(t *testing.T) {
tt := []struct {
Name string
Status types.Status
Expand Down Expand Up @@ -302,7 +302,7 @@ func TestStatus_ChangeStatus(t *testing.T) {
for _, test := range tt {
test := test
t.Run(test.Name, func(t *testing.T) {
test.Status.ChangeStatus(test.NonErrStatus, test.Msg)
test.Status.UpdateCctxStatus(test.NonErrStatus, false, test.Msg, "")
if test.IsErr {
require.Equal(t, test.ErrStatus, test.Status.Status)
} else {
Expand Down
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 @@ -185,7 +185,7 @@ SaveFailedOutbound saves a failed outbound transaction.It does the following thi
*/

func (k Keeper) SaveFailedOutbound(ctx sdk.Context, cctx *types.CrossChainTx, errMessage string, tssPubkey string) {
cctx.SetAbort(true, errMessage)
cctx.SetAbort("", errMessage)
ctx.Logger().Error(errMessage)
k.SaveOutbound(ctx, cctx, tssPubkey)
}
Expand Down
34 changes: 10 additions & 24 deletions x/crosschain/types/cctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,42 +170,29 @@ func (m *CrossChainTx) AddOutbound(
return nil
}

func (m *CrossChainTx) ChangeStatus(newStatus CctxStatus, isError bool, msg string) {
switch {
case isError && msg == "":
m.CctxStatus.ChangeStatus(newStatus, "")
m.Error = "unexpected error"
case isError && msg != "":
m.CctxStatus.ChangeStatus(newStatus, "")
m.Error = msg
case !isError:
m.CctxStatus.ChangeStatus(newStatus, msg)
}
}

// SetAbort sets the CCTX status to Aborted with the given error message.
func (m *CrossChainTx) SetAbort(isError bool, msg string) {
m.ChangeStatus(CctxStatus_Aborted, isError, msg)
func (m CrossChainTx) SetAbort(statusMsg, errorMsg string) {
m.CctxStatus.UpdateCctxStatus(CctxStatus_Aborted, true, statusMsg, errorMsg)
}

// SetPendingRevert sets the CCTX status to PendingRevert with the given error message.
func (m *CrossChainTx) SetPendingRevert(isError bool, msg string) {
m.ChangeStatus(CctxStatus_PendingRevert, isError, msg)
func (m CrossChainTx) SetPendingRevert(statusMsg, errorMsg string) {
m.CctxStatus.UpdateCctxStatus(CctxStatus_PendingRevert, true, statusMsg, errorMsg)
}

// SetPendingOutbound sets the CCTX status to PendingOutbound with the given error message.
func (m *CrossChainTx) SetPendingOutbound(isError bool, msg string) {
m.ChangeStatus(CctxStatus_PendingOutbound, isError, msg)
func (m CrossChainTx) SetPendingOutbound(statusMsg, errorMsg string) {
m.CctxStatus.UpdateCctxStatus(CctxStatus_PendingOutbound, false, statusMsg, errorMsg)
}

// SetOutboundMined sets the CCTX status to OutboundMined with the given error message.
func (m *CrossChainTx) SetOutboundMined(isError bool, msg string) {
m.ChangeStatus(CctxStatus_OutboundMined, isError, msg)
func (m CrossChainTx) SetOutboundMined(statusMsg, errorMsg string) {
m.CctxStatus.UpdateCctxStatus(CctxStatus_OutboundMined, false, statusMsg, errorMsg)
}

// SetReverted sets the CCTX status to Reverted with the given error message.
func (m *CrossChainTx) SetReverted(isError bool, msg string) {
m.ChangeStatus(CctxStatus_Reverted, isError, msg)
func (m CrossChainTx) SetReverted(statusMsg, errorMsg string) {
m.CctxStatus.UpdateCctxStatus(CctxStatus_Reverted, true, statusMsg, errorMsg)
}

func (m CrossChainTx) GetCCTXIndexBytes() ([32]byte, error) {
Expand Down Expand Up @@ -286,7 +273,6 @@ func NewCCTX(ctx sdk.Context, msg MsgVoteInbound, tssPubkey string) (CrossChainT
OutboundParams: []*OutboundParams{outboundParams},
ProtocolContractVersion: msg.ProtocolContractVersion,
RevertOptions: msg.RevertOptions,
Error: "",
}

// TODO: remove this validate call
Expand Down
60 changes: 11 additions & 49 deletions x/crosschain/types/cctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,83 +150,45 @@ func Test_SetRevertOutboundValues(t *testing.T) {

func TestCrossChainTx_SetAbort(t *testing.T) {
cctx := sample.CrossChainTx(t, "test")
cctx.SetAbort(false, "test")
cctx.CctxStatus.Status = types.CctxStatus_PendingOutbound
cctx.SetAbort("test", "test")
require.Equal(t, types.CctxStatus_Aborted, cctx.CctxStatus.Status)
require.Contains(t, cctx.CctxStatus.StatusMessage, "test")
}

func TestCrossChainTx_SetAbortWithError(t *testing.T) {
cctx := sample.CrossChainTx(t, "test")
cctx.SetAbort(true, "test")
require.Equal(t, types.CctxStatus_Aborted, cctx.CctxStatus.Status)
require.NotContains(t, cctx.CctxStatus.StatusMessage, "test")
require.Equal(t, "test", cctx.Error)
require.Contains(t, cctx.CctxStatus.ErrorMessage, "test")
}

func TestCrossChainTx_SetPendingRevert(t *testing.T) {
cctx := sample.CrossChainTx(t, "test")
cctx.CctxStatus.Status = types.CctxStatus_PendingOutbound
cctx.SetPendingRevert(false, "test")
cctx.SetPendingRevert("test", "test")
require.Equal(t, types.CctxStatus_PendingRevert, cctx.CctxStatus.Status)
require.Contains(t, cctx.CctxStatus.StatusMessage, "test")
}

func TestCrossChainTx_SetPendingWithError(t *testing.T) {
cctx := sample.CrossChainTx(t, "test")
cctx.CctxStatus.Status = types.CctxStatus_PendingOutbound
cctx.SetPendingRevert(true, "test")
require.Equal(t, types.CctxStatus_PendingRevert, cctx.CctxStatus.Status)
require.NotContains(t, cctx.CctxStatus.StatusMessage, "test")
require.Equal(t, "test", cctx.Error)
require.Contains(t, cctx.CctxStatus.ErrorMessage, "test")
}

func TestCrossChainTx_SetPendingOutbound(t *testing.T) {
cctx := sample.CrossChainTx(t, "test")
cctx.CctxStatus.Status = types.CctxStatus_PendingInbound
cctx.SetPendingOutbound(false, "test")
cctx.SetPendingOutbound("test", "test")
require.Equal(t, types.CctxStatus_PendingOutbound, cctx.CctxStatus.Status)
require.Contains(t, cctx.CctxStatus.StatusMessage, "test")
}

func TestCrossChainTx_SetPendingOutboundWithError(t *testing.T) {
cctx := sample.CrossChainTx(t, "test")
cctx.CctxStatus.Status = types.CctxStatus_PendingInbound
cctx.SetPendingOutbound(true, "test")
require.Equal(t, types.CctxStatus_PendingOutbound, cctx.CctxStatus.Status)
require.NotContains(t, cctx.CctxStatus.StatusMessage, "test")
require.Equal(t, "test", cctx.Error)
require.NotContains(t, cctx.CctxStatus.ErrorMessage, "test")
}

func TestCrossChainTx_SetOutboundMined(t *testing.T) {
cctx := sample.CrossChainTx(t, "test")
cctx.CctxStatus.Status = types.CctxStatus_PendingOutbound
cctx.SetOutboundMined(false, "test")
cctx.SetOutboundMined("test", "test")
require.Equal(t, types.CctxStatus_OutboundMined, cctx.CctxStatus.Status)
require.Contains(t, cctx.CctxStatus.StatusMessage, "test")
}

func TestCrossChainTx_SetOutboundMinedWithError(t *testing.T) {
cctx := sample.CrossChainTx(t, "test")
cctx.CctxStatus.Status = types.CctxStatus_PendingOutbound
cctx.SetOutboundMined(true, "test")
require.Equal(t, types.CctxStatus_OutboundMined, cctx.CctxStatus.Status)
require.NotContains(t, cctx.CctxStatus.StatusMessage, "test")
require.Contains(t, cctx.Error, "test")
require.NotContains(t, cctx.CctxStatus.ErrorMessage, "test")
}

func TestCrossChainTx_SetReverted(t *testing.T) {
cctx := sample.CrossChainTx(t, "test")
cctx.CctxStatus.Status = types.CctxStatus_PendingRevert
cctx.SetReverted(false, "test")
cctx.SetReverted("test", "test")
require.Equal(t, types.CctxStatus_Reverted, cctx.CctxStatus.Status)
require.Contains(t, cctx.CctxStatus.StatusMessage, "test")
}

func TestCrossChainTx_SetRevertedWithError(t *testing.T) {
cctx := sample.CrossChainTx(t, "test")
cctx.CctxStatus.Status = types.CctxStatus_PendingRevert
cctx.SetReverted(true, "test")
require.Equal(t, types.CctxStatus_Reverted, cctx.CctxStatus.Status)
require.NotContains(t, cctx.CctxStatus.StatusMessage, "test")
require.Contains(t, cctx.Error, "test")
require.Contains(t, cctx.CctxStatus.ErrorMessage, "test")
}
Loading

0 comments on commit 64217ae

Please sign in to comment.