1

What is the equivalent of Init function in Java chaincode? I have been looking at this documentation but am unable to find out what function will be called when I run peer chain code instantiate...

This is the go variation.

func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response {
}

I am looking for an equivalent or a way to initialize the ledger when deploying java chaincode. My Chaincode is implementing the ContractInterface.

Sanjay S B
  • 259
  • 2
  • 15
  • If you are using the ContractInterface then you don't have the Init or Invoke entry points. The contract implementation handles these for you. If you want to use the non contract interface then the answer given below by @harshit provides a link to example code. – david_k Sep 11 '19 at 09:05
  • Which function does the `ContractInterface` call on the instantiate command. How can I specifiy that function in my chaincode? Can I just create an Init function annotated as `Transaction` and it will work? – Sanjay S B Sep 11 '19 at 09:49
  • I've created an answer with a suggestion for you. – david_k Sep 11 '19 at 12:24

2 Answers2

2

Using ContractInterface you can't distinguish between Init and Invoke and this is a limitation of the Contract interface. you would have to determine if it is an instantiation request by other means. One way would be to have, for sake of the example, a method on your contract called instantiate and you would invoke this method when you instantiate the smart contract. It should have in it guard code so that it cannot be called at any other time and usually that would be checking something in the world state to see if it exists which determines that instantiate has already been performed.

david_k
  • 5,843
  • 2
  • 9
  • 16
  • could you please provide a example of the peer command to instantiate such chaincode? I am always gettign 500 error when trying to instantiate any chaincode implementing ContractInterface whilc all ChaincodeBase works ok – icordoba Oct 11 '19 at 11:31
0

There is a similar method for java as well with same name "init"

public Response init(ChaincodeStub stub) {
 ....
}

You can refer to this for Sample Java Chaincode

Harshit
  • 886
  • 7
  • 18
  • Thank you for your response but I am not using `ChaincodeBase` as I have mentioned in my question I am using `ContractInterface`. – Sanjay S B Sep 11 '19 at 09:49