-
Notifications
You must be signed in to change notification settings - Fork 206
/
spec.go
188 lines (169 loc) · 6.45 KB
/
spec.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
package types
import (
fmt "fmt"
"sort"
"strconv"
"strings"
"unicode"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
)
const (
minCU = 1
ContributorPrecision = 100000
maxContributorsPercentageStr = "0.8"
)
func (spec Spec) ValidateSpec(maxCU uint64) (map[string]string, error) {
details := map[string]string{"spec": spec.Name, "status": strconv.FormatBool(spec.Enabled), "chainID": spec.Index}
functionTags := map[FUNCTION_TAG]bool{}
availableAPIInterface := map[string]struct{}{
APIInterfaceJsonRPC: {},
APIInterfaceTendermintRPC: {},
APIInterfaceRest: {},
APIInterfaceGrpc: {},
}
availavleEncodings := map[string]struct{}{
EncodingBase64: {},
EncodingHex: {},
}
for _, char := range spec.Name {
if !unicode.IsLower(char) && char != ' ' {
return details, fmt.Errorf("spec name must contain lowercase characters only")
}
}
if spec.ReliabilityThreshold == 0 {
return details, fmt.Errorf("ReliabilityThreshold can't be zero")
}
if len(spec.Contributor) > 0 {
for _, contributorAddr := range spec.Contributor {
_, err := sdk.AccAddressFromBech32(contributorAddr)
if err != nil {
return details, fmt.Errorf("spec contributor is not a valid account address %s in list: %s", contributorAddr, strings.Join(spec.Contributor, ","))
}
}
}
if spec.ContributorPercentage != nil && (spec.ContributorPercentage.GT(math.LegacyMustNewDecFromStr(maxContributorsPercentageStr)) || (spec.ContributorPercentage.LT(math.LegacyMustNewDecFromStr(strconv.FormatFloat(1.0/ContributorPrecision, 'f', -1, 64))))) {
return details, fmt.Errorf("spec contributor percentage must be in the range [%s - %s]", math.LegacyMustNewDecFromStr(strconv.FormatFloat(1.0/ContributorPrecision, 'f', -1, 64)).String(), maxContributorsPercentageStr)
}
if spec.BlocksInFinalizationProof == 0 {
return details, fmt.Errorf("BlocksInFinalizationProof can't be zero")
}
if spec.AverageBlockTime <= 0 {
return details, fmt.Errorf("AverageBlockTime can't be zero")
}
if spec.AllowedBlockLagForQosSync <= 0 {
return details, fmt.Errorf("AllowedBlockLagForQosSync can't be zero")
}
if spec.MinStakeProvider.Denom != epochstoragetypes.TokenDenom || spec.MinStakeProvider.Amount.IsZero() {
return details, fmt.Errorf("MinStakeProvider can't be zero and must have denom of ulava")
}
for _, apiCollection := range spec.ApiCollections {
if len(apiCollection.Apis) == 0 {
return details, fmt.Errorf("api apiCollection list empty for %v", apiCollection.CollectionData)
}
if _, ok := availableAPIInterface[apiCollection.CollectionData.ApiInterface]; !ok {
// if the apiCollection is disabled and has an empty apiInterface it's allowed since this is a base collection for expand
if apiCollection.CollectionData.ApiInterface != "" || apiCollection.Enabled {
return details, fmt.Errorf("unsupported api interface %v", apiCollection.CollectionData.ApiInterface)
}
}
// validate function tags
for _, parsing := range apiCollection.ParseDirectives {
// Validate function tag
if parsing.FunctionTag == FUNCTION_TAG_DISABLED {
details["apiCollection"] = fmt.Sprintf("%v", apiCollection.CollectionData)
return details, fmt.Errorf("empty or unsupported function tag %s", parsing.FunctionTag)
}
functionTags[parsing.FunctionTag] = true
if parsing.ResultParsing.Encoding != "" {
if _, ok := availavleEncodings[parsing.ResultParsing.Encoding]; !ok {
return details, fmt.Errorf("unsupported api encoding %s in apiCollection %v ", parsing.ResultParsing.Encoding, apiCollection.CollectionData)
}
}
}
currentApis := map[string]struct{}{}
// validate apis
for _, api := range apiCollection.Apis {
_, ok := currentApis[api.Name]
if ok {
details["api"] = api.Name
return details, fmt.Errorf("api defined twice %s", api.Name)
}
currentApis[api.Name] = struct{}{}
if api.ComputeUnits < minCU || api.ComputeUnits > maxCU {
details["api"] = api.Name
return details, fmt.Errorf("compute units out or range %s", api.Name)
}
}
currentHeaders := map[string]struct{}{}
for _, header := range apiCollection.Headers {
_, ok := currentHeaders[header.Name]
if ok {
details["header"] = header.Name
return details, fmt.Errorf("header defined twice %s", header.Name)
}
currentHeaders[header.Name] = struct{}{}
if strings.ToLower(header.Name) != header.Name {
details["header"] = header.Name
return details, fmt.Errorf("header names must be lower case %s", header.Name)
}
}
}
if spec.DataReliabilityEnabled && spec.Enabled {
for _, tag := range []FUNCTION_TAG{FUNCTION_TAG_GET_BLOCKNUM, FUNCTION_TAG_GET_BLOCK_BY_NUM} {
if found := functionTags[tag]; !found {
return details, fmt.Errorf("missing tagged functions for hash comparison: %s", tag)
}
}
}
return details, nil
}
func (spec *Spec) CombineCollections(parentsCollections map[CollectionData][]*ApiCollection) error {
collectionDataList := make([]CollectionData, 0)
// Populate the keys slice with the map keys
for key := range parentsCollections {
collectionDataList = append(collectionDataList, key)
}
// sort the slice so the order is deterministic
sort.Slice(collectionDataList, func(i, j int) bool {
return collectionDataList[i].String() < collectionDataList[j].String()
})
for _, collectionData := range collectionDataList {
collectionsToCombine := parentsCollections[collectionData]
if len(collectionsToCombine) == 0 {
return fmt.Errorf("collection with length 0 %v", collectionData)
}
var combined *ApiCollection
var others []*ApiCollection
for i := 0; i < len(collectionsToCombine); i++ {
combined = collectionsToCombine[i]
others = collectionsToCombine[:i]
others = append(others, collectionsToCombine[i+1:]...)
if combined.Enabled {
break
}
}
if !combined.Enabled {
// no collections enabled to combine, we skip this
continue
}
err := combined.CombineWithOthers(others, false, false)
if err != nil {
return err
}
spec.ApiCollections = append(spec.ApiCollections, combined)
}
return nil
}
func (spec *Spec) ServicesMap() (addons, extensions map[string]struct{}) {
addons = map[string]struct{}{}
extensions = map[string]struct{}{}
for _, apiCollection := range spec.ApiCollections {
addons[apiCollection.CollectionData.AddOn] = struct{}{}
for _, extension := range apiCollection.Extensions {
extensions[extension.Name] = struct{}{}
}
}
return
}