forked from ferranbt/fastssz
/
tag_parser.go
221 lines (202 loc) · 5.07 KB
/
tag_parser.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
package generator
import (
"errors"
"fmt"
"strconv"
"strings"
"text/scanner"
)
type tokenState int
const (
tsBegin tokenState = iota
tsLabel
tsValue
tsCloseTick
)
func GetSSZTags(tag string) (map[string]string, error) {
var lastErr error
accumulateError := func(_ *scanner.Scanner, msg string) {
lastErr = errors.New(msg)
}
sr := strings.NewReader(tag)
sc := scanner.Scanner{}
sc.Init(sr)
sc.Filename = "tag"
sc.Mode ^= scanner.ScanRawStrings
sc.Error = accumulateError
var labelStr string
var state tokenState
tags := make(map[string]string)
for tok := sc.Scan(); tok != scanner.EOF; tok = sc.Scan() {
if lastErr != nil {
return nil, fmt.Errorf("GetSSZTags failed: token scanner error = %s", lastErr)
}
if state == tsCloseTick {
return nil, errors.New("GetSSZTags failed: undefined behavior when scanning beyond the end of the tag")
}
txt := sc.TokenText()
switch txt {
case "`":
if state == tsLabel {
state = tsCloseTick
continue
}
if state == tsBegin {
state = tsLabel
continue
}
case ":":
if state == tsLabel {
state = tsValue
continue
}
case "\"":
continue
default:
if state == tsValue {
tags[labelStr] = trimTagQuotes(txt)
state = tsLabel
labelStr = ""
continue
}
if state == tsLabel {
labelStr += txt
continue
}
}
}
return tags, nil
}
// cannot compare untyped nil to typed nil
// this value gives us a nil with type of *int
// to compare to ssz-size = '?' values
var nilInt *int
// handle tag structured like 'ssz:"bitlist"'
// this is not used in prysm but needs to be supported for fastssz tests
func isBitList(tags map[string]string) bool {
for k, v := range tags {
if k == "ssz" {
parts := strings.Split(v, ",")
for _, p := range parts {
if p == "bitlist" {
return true
}
}
}
}
return false
}
func extractSSZDimensions(tag string) ([]*SSZDimension, error) {
// parse the ssz-max and ssz-size key/value pairs out of the tag
tags, err := GetSSZTags(tag)
if err != nil {
return nil, err
}
sszSizes, sizeDefined := tags["ssz-size"]
sszMax, maxDefined := tags["ssz-max"]
if !sizeDefined && !maxDefined {
return nil, fmt.Errorf("no ssz-size or ssz-max tags found for element. tag=%s", tag)
}
// split each tag by ",". each position in the csv represents a dimension of an n-dimensional array
sizeSplit := strings.Split(sszSizes, ",")
maxSplit := strings.Split(sszMax, ",")
// find the largest of the two dimensions. for backward compat we'll be permissive and let them be uneven
ndims := len(sizeSplit)
if len(maxSplit) > len(sizeSplit) {
ndims = len(maxSplit)
}
dims := make([]*SSZDimension, ndims)
for i := 0; i < ndims; i++ {
isbl := false
// bitlist can only be the inner-most element by definition
if i == ndims-1 {
if isBitList(tags) {
isbl = true
}
}
var szi, mxi string
if len(sizeSplit) > i {
szi = sizeSplit[i]
}
if len(maxSplit) > i {
mxi = maxSplit[i]
}
if szi == "?" && mxi == "?" {
return nil, fmt.Errorf("at dimension %d both ssz-size and ssz-max had a '?' value. For each dimension, either ssz-size or ssz-max must have a value. Ex: 'ssz-size:\"?,32\" ssz-max:\"100\" defines a List with 100 element limit, containing 32 byte fixed-sized vectors. tag=%s", i, tag)
}
switch szi {
case "?", "":
if mxi == "?" || mxi == "" {
return nil, fmt.Errorf("no numeric ssz-size or ssz-max tag for value at dimesion %d, tag=%s", i, tag)
}
m, err := strconv.Atoi(mxi)
if err != nil {
return nil, fmt.Errorf("atoi failed on value %s for ssz-max at dimension %d, tag=%s. err=%s", mxi, i, tag, err)
}
dims[i] = &SSZDimension{
isBitlist: isbl,
ListLength: &m,
}
default: // szi is not empty or "?"
s, err := strconv.Atoi(szi)
if err != nil {
return nil, fmt.Errorf("atoi failed on value %s for ssz-size at dimension %d, tag=%s. err=%s", szi, i, tag, err)
}
dims[i] = &SSZDimension{
isBitlist: isbl,
VectorLength: &s,
}
continue
}
}
return dims, nil
}
type SSZDimension struct {
VectorLength *int
ListLength *int
isBitlist bool
}
func (dim *SSZDimension) IsVector() bool {
return dim.VectorLength != nilInt
}
func (dim *SSZDimension) IsList() bool {
return dim.ListLength != nilInt
}
func (dim *SSZDimension) IsBitlist() bool {
return dim.isBitlist
}
func (dim *SSZDimension) ListLen() int {
return *dim.ListLength
}
func (dim *SSZDimension) VectorLen() int {
return *dim.VectorLength
}
// ValueType returns a Type enum to be used in the construction of a fastssz Value type
func (dim *SSZDimension) ValueType() Type {
if dim.IsVector() {
return TypeVector
}
if dim.IsList() {
return TypeList
}
return TypeUndefined
}
// ValueType returns ssz-max or ssz-size, to be used in the construction of a fastssz Value type
func (dim *SSZDimension) ValueLen() uint64 {
if dim.IsList() {
return uint64(dim.ListLen())
}
if dim.IsVector() {
return uint64(dim.VectorLen())
}
return 0
}
func trimTagQuotes(s string) string {
if len(s) > 0 && s[0] == '"' {
s = s[1:]
}
if len(s) > 0 && s[len(s)-1] == '"' {
s = s[:len(s)-1]
}
return s
}