forked from eoscanada/eos-go
-
Notifications
You must be signed in to change notification settings - Fork 4
/
abidecoder.go
482 lines (413 loc) · 12.3 KB
/
abidecoder.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package eos
import (
"encoding/hex"
"encoding/json"
"fmt"
"math"
"strconv"
"strings"
"time"
"go.uber.org/zap"
)
func (a *ABI) DecodeAction(data []byte, actionName ActionName) ([]byte, error) {
binaryDecoder := NewDecoder(data)
action := a.ActionForName(actionName)
if action == nil {
return nil, fmt.Errorf("action %s not found in abi", actionName)
}
builtStruct, err := a.decode(binaryDecoder, action.Type)
if err != nil {
return nil, err
}
return json.Marshal(builtStruct)
}
func (a *ABI) DecodeActionResult(data []byte, actionName ActionName) ([]byte, error) {
binaryDecoder := NewDecoder(data)
actionResult := a.ActionResultForName(actionName)
if actionResult == nil {
return nil, fmt.Errorf("action_result %s not found in abi", actionName)
}
res, err := a.resolveField(binaryDecoder, actionResult.ResultType)
if err != nil {
return nil, err
}
return json.Marshal(res)
}
func (a *ABI) DecodeTableRow(tableName TableName, data []byte) ([]byte, error) {
binaryDecoder := NewDecoder(data)
tbl := a.TableForName(tableName)
if tbl == nil {
return nil, fmt.Errorf("table name %s not found in abi", tableName)
}
builtStruct, err := a.decode(binaryDecoder, tbl.Type)
if err != nil {
return nil, err
}
return json.Marshal(builtStruct)
}
func (a *ABI) DecodeTableRowTyped(tableType string, data []byte) ([]byte, error) {
binaryDecoder := NewDecoder(data)
builtStruct, err := a.decode(binaryDecoder, tableType)
if err != nil {
return nil, err
}
return json.Marshal(builtStruct)
}
func (a *ABI) Decode(binaryDecoder *Decoder, structName string) ([]byte, error) {
builtStruct, err := a.decode(binaryDecoder, structName)
if err != nil {
return nil, err
}
return json.Marshal(builtStruct)
}
func (a *ABI) decode(binaryDecoder *Decoder, structName string) (map[string]interface{}, error) {
if tracer.Enabled() {
zlog.Debug("decode struct", zap.String("name", structName))
}
if variant := a.VariantForName(structName); variant != nil {
out, err := binaryDecoder.ReadUvarint32()
if err != nil {
zlog.Error("error reading variant", zap.Error(err))
}
structName = variant.Types[out]
}
structure := a.StructForName(structName)
if structure == nil {
return nil, fmt.Errorf("structure [%s] not found in abi", structName)
}
builtStruct := map[string]interface{}{}
if structure.Base != "" {
if tracer.Enabled() {
zlog.Debug("struct has base struct", zap.String("name", structName), zap.String("base", structure.Base))
}
baseName, isAlias := a.TypeNameForNewTypeName(structure.Base)
if isAlias && tracer.Enabled() {
zlog.Debug("base is an alias", zap.String("from", structure.Base), zap.String("to", baseName))
}
var err error
builtStruct, err = a.decode(binaryDecoder, baseName)
if err != nil {
return nil, fmt.Errorf("decode base [%s]: %w", structName, err)
}
}
return a.decodeFields(binaryDecoder, structure.Fields, builtStruct)
}
func (a *ABI) decodeFields(binaryDecoder *Decoder, fields []FieldDef, builtStruct map[string]interface{}) (out map[string]interface{}, err error) {
out = builtStruct
for _, field := range fields {
resultingValue, err := a.resolveField(binaryDecoder, field.Type)
if err != nil {
return nil, fmt.Errorf("decoding field %s: %w", field.Name, err)
}
if resultingValue != skipField {
out[field.Name] = resultingValue
}
}
return
}
type skipFieldType int
var skipField = skipFieldType(0)
type field struct {
name string
value interface{}
}
func (a *ABI) resolveField(binaryDecoder *Decoder, initialFieldType string) (out interface{}, err error) {
// retrieve the fields characteristics, note we can be a few depth down here....
fieldType, isOptional, isArray, isBinaryExtension := analyzeFieldType(initialFieldType)
//fmt.Println("resolveField", isOptional, isArray, initialFieldType, fieldType)
if tracer.Enabled() {
zlog.Debug("analyzed field",
zap.String("field_type", fieldType),
zap.Bool("is_optional", isOptional),
zap.Bool("is_array", isArray),
zap.Bool("is_binaryExtension", isBinaryExtension),
)
}
// check if this field is an alias
aliasFieldType, isAlias := a.TypeNameForNewTypeName(fieldType)
if isAlias {
if tracer.Enabled() {
zlog.Debug("type is an alias",
zap.String("from", fieldType),
zap.String("to", aliasFieldType),
)
}
fieldType = aliasFieldType
}
// check if the field is a binary extension
if isBinaryExtension && !binaryDecoder.hasRemaining() {
if tracer.Enabled() {
zlog.Debug("type is a binary extension and no more data, skipping field", zap.String("type", fieldType))
}
return skipField, nil
}
// check if the field is optional
if isOptional {
b, err := binaryDecoder.ReadByte()
if err != nil {
return nil, fmt.Errorf("reading optional flag: %w", err)
}
if b == 0 {
if tracer.Enabled() {
zlog.Debug("field is not present")
}
if !a.fitNodeos {
return skipField, nil
}
// TODO: Not sure about this right now
fieldType = "null"
}
}
// if we have an array, we will loop in it and handle the subField (note that if we have an array of ALIAS we would loop here)
if isArray {
retVal, err := a.readArray(binaryDecoder, fieldType)
if err != nil {
return nil, err
}
return retVal, nil
}
// if the fiels is not an array, but is an alias we need to re-resolve the field again
if isAlias {
return a.resolveField(binaryDecoder, fieldType)
}
return a.read(binaryDecoder, fieldType)
}
func (a *ABI) readArray(binaryDecoder *Decoder, fieldType string) ([]interface{}, error) {
//fmt.Println("Read array", fieldType)
length, err := binaryDecoder.ReadUvarint64()
if err != nil {
return nil, fmt.Errorf("reading array length: %w", err)
}
if length == 0 {
return []interface{}{}, nil // we just want "[]" in the final output
}
var elements []interface{}
for i := uint64(0); i < length; i++ {
//fmt.Println("Field type", fieldType)
retVal, err := a.resolveField(binaryDecoder, fieldType)
if err != nil {
return nil, fmt.Errorf("resolve array index [%d]: %w", i, err)
}
if retVal != skipField {
elements = append(elements, retVal)
}
}
return elements, nil
}
// Decodes the EOS ABIs built-in types
func (a *ABI) read(binaryDecoder *Decoder, fieldType string) (interface{}, error) {
variant := a.VariantForName(fieldType)
if variant != nil {
variantIndex, err := binaryDecoder.ReadUvarint32()
if err != nil {
return nil, fmt.Errorf("unable to read variant type index: %w", err)
}
if int(variantIndex) >= len(variant.Types) {
return nil, fmt.Errorf("variant type index is unknown, got type index %d, know up to index %d", variantIndex, len(variant.Types)-1)
}
variantFieldType := variant.Types[variantIndex]
if tracer.Enabled() {
zlog.Debug("field is a variant", zap.String("type", variantFieldType))
}
resolvedVariantFieldType, isAlias := a.TypeNameForNewTypeName(variantFieldType)
if isAlias && tracer.Enabled() {
zlog.Debug("variant type is an alias", zap.String("from", fieldType), zap.String("to", resolvedVariantFieldType))
}
fieldType = resolvedVariantFieldType
}
structure := a.StructForName(fieldType)
if structure != nil {
builtStruct, err := a.decode(binaryDecoder, fieldType)
if err != nil {
return nil, fmt.Errorf("decoding [%s]: %w", fieldType, err)
}
return builtStruct, nil
}
var value interface{}
var err error
switch fieldType {
case "null":
value = nil
case "int8":
value, err = binaryDecoder.ReadInt8()
case "uint8":
value, err = binaryDecoder.ReadUInt8()
case "int16":
value, err = binaryDecoder.ReadInt16()
case "uint16":
value, err = binaryDecoder.ReadUint16()
case "int32":
value, err = binaryDecoder.ReadInt32()
case "uint32":
value, err = binaryDecoder.ReadUint32()
case "int64":
var val int64
val, err = binaryDecoder.ReadInt64()
value = Int64(val)
case "uint64":
var val uint64
val, err = binaryDecoder.ReadUint64()
value = Uint64(val)
case "int128":
v, e := binaryDecoder.ReadInt128()
if e == nil {
if a.fitNodeos {
value = v.DecimalString()
} else {
value = v
}
}
err = e
case "uint128":
v, e := binaryDecoder.ReadUint128("uint128")
if e == nil {
if a.fitNodeos {
value = v.DecimalString()
} else {
value = v
}
}
err = e
case "varint32":
value, err = binaryDecoder.ReadVarint32()
case "varuint32":
value, err = binaryDecoder.ReadUvarint32()
case "float32":
v, e := binaryDecoder.ReadFloat32()
if e == nil {
if a.fitNodeos {
value = strconv.FormatFloat(float64(v), 'f', 17, 32)
} else {
value = json.RawMessage(strconv.FormatFloat(float64(v), 'f', -1, 64)) // as sjson does
}
}
err = e
case "float64":
v, e := binaryDecoder.ReadFloat64()
if e == nil {
value = formatFloat(v, a.fitNodeos)
}
err = e
case "float128":
value, err = binaryDecoder.ReadUint128("float128")
case "bool":
if a.fitNodeos {
value, err = binaryDecoder.ReadByte()
} else {
value, err = binaryDecoder.ReadBool()
}
case "time_point":
timePoint, e := binaryDecoder.ReadTimePoint() //todo double check
if e == nil {
value = formatTimePoint(timePoint, a.fitNodeos)
}
err = e
case "time_point_sec":
timePointSec, e := binaryDecoder.ReadTimePointSec()
if e == nil {
t := time.Unix(int64(timePointSec), 0)
value = t.UTC().Format("2006-01-02T15:04:05")
}
err = e
case "block_timestamp_type":
value, err = binaryDecoder.ReadBlockTimestamp()
if err == nil {
value = value.(BlockTimestamp).Time.UTC().Format("2006-01-02T15:04:05")
}
case "name":
value, err = binaryDecoder.ReadName()
case "bytes":
value, err = binaryDecoder.ReadByteArray()
if err == nil {
value = hex.EncodeToString(value.([]byte))
}
case "string":
value, err = binaryDecoder.ReadString()
case "checksum160":
value, err = binaryDecoder.ReadChecksum160()
case "checksum256":
value, err = binaryDecoder.ReadChecksum256()
case "checksum512":
value, err = binaryDecoder.ReadChecksum512()
case "public_key":
value, err = binaryDecoder.ReadPublicKey()
case "signature":
value, err = binaryDecoder.ReadSignature()
case "symbol":
symbol, e := binaryDecoder.ReadSymbol()
err = e
if err == nil {
value = fmt.Sprintf("%d,%s", symbol.Precision, symbol.Symbol)
}
case "symbol_code":
value, err = binaryDecoder.ReadSymbolCode()
case "asset":
value, err = binaryDecoder.ReadAsset()
case "extended_asset":
value, err = binaryDecoder.ReadExtendedAsset()
default:
return nil, fmt.Errorf("read field of type [%s]: unknown type", fieldType)
}
if err != nil {
return nil, fmt.Errorf("read: %w", err)
}
if tracer.Enabled() {
zlog.Debug("set field value",
zap.Reflect("value", value),
)
}
if variant != nil {
// As a variant we need to include the field type in the json
return []interface{}{fieldType, value}, nil
}
// t0 := time.Now()
// defer func() {
// //fmt.Println("Doing field", fieldName, value, time.Since(t0))
// }()
return value, nil
}
func analyzeFieldType(fieldType string) (typeName string, isOptional bool, isArray bool, isBinaryExtension bool) {
if strings.HasSuffix(fieldType, "[]$") {
return fieldType[0 : len(fieldType)-3], false, true, true
}
if strings.HasSuffix(fieldType, "?") {
return fieldType[0 : len(fieldType)-1], true, false, false
}
if strings.HasSuffix(fieldType, "$") {
return fieldType[0 : len(fieldType)-1], false, false, true
}
if strings.HasSuffix(fieldType, "[]") {
return fieldType[0 : len(fieldType)-2], false, true, false
}
return fieldType, false, false, false
}
const standardTimePointFormat = "2006-01-02T15:04:05.999"
const nodeosTimePointFormat = "2006-01-02T15:04:05.000"
func formatTimePoint(timePoint TimePoint, shouldFitNodeos bool) string {
t := time.Unix(0, int64(timePoint*1000))
if shouldFitNodeos {
return t.UTC().Format(nodeosTimePointFormat)
}
return t.UTC().Format(standardTimePointFormat)
}
const standardTimePointSecFormat = "2006-01-02T15:04:05"
func formatTimePointSec(timePoint TimePointSec) string {
t := time.Unix(int64(timePoint), 0)
return t.UTC().Format(standardTimePointSecFormat)
}
func formatFloat(v float64, fitNodeos bool) interface{} {
switch {
case math.IsInf(v, 1):
return "inf"
case math.IsInf(v, -1):
return "-inf"
case math.IsNaN(v): // cannot check equality on math.NaN()
return "nan"
default:
}
if fitNodeos {
return strconv.FormatFloat(v, 'f', 17, 64)
} else {
return json.RawMessage(strconv.FormatFloat(float64(v), 'f', -1, 64))
}
}