forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
537 lines (450 loc) · 17.1 KB
/
table.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}
// Init create tables for tests
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
// Create table one
err := createTableOne(stub)
if err != nil {
return nil, fmt.Errorf("Error creating table one during init. %s", err)
}
// Create table two
err = createTableTwo(stub)
if err != nil {
return nil, fmt.Errorf("Error creating table two during init. %s", err)
}
// Create table three
err = createTableThree(stub)
if err != nil {
return nil, fmt.Errorf("Error creating table three during init. %s", err)
}
// Create table four
err = createTableFour(stub)
if err != nil {
return nil, fmt.Errorf("Error creating table four during init. %s", err)
}
return nil, nil
}
// Invoke callback representing the invocation of a chaincode
// This chaincode will manage two accounts A and B and will transfer X units from A to B upon invoke
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
switch function {
case "insertRowTableOne":
if len(args) < 3 {
return nil, errors.New("insertTableOne failed. Must include 3 column values")
}
col1Val := args[0]
col2Int, err := strconv.ParseInt(args[1], 10, 32)
if err != nil {
return nil, errors.New("insertTableOne failed. arg[1] must be convertable to int32")
}
col2Val := int32(col2Int)
col3Int, err := strconv.ParseInt(args[2], 10, 32)
if err != nil {
return nil, errors.New("insertTableOne failed. arg[2] must be convertable to int32")
}
col3Val := int32(col3Int)
var columns []*shim.Column
col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
col2 := shim.Column{Value: &shim.Column_Int32{Int32: col2Val}}
col3 := shim.Column{Value: &shim.Column_Int32{Int32: col3Val}}
columns = append(columns, &col1)
columns = append(columns, &col2)
columns = append(columns, &col3)
row := shim.Row{Columns: columns}
ok, err := stub.InsertRow("tableOne", row)
if err != nil {
return nil, fmt.Errorf("insertTableOne operation failed. %s", err)
}
if !ok {
return nil, errors.New("insertTableOne operation failed. Row with given key already exists")
}
case "insertRowTableTwo":
if len(args) < 4 {
return nil, errors.New("insertRowTableTwo failed. Must include 4 column values")
}
col1Val := args[0]
col2Int, err := strconv.ParseInt(args[1], 10, 32)
if err != nil {
return nil, errors.New("insertRowTableTwo failed. arg[1] must be convertable to int32")
}
col2Val := int32(col2Int)
col3Int, err := strconv.ParseInt(args[2], 10, 32)
if err != nil {
return nil, errors.New("insertRowTableTwo failed. arg[2] must be convertable to int32")
}
col3Val := int32(col3Int)
col4Val := args[3]
var columns []*shim.Column
col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
col2 := shim.Column{Value: &shim.Column_Int32{Int32: col2Val}}
col3 := shim.Column{Value: &shim.Column_Int32{Int32: col3Val}}
col4 := shim.Column{Value: &shim.Column_String_{String_: col4Val}}
columns = append(columns, &col1)
columns = append(columns, &col2)
columns = append(columns, &col3)
columns = append(columns, &col4)
row := shim.Row{Columns: columns}
ok, err := stub.InsertRow("tableTwo", row)
if err != nil {
return nil, fmt.Errorf("insertRowTableTwo operation failed. %s", err)
}
if !ok {
return nil, errors.New("insertRowTableTwo operation failed. Row with given key already exists")
}
case "insertRowTableThree":
if len(args) < 7 {
return nil, errors.New("insertRowTableThree failed. Must include 7 column values")
}
col1Val := args[0]
col2Int, err := strconv.ParseInt(args[1], 10, 32)
if err != nil {
return nil, errors.New("insertRowTableThree failed. arg[1] must be convertable to int32")
}
col2Val := int32(col2Int)
col3Val, err := strconv.ParseInt(args[2], 10, 64)
if err != nil {
return nil, errors.New("insertRowTableThree failed. arg[2] must be convertable to int64")
}
col4Uint, err := strconv.ParseUint(args[3], 10, 32)
if err != nil {
return nil, errors.New("insertRowTableThree failed. arg[3] must be convertable to uint32")
}
col4Val := uint32(col4Uint)
col5Val, err := strconv.ParseUint(args[4], 10, 64)
if err != nil {
return nil, errors.New("insertRowTableThree failed. arg[4] must be convertable to uint64")
}
col6Val := []byte(args[5])
col7Val, err := strconv.ParseBool(args[6])
if err != nil {
return nil, errors.New("insertRowTableThree failed. arg[6] must be convertable to bool")
}
var columns []*shim.Column
col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
col2 := shim.Column{Value: &shim.Column_Int32{Int32: col2Val}}
col3 := shim.Column{Value: &shim.Column_Int64{Int64: col3Val}}
col4 := shim.Column{Value: &shim.Column_Uint32{Uint32: col4Val}}
col5 := shim.Column{Value: &shim.Column_Uint64{Uint64: col5Val}}
col6 := shim.Column{Value: &shim.Column_Bytes{Bytes: col6Val}}
col7 := shim.Column{Value: &shim.Column_Bool{Bool: col7Val}}
columns = append(columns, &col1)
columns = append(columns, &col2)
columns = append(columns, &col3)
columns = append(columns, &col4)
columns = append(columns, &col5)
columns = append(columns, &col6)
columns = append(columns, &col7)
row := shim.Row{Columns: columns}
ok, err := stub.InsertRow("tableThree", row)
if err != nil {
return nil, fmt.Errorf("insertRowTableThree operation failed. %s", err)
}
if !ok {
return nil, errors.New("insertRowTableThree operation failed. Row with given key already exists")
}
case "insertRowTableFour":
if len(args) < 1 {
return nil, errors.New("insertRowTableFour failed. Must include 1 column value1")
}
col1Val := args[0]
var columns []*shim.Column
col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
columns = append(columns, &col1)
row := shim.Row{Columns: columns}
ok, err := stub.InsertRow("tableFour", row)
if err != nil {
return nil, fmt.Errorf("insertRowTableFour operation failed. %s", err)
}
if !ok {
return nil, errors.New("insertRowTableFour operation failed. Row with given key already exists")
}
case "deleteRowTableOne":
if len(args) < 1 {
return nil, errors.New("deleteRowTableOne failed. Must include 1 key value")
}
col1Val := args[0]
var columns []shim.Column
col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
columns = append(columns, col1)
err := stub.DeleteRow("tableOne", columns)
if err != nil {
return nil, fmt.Errorf("deleteRowTableOne operation failed. %s", err)
}
case "replaceRowTableOne":
if len(args) < 3 {
return nil, errors.New("replaceRowTableOne failed. Must include 3 column values")
}
col1Val := args[0]
col2Int, err := strconv.ParseInt(args[1], 10, 32)
if err != nil {
return nil, errors.New("replaceRowTableOne failed. arg[1] must be convertable to int32")
}
col2Val := int32(col2Int)
col3Int, err := strconv.ParseInt(args[2], 10, 32)
if err != nil {
return nil, errors.New("replaceRowTableOne failed. arg[2] must be convertable to int32")
}
col3Val := int32(col3Int)
var columns []*shim.Column
col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
col2 := shim.Column{Value: &shim.Column_Int32{Int32: col2Val}}
col3 := shim.Column{Value: &shim.Column_Int32{Int32: col3Val}}
columns = append(columns, &col1)
columns = append(columns, &col2)
columns = append(columns, &col3)
row := shim.Row{Columns: columns}
ok, err := stub.ReplaceRow("tableOne", row)
if err != nil {
return nil, fmt.Errorf("replaceRowTableOne operation failed. %s", err)
}
if !ok {
return nil, errors.New("replaceRowTableOne operation failed. Row with given key does not exist")
}
case "deleteAndRecreateTableOne":
err := stub.DeleteTable("tableOne")
if err != nil {
return nil, fmt.Errorf("deleteAndRecreateTableOne operation failed. Error deleting table. %s", err)
}
err = createTableOne(stub)
if err != nil {
return nil, fmt.Errorf("deleteAndRecreateTableOne operation failed. Error creating table. %s", err)
}
return nil, nil
default:
return nil, errors.New("Unsupported operation")
}
return nil, nil
}
// Query callback representing the query of a chaincode
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
switch function {
case "getRowTableOne":
if len(args) < 1 {
return nil, errors.New("getRowTableOne failed. Must include 1 key value")
}
col1Val := args[0]
var columns []shim.Column
col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
columns = append(columns, col1)
row, err := stub.GetRow("tableOne", columns)
if err != nil {
return nil, fmt.Errorf("getRowTableOne operation failed. %s", err)
}
rowString := fmt.Sprintf("%s", row)
return []byte(rowString), nil
case "getRowTableTwo":
if len(args) < 3 {
return nil, errors.New("getRowTableTwo failed. Must include 3 key values")
}
col1Val := args[0]
col2Int, err := strconv.ParseInt(args[1], 10, 32)
if err != nil {
return nil, errors.New("getRowTableTwo failed. arg[1] must be convertable to int32")
}
col2Val := int32(col2Int)
col3Val := args[2]
var columns []shim.Column
col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
col2 := shim.Column{Value: &shim.Column_Int32{Int32: col2Val}}
col3 := shim.Column{Value: &shim.Column_String_{String_: col3Val}}
columns = append(columns, col1)
columns = append(columns, col2)
columns = append(columns, col3)
row, err := stub.GetRow("tableTwo", columns)
if err != nil {
return nil, fmt.Errorf("getRowTableTwo operation failed. %s", err)
}
rowString := fmt.Sprintf("%s", row)
return []byte(rowString), nil
case "getRowTableThree":
if len(args) < 1 {
return nil, errors.New("getRowTableThree failed. Must include 1 key value")
}
col1Val := args[0]
var columns []shim.Column
col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
columns = append(columns, col1)
row, err := stub.GetRow("tableThree", columns)
if err != nil {
return nil, fmt.Errorf("getRowTableThree operation failed. %s", err)
}
rowString := fmt.Sprintf("%s", row)
return []byte(rowString), nil
case "getRowsTableTwo":
if len(args) < 1 {
return nil, errors.New("getRowsTableTwo failed. Must include at least key values")
}
var columns []shim.Column
col1Val := args[0]
col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
columns = append(columns, col1)
if len(args) > 1 {
col2Int, err := strconv.ParseInt(args[1], 10, 32)
if err != nil {
return nil, errors.New("getRowsTableTwo failed. arg[1] must be convertable to int32")
}
col2Val := int32(col2Int)
col2 := shim.Column{Value: &shim.Column_Int32{Int32: col2Val}}
columns = append(columns, col2)
}
rowChannel, err := stub.GetRows("tableTwo", columns)
if err != nil {
return nil, fmt.Errorf("getRowsTableTwo operation failed. %s", err)
}
var rows []shim.Row
for {
select {
case row, ok := <-rowChannel:
if !ok {
rowChannel = nil
} else {
rows = append(rows, row)
}
}
if rowChannel == nil {
break
}
}
jsonRows, err := json.Marshal(rows)
if err != nil {
return nil, fmt.Errorf("getRowsTableTwo operation failed. Error marshaling JSON: %s", err)
}
return jsonRows, nil
case "getRowTableFour":
if len(args) < 1 {
return nil, errors.New("getRowTableFour failed. Must include 1 key")
}
col1Val := args[0]
var columns []shim.Column
col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
columns = append(columns, col1)
row, err := stub.GetRow("tableFour", columns)
if err != nil {
return nil, fmt.Errorf("getRowTableFour operation failed. %s", err)
}
rowString := fmt.Sprintf("%s", row)
return []byte(rowString), nil
case "getRowsTableFour":
if len(args) < 1 {
return nil, errors.New("getRowsTableFour failed. Must include 1 key value")
}
var columns []shim.Column
col1Val := args[0]
col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
columns = append(columns, col1)
rowChannel, err := stub.GetRows("tableFour", columns)
if err != nil {
return nil, fmt.Errorf("getRowsTableFour operation failed. %s", err)
}
var rows []shim.Row
for {
select {
case row, ok := <-rowChannel:
if !ok {
rowChannel = nil
} else {
rows = append(rows, row)
}
}
if rowChannel == nil {
break
}
}
jsonRows, err := json.Marshal(rows)
if err != nil {
return nil, fmt.Errorf("getRowsTableFour operation failed. Error marshaling JSON: %s", err)
}
return jsonRows, nil
default:
return nil, errors.New("Unsupported operation")
}
}
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}
func createTableOne(stub shim.ChaincodeStubInterface) error {
// Create table one
var columnDefsTableOne []*shim.ColumnDefinition
columnOneTableOneDef := shim.ColumnDefinition{Name: "colOneTableOne",
Type: shim.ColumnDefinition_STRING, Key: true}
columnTwoTableOneDef := shim.ColumnDefinition{Name: "colTwoTableOne",
Type: shim.ColumnDefinition_INT32, Key: false}
columnThreeTableOneDef := shim.ColumnDefinition{Name: "colThreeTableOne",
Type: shim.ColumnDefinition_INT32, Key: false}
columnDefsTableOne = append(columnDefsTableOne, &columnOneTableOneDef)
columnDefsTableOne = append(columnDefsTableOne, &columnTwoTableOneDef)
columnDefsTableOne = append(columnDefsTableOne, &columnThreeTableOneDef)
return stub.CreateTable("tableOne", columnDefsTableOne)
}
func createTableTwo(stub shim.ChaincodeStubInterface) error {
var columnDefsTableTwo []*shim.ColumnDefinition
columnOneTableTwoDef := shim.ColumnDefinition{Name: "colOneTableTwo",
Type: shim.ColumnDefinition_STRING, Key: true}
columnTwoTableTwoDef := shim.ColumnDefinition{Name: "colTwoTableTwo",
Type: shim.ColumnDefinition_INT32, Key: false}
columnThreeTableTwoDef := shim.ColumnDefinition{Name: "colThreeTableThree",
Type: shim.ColumnDefinition_INT32, Key: true}
columnFourTableTwoDef := shim.ColumnDefinition{Name: "colFourTableFour",
Type: shim.ColumnDefinition_STRING, Key: true}
columnDefsTableTwo = append(columnDefsTableTwo, &columnOneTableTwoDef)
columnDefsTableTwo = append(columnDefsTableTwo, &columnTwoTableTwoDef)
columnDefsTableTwo = append(columnDefsTableTwo, &columnThreeTableTwoDef)
columnDefsTableTwo = append(columnDefsTableTwo, &columnFourTableTwoDef)
return stub.CreateTable("tableTwo", columnDefsTableTwo)
}
func createTableThree(stub shim.ChaincodeStubInterface) error {
var columnDefsTableThree []*shim.ColumnDefinition
columnOneTableThreeDef := shim.ColumnDefinition{Name: "colOneTableThree",
Type: shim.ColumnDefinition_STRING, Key: true}
columnTwoTableThreeDef := shim.ColumnDefinition{Name: "colTwoTableThree",
Type: shim.ColumnDefinition_INT32, Key: false}
columnThreeTableThreeDef := shim.ColumnDefinition{Name: "colThreeTableThree",
Type: shim.ColumnDefinition_INT64, Key: false}
columnFourTableThreeDef := shim.ColumnDefinition{Name: "colFourTableFour",
Type: shim.ColumnDefinition_UINT32, Key: false}
columnFiveTableThreeDef := shim.ColumnDefinition{Name: "colFourTableFive",
Type: shim.ColumnDefinition_UINT64, Key: false}
columnSixTableThreeDef := shim.ColumnDefinition{Name: "colFourTableSix",
Type: shim.ColumnDefinition_BYTES, Key: false}
columnSevenTableThreeDef := shim.ColumnDefinition{Name: "colFourTableSeven",
Type: shim.ColumnDefinition_BOOL, Key: false}
columnDefsTableThree = append(columnDefsTableThree, &columnOneTableThreeDef)
columnDefsTableThree = append(columnDefsTableThree, &columnTwoTableThreeDef)
columnDefsTableThree = append(columnDefsTableThree, &columnThreeTableThreeDef)
columnDefsTableThree = append(columnDefsTableThree, &columnFourTableThreeDef)
columnDefsTableThree = append(columnDefsTableThree, &columnFiveTableThreeDef)
columnDefsTableThree = append(columnDefsTableThree, &columnSixTableThreeDef)
columnDefsTableThree = append(columnDefsTableThree, &columnSevenTableThreeDef)
return stub.CreateTable("tableThree", columnDefsTableThree)
}
func createTableFour(stub shim.ChaincodeStubInterface) error {
var columnDefsTableFour []*shim.ColumnDefinition
columnOneTableFourDef := shim.ColumnDefinition{Name: "colOneTableFour",
Type: shim.ColumnDefinition_STRING, Key: true}
columnDefsTableFour = append(columnDefsTableFour, &columnOneTableFourDef)
return stub.CreateTable("tableFour", columnDefsTableFour)
}