We have our Hyperledger Fabric Network bootstrapped now. There is a channel, peers have joined that channel, chaincode is installed and instantiated. As part of instantiation we provided the name of the entities and their initial values. We should be able to query
the values of an entity say the entity a
.
The steps to perform query of the data in the ledger are done in src/query-chaincode.ts script. Here is the relevant code snippet that shows the important steps:
async function queryChaincode(org: Organization) {
const client = await getClient(org);
const channel = await getChannel(client, org);
console.log(`Quering the Chaincode on the peers of ${org} ..`);
const response = await channel.queryByChaincode({
chaincodeId: config.CHAIN_CODE_ID,
fcn: 'query',
args: ["a"],
txId: client.newTransactionID()
});
console.log(`Peer0 of ${org} has ${response[0].toString('utf8')} as the current value for 'a'..`);
console.log(`Peer1 of ${org} has ${response[1].toString('utf8')} as the current value for 'a'..`);
}
async function main() {
console.log('############ ORG1 ###################');
await queryChaincode(Organization.ORG1);
console.log('############ ORG2 ###################');
await queryChaincode(Organization.ORG2);
console.log('############ ORG3 ###################');
await queryChaincode(Organization.ORG3);
}
main();
To run this script issue following command:
npm run query-chaincode
You should following output:
$ npm run query-chaincode
> hyperledger-fabric-example@0.1.0 query-chaincode /Users/ksachdeva/Desktop/Dev/myoss/hyperledger-fabric-example
> ts-node src/query-chaincode.ts
############ ORG1 ###################
Setting up the cryptoSuite ..
Setting up the keyvalue store ..
Creating the admin user context ..
Creating a Channel object ..
Specifiying the orderer to connect to ..
Getting the peers ..
Initializing the channel ..
Quering the Chaincode on the peers of org1 ..
Peer0 of org1 has 100 as the current value for 'a'..
Peer1 of org1 has 100 as the current value for 'a'..
############ ORG2 ###################
Setting up the cryptoSuite ..
Setting up the keyvalue store ..
Creating the admin user context ..
Creating a Channel object ..
Specifiying the orderer to connect to ..
Getting the peers ..
Initializing the channel ..
Quering the Chaincode on the peers of org2 ..
Peer0 of org2 has 100 as the current value for 'a'..
Peer1 of org2 has 100 as the current value for 'a'..
############ ORG3 ###################
Setting up the cryptoSuite ..
Setting up the keyvalue store ..
Creating the admin user context ..
Creating a Channel object ..
Specifiying the orderer to connect to ..
Getting the peers ..
Initializing the channel ..
Quering the Chaincode on the peers of org3 ..
Peer0 of org3 has 100 as the current value for 'a'..
Peer1 of org3 has 100 as the current value for 'a'..
Above execution of src/query-chaincode.ts shows that the value of a
in the ledgers of all the peers of all participating organizations is 100
which is the value that we provided at the time of instantiation of the chaincode.
What we would like to do next is to exercise the business logic of ‘moving/transferring’ some value from entity a
to entity b
. Below is shown the src/invoke-transaction.ts script that shows the necessary steps to perform this operation.
async function invokeTransactionOnPeers(org: Organization) {
const client = await getClient(org);
const orderer = await getOrderer(client);
console.log('Creating a Channel object ..');
const channel = client.newChannel(config.CHANNEL_NAME);
console.log('Specifying the orderer to connect to ..');
channel.addOrderer(orderer);
console.log('Getting the peers ..');
const peers = await getPeers(client, org);
peers.map(p => channel.addPeer(p));
console.log('Initializing the channel ..');
await channel.initialize();
console.log('Sending the Invoke Proposal ..');
const proposalResponse = await channel.sendTransactionProposal({
chaincodeId: config.CHAIN_CODE_ID,
fcn: 'move',
args: ["a", "b", "10"],
txId: client.newTransactionID()
});
console.log('Sending the Transaction ..');
const transactionResponse = await channel.sendTransaction({
proposalResponses: proposalResponse[0],
proposal: proposalResponse[1]
});
}
async function main() {
await invokeTransactionOnPeers(Organization.ORG1);
}
main();
You can run the script by issuing command:
npm run invoke-transaction
Pay attention to the following part:
console.log('Sending the Invoke Proposal ..');
const proposalResponse = await channel.sendTransactionProposal({
chaincodeId: config.CHAIN_CODE_ID,
fcn: 'move',
args: ["a", "b", "10"],
txId: client.newTransactionID()
});
console.log('Sending the Transaction ..');
const transactionResponse = await channel.sendTransaction({
proposalResponses: proposalResponse[0],
proposal: proposalResponse[1]
});
- See that the name of the function is
move
. - We are transferring a value of
10
froma
tob
. - The execution of
sendTransactionProposal
is returning the proposal endorsed by the peers. - The execution of
sendTransaction
is what is finally making it commit to the various ledgers in the network.
You should also note that unlike other scripts that we used in previous blog posts we are performing this transaction only using the client of one organization.
Time for the moment of truth -:
Even though we executed the transaction on the peers of organization 1, we are expecting that the ledgers of other peers of other organizations are updated as well i.e. the new value of entity a
on all the ledgers of all the peers should now be 90
(i.e. 100 - 10).
Run following command to query the chaincode:
npm run query-chaincode
and the output should look like
$ npm run query-chaincode
> hyperledger-fabric-example@0.1.0 query-chaincode /Users/ksachdeva/Desktop/Dev/myoss/hyperledger-fabric-example
> ts-node src/query-chaincode.ts
############ ORG1 ###################
Setting up the cryptoSuite ..
Setting up the keyvalue store ..
Creating the admin user context ..
Creating a Channel object ..
Specifiying the orderer to connect to ..
Getting the peers ..
Initializing the channel ..
Quering the Chaincode on the peers of org1 ..
Peer0 of org1 has 90 as the current value for 'a'..
Peer1 of org1 has 90 as the current value for 'a'..
############ ORG2 ###################
Setting up the cryptoSuite ..
Setting up the keyvalue store ..
Creating the admin user context ..
Creating a Channel object ..
Specifiying the orderer to connect to ..
Getting the peers ..
Initializing the channel ..
Quering the Chaincode on the peers of org2 ..
Peer0 of org2 has 90 as the current value for 'a'..
Peer1 of org2 has 90 as the current value for 'a'..
############ ORG3 ###################
Setting up the cryptoSuite ..
Setting up the keyvalue store ..
Creating the admin user context ..
Creating a Channel object ..
Specifiying the orderer to connect to ..
Getting the peers ..
Initializing the channel ..
Quering the Chaincode on the peers of org3 ..
Peer0 of org3 has 90 as the current value for 'a'..
Peer1 of org3 has 90 as the current value for 'a'..