Skip to content

Commit

Permalink
Merge pull request #578 from nklincoln/add-querysmartcontract
Browse files Browse the repository at this point in the history
add a query smart contract method
  • Loading branch information
aklenik authored Sep 11, 2019
2 parents c9dbfef + 01c4e3c commit e1db1b5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
14 changes: 13 additions & 1 deletion packages/caliper-core/lib/blockchain-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 32 additions & 1 deletion packages/caliper-core/lib/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,45 @@ 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
* @param {String} contractID identiy of the contract
* @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);
Expand Down

0 comments on commit e1db1b5

Please sign in to comment.