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
/
clone.go
320 lines (301 loc) · 6.71 KB
/
clone.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
// Copyright (c) 2023 Blockwatch Data Inc.
// Author: alex@blockwatch.cc, abdul@blockwatch.cc
package compose
import (
"encoding/hex"
"encoding/json"
"fmt"
"os"
"blockwatch.cc/tzgo/micheline"
"blockwatch.cc/tzgo/tezos"
)
type CloneMode byte
const (
CloneModeFile CloneMode = iota
CloneModeJson
CloneModeBinary
CloneModeUrl
CloneModeArgs
)
func (m CloneMode) String() string {
switch m {
case CloneModeFile:
return "file"
case CloneModeJson:
return "json"
case CloneModeBinary:
return "binary"
case CloneModeUrl:
return "url"
case CloneModeArgs:
return "args"
default:
return ""
}
}
func (m *CloneMode) Set(s string) (err error) {
switch s {
case "file":
*m = CloneModeFile
case "json":
*m = CloneModeJson
case "bin":
*m = CloneModeBinary
case "url":
*m = CloneModeUrl
case "args":
*m = CloneModeArgs
default:
err = fmt.Errorf("invalid clone mode")
}
return
}
type CloneConfig struct {
Name string
Contract tezos.Address
IndexUrl string
NumOps uint
Path string
Mode CloneMode
}
type Op struct {
Type string `json:"type"`
Hash string `json:"hash"`
Height int `json:"height"`
OpP int `json:"op_p"`
OpC int `json:"op_c"`
OpI int `json:"op_i"`
IsInternal bool `json:"is_internal"`
Sender string `json:"sender"`
Receiver string `json:"receiver"`
Amount float64 `json:"volume"`
Script *micheline.Script `json:"script"`
Params *struct {
Entrypoint string `json:"entrypoint"`
Prim micheline.Prim `json:"prim"`
} `json:"parameters"`
// processed
Url string `json:"-"`
PackedCode string `json:"-"`
PackedStorage string `json:"-"`
PackedParams string `json:"-"`
Args any `json:"-"`
}
func Clone(ctx Context, version string, cfg CloneConfig) error {
if !HasVersion(version) {
return ErrInvalidVersion
}
if !cfg.Contract.IsContract() {
return fmt.Errorf("invalid contract address")
}
if cfg.Name == "" {
cfg.Name = cfg.Contract.String()
}
ops, err := fetchOps(ctx, cfg)
if err != nil {
return err
}
eng := New(version)
buf, err := eng.Clone(ctx, ops, cfg)
if err != nil {
return err
}
err = os.WriteFile(cfg.Path, buf, 0644)
if err != nil {
return err
}
ctx.Log.Infof("File %s written successfully.", cfg.Path)
return nil
}
func fetchOps(ctx Context, cfg CloneConfig) ([]Op, error) {
ctx.Log.Infof("Fetching contract operations...")
u := fmt.Sprintf("%s/explorer/account/%s/operations?prim=1&storage=1&order=asc&limit=%d",
cfg.IndexUrl, cfg.Contract, cfg.NumOps+1)
resp, err := Fetch[[]Op](ctx, u)
if err != nil {
return nil, err
}
ops := *resp
if len(ops) == 0 {
return nil, fmt.Errorf("contract %q has no transactions", cfg.Contract)
}
switch cfg.Mode {
case CloneModeFile:
err = storeOps(ctx, ops, cfg)
case CloneModeJson:
err = encodeJson(ops)
case CloneModeBinary:
err = encodeBinary(ops)
case CloneModeUrl:
encodeUrl(ops, ctx.url)
}
if err != nil {
return nil, err
}
if err := encodeArgs(ops); err != nil {
ctx.Log.Warnf("Marshaling args: %v", err)
}
return ops, nil
}
func storeOps(ctx Context, ops []Op, cfg CloneConfig) error {
for i := range ops {
var (
buf []byte
err error
)
ops[i].Url = generateFilename(cfg, ops[i], i)
switch ops[i].Type {
case "origination":
buf, err = json.Marshal(ops[i].Script)
case "transaction":
if ops[i].Params != nil {
buf, err = json.Marshal(ops[i].Params.Prim)
}
}
if err != nil {
return err
}
err = os.WriteFile(ops[i].Url, buf, 0644)
if err != nil {
return err
}
ctx.Log.Infof("File %s written.", ops[i].Url)
}
return nil
}
func encodeJson(ops []Op) error {
for i := range ops {
var (
buf []byte
err error
)
switch ops[i].Type {
case "origination":
buf, err = json.Marshal(ops[i].Script.Code)
if err == nil {
ops[i].PackedCode = string(buf)
buf, err = json.Marshal(ops[i].Script.Storage)
}
if err == nil {
ops[i].PackedStorage = string(buf)
}
case "transaction":
if ops[i].Params != nil {
buf, err = json.Marshal(ops[i].Params.Prim)
ops[i].PackedParams = string(buf)
}
}
if err != nil {
return err
}
}
return nil
}
func encodeBinary(ops []Op) error {
for i := range ops {
var (
buf []byte
err error
)
switch ops[i].Type {
case "origination":
buf, err = ops[i].Script.Code.MarshalBinary()
if err == nil {
ops[i].PackedCode = hex.EncodeToString(buf)
buf, err = ops[i].Script.Storage.MarshalBinary()
}
if err == nil {
ops[i].PackedStorage = hex.EncodeToString(buf)
}
case "transaction":
if ops[i].Params != nil {
buf, err = ops[i].Params.Prim.MarshalBinary()
ops[i].PackedParams = hex.EncodeToString(buf)
}
}
if err != nil {
return err
}
}
return nil
}
func encodeUrl(ops []Op, host string) {
for i, op := range ops {
switch op.Type {
case "origination":
if ops[i].IsInternal {
ops[i].Url = fmt.Sprintf(
"%s/chains/main/blocks/%d/operations/3/%d#contents.%d.metadata.internal_operation_results.%d.script",
host,
op.Height,
op.OpP,
op.OpC,
op.OpI,
)
} else {
ops[i].Url = fmt.Sprintf(
"%s/chains/main/blocks/%d/operations/3/%d#contents.%d.script",
host,
op.Height,
op.OpP,
op.OpC,
)
}
case "transaction":
if ops[i].IsInternal {
ops[i].Url = fmt.Sprintf(
"%s/chains/main/blocks/%d/operations/3/%d#contents.%d.metadata.internal_operation_results.%d.parameters.value",
host,
op.Height,
op.OpP,
op.OpC,
op.OpI,
)
} else {
ops[i].Url = fmt.Sprintf(
"%s/chains/main/blocks/%d/operations/3/%d#contents.%d.parameters.value",
host,
op.Height,
op.OpP,
op.OpC,
)
}
}
}
}
func encodeArgs(ops []Op) error {
var script *micheline.Script
for i, op := range ops {
switch op.Type {
case "origination":
script = op.Script
val := micheline.NewValue(script.StorageType(), script.Storage).
UnpackAllAsciiStrings()
res, err := val.Map()
if err != nil {
return err
}
ops[i].Args = res
case "transaction":
eps, err := script.Entrypoints(true)
if err != nil {
return err
}
ep, ok := eps[op.Params.Entrypoint]
if !ok {
return fmt.Errorf("missing entrypoint %s", op.Params.Entrypoint)
}
val := micheline.NewValue(ep.Type(), op.Params.Prim).
UnpackAllAsciiStrings()
res, err := val.Map()
if err != nil {
return err
}
ops[i].Args = res.(map[string]any)[op.Params.Entrypoint]
}
}
return nil
}
func generateFilename(cfg CloneConfig, op Op, index int) string {
return fmt.Sprintf("%02d-%s-%s.json", index, cfg.Name, op.Type)
}