From 1be27900f455bd467c6296248bfba706179743cc Mon Sep 17 00:00:00 2001 From: bylingo <790653865@qq.com> Date: Mon, 24 Jul 2023 16:52:27 +0800 Subject: [PATCH 1/2] modify TransformDataError & preProcessError --- app/rpc/namespaces/eth/utils.go | 152 +++++++++++++++------------ app/rpc/namespaces/eth/utils_test.go | 13 +-- 2 files changed, 92 insertions(+), 73 deletions(-) diff --git a/app/rpc/namespaces/eth/utils.go b/app/rpc/namespaces/eth/utils.go index 649988e4ce..74ebd2932c 100644 --- a/app/rpc/namespaces/eth/utils.go +++ b/app/rpc/namespaces/eth/utils.go @@ -9,7 +9,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/ethereum/go-ethereum/core/vm" ethcrypto "github.com/ethereum/go-ethereum/crypto" "github.com/spf13/viper" @@ -20,7 +19,6 @@ import ( sdkerror "github.com/okex/exchain/libs/cosmos-sdk/types/errors" authexported "github.com/okex/exchain/libs/cosmos-sdk/x/auth/exported" "github.com/okex/exchain/libs/cosmos-sdk/x/supply" - "github.com/okex/exchain/x/evm/types" "github.com/okex/exchain/x/token" wasmkeeper "github.com/okex/exchain/x/wasm/keeper" wasmtypes "github.com/okex/exchain/x/wasm/types" @@ -40,7 +38,7 @@ const ( RPCNullData = "null" ) -//gasPrice: to get "minimum-gas-prices" config or to get ethermint.DefaultGasPrice +// gasPrice: to get "minimum-gas-prices" config or to get ethermint.DefaultGasPrice func ParseGasPrice() *hexutil.Big { gasPrices, err := sdk.ParseDecCoins(viper.GetString(server.FlagMinGasPrices)) if err == nil && gasPrices != nil && len(gasPrices) > 0 { @@ -144,85 +142,105 @@ func TransformDataError(err error, method string) error { data: RPCNullData, } } - m, retErr := preProcessError(realErr, err.Error()) - if retErr != nil { - return realErr - } - //if there have multi error type of EVM, this need a reactor mode to process error - revert, f := m[vm.ErrExecutionReverted.Error()] - if !f { - revert = RPCUnknowErr - } - data, f := m[types.ErrorHexData] - if !f { - data = RPCNullData - } - switch method { - case RPCEthEstimateGas: - return DataError{ - code: VMExecuteExceptionInEstimate, - Msg: revert, - data: data, - } - case RPCEthCall: - return DataError{ - code: VMExecuteException, - Msg: revert, - data: newDataError(revert, data), - } - default: - return DataError{ - code: DefaultEVMErrorCode, - Msg: revert, - data: newDataError(revert, data), - } - } + //m, retErr := preProcessError(realErr, err.Error()) + return preProcessError(realErr, err.Error()) + //if retErr != nil { + // return realErr + //} + ////if there have multi error type of EVM, this need a reactor mode to process error + //revert, f := m[vm.ErrExecutionReverted.Error()] + //if !f { + // revert = RPCUnknowErr + //} + //data, f := m[types.ErrorHexData] + //if !f { + // data = RPCNullData + //} + //switch method { + //case RPCEthEstimateGas: + // return DataError{ + // code: VMExecuteExceptionInEstimate, + // Msg: revert, + // data: data, + // } + //case RPCEthCall: + // return DataError{ + // code: VMExecuteException, + // Msg: revert, + // data: newDataError(revert, data), + // } + //default: + // return DataError{ + // code: DefaultEVMErrorCode, + // Msg: revert, + // data: newDataError(revert, data), + // } + //} } //Preprocess error string, the string of realErr.Log is most like: //`["execution reverted","message","HexData","0x00000000000"];some failed information` //we need marshalled json slice from realErr.Log and using segment tag `[` and `]` to cut it -func preProcessError(realErr *cosmosError, origErrorMsg string) (map[string]string, error) { - var logs []string - lastSeg := strings.LastIndexAny(realErr.Log, "]") + +// realErrs are all cosmosError, which is formatted from wrappedError. Msgs are concatenated with ':' between each of them +// Main cause always appears in the first place, thus this function only get the first part of the error out of realErr. +func preProcessError(realErr *cosmosError, origErrorMsg string) error { + lastSeg := strings.IndexAny(realErr.Log, ":") if lastSeg < 0 { - return nil, DataError{ - code: DefaultEVMErrorCode, - Msg: origErrorMsg, - data: RPCNullData, - } - } - marshaler := realErr.Log[0 : lastSeg+1] - e := json.Unmarshal([]byte(marshaler), &logs) - if e != nil { - return nil, DataError{ - code: DefaultEVMErrorCode, - Msg: origErrorMsg, - data: RPCNullData, - } - } - m := genericStringMap(logs) - if m == nil { - return nil, DataError{ + return DataError{ code: DefaultEVMErrorCode, Msg: origErrorMsg, data: RPCNullData, } } - return m, nil -} -func genericStringMap(s []string) map[string]string { - var ret = make(map[string]string) - if len(s)%2 != 0 { - return nil - } - for i := 0; i < len(s); i += 2 { - ret[s[i]] = s[i+1] + errorSeg := realErr.Log[0:lastSeg] + + return DataError{ + code: DefaultEVMErrorCode, + Msg: errorSeg, + data: RPCNullData, } - return ret + //var logs []string + //lastSeg := strings.LastIndexAny(realErr.Log, "]") + //if lastSeg < 0 { + // return nil, DataError{ + // code: DefaultEVMErrorCode, + // Msg: origErrorMsg, + // data: RPCNullData, + // } + //} + //marshaler := realErr.Log[0 : lastSeg+1] + //e := json.Unmarshal([]byte(marshaler), &logs) + //if e != nil { + // return nil, DataError{ + // code: DefaultEVMErrorCode, + // Msg: origErrorMsg, + // data: RPCNullData, + // } + //} + //m := genericStringMap(logs) + //if m == nil { + // return nil, DataError{ + // code: DefaultEVMErrorCode, + // Msg: origErrorMsg, + // data: RPCNullData, + // } + //} + //return m, nil } +//func genericStringMap(s []string) map[string]string { +// var ret = make(map[string]string) +// if len(s)%2 != 0 { +// return nil +// } +// for i := 0; i < len(s); i += 2 { +// ret[s[i]] = s[i+1] +// } +// return ret +//} + func CheckError(txRes sdk.TxResponse) (common.Hash, error) { switch txRes.Code { case sdkerror.ErrTxInMempoolCache.ABCICode(): diff --git a/app/rpc/namespaces/eth/utils_test.go b/app/rpc/namespaces/eth/utils_test.go index 6b30b1f331..a0063be805 100644 --- a/app/rpc/namespaces/eth/utils_test.go +++ b/app/rpc/namespaces/eth/utils_test.go @@ -10,14 +10,15 @@ import ( func Test_TransformDataError(t *testing.T) { - sdkerr := newWrappedCosmosError(7, `["execution reverted","message","HexData","0x00000000000"];failed message tail`, evmtypes.ModuleName) + sdkerr := newWrappedCosmosError(7, `["execution reverted","message","HexData","0x00000000000"]:failed message tail`, evmtypes.ModuleName) err := TransformDataError(sdkerr, "eth_estimateGas").(DataError) require.NotNil(t, err.ErrorData()) - require.Equal(t, err.ErrorData(), "0x00000000000") - require.Equal(t, err.ErrorCode(), VMExecuteExceptionInEstimate) + require.Equal(t, err.Error(), `["execution reverted","message","HexData","0x00000000000"]`) + require.Equal(t, err.ErrorData(), RPCNullData) + require.Equal(t, err.ErrorCode(), DefaultEVMErrorCode) err = TransformDataError(sdkerr, "eth_call").(DataError) require.NotNil(t, err.ErrorData()) - data, ok := err.ErrorData().(*wrappedEthError) - require.True(t, ok) - require.NotNil(t, data) + require.Equal(t, err.Error(), `["execution reverted","message","HexData","0x00000000000"]`) + require.Equal(t, err.ErrorData(), RPCNullData) + require.Equal(t, err.ErrorCode(), DefaultEVMErrorCode) } From df26defa86d8f5e64698be94817c9d8767bac2ca Mon Sep 17 00:00:00 2001 From: bylingo <790653865@qq.com> Date: Mon, 24 Jul 2023 17:32:37 +0800 Subject: [PATCH 2/2] remove unused codes --- app/rpc/namespaces/eth/utils.go | 77 +-------------------------------- 1 file changed, 1 insertion(+), 76 deletions(-) diff --git a/app/rpc/namespaces/eth/utils.go b/app/rpc/namespaces/eth/utils.go index 74ebd2932c..b5cd2d0a15 100644 --- a/app/rpc/namespaces/eth/utils.go +++ b/app/rpc/namespaces/eth/utils.go @@ -142,47 +142,10 @@ func TransformDataError(err error, method string) error { data: RPCNullData, } } - //m, retErr := preProcessError(realErr, err.Error()) return preProcessError(realErr, err.Error()) - //if retErr != nil { - // return realErr - //} - ////if there have multi error type of EVM, this need a reactor mode to process error - //revert, f := m[vm.ErrExecutionReverted.Error()] - //if !f { - // revert = RPCUnknowErr - //} - //data, f := m[types.ErrorHexData] - //if !f { - // data = RPCNullData - //} - //switch method { - //case RPCEthEstimateGas: - // return DataError{ - // code: VMExecuteExceptionInEstimate, - // Msg: revert, - // data: data, - // } - //case RPCEthCall: - // return DataError{ - // code: VMExecuteException, - // Msg: revert, - // data: newDataError(revert, data), - // } - //default: - // return DataError{ - // code: DefaultEVMErrorCode, - // Msg: revert, - // data: newDataError(revert, data), - // } - //} } -//Preprocess error string, the string of realErr.Log is most like: -//`["execution reverted","message","HexData","0x00000000000"];some failed information` -//we need marshalled json slice from realErr.Log and using segment tag `[` and `]` to cut it - -// realErrs are all cosmosError, which is formatted from wrappedError. Msgs are concatenated with ':' between each of them +// realErr is a cosmosError, which is formatted from wrappedError. Msgs are concatenated with ':' between each of them // Main cause always appears in the first place, thus this function only get the first part of the error out of realErr. func preProcessError(realErr *cosmosError, origErrorMsg string) error { lastSeg := strings.IndexAny(realErr.Log, ":") @@ -201,46 +164,8 @@ func preProcessError(realErr *cosmosError, origErrorMsg string) error { Msg: errorSeg, data: RPCNullData, } - //var logs []string - //lastSeg := strings.LastIndexAny(realErr.Log, "]") - //if lastSeg < 0 { - // return nil, DataError{ - // code: DefaultEVMErrorCode, - // Msg: origErrorMsg, - // data: RPCNullData, - // } - //} - //marshaler := realErr.Log[0 : lastSeg+1] - //e := json.Unmarshal([]byte(marshaler), &logs) - //if e != nil { - // return nil, DataError{ - // code: DefaultEVMErrorCode, - // Msg: origErrorMsg, - // data: RPCNullData, - // } - //} - //m := genericStringMap(logs) - //if m == nil { - // return nil, DataError{ - // code: DefaultEVMErrorCode, - // Msg: origErrorMsg, - // data: RPCNullData, - // } - //} - //return m, nil } -//func genericStringMap(s []string) map[string]string { -// var ret = make(map[string]string) -// if len(s)%2 != 0 { -// return nil -// } -// for i := 0; i < len(s); i += 2 { -// ret[s[i]] = s[i+1] -// } -// return ret -//} - func CheckError(txRes sdk.TxResponse) (common.Hash, error) { switch txRes.Code { case sdkerror.ErrTxInMempoolCache.ABCICode():