Invoke and Query

There are two useful commands to trigger the chaincode execution from the CLI: peer chaincode invoke and peer chaincode query.

The peer chaincode invoke command invokes the specified chaincode and tries to commit the endorsed transaction to the network. Once the transaction is successfully submitted, the ledger update occurs.

The peer chaincode query command returns an endorsed result of the chaincode function call, but it does not generate a transaction. No changes or information about the chaincode call will be reflected in the ledger if query is used. Thus, if you want to keep track of all chaincode calls, you should use invoke even for read-only functions.

A common structure of the peer chaincode invoke and query commands is pretty similar:

peer chaincode invoke/query -n chaincode -C mychannel -c '{"function":"func", "Args":["arg1","arg2"]}' -o orderer.example.com:7050

where:

  • -n, or --name, is the name of the chaincode.

  • -C, or --channelID, is the channel name.

  • -c, or --ctor, is a JSON object containing the function name and arguments to pass into the chaincode Init function.

  • -o, or --orderer, is an ordering service endpoint.

Usually, it is necessary to collect a sufficient number of endorsements from channel members to be able to commit a transaction result to the ledger. To do this, we can target multiple peers during the invocation using the --peerAddresses argument multiple times.

peer chaincode invoke -n chaincode -C channel -c '{"function":"func", "Args":["arg1","arg2"]}' -o orderer.example.com:7050 --peerAddresses peer0.org1.example.com:7051 --peerAddresses peer0.org2.example.com:9051

Last updated

Was this helpful?