Chaincode as external service
Last updated
Was this helpful?
Last updated
Was this helpful?
Spydra runs chaincode as an external service to be able to gain more control over and to manage chaincode runtime more effectively.
Steps to write a chaincode compatible for running as an external service
Golang:
Import the shim package and create the ChaincodeServer struct from the shim package.
The ChaincodeServer struct takes the following values:
CC: It takes contractapi.ContractInterface which was created using contractapi.NewChaincode() function.
CCID: It takes the chaincode package id. This can be accessed from the CORE_CHAINCODE_ID environment variable.
Address: Chaincode server address to expose the chaincode. This can be accessed from the CORE_CHAINCODE_ADDRESS environment variable.
TLSProps: It accepts the struct value of shim.TLSProperties
Then call the start function from the created ChaincodeServer struct value (server.Start())
Eg:
Javascript:
If chaincode is written using contract api:
In package.json, modify the value of scripts.start section to ”fabric-chaincode-node server”
If chaincode is written using fabric-shim:
In the file where the chaincode is initialised / started, create the shim.server by passing ccid and address which are taken from the runtime environment variables CORE_CHAINCODE_ID and CHAINCODE_SERVER_ADDRESS
Then call the start function from the created shim.server object (server.start())
Java:
Create a new java file (ContractBootstrap.java) that will import NettyChaincodeServer and will have a ContractBootstrap class. This will be the main class.
The main function in ContractBootstrap class will take chaincode metadata from the environment variables such as:
CHAINCODE_SERVER_ADDRESS
CORE_CHAINCODE_ID
CHAINCODE_TLS_DISABLED
CHAINCODE_TLS_KEY
CHAINCODE_TLS_CERT
These env variables will be created inside the chaincode container when the chaincode is deployed.
If remote git is used, make sure the gradle/wrapper directory contains the gradle-wrapper.jar file after publishing as .jar is normally added in the . gitignore file.
Make sure the gradlew file is present at the root directory of your repository.
Eg:
Eg:
Using fabric’s NettyChaincodeServer and ContractRouter, the chaincode server will be started. Eg:
Modify the build.gradle file to include the plugin com.github.johnrengelman.shadow and add shadowJar section with basename parameter as chaincode. Create manifest section inside shadowJar with attribute: Main-Class with the ContractBootstrap class’s path. Eg: