Read (query) transaction flow in Hyperledger Fabric

亡梦爱人 提交于 2019-12-04 07:34:33

You might want to take a look at https://github.com/hyperledger/fabric-samples/blob/release/fabcar/query.js which demonstrates how to query using the Node SDK. You'll notice that it uses the convenience method https://fabric-sdk-node.github.io/Channel.html#queryByChaincode__anchor provided by the SDK to help facilitate queries.

In terms of the flow you outlined in your post, steps 1 and 2 are correct.
Additionally, chaincode functions which are intended for query transactions will typically use the https://godoc.org/github.com/hyperledger/fabric/core/chaincode/shim#Success helper function to actually return the result of the query. In the sample I posted above, https://github.com/hyperledger/fabric-samples/blob/release/fabcar/query.js#L51 invokes https://github.com/hyperledger/fabric-samples/blob/release/chaincode/fabcar/fabcar.go#L135.

There is no need to send the responses from a query transaction to the orderer although you can as long as you meet the endorsement policy. Thanks to Dave Enyeart for the following:

A query is a chaincode invocation which reads the ledger current state but does
not write to the ledger. The chaincode function may query certain keys on the ledger,
or may query for a set of keys on the ledger. Since queries do not change ledger state,
the client application will typically not submit these read-only transactions for ordering,
validation, and committal. Although not typical, the client application can choose to
submit the read-only transaction for ordering, validation, and commit, for example if the
client wants auditable proof on the ledger chain that it had knowledge of specific ledger
state at a certain point in time. Peers will validate and add the read-only transaction
to the ledger chain, but there will be no updates to ledger current state.

This seems to contradict what you posted in your reply to this question: Roles (read+write) in hyperledger

The behavior you describe here makes sense, whereas the behavior in the above answer seems totally broken. Yet it seems that readers on a channel are not allowed to perform invokes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!