minifabric icon indicating copy to clipboard operation
minifabric copied to clipboard

Create a chaincode methods returning all transactions

Open alphacybercom opened this issue 3 years ago • 3 comments

I'm trying to create a chaincode method able to return all transactions history, hence, all objects stored on the blockchain.

Here is the sample method.

async queryall(stub, args) {
     if ((args.length != 0) && (args.length != 2)) {
       throw new Error('Incorrect number of arguments. Expecting no args (returning all objects), or start+end strings used by the filtering engine.')
     }
     const allResults = [];
     // range query with empty string for startKey and endKey does an open-ended query of all assets in the chaincode namespace. => see https://hyperledger-fabric.readthedocs.io/en/release-2.2/chaincode4ade.html
     let startFilter = '';
     let endFilter = '';
     if (args.length == 2) {
       startFilter = args[0];
       endFilter = args[1];
     }
     console.info(util.format('Filtering from %s to %s\n', startFilter, endFilter));
     const iterator = await stub.getStateByRange(startFilter.toString(), endFilter.toString());
     let result = await iterator.next();
     while (!result.done) {
         const strValue = Buffer.from(result.value.value.toString()).toString('utf8');
         let record;
         try {
             record = JSON.parse(strValue);
         } catch (err) {
             console.log(err);
             record = strValue;
         }
         console.info(util.format('Found record = %s\n', record));
         allResults.push({ Key: result.value.key, Record: record });
         result = await iterator.next();
     }
     console.info(util.format('returning %s\n', allResults));
     return JSON.stringify(allResults);
 }

Nevertheless, the adopted method does not work and nothing is returned.

alphacybercom avatar Sep 30 '21 09:09 alphacybercom

This is an client app issue. Minifabric cannot do any thing to help. Minifabric already has blockquery command to return txs ina block, please try the command to see returned txs. This issue should be closed.

litong01 avatar Oct 07 '21 12:10 litong01

Thank you for your reply. Nevertheless, it's not clear to me (1) how to trigger the block query command from minifabric, and (2) how to integrate it on my chaincode.

alphacybercom avatar Oct 08 '21 08:10 alphacybercom

This is an client app issue. Minifabric cannot do any thing to help. Minifabric already has blockquery command to return txs ina block, please try the command to see returned txs. This issue should be closed.

@litong01 after Composer, there is some frame-work for recommend to help build client app ?

i know minifab resolve easy manage Fabric network ..etc., but we need next tools for easy manage applications API for others web client app.

ZoomQuiet avatar Oct 29 '21 09:10 ZoomQuiet