Skip to content

Commit

Permalink
add function to update error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Oct 2, 2024
1 parent 445c85d commit 48f9052
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 31 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* [2826](https://github.com/zeta-chain/node/pull/2826) - remove unused code from emissions module and add new parameter for fixed block reward amount
* [2890](https://github.com/zeta-chain/node/pull/2890) - refactor `MsgUpdateChainInfo` to accept a single chain, and add `MsgRemoveChainInfo` to remove a chain
* [2899](https://github.com/zeta-chain/node/pull/2899) - remove btc deposit fee v1 and improve unit tests
* [2952](https://github.com/zeta-chain/node/pull/2952) - add error_message to cctx.status

### Tests

Expand Down
4 changes: 2 additions & 2 deletions e2e/e2etests/test_eth_deposit_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ func TestEtherDepositAndCall(r *runner.E2ERunner, args []string) {

r.Logger.Info("Cross-chain call to reverter reverted")

// check the status message contains revert error hash in case of revert
require.Contains(r, cctx.CctxStatus.ErrorMessage, utils.ErrHashRevertFoo)
// Check the error carries the revert executed.
require.Contains(r, cctx.CctxStatus.ErrorMessage, "revert executed")
}
4 changes: 2 additions & 2 deletions e2e/e2etests/test_solana_deposit_refund.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ func TestSolanaDepositAndCallRefund(r *runner.E2ERunner, args []string) {
r.Logger.CCTX(*cctx, "solana_deposit_and_refund")
utils.RequireCCTXStatus(r, cctx, crosschaintypes.CctxStatus_Reverted)

// check the status message contains revert error hash in case of revert
require.Contains(r, cctx.CctxStatus.StatusMessage, utils.ErrHashRevertFoo)
// Check the error carries the revert executed.
require.Contains(r, cctx.CctxStatus.ErrorMessage, "revert executed")
}
1 change: 1 addition & 0 deletions testutil/sample/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func Status(t *testing.T, index string) *types.Status {
return &types.Status{
Status: types.CctxStatus(r.Intn(100)),
StatusMessage: String(),
ErrorMessage: String(),
CreatedTimestamp: createdAt,
LastUpdateTimestamp: createdAt,
}
Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/keeper/msg_server_vote_inbound_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func TestStatus_UpdateCctxStatus(t *testing.T) {
for _, test := range tt {
test := test
t.Run(test.Name, func(t *testing.T) {
test.Status.UpdateCctxStatus(test.NonErrStatus, false, test.Msg, "")
test.Status.UpdateCctxMessages(test.NonErrStatus, false, test.Msg, "")
if test.IsErr {
require.Equal(t, test.ErrStatus, test.Status.Status)
} else {
Expand Down
11 changes: 6 additions & 5 deletions x/crosschain/types/cctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,27 +172,27 @@ func (m *CrossChainTx) AddOutbound(

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

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

// SetPendingOutbound sets the CCTX status to PendingOutbound with the given error message.
func (m CrossChainTx) SetPendingOutbound(statusMsg string) {
m.CctxStatus.UpdateCctxStatus(CctxStatus_PendingOutbound, false, statusMsg, "")
m.CctxStatus.UpdateCctxMessages(CctxStatus_PendingOutbound, false, statusMsg, "")
}

// SetOutboundMined sets the CCTX status to OutboundMined with the given error message.
func (m CrossChainTx) SetOutboundMined(statusMsg string) {
m.CctxStatus.UpdateCctxStatus(CctxStatus_OutboundMined, false, statusMsg, "")
m.CctxStatus.UpdateCctxMessages(CctxStatus_OutboundMined, false, statusMsg, "")
}

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

func (m CrossChainTx) GetCCTXIndexBytes() ([32]byte, error) {
Expand Down Expand Up @@ -259,6 +259,7 @@ func NewCCTX(ctx sdk.Context, msg MsgVoteInbound, tssPubkey string) (CrossChainT
status := &Status{
Status: CctxStatus_PendingInbound,
StatusMessage: "",
ErrorMessage: "",
CreatedTimestamp: ctx.BlockHeader().Time.Unix(),
LastUpdateTimestamp: ctx.BlockHeader().Time.Unix(),
IsAbortRefunded: false,
Expand Down
4 changes: 3 additions & 1 deletion x/crosschain/types/cctx_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types_test

import (
"fmt"
"math/rand"
"testing"

Expand Down Expand Up @@ -151,7 +152,8 @@ func Test_SetRevertOutboundValues(t *testing.T) {
func TestCrossChainTx_SetAbort(t *testing.T) {
cctx := sample.CrossChainTx(t, "test")
cctx.CctxStatus.Status = types.CctxStatus_PendingOutbound
cctx.SetAbort("test", "test")
cctx.SetAbort("test", fmt.Sprintf("deposit: %s, error: %s", "foo", "bar"))
fmt.Printf("DEBUG: %+v\n", cctx.CctxStatus)
require.Equal(t, types.CctxStatus_Aborted, cctx.CctxStatus.Status)
require.Contains(t, cctx.CctxStatus.StatusMessage, "test")
require.Contains(t, cctx.CctxStatus.ErrorMessage, "test")
Expand Down
41 changes: 24 additions & 17 deletions x/crosschain/types/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,14 @@ func (m *Status) AbortRefunded() {
m.StatusMessage = "CCTX aborted and Refunded"
}

// UpdateCctxStatus transitions the Status.
// In case of an error, ErrorMessage is updated.
// In case of no error, StatusMessage is updated.
func (m *Status) UpdateCctxStatus(newStatus CctxStatus, isError bool, statusMsg, errorMsg string) {
m.ChangeStatus(newStatus, statusMsg)

if isError && errorMsg != "" {
m.ErrorMessage = errorMsg
} else if isError && errorMsg == "" {
m.ErrorMessage = "unknown error"
}
// UpdateCctxMessages transitions the Status and Error messages.
func (m *Status) UpdateCctxMessages(newStatus CctxStatus, isError bool, statusMsg, errorMsg string) {
m.UpdateStatusMessage(newStatus, statusMsg)
m.UpdateErrorMessage(isError, errorMsg)
}

// ChangeStatus changes the status of the cross chain transaction.
func (m *Status) ChangeStatus(newStatus CctxStatus, statusMsg string) {
// UpdateStatusMessage updates cctx.status.status_message.
func (m *Status) UpdateStatusMessage(newStatus CctxStatus, statusMsg string) {
if !m.ValidateTransition(newStatus) {
m.StatusMessage = fmt.Sprintf(
"Failed to transition status from %s to %s",
Expand All @@ -35,15 +28,29 @@ func (m *Status) ChangeStatus(newStatus CctxStatus, statusMsg string) {
return
}

if statusMsg == "" {
m.StatusMessage = fmt.Sprintf("Status changed from %s to %s", m.Status.String(), newStatus.String())
} else {
m.StatusMessage = statusMsg
m.StatusMessage = fmt.Sprintf("Status changed from %s to %s", m.Status.String(), newStatus.String())

if statusMsg != "" {
m.StatusMessage += fmt.Sprintf(" - %s", statusMsg)
}

m.Status = newStatus
}

// UpdateErrorMessage updates cctx.status.error_message.
func (m *Status) UpdateErrorMessage(isError bool, errorMsg string) {
if !isError {
return
}

errMsg := errorMsg
if errMsg == "" {
errMsg = "unknown error"
}

m.ErrorMessage = errMsg
}

func (m *Status) ValidateTransition(newStatus CctxStatus) bool {
stateTransitionMap := stateTransitionMap()
oldStatus := m.Status
Expand Down
6 changes: 3 additions & 3 deletions x/crosschain/types/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ func TestStatus_ChangeStatus(t *testing.T) {
t.Run("should change status and msg if transition is valid", func(t *testing.T) {
s := types.Status{Status: types.CctxStatus_PendingInbound}

s.ChangeStatus(types.CctxStatus_PendingOutbound, "msg")
s.UpdateStatusMessage(types.CctxStatus_PendingOutbound, "msg")
assert.Equal(t, s.Status, types.CctxStatus_PendingOutbound)
assert.Equal(t, s.StatusMessage, "msg")
})

t.Run("should change status if transition is valid", func(t *testing.T) {
s := types.Status{Status: types.CctxStatus_PendingInbound}

s.ChangeStatus(types.CctxStatus_PendingOutbound, "")
s.UpdateStatusMessage(types.CctxStatus_PendingOutbound, "")
fmt.Printf("%+v\n", s)
assert.Equal(t, s.Status, types.CctxStatus_PendingOutbound)
assert.Equal(t, s.StatusMessage, fmt.Sprintf(
Expand All @@ -161,7 +161,7 @@ func TestStatus_ChangeStatus(t *testing.T) {
t.Run("should change status to aborted and msg if transition is invalid", func(t *testing.T) {
s := types.Status{Status: types.CctxStatus_PendingOutbound}

s.ChangeStatus(types.CctxStatus_PendingInbound, "msg")
s.UpdateStatusMessage(types.CctxStatus_PendingInbound, "msg")
assert.Equal(t, s.Status, types.CctxStatus_Aborted)
assert.Equal(
t,
Expand Down

0 comments on commit 48f9052

Please sign in to comment.