/
utils.go
227 lines (200 loc) · 5.3 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package eth
import (
"encoding/json"
"fmt"
"math/big"
"strings"
sdkerror "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
"github.com/dogschain/dogschain/x/evm/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
ethermint "github.com/dogschain/dogschain/app/types"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/spf13/viper"
)
const (
DefaultEVMErrorCode = -32000
VMExecuteException = -32015
VMExecuteExceptionInEstimate = 3
RPCEthCall = "eth_call"
RPCEthEstimateGas = "eth_estimateGas"
RPCEthGetBlockByHash = "eth_getBlockByHash"
RPCUnknowErr = "unknow"
RPCNullData = "null"
)
//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 {
return (*hexutil.Big)(gasPrices[0].Amount.BigInt())
}
//return the default gas price : DefaultGasPrice
return (*hexutil.Big)(sdk.NewDecFromBigIntWithPrec(big.NewInt(ethermint.DefaultGasPrice), sdk.Precision/2).BigInt())
}
type cosmosError struct {
Code int `json:"code"`
Log string `json:"log"`
Codespace string `json:"codespace"`
}
func (c cosmosError) Error() string {
return c.Log
}
func newCosmosError(code int, log, codeSpace string) cosmosError {
return cosmosError{
Code: code,
Log: log,
Codespace: codeSpace,
}
}
func newWrappedCosmosError(code int, log, codeSpace string) cosmosError {
e := newCosmosError(code, log, codeSpace)
b, _ := json.Marshal(e)
e.Log = string(b)
return e
}
type wrappedEthError struct {
Wrap ethDataError `json:"0x00000000000000000000000000000000"`
}
type ethDataError struct {
Error string `json:"error"`
ProgramCounter int `json:"program_counter"`
Reason string `json:"reason"`
Ret string `json:"return"`
}
type DataError struct {
code int `json:"code"`
Msg string `json:"msg"`
data interface{} `json:"data,omitempty"`
}
func (d DataError) Error() string {
return d.Msg
}
func (d DataError) ErrorData() interface{} {
return d.data
}
func (d DataError) ErrorCode() int {
return d.code
}
func newDataError(revert string, data string) *wrappedEthError {
return &wrappedEthError{
Wrap: ethDataError{
Error: "revert",
ProgramCounter: 0,
Reason: revert,
Ret: data,
}}
}
func TransformDataError(err error, method string) error {
msg := err.Error()
var realErr cosmosError
if len(msg) > 0 {
e := json.Unmarshal([]byte(msg), &realErr)
if e != nil {
return DataError{
code: DefaultEVMErrorCode,
Msg: err.Error(),
data: RPCNullData,
}
}
if method == RPCEthGetBlockByHash {
return DataError{
code: DefaultEVMErrorCode,
Msg: realErr.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),
}
}
}
return DataError{
code: DefaultEVMErrorCode,
Msg: err.Error(),
data: RPCNullData,
}
}
//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, "]")
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():
return common.Hash{}, sdkerror.ErrTxInMempoolCache
case sdkerror.ErrMempoolIsFull.ABCICode():
return common.Hash{}, sdkerror.ErrMempoolIsFull
case sdkerror.ErrTxTooLarge.ABCICode():
return common.Hash{}, sdkerror.Wrapf(sdkerror.ErrTxTooLarge, txRes.RawLog)
}
return common.Hash{}, fmt.Errorf(txRes.RawLog)
}