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 30
/
bigmap.go
271 lines (250 loc) · 6.39 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
// Copyright (c) 2020-2021 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package micheline
import (
"bytes"
"encoding/binary"
"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 BigmapEvents []BigmapEvent
type BigmapEvent struct {
Action DiffAction `json:"action"`
Id int64 `json:"big_map,string"`
KeyHash tezos.ExprHash `json:"key_hash"` // update/remove
Key Prim `json:"key"` // update/remove
Value Prim `json:"value"` // update
KeyType Prim `json:"key_type"` // alloc
ValueType Prim `json:"value_type"` // alloc
SourceId int64 `json:"source_big_map,string"` // copy
DestId int64 `json:"destination_big_map,string"` // copy
}
func (e BigmapEvent) 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 BigmapEvent) GetKey(typ Type) Key {
k, err := NewKey(typ, e.Key)
if err != nil {
log.Error(err)
}
return k
}
func (e BigmapEvent) GetKeyPtr(typ Type) *Key {
k, err := NewKey(typ, e.Key)
if err != nil {
log.Error(err)
}
return &k
}
func (e *BigmapEvent) UnmarshalJSON(data []byte) error {
type alias BigmapEvent
err := json.Unmarshal(data, (*alias)(e))
if err != nil {
return err
}
// translate update with empty value to remove
if e.Action == DiffActionUpdate && !e.Value.IsValid() {
e.Action = DiffActionRemove
}
return nil
}
func (e BigmapEvent) 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 *Prim `json:"key,omitempty"`
KeyHash *tezos.ExprHash `json:"key_hash,omitempty"`
Value *Prim `json:"value,omitempty"`
}{
Id: e.Id,
Action: e.Action,
}
if e.KeyHash.IsValid() {
val.KeyHash = &e.KeyHash
}
if e.Key.IsValid() {
val.Key = &e.Key
}
if e.Value.IsValid() {
val.Value = &e.Value
}
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 BigmapEvents) 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,
{
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{
{
Type: PrimInt,
Int: big.NewInt(v.SourceId),
},
{
Type: PrimInt,
Int: big.NewInt(v.DestId),
},
},
}
if err := kvpair.EncodeBuffer(buf); err != nil {
return nil, err
}
}
}
return buf.Bytes(), nil
}
func (b *BigmapEvents) UnmarshalBinary(data []byte) error {
buf := bytes.NewBuffer(data)
for buf.Len() > 0 {
id := int32(binary.BigEndian.Uint32(buf.Next(4)))
elem := BigmapEvent{
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
}