forked from eoscanada/eos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abidecoder.go
440 lines (380 loc) · 12.2 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
package eos
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
"time"
"github.com/tidwall/sjson"
"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)
}
return a.decode(binaryDecoder, action.Type)
}
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)
}
return a.decode(binaryDecoder, tbl.Type)
}
func (a *ABI) DecodeTableRowTyped(tableType string, data []byte) ([]byte, error) {
binaryDecoder := NewDecoder(data)
return a.decode(binaryDecoder, tableType)
}
func (a *ABI) Decode(binaryDecoder *Decoder, structName string) ([]byte, error) {
return a.decode(binaryDecoder, structName)
}
func (a *ABI) decode(binaryDecoder *Decoder, structName string) ([]byte, error) {
if loggingEnabled {
abiDecoderLog.Debug("decode struct", zap.String("name", structName))
}
structure := a.StructForName(structName)
if structure == nil {
return nil, fmt.Errorf("structure [%s] not found in abi", structName)
}
resultingJSON := make([]byte, 0)
if structure.Base != "" {
if loggingEnabled {
abiDecoderLog.Debug("struct has base struct", zap.String("name", structName), zap.String("base", structure.Base))
}
baseName, isAlias := a.TypeNameForNewTypeName(structure.Base)
if isAlias && loggingEnabled {
abiDecoderLog.Debug("base is an alias", zap.String("from", structure.Base), zap.String("to", baseName))
}
var err error
resultingJSON, err = a.decode(binaryDecoder, baseName)
if err != nil {
return nil, fmt.Errorf("decode base [%s]: %s", structName, err)
}
}
return a.decodeFields(binaryDecoder, structure.Fields, resultingJSON)
}
func (a *ABI) decodeFields(binaryDecoder *Decoder, fields []FieldDef, json []byte) ([]byte, error) {
resultingJSON := json
var err error
for _, field := range fields {
resultingJSON, err = a.resolveField(binaryDecoder, field.Name, field.Type, resultingJSON)
if err != nil {
return nil, err
}
}
return resultingJSON, nil
}
func (a *ABI) resolveField(binaryDecoder *Decoder, fieldName, fieldType string, json []byte) ([]byte, error) {
// retrieve the fields characteristics, note we can be a few depth down here....
fieldType, isOptional, isArray, isBinaryExtension := analyzeFieldType(fieldType)
if loggingEnabled {
abiDecoderLog.Debug("analyzed field",
zap.String("field_name", fieldName),
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 loggingEnabled {
abiDecoderLog.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 loggingEnabled {
abiDecoderLog.Debug("type is a binary extension and no more data, skipping field", zap.String("type", fieldType))
}
return json, nil
}
// check if the field is optional
if isOptional {
if loggingEnabled {
abiDecoderLog.Debug("field is optional", zap.String("name", fieldName))
}
b, err := binaryDecoder.ReadByte()
if err != nil {
return nil, fmt.Errorf("decoding field [%s] optional flag: %s", fieldName, err)
}
if b == 0 {
if loggingEnabled {
abiDecoderLog.Debug("field is not present", zap.String("name", fieldName))
}
if !a.fitNodeos {
return json, 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 {
return a.readArray(binaryDecoder, fieldName, fieldType, json)
}
// 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, fieldName, fieldType, json)
}
return a.read(binaryDecoder, fieldName, fieldType, json)
}
func (a *ABI) readArray(binaryDecoder *Decoder, fieldName, fieldType string, json []byte) ([]byte, error) {
length, err := binaryDecoder.ReadUvarint64()
if err != nil {
return nil, fmt.Errorf("reading field [%s] array length: %s", fieldName, err)
}
if length == 0 {
json, _ = sjson.SetBytes(json, fieldName, []interface{}{})
//ignoring err because there is a bug in sjson. sjson shadow the err in case of a default type ...
//if err != nil {
// return nil, fmt.Errorf("reading field [%s] setting empty array: %s", fieldName, err)
//}
}
for i := uint64(0); i < length; i++ {
if loggingEnabled {
abiDecoderLog.Debug("adding value for field", zap.String("name", fieldName), zap.Uint64("index", i))
}
indexedFieldName := fmt.Sprintf("%s.%d", fieldName, i)
json, err = a.resolveField(binaryDecoder, indexedFieldName, fieldType, json)
if err != nil {
return nil, fmt.Errorf("reading field [%s] index [%d]: %s", fieldName, i, err)
}
}
return json, nil
}
// Decodes the EOS ABIs built-in types
func (a *ABI) read(binaryDecoder *Decoder, fieldName string, fieldType string, json []byte) ([]byte, 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: %s", 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 loggingEnabled {
abiDecoderLog.Debug("field is a variant", zap.String("type", variantFieldType))
}
resolvedVariantFieldType, isAlias := a.TypeNameForNewTypeName(variantFieldType)
if isAlias && loggingEnabled {
abiDecoderLog.Debug("variant type is an alias", zap.String("from", fieldType), zap.String("to", resolvedVariantFieldType))
}
fieldType = resolvedVariantFieldType
}
structure := a.StructForName(fieldType)
if structure != nil {
if loggingEnabled {
abiDecoderLog.Debug("field is a struct", zap.String("name", fieldName))
}
structureJSON := []byte{}
if structure.Base != "" {
if loggingEnabled {
abiDecoderLog.Debug("field's struct has base struct", zap.String("name", fieldName), zap.String("struct", structure.Name), zap.String("base", structure.Base))
}
baseName, isAlias := a.TypeNameForNewTypeName(structure.Base)
if isAlias {
if loggingEnabled {
abiDecoderLog.Debug("base is an alias", zap.String("from", structure.Base), zap.String("to", baseName))
}
}
baseStructure := a.StructForName(baseName)
if baseStructure == nil {
return nil, fmt.Errorf("base structure [%s] not found in abi", baseName)
}
var err error
structureJSON, err = a.decodeFields(binaryDecoder, baseStructure.Fields, structureJSON)
if err != nil {
return nil, fmt.Errorf("decoding field [%s] struct [%s] base [%s]: %s", fieldName, structure.Name, baseName, err)
}
}
var err error
structureJSON, err = a.decodeFields(binaryDecoder, structure.Fields, structureJSON)
if err != nil {
return nil, err
}
if loggingEnabled {
abiDecoderLog.Debug("set field value", zap.String("name", fieldName), zap.ByteString("json", structureJSON))
}
return sjson.SetRawBytes(json, fieldName, structureJSON)
}
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 = v
}
}
err = e
case "float64":
v, e := binaryDecoder.ReadFloat64()
if e == nil {
if a.fitNodeos {
value = strconv.FormatFloat(v, 'f', 17, 64)
} else {
value = v
}
}
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: %s", err)
}
if loggingEnabled {
abiDecoderLog.Debug("set field value",
zap.String("name", fieldName),
zap.Reflect("value", value),
)
}
if variant != nil {
// As a variant we need to include the field type in the json
return sjson.SetBytes(json, fieldName, []interface{}{fieldType, value})
}
return sjson.SetBytes(json, fieldName, value)
}
func analyzeFieldType(fieldType string) (typeName string, isOptional bool, isArray bool, isBinaryExtension bool) {
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)
}