Interact with "fabcar" Chaincode on CLI

Q

How to Interact "fabcar" Chaincode on CLI?

✍: FYIcenter.com

A

After you started the WYFA network properly, you will have the "fabcar" chaincode running on "mychannel" on the peer0.org1.example.com peer container.

You can now interact with the "fabcar" chaincode from the CLI container as shown below.

1. Connect to CLI container:

$ docker exec -it cli bash
root@262918fdcbc0:/opt/gopath/src/github.com/hyperledger/fabric/peer# cd
root@262918fdcbc0:~# 

2. Make sure that the "fabcar" chaincode is instantiated:

root@262918fdcbc0:~# peer chaincode list --instantiated -C mychannel
Get instantiated chaincodes on channel mychannel:
Name: fabcar, Version: 1.0, Path: github.com/fabcar/go, Escc: escc, Vscc: vscc

3. Try the query method on the chaincode:

root@262918fdcbc0:~# peer chaincode query -C mychannel -n fabcar -c '{"Args":[]}'
Error: endorsement failure during query. response: status:500 
message:"Invalid Smart Contract function name."

The error is expected, since the chaincode does not implement the "Query" method.

4. Try the invoke method on the chaincode:

root@262918fdcbc0:~# peer chaincode invoke -C mychannel -n fabcar -c '{"Args":[]}'
[chaincodeCmd] InitCmdFactory -> INFO 001 Retrieved channel (mychannel) orderer endpoint: orderer.example.com:7050
Error: endorsement failure during invoke. response: status:500 message:"Invalid Smart Contract function name."

This error is also expected, since chaincode's Invoke method requires the function name to perform a pre-defined task.

5. Try the invoke method with initLedger function on the chaincode:

root@262918fdcbc0:~# peer chaincode invoke -C mychannel -n fabcar -c '{"function":"initLedger","Args":[]}'
[chaincodeCmd] InitCmdFactory -> INFO 001 Retrieved channel (mychannel) orderer endpoint: orderer.example.com:7050
[chaincodeCmd] chaincodeInvokeOrQuery -> INFO 002 Chaincode invoke successful. result: status:200

The chaincode executed the initLedger and returned a successful code.

6. Try the invoke method with queryAllCars function on the chaincode:

root@262918fdcbc0:~# peer chaincode invoke -C mychannel -n fabcar -c '{"function":"queryAllCars","Args":[]}'
[chaincodeCmd] InitCmdFactory -> INFO 001 Retrieved channel (mychannel) orderer endpoint: orderer.example.com:7050
[chaincodeCmd] chaincodeInvokeOrQuery -> INFO 002 Chaincode invoke successful. result: status:200 \
payload:"[
{\"Key\":\"CAR0\", \"Record\":{\"colour\":\"blue\",\"make\":\"Toyota\",\"model\":\"Prius\",\"owner\":\"Tomoko\"}},
{\"Key\":\"CAR1\", \"Record\":{\"colour\":\"red\",\"make\":\"Ford\",\"model\":\"Mustang\",\"owner\":\"Brad\"}},
{\"Key\":\"CAR2\", \"Record\":{\"colour\":\"green\",\"make\":\"Hyundai\",\"model\":\"Tucson\",\"owner\":\"Jin Soo\"}},
...
]"

The chaincode executed the queryAllCars and returned a payload message.

 

Verify World State in CouchDB

fabcar.go - The "fabcar" Chaincode

WYFA (Writing Your First Application)

⇑⇑ Hyperledger Tutorials

2020-02-20, 1872🔥, 0💬