forked from accordproject/concerto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datacollection.go
498 lines (408 loc) · 17.1 KB
/
datacollection.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
/*
* 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 (
duktape "gopkg.in/olebedev/go-duktape.v3"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
import "time"
// DataCollection is a Go wrapper around an instance of the DataCollection JavaScript class.
type DataCollection struct {
VM *duktape.Context
Stub shim.ChaincodeStubInterface
CollectionID string
}
// NewDataCollection creates a Go wrapper around a new instance of the DataCollection JavaScript class.
func NewDataCollection(vm *duktape.Context, dataService *DataService, stub shim.ChaincodeStubInterface, collectionID string) (result *DataCollection) {
logger.Debug("Entering NewDataCollection", vm, dataService, &stub)
defer func() { logger.Debug("Exiting NewDataCollection", result) }()
// Ensure the JavaScript stack is reset.
defer vm.SetTop(vm.GetTop())
// Create the new data service.
result = &DataCollection{VM: vm, Stub: stub, CollectionID: collectionID}
// Get the data service.
vm.PushGlobalStash() // [ stash ]
vm.GetPropString(-1, "dataService") // [ stash theDataService ]
// Create a new instance of the JavaScript DataCollection class.
vm.PushGlobalObject() // [ stash theDataService global ]
vm.GetPropString(-1, "composer") // [ stash theDataService global composer ]
vm.GetPropString(-1, "DataCollection") // [ stash theDataService global composer DataCollection ]
vm.Dup(-4) // [ stash theDataService global composer DataCollection theDataService ]
err := vm.Pnew(1) // [ stash theDataService global composer theDataCollection ]
if err != nil {
panic(err)
}
// Store the data collection into the global stash.
vm.DupTop() // [ stash theDataService global composer theDataCollection theDataCollection ]
vm.PutPropString(-6, "dataCollection") // [ stash theDataService global composer theDataCollection ]
// Bind the methods into the JavaScript object.
vm.PushGoFunction(result.getAll) // [ stash theDataService global composer theDataCollection getAll ]
vm.PutPropString(-2, "_getAll") // [ stash theDataService global composer theDataCollection ]
vm.PushGoFunction(result.get) // [ stash theDataService global composer theDataCollection get ]
vm.PutPropString(-2, "_get") // [ stash theDataService global composer theDataCollection ]
vm.PushGoFunction(result.exists) // [ stash theDataService global composer theDataCollection exists ]
vm.PutPropString(-2, "_exists") // [ stash theDataService global composer theDataCollection ]
vm.PushGoFunction(result.add) // [ stash theDataService global composer theDataCollection add ]
vm.PutPropString(-2, "_add") // [ stash theDataService global composer theDataCollection ]
vm.PushGoFunction(result.update) // [ stash theDataService global composer theDataCollection update ]
vm.PutPropString(-2, "_update") // [ stash theDataService global composer theDataCollection ]
vm.PushGoFunction(result.remove) // [ stash theDataService global composer theDataCollection remove ]
vm.PutPropString(-2, "_remove") // [ stash theDataService global composer theDataCollection ]
// Return the new data collection.
return result
}
// getAll retreieves all of the objects in this collection from the world state.
func (dataCollection *DataCollection) getAll(vm *duktape.Context) (result int) {
logger.Debug("Entering DataCollection.getAll", vm)
start := time.Now()
defer func() { logger.Debug("@perf DataCollection.getAll total duration for [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(start)) }()
defer func() { logger.Debug("Exiting DataCollection.getAll", result) }()
// Validate the arguments from JavaScript.
vm.RequireFunction(0)
increment := time.Now();
// Create the composite key.
// The objects are stored with composite keys of collectionID + objectID.
iterator, err := dataCollection.Stub.GetStateByPartialCompositeKey(dataCollection.CollectionID, []string{})
if err != nil {
vm.Dup(0)
vm.PushErrorObjectVa(duktape.ErrError, "%s", err.Error())
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
}
logger.Debug("@perf DataCollection.getAll GetStateByPartialCompositeKey duration for [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(increment))
increment = time.Now();
// Must close iterator to free resources.
defer iterator.Close()
// Iterate over all the keys returned by the partial query above.
arrIdx := vm.PushArray()
arrCount := uint(0)
for iterator.HasNext() {
// Read the current key and value.
kv, err := iterator.Next()
if err != nil {
vm.Dup(0)
vm.PushErrorObjectVa(duktape.ErrError, "%s", err.Error())
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
}
// Parse the current value.
vm.PushString(string(kv.Value))
vm.JsonDecode(-1)
vm.PutPropIndex(arrIdx, arrCount)
arrCount++
}
logger.Debug("@perf DataCollection.getAll key-iterator duration for [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(increment))
increment = time.Now();
// Call the callback.
vm.Dup(0)
vm.PushNull()
vm.Dup(arrIdx)
vm.Pcall(2)
logger.Debug("@perf DataCollection.getAll callback duration for [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(increment))
return 0
}
// get retrieves a specific object in this collection from the world state.
func (dataCollection *DataCollection) get(vm *duktape.Context) (result int) {
logger.Debug("Entering DataCollection.get", vm)
start := time.Now()
defer func() { logger.Debug("@perf DataCollection.get total duration for [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(start)) }()
defer func() { logger.Debug("Exiting DataCollection.get", result) }()
// Validate the arguments from JavaScript.
id := vm.RequireString(0)
vm.RequireFunction(1)
// Create the composite key.
// The objects are stored with composite keys of collectionID + objectID.
key, err := dataCollection.Stub.CreateCompositeKey(dataCollection.CollectionID, []string{id})
if err != nil {
vm.Dup(1)
vm.PushErrorObjectVa(duktape.ErrError, "%s", err.Error())
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
}
increment := time.Now();
// Get the collection.
value, err := dataCollection.Stub.GetState(key)
if err != nil {
vm.Dup(1)
vm.PushErrorObjectVa(duktape.ErrError, "%s", err.Error())
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
} else if value == nil {
vm.Dup(1)
vm.PushErrorObjectVa(duktape.ErrError, "Object with ID '%s' in collection with ID '%s' does not exist", id, dataCollection.CollectionID)
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
}
logger.Debug("@perf DataCollection.get GetState duration for [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(increment))
increment = time.Now();
// Parse the current value.
vm.PushString(string(value))
vm.JsonDecode(-1)
// Call the callback.
vm.Dup(1)
vm.PushNull()
vm.Dup(-3)
if vm.Pcall(2) == duktape.ExecError {
panic(vm.ToString(-1))
}
logger.Debug("@perf DataCollection.get callback duration for [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(increment))
return 0
}
// exists checks to see if an object exists in this collection in the world state.
func (dataCollection *DataCollection) exists(vm *duktape.Context) (result int) {
logger.Debug("Entering DataCollection.exists", vm)
start := time.Now()
defer func() { logger.Debug("@perf DataCollection.exists total duration for [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(start)) }()
defer func() { logger.Debug("Exiting DataCollection.exists", result) }()
// Validate the arguments from JavaScript.
id := vm.RequireString(0)
vm.RequireFunction(1)
// Create the composite key.
// The objects are stored with composite keys of collectionID + objectID.
key, err := dataCollection.Stub.CreateCompositeKey(dataCollection.CollectionID, []string{id})
if err != nil {
vm.Dup(1)
vm.PushErrorObjectVa(duktape.ErrError, "%s", err.Error())
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
}
increment := time.Now();
// Get the object.
value, err := dataCollection.Stub.GetState(key)
if err != nil {
vm.Dup(1)
vm.PushErrorObjectVa(duktape.ErrError, "%s", err.Error())
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
}
logger.Debug("@perf DataCollection.get GetState duration for [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(increment))
increment = time.Now();
// Call the callback.
vm.Dup(1)
vm.PushNull()
vm.PushBoolean(value != nil)
if vm.Pcall(2) == duktape.ExecError {
panic(vm.ToString(-1))
}
logger.Debug("@perf DataCollection.get callback duration for [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(increment))
return 0
}
// add adds an object to this collection in the world satte.
func (dataCollection *DataCollection) add(vm *duktape.Context) (result int) {
logger.Debug("Entering DataCollection.add", vm)
start := time.Now()
defer func() { logger.Debug("@perf DataCollection.add total duration for [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(start)) }()
defer func() { logger.Debug("Exiting DataCollection.add", result) }()
// Validate the arguments from JavaScript.
id := vm.RequireString(0)
vm.RequireObjectCoercible(1)
force := vm.RequireBoolean(2)
vm.RequireFunction(3)
// Serialize the object.
vm.Dup(1)
vm.JsonEncode(-1)
value := vm.RequireString(-1)
// Create the composite key.
// The objects are stored with composite keys of collectionID + objectID.
key, err := dataCollection.Stub.CreateCompositeKey(dataCollection.CollectionID, []string{id})
if err != nil {
vm.Dup(3)
vm.PushErrorObjectVa(duktape.ErrError, "%s", err.Error())
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
}
if !force {
// Check to see if the object already exists.
existingValue, err := dataCollection.Stub.GetState(key)
if err != nil {
vm.Dup(3)
vm.PushErrorObjectVa(duktape.ErrError, "%s", err.Error())
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
} else if existingValue != nil {
vm.Dup(3)
vm.PushErrorObjectVa(duktape.ErrError, "Failed to add object with ID '%s' as the object already exists", id)
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
}
}
increment := time.Now();
// Store the object in the collection.
err = dataCollection.Stub.PutState(key, []byte(value))
if err != nil {
vm.Dup(2)
vm.PushErrorObjectVa(duktape.ErrError, "%s", err.Error())
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
}
logger.Debug("@perf DataCollection.get PutState duration for collection [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(increment))
increment = time.Now();
// Call the callback.
vm.Dup(3)
vm.PushNull()
if vm.Pcall(1) == duktape.ExecError {
panic(vm.ToString(-1))
}
logger.Debug("@perf DataCollection.get callback duration for collection [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(increment))
return 0
}
// update updates an existing object in this collection in the world state.
func (dataCollection *DataCollection) update(vm *duktape.Context) (result int) {
logger.Debug("Entering DataCollection.update", vm)
start := time.Now()
defer func() { logger.Debug("@perf DataCollection.update total duration for [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(start)) }()
defer func() { logger.Debug("Exiting DataCollection.update", result) }()
// Validate the arguments from JavaScript.
id := vm.RequireString(0)
vm.RequireObjectCoercible(1)
vm.RequireFunction(2)
// Serialize the object.
vm.Dup(1)
vm.JsonEncode(-1)
value := vm.RequireString(-1)
// Create the composite key.
// The objects are stored with composite keys of collectionID + objectID.
key, err := dataCollection.Stub.CreateCompositeKey(dataCollection.CollectionID, []string{id})
if err != nil {
vm.Dup(2)
vm.PushErrorObjectVa(duktape.ErrError, "%s", err.Error())
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
}
increment := time.Now();
// Check to see if the object already exists.
existingValue, err := dataCollection.Stub.GetState(key)
if err != nil {
vm.Dup(2)
vm.PushErrorObjectVa(duktape.ErrError, "%s", err.Error())
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
} else if existingValue == nil {
vm.Dup(2)
vm.PushErrorObjectVa(duktape.ErrError, "Failed to update object with ID '%s' as the object does not exist", id)
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
}
logger.Debug("@perf DataCollection.update GetState duration for collection [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(increment))
increment = time.Now();
// Store the object in the collection.
err = dataCollection.Stub.PutState(key, []byte(value))
if err != nil {
vm.Dup(2)
vm.PushErrorObjectVa(duktape.ErrError, "%s", err.Error())
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
}
logger.Debug("@perf DataCollection.update PutState duration for collection [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(increment))
increment = time.Now();
// Call the callback.
vm.Dup(2)
vm.PushNull()
if vm.Pcall(1) == duktape.ExecError {
panic(vm.ToString(-1))
}
logger.Debug("@perf DataCollection.update callback duration for collection [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(increment))
return 0
}
// remove removes an object from this collection in the world state.
func (dataCollection *DataCollection) remove(vm *duktape.Context) (result int) {
logger.Debug("Entering DataCollection.remove", vm)
start := time.Now()
defer func() { logger.Debug("@perf DataCollection.remove total duration for [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(start)) }()
defer func() { logger.Debug("Exiting DataCollection.remove", result) }()
// Validate the arguments from JavaScript.
id := vm.RequireString(0)
vm.RequireFunction(1)
// Create the composite key.
// The objects are stored with composite keys of collectionID + objectID.
key, err := dataCollection.Stub.CreateCompositeKey(dataCollection.CollectionID, []string{id})
if err != nil {
vm.Dup(1)
vm.PushErrorObjectVa(duktape.ErrError, "%s", err.Error())
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
}
increment := time.Now();
// Check to see if the object already exists.
existingValue, err := dataCollection.Stub.GetState(key)
if err != nil {
vm.Dup(1)
vm.PushErrorObjectVa(duktape.ErrError, "%s", err.Error())
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
} else if existingValue == nil {
vm.Dup(1)
vm.PushErrorObjectVa(duktape.ErrError, "Failed to delete object with ID '%s' as the object does not exist", id)
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
}
logger.Debug("@perf DataCollection.remove GetState duration for collection [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(increment))
increment = time.Now();
// Remove the object from the collection.
err = dataCollection.Stub.DelState(key)
if err != nil {
vm.Dup(1)
vm.PushErrorObjectVa(duktape.ErrError, "%s", err.Error())
if vm.Pcall(1) == duktape.ExecError {
panic(err)
}
return 0
}
logger.Debug("@perf DataCollection.remove DelState duration for collection [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(increment))
increment = time.Now();
// Call the callback.
vm.Dup(1)
vm.PushNull()
if vm.Pcall(1) == duktape.ExecError {
panic(vm.ToString(-1))
}
logger.Debug("@perf DataCollection.remove callback duration for collection [", dataCollection.CollectionID, "] and transaction [", dataCollection.Stub.GetTxID(),"] :", time.Now().Sub(increment))
return 0
}