From 01c4e3c506aa6fd09472d53ed0cd4af2a364d034 Mon Sep 17 00:00:00 2001 From: "nkl199@yahoo.co.uk" Date: Mon, 9 Sep 2019 15:18:05 +0100 Subject: [PATCH] add a query smart contract method Signed-off-by: nkl199@yahoo.co.uk --- .../caliper-core/lib/blockchain-interface.js | 14 +++++++- packages/caliper-core/lib/blockchain.js | 33 ++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/packages/caliper-core/lib/blockchain-interface.js b/packages/caliper-core/lib/blockchain-interface.js index 4be10dae5..afe5dfe51 100644 --- a/packages/caliper-core/lib/blockchain-interface.js +++ b/packages/caliper-core/lib/blockchain-interface.js @@ -81,12 +81,24 @@ class BlockchainInterface { * @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 + * @param {Number} timeout request timeout, in seconds */ async invokeSmartContract(context, contractID, contractVer, args, timeout) { throw new Error('invokeSmartContract is not implemented for this blockchain system'); } + /** + * 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 + */ + async querySmartContract(context, contractID, contractVer, args, timeout) { + throw new Error('querySmartContract is not implemented for this blockchain system'); + } + /** * Query state from the ledger * @param {Object} context context object from getContext diff --git a/packages/caliper-core/lib/blockchain.js b/packages/caliper-core/lib/blockchain.js index f003beffa..057c7996d 100644 --- a/packages/caliper-core/lib/blockchain.js +++ b/packages/caliper-core/lib/blockchain.js @@ -114,6 +114,37 @@ class Blockchain { 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 identiy 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 @@ -121,7 +152,7 @@ class Blockchain { * @param {String} contractVer version of the contract * @param {String} key lookup key * @param {String=} [fcn] query function name - * @return {Promise} as invokeSmateContract() + * @return {Object} as invokeSmateContract() */ async queryState(context, contractID, contractVer, key, fcn) { return await this.bcObj.queryState(context, contractID, contractVer, key, fcn);