This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
translate.go
391 lines (351 loc) · 12.6 KB
/
translate.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
// Copyright (c) 2020-2021 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
//go:build !trace
// +build !trace
package micheline
import (
"encoding/json"
"fmt"
"strconv"
)
func walkTree(m map[string]interface{}, label string, typ Type, stack *Stack, lvl int) error {
// abort infinite type recursions
if lvl > 99 {
return fmt.Errorf("micheline: max nesting level reached")
}
// take next value from stack
val := stack.Pop()
// unfold unexpected pairs
if !val.WasPacked && val.IsPair() && !typ.IsPair() {
unfolded := val.UnfoldPair(typ)
stack.Push(unfolded...)
val = stack.Pop()
}
// detect type for unpacked values
if val.WasPacked && (!val.IsScalar() || typ.OpCode == T_BYTES) {
labels := typ.Anno
typ = val.BuildType()
typ.WasPacked = true
typ.Anno = labels
}
// make sure value + type we're going to process actually match up
// accept any kind of pairs/seq which will be unfolded again below
if !typ.IsPair() && !val.IsSequence() && !val.matchOpCode(typ.OpCode) {
return fmt.Errorf("micheline: type mismatch: type[%s]=%s value[%s/%d]=%s",
typ.OpCode, typ.DumpLimit(512), val.Type, val.OpCode, val.DumpLimit(512))
}
// get the label from our type tree
typeLabel := typ.Label()
haveTypeLabel := len(typeLabel) > 0
haveKeyLabel := label != EMPTY_LABEL && len(label) > 0
if label == EMPTY_LABEL {
if haveTypeLabel {
// overwrite struct field label from type annotation
label = typeLabel
} else {
// or use sequence number when type annotation is empty
label = strconv.Itoa(len(m))
}
}
// attach sub-records and array elements based on type code
switch typ.OpCode {
case T_SET:
// set <comparable type>
arr := make([]interface{}, 0, len(val.Args))
for _, v := range val.Args {
if v.IsScalar() && !v.IsSequence() {
// array of scalar types
arr = append(arr, v.Value(typ.Args[0].OpCode))
} else {
// array of complex types
mm := make(map[string]interface{})
if err := walkTree(mm, EMPTY_LABEL, Type{typ.Args[0]}, NewStack(v), lvl+1); err != nil {
return err
}
arr = append(arr, mm)
}
}
m[label] = arr
case T_LIST:
// list <type>
arr := make([]interface{}, 0, len(val.Args))
for i, v := range val.Args {
// lists may contain different types, i.e. when unpack+detect is used
valType := typ.Args[0]
if len(typ.Args) > i {
valType = typ.Args[i]
}
// unpack into map
mm := make(map[string]interface{})
if err := walkTree(mm, EMPTY_LABEL, Type{valType}, NewStack(v), lvl+1); err != nil {
return err
}
// lift scalar nested list and simple element
unwrapped := false
if len(mm) == 1 {
if mval, ok := mm["0"]; ok {
if marr, ok := mval.([]interface{}); ok {
arr = append(arr, marr)
} else {
arr = append(arr, mval)
}
unwrapped = true
}
}
if !unwrapped {
arr = append(arr, mm)
}
}
m[label] = arr
case T_LAMBDA:
// LAMBDA <type> <type> { <instruction> ... }
m[label] = val
case T_MAP, T_BIG_MAP:
// map <comparable type> <type>
// big_map <comparable type> <type>
// sequence of Elt (key/value) pairs
// render bigmap reference
if typ.OpCode == T_BIG_MAP && (len(val.Args) == 0 || !val.Args[0].IsElt()) {
switch val.Type {
case PrimInt:
// Babylon bigmaps contain a reference here
m[label] = val.Int.Int64()
case PrimSequence:
if len(val.Args) == 0 {
// pre-babylon there's only an empty sequence
// FIXME: we could insert the bigmap id, but this is unknown at ths point
m[label] = nil
} else {
if val.Args[0].IsSequence() {
if err := walkTree(m, label, typ, NewStack(val.Args[0]), lvl); err != nil {
return err
}
} else {
m[label] = val.Args[0].Int.Int64()
}
stack.Push(val.Args[1:]...)
}
}
return nil
}
switch val.Type {
case PrimBinary: // single ELT
keyType := Type{typ.Args[0]}
valType := Type{typ.Args[1]}
// build type info if prim was packed
if val.Args[0].WasPacked {
keyType = val.Args[0].BuildType()
}
// build type info if prim was packed
if val.Args[1].WasPacked {
valType = val.Args[1].BuildType()
}
// prepare key
key, err := NewKey(keyType, val.Args[0])
if err != nil {
return err
}
mm := make(map[string]interface{})
if err := walkTree(mm, key.String(), valType, NewStack(val.Args[1]), lvl+1); err != nil {
return err
}
m[label] = mm
case PrimSequence: // sequence of ELTs
mm := make(map[string]interface{})
for _, v := range val.Args {
if v.OpCode != D_ELT {
return fmt.Errorf("micheline: unexpected type %s [%s] for %s Elt item", v.Type, v.OpCode, typ.OpCode)
}
keyType := Type{typ.Args[0]}
valType := Type{typ.Args[1]}
// build type info if prim was packed
if v.Args[0].WasPacked {
keyType = v.Args[0].BuildType()
}
// build type info if prim was packed
if v.Args[1].WasPacked {
valType = v.Args[1].BuildType()
}
key, err := NewKey(keyType, v.Args[0])
if err != nil {
return err
}
if err := walkTree(mm, key.String(), valType, NewStack(v.Args[1]), lvl+1); err != nil {
return err
}
}
m[label] = mm
default:
buf, _ := json.Marshal(val)
return fmt.Errorf("%*s> micheline: unexpected type %s [%s] for %s Elt sequence: %s",
lvl, "", val.Type, val.OpCode, typ.OpCode, buf)
}
case T_PAIR:
// pair <type> <type> or COMB
mm := m
if haveTypeLabel || haveKeyLabel {
mm = make(map[string]interface{})
}
// Try unfolding value (again) when type is T_PAIR,
// reuse the existing stack and push unfolded values
if val.IsPair() && !typ.IsPair() {
// unfold regular pair
unfolded := val.UnfoldPair(typ)
stack.Push(unfolded...)
} else if val.CanUnfold(typ) {
// comb pair
stack.Push(val.Args...)
} else {
// push value back on stack
stack.Push(val)
}
for _, t := range typ.Args {
if err := walkTree(mm, EMPTY_LABEL, Type{t}, stack, lvl+1); err != nil {
return err
}
}
if haveTypeLabel || haveKeyLabel {
m[label] = mm
}
case T_OPTION:
// option <type>
switch val.OpCode {
case D_NONE:
// add empty option values as null
m[label] = nil
case D_SOME:
// with annots (name) use it for scalar or complex render
// when next level annot equals this option annot, skip this annot
if val.IsScalar() || label == typ.Args[0].GetVarAnnoAny() {
if err := walkTree(m, label, Type{typ.Args[0]}, NewStack(val.Args[0]), lvl+1); err != nil {
return err
}
} else {
mm := make(map[string]interface{})
if err := walkTree(mm, EMPTY_LABEL, Type{typ.Args[0]}, NewStack(val.Args[0]), lvl+1); err != nil {
return err
}
m[label] = mm
}
default:
return fmt.Errorf("micheline: unexpected T_OPTION code %s [%s]: %s", val.OpCode, val.OpCode, val.Dump())
}
case T_OR:
// or <type> <type>
// use map to capture nested names
mm := make(map[string]interface{})
switch val.OpCode {
case D_LEFT:
if !(haveTypeLabel || haveKeyLabel) {
mmm := make(map[string]interface{})
if err := walkTree(mmm, EMPTY_LABEL, Type{typ.Args[0]}, NewStack(val.Args[0]), lvl+1); err != nil {
return err
}
// lift named content
if len(mmm) == 1 {
for n, v := range mmm {
switch n {
case "0":
mm["@or_0"] = v
default:
mm[n] = v
}
}
} else {
mm["@or_0"] = mmm
}
} else {
if err := walkTree(mm, EMPTY_LABEL, Type{typ.Args[0]}, NewStack(val.Args[0]), lvl+1); err != nil {
return err
}
}
case D_RIGHT:
if !(haveTypeLabel || haveKeyLabel) {
mmm := make(map[string]interface{})
if err := walkTree(mmm, EMPTY_LABEL, Type{typ.Args[1]}, NewStack(val.Args[0]), lvl+1); err != nil {
return err
}
// lift named content
if len(mmm) == 1 {
for n, v := range mmm {
switch n {
case "0":
mm["@or_1"] = v
default:
mm[n] = v
}
}
} else {
mm["@or_1"] = mmm
}
} else {
if err := walkTree(mm, EMPTY_LABEL, Type{typ.Args[1]}, NewStack(val.Args[0]), lvl+1); err != nil {
return err
}
}
default:
return fmt.Errorf("micheline: unexpected T_OR branch with value opcode %s", val.OpCode)
}
// lift anon content
if v, ok := mm["0"]; ok && len(mm) == 1 {
m[label] = v
} else {
m[label] = mm
}
case T_TICKET:
// always Pair( ticketer:address, Pair( original_type, int ))
stack.Push(val)
if err := walkTree(m, label, TicketType(typ.Args[0]), stack, lvl+1); err != nil {
return err
}
case T_SAPLING_STATE:
mm := make(map[string]interface{})
if err := walkTree(mm, "memo_size", Type{NewPrim(T_INT)}, NewStack(typ.Args[0]), lvl+1); err != nil {
return err
}
if err := walkTree(mm, "content", val.BuildType(), NewStack(val), lvl+1); err != nil {
return err
}
m[label] = mm
default:
// int
// nat
// string
// bytes
// mutez
// bool
// key_hash
// timestamp
// address
// key
// unit
// signature
// operation
// contract <type> (??)
// chain_id
// never
// chest_key
// chest
// append scalar or other complex value
// comb-pair records might have slipped through in LooksLikeContainer()
// so if we detect any unpacked comb part (i.e. sequence) we unpack it here
if val.IsSequence() {
stack.Push(val.Args...)
val = stack.Pop()
}
// safety check: skip invalid values (only happens if type detect was wrong)
if !val.IsValid() {
break
}
if val.IsScalar() {
m[label] = val.Value(typ.OpCode)
} else {
mm := make(map[string]interface{})
if err := walkTree(mm, EMPTY_LABEL, typ, NewStack(val), lvl+1); err != nil {
return err
}
m[label] = mm
}
}
return nil
}