Search code examples
hyperledger-fabrichyperledger-chaincodechaincode

Init equivalent in Java Chaincode


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.


Solution

  • 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.