-
Notifications
You must be signed in to change notification settings - Fork 45
/
manifest.go
444 lines (384 loc) · 11.3 KB
/
manifest.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package manifest
import (
"bytes"
"errors"
"fmt"
"math"
"os"
"path/filepath"
"regexp"
"strings"
pbsubstreams "github.com/streamingfast/substreams/pb/sf/substreams/v1"
"gopkg.in/yaml.v3"
)
const UNSET = math.MaxUint64
var moduleNameRegexp *regexp.Regexp
func init() {
moduleNameRegexp = regexp.MustCompile(`^([a-zA-Z][a-zA-Z0-9_]{0,63})$`)
}
const (
ModuleKindStore = "store"
ModuleKindMap = "map"
ModuleKindBlockIndex = "blockIndex"
)
// Manifest is a YAML structure used to create a Package and its list
// of Modules. The notion of a manifest does not live in protobuf definitions.
type Manifest struct {
SpecVersion string `yaml:"specVersion"` // check that it equals v0.1.0
Package PackageMeta `yaml:"package"`
Protobuf Protobuf `yaml:"protobuf"`
Imports mapSlice `yaml:"imports"`
Binaries map[string]Binary `yaml:"binaries"`
Modules []*Module `yaml:"modules"`
Params map[string]string `yaml:"params"`
BlockFilters map[string]string `yaml:"blockFilters"`
Network string `yaml:"network"`
Networks map[string]*NetworkParams `yaml:"networks"`
Sink *Sink `yaml:"sink"`
Graph *ModuleGraph `yaml:"-"`
Workdir string `yaml:"-"`
}
type NetworkParams struct {
InitialBlocks map[string]uint64 `yaml:"initialBlock,omitempty" json:"initialBlock,omitempty"`
Params map[string]string `yaml:"params,omitempty" json:"params,omitempty"`
}
type Sink struct {
Type string `yaml:"type"`
Module string `yaml:"module"`
Config interface{} `yaml:"config"`
}
var httpSchemePrefixRegex = regexp.MustCompile("^https?://")
func (m *Manifest) resolvePath(path string) string {
if m.Workdir == "" || filepath.IsAbs(path) || httpSchemePrefixRegex.MatchString(path) {
return path
}
return filepath.Join(m.Workdir, path)
}
type PackageMeta struct {
Name string `yaml:"name"`
Version string `yaml:"version"` // Semver for package authors
URL string `yaml:"url"`
Doc string `yaml:"doc"`
Image string `yaml:"image"`
}
type Protobuf struct {
Files []string `yaml:"files"`
ImportPaths []string `yaml:"importPaths"`
}
type Module struct {
Name string `yaml:"name"`
Doc string `yaml:"doc"`
Kind string `yaml:"kind"`
InitialBlock *uint64 `yaml:"initialBlock"`
BlockFilter *BlockFilter `yaml:"blockFilter"`
UpdatePolicy string `yaml:"updatePolicy"`
ValueType string `yaml:"valueType"`
Binary string `yaml:"binary"`
Inputs []*Input `yaml:"inputs"`
Output StreamOutput `yaml:"output"`
Use string `yaml:"use"`
}
type BlockFilter struct {
Module string `yaml:"module"`
Query string `yaml:"query"`
}
type Input struct {
Source string `yaml:"source"`
Store string `yaml:"store"`
Map string `yaml:"map"`
Params string `yaml:"params"`
Mode string `yaml:"mode"`
}
type Binary struct {
File string `yaml:"file"`
Type string `yaml:"type"`
Native string `yaml:"native"`
Content []byte `yaml:"-"`
Entrypoint string `yaml:"entrypoint"`
ProtoPackageMapping map[string]string `yaml:"protoPackageMapping"`
}
type StreamOutput struct {
// For 'map'
Type string `yaml:"type"`
}
func decodeYamlManifestFromFile(yamlFilePath, workingDir string) (out *Manifest, err error) {
//if yamlFilePath is a relative path, make it absolute
if !filepath.IsAbs(yamlFilePath) {
yamlFilePath = filepath.Join(workingDir, yamlFilePath)
}
cnt, err := os.ReadFile(yamlFilePath)
if err != nil {
return nil, fmt.Errorf("reading substreams manifest %q: %w", yamlFilePath, err)
}
decoder := yaml.NewDecoder(bytes.NewReader(cnt))
decoder.KnownFields(true)
if err := decoder.Decode(&out); err != nil {
return nil, fmt.Errorf("decoding manifest content: %w", err)
}
return
}
func (i *Input) IsMap() bool {
return i.Map != "" && i.Store == "" && i.Source == "" && i.Params == ""
}
func (i *Input) IsStore() bool {
return i.Store != "" && i.Map == "" && i.Source == "" && i.Params == ""
}
func (i *Input) IsSource() bool {
return i.Source != "" && i.Map == "" && i.Store == "" && i.Params == ""
}
func (i *Input) IsParams() bool {
return i.Params != "" && i.Source == "" && i.Map == "" && i.Store == ""
}
func (i *Input) parse() error {
if i.IsMap() {
//i.Name = fmt.Sprintf("map:%s", i.Map)
return nil
}
if i.IsStore() {
if i.Mode == "" {
i.Mode = "get"
}
if i.Mode != "get" && i.Mode != "deltas" {
return fmt.Errorf("input store %q: 'mode' parameter must be one of: 'get', 'deltas'", i.Store)
}
return nil
}
if i.IsSource() {
return nil
}
if i.IsParams() {
if i.Params != "string" {
return fmt.Errorf("input 'params': 'string' is the only acceptable value here; specify the parameter's value under the top-level 'params' mapping")
}
return nil
}
return fmt.Errorf("input has an unknown or mixed types; expect one, and only one of: 'params', 'map', 'store' or 'source'")
}
func validateModuleWithUse(module *Module) error {
if module.Output.Type != "" {
return fmt.Errorf("module %q: 'output.type' cannot be set when 'use' is set", module.Name)
}
if module.UpdatePolicy != "" {
return fmt.Errorf("module %q: 'output.updatePolicy' cannot be set when 'use' is set", module.Name)
}
if module.Binary != "" {
return fmt.Errorf("module %q: 'binary' cannot be set when 'use' is set", module.Name)
}
if module.ValueType != "" {
return fmt.Errorf("module %q: 'valueType' cannot be set when 'use' is set", module.Name)
}
return nil
}
func validateStoreBuilder(module *Module) error {
if module.UpdatePolicy == "" {
return errors.New("missing 'output.updatePolicy' for kind 'store'")
}
if module.ValueType == "" {
return errors.New("missing 'output.valueType' for kind 'store'")
}
// keep big float to be backward-compatible
combinations := []string{
"max:bigint",
"max:int64",
"max:bigdecimal",
"max:bigfloat",
"max:float64",
"min:bigint",
"min:int64",
"min:bigdecimal",
"min:bigfloat",
"min:float64",
"add:bigint",
"add:int64",
"add:bigdecimal",
"add:bigfloat",
"add:float64",
"set:bytes",
"set:string",
"set:proto",
"set:bigdecimal",
"set:bigfloat",
"set:bigint",
"set:int64",
"set:float64",
"set_if_not_exists:bytes",
"set_if_not_exists:string",
"set_if_not_exists:proto",
"set_if_not_exists:bigdecimal",
"set_if_not_exists:bigfloat",
"set_if_not_exists:bigint",
"set_if_not_exists:int64",
"set_if_not_exists:float64",
"append:bytes",
"append:string",
}
found := false
var lastCombination string
for _, comb := range combinations {
valType := module.ValueType
if strings.HasPrefix(valType, "proto:") {
valType = "proto"
}
lastCombination = fmt.Sprintf("%s:%s", module.UpdatePolicy, valType)
if lastCombination == comb {
found = true
}
}
if !found {
return fmt.Errorf("invalid 'output.updatePolicy' and 'output.valueType' combination, found %q use one of: %s", lastCombination, combinations)
}
return nil
}
func (m *Module) String() string {
return m.Name
}
func (m *Module) ToProtoWASM(codeIndex uint32) (*pbsubstreams.Module, error) {
out := &pbsubstreams.Module{
Name: m.Name,
BinaryIndex: codeIndex,
BinaryEntrypoint: m.Name,
}
out.InitialBlock = UNSET
if m.InitialBlock != nil {
out.InitialBlock = *m.InitialBlock
}
m.setOutputToProto(out)
m.setKindToProto(out)
m.setBlockFilterToProto(out)
err := m.setInputsToProto(out)
if err != nil {
return nil, fmt.Errorf("setting input for module, %s: %w", m.Name, err)
}
return out, nil
}
func (m *Module) setBlockFilterToProto(pbModule *pbsubstreams.Module) {
if m.BlockFilter != nil {
pbModule.BlockFilter = &pbsubstreams.Module_BlockFilter{
Module: m.BlockFilter.Module,
Query: m.BlockFilter.Query,
}
}
}
func (m *Module) setInputsToProto(pbModule *pbsubstreams.Module) error {
for i, input := range m.Inputs {
if input.Source != "" {
pbInput := &pbsubstreams.Module_Input{
Input: &pbsubstreams.Module_Input_Source_{
Source: &pbsubstreams.Module_Input_Source{
Type: input.Source,
},
},
}
pbModule.Inputs = append(pbModule.Inputs, pbInput)
continue
}
if input.Map != "" {
pbInput := &pbsubstreams.Module_Input{
Input: &pbsubstreams.Module_Input_Map_{
Map: &pbsubstreams.Module_Input_Map{
ModuleName: input.Map,
},
},
}
pbModule.Inputs = append(pbModule.Inputs, pbInput)
continue
}
if input.Store != "" {
var mode pbsubstreams.Module_Input_Store_Mode
switch input.Mode {
case "":
mode = pbsubstreams.Module_Input_Store_UNSET
case "get":
mode = pbsubstreams.Module_Input_Store_GET
case "deltas":
mode = pbsubstreams.Module_Input_Store_DELTAS
default:
panic(fmt.Sprintf("invalid input mode %s", input.Mode))
}
pbInput := &pbsubstreams.Module_Input{
Input: &pbsubstreams.Module_Input_Store_{
Store: &pbsubstreams.Module_Input_Store{
ModuleName: input.Store,
Mode: mode,
},
},
}
pbModule.Inputs = append(pbModule.Inputs, pbInput)
continue
}
if input.Params != "" {
if i != 0 {
return fmt.Errorf("input.params must be the first input")
}
pbInput := &pbsubstreams.Module_Input{
Input: &pbsubstreams.Module_Input_Params_{
Params: &pbsubstreams.Module_Input_Params{
Value: "",
},
},
}
pbModule.Inputs = append(pbModule.Inputs, pbInput)
continue
}
return fmt.Errorf("invalid input")
}
return nil
}
const (
OutputValueTypeInt64 = "int64"
OutputValueTypeFloat64 = "float64"
OutputValueTypeBigInt = "bigint"
OutputValueTypeBigDecimal = "bigdecimal"
// Deprecated: bigfloat value type replaced with bigdecimal
OutputValueTypeBigFloat = "bigfloat"
OutputValueTypeString = "string"
)
const (
UpdatePolicySet = "set"
UpdatePolicySetIfNotExists = "set_if_not_exists"
UpdatePolicyAdd = "add"
UpdatePolicyMax = "max"
UpdatePolicyMin = "min"
UpdatePolicyAppend = "append"
)
func (m *Module) setKindToProto(pbModule *pbsubstreams.Module) {
switch m.Kind {
case ModuleKindMap:
pbModule.Kind = &pbsubstreams.Module_KindMap_{
KindMap: &pbsubstreams.Module_KindMap{
OutputType: m.Output.Type,
},
}
case ModuleKindStore:
var updatePolicy pbsubstreams.Module_KindStore_UpdatePolicy
switch m.UpdatePolicy {
case UpdatePolicySet:
updatePolicy = pbsubstreams.Module_KindStore_UPDATE_POLICY_SET
case UpdatePolicySetIfNotExists:
updatePolicy = pbsubstreams.Module_KindStore_UPDATE_POLICY_SET_IF_NOT_EXISTS
case UpdatePolicyAdd:
updatePolicy = pbsubstreams.Module_KindStore_UPDATE_POLICY_ADD
case UpdatePolicyMax:
updatePolicy = pbsubstreams.Module_KindStore_UPDATE_POLICY_MAX
case UpdatePolicyMin:
updatePolicy = pbsubstreams.Module_KindStore_UPDATE_POLICY_MIN
case UpdatePolicyAppend:
updatePolicy = pbsubstreams.Module_KindStore_UPDATE_POLICY_APPEND
default:
panic(fmt.Sprintf("invalid update policy %s", m.UpdatePolicy))
}
pbModule.Kind = &pbsubstreams.Module_KindStore_{
KindStore: &pbsubstreams.Module_KindStore{
UpdatePolicy: updatePolicy,
ValueType: m.ValueType,
},
}
}
}
func (m *Module) setOutputToProto(pbModule *pbsubstreams.Module) {
if m.Output.Type != "" {
pbModule.Output = &pbsubstreams.Module_Output{
Type: m.Output.Type,
}
}
}