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)



Matthew White
 

Hi, for background interest - could I ask what usecase do you have (or anybody actually) for access the block information. 
 
As to channel info, I believe there are new APIs to get some there, but would need to check.
 
 
Regards, Matthew.
Matthew B White  IBM Blockchain Solutions Architect
 
Email me at WHITEMAT@...
Find me on StackOverflow, and generally at  calanais.me.uk
 
Note: restricted availability for meetings 14:30 to 17:00 UK Tuesday 
IBM United Kingdom Limited, Hursley Park, Winchester, Hampshire, SO21 2JN

"The wrong answers are the ones you go looking for when the right answers stare you in the face"
 
 
 
----- Original message -----
From: "David Faulstich Diniz Reis" <davidfdr@...>
Sent by: fabric@...
To: "fabric" <fabric@...>
Cc:
Subject: [EXTERNAL] [Hyperledger Fabric] Query qscc system chaincodes using NODE.js
Date: Wed, Oct 27, 2021 8:35 PM
 
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 ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.
ZjQcmQRYFpfptBannerEnd
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)
 
 
 
 

Unless stated otherwise above:

IBM United Kingdom Limited - Registered in England and Wales with number 741598.

Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU



David F. D. Reis
 

Hi Matthew.

I have some objectives in my agenda:

1 - Get a deep understanding of Hyperledger Fabric and APIs;
2 - Build some tools to monitor  and check the network usage;
3 - Build some automatization tasks;
4 - In the future, contribute to the community.

About the new APIs that you mentioned, I would love to study it. I did a lot of googling, and decided to learn using the source code because I think that the official documentation isn´t so detailed. 

I am studying some hyperledger projects too:


I am fascinated with Hyperledger Fabric.

I know I have a long way to go but I know I'll get there.

Thank you.

Best regards,

David


Em qui., 28 de out. de 2021 às 05:18, Matthew White <WHITEMAT@...> escreveu:

Hi, for background interest - could I ask what usecase do you have (or anybody actually) for access the block information. 
 
As to channel info, I believe there are new APIs to get some there, but would need to check.
 
 
Regards, Matthew.
Matthew B White  IBM Blockchain Solutions Architect
 
Email me at WHITEMAT@...
Find me on StackOverflow, and generally at  calanais.me.uk
 
Note: restricted availability for meetings 14:30 to 17:00 UK Tuesday 
IBM United Kingdom Limited, Hursley Park, Winchester, Hampshire, SO21 2JN

"The wrong answers are the ones you go looking for when the right answers stare you in the face"
 
 
 
----- Original message -----
From: "David Faulstich Diniz Reis" <davidfdr@...>
Sent by: fabric@...
To: "fabric" <fabric@...>
Cc:
Subject: [EXTERNAL] [Hyperledger Fabric] Query qscc system chaincodes using NODE.js
Date: Wed, Oct 27, 2021 8:35 PM
 
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 ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.
ZjQcmQRYFpfptBannerEnd
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)
 
 
 
 

Unless stated otherwise above:

IBM United Kingdom Limited - Registered in England and Wales with number 741598.

Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU




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