forked from hyperledger-archives/caliper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain.js
206 lines (187 loc) · 6.83 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
/**
* Copyright 2017 HUAWEI. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* @file, definition of the BlockChain class, which is used to interact with backend's blockchain system
*/
'use strict'
var path = require('path');
var Blockchain = class {
constructor(configPath) {
var config = require(configPath);
if(config.hasOwnProperty('fabric')) {
var fabric = require('../fabric/fabric.js');
this.bcType = 'fabric';
this.bcObj = new fabric(configPath);
}
else if(config.hasOwnProperty('sawtooth')) {
var sawtooth = require('../sawtooth/sawtooth.js')
this.bcType = 'sawtooth';
this.bcObj = new sawtooth(configPath);
}
else {
this.bcType = 'unknown';
throw new Error('Unknown blockchain config file ' + configPath);
}
}
/**
* return the blockchain type
* @return {string}
*/
gettype() {
return this.bcType;
}
/**
* prepare the underlying blockchain environment, e.g. join channel for fabric's peers
* the function should be called only once for the same backend's blockchain system
* even if multiple Blockchain objects are instantiated
* @return {Promise}
*/
init() {
return this.bcObj.init();
}
/**
* install smart contract on peers
* the detailed smart contract's information should be defined in the configuration file
* @return {Promise}
*/
installSmartContract() {
return this.bcObj.installSmartContract();
}
/**
* get a system context that will be used to interact with backend's blockchain system
* @name {string}, name of the context
* @return {Promise.resolve(context)}
*/
getContext(name) {
return this.bcObj.getContext(name);
}
/**
* release the system context
* @return {Promise}
*/
releaseContext(context) {
return this.bcObj.releaseContext(context);
}
/**
* perform an 'invoke' transaction
* @context {Object}, context returned by getContext
* @contractID {string}, smart contract's id
* @contractVer {string}, smart contract's version
* @args {Array}, invoking arguments [arg1, arg2, ...]
* @timeout {Number}, return directly after that time in seconds has elapsed
* @return {Promise.resolve(Object)}, return the key informations of the transaction, the format is
* {
* 'id': transaction's id
* 'status': status of the transaction, should be:
* - 'created': successfully created, but not validated or committed yet
* - 'success': successfully validated and committed in the ledger
* 'time_create': time that the transaction was created
* 'time_valid': time that the transaction was known to be valid and committed in ledger
* 'result': response payloads of the transaction request
* ...... : blockchain platform specific values
* }
*/
invokeSmartContract(context, contractID, contractVer, args, timeout) {
if(typeof timeout !== 'number' || timeout < 0) {
return this.bcObj.invokeSmartContract(context, contractID, contractVer, args, 120);
}
else {
return this.bcObj.invokeSmartContract(context, contractID, contractVer, args, timeout);
}
}
/**
* * perform a 'query' transaction to get state from the ledger
* @return {Promsie}, same format as invokeSmartContract's returning
*/
queryState(context, contractID, contractVer, key) {
return this.bcObj.queryState(context, contractID, contractVer, key);
}
/**
* txStatistics = {
* succ : , // number of succeeded txs
* fail : , // number of failed txs
* create : {min: , max: }, // min/max time of tx created
* valid : {min: , max: }, // min/max time of tx becoming valid
* delay : {min: , max: , sum: }, // min/max/sum time of txs' processing delay
* throughput : {time: ,...}, // tps of each time slot
* others: {object} // blockchain platform specific values
* }
*/
/**
* generate and return the default statistics of transactions
* @ results {Array}, results of 'invoke'/'query' transactions
* @ return {Promise.resolve(txStatistics)}
*/
// TODO: should be moved to a dependent 'analyser' module in which to do all result analysing work
getDefaultTxStats(results) {
var succ = 0, fail = 0, delay = 0;
var minValid, maxValid, minCreate, maxCreate;
var minDelay = 100000, maxDelay = 0;
var throughput = {};
for(let i = 0 ; i < results.length ; i++) {
let stat = results[i];
let create = stat['time_create'];
if(typeof minCreate === 'undefined') {
minCreate = create;
maxCreate = create;
}
else {
if(create < minCreate) {
minCreate = create;
}
if(create > maxCreate) {
maxCreate = create;
}
}
if(stat.status === 'success') {
succ++;
let valid = stat['time_valid'];
let d = valid - create;
if(typeof minValid === 'undefined') {
minValid = valid;
maxValid = valid;
}
else {
if(valid < minValid) {
minValid = valid;
}
if(valid > maxValid) {
maxValid = valid;
}
}
delay += d;
if(d < minDelay) {
minDelay = d;
}
if(d > maxDelay) {
maxDelay = d;
}
let idx = Math.round(valid).toString();
if(typeof throughput[idx] === 'undefined') {
throughput[idx] = 1;
}
else {
throughput[idx] += 1;
}
}
else {
fail++;
}
}
var stats = {
'succ' : succ,
'fail' : fail,
'create' : {'min' : minCreate, 'max' : maxCreate},
'valid' : {'min' : minValid, 'max' : maxValid },
'delay' : {'min' : minDelay, 'max' : maxDelay, 'sum' : delay },
'throughput' : throughput
};
if(this.bcObj.getDefaultTxStats !== 'undefined') {
this.bcObj.getDefaultTxStats(stats, results);
}
return stats;
}
}
module.exports = Blockchain;