-
Notifications
You must be signed in to change notification settings - Fork 145
/
vmerror.go
394 lines (332 loc) · 8.57 KB
/
vmerror.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package isc
import (
"errors"
"fmt"
"io"
"math"
"reflect"
"github.com/iotaledger/wasp/packages/hashing"
"github.com/iotaledger/wasp/packages/util/rwutil"
)
const VMErrorMessageLimit = math.MaxUint16
// VMCoreErrorContractID defines that all errors with a MaxUint32 contract id will be considered as core errors.
const VMCoreErrorContractID = math.MaxUint32
type VMErrorCode struct {
ContractID Hname
ID uint16
}
func NewVMErrorCode(contractID Hname, id uint16) VMErrorCode {
return VMErrorCode{ContractID: contractID, ID: id}
}
func NewCoreVMErrorCode(id uint16) VMErrorCode {
return VMErrorCode{ContractID: VMCoreErrorContractID, ID: id}
}
func VMErrorCodeFromBytes(b []byte) (ret VMErrorCode, err error) {
_, err = rwutil.ReadFromBytes(b, &ret)
return ret, err
}
func (c VMErrorCode) Bytes() []byte {
return rwutil.WriteToBytes(&c)
}
func (c VMErrorCode) String() string {
if c.ContractID == VMCoreErrorContractID {
return fmt.Sprintf("%04x", c.ID)
}
return fmt.Sprintf("%s:%04x", c.ContractID, c.ID)
}
func (c *VMErrorCode) Read(r io.Reader) error {
rr := rwutil.NewReader(r)
rr.Read(&c.ContractID)
c.ID = rr.ReadUint16()
return rr.Err
}
func (c *VMErrorCode) Write(w io.Writer) error {
ww := rwutil.NewWriter(w)
ww.Write(&c.ContractID)
ww.WriteUint16(c.ID)
return ww.Err
}
// VMErrorBase is the common interface of UnresolvedVMError and VMError
type VMErrorBase interface {
error
Code() VMErrorCode
}
type VMErrorTemplate struct {
code VMErrorCode
messageFormat string
}
var _ VMErrorBase = &VMErrorTemplate{}
func NewVMErrorTemplate(code VMErrorCode, messageFormat string) *VMErrorTemplate {
return &VMErrorTemplate{code: code, messageFormat: messageFormat}
}
func VMErrorTemplateFromBytes(data []byte) (*VMErrorTemplate, error) {
return rwutil.ReadFromBytes(data, new(VMErrorTemplate))
}
func (e *VMErrorTemplate) Bytes() []byte {
return rwutil.WriteToBytes(e)
}
func (e *VMErrorTemplate) Code() VMErrorCode {
return e.code
}
func (e *VMErrorTemplate) Create(params ...any) *VMError {
validateParams(params)
return &VMError{
template: e,
params: params,
}
}
// VMErrorTemplate implements error just in case someone panics with
// VMErrorTemplate by mistake, so that we don't crash the VM because of that.
func (e *VMErrorTemplate) Error() string {
// calling Sprintf so that it marks missing parameters as errors
return fmt.Sprintf(e.messageFormat)
}
func (e *VMErrorTemplate) MessageFormat() string {
return e.messageFormat
}
func (e *VMErrorTemplate) Read(r io.Reader) error {
rr := rwutil.NewReader(r)
rr.Read(&e.code)
e.messageFormat = rr.ReadString()
return rr.Err
}
func (e *VMErrorTemplate) Write(w io.Writer) error {
ww := rwutil.NewWriter(w)
ww.Write(&e.code)
ww.WriteString(e.messageFormat)
return ww.Err
}
type UnresolvedVMError struct {
ErrorCode VMErrorCode `json:"code"`
Params []any `json:"params"`
}
var _ VMErrorBase = &UnresolvedVMError{}
type UnresolvedVMErrorJSON struct {
ErrorCode string `json:"code"`
Params []string `json:"params"`
}
func (e *UnresolvedVMError) AsGoError() error {
// this is necessary because *UnresolvedVMError(nil) != error(nil)
if e == nil {
return nil
}
return e
}
func (e *UnresolvedVMError) Bytes() []byte {
return rwutil.WriteToBytes(e)
}
func (e *UnresolvedVMError) Code() VMErrorCode {
return e.ErrorCode
}
func (e *UnresolvedVMError) Error() string {
return fmt.Sprintf("UnresolvedVMError(code: %s)", e.ErrorCode)
}
// produce the params as humanly readably json, and the uints as strings
func (e *UnresolvedVMError) ToJSONStruct() *UnresolvedVMErrorJSON {
if e == nil {
return &UnresolvedVMErrorJSON{
Params: []string{},
ErrorCode: "",
}
}
return &UnresolvedVMErrorJSON{
Params: humanlyReadableParams(e.Params),
ErrorCode: e.ErrorCode.String(),
}
}
func (e *UnresolvedVMError) Read(r io.Reader) error {
rr := rwutil.NewReader(r)
rr.Read(&e.ErrorCode)
e.Params = readParams(rr)
return rr.Err
}
func (e *UnresolvedVMError) Write(w io.Writer) error {
ww := rwutil.NewWriter(w)
ww.Write(&e.ErrorCode)
writeParams(ww, e.Params)
return ww.Err
}
func humanlyReadableParams(params []any) []string {
res := make([]string, len(params))
for i, param := range params {
res[i] = fmt.Sprintf("%v:%s", param, reflect.TypeOf(param).String())
}
return res
}
type VMError struct {
template *VMErrorTemplate
params []any
}
var _ VMErrorBase = &VMError{}
func (e *VMError) AsGoError() error {
// this is necessary because *UnresolvedVMError(nil) != error(nil)
if e == nil {
return nil
}
return e
}
func (e *VMError) AsTemplate() *VMErrorTemplate {
return e.template
}
func (e *VMError) AsUnresolvedError() *UnresolvedVMError {
return &UnresolvedVMError{
ErrorCode: e.template.code,
Params: e.params,
}
}
func (e *VMError) Bytes() []byte {
return rwutil.WriteToBytes(e)
}
func (e *VMError) Code() VMErrorCode {
return e.template.code
}
func (e *VMError) Error() string {
if e == nil {
return ""
}
return fmt.Sprintf(e.MessageFormat(), e.params...)
}
func (e *VMError) MessageFormat() string {
return e.template.messageFormat
}
func (e *VMError) Params() []any {
return e.params
}
func (e *VMError) Read(r io.Reader) error {
rr := rwutil.NewReader(r)
rr.Read(&e.template.code)
e.params = readParams(rr)
return rr.Err
}
func (e *VMError) Write(w io.Writer) error {
ww := rwutil.NewWriter(w)
ww.Write(&e.template.code)
writeParams(ww, e.params)
return ww.Err
}
func GetErrorIDFromMessageFormat(messageFormat string) uint16 {
messageFormatHash := hashing.HashStrings(messageFormat).Bytes()
rr := rwutil.NewBytesReader(messageFormatHash)
errorID := rr.ReadUint16()
if rr.Err != nil {
panic(rr.Err)
}
return errorID
}
// VMErrorIs returns true if the error includes a VMErrorCode in its chain that matches the given code
func VMErrorIs(err error, expected VMErrorBase) bool {
var vmError VMErrorBase
if errors.As(err, &vmError) {
return vmError.Code() == expected.Code()
}
return false
}
// VMErrorMustBe panics unless the error includes a VMErrorCode in its chain that matches the given code
func VMErrorMustBe(err error, expected VMErrorBase) {
if !VMErrorIs(err, expected) {
panic(fmt.Sprintf("%v does not match %v", err, expected))
}
}
func validateParams(params []any) {
if len(params) > 255 {
panic("params too long")
}
for _, param := range params {
t := reflect.TypeOf(param)
v := reflect.ValueOf(param)
switch t.Kind() {
case reflect.String:
s := v.String()
if len(s) > 1024 {
panic("string param too long")
}
case reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64:
default:
panic(fmt.Sprintf("no handler for param of type %s", t.Name()))
}
}
}
//nolint:gocyclo
func readParams(rr *rwutil.Reader) []any {
size := rr.ReadSize16()
if rr.Err != nil || size == 0 {
return nil
}
ret := make([]any, size)
for i := 0; i < size; i++ {
kind := rr.ReadUint8()
switch reflect.Kind(kind) {
case reflect.String:
ret[i] = rr.ReadString()
case reflect.Uint:
ret[i] = uint(rr.ReadUint32())
case reflect.Uint8:
ret[i] = rr.ReadUint8()
case reflect.Uint16:
ret[i] = rr.ReadUint16()
case reflect.Uint32:
ret[i] = rr.ReadUint32()
case reflect.Uint64:
ret[i] = rr.ReadUint64()
case reflect.Int:
ret[i] = int(rr.ReadInt32())
case reflect.Int8:
ret[i] = rr.ReadInt8()
case reflect.Int16:
ret[i] = rr.ReadInt16()
case reflect.Int32:
ret[i] = rr.ReadInt32()
case reflect.Int64:
ret[i] = rr.ReadInt64()
default:
panic(fmt.Sprintf("no handler for param of kind %s", reflect.Kind(kind)))
}
}
if rr.Err != nil || size == 0 {
return nil
}
return ret
}
func writeParams(ww *rwutil.Writer, params []any) {
ww.WriteSize16(len(params))
for _, param := range params {
t := reflect.TypeOf(param)
v := reflect.ValueOf(param)
ww.WriteUint8(uint8(t.Kind()))
switch t.Kind() {
case reflect.String:
ww.WriteString(v.String())
case reflect.Uint:
ww.WriteUint32(uint32(v.Uint()))
case reflect.Uint8:
ww.WriteUint8(uint8(v.Uint()))
case reflect.Uint16:
ww.WriteUint16(uint16(v.Uint()))
case reflect.Uint32:
ww.WriteUint32(uint32(v.Uint()))
case reflect.Uint64:
ww.WriteUint64(v.Uint())
case reflect.Int:
ww.WriteInt32(int32(v.Int()))
case reflect.Int8:
ww.WriteInt8(int8(v.Int()))
case reflect.Int16:
ww.WriteInt16(int16(v.Int()))
case reflect.Int32:
ww.WriteInt32(int32(v.Int()))
case reflect.Int64:
ww.WriteInt64(v.Int())
default:
panic(fmt.Sprintf("no handler for param of type %s", t.Name()))
}
}
}