-
Notifications
You must be signed in to change notification settings - Fork 402
/
sawtooth.js
456 lines (421 loc) · 14.9 KB
/
sawtooth.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
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
/*
* 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 { BlockchainInterface, CaliperUtils, TxStatus }= require('@hyperledger/caliper-core');
const logger = CaliperUtils.getLogger('sawtooth.js');
const BatchBuilderFactory = require('./batch/BatchBuilderFactory.js');
const { Stream } = require('sawtooth-sdk/messaging/stream');
const {
Message,
EventList,
EventSubscription,
ClientEventsSubscribeRequest,
ClientEventsSubscribeResponse,
ClientEventsUnsubscribeRequest,
ClientEventsUnsubscribeResponse
} = require('sawtooth-sdk/protobuf');
const request = require('request-promise');
const _ = require('lodash');
let configPath;
let lastKnownBlockId=null;
//let blockCommitSatus = new Map();
let batchCommitStatus = new Map();
let currentBlockNum=0;
let currentEndpoint= 0;
/**
* Get the current rest end point
* @return {String} rest endpoint url
*/
function getRESTUrl() {
let config = require(configPath);
let restApiUrls = config.sawtooth.network.restapi.urls;
currentEndpoint++;
if(currentEndpoint >= restApiUrls.length) {
currentEndpoint = currentEndpoint % restApiUrls.length;
}
return restApiUrls[currentEndpoint];
}
/**
* Get the last recent block id for the block chain
* @return {Promise<String>} last recent block id
*/
async function getCurrentBlockId() {
const request = require('request-promise');
let restAPIUrl = getRESTUrl();
const blocks = restAPIUrl + '/blocks?limit=1';
let options = {
uri: blocks
};
return request(options)
.then(function(body) {
let data = (JSON.parse(body)).data;
if (data.length > 0) {
currentBlockNum = parseInt(data[0].header.block_num);
lastKnownBlockId = data[0].header_signature.toString();
return currentBlockNum;
}
});
}
/**
* Get batch ids from block
* @param {String} blockId the ID of a block
* @return {Promise<object>} The promise for the batch ids
*/
async function getBlockBatchIds(blockId) {
const request = require('request-promise');
let restAPIUrl = getRESTUrl();
const blocks = restAPIUrl + '/blocks/' + blockId;
let options = {
uri: blocks
};
return request(options)
.then(function(body) {
let data = (JSON.parse(body)).data;
if (data !== undefined) {
let batchIds = data.header.batch_ids;
return batchIds;
}else{
return [];
}
});
}
/**
* Get block data from event message
* @param {Object} events message
* @return {Promise<object>} The promise for the result of event message
*/
async function getBlock(events) {
const block = _.chain(events)
.find(e => e.eventType === 'sawtooth/block-commit')
.get('attributes')
.map(a => [a.key, a.value])
.fromPairs()
.value();
let batchIds = await getBlockBatchIds(block.block_id);
return {
blockNum: parseInt(block.block_num),
blockId: block.block_id.toString(),
batchIds: batchIds,
stateRootHash: block.state_root_hash
};
}
/**
* Handle event message to updated lastKnownBlockId for next event subscription
* @param {Object} msg event message
* @return {void}
*/
async function handleEvent(msg) {
if (msg.messageType === Message.MessageType.CLIENT_EVENTS) {
//logger.info('msg: ' + JSON.stringify(msg));
const events = EventList.decode(msg.content).events;
getBlock(events).then(result => {
//On receiving event with block, update the statuses of the batches success
for(let i = 0; i < result.batchIds.length; i++) {
batchCommitStatus.set(result.batchIds[i].toString('hex'), 'success');
}
});
} else {
logger.warn('Warn: Received message of unknown type:', msg.messageType);
}
}
/**
* Subscribe to block-commit delta events
* @param {Object} stream object to send event subscribe message
* @return {void}
*/
async function subscribe(stream) {
//Subscribe to block-commit delta event
const blockSub = EventSubscription.create({
eventType: 'sawtooth/block-commit'
});
if(lastKnownBlockId === null) {
await getCurrentBlockId().then(() => {
});
}
stream.send(
Message.MessageType.CLIENT_EVENTS_SUBSCRIBE_REQUEST,
ClientEventsSubscribeRequest.encode({
subscriptions: [blockSub],
lastKnownBlockIds: [lastKnownBlockId]
}).finish()
)
.then(response => ClientEventsSubscribeResponse.decode(response))
.then(decoded => {
const status = _.findKey(ClientEventsSubscribeResponse.Status,
val => val === decoded.status);
if (status !== 'OK') {
throw new Error(`Validator responded with status "${status}"`);
}
});
}
/**
* Unsubscribe to block-commit delta events
* @param {Object} stream1 object to send event unsubscribe message
* @return {void}
*/
function unsubscribe(stream1) {
stream1.send(
Message.MessageType.CLIENT_EVENTS_UNSUBSCRIBE_REQUEST,
ClientEventsUnsubscribeRequest.encode({
}).finish()
)
.then(response => ClientEventsUnsubscribeResponse.decode(response))
.then(decoded => {
const status = _.findKey(ClientEventsUnsubscribeResponse.Status,
val => val === decoded.status);
if (status !== 'OK') {
throw new Error(`Validator responded with status "${status}"`);
}
stream1.close();
return CaliperUtils.sleep(1000);
});
}
/**
* Get batch commit event message on block commit
* @param {String} batchID The ID of a batch
* @param {Number} batchStats Batch status object to update commit status
* @param {Number} timeout The timeout for the execution in millseconds
* @return {Promise<object>} returns batch commit status
*/
async function getBatchEventResponse(batchID, batchStats, timeout) {
try {
const beforeTime = Date.now();
while(batchCommitStatus.get(batchID) === 'pending') {
if((Date.now() - beforeTime) > timeout) {
throw new Error('Timeout, batchID: ' + batchID);
}
await CaliperUtils.sleep(200);
}
batchStats.SetStatusSuccess();
return batchStats;
} catch(err){
logger.info('getBatchEventResponse err: ' + err);
batchStats.SetStatusFail();
return batchStats;
}
}
/**
* Get state according from given address
* @param {String} address Sawtooth address
* @return {Promise<object>} The promise for the result of the execution.
*/
function getState(address) {
let txStatus = new TxStatus(0);
let restApiUrl = getRESTUrl();
const stateLink = restApiUrl + '/state?address=' + address;
let options = {
uri: stateLink
};
return request(options)
.then(function(body) {
let data = (JSON.parse(body)).data;
if (data.length > 0) {
let stateDataBase64 = data[0].data;
let stateDataBuffer = new Buffer(stateDataBase64, 'base64');
let stateData = stateDataBuffer.toString('hex');
txStatus.SetStatusSuccess();
txStatus.SetResult(stateData);
return Promise.resolve(txStatus);
}
else {
throw new Error('no query responses');
}
})
.catch(function (err) {
logger.error('Query failed, ' + (err.stack?err.stack:err));
return Promise.resolve(txStatus);
});
}
/**
* Query state according to given address
* @param {object} context Sawtooth context
* @param {string} contractID The identity of the smart contract.
* @param {string} contractVer The version of the smart contract.
* @param {string} address Lookup address
* @param {string} workspaceRoot the workspace root
* @return {Promise<object>} The promise for the result of the execution.
*/
function querybycontext(context, contractID, contractVer, address, workspaceRoot) {
let config = require(configPath);
const builder = BatchBuilderFactory.getBatchBuilder(contractID, contractVer, config, workspaceRoot);
const addr = builder.calculateAddress(address);
if(context.engine) {
context.engine.submitCallback(1);
}
return getState(addr);
}
/**
* Submit a batch of transactions
* @param {Number} block_num of batches
* @param {Object} batchBytes batch bytes
* @param {Number} timeout The timeout to set for the execution in seconds
* @return {Promise<object>} The promise for the result of the execution.
*/
async function submitBatches(block_num, batchBytes, timeout) {
let txStatus = new TxStatus(0);
let restApiUrl = getRESTUrl();
const request = require('request-promise');
let options = {
method: 'POST',
url: restApiUrl + '/batches',
body: batchBytes,
headers: {'Content-Type': 'application/octet-stream'}
};
return request(options)
.then(function (body) {
let batchId = (JSON.parse(body).link.split('id='))[1];
if(batchCommitStatus.get(batchId) !== 'success') {
batchCommitStatus.set(batchId,'pending');
}
let txnStatus = getBatchEventResponse(batchId, txStatus, timeout);
return Promise.resolve(txnStatus);
})
.catch(function (err) {
logger.error('Submit batches failed, ' + err);
txStatus.SetStatusFail();
return Promise.resolve(txStatus);
});
}
/**
* Sawtooth class, which implements the caliper's NBI for hyperledger sawtooth lake
*/
class Sawtooth extends BlockchainInterface {
/**
* Constructor
* @param {String} config_path path of the Sawtooth configuration file
* @param {string} workspace_root The absolute path to the root location for the application configuration files.
*/
constructor(config_path, workspace_root) {
super(config_path);
configPath = config_path;
this.bcType = 'sawtooth';
this.workspaceRoot = workspace_root;
}
/**
* Initialize the {Sawtooth} object.
* Nothing to do now
* @return {Promise} The return promise.
*/
init() {
// todo: sawtooth
return Promise.resolve();
}
/**
* Deploy the chaincode specified in the network configuration file to all peers.
* Not supported now
* @return {Promise} The return promise.
*/
installSmartContract() {
// todo:
return Promise.resolve();
}
/**
* Return the Sawtooth context
* Nothing to do now
* @param {string} name Unused.
* @param {object} args Unused.
* @return {Promise} The return promise.
*/
getContext(name, args) {
let config = require(this.configPath);
let context = config.sawtooth.context;
if(typeof context === 'undefined') {
let validatorUrl = config.sawtooth.network.validator.url;
if(validatorUrl === null) {
logger.error('Error: Validator url is missing!!!');
}
let stream = new Stream(validatorUrl);
stream.connect(() => {
subscribe(stream);
stream.onReceive(handleEvent);
});
context = {stream: stream};
}
return Promise.resolve(context);
}
/**
* Release the Sawtooth context
* @param {object} context Sawtooth context to be released.
* @return {Promise} The return promise.
*/
releaseContext(context) {
return unsubscribe(context.stream);
}
/**
* Invoke the given smart contract according to the specified options.
* @param {object} context Sawtooth context
* @param {string} contractID The identity of the contract.
* @param {string} contractVer The version of the contract.
* @param {Array} args array of JSON formatted arguments for multiple transactions
* @param {number} timeout The timeout to set for the execution in seconds.
* @return {Promise<object>} The promise for the result of the execution.
*/
async invokeSmartContract(context, contractID, contractVer, args, timeout) {
try {
let config = require(configPath);
let builder = BatchBuilderFactory.getBatchBuilder(contractID, contractVer, config, this.workspaceRoot);
const batchBytes = builder.buildBatch(args);
if(context.engine) {
context.engine.submitCallback(args.length);
}
//Get the next block number and status of block to pending
if(currentBlockNum === 0) {
await getCurrentBlockId().then(block_num => {
currentBlockNum = block_num+1;
});
}
else {
currentBlockNum = currentBlockNum +1;
}
let batchStats = await submitBatches(currentBlockNum, batchBytes, timeout * 1000);
let txStats = [];
for(let i = 0 ; i < args.length ; i++) {
let cloned = Object.assign({}, batchStats);
Object.setPrototypeOf(cloned, TxStatus.prototype);
txStats.push(cloned);
}
return txStats;
} catch (err) {
logger.error('invokeSmartContract failed, ' + err);
let txStats = [];
for(let i = 0 ; i < args.length ; i++) {
let txStatus = new TxStatus(0);
txStatus.SetStatusFail();
txStats.push(txStatus);
}
return txStats;
}
}
/**
* Query state according to given name
* @param {object} context Sawtooth context
* @param {string} contractID The identity of the smart contract.
* @param {string} contractVer The version of the smart contract.
* @param {string} queryName Lookup name
* @return {Promise<object>} The promise for the result of the execution.
*/
queryState(context, contractID, contractVer, queryName) {
return querybycontext(context, contractID, contractVer, queryName, this.workspaceRoot);
}
/**
* Calculate basic statistics of the execution results.
* Nothing to do now.
* @param {object} stats Unused.
* @param {object[]} results Unused.
*/
getDefaultTxStats(stats, results) {
// nothing to do now
}
}
module.exports = Sawtooth;