alwayscurious
alwayscurious

Reputation: 51

hyperledger fabric v1.0 using query on methods that change ledger

While going over v1.0 examples, I ran into a confusion.

As far as I understand, query does not change the ledger as it is executed locally (e.g. no ordering, committing, endorsing involved).

But that is from the caller's point of view. Within chaincode, it just executes whatever the client calls given all of CAs and info are valid.

So for example, if I call

peer chaincode query -C mychannel -n chaincode -c '{"Args":["query", "a"]}'

this would be ok. It just queries a.

But if I call

peer chaincode query -C mychannel -n chaincode -c '{"Args":["**invoke**", "a"]}'

What would be the behavior given that invoke involves writing on the ledger?

Also on the other hand, if I call invoke on query method

(e.g. peer chaincode invoke ~~~ {"Args":["query", "a"]}) 

What would be the behavior?

As far as I understand, actual chaincode cannot distinguish whether it is query or invoke. It just executes the chaincode method.

Am I far off?

Upvotes: 1

Views: 374

Answers (1)

Artem Barger
Artem Barger

Reputation: 41222

Indeed this is a bit confusing, especially taking into account the thing not very obvious when you using the cli tool. Here is the thing, when you are using peer cli command with invoke the flow works as following:

  1. You send transaction proposal to the endorsing peer
  2. Endorsing peers executes transaction simulations and signs the results
  3. Client receives the results and send them out to the ordering service
  4. Ordering service cut the block
  5. Block delivered to the peers
  6. Peers validates transactions
  7. Eventually block appended to the blockchain

Now here is the difference, when you running peer cli with query parameters key it will do only:

  1. You send transaction proposal to the endorsing peer
  2. Endorsing peers executes transaction simulations and signs the results

Therefore, since results is not sent to ordering service there is no effect on the final peer state, since even if you chaincode has made any changes it won't be committed.

Upvotes: 1

Related Questions