forked from hyperledger-caliper/caliper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain.js
290 lines (266 loc) · 9.05 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
/**
* Copyright 2017 HUAWEI. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
*/
'use strict';
/**
* BlockChain class, define operations to interact with the blockchain system under test
*/
class Blockchain {
/**
* Constructor
* @param {String} configPath path of the blockchain configuration file
*/
constructor(configPath) {
let config = require(configPath);
if(config.hasOwnProperty('fabric')) {
let fabric = require('../fabric/fabric.js');
this.bcType = 'fabric';
this.bcObj = new fabric(configPath);
}
else if(config.hasOwnProperty('sawtooth')) {
let sawtooth = require('../sawtooth/sawtooth.js');
this.bcType = 'sawtooth';
this.bcObj = new sawtooth(configPath);
}
else if(config.hasOwnProperty('iroha')) {
let iroha = require('../iroha/iroha.js');
this.bcType = 'iroha';
this.bcObj = new iroha(configPath);
}
else if(config.hasOwnProperty('composer')) {
let composer = require('../composer/composer.js');
this.bcType = 'composer';
this.bcObj = new composer(configPath);
}
else {
this.bcType = 'unknown';
throw new Error('Unknown blockchain config file ' + configPath);
}
}
/**
* return the blockchain's type
* @return {string} type of the blockchain
*/
gettype() {
return this.bcType;
}
/**
* Initialise test environment, e.g. create a fabric channel for the test
* @return {Promise} promise object
*/
init() {
return 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
*/
prepareClients (number) {
return this.bcObj.prepareClients(number);
}
/**
* Install smart contract(s), detail informations are defined in the blockchain configuration file
* @return {Promise} promise object
*/
installSmartContract() {
return 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
* @return {Promise} obtained context object
*/
getContext(name, args) {
return this.bcObj.getContext(name, args);
}
/**
* Release a context as well as related resources
* @param {Object} context adapter specific object
* @return {Promise} promise object
*/
releaseContext(context) {
return this.bcObj.releaseContext(context);
}
/**
* Invoke smart contract/submit transactions and return corresponding transactions' status
* @param {Object} context context object
* @param {String} contractID identiy 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
*/
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 {
return Promise.reject(new Error('Invalid args for invokeSmartContract()'));
}
if(typeof timeout !== 'number' || timeout < 0) {
time = 120;
}
else {
time = timeout;
}
return this.bcObj.invokeSmartContract(context, contractID, contractVer, arg, time);
}
/**
* Query state from the ledger
* @param {Object} context context object from getContext
* @param {String} contractID identiy of the contract
* @param {String} contractVer version of the contract
* @param {String} key lookup key
* @return {Promise} as invokeSmateContract()
*/
queryState(context, contractID, contractVer, key) {
return this.bcObj.queryState(context, contractID, contractVer, key);
}
/**
* Calculate the default transaction statistics
* @param {Array} results array of txStatus
* @param {Boolean} detail indicates whether to keep detailed information
* @return {JSON} txStatistics JSON object
*/
getDefaultTxStats(results, detail) {
let succ = 0, fail = 0, delay = 0;
let minFinal, maxFinal, minCreate, maxCreate;
let minDelay = 100000, maxDelay = 0;
let delays = [];
for(let i = 0 ; i < results.length ; i++) {
let stat = results[i];
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 stats = {
'succ' : succ,
'fail' : fail,
'create' : {'min' : minCreate/1000, 'max' : maxCreate/1000}, // convert to second
'final' : {'min' : minFinal/1000, 'max' : maxFinal/1000 },
'delay' : {'min' : minDelay, 'max' : maxDelay, 'sum' : delay, 'detail': (detail?delays:[]) },
'out' : []
};
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} results txStatistics array
* @return {Number} 0 if failed; otherwise 1
*/
static mergeDefaultTxStats(results) {
try{
// skip invalid result
let skip = 0;
for(let i = 0 ; i < results.length ; i++) {
let result = results[i];
if(!result.hasOwnProperty('succ') || !result.hasOwnProperty('fail') || (result.succ + result.fail) === 0) {
skip++;
}
else {
break;
}
}
if(skip > 0) {
results.splice(0, skip);
}
if(results.length === 0) {
return 0;
}
let r = results[0];
for(let i = 1 ; i < results.length ; i++) {
let v = results[i];
if(!v.hasOwnProperty('succ') || !v.hasOwnProperty('fail') || (v.succ + v.fail) === 0) {
continue;
}
r.succ += v.succ;
r.fail += v.fail;
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.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) {
return 0;
}
}
/**
* create a 'null' txStatistics object
* @return {JSON} 'null' txStatistics object
*/
static createNullDefaultTxStats() {
return {succ: 0, fail: 0};
}
}
module.exports = Blockchain;