From 7e6b78ddd0acc018d0751b20e4b4aad79f21604e Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Thu, 15 Feb 2024 22:36:07 +0900 Subject: [PATCH 01/13] Change so that Custom Error can be output to revertReason Signed-off-by: Dongri Jin Add defaultError Signed-off-by: Dongri Jin Add test case Signed-off-by: Dongri Jin Add panic error Signed-off-by: Dongri Jin Add abi_paths Signed-off-by: Dongri Jin Fix parse abi files Signed-off-by: Dongri Jin * Move package Signed-off-by: Dongri Jin Add GetRevertReason to after DebugTraceTransaction Signed-off-by: Dongri Jin * Fix erepo create * Fix add error repo Signed-off-by: Dongri Jin --- pkg/client/client.go | 207 ++++++++++-------- pkg/client/client_test.go | 35 --- pkg/relay/ethereum/config.pb.go | 160 +++++++++----- pkg/relay/ethereum/error_parse.go | 160 ++++++++++++++ pkg/relay/ethereum/error_parse_test.go | 99 +++++++++ pkg/relay/ethereum/tx.go | 60 +++-- proto/buf.lock | 8 +- .../chains/ethereum/config/config.proto | 3 +- 8 files changed, 529 insertions(+), 203 deletions(-) delete mode 100644 pkg/client/client_test.go create mode 100644 pkg/relay/ethereum/error_parse.go create mode 100644 pkg/relay/ethereum/error_parse_test.go diff --git a/pkg/client/client.go b/pkg/client/client.go index a5889cb..f1c0d98 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -2,6 +2,7 @@ package client import ( "context" + "encoding/json" "fmt" "math/big" "time" @@ -14,7 +15,6 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/rpc" - "github.com/hyperledger-labs/yui-relayer/log" ) type ETHClient struct { @@ -62,70 +62,35 @@ func (cl *ETHClient) Raw() *rpc.Client { return cl.Client.Client() } -func (cl *ETHClient) GetTransactionReceipt(ctx context.Context, txHash common.Hash, enableDebugTrace bool) (rc *gethtypes.Receipt, revertReason string, err error) { +func (cl *ETHClient) GetTransactionReceipt(ctx context.Context, txHash common.Hash) (rc *Receipt, err error) { var r *Receipt if err := cl.Raw().CallContext(ctx, &r, "eth_getTransactionReceipt", txHash); err != nil { - return nil, "", err + return nil, err } else if r == nil { - return nil, "", ethereum.NotFound - } else if r.Status == gethtypes.ReceiptStatusSuccessful { - return &r.Receipt, "", nil - } else if r.HasRevertReason() { - reason, err := r.GetRevertReason() - if err != nil { - // TODO: use more proper logger - logger := log.GetLogger().WithModule("ethereum.chain") - logger.Error("failed to get revert reason", err) - } - return &r.Receipt, reason, nil - } else if enableDebugTrace { - reason, err := cl.DebugTraceTransaction(ctx, txHash) - if err != nil { - // TODO: use more proper logger - logger := log.GetLogger().WithModule("ethereum.chain") - logger.Error("failed to call debug_traceTransaction", err) - } - return &r.Receipt, reason, nil + return nil, ethereum.NotFound } else { - // TODO: use more proper logger - logger := log.GetLogger().WithModule("ethereum.chain") - logger.Info("tx execution failed but the reason couldn't be obtained", "tx_hash", txHash.Hex()) - return &r.Receipt, "", nil + return r, nil } } -func (cl *ETHClient) WaitForReceiptAndGet(ctx context.Context, txHash common.Hash, enableDebugTrace bool) (*gethtypes.Receipt, string, error) { - var receipt *gethtypes.Receipt - var revertReason string +func (cl *ETHClient) WaitForReceiptAndGet(ctx context.Context, txHash common.Hash) (*Receipt, error) { + var receipt *Receipt err := retry.Do( func() error { - rc, reason, err := cl.GetTransactionReceipt(ctx, txHash, enableDebugTrace) + rc, err := cl.GetTransactionReceipt(ctx, txHash) if err != nil { return err } receipt = rc - revertReason = reason return nil }, cl.option.retryOpts..., ) if err != nil { - return nil, "", err - } - return receipt, revertReason, nil -} - -func (cl *ETHClient) DebugTraceTransaction(ctx context.Context, txHash common.Hash) (string, error) { - var result *callFrame - if err := cl.Raw().CallContext(ctx, &result, "debug_traceTransaction", txHash, map[string]string{"tracer": "callTracer"}); err != nil { - return "", err - } - revertReason, err := searchRevertReason(result) - if err != nil { - return "", err + return nil, err } - return revertReason, nil + return receipt, nil } type Receipt struct { @@ -137,31 +102,45 @@ func (rc Receipt) HasRevertReason() bool { return len(rc.RevertReason) > 0 } -func (rc Receipt) GetRevertReason() (string, error) { - return parseRevertReason(rc.RevertReason) -} - -// A format of revertReason is: -// 4byte: Function selector for Error(string) -// 32byte: Data offset -// 32byte: String length -// Remains: String Data -func parseRevertReason(bz []byte) (string, error) { - if l := len(bz); l == 0 { - return "", nil - } else if l < 68 { - return "", fmt.Errorf("invalid length") +func (cl *ETHClient) EstimateGasFromTx(ctx context.Context, tx *gethtypes.Transaction) (uint64, error) { + from, err := gethtypes.LatestSignerForChainID(tx.ChainId()).Sender(tx) + if err != nil { + return 0, err } - - size := &big.Int{} - size.SetBytes(bz[36:68]) - return string(bz[68 : 68+size.Int64()]), nil + to := tx.To() + value := tx.Value() + gasTipCap := tx.GasTipCap() + gasFeeCap := tx.GasFeeCap() + gasPrice := tx.GasPrice() + data := tx.Data() + accessList := tx.AccessList() + callMsg := ethereum.CallMsg{ + From: from, + To: to, + GasPrice: gasPrice, + GasTipCap: gasTipCap, + GasFeeCap: gasFeeCap, + Value: value, + Data: data, + AccessList: accessList, + } + estimatedGas, err := cl.EstimateGas(ctx, callMsg) + if err != nil { + return 0, err + } + return estimatedGas, nil } -type callLog struct { - Address common.Address `json:"address"` - Topics []common.Hash `json:"topics"` - Data hexutil.Bytes `json:"data"` +func (cl *ETHClient) DebugTraceTransaction(ctx context.Context, txHash common.Hash) (string, error) { + var result *callFrame + if err := cl.Raw().CallContext(ctx, &result, "debug_traceTransaction", txHash, map[string]string{"tracer": "callTracer"}); err != nil { + return "", err + } + revertReason, err := searchRevertReason(result) + if err != nil { + return "", err + } + return revertReason, nil } // see: https://github.com/ethereum/go-ethereum/blob/v1.12.0/eth/tracers/native/call.go#L44-L59 @@ -182,6 +161,71 @@ type callFrame struct { Value *big.Int `json:"value,omitempty" rlp:"optional"` } +type callLog struct { + Address common.Address `json:"address"` + Topics []common.Hash `json:"topics"` + Data hexutil.Bytes `json:"data"` +} + +// UnmarshalJSON unmarshals from JSON. +func (c *callFrame) UnmarshalJSON(input []byte) error { + type callFrame0 struct { + Type *vm.OpCode `json:"-"` + From *common.Address `json:"from"` + Gas *hexutil.Uint64 `json:"gas"` + GasUsed *hexutil.Uint64 `json:"gasUsed"` + To *common.Address `json:"to,omitempty" rlp:"optional"` + Input *hexutil.Bytes `json:"input" rlp:"optional"` + Output *hexutil.Bytes `json:"output,omitempty" rlp:"optional"` + Error *string `json:"error,omitempty" rlp:"optional"` + RevertReason *string `json:"revertReason,omitempty"` + Calls []callFrame `json:"calls,omitempty" rlp:"optional"` + Logs []callLog `json:"logs,omitempty" rlp:"optional"` + Value *hexutil.Big `json:"value,omitempty" rlp:"optional"` + } + var dec callFrame0 + if err := json.Unmarshal(input, &dec); err != nil { + return err + } + if dec.Type != nil { + c.Type = *dec.Type + } + if dec.From != nil { + c.From = *dec.From + } + if dec.Gas != nil { + c.Gas = uint64(*dec.Gas) + } + if dec.GasUsed != nil { + c.GasUsed = uint64(*dec.GasUsed) + } + if dec.To != nil { + c.To = dec.To + } + if dec.Input != nil { + c.Input = *dec.Input + } + if dec.Output != nil { + c.Output = *dec.Output + } + if dec.Error != nil { + c.Error = *dec.Error + } + if dec.RevertReason != nil { + c.RevertReason = *dec.RevertReason + } + if dec.Calls != nil { + c.Calls = dec.Calls + } + if dec.Logs != nil { + c.Logs = dec.Logs + } + if dec.Value != nil { + c.Value = (*big.Int)(dec.Value) + } + return nil +} + func searchRevertReason(result *callFrame) (string, error) { if result.RevertReason != "" { return result.RevertReason, nil @@ -194,32 +238,3 @@ func searchRevertReason(result *callFrame) (string, error) { } return "", fmt.Errorf("revert reason not found") } - -func (cl *ETHClient) EstimateGasFromTx(ctx context.Context, tx *gethtypes.Transaction) (uint64, error) { - from, err := gethtypes.LatestSignerForChainID(tx.ChainId()).Sender(tx) - if err != nil { - return 0, err - } - to := tx.To() - value := tx.Value() - gasTipCap := tx.GasTipCap() - gasFeeCap := tx.GasFeeCap() - gasPrice := tx.GasPrice() - data := tx.Data() - accessList := tx.AccessList() - callMsg := ethereum.CallMsg{ - From: from, - To: to, - GasPrice: gasPrice, - GasTipCap: gasTipCap, - GasFeeCap: gasFeeCap, - Value: value, - Data: data, - AccessList: accessList, - } - estimatedGas, err := cl.EstimateGas(ctx, callMsg) - if err != nil { - return 0, err - } - return estimatedGas, nil -} diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go deleted file mode 100644 index a385aa2..0000000 --- a/pkg/client/client_test.go +++ /dev/null @@ -1,35 +0,0 @@ -package client - -import ( - "encoding/hex" - "strings" - "testing" - - "github.com/stretchr/testify/require" -) - -func TestRevertReasonParser(t *testing.T) { - // 1. Valid format - s, err := parseRevertReason( - hexToBytes("0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a4e6f7420656e6f7567682045746865722070726f76696465642e000000000000"), - ) - require.NoError(t, err) - require.Equal(t, "Not enough Ether provided.", s) - - // 2. Empty bytes - s, err = parseRevertReason(nil) - require.NoError(t, err) - require.Equal(t, "", s) - - // 3. Invalid format - _, err = parseRevertReason([]byte{0}) - require.Error(t, err) -} - -func hexToBytes(s string) []byte { - reason, err := hex.DecodeString(strings.TrimPrefix(s, "0x")) - if err != nil { - panic(err) - } - return reason -} diff --git a/pkg/relay/ethereum/config.pb.go b/pkg/relay/ethereum/config.pb.go index 664d3ed..6c1a4cf 100644 --- a/pkg/relay/ethereum/config.pb.go +++ b/pkg/relay/ethereum/config.pb.go @@ -44,6 +44,7 @@ type ChainConfig struct { TxType string `protobuf:"bytes,14,opt,name=tx_type,json=txType,proto3" json:"tx_type,omitempty"` DynamicTxGasConfig *DynamicTxGasConfig `protobuf:"bytes,15,opt,name=dynamic_tx_gas_config,json=dynamicTxGasConfig,proto3" json:"dynamic_tx_gas_config,omitempty"` BlocksPerEventQuery uint64 `protobuf:"varint,16,opt,name=blocks_per_event_query,json=blocksPerEventQuery,proto3" json:"blocks_per_event_query,omitempty"` + AbiPaths []string `protobuf:"bytes,17,rep,name=abi_paths,json=abiPaths,proto3" json:"abi_paths,omitempty"` } func (m *ChainConfig) Reset() { *m = ChainConfig{} } @@ -210,61 +211,63 @@ func init() { } var fileDescriptor_a8a57ab2f9f14837 = []byte{ - // 862 bytes of a gzipped FileDescriptorProto + // 882 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x55, 0xcd, 0x6e, 0x1b, 0x37, - 0x10, 0xb6, 0x62, 0x57, 0x96, 0xa8, 0x28, 0x71, 0x58, 0xff, 0xac, 0xdd, 0x46, 0x15, 0x74, 0x52, - 0xd1, 0x78, 0x05, 0x24, 0x68, 0x50, 0xf4, 0xa6, 0x28, 0x51, 0x9a, 0xc2, 0x05, 0xdc, 0xad, 0x4e, - 0xbd, 0x10, 0x5c, 0xee, 0x68, 0x45, 0x98, 0x4b, 0x6e, 0x49, 0xca, 0xd1, 0xbe, 0x45, 0x1f, 0xa0, - 0x0f, 0x94, 0x63, 0x2e, 0x05, 0x7a, 0x6c, 0xed, 0x17, 0x29, 0x48, 0xea, 0xcf, 0x48, 0xd1, 0x20, - 0x27, 0x89, 0xf3, 0xfd, 0xcc, 0x0c, 0x39, 0xe4, 0xa2, 0x6f, 0x34, 0x08, 0x5a, 0x81, 0x1e, 0xb0, - 0x19, 0xe5, 0xd2, 0x0c, 0xc0, 0xce, 0x40, 0xc3, 0xbc, 0x18, 0x30, 0x25, 0xa7, 0x3c, 0x5f, 0xfe, - 0xc4, 0xa5, 0x56, 0x56, 0xe1, 0xce, 0x92, 0x1c, 0x07, 0x72, 0xbc, 0x22, 0xc7, 0x81, 0x75, 0x76, - 0x98, 0xab, 0x5c, 0x79, 0xea, 0xc0, 0xfd, 0x0b, 0xaa, 0xb3, 0xd3, 0x5c, 0xa9, 0x5c, 0xc0, 0xc0, - 0xaf, 0xd2, 0xf9, 0x74, 0x40, 0x65, 0x15, 0xa0, 0xde, 0x9f, 0x75, 0xd4, 0x1a, 0x39, 0xaf, 0x91, - 0x37, 0xc0, 0xa7,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, 0x78, 0xcd, 0xa0, 0xdf, 0x91, 0xb3, 0xbb, + 0xdb, 0x60, 0xcb, 0x10, 0x97, 0x5c, 0x2b, 0x3a, 0xde, 0xda, 0x07, 0xa3, 0xb5, 0xd3, 0x0b, 0xfe, + 0xee, 0xef, 0xf6, 0xce, 0xbb, 0x9b, 0x76, 0xed, 0xfd, 0x4d, 0xbb, 0xf6, 0xd7, 0x4d, 0xbb, 0xf6, + 0xfb, 0x6d, 0x7b, 0xe7, 0xfd, 0x6d, 0x7b, 0xe7, 0xcf, 0xdb, 0xf6, 0xce, 0xaf, 0xc3, 0x54, 0xba, + 0xd9, 0x3c, 0x0e, 0x85, 0xce, 0xfb, 0x09, 0x77, 0x1c, 0xfb, 0xca, 0x78, 0xbc, 0xfe, 0xe1, 0xb9, + 0x90, 0xb1, 0xb8, 0xc0, 0xae, 0x2f, 0x10, 0xeb, 0x17, 0x57, 0x69, 0x1f, 0xcf, 0x6b, 0x4a, 0xbc, + 0x8f, 0x6b, 0xfb, 0xd9, 0xbf, 0x01, 0x00, 0x00, 0xff, 0xff, 0x74, 0xdf, 0xe9, 0xe1, 0xbd, 0x06, + 0x00, 0x00, } func (m *ChainConfig) Marshal() (dAtA []byte, err error) { @@ -287,6 +290,17 @@ func (m *ChainConfig) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.AbiPaths) > 0 { + for iNdEx := len(m.AbiPaths) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.AbiPaths[iNdEx]) + copy(dAtA[i:], m.AbiPaths[iNdEx]) + i = encodeVarintConfig(dAtA, i, uint64(len(m.AbiPaths[iNdEx]))) + i-- + dAtA[i] = 0x1 + i-- + dAtA[i] = 0x8a + } + } if m.BlocksPerEventQuery != 0 { i = encodeVarintConfig(dAtA, i, uint64(m.BlocksPerEventQuery)) i-- @@ -639,6 +653,12 @@ func (m *ChainConfig) Size() (n int) { if m.BlocksPerEventQuery != 0 { n += 2 + sovConfig(uint64(m.BlocksPerEventQuery)) } + if len(m.AbiPaths) > 0 { + for _, s := range m.AbiPaths { + l = len(s) + n += 2 + l + sovConfig(uint64(l)) + } + } return n } @@ -1170,6 +1190,38 @@ func (m *ChainConfig) Unmarshal(dAtA []byte) error { break } } + case 17: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field AbiPaths", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowConfig + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthConfig + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthConfig + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.AbiPaths = append(m.AbiPaths, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipConfig(dAtA[iNdEx:]) diff --git a/pkg/relay/ethereum/error_parse.go b/pkg/relay/ethereum/error_parse.go new file mode 100644 index 0000000..586b9b9 --- /dev/null +++ b/pkg/relay/ethereum/error_parse.go @@ -0,0 +1,160 @@ +package ethereum + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/ethereum/go-ethereum/accounts/abi" +) + +type Artifact struct { + ABI []interface{} `json:"abi"` +} + +type ErrorsRepository struct { + errs map[[4]byte]abi.Error +} + +var erepo *ErrorsRepository + +func GetErepo(abiPaths []string) (*ErrorsRepository, error) { + var erepoErr error + if erepo == nil { + erepo, erepoErr = CreateErepo(abiPaths) + } + return erepo, erepoErr +} + +func CreateErepo(abiPaths []string) (*ErrorsRepository, error) { + var abiErrors []abi.Error + for _, dir := range abiPaths { + if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if !info.IsDir() && strings.HasSuffix(info.Name(), ".json") { + data, err := os.ReadFile(path) + if err != nil { + return err + } + var artifact Artifact + var abiData []byte + if err := json.Unmarshal(data, &artifact); err != nil { + abiData = data + } else { + abiData, err = json.Marshal(artifact.ABI) + if err != nil { + return err + } + } + abiABI, err := abi.JSON(strings.NewReader(string(abiData))) + if err != nil { + return err + } + for _, error := range abiABI.Errors { + abiErrors = append(abiErrors, error) + } + } + return nil + }); err != nil { + return nil, err + } + } + return NewErrorsRepository(abiErrors) +} + +func NewErrorsRepository(customErrors []abi.Error) (*ErrorsRepository, error) { + defaultErrs, err := defaultErrors() + if err != nil { + return nil, err + } + customErrors = append(customErrors, defaultErrs...) + er := ErrorsRepository{ + errs: make(map[[4]byte]abi.Error), + } + for _, e := range customErrors { + e := abi.NewError(e.Name, e.Inputs) + if err := er.Add(e); err != nil { + return nil, err + } + } + return &er, nil +} + +func (r *ErrorsRepository) Add(e0 abi.Error) error { + var sel [4]byte + copy(sel[:], e0.ID[:4]) + if e1, ok := r.errs[sel]; ok { + if e1.Sig == e0.Sig { + return nil + } + return fmt.Errorf("error selector collision: sel=%x e0=%v e1=%v", sel, e0, e1) + } + r.errs[sel] = e0 + return nil +} + +func (r *ErrorsRepository) GetError(sel [4]byte) (abi.Error, bool) { + e, ok := r.errs[sel] + return e, ok +} + +func (r *ErrorsRepository) ParseError(bz []byte) (string, interface{}, error) { + if len(bz) < 4 { + return "", nil, fmt.Errorf("invalid error data: %v", bz) + } + var sel [4]byte + copy(sel[:], bz[:4]) + e, ok := r.GetError(sel) + if !ok { + return "", nil, fmt.Errorf("unknown error: sel=%x", sel) + } + v, err := e.Unpack(bz) + return e.Sig, v, err +} + +func defaultErrors() ([]abi.Error, error) { + strT, err := abi.NewType("string", "", nil) + if err != nil { + return nil, err + } + uintT, err := abi.NewType("uint256", "", nil) + if err != nil { + return nil, err + } + + errors := []abi.Error{ + { + Name: "Error", + Inputs: []abi.Argument{ + { + Type: strT, + }, + }, + }, + { + Name: "Panic", + Inputs: []abi.Argument{ + { + Type: uintT, + }, + }, + }, + } + return errors, nil +} + +func GetRevertReason(revertReason []byte, abiPaths []string) string { + erepo, err := GetErepo(abiPaths) + if err != nil { + return fmt.Sprintf("GetErepo err=%v", err) + } + sig, args, err := erepo.ParseError(revertReason) + if err != nil { + return fmt.Sprintf("raw-revert-reason=\"%x\" parse-err=\"%v\"", revertReason, err) + } + return fmt.Sprintf("revert-reason=\"%v\" args=\"%v\"", sig, args) +} diff --git a/pkg/relay/ethereum/error_parse_test.go b/pkg/relay/ethereum/error_parse_test.go new file mode 100644 index 0000000..716128b --- /dev/null +++ b/pkg/relay/ethereum/error_parse_test.go @@ -0,0 +1,99 @@ +package ethereum + +import ( + "encoding/hex" + "math/big" + "strings" + "testing" + + "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/stretchr/testify/require" +) + +func TestRevertReasonParserDefault(t *testing.T) { + customErrors := []abi.Error{} + + erepo, err := NewErrorsRepository(customErrors) + require.NoError(t, err) + s, args, err := erepo.ParseError( + hexToBytes("0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a4e6f7420656e6f7567682045746865722070726f76696465642e000000000000"), + ) + require.NoError(t, err) + require.Equal(t, "Error(string)", s) + require.Equal(t, []interface{}{"Not enough Ether provided."}, args) +} + +func TestRevertReasonParserAddedCustomError(t *testing.T) { + uintT, err := abi.NewType("uint256", "", nil) + if err != nil { + panic(err) + } + customErrors := []abi.Error{ + { + Name: "AppError", + Inputs: []abi.Argument{ + { + Type: uintT, + }, + }, + }, + } + + erepo, err := NewErrorsRepository(customErrors) + require.NoError(t, err) + s, args, err := erepo.ParseError( + hexToBytes("0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a4e6f7420656e6f7567682045746865722070726f76696465642e000000000000"), + ) + require.NoError(t, err) + require.Equal(t, "Error(string)", s) + require.Equal(t, []interface{}{"Not enough Ether provided."}, args) +} + +func TestRevertReasonParserCustomError(t *testing.T) { + strT, err := abi.NewType("string", "", nil) + if err != nil { + panic(err) + } + uintT, err := abi.NewType("uint256", "", nil) + if err != nil { + panic(err) + } + customErrors := []abi.Error{ + { + Name: "AppError", + Inputs: []abi.Argument{ + { + Type: strT, + }, + }, + }, + { + Name: "InsufficientBalance", + Inputs: []abi.Argument{ + { + Type: uintT, + }, + { + Type: uintT, + }, + }, + }, + } + + erepo, err := NewErrorsRepository(customErrors) + require.NoError(t, err) + s, args, err := erepo.ParseError( + hexToBytes("0xcf47918100000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000009"), + ) + require.NoError(t, err) + require.Equal(t, "InsufficientBalance(uint256,uint256)", s) + require.Equal(t, []interface{}{big.NewInt(7), big.NewInt(9)}, args) +} + +func hexToBytes(s string) []byte { + reason, err := hex.DecodeString(strings.TrimPrefix(s, "0x")) + if err != nil { + panic(err) + } + return reason +} diff --git a/pkg/relay/ethereum/tx.go b/pkg/relay/ethereum/tx.go index fbc9f7e..e4fd57c 100644 --- a/pkg/relay/ethereum/tx.go +++ b/pkg/relay/ethereum/tx.go @@ -44,12 +44,20 @@ func (c *Chain) SendMsgs(msgs []sdk.Msg) ([]core.MsgID, error) { opts.NoSend = true tx, err = c.SendTx(opts, msg, skipUpdateClientCommitment) if err != nil { - logger.Error("failed to send msg / NoSend: true", err, "msg", msg) + logger.Error("failed to send msg / NoSend: true", err) return nil, err } estimatedGas, err := c.client.EstimateGasFromTx(ctx, tx) if err != nil { - logger.Error("failed to estimate gas", err, "msg", msg) + logger.Error("failed to estimate gas", err) + if c.config.EnableDebugTrace { + revertReason, err := c.client.DebugTraceTransaction(ctx, tx.Hash()) + if err != nil { + logger.Error("debug trace transaction", err, "revert_reason", revertReason) + } + revertReason = GetRevertReason([]byte(revertReason), c.config.AbiPaths) + logger.Error("failed to estimate gas", err, "revert_reason", revertReason) + } return nil, err } txGasLimit := estimatedGas * c.Config().GasEstimateRate.Numerator / c.Config().GasEstimateRate.Denominator @@ -61,20 +69,32 @@ func (c *Chain) SendMsgs(msgs []sdk.Msg) ([]core.MsgID, error) { opts.NoSend = false tx, err = c.SendTx(opts, msg, skipUpdateClientCommitment) if err != nil { - logger.Error("failed to send msg / NoSend: false", err, "msg", msg) + logger.Error("failed to send msg / NoSend: false", err) return nil, err } - if receipt, revertReason, err := c.client.WaitForReceiptAndGet(ctx, tx.Hash(), c.config.EnableDebugTrace); err != nil { - logger.Error("failed to get receipt", err, "msg", msg) + receipt, err := c.client.WaitForReceiptAndGet(ctx, tx.Hash()) + if err != nil { + logger.Error("failed to get receipt", err) return nil, err - } else if receipt.Status == gethtypes.ReceiptStatusFailed { - err := fmt.Errorf("tx execution failed: revertReason=%s, msgIndex=%d, msg=%v", revertReason, i, msg) - logger.Error("tx execution failed", err, "revert_reason", revertReason, "msg_index", i, "msg", msg) + } + if receipt.Status == gethtypes.ReceiptStatusFailed { + var revertReason string + if receipt.HasRevertReason() { + revertReason = GetRevertReason(receipt.RevertReason, c.config.AbiPaths) + } else if c.config.EnableDebugTrace { + revertReason, err = c.client.DebugTraceTransaction(ctx, tx.Hash()) + if err != nil { + logger.Error("debug trace transaction", err, "revert_reason", revertReason) + } + revertReason = GetRevertReason([]byte(revertReason), c.config.AbiPaths) + } + err = fmt.Errorf("revert_reason=%s", revertReason) + logger.Error("tx execution failed", err, "msg_index", i) return nil, err } if c.msgEventListener != nil { if err := c.msgEventListener.OnSentMsg([]sdk.Msg{msg}); err != nil { - logger.Error("failed to OnSendMsg call", err, "msg", msg) + logger.Error("failed to OnSendMsg call", err) } } msgIDs = append(msgIDs, NewMsgID(tx.Hash())) @@ -83,17 +103,31 @@ func (c *Chain) SendMsgs(msgs []sdk.Msg) ([]core.MsgID, error) { } func (c *Chain) GetMsgResult(id core.MsgID) (core.MsgResult, error) { + logger := c.GetChainLogger() msgID, ok := id.(*MsgID) if !ok { return nil, fmt.Errorf("unexpected message id type: %T", id) } - - receipt, revertReason, err := c.client.WaitForReceiptAndGet(context.TODO(), msgID.TxHash(), c.config.EnableDebugTrace) + ctx := context.TODO() + txHash := msgID.TxHash() + receipt, err := c.client.WaitForReceiptAndGet(ctx, txHash) if err != nil { return nil, err } - - return c.makeMsgResultFromReceipt(receipt, revertReason) + if receipt.Status == gethtypes.ReceiptStatusSuccessful { + return c.makeMsgResultFromReceipt(&receipt.Receipt, "") + } + var revertReason string + if receipt.HasRevertReason() { + revertReason = GetRevertReason(receipt.RevertReason, c.config.AbiPaths) + } else if c.config.EnableDebugTrace { + revertReason, err = c.client.DebugTraceTransaction(ctx, txHash) + if err != nil { + logger.Error("debug trace transaction", err, "revert_reason", revertReason) + } + revertReason = GetRevertReason([]byte(revertReason), c.config.AbiPaths) + } + return c.makeMsgResultFromReceipt(&receipt.Receipt, revertReason) } func (c *Chain) TxCreateClient(opts *bind.TransactOpts, msg *clienttypes.MsgCreateClient) (*gethtypes.Transaction, error) { diff --git a/proto/buf.lock b/proto/buf.lock index 36dc795..7dd0839 100644 --- a/proto/buf.lock +++ b/proto/buf.lock @@ -14,13 +14,13 @@ deps: - remote: buf.build owner: cosmos repository: gogo-proto - commit: 5e5b9fdd01804356895f8f79a6f1ddc1 - digest: shake256:0b85da49e2e5f9ebc4806eae058e2f56096ff3b1c59d1fb7c190413dd15f45dd456f0b69ced9059341c80795d2b6c943de15b120a9e0308b499e43e4b5fc2952 + commit: 88ef6483f90f478fb938c37dde52ece3 + digest: shake256:89c45df2aa11e0cff97b0d695436713db3d993d76792e9f8dc1ae90e6ab9a9bec55503d48ceedd6b86069ab07d3041b32001b2bfe0227fa725dd515ff381e5ba - remote: buf.build owner: cosmos repository: ibc - commit: c805262eb05540e7a4d8723e8b39108e - digest: shake256:1c90107847d072bcf9de2f56cefe162b01dea4adc10316e7fb506942ee73a0f87ccd5747b2abb749d6c403b48c814cdfeefe570bd981b81778f27684babddf0b + commit: e2006674271c4a53bf0b98b66de0fc50 + digest: shake256:0b14a68d79c8e911da13735affed05674e2d81f157a9a804f8761db29fba77f61af1e711181cfde8bbf3a81a81c0ac9f553b269f95d59896dd193251aa35929e - remote: buf.build owner: cosmos repository: ics23 diff --git a/proto/relayer/chains/ethereum/config/config.proto b/proto/relayer/chains/ethereum/config/config.proto index 11c4e28..18789bd 100644 --- a/proto/relayer/chains/ethereum/config/config.proto +++ b/proto/relayer/chains/ethereum/config/config.proto @@ -36,6 +36,8 @@ message ChainConfig { DynamicTxGasConfig dynamic_tx_gas_config = 15; uint64 blocks_per_event_query = 16; + + repeated string abi_paths = 17; } message AllowLCFunctionsConfig { @@ -57,4 +59,3 @@ message DynamicTxGasConfig { uint32 fee_history_reward_percentile = 5; uint32 max_retry_for_fee_history = 6; } - From dc2faf62ac4663a36ed77f17f677865d52fce9c7 Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Wed, 24 Apr 2024 09:32:15 +0900 Subject: [PATCH 02/13] fix to the way to get revert reason Signed-off-by: Masanori Yoshida --- pkg/client/client.go | 102 ++++++++++++++++----------------------- pkg/relay/ethereum/tx.go | 77 ++++++++++++++++++----------- 2 files changed, 90 insertions(+), 89 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index f1c0d98..8b44a98 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -3,7 +3,6 @@ package client import ( "context" "encoding/json" - "fmt" "math/big" "time" @@ -93,6 +92,12 @@ func (cl *ETHClient) WaitForReceiptAndGet(ctx context.Context, txHash common.Has return receipt, nil } +func (cl *ETHClient) DebugTraceTransaction(ctx context.Context, txHash common.Hash) (CallFrame, error) { + var callFrame CallFrame + err := cl.Raw().CallContext(ctx, &callFrame, "debug_traceTransaction", txHash, map[string]string{"tracer": "callTracer"}) + return callFrame, err +} + type Receipt struct { gethtypes.Receipt RevertReason []byte `json:"revertReason,omitempty"` @@ -102,49 +107,14 @@ func (rc Receipt) HasRevertReason() bool { return len(rc.RevertReason) > 0 } -func (cl *ETHClient) EstimateGasFromTx(ctx context.Context, tx *gethtypes.Transaction) (uint64, error) { - from, err := gethtypes.LatestSignerForChainID(tx.ChainId()).Sender(tx) - if err != nil { - return 0, err - } - to := tx.To() - value := tx.Value() - gasTipCap := tx.GasTipCap() - gasFeeCap := tx.GasFeeCap() - gasPrice := tx.GasPrice() - data := tx.Data() - accessList := tx.AccessList() - callMsg := ethereum.CallMsg{ - From: from, - To: to, - GasPrice: gasPrice, - GasTipCap: gasTipCap, - GasFeeCap: gasFeeCap, - Value: value, - Data: data, - AccessList: accessList, - } - estimatedGas, err := cl.EstimateGas(ctx, callMsg) - if err != nil { - return 0, err - } - return estimatedGas, nil -} - -func (cl *ETHClient) DebugTraceTransaction(ctx context.Context, txHash common.Hash) (string, error) { - var result *callFrame - if err := cl.Raw().CallContext(ctx, &result, "debug_traceTransaction", txHash, map[string]string{"tracer": "callTracer"}); err != nil { - return "", err - } - revertReason, err := searchRevertReason(result) - if err != nil { - return "", err - } - return revertReason, nil +type CallLog struct { + Address common.Address `json:"address"` + Topics []common.Hash `json:"topics"` + Data hexutil.Bytes `json:"data"` } // see: https://github.com/ethereum/go-ethereum/blob/v1.12.0/eth/tracers/native/call.go#L44-L59 -type callFrame struct { +type CallFrame struct { Type vm.OpCode `json:"-"` From common.Address `json:"from"` Gas uint64 `json:"gas"` @@ -154,21 +124,15 @@ type callFrame struct { Output []byte `json:"output,omitempty" rlp:"optional"` Error string `json:"error,omitempty" rlp:"optional"` RevertReason string `json:"revertReason,omitempty"` - Calls []callFrame `json:"calls,omitempty" rlp:"optional"` - Logs []callLog `json:"logs,omitempty" rlp:"optional"` + Calls []CallFrame `json:"calls,omitempty" rlp:"optional"` + Logs []CallLog `json:"logs,omitempty" rlp:"optional"` // Placed at end on purpose. The RLP will be decoded to 0 instead of // nil if there are non-empty elements after in the struct. Value *big.Int `json:"value,omitempty" rlp:"optional"` } -type callLog struct { - Address common.Address `json:"address"` - Topics []common.Hash `json:"topics"` - Data hexutil.Bytes `json:"data"` -} - // UnmarshalJSON unmarshals from JSON. -func (c *callFrame) UnmarshalJSON(input []byte) error { +func (c *CallFrame) UnmarshalJSON(input []byte) error { type callFrame0 struct { Type *vm.OpCode `json:"-"` From *common.Address `json:"from"` @@ -179,8 +143,8 @@ func (c *callFrame) UnmarshalJSON(input []byte) error { Output *hexutil.Bytes `json:"output,omitempty" rlp:"optional"` Error *string `json:"error,omitempty" rlp:"optional"` RevertReason *string `json:"revertReason,omitempty"` - Calls []callFrame `json:"calls,omitempty" rlp:"optional"` - Logs []callLog `json:"logs,omitempty" rlp:"optional"` + Calls []CallFrame `json:"calls,omitempty" rlp:"optional"` + Logs []CallLog `json:"logs,omitempty" rlp:"optional"` Value *hexutil.Big `json:"value,omitempty" rlp:"optional"` } var dec callFrame0 @@ -226,15 +190,31 @@ func (c *callFrame) UnmarshalJSON(input []byte) error { return nil } -func searchRevertReason(result *callFrame) (string, error) { - if result.RevertReason != "" { - return result.RevertReason, nil +func (cl *ETHClient) EstimateGasFromTx(ctx context.Context, tx *gethtypes.Transaction) (uint64, error) { + from, err := gethtypes.LatestSignerForChainID(tx.ChainId()).Sender(tx) + if err != nil { + return 0, err } - for _, call := range result.Calls { - reason, err := searchRevertReason(&call) - if err == nil { - return reason, nil - } + to := tx.To() + value := tx.Value() + gasTipCap := tx.GasTipCap() + gasFeeCap := tx.GasFeeCap() + gasPrice := tx.GasPrice() + data := tx.Data() + accessList := tx.AccessList() + callMsg := ethereum.CallMsg{ + From: from, + To: to, + GasPrice: gasPrice, + GasTipCap: gasTipCap, + GasFeeCap: gasFeeCap, + Value: value, + Data: data, + AccessList: accessList, + } + estimatedGas, err := cl.EstimateGas(ctx, callMsg) + if err != nil { + return 0, err } - return "", fmt.Errorf("revert reason not found") + return estimatedGas, nil } diff --git a/pkg/relay/ethereum/tx.go b/pkg/relay/ethereum/tx.go index e4fd57c..32f7dc0 100644 --- a/pkg/relay/ethereum/tx.go +++ b/pkg/relay/ethereum/tx.go @@ -14,10 +14,13 @@ import ( "github.com/cosmos/ibc-go/v7/modules/core/exported" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" gethtypes "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/rpc" "github.com/hyperledger-labs/yui-relayer/core" "github.com/hyperledger-labs/yui-relayer/log" + "github.com/datachainlab/ethereum-ibc-relay-chain/pkg/client" "github.com/datachainlab/ethereum-ibc-relay-chain/pkg/contract/ibchandler" ) @@ -49,15 +52,12 @@ func (c *Chain) SendMsgs(msgs []sdk.Msg) ([]core.MsgID, error) { } estimatedGas, err := c.client.EstimateGasFromTx(ctx, tx) if err != nil { - logger.Error("failed to estimate gas", err) - if c.config.EnableDebugTrace { - revertReason, err := c.client.DebugTraceTransaction(ctx, tx.Hash()) - if err != nil { - logger.Error("debug trace transaction", err, "revert_reason", revertReason) - } - revertReason = GetRevertReason([]byte(revertReason), c.config.AbiPaths) - logger.Error("failed to estimate gas", err, "revert_reason", revertReason) + revertReason, err2 := c.getRevertReasonFromEstimateGas(err) + if err2 != nil { + logger.Error("failed to get revert reason", err2, "msg_index", i) } + + logger.Error("failed to estimate gas", err, "revert_reason", revertReason, "msg_index", i) return nil, err } txGasLimit := estimatedGas * c.Config().GasEstimateRate.Numerator / c.Config().GasEstimateRate.Denominator @@ -78,18 +78,13 @@ func (c *Chain) SendMsgs(msgs []sdk.Msg) ([]core.MsgID, error) { return nil, err } if receipt.Status == gethtypes.ReceiptStatusFailed { - var revertReason string - if receipt.HasRevertReason() { - revertReason = GetRevertReason(receipt.RevertReason, c.config.AbiPaths) - } else if c.config.EnableDebugTrace { - revertReason, err = c.client.DebugTraceTransaction(ctx, tx.Hash()) - if err != nil { - logger.Error("debug trace transaction", err, "revert_reason", revertReason) - } - revertReason = GetRevertReason([]byte(revertReason), c.config.AbiPaths) + revertReason, err2 := c.getRevertReasonFromReceipt(ctx, receipt) + if err2 != nil { + logger.Error("failed to get revert reason", err2, "msg_index", i) } - err = fmt.Errorf("revert_reason=%s", revertReason) - logger.Error("tx execution failed", err, "msg_index", i) + + err := fmt.Errorf("tx execution reverted: revertReason=%s, msgIndex=%d", revertReason, i) + logger.Error("tx execution reverted", err, "revert_reason", revertReason, "msg_index", i) return nil, err } if c.msgEventListener != nil { @@ -104,6 +99,7 @@ func (c *Chain) SendMsgs(msgs []sdk.Msg) ([]core.MsgID, error) { func (c *Chain) GetMsgResult(id core.MsgID) (core.MsgResult, error) { logger := c.GetChainLogger() + msgID, ok := id.(*MsgID) if !ok { return nil, fmt.Errorf("unexpected message id type: %T", id) @@ -117,15 +113,9 @@ func (c *Chain) GetMsgResult(id core.MsgID) (core.MsgResult, error) { if receipt.Status == gethtypes.ReceiptStatusSuccessful { return c.makeMsgResultFromReceipt(&receipt.Receipt, "") } - var revertReason string - if receipt.HasRevertReason() { - revertReason = GetRevertReason(receipt.RevertReason, c.config.AbiPaths) - } else if c.config.EnableDebugTrace { - revertReason, err = c.client.DebugTraceTransaction(ctx, txHash) - if err != nil { - logger.Error("debug trace transaction", err, "revert_reason", revertReason) - } - revertReason = GetRevertReason([]byte(revertReason), c.config.AbiPaths) + revertReason, err := c.getRevertReasonFromReceipt(ctx, receipt) + if err != nil { + logger.Error("failed to get revert reason", err) } return c.makeMsgResultFromReceipt(&receipt.Receipt, revertReason) } @@ -372,3 +362,34 @@ func (c *Chain) SendTx(opts *bind.TransactOpts, msg sdk.Msg, skipUpdateClientCom } return tx, err } + +func (c *Chain) getRevertReasonFromReceipt(ctx context.Context, receipt *client.Receipt) (string, error) { + var errorData []byte + if receipt.HasRevertReason() { + errorData = receipt.RevertReason + } else if c.config.EnableDebugTrace { + callFrame, err := c.client.DebugTraceTransaction(ctx, receipt.TxHash) + if err != nil { + return "", err + } else if len(callFrame.Output) == 0 { + return "", fmt.Errorf("execution reverted without error data") + } + } else { + return "", fmt.Errorf("no way to get revert reason") + } + + revertReason := GetRevertReason(errorData, c.config.AbiPaths) + return revertReason, nil +} + +func (c *Chain) getRevertReasonFromEstimateGas(err error) (string, error) { + if de, ok := err.(rpc.DataError); !ok { + return "", fmt.Errorf("eth_estimateGas failed with unexpected error type: errorType=%T", err) + } else if de.ErrorData() == nil { + return "", fmt.Errorf("eth_estimateGas failed without error data") + } else { + errorData := common.FromHex(de.ErrorData().(string)) + revertReason := GetRevertReason(errorData, c.config.AbiPaths) + return revertReason, nil + } +} From d3716256e674b0bce01d8b6307fadce2a3029a72 Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Wed, 24 Apr 2024 11:12:08 +0900 Subject: [PATCH 03/13] overhaul ErrorRepository Signed-off-by: Masanori Yoshida --- pkg/relay/ethereum/chain.go | 11 +- pkg/relay/ethereum/config.go | 5 + pkg/relay/ethereum/error_parse.go | 149 +++++++++---------------- pkg/relay/ethereum/error_parse_test.go | 72 ++++-------- pkg/relay/ethereum/tx.go | 10 +- 5 files changed, 93 insertions(+), 154 deletions(-) diff --git a/pkg/relay/ethereum/chain.go b/pkg/relay/ethereum/chain.go index 7310478..32dddef 100644 --- a/pkg/relay/ethereum/chain.go +++ b/pkg/relay/ethereum/chain.go @@ -45,6 +45,8 @@ type Chain struct { signer Signer + errorRepository ErrorRepository + // cache connectionOpenedConfirmed bool allowLCFunctions *AllowLCFunctions @@ -79,6 +81,10 @@ func NewChain(config ChainConfig) (*Chain, error) { return nil, fmt.Errorf("failed to build allowLcFunctions: %v", err) } } + errorRepository, err := CreateErrorRepository(config.AbiPaths) + if err != nil { + return nil, fmt.Errorf("failed to create error repository: %v", err) + } return &Chain{ config: config, client: client, @@ -86,7 +92,10 @@ func NewChain(config ChainConfig) (*Chain, error) { ibcHandler: ibcHandler, - signer: signer, + signer: signer, + + errorRepository: errorRepository, + allowLCFunctions: alfs, }, nil } diff --git a/pkg/relay/ethereum/config.go b/pkg/relay/ethereum/config.go index a807902..4151975 100644 --- a/pkg/relay/ethereum/config.go +++ b/pkg/relay/ethereum/config.go @@ -79,6 +79,11 @@ func (c ChainConfig) Validate() error { } } } + for i, path := range c.AbiPaths { + if isEmpty(path) { + errs = append(errs, fmt.Errorf("config attribute \"abi_paths[%d]\" is empty", i)) + } + } return errors.Join(errs...) } diff --git a/pkg/relay/ethereum/error_parse.go b/pkg/relay/ethereum/error_parse.go index 586b9b9..8921130 100644 --- a/pkg/relay/ethereum/error_parse.go +++ b/pkg/relay/ethereum/error_parse.go @@ -1,6 +1,7 @@ package ethereum import ( + "bytes" "encoding/json" "fmt" "os" @@ -10,26 +11,11 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi" ) -type Artifact struct { - ABI []interface{} `json:"abi"` -} - -type ErrorsRepository struct { - errs map[[4]byte]abi.Error -} +type ErrorRepository map[[4]byte]abi.Error -var erepo *ErrorsRepository - -func GetErepo(abiPaths []string) (*ErrorsRepository, error) { - var erepoErr error - if erepo == nil { - erepo, erepoErr = CreateErepo(abiPaths) - } - return erepo, erepoErr -} +func CreateErrorRepository(abiPaths []string) (ErrorRepository, error) { + var errABIs []abi.Error -func CreateErepo(abiPaths []string) (*ErrorsRepository, error) { - var abiErrors []abi.Error for _, dir := range abiPaths { if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil { @@ -40,22 +26,10 @@ func CreateErepo(abiPaths []string) (*ErrorsRepository, error) { if err != nil { return err } - var artifact Artifact - var abiData []byte - if err := json.Unmarshal(data, &artifact); err != nil { - abiData = data - } else { - abiData, err = json.Marshal(artifact.ABI) - if err != nil { - return err - } - } - abiABI, err := abi.JSON(strings.NewReader(string(abiData))) - if err != nil { - return err - } - for _, error := range abiABI.Errors { - abiErrors = append(abiErrors, error) + + contractABI, err := abi.JSON(bytes.NewReader(data)) + for _, error := range contractABI.Errors { + errABIs = append(errABIs, error) } } return nil @@ -63,98 +37,77 @@ func CreateErepo(abiPaths []string) (*ErrorsRepository, error) { return nil, err } } - return NewErrorsRepository(abiErrors) + + return NewErrorRepository(errABIs) } -func NewErrorsRepository(customErrors []abi.Error) (*ErrorsRepository, error) { - defaultErrs, err := defaultErrors() - if err != nil { - return nil, err - } - customErrors = append(customErrors, defaultErrs...) - er := ErrorsRepository{ - errs: make(map[[4]byte]abi.Error), - } - for _, e := range customErrors { - e := abi.NewError(e.Name, e.Inputs) - if err := er.Add(e); err != nil { +func NewErrorRepository(errABIs []abi.Error) (ErrorRepository, error) { + repo := make(ErrorRepository) + for _, errABI := range errABIs { + if err := repo.Add(errABI); err != nil { return nil, err } } - return &er, nil + + return repo, nil } -func (r *ErrorsRepository) Add(e0 abi.Error) error { +func (r ErrorRepository) Add(errABI abi.Error) error { var sel [4]byte - copy(sel[:], e0.ID[:4]) - if e1, ok := r.errs[sel]; ok { - if e1.Sig == e0.Sig { + copy(sel[:], errABI.ID[:4]) + if existingErrABI, ok := r[sel]; ok { + if existingErrABI.Sig == errABI.Sig { return nil } - return fmt.Errorf("error selector collision: sel=%x e0=%v e1=%v", sel, e0, e1) + return fmt.Errorf("error selector collision: selector=%x, newErrABI=%v, existingErrABI=%v", sel, errABI, existingErrABI) } - r.errs[sel] = e0 + r[sel] = errABI return nil } -func (r *ErrorsRepository) GetError(sel [4]byte) (abi.Error, bool) { - e, ok := r.errs[sel] - return e, ok -} - -func (r *ErrorsRepository) ParseError(bz []byte) (string, interface{}, error) { - if len(bz) < 4 { - return "", nil, fmt.Errorf("invalid error data: %v", bz) +func (r ErrorRepository) Get(errorData []byte) (abi.Error, error) { + if len(errorData) < 4 { + return abi.Error{}, fmt.Errorf("the size of error data is less than 4 bytes: errorData=%x", errorData) } var sel [4]byte - copy(sel[:], bz[:4]) - e, ok := r.GetError(sel) + copy(sel[:], errorData[:4]) + errABI, ok := r[sel] if !ok { - return "", nil, fmt.Errorf("unknown error: sel=%x", sel) + return abi.Error{}, fmt.Errorf("error ABI not found") } - v, err := e.Unpack(bz) - return e.Sig, v, err + return errABI, nil } -func defaultErrors() ([]abi.Error, error) { - strT, err := abi.NewType("string", "", nil) - if err != nil { - return nil, err +func errorToJSON(errVal interface{}, errABI abi.Error) (string, error) { + m := make(map[string]interface{}) + for i, v := range errVal.([]interface{}) { + m[errABI.Inputs[i].Name] = v } - uintT, err := abi.NewType("uint256", "", nil) + bz, err := json.Marshal(m) if err != nil { - return nil, err + return "", err } + return string(bz), nil +} - errors := []abi.Error{ - { - Name: "Error", - Inputs: []abi.Argument{ - { - Type: strT, - }, - }, - }, - { - Name: "Panic", - Inputs: []abi.Argument{ - { - Type: uintT, - }, - }, - }, +func (r ErrorRepository) ParseError(errorData []byte) (string, error) { + // handle Error(string) and Panic(uint256) + if revertReason, err := abi.UnpackRevert(errorData); err == nil { + return revertReason, nil } - return errors, nil -} -func GetRevertReason(revertReason []byte, abiPaths []string) string { - erepo, err := GetErepo(abiPaths) + // handle custom error + errABI, err := r.Get(errorData) + if err != nil { + return "", fmt.Errorf("failed to find error ABI: %v", err) + } + errVal, err := errABI.Unpack(errorData) if err != nil { - return fmt.Sprintf("GetErepo err=%v", err) + return "", fmt.Errorf("failed to unpack error: %v", err) } - sig, args, err := erepo.ParseError(revertReason) + errStr, err := errorToJSON(errVal, errABI) if err != nil { - return fmt.Sprintf("raw-revert-reason=\"%x\" parse-err=\"%v\"", revertReason, err) + return "", fmt.Errorf("failed to marshal error inputs into JSON: %v", err) } - return fmt.Sprintf("revert-reason=\"%v\" args=\"%v\"", sig, args) + return fmt.Sprintf("%s%s", errABI.Name, errStr), nil } diff --git a/pkg/relay/ethereum/error_parse_test.go b/pkg/relay/ethereum/error_parse_test.go index 716128b..580458b 100644 --- a/pkg/relay/ethereum/error_parse_test.go +++ b/pkg/relay/ethereum/error_parse_test.go @@ -1,26 +1,24 @@ package ethereum import ( - "encoding/hex" - "math/big" - "strings" "testing" "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/require" ) func TestRevertReasonParserDefault(t *testing.T) { customErrors := []abi.Error{} - erepo, err := NewErrorsRepository(customErrors) + erepo, err := NewErrorRepository(customErrors) require.NoError(t, err) - s, args, err := erepo.ParseError( - hexToBytes("0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a4e6f7420656e6f7567682045746865722070726f76696465642e000000000000"), + + revertReason, err := erepo.ParseError( + common.FromHex("0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a4e6f7420656e6f7567682045746865722070726f76696465642e000000000000"), ) require.NoError(t, err) - require.Equal(t, "Error(string)", s) - require.Equal(t, []interface{}{"Not enough Ether provided."}, args) + require.Equal(t, "Not enough Ether provided.", revertReason) } func TestRevertReasonParserAddedCustomError(t *testing.T) { @@ -29,24 +27,17 @@ func TestRevertReasonParserAddedCustomError(t *testing.T) { panic(err) } customErrors := []abi.Error{ - { - Name: "AppError", - Inputs: []abi.Argument{ - { - Type: uintT, - }, - }, - }, + abi.NewError("AppError", abi.Arguments{{Name: "x", Type: uintT}}), } - erepo, err := NewErrorsRepository(customErrors) + erepo, err := NewErrorRepository(customErrors) require.NoError(t, err) - s, args, err := erepo.ParseError( - hexToBytes("0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a4e6f7420656e6f7567682045746865722070726f76696465642e000000000000"), + + revertReason, err := erepo.ParseError( + common.FromHex("0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a4e6f7420656e6f7567682045746865722070726f76696465642e000000000000"), ) require.NoError(t, err) - require.Equal(t, "Error(string)", s) - require.Equal(t, []interface{}{"Not enough Ether provided."}, args) + require.Equal(t, "Not enough Ether provided.", revertReason) } func TestRevertReasonParserCustomError(t *testing.T) { @@ -59,41 +50,16 @@ func TestRevertReasonParserCustomError(t *testing.T) { panic(err) } customErrors := []abi.Error{ - { - Name: "AppError", - Inputs: []abi.Argument{ - { - Type: strT, - }, - }, - }, - { - Name: "InsufficientBalance", - Inputs: []abi.Argument{ - { - Type: uintT, - }, - { - Type: uintT, - }, - }, - }, + abi.NewError("AppError", abi.Arguments{{Name: "x", Type: strT}}), + abi.NewError("InsufficientBalance", abi.Arguments{{Name: "y", Type: uintT}, {Name: "z", Type: uintT}}), } - erepo, err := NewErrorsRepository(customErrors) + erepo, err := NewErrorRepository(customErrors) require.NoError(t, err) - s, args, err := erepo.ParseError( - hexToBytes("0xcf47918100000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000009"), + + revertReason, err := erepo.ParseError( + common.FromHex("0xcf47918100000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000009"), ) require.NoError(t, err) - require.Equal(t, "InsufficientBalance(uint256,uint256)", s) - require.Equal(t, []interface{}{big.NewInt(7), big.NewInt(9)}, args) -} - -func hexToBytes(s string) []byte { - reason, err := hex.DecodeString(strings.TrimPrefix(s, "0x")) - if err != nil { - panic(err) - } - return reason + require.Equal(t, `InsufficientBalance{"y":7,"z":9}`, revertReason) } diff --git a/pkg/relay/ethereum/tx.go b/pkg/relay/ethereum/tx.go index 32f7dc0..b6df99e 100644 --- a/pkg/relay/ethereum/tx.go +++ b/pkg/relay/ethereum/tx.go @@ -378,7 +378,10 @@ func (c *Chain) getRevertReasonFromReceipt(ctx context.Context, receipt *client. return "", fmt.Errorf("no way to get revert reason") } - revertReason := GetRevertReason(errorData, c.config.AbiPaths) + revertReason, err := c.errorRepository.ParseError(errorData) + if err != nil { + return "", fmt.Errorf("failed to parse error: %v", err) + } return revertReason, nil } @@ -389,7 +392,10 @@ func (c *Chain) getRevertReasonFromEstimateGas(err error) (string, error) { return "", fmt.Errorf("eth_estimateGas failed without error data") } else { errorData := common.FromHex(de.ErrorData().(string)) - revertReason := GetRevertReason(errorData, c.config.AbiPaths) + revertReason, err := c.errorRepository.ParseError(errorData) + if err != nil { + return "", fmt.Errorf("failed to parse error: %v", err) + } return revertReason, nil } } From c852e9fb87b42424970f975da34099c16da004c6 Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Wed, 24 Apr 2024 13:14:47 +0900 Subject: [PATCH 04/13] fix a bug Signed-off-by: Masanori Yoshida --- pkg/relay/ethereum/tx.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/relay/ethereum/tx.go b/pkg/relay/ethereum/tx.go index b6df99e..1e16bea 100644 --- a/pkg/relay/ethereum/tx.go +++ b/pkg/relay/ethereum/tx.go @@ -374,6 +374,7 @@ func (c *Chain) getRevertReasonFromReceipt(ctx context.Context, receipt *client. } else if len(callFrame.Output) == 0 { return "", fmt.Errorf("execution reverted without error data") } + errorData = callFrame.Output } else { return "", fmt.Errorf("no way to get revert reason") } From 7d9990a9742112325da8a36877538d6f06dac754 Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Wed, 24 Apr 2024 13:15:21 +0900 Subject: [PATCH 05/13] avoid using panicable type assertions Signed-off-by: Masanori Yoshida --- pkg/relay/ethereum/error_parse.go | 11 +++++++++-- pkg/relay/ethereum/tx.go | 4 +++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/relay/ethereum/error_parse.go b/pkg/relay/ethereum/error_parse.go index 8921130..2a6c6b1 100644 --- a/pkg/relay/ethereum/error_parse.go +++ b/pkg/relay/ethereum/error_parse.go @@ -79,14 +79,21 @@ func (r ErrorRepository) Get(errorData []byte) (abi.Error, error) { } func errorToJSON(errVal interface{}, errABI abi.Error) (string, error) { + errVals, ok := errVal.([]interface{}) + if !ok { + return "", fmt.Errorf("error value has unexpected type: expected=[]interface{}, actual=%T", errVal) + } + m := make(map[string]interface{}) - for i, v := range errVal.([]interface{}) { + for i, v := range errVals { m[errABI.Inputs[i].Name] = v } + bz, err := json.Marshal(m) if err != nil { - return "", err + return "", fmt.Errorf("failed to marshal error value: %v", err) } + return string(bz), nil } diff --git a/pkg/relay/ethereum/tx.go b/pkg/relay/ethereum/tx.go index 1e16bea..b944b9e 100644 --- a/pkg/relay/ethereum/tx.go +++ b/pkg/relay/ethereum/tx.go @@ -391,8 +391,10 @@ func (c *Chain) getRevertReasonFromEstimateGas(err error) (string, error) { return "", fmt.Errorf("eth_estimateGas failed with unexpected error type: errorType=%T", err) } else if de.ErrorData() == nil { return "", fmt.Errorf("eth_estimateGas failed without error data") + } else if errorData, ok := de.ErrorData().(string); !ok { + return "", fmt.Errorf("eth_estimateGas failed with unexpected error data type: errorDataType=%T", de.ErrorData()) } else { - errorData := common.FromHex(de.ErrorData().(string)) + errorData := common.FromHex(errorData) revertReason, err := c.errorRepository.ParseError(errorData) if err != nil { return "", fmt.Errorf("failed to parse error: %v", err) From 16a993702768fcfda7c5b9000820d2e55ae62026 Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Wed, 24 Apr 2024 15:16:58 +0900 Subject: [PATCH 06/13] fix a bug Signed-off-by: Masanori Yoshida --- pkg/relay/ethereum/error_parse.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pkg/relay/ethereum/error_parse.go b/pkg/relay/ethereum/error_parse.go index 2a6c6b1..babedba 100644 --- a/pkg/relay/ethereum/error_parse.go +++ b/pkg/relay/ethereum/error_parse.go @@ -1,7 +1,6 @@ package ethereum import ( - "bytes" "encoding/json" "fmt" "os" @@ -22,14 +21,19 @@ func CreateErrorRepository(abiPaths []string) (ErrorRepository, error) { return err } if !info.IsDir() && strings.HasSuffix(info.Name(), ".json") { - data, err := os.ReadFile(path) + f, err := os.Open(path) if err != nil { return err } + defer f.Close() - contractABI, err := abi.JSON(bytes.NewReader(data)) - for _, error := range contractABI.Errors { - errABIs = append(errABIs, error) + contractABI, err := abi.JSON(f) + if err != nil { + return err + } + + for _, errABI := range contractABI.Errors { + errABIs = append(errABIs, errABI) } } return nil From aa347c27263ee5a4eeb5bcd6894ddaaa2e96e415 Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Wed, 24 Apr 2024 15:33:53 +0900 Subject: [PATCH 07/13] make errors more informative Signed-off-by: Masanori Yoshida --- pkg/relay/ethereum/error_parse.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/relay/ethereum/error_parse.go b/pkg/relay/ethereum/error_parse.go index babedba..459bb46 100644 --- a/pkg/relay/ethereum/error_parse.go +++ b/pkg/relay/ethereum/error_parse.go @@ -18,18 +18,18 @@ func CreateErrorRepository(abiPaths []string) (ErrorRepository, error) { for _, dir := range abiPaths { if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil { - return err + return fmt.Errorf("error encountered during the file tree walk: err=%v, path=%s", err, path) } if !info.IsDir() && strings.HasSuffix(info.Name(), ".json") { f, err := os.Open(path) if err != nil { - return err + return fmt.Errorf("failed to open a file: err=%v, path=%s", err, path) } defer f.Close() contractABI, err := abi.JSON(f) if err != nil { - return err + return fmt.Errorf("failed to parse a ABI JSON file: err=%v, path=%s", err, path) } for _, errABI := range contractABI.Errors { @@ -38,7 +38,7 @@ func CreateErrorRepository(abiPaths []string) (ErrorRepository, error) { } return nil }); err != nil { - return nil, err + return nil, fmt.Errorf("failed to read ABIs from a directory: err=%v, dirpath=%s", err, dir) } } From 399dd8edae64eccff44ffe10e6dd1c78bca763d4 Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Thu, 25 Apr 2024 16:07:20 +0900 Subject: [PATCH 08/13] emit an error log that contains the whole error data returned by EVM if the corresponding error ABI is unknown Signed-off-by: Masanori Yoshida --- pkg/relay/ethereum/chain.go | 1 + pkg/relay/ethereum/error_parse.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/relay/ethereum/chain.go b/pkg/relay/ethereum/chain.go index 32dddef..5af83a1 100644 --- a/pkg/relay/ethereum/chain.go +++ b/pkg/relay/ethereum/chain.go @@ -85,6 +85,7 @@ func NewChain(config ChainConfig) (*Chain, error) { if err != nil { return nil, fmt.Errorf("failed to create error repository: %v", err) } + return &Chain{ config: config, client: client, diff --git a/pkg/relay/ethereum/error_parse.go b/pkg/relay/ethereum/error_parse.go index 459bb46..d28e4bd 100644 --- a/pkg/relay/ethereum/error_parse.go +++ b/pkg/relay/ethereum/error_parse.go @@ -77,7 +77,7 @@ func (r ErrorRepository) Get(errorData []byte) (abi.Error, error) { copy(sel[:], errorData[:4]) errABI, ok := r[sel] if !ok { - return abi.Error{}, fmt.Errorf("error ABI not found") + return abi.Error{}, fmt.Errorf("error ABI not found: errorData=%x", errorData) } return errABI, nil } From f6fc68e2d4d398f3cd24662fd3fe4f15be45bdfc Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Thu, 25 Apr 2024 17:09:40 +0900 Subject: [PATCH 09/13] add "msg_index" attribute to logging in the loop of `SendMsgs` Signed-off-by: Masanori Yoshida --- pkg/relay/ethereum/tx.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/relay/ethereum/tx.go b/pkg/relay/ethereum/tx.go index b944b9e..1591c58 100644 --- a/pkg/relay/ethereum/tx.go +++ b/pkg/relay/ethereum/tx.go @@ -47,7 +47,7 @@ func (c *Chain) SendMsgs(msgs []sdk.Msg) ([]core.MsgID, error) { opts.NoSend = true tx, err = c.SendTx(opts, msg, skipUpdateClientCommitment) if err != nil { - logger.Error("failed to send msg / NoSend: true", err) + logger.Error("failed to send msg / NoSend: true", err, "msg_index", i) return nil, err } estimatedGas, err := c.client.EstimateGasFromTx(ctx, tx) @@ -62,19 +62,19 @@ func (c *Chain) SendMsgs(msgs []sdk.Msg) ([]core.MsgID, error) { } txGasLimit := estimatedGas * c.Config().GasEstimateRate.Numerator / c.Config().GasEstimateRate.Denominator if txGasLimit > c.Config().MaxGasLimit { - logger.Warn("estimated gas exceeds max gas limit", "estimated_gas", txGasLimit, "max_gas_limit", c.Config().MaxGasLimit) + logger.Warn("estimated gas exceeds max gas limit", "estimated_gas", txGasLimit, "max_gas_limit", c.Config().MaxGasLimit, "msg_index", i) txGasLimit = c.Config().MaxGasLimit } opts.GasLimit = txGasLimit opts.NoSend = false tx, err = c.SendTx(opts, msg, skipUpdateClientCommitment) if err != nil { - logger.Error("failed to send msg / NoSend: false", err) + logger.Error("failed to send msg / NoSend: false", err, "msg_index", i) return nil, err } receipt, err := c.client.WaitForReceiptAndGet(ctx, tx.Hash()) if err != nil { - logger.Error("failed to get receipt", err) + logger.Error("failed to get receipt", err, "msg_index", i) return nil, err } if receipt.Status == gethtypes.ReceiptStatusFailed { @@ -89,7 +89,7 @@ func (c *Chain) SendMsgs(msgs []sdk.Msg) ([]core.MsgID, error) { } if c.msgEventListener != nil { if err := c.msgEventListener.OnSentMsg([]sdk.Msg{msg}); err != nil { - logger.Error("failed to OnSendMsg call", err) + logger.Error("failed to OnSendMsg call", err, "msg_index", i) } } msgIDs = append(msgIDs, NewMsgID(tx.Hash())) From d972fffc3db7ab50852b0bd45b7959e0e4dfc7ee Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Fri, 26 Apr 2024 08:40:55 +0900 Subject: [PATCH 10/13] change the way to handle default errors (Error and Panic) Signed-off-by: Masanori Yoshida --- pkg/relay/ethereum/error_parse.go | 29 ++++++++++++++++++++------ pkg/relay/ethereum/error_parse_test.go | 4 ++-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/pkg/relay/ethereum/error_parse.go b/pkg/relay/ethereum/error_parse.go index d28e4bd..1de0208 100644 --- a/pkg/relay/ethereum/error_parse.go +++ b/pkg/relay/ethereum/error_parse.go @@ -12,6 +12,22 @@ import ( type ErrorRepository map[[4]byte]abi.Error +func defaultErrorABIs() ([]abi.Error, error) { + strT, err := abi.NewType("string", "", nil) + if err != nil { + return nil, fmt.Errorf("failed to create string type: %v", err) + } + uintT, err := abi.NewType("uint256", "", nil) + if err != nil { + return nil, fmt.Errorf("failed to create uint256 type: %v", err) + } + + return []abi.Error{ + abi.NewError("Error", []abi.Argument{{Name: "desc", Type: strT}}), + abi.NewError("Panic", []abi.Argument{{Name: "reason", Type: uintT}}), + }, nil +} + func CreateErrorRepository(abiPaths []string) (ErrorRepository, error) { var errABIs []abi.Error @@ -46,6 +62,13 @@ func CreateErrorRepository(abiPaths []string) (ErrorRepository, error) { } func NewErrorRepository(errABIs []abi.Error) (ErrorRepository, error) { + defaultErrABIs, err := defaultErrorABIs() + if err != nil { + return nil, fmt.Errorf("failed to create default error ABIs: %v", err) + } + + errABIs = append(errABIs, defaultErrABIs...) + repo := make(ErrorRepository) for _, errABI := range errABIs { if err := repo.Add(errABI); err != nil { @@ -102,12 +125,6 @@ func errorToJSON(errVal interface{}, errABI abi.Error) (string, error) { } func (r ErrorRepository) ParseError(errorData []byte) (string, error) { - // handle Error(string) and Panic(uint256) - if revertReason, err := abi.UnpackRevert(errorData); err == nil { - return revertReason, nil - } - - // handle custom error errABI, err := r.Get(errorData) if err != nil { return "", fmt.Errorf("failed to find error ABI: %v", err) diff --git a/pkg/relay/ethereum/error_parse_test.go b/pkg/relay/ethereum/error_parse_test.go index 580458b..9070daf 100644 --- a/pkg/relay/ethereum/error_parse_test.go +++ b/pkg/relay/ethereum/error_parse_test.go @@ -18,7 +18,7 @@ func TestRevertReasonParserDefault(t *testing.T) { common.FromHex("0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a4e6f7420656e6f7567682045746865722070726f76696465642e000000000000"), ) require.NoError(t, err) - require.Equal(t, "Not enough Ether provided.", revertReason) + require.Equal(t, `Error{"desc":"Not enough Ether provided."}`, revertReason) } func TestRevertReasonParserAddedCustomError(t *testing.T) { @@ -37,7 +37,7 @@ func TestRevertReasonParserAddedCustomError(t *testing.T) { common.FromHex("0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a4e6f7420656e6f7567682045746865722070726f76696465642e000000000000"), ) require.NoError(t, err) - require.Equal(t, "Not enough Ether provided.", revertReason) + require.Equal(t, `Error{"desc":"Not enough Ether provided."}`, revertReason) } func TestRevertReasonParserCustomError(t *testing.T) { From aac514767669f2e5f7dc095ed3f9036aa55b76d9 Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Fri, 26 Apr 2024 13:38:00 +0900 Subject: [PATCH 11/13] add "raw_error_data" and "tx_hash" attributes to log outputs from `SendMsgs` Signed-off-by: Masanori Yoshida --- pkg/relay/ethereum/tx.go | 43 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/pkg/relay/ethereum/tx.go b/pkg/relay/ethereum/tx.go index 1591c58..88d8005 100644 --- a/pkg/relay/ethereum/tx.go +++ b/pkg/relay/ethereum/tx.go @@ -2,6 +2,7 @@ package ethereum import ( "context" + "encoding/hex" "errors" "fmt" math "math" @@ -52,12 +53,12 @@ func (c *Chain) SendMsgs(msgs []sdk.Msg) ([]core.MsgID, error) { } estimatedGas, err := c.client.EstimateGasFromTx(ctx, tx) if err != nil { - revertReason, err2 := c.getRevertReasonFromEstimateGas(err) + revertReason, rawErrorData, err2 := c.getRevertReasonFromEstimateGas(err) if err2 != nil { logger.Error("failed to get revert reason", err2, "msg_index", i) } - logger.Error("failed to estimate gas", err, "revert_reason", revertReason, "msg_index", i) + logger.Error("failed to estimate gas", err, "revert_reason", revertReason, "raw_error_data", hex.EncodeToString(rawErrorData), "msg_index", i) return nil, err } txGasLimit := estimatedGas * c.Config().GasEstimateRate.Numerator / c.Config().GasEstimateRate.Denominator @@ -74,22 +75,22 @@ func (c *Chain) SendMsgs(msgs []sdk.Msg) ([]core.MsgID, error) { } receipt, err := c.client.WaitForReceiptAndGet(ctx, tx.Hash()) if err != nil { - logger.Error("failed to get receipt", err, "msg_index", i) + logger.Error("failed to get receipt", err, "msg_index", i, "tx_hash", tx.Hash()) return nil, err } if receipt.Status == gethtypes.ReceiptStatusFailed { - revertReason, err2 := c.getRevertReasonFromReceipt(ctx, receipt) + revertReason, rawErrorData, err2 := c.getRevertReasonFromReceipt(ctx, receipt) if err2 != nil { - logger.Error("failed to get revert reason", err2, "msg_index", i) + logger.Error("failed to get revert reason", err2, "msg_index", i, "tx_hash", tx.Hash()) } - err := fmt.Errorf("tx execution reverted: revertReason=%s, msgIndex=%d", revertReason, i) - logger.Error("tx execution reverted", err, "revert_reason", revertReason, "msg_index", i) + err := fmt.Errorf("tx execution reverted: revertReason=%s, rawErrorData=%x, msgIndex=%d, txHash=%s", revertReason, rawErrorData, i, tx.Hash()) + logger.Error("tx execution reverted", err, "revert_reason", revertReason, "raw_error_data", hex.EncodeToString(rawErrorData), "msg_index", i, "tx_hash", tx.Hash()) return nil, err } if c.msgEventListener != nil { if err := c.msgEventListener.OnSentMsg([]sdk.Msg{msg}); err != nil { - logger.Error("failed to OnSendMsg call", err, "msg_index", i) + logger.Error("failed to OnSendMsg call", err, "msg_index", i, "tx_hash", tx.Hash()) } } msgIDs = append(msgIDs, NewMsgID(tx.Hash())) @@ -113,7 +114,7 @@ func (c *Chain) GetMsgResult(id core.MsgID) (core.MsgResult, error) { if receipt.Status == gethtypes.ReceiptStatusSuccessful { return c.makeMsgResultFromReceipt(&receipt.Receipt, "") } - revertReason, err := c.getRevertReasonFromReceipt(ctx, receipt) + revertReason, _, err := c.getRevertReasonFromReceipt(ctx, receipt) if err != nil { logger.Error("failed to get revert reason", err) } @@ -363,42 +364,42 @@ func (c *Chain) SendTx(opts *bind.TransactOpts, msg sdk.Msg, skipUpdateClientCom return tx, err } -func (c *Chain) getRevertReasonFromReceipt(ctx context.Context, receipt *client.Receipt) (string, error) { +func (c *Chain) getRevertReasonFromReceipt(ctx context.Context, receipt *client.Receipt) (string, []byte, error) { var errorData []byte if receipt.HasRevertReason() { errorData = receipt.RevertReason } else if c.config.EnableDebugTrace { callFrame, err := c.client.DebugTraceTransaction(ctx, receipt.TxHash) if err != nil { - return "", err + return "", nil, err } else if len(callFrame.Output) == 0 { - return "", fmt.Errorf("execution reverted without error data") + return "", nil, fmt.Errorf("execution reverted without error data") } errorData = callFrame.Output } else { - return "", fmt.Errorf("no way to get revert reason") + return "", nil, fmt.Errorf("no way to get revert reason") } revertReason, err := c.errorRepository.ParseError(errorData) if err != nil { - return "", fmt.Errorf("failed to parse error: %v", err) + return "", errorData, fmt.Errorf("failed to parse error: %v", err) } - return revertReason, nil + return revertReason, errorData, nil } -func (c *Chain) getRevertReasonFromEstimateGas(err error) (string, error) { +func (c *Chain) getRevertReasonFromEstimateGas(err error) (string, []byte, error) { if de, ok := err.(rpc.DataError); !ok { - return "", fmt.Errorf("eth_estimateGas failed with unexpected error type: errorType=%T", err) + return "", nil, fmt.Errorf("eth_estimateGas failed with unexpected error type: errorType=%T", err) } else if de.ErrorData() == nil { - return "", fmt.Errorf("eth_estimateGas failed without error data") + return "", nil, fmt.Errorf("eth_estimateGas failed without error data") } else if errorData, ok := de.ErrorData().(string); !ok { - return "", fmt.Errorf("eth_estimateGas failed with unexpected error data type: errorDataType=%T", de.ErrorData()) + return "", nil, fmt.Errorf("eth_estimateGas failed with unexpected error data type: errorDataType=%T", de.ErrorData()) } else { errorData := common.FromHex(errorData) revertReason, err := c.errorRepository.ParseError(errorData) if err != nil { - return "", fmt.Errorf("failed to parse error: %v", err) + return "", errorData, fmt.Errorf("failed to parse error: %v", err) } - return revertReason, nil + return revertReason, errorData, nil } } From fcb8da1647c8489d0c8cb2e5fdf6d8f66beea6ab Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Fri, 26 Apr 2024 14:48:30 +0900 Subject: [PATCH 12/13] output raw_error_data also in logging in GetMsgResult Signed-off-by: Masanori Yoshida --- pkg/relay/ethereum/tx.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/relay/ethereum/tx.go b/pkg/relay/ethereum/tx.go index 88d8005..1c2be34 100644 --- a/pkg/relay/ethereum/tx.go +++ b/pkg/relay/ethereum/tx.go @@ -114,9 +114,9 @@ func (c *Chain) GetMsgResult(id core.MsgID) (core.MsgResult, error) { if receipt.Status == gethtypes.ReceiptStatusSuccessful { return c.makeMsgResultFromReceipt(&receipt.Receipt, "") } - revertReason, _, err := c.getRevertReasonFromReceipt(ctx, receipt) + revertReason, rawErrorData, err := c.getRevertReasonFromReceipt(ctx, receipt) if err != nil { - logger.Error("failed to get revert reason", err) + logger.Error("failed to get revert reason", err, "raw_error_data", hex.EncodeToString(rawErrorData)) } return c.makeMsgResultFromReceipt(&receipt.Receipt, revertReason) } From 99492cdacef7cee5d5273bafe195f3a11f16d53e Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Fri, 26 Apr 2024 15:11:14 +0900 Subject: [PATCH 13/13] add tx_hash attribute in logging in GetMsgResult Signed-off-by: Masanori Yoshida --- pkg/relay/ethereum/tx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/relay/ethereum/tx.go b/pkg/relay/ethereum/tx.go index 1c2be34..759ab27 100644 --- a/pkg/relay/ethereum/tx.go +++ b/pkg/relay/ethereum/tx.go @@ -116,7 +116,7 @@ func (c *Chain) GetMsgResult(id core.MsgID) (core.MsgResult, error) { } revertReason, rawErrorData, err := c.getRevertReasonFromReceipt(ctx, receipt) if err != nil { - logger.Error("failed to get revert reason", err, "raw_error_data", hex.EncodeToString(rawErrorData)) + logger.Error("failed to get revert reason", err, "raw_error_data", hex.EncodeToString(rawErrorData), "tx_hash", msgID.TxHashHex) } return c.makeMsgResultFromReceipt(&receipt.Receipt, revertReason) }