Query qscc system chaincodes using NODE.js


David F. D. Reis
 

Hi.

I am querying qscc chaincode using Node.js but I am unable to find more information in official documentation.

To query the block number and get channel info, I am using the following code (test purposes only):

import { Gateway, Wallets } from 'fabric-network';
import * as common from 'fabric-common';
import * as fabproto6 from 'fabric-protos';
import * as path from 'path';
import * as fs from 'fs';
import { createQueryHandler } from './config/queryhandler/QueryHandler';

//const BlockDecoder = (common as any).BlockDecoder;

async function main() {
  try {
    // Create a new file system based wallet for managing identities.
    const walletPath = path.join(process.cwd(), 'Org1Wallet');
    const wallet = await Wallets.newFileSystemWallet(walletPath);
    console.log(`Wallet path: ${walletPath}`);
    // Create a new gateway for connecting to our peer node.
    const gateway = new Gateway();
    const connectionProfilePath = path.resolve(__dirname, '..', 'connection.json');
    const connectionProfile = JSON.parse(fs.readFileSync(connectionProfilePath, 'utf8'));
    const connectionOptions = {
      wallet,
      identity: 'Org1 Admin',
      discovery: { enabled: true, asLocalhost: true },
      queryHandlerOptions: {
        timeout: 10, // timeout in seconds
        strategy: createQueryHandler,
      },
    };
    await gateway.connect(connectionProfile, connectionOptions);

    // Get the network (channel) our contract is deployed to.
    const network = await gateway.getNetwork('mychannel');

    // Get the contract from the network.
    const contract = network.getContract('qscc');

    // Submit the specified transaction.
    const currentTimeBefore = new Date().getMilliseconds();

    const result = await contract.evaluateTransaction('GetChainInfo', 'mychannel');

    const blockProto = JSON.stringify(fabproto6.common.BlockchainInfo.decode(result));

    console.log(`# GetChainInfo: ${blockProto} \n`);

    const resultGetBlockByNumber = await contract.evaluateTransaction('GetBlockByNumber', 'mychannel', '5');

    //const resultDecoded = JSON.stringify(BlockDecoder.decode(resultGetBlockByNumber));

    const resultDecoded = JSON.stringify(fabproto6.common.Block.decode(resultGetBlockByNumber));

    const currentTimeAfter = new Date().getMilliseconds();

    console.log(`# GetBlockByNumber: ${resultDecoded} \n`);

    const totalRunTime = (currentTimeAfter - currentTimeBefore) / 1000;

    console.log(`#==> Totaltime: ${totalRunTime} seconds.`);

    // Disconnect from the gateway.
    gateway.disconnect();
  } catch (error) {
    if (error instanceof Error) {
      console.error(error.stack);
    }
    console.error('RED ALERT, SHIELDS UP, PHASER CHARGED:', error);

    process.exit(1);
  }
}

main();



I am doing it right?
GetChainInfo decode:
const blockProto = JSON.stringify(fabproto6.common.BlockchainInfo.decode(result));

GetBlockByNumber:
const resultDecoded = JSON.stringify(fabproto6.common.Block.decode(resultGetBlockByNumber));

I tried to import the BlockDecoder class (https://github.com/hyperledger/fabric-sdk-node/blob/main/fabric-common/lib/BlockDecoder.js) but the type definition is missing, so I used fabric_protos lib directly.

Thanks in advance,


David



--
David Reis
@davidfaulstich (Instagram)
@davidfdr (Github)


Join fabric@lists.hyperledger.org to automatically receive all group messages.