Install and run test chaincode

Run the following command to install example chaincode on the peer node:

docker exec cli peer chaincode install -n testcc -v v0 -l node -p  /opt/gopath/src/github.com/chaincode_example02/node

Your output should look something like this:

2020-10-16 02:30:19.454 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2020-10-16 02:30:19.454 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2020-10-16 02:30:19.523 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" >

Run the following command to instantiate the chaincode:

docker exec cli peer chaincode instantiate -o $ORDERER -C $TEST_CHANNEL_NAME -n testcc -v v0 -c '{"Args":["init","a","100","b","200"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls

This can take up to a minute to finish, because part of the instantiation process involves booting a container. You should see output like this:

2020-05-11 23:57:53.122 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2020-05-11 23:57:53.122 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc

To verify that the chaincode was instantiated successfully, you can use this command. Sometimes it takes a few seconds for the installed chaincode to show up. If you don’t see it immediately after install, try again.

docker exec cli peer chaincode list --instantiated -o $ORDERER -C $TEST_CHANNEL_NAME --cafile /opt/home/managedblockchain-tls-chain.pem --tls

It should return output like this:

Name: testcc, Version: v0, Path: /opt/gopath/src/github.com/chaincode_example02/node, Escc: escc, Vscc: vscc

Next we’ll query the chaincode using the following command to see that the initial values we specified in our instantiation are what we expect them to be:

docker exec cli peer chaincode query -C $TEST_CHANNEL_NAME -n testcc -c '{"Args":["query","a"]}'

We should expect to get an output of 100, which is the value we initialized a to when the chaincode was instantiated.

Now it’s time to invoke the chaincode and test that it works. In the previous steps, we instantiated the key a with a value of 100 and queried to verify. Using the invoke command in the following example, we remove 10 from that initial value:

docker exec cli peer chaincode invoke -C $TEST_CHANNEL_NAME -n testcc -c  '{"Args":["invoke","a","b","10"]}' -o $ORDERER --cafile /opt/home/managedblockchain-tls-chain.pem --tls

If the command is successful, you should see output like this:

2020-05-12 01:56:22.668 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200

Now we’ll query the chaincode again using the following command:

docker exec cli peer chaincode query -C $TEST_CHANNEL_NAME -n testcc -c '{"Args":["query","a"]}'

The command should return the new value for a, which is 90.