Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of custom errors in receipts - capture returnValue #112

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 27 additions & 43 deletions internal/ethereum/get_receipt.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -17,6 +17,7 @@
package ethereum

import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
Expand All @@ -25,9 +26,7 @@ import (

"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/hyperledger/firefly-common/pkg/i18n"
"github.com/hyperledger/firefly-common/pkg/log"
"github.com/hyperledger/firefly-evmconnect/internal/msgs"
"github.com/hyperledger/firefly-signer/pkg/abi"
"github.com/hyperledger/firefly-signer/pkg/ethtypes"
"github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi"
)
Expand Down Expand Up @@ -59,6 +58,7 @@ type receiptExtraInfo struct {
GasUsed *fftypes.FFBigInt `json:"gasUsed"`
Status *fftypes.FFBigInt `json:"status"`
ErrorMessage *string `json:"errorMessage"`
ReturnValue *string `json:"returnValue,omitempty"`
}

// txInfoJSONRPC is the transaction info obtained over JSON/RPC from the ethereum client, with input data
Expand Down Expand Up @@ -119,36 +119,14 @@ func ProtocolIDForReceipt(blockNumber, transactionIndex *fftypes.FFBigInt) strin
return ""
}

/*
Accepts hex string, with or without leading 0x and also accepts short hex (leading zeros removed)
Converts the hex string to bytes then decodes it as call data for the given abi.Entry
*/
func decodeCallDataHex(ctx context.Context, function *abi.Entry, callDataHex string) (*abi.ComponentValue, error) {
callDataHex = strings.TrimPrefix(callDataHex, "0x")
if len(callDataHex)%2 == 1 {
callDataHex = "0" + callDataHex
}

callDataBytes, err := hex.DecodeString(callDataHex)
if err != nil {
return nil, err
}

componentValue, err := function.DecodeCallDataCtx(ctx, callDataBytes)
if err != nil {
return nil, err
}

return componentValue, nil
}

func (c *ethConnector) getErrorMessage(ctx context.Context, transactionHash string) (*string, error) {
func (c *ethConnector) getErrorInfo(ctx context.Context, transactionHash string) (pReturnValue *string, pErrorMessage *string) {

// Attempt to get the return value of the transaction - not possible on all RPC endpoints
var debugTrace *txDebugTrace

traceErr := c.backend.CallRPC(ctx, &debugTrace, "debug_traceTransaction", transactionHash)
if traceErr != nil {
return nil, traceErr.Error()
msg := i18n.NewError(ctx, msgs.MsgUnableToCallDebug, traceErr).Error()
return nil, &msg
}

returnValue := debugTrace.ReturnValue
Expand All @@ -161,18 +139,26 @@ func (c *ethConnector) getErrorMessage(ctx context.Context, transactionHash stri
}
}
}
if returnValue == "" {
// not found return value in any of the expected places
return nil, nil

// See if the return value is using the default error you get from "revert"
var errorMessage string
returnDataBytes, _ := hex.DecodeString(strings.TrimPrefix(returnValue, "0x"))
if len(returnDataBytes) > 4 && bytes.Equal(returnDataBytes[0:4], defaultErrorID) {
value, err := defaultError.DecodeCallDataCtx(ctx, returnDataBytes)
if err == nil {
errorMessage = value.Children[0].Value.(string)
}
}

value, err := decodeCallDataHex(ctx, defaultError, returnValue)
if err != nil {
return nil, err
// Otherwise we can't decode it, so put it directly in the error
if errorMessage == "" {
if len(returnDataBytes) > 0 {
errorMessage = i18n.NewError(ctx, msgs.MsgReturnValueNotDecoded, returnValue).Error()
} else {
errorMessage = i18n.NewError(ctx, msgs.MsgReturnValueNotAvailable).Error()
}
}
errorMessage := value.Children[0].Value
errorMessageString := errorMessage.(string)
return &errorMessageString, nil
return &returnValue, &errorMessage
}

func (c *ethConnector) TransactionReceipt(ctx context.Context, req *ffcapi.TransactionReceiptRequest) (*ffcapi.TransactionReceiptResponse, ffcapi.ErrorReason, error) {
Expand All @@ -188,14 +174,11 @@ func (c *ethConnector) TransactionReceipt(ctx context.Context, req *ffcapi.Trans
}
isSuccess := (ethReceipt.Status != nil && ethReceipt.Status.BigInt().Int64() > 0)

var returnDataString *string
var transactionErrorMessage *string

if !isSuccess {
var err error
transactionErrorMessage, err = c.getErrorMessage(ctx, req.TransactionHash)
if err != nil {
log.L(ctx).Debugf("Ignoring error getting error message: %s ", err)
}
returnDataString, transactionErrorMessage = c.getErrorInfo(ctx, req.TransactionHash)
}

ethReceipt.Logs = nil
Expand All @@ -206,6 +189,7 @@ func (c *ethConnector) TransactionReceipt(ctx context.Context, req *ffcapi.Trans
To: ethReceipt.To,
GasUsed: (*fftypes.FFBigInt)(ethReceipt.GasUsed),
Status: (*fftypes.FFBigInt)(ethReceipt.Status),
ReturnValue: returnDataString,
ErrorMessage: transactionErrorMessage,
})

Expand Down
2 changes: 1 addition & 1 deletion internal/ethereum/get_receipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ const sampleTransactionTraceBesu = `{
"0000000000000000000000000000000000000000000000000000000000000000"
],
"storage": {},
"reason": "0x8c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000114e6f7420656e6f75676820746f6b656e73000000000000000000000000000000"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not a correct error - there is a missing 0 in the default Error(string) function selector 0x08c379a0

Copy link
Contributor

@matthew1001 matthew1001 Apr 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peterbroadhurst some nodes return the error in that format (i.e. with an odd-number of hex digits) and assume that you will pad with a single 0 client-side if it's odd. The new value you've used is valid, but the reason for including this particular reason was to emulate nodes which do that.

"reason": "08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000114e6f7420656e6f75676820746f6b656e73000000000000000000000000000000"
}
]
}`
Expand Down
3 changes: 3 additions & 0 deletions internal/msgs/en_error_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,7 @@ var (
MsgUnmarshalErrorFail = ffe("FF23049", "Failed to parse error %d: %s")
MsgUnmarshalABIErrorsFail = ffe("FF23050", "Failed to parse errors ABI: %s")
MsgInvalidRegex = ffe("FF23051", "Invalid regular expression for auto-backoff catchup error: %s")
MsgUnableToCallDebug = ffe("FF23052", "Failed to call debug_traceTransaction to get error detail: %s")
MsgReturnValueNotDecoded = ffe("FF23053", "Error return value for custom error: %s")
MsgReturnValueNotAvailable = ffe("FF23054", "Error return value unavailable")
)
Loading