Search code examples
hyperledger-fabrichyperledger-chaincode

How to check if current chaincode execution context is called via invoke or query


Scenario

Hyperledger Fabric v 2.4
Golang

I am developing a chaincode for Car Registration Record management.

There is a function in my chaincode getCarRegistrationDetails(...) which searches for a car based on input data and returns the result. Apart from the car registration record, I am also maintaining a credit value for each user (using client identity). When a user request to query registration details of a car, some credit amount is deducted from the user's balance and the car registration record is returned to the user.

Problem

Since I am updating the state in getCarRegistrationDetails(...), it should be invoked. But the problem is that when I call the same function using query it returns the required details but do not deduct from user's balance.

How can I handle this problem in chaincode. More specifically, is there a way to prevent query requests on getCarRegistrationDetails(...) and only allow invoke?


Solution

  • Unfortunately chaincode cannot make that decision. Fundamentally the difference between a client using invoke vs query (in gateway api terms this would be submit vs evaluate) is whether the proposal response (and associated signatures) are then sent to an orderer (invoke) or just the payload is returned to the caller (query), but both an invoke and a query will send the same endorsement request to a peer which is then executed by chaincode.

    The way you would handle this is within your client application. Your client application knows which methods should be invoked/submitted and which should be queried/evaluated