/
bigmap.go
403 lines (374 loc) · 9.84 KB
/
bigmap.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
// Copyright (c) 2020-2021 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package micheline
import (
"bytes"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"blockwatch.cc/tzgo/tezos"
)
var BigmapRefType = Prim{
Type: PrimNullary,
OpCode: T_INT,
}
func NewBigmapRefType(anno string) Prim {
r := Prim{
Type: PrimNullary,
OpCode: T_INT,
}
if anno != "" {
r.Anno = []string{"@" + anno}
}
return r
}
func NewBigmapRef(id int64) Prim {
return Prim{
Type: PrimInt,
Int: big.NewInt(id),
}
}
type BigmapDiff []BigmapDiffElem
type BigmapDiffElem struct {
Action DiffAction
Id int64
SourceId int64 // used on copy
DestId int64 // used on copy
KeyHash tezos.ExprHash
Key Prim // works with any type
Value Prim
KeyType Prim // used on alloc/copy, uses Prim for native marshalling
ValueType Prim // used on alloc/copy, uses Prim for native marshalling
}
func (e BigmapDiffElem) Encoding() PrimType {
switch e.Action {
case DiffActionRemove, DiffActionUpdate:
return e.Key.OpCode.PrimType()
case DiffActionAlloc, DiffActionCopy:
return e.KeyType.OpCode.PrimType()
default:
// invalid
return PrimBytes
}
}
func (e BigmapDiffElem) GetKey(typ Type) Key {
k, err := NewKey(typ, e.Key)
if err != nil {
log.Error(err)
}
return k
}
func (e BigmapDiffElem) GetKeyPtr(typ Type) *Key {
k, err := NewKey(typ, e.Key)
if err != nil {
log.Error(err)
}
return &k
}
// TODO: lazy_storage_diff updates
func (e *BigmapDiffElem) UnmarshalJSON(data []byte) error {
var val struct {
Id int64 `json:"big_map,string"`
Action DiffAction `json:"action"`
KeyType Prim `json:"key_type"` // alloc
ValueType Prim `json:"value_type"` // alloc
Key interface{} `json:"key"` // update/remove
KeyHash tezos.ExprHash `json:"key_hash"` // update/remove
Value Prim `json:"value"` // update
SourceId int64 `json:"source_big_map,string"` // copy
DestId int64 `json:"destination_big_map,string"` // copy
}
err := json.Unmarshal(data, &val)
if err != nil {
return err
}
switch val.Action {
case DiffActionUpdate, DiffActionRemove:
// Note: on Edo key can be list or object
switch key := val.Key.(type) {
case map[string]interface{}:
switch len(key) {
case 0:
// EMPTY_BIG_MAP opcode emits a remove action without key
e.Key = Prim{
Type: PrimNullary,
OpCode: I_EMPTY_BIG_MAP,
}
case 1:
for n, v := range key {
vv, ok := v.(string)
if !ok {
return fmt.Errorf("micheline: decoding bigmap key '%v': unexpected type %T", v, v)
}
switch n {
case "int":
p := Prim{
Type: PrimInt,
Int: big.NewInt(0),
}
if err := p.Int.UnmarshalText([]byte(vv)); err != nil {
return fmt.Errorf("micheline: decoding bigmap int key '%s': %v", v, err)
}
e.Key = p
case "bytes":
p := Prim{
Type: PrimBytes,
}
p.Bytes, err = hex.DecodeString(vv)
if err != nil {
return fmt.Errorf("micheline: decoding bigmap bytes key '%s': %v", v, err)
}
e.Key = p
case "string":
e.Key = Prim{
Type: PrimString,
String: vv,
}
case "prim":
p := Prim{}
if err := p.UnpackPrimitive(key); err != nil {
return fmt.Errorf("micheline: decoding bigmap prim key: %v", err)
}
e.Key = p
default:
return fmt.Errorf("micheline: unsupported bigmap key type %s", n)
}
}
default:
p := Prim{}
if err := p.UnpackPrimitive(key); err != nil {
return fmt.Errorf("micheline: decoding bigmap pair key: %v", err)
}
e.Key = p
}
case []interface{}:
p := Prim{}
if err := p.UnpackSequence(key); err != nil {
return fmt.Errorf("micheline: decoding bigmap list key: %v", err)
}
e.Key = p
default:
// EMPTY_BIG_MAP opcode emits a remove action without key
if val.Key == nil {
e.Key = Prim{
Type: PrimNullary,
OpCode: I_EMPTY_BIG_MAP,
}
}
}
e.KeyHash = val.KeyHash
e.Value = val.Value
case DiffActionAlloc:
if !val.KeyType.OpCode.IsValid() {
return fmt.Errorf("micheline: unsupported bigmap key type (opcode) %s [%d]",
e.KeyType.OpCode, e.KeyType.OpCode)
}
e.KeyType = val.KeyType
e.ValueType = val.ValueType
case DiffActionCopy:
e.SourceId = val.SourceId
e.DestId = val.DestId
}
// assign remaining values
e.Id = val.Id
e.Action = val.Action
// pre-v005: set correct action on value deletion (missing value in JSON)
if !e.Value.IsValid() && e.Action == DiffActionUpdate {
e.Action = DiffActionRemove
}
return nil
}
func (e BigmapDiffElem) MarshalJSON() ([]byte, error) {
var res interface{}
switch e.Action {
case DiffActionUpdate, DiffActionRemove:
// set key, keyhash, value
val := struct {
Id int64 `json:"big_map,string"`
Action DiffAction `json:"action"`
Key map[string]interface{} `json:"key,omitempty"`
KeyHash *tezos.ExprHash `json:"key_hash,omitempty"`
Value *Prim `json:"value,omitempty"`
}{
Id: e.Id,
Action: e.Action,
Value: &e.Value,
}
switch e.Key.Type {
case PrimNullary:
// no key on empty bigmap
case PrimInt:
val.Key = make(map[string]interface{})
val.Key["int"] = e.Key.Int.Text(10)
val.KeyHash = &e.KeyHash
case PrimBytes:
val.Key = make(map[string]interface{})
val.Key["bytes"] = hex.EncodeToString(e.Key.Bytes)
val.KeyHash = &e.KeyHash
case PrimString:
val.Key = make(map[string]interface{})
val.Key["string"] = e.Key.String
val.KeyHash = &e.KeyHash
case PrimBinary:
val.Key = make(map[string]interface{})
val.KeyHash = &e.KeyHash
buf, err := e.Key.MarshalJSON()
if err != nil {
return nil, err
}
if err := json.Unmarshal(buf, &val.Key); err != nil {
return nil, err
}
}
// be API compatible with Babylon
if e.Action == DiffActionRemove {
val.Value = nil
}
res = val
case DiffActionAlloc:
res = struct {
Id int64 `json:"big_map,string"`
Action DiffAction `json:"action"`
KeyType Prim `json:"key_type"` // alloc, copy only; native Prim!
ValueType Prim `json:"value_type"` // alloc, copy only; native Prim!
}{
Id: e.Id,
Action: e.Action,
KeyType: e.KeyType,
ValueType: e.ValueType,
}
case DiffActionCopy:
res = struct {
Action DiffAction `json:"action"`
SourceId int64 `json:"source_big_map,string"` // copy
DestId int64 `json:"destination_big_map,string"` // copy
}{
Action: e.Action,
SourceId: e.SourceId,
DestId: e.DestId,
}
}
return json.Marshal(res)
}
func (b BigmapDiff) MarshalBinary() ([]byte, error) {
buf := bytes.NewBuffer(nil)
for _, v := range b {
// prefix with id (4 byte) and action (1 byte)
// temp bigmaps have negative numbers
binary.Write(buf, binary.BigEndian, uint32(v.Id))
buf.WriteByte(byte(v.Action))
// encoding depends on action
switch v.Action {
case DiffActionUpdate, DiffActionRemove:
// pair(pair(key_type, hash), value)
key := Prim{
Type: PrimBinary,
OpCode: T_PAIR,
Args: []Prim{
v.Key,
Prim{
Type: PrimBytes,
Bytes: v.KeyHash.Hash.Hash,
},
},
}
val := v.Value
if !val.IsValid() {
// DiffActionRemove
val = Prim{
Type: PrimNullary,
OpCode: D_NONE,
}
}
kvpair := Prim{
Type: PrimBinary,
OpCode: T_PAIR,
Args: []Prim{key, val},
}
if err := kvpair.EncodeBuffer(buf); err != nil {
return nil, err
}
case DiffActionAlloc:
// pair(key_type, value_type)
kvpair := Prim{
Type: PrimBinary,
OpCode: T_PAIR,
Args: []Prim{v.KeyType, v.ValueType},
}
if err := kvpair.EncodeBuffer(buf); err != nil {
return nil, err
}
case DiffActionCopy:
// pair(src, dest)
kvpair := Prim{
Type: PrimBinary,
OpCode: T_PAIR,
Args: []Prim{
Prim{
Type: PrimInt,
Int: big.NewInt(v.SourceId),
},
Prim{
Type: PrimInt,
Int: big.NewInt(v.DestId),
},
},
}
if err := kvpair.EncodeBuffer(buf); err != nil {
return nil, err
}
}
}
return buf.Bytes(), nil
}
func (b *BigmapDiff) UnmarshalBinary(data []byte) error {
buf := bytes.NewBuffer(data)
for buf.Len() > 0 {
id := int32(binary.BigEndian.Uint32(buf.Next(4)))
elem := BigmapDiffElem{
Id: int64(id),
Action: DiffAction(buf.Next(1)[0]),
}
prim := Prim{}
if err := prim.DecodeBuffer(buf); err != nil {
return err
}
if prim.Type != PrimBinary && prim.OpCode != T_PAIR {
return fmt.Errorf("micheline: unexpected big_map_diff keypair type %s opcode %s", prim.Type, prim.OpCode)
}
if l := len(prim.Args); l != 2 {
return fmt.Errorf("micheline: unexpected big_map_diff keypair len %d", l)
}
switch elem.Action {
case DiffActionUpdate, DiffActionRemove:
// encoded as pair(pair(key,hash), val)
if prim.Args[0].Args[1].Type != PrimBytes {
return fmt.Errorf("micheline: unexpected big_map_diff keyhash type %s", prim.Args[0].Args[1].Type)
}
if err := elem.KeyHash.UnmarshalBinary(prim.Args[0].Args[1].Bytes); err != nil {
return err
}
elem.Key = prim.Args[0].Args[0]
elem.Value = prim.Args[1]
if elem.Action == DiffActionRemove {
elem.Value = Prim{}
}
case DiffActionAlloc:
// encoded as pair(key_type, value_type)
elem.KeyType = prim.Args[0]
elem.ValueType = prim.Args[1]
if !elem.KeyType.OpCode.IsValid() {
return fmt.Errorf("micheline: invalid big_map_diff key type opcode %s [%d]",
prim.Args[0].OpCode, prim.Args[0].OpCode)
}
case DiffActionCopy:
// encoded as pair(src_id, dest_id)
elem.SourceId = prim.Args[0].Int.Int64()
elem.DestId = prim.Args[1].Int.Int64()
}
*b = append(*b, elem)
}
return nil
}