Skip to content

Commit

Permalink
define common TxStatus Class
Browse files Browse the repository at this point in the history
Signed-off-by: Haojun Zhou <[email protected]>
  • Loading branch information
Haojun Zhou committed Jul 10, 2018
1 parent 50cbf67 commit c443e70
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 274 deletions.
21 changes: 6 additions & 15 deletions benchmark/composer/composer-micro/query-asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
'use strict';

const Util = require('../../../src/comm/util');
const TxStatus = require('../../../src/comm/transaction');

module.exports.info = 'Query Asset Performance Test';

Expand Down Expand Up @@ -80,29 +81,19 @@ module.exports.init = async function(blockchain, context, args) {
};

module.exports.run = function() {
let invoke_status = {
id : qryRef++,
status : 'created',
time_create : Date.now(),
time_final : 0,
time_endorse : 0,
time_order : 0,
result : null
};
let invoke_status = new TxStatus(qryRef++);

// use the pre-compiled query named 'selectAllCarsByColour' that is within the business
// network queries file
return busNetConnection.query('selectAllCarsByColour', { colour: matchColor})
.then((result) => {
invoke_status.status = 'success';
invoke_status.time_final = Date.now();
invoke_status.result = result;
invoke_status.SetStatusSuccess();
invoke_status.SetResult(result);
return Promise.resolve(invoke_status);
})
.catch((err) => {
invoke_status.time_final = Date.now();
invoke_status.status = 'failed';
invoke_status.result = [];
invoke_status.SetStatusFail();
invoke_status.SetResult([]);
return Promise.resolve(invoke_status);
});
};
Expand Down
2 changes: 1 addition & 1 deletion benchmark/drm/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports.run = function() {
module.exports.end = function(results) {
for (let i in results){
let stat = results[i];
if(stat.status === 'success') {
if(stat.IsCommitted()) {
ids.push(stat.result.toString());
}
}
Expand Down
16 changes: 3 additions & 13 deletions src/comm/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,6 @@ class Blockchain {

/**
* Invoke smart contract/submit transactions and return corresponding transactions' status
* txStatus = {
* '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(ms) that the transaction was created
* 'time_final': time(ms) that the transaction was known to be final and committed in ledger
* 'result': response payloads of the transaction request
* ...... : other adapter specific properties
* }
* @param {Object} context context object
* @param {String} contractID identiy of the contract
* @param {String} contractVer version of the contract
Expand Down Expand Up @@ -162,7 +152,7 @@ class Blockchain {
let delays = [];
for(let i = 0 ; i < results.length ; i++) {
let stat = results[i];
let create = stat.time_create;
let create = stat.GetTimeCreate();

if(typeof minCreate === 'undefined') {
minCreate = create;
Expand All @@ -177,9 +167,9 @@ class Blockchain {
}
}

if(stat.status === 'success') {
if(stat.IsCommitted()) {
succ++;
let final = stat.time_final;
let final = stat.GetTimeFinal();
let d = (final - create) / 1000;
if(typeof minFinal === 'undefined') {
minFinal = final;
Expand Down
196 changes: 196 additions & 0 deletions src/comm/transaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
* 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';

/**
* Internal transaction status class for Caliper
*/
class TxStatus {
/**
* Constructor
* @param {string} id, transaction id
*/
constructor(id) {
this.status = {
id: id,
status: 'created', // submitting status, three status 'created', 'success', 'failed' are reserved
time_create: Date.now(),
time_final: 0,
result: null,
verified: false, // if false, we cannot be sure that the final Tx status is accurate
flags: 0, // the blockchain specified flag
error_messages: [] // the blockchain specified error messages
};
}

/**
* Getter of the tx's id
* @return {string}, id
*/
GetID() {
return this.status.id;
}

/**
* Setter of the tx's id
* @param {string} id, id
*/
SetID(id) {
this.status.id = id;
}

/**
* Getter of the tx's status
* @return {string}, status
*/
GetStatus() {
return this.status.status;
}

/**
* Check if the tx has been committed succesfully
* @return {boolean} committed or not
*/
IsCommitted() {
return (this.status.status === 'success');
}

/**
* Set the tx's status to 'success'
* The 'time_final' will also be recorded
*/
SetStatusSuccess() {
this.status.status = 'success';
this.status.time_final = Date.now();
}

/**
* Getter of the tx's creating time
* @return {int} create time in ms
*/
GetTimeCreate() {
return this.status.time_create;
}

/**
* Getter of the tx's final time
* @return {int} final time in ms
*/
GetTimeFinal() {
return this.status.time_final;
}

/**
* Set the tx's status to 'failed'
* The 'time_final' will also be recorded
*/
SetStatusFail() {
this.status.status = 'failed';
this.status.time_final = Date.now();
}

/**
* Check if the tx's status is verified
* @return {boolean}, verified or not
*/
IsVerified() {
return this.status.verified;
}

/**
* Setter of the tx's verification state
* @param {*} isVerified, verified or not
*/
SetVerification(isVerified) {
this.status.verified = isVerified;
}

/**
* Getter of the blockchain sepecified flag
* @return {any}, flag
*/
GetFlag() {
return this.status.flags;
}

/**
* Setter of the blockchain specified flag
* @param {any} flag, flag to be set
*/
SetFlag(flag) {
this.status.flags = flag;
}

/**
* Setter of the error message
* @param {int} idx, index of the error message
* @param {any} msg, message to be stored
*/
SetErrMsg(idx, msg) {
this.status.error_messages[idx] = msg;
}

/**
* Getter of the error messages
* @return {array}, stored messages
*/
GetErrMsg() {
return this.status.error_messages;
}

/**
* Setter of blockchain specified submitting result
* @param {any} result result
*/
SetResult(result) {
this.status.result = result;
}

/**
* Getter of stored submitting result
* @return {any} result
*/
GetResult() {
return this.status.result;
}

/**
* Getter of the status object
* @return {object} status object
*/
Marshal() {
return this.status;
}

/**
* Set any key/value
* @param {string} key key
* @param {any} value value
*/
Set(key, value) {
this.status[key] = value;
}

/**
* Get any specified element
* @param {string} key key
* @return {any} value
*/
Get(key) {
return this.status[key];
}
}

module.exports = TxStatus;
1 change: 1 addition & 0 deletions src/comm/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

'use strict';


/**
* Internal Utility class for Caliper
*/class Util {
Expand Down
72 changes: 4 additions & 68 deletions src/composer/composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// Caliper requires
const BlockchainInterface = require('../comm/blockchain-interface.js');
const Util = require('../comm/util');
const TxStatus = require('../comm/transaction');

// Composer helpers
const composer_utils = require('./composer_utils.js');
Expand Down Expand Up @@ -146,84 +147,19 @@ class Composer extends BlockchainInterface {
* @returns {Promise} a completed Promise containing a result
*/
submitTransaction(connection, transaction) {
let invoke_status = {
id : transaction.getIdentifier(),
status : 'created',
time_create : Date.now(),
time_final : 0,
time_endorse : 0,
time_order : 0,
result : null
};

let invoke_status = new TxStatus(transaction.getIdentifier());
return connection.submitTransaction(transaction)
.then((complete) => {
invoke_status.status = 'success';
invoke_status.time_final = Date.now();
invoke_status.SetStatusSuccess();
return Promise.resolve(invoke_status);
})
.catch((err) => {
invoke_status.time_final = Date.now();
invoke_status.status = 'failed';
invoke_status.SetStatusFail();
invoke_status.result = [];

return Promise.resolve(invoke_status);
});
}

/**
* Concrete implementation of getDefaultTxStats
* @param {*} stats the stats
* @param {*} results the results
*/
getDefaultTxStats(stats, results) {
let minDelayC2E = 100000, maxDelayC2E = 0, sumDelayC2E = 0; // time from created to endorsed
let minDelayE2O = 100000, maxDelayE2O = 0, sumDelayE2O = 0; // time from endorsed to ordered
let minDelayO2V = 100000, maxDelayO2V = 0, sumDelayO2V = 0; // time from ordered to recorded
let hasValue = true;
for(let i = 0 ; i < results.length ; i++) {
let stat = results[i];
if(!stat.hasOwnProperty('time_endorse')) {
hasValue = false;
break;
}
if(stat.status === 'success') {
let delayC2E = stat.time_endorse - stat.time_create;
let delayE2O = stat.time_order - stat.time_endorse;
let delayO2V = stat.time_valid - stat.time_order;

if(delayC2E < minDelayC2E) {
minDelayC2E = delayC2E;
}
if(delayC2E > maxDelayC2E) {
maxDelayC2E = delayC2E;
}
sumDelayC2E += delayC2E;

if(delayE2O < minDelayE2O) {
minDelayE2O = delayE2O;
}
if(delayE2O > maxDelayE2O) {
maxDelayE2O = delayE2O;
}
sumDelayE2O += delayE2O;

if(delayO2V < minDelayO2V) {
minDelayO2V = delayO2V;
}
if(delayO2V > maxDelayO2V) {
maxDelayO2V = delayO2V;
}
sumDelayO2V += delayO2V;
}
}

if(hasValue) {
stats.delayC2E = {'min': minDelayC2E, 'max': maxDelayC2E, 'sum': sumDelayC2E};
stats.delayE2O = {'min': minDelayE2O, 'max': maxDelayE2O, 'sum': sumDelayE2O};
stats.delayO2V = {'min': minDelayO2V, 'max': maxDelayO2V, 'sum': sumDelayO2V};
}
}
}

module.exports = Composer;
Loading

0 comments on commit c443e70

Please sign in to comment.