This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
projection.go
335 lines (291 loc) · 9.55 KB
/
projection.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
package sqlsol
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"github.com/hyperledger/burrow/vent/types"
"github.com/pkg/errors"
"github.com/xeipuuv/gojsonschema"
)
// Projection contains EventTable, Event & Abi specifications
type Projection struct {
Tables types.EventTables
EventSpec types.EventSpec
}
// NewProjectionFromBytes creates a Projection from a stream of bytes
func NewProjectionFromBytes(bs []byte) (*Projection, error) {
eventSpec := types.EventSpec{}
err := ValidateJSONEventSpec(bs)
if err != nil {
return nil, err
}
err = json.Unmarshal(bs, &eventSpec)
if err != nil {
return nil, errors.Wrap(err, "Error unmarshalling eventSpec")
}
return NewProjectionFromEventSpec(eventSpec)
}
// NewProjectionFromFolder creates a Projection from a folder containing spec files
func NewProjectionFromFolder(specFileOrDirs ...string) (*Projection, error) {
eventSpec := types.EventSpec{}
const errHeader = "NewProjectionFromFolder():"
for _, dir := range specFileOrDirs {
err := filepath.Walk(dir, func(path string, _ os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("error walking event spec files location '%s': %v", dir, err)
}
if filepath.Ext(path) == ".json" {
bs, err := readFile(path)
if err != nil {
return fmt.Errorf("error reading spec file '%s': %v", path, err)
}
err = ValidateJSONEventSpec(bs)
if err != nil {
return fmt.Errorf("could not validate spec file '%s': %v", path, err)
}
fileEventSpec := types.EventSpec{}
err = json.Unmarshal(bs, &fileEventSpec)
if err != nil {
return fmt.Errorf("error reading spec file '%s': %v", path, err)
}
eventSpec = append(eventSpec, fileEventSpec...)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("%s %v", errHeader, err)
}
}
return NewProjectionFromEventSpec(eventSpec)
}
// NewProjectionFromEventSpec receives a sqlsol event specification
// and returns a pointer to a filled projection structure
// that contains event types mapped to SQL column types
// and Event tables structures with table and columns info
func NewProjectionFromEventSpec(eventSpec types.EventSpec) (*Projection, error) {
// builds abi information from specification
tables := make(types.EventTables)
// obtain global field mappings to add to table definitions
globalFieldMappings := getGlobalFieldMappings()
for _, eventClass := range eventSpec {
// validate json structure
if err := eventClass.Validate(); err != nil {
return nil, fmt.Errorf("validation error on %v: %v", eventClass, err)
}
// build columns mapping
var columns []*types.SQLTableColumn
channels := make(map[string][]string)
// Add the global mappings
eventClass.FieldMappings = append(globalFieldMappings, eventClass.FieldMappings...)
i := 0
for _, mapping := range eventClass.FieldMappings {
sqlType, sqlTypeLength, err := getSQLType(mapping.Type, mapping.BytesToString)
if err != nil {
return nil, err
}
i++
// Update channels broadcast payload subsets with this column
for _, channel := range mapping.Notify {
channels[channel] = append(channels[channel], mapping.ColumnName)
}
columns = append(columns, &types.SQLTableColumn{
Name: mapping.ColumnName,
Type: sqlType,
Primary: mapping.Primary,
Length: sqlTypeLength,
})
}
// Allow for compatible composition of tables
var err error
tables[eventClass.TableName], err = mergeTables(tables[eventClass.TableName],
&types.SQLTable{
Name: eventClass.TableName,
NotifyChannels: channels,
Columns: columns,
})
if err != nil {
return nil, err
}
}
// check if there are duplicated duplicated column names (for a given table)
colName := make(map[string]int)
for _, table := range tables {
for _, column := range table.Columns {
colName[table.Name+column.Name]++
if colName[table.Name+column.Name] > 1 {
return nil, fmt.Errorf("duplicated column name: '%s' in table '%s'", column.Name, table.Name)
}
}
}
return &Projection{
Tables: tables,
EventSpec: eventSpec,
}, nil
}
// Get the column for a particular table and column name
func (p *Projection) GetColumn(tableName, columnName string) (*types.SQLTableColumn, error) {
if table, ok := p.Tables[tableName]; ok {
column := table.GetColumn(columnName)
if column == nil {
return nil, fmt.Errorf("GetColumn: table '%s' has no column '%s'",
tableName, columnName)
}
return column, nil
}
return nil, fmt.Errorf("GetColumn: table does not exist projection: %s ", tableName)
}
func ValidateJSONEventSpec(bs []byte) error {
schemaLoader := gojsonschema.NewGoLoader(types.EventSpecSchema())
specLoader := gojsonschema.NewBytesLoader(bs)
result, err := gojsonschema.Validate(schemaLoader, specLoader)
if err != nil {
return fmt.Errorf("could not validate using JSONSchema: %v", err)
}
if !result.Valid() {
errs := make([]string, len(result.Errors()))
for i, err := range result.Errors() {
errs[i] = err.String()
}
return fmt.Errorf("EventSpec failed JSONSchema validation:\n%s", strings.Join(errs, "\n"))
}
return nil
}
// readFile opens a given file and reads it contents into a stream of bytes
func readFile(file string) ([]byte, error) {
theFile, err := os.Open(file)
if err != nil {
return nil, err
}
defer theFile.Close()
byteValue, err := ioutil.ReadAll(theFile)
if err != nil {
return nil, err
}
return byteValue, nil
}
// getSQLType maps event input types with corresponding SQL column types
// takes into account related solidity types info and element indexed or hashed
func getSQLType(evmSignature string, bytesToString bool) (types.SQLColumnType, int, error) {
evmSignature = strings.ToLower(evmSignature)
re := regexp.MustCompile("[0-9]+")
typeSize, _ := strconv.Atoi(re.FindString(evmSignature))
switch {
// solidity address => sql varchar
case evmSignature == types.EventFieldTypeAddress:
return types.SQLColumnTypeVarchar, 40, nil
// solidity bool => sql bool
case evmSignature == types.EventFieldTypeBool:
return types.SQLColumnTypeBool, 0, nil
// solidity bytes => sql bytes
// bytesToString == true means there is a string in there so => sql varchar
case strings.HasPrefix(evmSignature, types.EventFieldTypeBytes):
if bytesToString {
return types.SQLColumnTypeVarchar, 40, nil
} else {
return types.SQLColumnTypeByteA, 0, nil
}
// solidity string => sql text
case evmSignature == types.EventFieldTypeString:
return types.SQLColumnTypeText, 0, nil
// solidity int or int256 => sql bigint
// solidity int <= 32 => sql int
// solidity int > 32 => sql numeric
case strings.HasPrefix(evmSignature, types.EventFieldTypeInt):
if typeSize == 0 || typeSize == 256 {
return types.SQLColumnTypeBigInt, 0, nil
}
if typeSize <= 32 {
return types.SQLColumnTypeInt, 0, nil
} else {
return types.SQLColumnTypeNumeric, 0, nil
}
// solidity uint or uint256 => sql bigint
// solidity uint <= 16 => sql int
// solidity uint > 16 => sql numeric
case strings.HasPrefix(evmSignature, types.EventFieldTypeUInt):
if typeSize == 0 || typeSize == 256 {
return types.SQLColumnTypeBigInt, 0, nil
}
if typeSize <= 16 {
return types.SQLColumnTypeInt, 0, nil
} else {
return types.SQLColumnTypeNumeric, 0, nil
}
default:
return -1, 0, fmt.Errorf("Don't know how to map evmSignature: %s ", evmSignature)
}
}
// getGlobalColumns returns global columns for event table structures,
// these columns will be part of every SQL event table to relate data with source events
func getGlobalFieldMappings() []*types.EventFieldMapping {
return []*types.EventFieldMapping{
{
ColumnName: types.SQLColumnLabelHeight,
Field: types.BlockHeightLabel,
Type: types.EventFieldTypeString,
},
{
ColumnName: types.SQLColumnLabelTxHash,
Field: types.TxTxHashLabel,
Type: types.EventFieldTypeString,
},
{
ColumnName: types.SQLColumnLabelEventType,
Field: types.EventTypeLabel,
Type: types.EventFieldTypeString,
},
{
ColumnName: types.SQLColumnLabelEventName,
Field: types.EventNameLabel,
Type: types.EventFieldTypeString,
},
}
}
// Merges tables a and b provided the intersection of their columns (by name) are identical
func mergeTables(tables ...*types.SQLTable) (*types.SQLTable, error) {
table := &types.SQLTable{
NotifyChannels: make(map[string][]string),
}
columns := make(map[string]*types.SQLTableColumn)
notifications := make(map[string]map[string]struct{})
for _, t := range tables {
if t != nil {
table.Name = t.Name
for _, columnB := range t.Columns {
if columnA, ok := columns[columnB.Name]; ok {
if !columnA.Equals(columnB) {
return nil, fmt.Errorf("cannot merge event class tables for %s because of "+
"conflicting columns: %v and %v", t.Name, columnB, columnB)
}
// Just keep existing column from A - they match
} else {
// Add as new column
table.Columns = append(table.Columns, columnB)
columns[columnB.Name] = columnB
}
}
for channel, columnNames := range t.NotifyChannels {
for _, columnName := range columnNames {
if notifications[channel] == nil {
notifications[channel] = make(map[string]struct{})
}
notifications[channel][columnName] = struct{}{}
}
}
}
}
// Merge notification channels requested by specs
for channel, colMap := range notifications {
for columnName := range colMap {
table.NotifyChannels[channel] = append(table.NotifyChannels[channel], columnName)
}
sort.Strings(table.NotifyChannels[channel])
}
return table, nil
}