-
Notifications
You must be signed in to change notification settings - Fork 147
/
vmerror.go
309 lines (251 loc) · 6.92 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
package isc
import (
"encoding/json"
"errors"
"fmt"
"hash/crc32"
"math"
"github.com/iotaledger/hive.go/serializer/v2/marshalutil"
"github.com/iotaledger/wasp/packages/hashing"
)
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 (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) Bytes() []byte {
mu := marshalutil.New()
c.Serialize(mu)
return mu.Bytes()
}
func (c VMErrorCode) Serialize(mu *marshalutil.MarshalUtil) {
mu.Write(c.ContractID).
WriteUint16(c.ID)
}
func VMErrorCodeFromBytes(b []byte) (code VMErrorCode, err error) {
return VMErrorCodeFromMarshalUtil(marshalutil.New(b))
}
func VMErrorCodeFromMarshalUtil(mu *marshalutil.MarshalUtil) (code VMErrorCode, err error) {
if code.ContractID, err = HnameFromMarshalUtil(mu); err != nil {
return
}
if code.ID, err = mu.ReadUint16(); err != nil {
return
}
return
}
// 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}
}
// 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) Code() VMErrorCode {
return e.code
}
func (e *VMErrorTemplate) MessageFormat() string {
return e.messageFormat
}
func (e *VMErrorTemplate) Create(params ...interface{}) *VMError {
return &VMError{
template: e,
params: params,
}
}
func (e *VMErrorTemplate) Serialize(mu *marshalutil.MarshalUtil) {
e.code.Serialize(mu)
messageFormatBytes := []byte(e.MessageFormat())
mu.WriteUint16(uint16(len(messageFormatBytes))).
WriteBytes(messageFormatBytes)
}
func (e *VMErrorTemplate) Bytes() []byte {
mu := marshalutil.New()
e.Serialize(mu)
return mu.Bytes()
}
func VMErrorTemplateFromMarshalUtil(mu *marshalutil.MarshalUtil) (*VMErrorTemplate, error) {
var err error
e := &VMErrorTemplate{}
if e.code, err = VMErrorCodeFromMarshalUtil(mu); err != nil {
return nil, err
}
var messageLength uint16
if messageLength, err = mu.ReadUint16(); err != nil {
return nil, err
}
messageInBytes, err := mu.ReadBytes(int(messageLength))
if err != nil {
return nil, err
}
e.messageFormat = string(messageInBytes)
return e, nil
}
type UnresolvedVMError struct {
ErrorCode VMErrorCode `json:"code"`
Params []interface{} `json:"params"`
Hash uint32 `json:"hash"`
}
var _ VMErrorBase = &UnresolvedVMError{}
func (e *UnresolvedVMError) Error() string {
return fmt.Sprintf("UnresolvedVMError(code: %s, hash: %x)", e.ErrorCode, e.Hash)
}
func (e *UnresolvedVMError) Code() VMErrorCode {
return e.ErrorCode
}
func (e *UnresolvedVMError) deserializeParams(mu *marshalutil.MarshalUtil) error {
var err error
var paramLength uint16
var params []byte
if paramLength, err = mu.ReadUint16(); err != nil {
return err
}
if params, err = mu.ReadBytes(int(paramLength)); err != nil {
return err
}
return json.Unmarshal(params, &e.Params)
}
func (e *UnresolvedVMError) serializeParams(mu *marshalutil.MarshalUtil) {
bytes, err := json.Marshal(e.Params)
if err != nil {
panic(err)
}
mu.WriteUint16(uint16(len(bytes)))
mu.WriteBytes(bytes)
}
func (e *UnresolvedVMError) Bytes() []byte {
mu := marshalutil.New()
e.ErrorCode.Serialize(mu)
mu.WriteUint32(e.Hash)
e.serializeParams(mu)
return mu.Bytes()
}
func (e *UnresolvedVMError) AsGoError() error {
// this is necessary because *UnresolvedVMError(nil) != error(nil)
if e == nil {
return nil
}
return e
}
type VMError struct {
template *VMErrorTemplate
params []interface{}
}
var _ VMErrorBase = &VMError{}
func (e *VMError) Code() VMErrorCode {
return e.template.code
}
func (e *VMError) MessageFormat() string {
return e.template.messageFormat
}
func (e *VMError) Params() []interface{} {
return e.params
}
func (e *VMError) Error() string {
if e == nil {
return ""
}
return fmt.Sprintf(e.MessageFormat(), e.params...)
}
func (e *VMError) Hash() uint32 {
if e.MessageFormat() == "" {
return 0
}
hash := crc32.Checksum([]byte(e.Error()), crc32.IEEETable)
return hash
}
func (e *VMError) serializeParams(mu *marshalutil.MarshalUtil) {
bytes, err := json.Marshal(e.params)
if err != nil {
panic(err)
}
mu.WriteUint16(uint16(len(bytes)))
mu.WriteBytes(bytes)
}
func (e *VMError) Bytes() []byte {
mu := marshalutil.New()
e.template.code.Serialize(mu)
mu.WriteUint32(e.Hash())
e.serializeParams(mu)
return mu.Bytes()
}
func (e *VMError) AsGoError() error {
// this is necessary because *UnresolvedVMError(nil) != error(nil)
if e == nil {
return nil
}
return e
}
func (e *VMError) AsUnresolvedError() *UnresolvedVMError {
return &UnresolvedVMError{
ErrorCode: e.template.code,
Params: e.params,
Hash: e.Hash(),
}
}
func (e *VMError) AsTemplate() *VMErrorTemplate {
return e.template
}
func UnresolvedVMErrorFromMarshalUtil(mu *marshalutil.MarshalUtil) (*UnresolvedVMError, error) {
var err error
unresolvedError := &UnresolvedVMError{}
if unresolvedError.ErrorCode, err = VMErrorCodeFromMarshalUtil(mu); err != nil {
return nil, err
}
if unresolvedError.Hash, err = mu.ReadUint32(); err != nil {
return nil, err
}
if err := unresolvedError.deserializeParams(mu); err != nil {
return nil, err
}
return unresolvedError, nil
}
func GetErrorIDFromMessageFormat(messageFormat string) uint16 {
messageFormatHash := hashing.HashStrings(messageFormat).Bytes()
mu := marshalutil.New(messageFormatHash)
errorID, err := mu.ReadUint16()
if err != nil {
panic(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))
}
}