hyperledger / fabric-contract-api-go

Packages for the implementation of the contract API for use in Go chaincode
https://wiki.hyperledger.org/display/fabric
Apache License 2.0
221 stars 103 forks source link

Can I Init a function In main funcion when i deploy the chaincode? #123

Closed WenTesla closed 9 months ago

WenTesla commented 9 months ago

run a init function once when a chaincode deploy in main funcion

WenTesla commented 9 months ago

I notice this function func (cc *ContractChaincode) Init(stub shim.ChaincodeStubInterface) peer.Response { can it solve my problem?

bestbeforetoday commented 9 months ago

Initialization of chaincode is discussed in this section of the Fabric documentation.

If you are using the Fabric contract API, you do not need to include an Init method in your chaincode. However, you can still use the --init-required flag to request that the chaincode be initialized by a call from your applications. If you use the --init-required flag, you will need to pass the --isInit flag or parameter to a chaincode call in order to initialize the chaincode every time you increment the chaincode version. You can pass --isInit and initialize the chaincode using any function in your chaincode.

Note that in most scenarios it is recommended to embed initialization logic into chaincode rather than use the chaincode lifecycle mechanism described above. Chaincode functions often perform checks against existing state, and initialization state can be implemented like any other chaincode state and be checked in subsequent chaincode function calls. Handling initialization state within chaincode logic rather than with the chaincode lifecycle mechanism has the benefit that you are not limited to a single initialization function, rather you are in full control of initialization logic and can call your own functions that initialize state from an application consistent with how all other application functions are called.