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
/
lazy.go
256 lines (224 loc) · 5.28 KB
/
lazy.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
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package micheline
import (
"bytes"
"encoding/json"
"fmt"
"blockwatch.cc/tzgo/tezos"
)
type LazyKind string
const (
LazyKindInvalid LazyKind = ""
LazyKindBigmap LazyKind = "big_map"
LazyKindSapling LazyKind = "sapling_state"
)
func (k LazyKind) String() string {
switch k {
case LazyKindBigmap:
return "big_map"
case LazyKindSapling:
return "sapling_state"
}
return ""
}
func ParseLazyKind(data string) LazyKind {
switch data {
case "big_map":
return LazyKindBigmap
case "sapling_state":
return LazyKindSapling
default:
return LazyKindInvalid
}
}
func (k LazyKind) IsValid() bool {
return k != LazyKindInvalid
}
func (k LazyKind) MarshalText() ([]byte, error) {
return []byte(k.String()), nil
}
func (k *LazyKind) UnmarshalText(data []byte) error {
lk := ParseLazyKind(string(data))
if !lk.IsValid() {
return fmt.Errorf("micheline: invalid lazy kind %q", string(data))
}
*k = lk
return nil
}
type LazyEvent interface {
Kind() LazyKind
Id() int64
}
type GenericEvent struct {
EventKind LazyKind `json:"kind"`
ObjectId int64 `json:"id,string"`
}
func (d *GenericEvent) Kind() LazyKind {
return d.EventKind
}
func (d *GenericEvent) Id() int64 {
return d.ObjectId
}
type LazyEvents []LazyEvent
func (d LazyEvents) BigmapEvents() BigmapEvents {
if len(d) == 0 {
return nil
}
// count number of updates before allocating a slice
var count int
for _, v := range d {
if v.Kind() != LazyKindBigmap {
continue
}
ev := v.(*LazyBigmapEvent)
switch ev.Diff.Action {
case DiffActionAlloc:
count += 1 + len(ev.Diff.Updates)
case DiffActionUpdate:
count += len(ev.Diff.Updates)
default:
count++
}
}
if count == 0 {
return nil
}
// translate updates
events := make(BigmapEvents, 0, count)
for _, v := range d {
if v.Kind() != LazyKindBigmap {
continue
}
ev := v.(*LazyBigmapEvent)
switch ev.Diff.Action {
case DiffActionUpdate:
// upsert or remove
for _, vv := range ev.Diff.Updates {
event := BigmapEvent{
Action: DiffActionUpdate,
Id: ev.BigmapId,
KeyHash: vv.KeyHash,
Key: vv.Key,
Value: vv.Value,
}
if !vv.Value.IsValid() {
event.Action = DiffActionRemove
}
events = append(events, event)
}
case DiffActionRemove:
// key remove or bigmap remove
for _, vv := range ev.Diff.Updates {
event := BigmapEvent{
Action: DiffActionRemove,
Id: ev.BigmapId,
KeyHash: vv.KeyHash,
Key: vv.Key,
}
if !vv.Key.IsValid() && !vv.KeyHash.IsValid() {
event.Key = Prim{
Type: PrimNullary,
OpCode: I_EMPTY_BIG_MAP,
}
}
events = append(events, event)
}
case DiffActionAlloc:
// add an alloc event
events = append(events, BigmapEvent{
Action: DiffActionAlloc,
Id: ev.BigmapId,
KeyType: ev.Diff.KeyType,
ValueType: ev.Diff.ValueType,
})
// may contain upserts
for _, vv := range ev.Diff.Updates {
events = append(events, BigmapEvent{
Action: DiffActionUpdate,
Id: ev.BigmapId,
KeyHash: vv.KeyHash,
Key: vv.Key,
Value: vv.Value,
})
}
case DiffActionCopy:
events = append(events, BigmapEvent{
Action: DiffActionCopy,
SourceId: ev.Diff.SourceId,
DestId: ev.BigmapId,
})
}
}
return events
}
func (d *LazyEvents) UnmarshalJSON(data []byte) error {
if len(data) <= 2 {
return nil
}
if data[0] != '[' {
return fmt.Errorf("micheline: expected lazy event array")
}
// fmt.Printf("Decoding ops: %s\n", string(data))
dec := json.NewDecoder(bytes.NewReader(data))
// read open bracket
_, err := dec.Token()
if err != nil {
return fmt.Errorf("micheline: %v", err)
}
for dec.More() {
// peek into `{"kind":"...",` field
start := int(dec.InputOffset()) + 9
// after first JSON object, decoder pos is at `,`
if data[start] == '"' {
start += 1
}
end := start + bytes.IndexByte(data[start:], '"')
kind := ParseLazyKind(string(data[start:end]))
var ev LazyEvent
switch kind {
case LazyKindBigmap:
ev = &LazyBigmapEvent{}
case LazyKindSapling:
ev = &LazySaplingEvent{}
default:
log.Warnf("micheline: unsupported lazy diff kind %q", string(data[start:end]))
ev = &GenericEvent{}
}
if err := dec.Decode(ev); err != nil {
return fmt.Errorf("micheline: lazy kind %s: %v", kind, err)
}
(*d) = append(*d, ev)
}
return nil
}
type LazyBigmapEvent struct {
BigmapId int64 `json:"id,string"`
Diff struct {
Action DiffAction `json:"action"`
Updates []struct {
KeyHash tezos.ExprHash `json:"key_hash"` // update/remove
Key Prim `json:"key"` // update/remove
Value Prim `json:"value"` // update
} `json:"updates"` // update
KeyType Prim `json:"key_type"` // alloc
ValueType Prim `json:"value_type"` // alloc
SourceId int64 `json:"source,string"` // copy
} `json:"diff"`
}
func (d *LazyBigmapEvent) Kind() LazyKind {
return LazyKindBigmap
}
func (d *LazyBigmapEvent) Id() int64 {
return d.BigmapId
}
type LazySaplingEvent struct {
PoolId int64 `json:"id,string"`
Diff SaplingDiffElem `json:"diff"`
}
func (d *LazySaplingEvent) Kind() LazyKind {
return LazyKindSapling
}
func (d *LazySaplingEvent) Id() int64 {
return d.PoolId
}