1

I am trying to get the operation which is responsible for the new state. In simple words, I want to get the function name passed to the invoke method. When I am looking into the historical states I am able to fetch its value, timestamp, transaction ID, etc. but not the parameters which are responsible for that transaction.

I know we can get the parameters from the block history, but I need it for the states.

Is there any way to achieve this from the chaincode side?

Paradox
  • 327
  • 3
  • 19

2 Answers2

0

Yes, from the chaincode you can create an Invoke endpoint and do like:

func (s *SmartContract) Invoke(APIstub shim.ChaincodeStubInterface) sc.Response {
function, args := APIstub.GetFunctionAndParameters()
}

Here you got everything you want. For example if you print function you will see the name of the function you invoked.

Riki95
  • 706
  • 1
  • 7
  • 16
0

I would like to know about your approach as well. However this would be my take on this,we can probably register an event listener in our sdk (using registerChaincodeEvent method) and create an Event (using setEvent method) in our chaincode ,wherein our payload would be the result from getFunctionAndParameters method. And you can probably keep storing these results into a datastore to later fetch them all.

Alternatively: You can simply send back the result of getFunctionAndParameters as a key in the response object via shim.success.

Udyan Sharma
  • 134
  • 7
  • This is my approach: 1) We'll get the transaction ID for the state change 2) Find the block with the transaction 3) Get the parameters from the block – Paradox Dec 06 '19 at 10:43
  • For step 1, if I am not wrong you might have registered an event for each transaction, based on a tx_id using the registerTxEvent ? If yes, then you can possibly try the approach I suggested for chaincode Events – Udyan Sharma Dec 07 '19 at 14:23
  • No, I am getting the txID from the state itself – Paradox Dec 09 '19 at 09:06
  • If I am not wrong, then you are probably using the getHistoryForKey. Then [https://godoc.org/github.com/hyperledger/fabric-protos-go/ledger/queryresult#KeyModification](this) is the response proto for each object in the iterator, and hence I think there is no way you can get it from that API and IMHO I think chaincode event is your way to achieve this. – Udyan Sharma Dec 09 '19 at 09:45