diff --git a/pkg/client/client.go b/pkg/client/client.go index a5889cb..8b44a98 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -2,7 +2,7 @@ package client import ( "context" - "fmt" + "encoding/json" "math/big" "time" @@ -14,7 +14,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 +61,41 @@ 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 nil, err } - return receipt, revertReason, nil + return receipt, 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 +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 { @@ -137,35 +107,14 @@ 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") - } - - size := &big.Int{} - size.SetBytes(bz[36:68]) - return string(bz[68 : 68+size.Int64()]), nil -} - -type callLog struct { +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"` @@ -175,24 +124,70 @@ 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"` } -func searchRevertReason(result *callFrame) (string, error) { - if result.RevertReason != "" { - return result.RevertReason, nil +// 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 } - for _, call := range result.Calls { - reason, err := searchRevertReason(&call) - if err == nil { - return reason, nil - } + if dec.Value != nil { + c.Value = (*big.Int)(dec.Value) } - return "", fmt.Errorf("revert reason not found") + return nil } func (cl *ETHClient) EstimateGasFromTx(ctx context.Context, tx *gethtypes.Transaction) (uint64, error) { 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/chain.go b/pkg/relay/ethereum/chain.go index 7310478..5af83a1 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,11 @@ 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 +93,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/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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, 0x59, 0x72, 0xf0, 0x57, - 0xa8, 0xc5, 0xd3, 0x60, 0x04, 0xc6, 0x44, 0x9f, 0x79, 0x2f, 0xc4, 0x53, 0xef, 0x05, 0xc6, 0xe0, - 0xe7, 0xe8, 0x84, 0x4b, 0x6e, 0x39, 0x15, 0xc4, 0x80, 0xcc, 0x08, 0x9b, 0x01, 0xbb, 0x2a, 0x15, - 0x97, 0x36, 0xaa, 0xfb, 0xb2, 0x8e, 0x96, 0xf0, 0x2f, 0x20, 0xb3, 0xd1, 0x1a, 0xdc, 0xd6, 0x69, - 0x60, 0xd7, 0xdb, 0xba, 0xfd, 0x3b, 0xba, 0x04, 0xd8, 0xf5, 0x96, 0xee, 0x09, 0xc2, 0x20, 0x69, - 0x2a, 0x80, 0x64, 0x90, 0xce, 0x73, 0x62, 0x35, 0x65, 0x10, 0x35, 0xba, 0xb5, 0x7e, 0x23, 0x39, - 0x08, 0xc8, 0x4b, 0x07, 0x4c, 0x5c, 0x1c, 0x7f, 0x8b, 0x4e, 0xe8, 0x35, 0x68, 0x9a, 0x03, 0x49, - 0x85, 0x62, 0x57, 0xc4, 0xf2, 0x02, 0x48, 0x61, 0x80, 0x45, 0x4d, 0x9f, 0xe5, 0x70, 0x09, 0xbf, - 0x70, 0xe8, 0x84, 0x17, 0xf0, 0x93, 0x01, 0xe6, 0x64, 0x05, 0x5d, 0x10, 0x0d, 0x56, 0x57, 0x64, - 0xaa, 0x34, 0xe1, 0x92, 0x89, 0xb9, 0xe1, 0x4a, 0x46, 0x28, 0xc8, 0x0a, 0xba, 0x48, 0x1c, 0x3a, - 0x56, 0xfa, 0xcd, 0x0a, 0xc3, 0x19, 0xc2, 0x54, 0x08, 0xf5, 0x96, 0x08, 0x46, 0xa6, 0x73, 0xc9, - 0x2c, 0x57, 0xd2, 0x44, 0x2d, 0xbf, 0xcd, 0xcf, 0xe3, 0xff, 0x1f, 0x98, 0x78, 0xe8, 0x94, 0x17, - 0xa3, 0xf1, 0x4a, 0x17, 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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..1de0208 --- /dev/null +++ b/pkg/relay/ethereum/error_parse.go @@ -0,0 +1,141 @@ +package ethereum + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/ethereum/go-ethereum/accounts/abi" +) + +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 + + for _, dir := range abiPaths { + if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { + if err != nil { + 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 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 fmt.Errorf("failed to parse a ABI JSON file: err=%v, path=%s", err, path) + } + + for _, errABI := range contractABI.Errors { + errABIs = append(errABIs, errABI) + } + } + return nil + }); err != nil { + return nil, fmt.Errorf("failed to read ABIs from a directory: err=%v, dirpath=%s", err, dir) + } + } + + return NewErrorRepository(errABIs) +} + +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 { + return nil, err + } + } + + return repo, nil +} + +func (r ErrorRepository) Add(errABI abi.Error) error { + var sel [4]byte + copy(sel[:], errABI.ID[:4]) + if existingErrABI, ok := r[sel]; ok { + if existingErrABI.Sig == errABI.Sig { + return nil + } + return fmt.Errorf("error selector collision: selector=%x, newErrABI=%v, existingErrABI=%v", sel, errABI, existingErrABI) + } + r[sel] = errABI + return nil +} + +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[:], errorData[:4]) + errABI, ok := r[sel] + if !ok { + return abi.Error{}, fmt.Errorf("error ABI not found: errorData=%x", errorData) + } + return errABI, nil +} + +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 errVals { + m[errABI.Inputs[i].Name] = v + } + + bz, err := json.Marshal(m) + if err != nil { + return "", fmt.Errorf("failed to marshal error value: %v", err) + } + + return string(bz), nil +} + +func (r ErrorRepository) ParseError(errorData []byte) (string, 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.Errorf("failed to unpack error: %v", err) + } + errStr, err := errorToJSON(errVal, errABI) + if err != nil { + return "", fmt.Errorf("failed to marshal error inputs into JSON: %v", err) + } + 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 new file mode 100644 index 0000000..9070daf --- /dev/null +++ b/pkg/relay/ethereum/error_parse_test.go @@ -0,0 +1,65 @@ +package ethereum + +import ( + "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 := NewErrorRepository(customErrors) + require.NoError(t, err) + + revertReason, err := erepo.ParseError( + common.FromHex("0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a4e6f7420656e6f7567682045746865722070726f76696465642e000000000000"), + ) + require.NoError(t, err) + require.Equal(t, `Error{"desc":"Not enough Ether provided."}`, revertReason) +} + +func TestRevertReasonParserAddedCustomError(t *testing.T) { + uintT, err := abi.NewType("uint256", "", nil) + if err != nil { + panic(err) + } + customErrors := []abi.Error{ + abi.NewError("AppError", abi.Arguments{{Name: "x", Type: uintT}}), + } + + erepo, err := NewErrorRepository(customErrors) + require.NoError(t, err) + + revertReason, err := erepo.ParseError( + common.FromHex("0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a4e6f7420656e6f7567682045746865722070726f76696465642e000000000000"), + ) + require.NoError(t, err) + require.Equal(t, `Error{"desc":"Not enough Ether provided."}`, revertReason) +} + +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{ + abi.NewError("AppError", abi.Arguments{{Name: "x", Type: strT}}), + abi.NewError("InsufficientBalance", abi.Arguments{{Name: "y", Type: uintT}, {Name: "z", Type: uintT}}), + } + + erepo, err := NewErrorRepository(customErrors) + require.NoError(t, err) + + revertReason, err := erepo.ParseError( + common.FromHex("0xcf47918100000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000009"), + ) + require.NoError(t, err) + require.Equal(t, `InsufficientBalance{"y":7,"z":9}`, revertReason) +} diff --git a/pkg/relay/ethereum/tx.go b/pkg/relay/ethereum/tx.go index fbc9f7e..759ab27 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" @@ -14,10 +15,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" ) @@ -44,37 +48,49 @@ 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, "msg_index", i) return nil, err } estimatedGas, err := c.client.EstimateGasFromTx(ctx, tx) if err != nil { - logger.Error("failed to estimate gas", err, "msg", msg) + 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, "raw_error_data", hex.EncodeToString(rawErrorData), "msg_index", i) return nil, err } 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, "msg", msg) + logger.Error("failed to send msg / NoSend: false", err, "msg_index", i) 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, "msg_index", i, "tx_hash", tx.Hash()) 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 { + revertReason, rawErrorData, err2 := c.getRevertReasonFromReceipt(ctx, receipt) + if err2 != nil { + logger.Error("failed to get revert reason", err2, "msg_index", i, "tx_hash", tx.Hash()) + } + + 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", msg) + logger.Error("failed to OnSendMsg call", err, "msg_index", i, "tx_hash", tx.Hash()) } } msgIDs = append(msgIDs, NewMsgID(tx.Hash())) @@ -83,17 +99,26 @@ 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, "") + } + revertReason, rawErrorData, err := c.getRevertReasonFromReceipt(ctx, receipt) + if err != nil { + logger.Error("failed to get revert reason", err, "raw_error_data", hex.EncodeToString(rawErrorData), "tx_hash", msgID.TxHashHex) + } + return c.makeMsgResultFromReceipt(&receipt.Receipt, revertReason) } func (c *Chain) TxCreateClient(opts *bind.TransactOpts, msg *clienttypes.MsgCreateClient) (*gethtypes.Transaction, error) { @@ -338,3 +363,43 @@ 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, []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 "", nil, err + } else if len(callFrame.Output) == 0 { + return "", nil, fmt.Errorf("execution reverted without error data") + } + errorData = callFrame.Output + } else { + return "", nil, fmt.Errorf("no way to get revert reason") + } + + revertReason, err := c.errorRepository.ParseError(errorData) + if err != nil { + return "", errorData, fmt.Errorf("failed to parse error: %v", err) + } + return revertReason, errorData, nil +} + +func (c *Chain) getRevertReasonFromEstimateGas(err error) (string, []byte, error) { + if de, ok := err.(rpc.DataError); !ok { + return "", nil, fmt.Errorf("eth_estimateGas failed with unexpected error type: errorType=%T", err) + } else if de.ErrorData() == nil { + return "", nil, fmt.Errorf("eth_estimateGas failed without error data") + } else if errorData, ok := de.ErrorData().(string); !ok { + 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 "", errorData, fmt.Errorf("failed to parse error: %v", err) + } + return revertReason, errorData, nil + } +} 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; } -