/
blockchain.js
342 lines (312 loc) · 10.9 KB
/
blockchain.js
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
/*
* 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.
*/
'use strict';
const Logger = require('./utils/caliper-utils').getLogger('blockchain');
/**
* BlockChain class, define operations to interact with the blockchain system under test
*/
class Blockchain {
/**
* Constructor
* @param {Object} bcObj the concrete blockchain object
*/
constructor(bcObj) {
this.bcObj = bcObj;
this.bcType = bcObj.bcType;
}
/**
* return the blockchain type
* @return {string} type of the blockchain
*/
getType() {
return this.bcType;
}
/**
* Initialise test environment, e.g. create a fabric channel for the test
* @async
*/
async init() {
await this.bcObj.init();
}
/**
* Perform required preparation for test clients, e.g. enroll clients and obtain key pairs
* @param {Number} number count of test clients
* @return {Promise} array of obtained material for test clients
* @async
*/
async prepareClients (number) {
return await this.bcObj.prepareClients(number);
}
/**
* Install smart contract(s), detail information is defined in the blockchain configuration file
* @async
*/
async installSmartContract() {
await this.bcObj.installSmartContract();
}
/**
* Get a context for subsequent operations, e.g. invoke smart contract or query state
* @param {String} name name of the context
* @param {Object} args adapter specific arguments
* @param {Integer} clientIdx the client index
* @param {Object} txFile the file information for reading or writing.
* @return {Promise} obtained context object
* @async
*/
async getContext(name, args, clientIdx, txFile) {
return await this.bcObj.getContext(name, args, clientIdx, txFile);
}
/**
* Release a context as well as related resources
* @param {Object} context adapter specific object
* @return {Promise} promise object
*/
async releaseContext(context) {
return await this.bcObj.releaseContext(context);
}
/**
* Invoke smart contract/submit transactions and return corresponding transactions' status
* @param {Object} context context object
* @param {String} contractID identity of the contract
* @param {String} contractVer version of the contract
* @param {Array} args array of JSON formatted arguments for multiple transactions
* @param {Number} timeout request timeout, in second
* @return {Promise} txStatus object or an array of txStatus objects
*/
async invokeSmartContract(context, contractID, contractVer, args, timeout) {
let arg, time; // compatible with old version
if(Array.isArray(args)) {
arg = args;
}
else if(typeof args === 'object') {
arg = [args];
}
else {
throw new Error('Invalid args for invokeSmartContract()');
}
if(typeof timeout !== 'number' || timeout < 0) {
time = 120;
}
else {
time = timeout;
}
return await this.bcObj.invokeSmartContract(context, contractID, contractVer, arg, time);
}
/**
* Query state from the ledger using a smart contract
* @param {Object} context context object
* @param {String} contractID identity of the contract
* @param {String} contractVer version of the contract
* @param {Array} args array of JSON formatted arguments
* @param {Number} timeout request timeout, in seconds
* @return {Promise} query response object
*/
async querySmartContract(context, contractID, contractVer, args, timeout) {
let arg, time;
if(Array.isArray(args)) {
arg = args;
}
else if(typeof args === 'object') {
arg = [args];
}
else {
throw new Error('Invalid args for querySmartContract()');
}
if(typeof timeout !== 'number' || timeout < 0) {
time = 120;
}
else {
time = timeout;
}
return await this.bcObj.querySmartContract(context, contractID, contractVer, arg, time);
}
/**
* Query state from the ledger
* @param {Object} context context object from getContext
* @param {String} contractID identity of the contract
* @param {String} contractVer version of the contract
* @param {String} key lookup key
* @param {String=} [fcn] query function name
* @return {Object} as invokeSmartContract()
*/
async queryState(context, contractID, contractVer, key, fcn) {
return await this.bcObj.queryState(context, contractID, contractVer, key, fcn);
}
/**
* Calculate the default transaction statistics
* @param {Array} resultArray array of txStatus
* @param {Boolean} detail indicates whether to keep detailed information
* @return {JSON} txStatistics JSON object
*/
getDefaultTxStats(resultArray, detail) {
let succ = 0, fail = 0, delay = 0;
let minFinal, maxFinal, minCreate, maxCreate;
let maxLastFinal;
let minDelay = 100000, maxDelay = 0;
let delays = [];
let sTPTotal = 0;
let sTTotal = 0;
let invokeTotal = 0;
for(let i = 0 ; i < resultArray.length ; i++) {
let stat = resultArray[i];
sTPTotal = sTPTotal + stat.Get('sTP');
sTTotal = sTTotal + stat.Get('sT');
invokeTotal += stat.Get('invokeLatency');
let create = stat.GetTimeCreate();
if(typeof minCreate === 'undefined') {
minCreate = create;
maxCreate = create;
}
else {
if(create < minCreate) {
minCreate = create;
}
if(create > maxCreate) {
maxCreate = create;
}
}
if(stat.IsCommitted()) {
succ++;
let final = stat.GetTimeFinal();
let d = (final - create) / 1000;
if(typeof minFinal === 'undefined') {
minFinal = final;
maxFinal = final;
}
else {
if(final < minFinal) {
minFinal = final;
}
if(final > maxFinal) {
maxFinal = final;
}
}
delay += d;
if(d < minDelay) {
minDelay = d;
}
if(d > maxDelay) {
maxDelay = d;
}
if(detail) {
delays.push(d);
}
}
else {
fail++;
}
let curFinal = stat.GetTimeFinal();
if(typeof maxLastFinal === 'undefined') {
maxLastFinal = curFinal;
}
else{
if(curFinal > maxLastFinal){
maxLastFinal = curFinal;
}
}
}
let stats = {
'succ' : succ,
'fail' : fail,
'create' : {'min' : minCreate/1000, 'max' : maxCreate/1000}, // convert to second
'final' : {'min' : minFinal/1000, 'max' : maxFinal/1000, 'last' : maxLastFinal/1000 },
'delay' : {'min' : minDelay, 'max' : maxDelay, 'sum' : delay, 'detail': (detail?delays:[]) },
'out' : [],
'sTPTotal': sTPTotal,
'sTTotal': sTTotal,
'invokeTotal': invokeTotal,
'length': resultArray.length
};
return stats;
}
/**
* merge an array of default 'txStatistics', the result is in first object of the array
* Note even failed the first object of the array may still be changed
* @param {Array} resultArray txStatistics array
* @return {Number} 0 if failed; otherwise 1
*/
static mergeDefaultTxStats(resultArray) {
try{
// skip invalid result
let skip = 0;
for(let i = 0 ; i < resultArray.length ; i++) {
let result = resultArray[i];
if(!result.hasOwnProperty('succ') || !result.hasOwnProperty('fail') || (result.succ + result.fail) === 0) {
skip++;
}
else {
break;
}
}
if(skip > 0) {
resultArray.splice(0, skip);
}
if(resultArray.length === 0) {
return 0;
}
let r = resultArray[0];
for(let i = 1 ; i < resultArray.length ; i++) {
let v = resultArray[i];
if(!v.hasOwnProperty('succ') || !v.hasOwnProperty('fail') || (v.succ + v.fail) === 0) {
continue;
}
r.succ += v.succ;
r.fail += v.fail;
r.sTPTotal += v.sTPTotal;
r.sTTotal += v.sTTotal;
r.invokeTotal += v.invokeTotal;
r.length += v.length;
r.out.push.apply(r.out, v.out);
if(v.create.min < r.create.min) {
r.create.min = v.create.min;
}
if(v.create.max > r.create.max) {
r.create.max = v.create.max;
}
if(v.final.min < r.final.min) {
r.final.min = v.final.min;
}
if(v.final.max > r.final.max) {
r.final.max = v.final.max;
}
if(v.final.last > r.final.last){
r.final.last = v.final.last;
}
if(v.delay.min < r.delay.min) {
r.delay.min = v.delay.min;
}
if(v.delay.max > r.delay.max) {
r.delay.max = v.delay.max;
}
r.delay.sum += v.delay.sum;
for(let j = 0 ; j < v.delay.detail.length ; j++) {
r.delay.detail.push(v.delay.detail[j]);
}
}
return 1;
}
catch(err) {
Logger.error(err);
return 0;
}
}
/**
* create a 'null' txStatistics object
* @return {JSON} 'null' txStatistics object
*/
static createNullDefaultTxStats() {
return {succ: 0, fail: 0};
}
}
module.exports = Blockchain;