src/fabric/e2eUtils.js
/**
 * Submit a query to the given chaincode with the specified options.
 * @param {object} context The Fabric context.
 * @param {string} id The name of the chaincode.
 * @param {string} version The version of the chaincode.
 * @param {string} name The single argument to pass to the chaincode.
 * @return {Promise<object>} The result and stats of the transaction invocation.
 */
Submit a query to the given chaincode with the specified options.
@param {object} context The Fabric context.
@param {string} id The name of the chaincode.
@param {string} version The version of the chaincode.
@param {string} name The single argument to pass to the chaincode.
@return {Promise} The result and stats of the transaction invocation.
Submit a query to the given chaincode with the specified options . @param { object } context The Fabric context . @param { string } id The name of the chaincode . @param { string } version The version of the chaincode . @param { string } name The single argument to pass to the chaincode . @return { Promise } The result and stats of the transaction invocation .
function querybycontext(context, id, version, name) {
    const client = context.client;
    const channel = context.channel;
    const tx_id = client.newTransactionID();
    const txStatus = new TxStatus(tx_id.getTransactionID());
    let all_idx = [];
    let len = channel.getPeers().length;
    for (var i = 0;i < len; i++) {all_idx.push(i); }
    let selected_idx = commUtils.shuffle(all_idx).slice(0, 1);
    let target_peers = [];
    selected_idx.forEach((idx)=>{
        target_peers.push(channel.getPeers()[idx].getPeer());
    });
    const request = {
        targets: target_peers,
        chaincodeId: id,
        chaincodeVersion: version,
        txId: tx_id,
        fcn: 'query',
        args: [name]
    };
    return channel.queryByChaincode(request)
        .then((responses) => {
            if(responses.length > 0) {
                const value = responses[0];
                if(value instanceof Error) {
                    throw value;
                }
                for(let i = 1 ; i < responses.length ; i++) {
                    if(responses[i].length !== value.length || !responses[i].every(function(v,idx){
                        return v === value[idx]; })) {
                        throw new Error('conflicting query responses');
                    }
                }
                txStatus.SetStatusSuccess();
                txStatus.SetVerification(true);
                txStatus.SetResult(responses[0]);
                return Promise.resolve(txStatus);
            }
            else {
                throw new Error('no query responses');
            }
        })
        .catch((err) => {
            txStatus.SetStatusSuccess();
            txStatus.SetVerification(true);
            return Promise.resolve(txStatus);
        });
}
function querybycontext ( context , id , version , name ) { const client = context . client ; const channel = context . channel ; const tx_id = client . newTransactionID ( ) ; const txStatus = new TxStatus ( tx_id . getTransactionID ( ) ) ; let all_idx = [ ] ; let len = channel . getPeers ( ) . length ; for ( var i = 0 ; i < len ; i ++ ) { all_idx . push ( i ) ; } let selected_idx = commUtils . shuffle ( all_idx ) . slice ( 0 , 1 ) ; let target_peers = [ ] ; selected_idx . forEach ( ( idx ) => { target_peers . push ( channel . getPeers ( ) [ idx ] . getPeer ( ) ) ; } ) ; const request = { targets : target_peers , chaincodeId : id , chaincodeVersion : version , txId : tx_id , fcn : 'query' , args : [ name ] } ; return channel . queryByChaincode ( request ) . then ( ( responses ) => { if ( responses . length > 0 ) { const value = responses [ 0 ] ; if ( value instanceof Error ) { throw value ; } for ( let i = 1 ; i < responses . length ; i ++ ) { if ( responses [ i ] . length !== value . length || ! responses [ i ] . every ( function ( v , idx ) { return v === value [ idx ] ; } ) ) { throw new Error ( 'conflicting query responses' ) ; } } txStatus . SetStatusSuccess ( ) ; txStatus . SetVerification ( true ) ; txStatus . SetResult ( responses [ 0 ] ) ; return Promise . resolve ( txStatus ) ; } else { throw new Error ( 'no query responses' ) ; } } ) . catch ( ( err ) => { txStatus . SetStatusSuccess ( ) ; txStatus . SetVerification ( true ) ; return Promise . resolve ( txStatus ) ; } ) ; }
Submit a query to the given chaincode with the specified options.
Submit a query to the given chaincode with the specified options .
//const userOrg = context.org; //const eventhubs = context.eventhubs; // txStatus.Set('operation', 'query'); // let selected_idx = commUtils.shuffle(all_idx).slice(0, len / 2 + 1); // console.log("Selected Idx: ", selected_idx, " with # peer ", target_peers.length); // send query // if(context.engine) { //     context.engine.submitCallback(1); // } // commUtils.log('Query failed, ' + (err.stack?err.stack:err)); // txStatus.SetStatusFail();