Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a query smart contract method #578

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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