Write a Chaincode

A chaincode is written in Go, Node.js, or Java and then packaged into a ZIP file that is installed on the Oracle Blockchain Platform network.

Chaincodes define the data schema in the ledger, initialize it, perform updates when triggered by applications, and respond to queries. Chaincodes can also post events that allow applications to be notified and perform downstream operations. For example, after purchase orders, invoices, and delivery records have been matched by a chaincode, it can post an event so that a subscribing application can process related payments and update an internal ERP system.

Resources for Chaincode Development

Oracle Blockchain Platform uses Hyperledger Fabric as its foundation. Use the Hyperledger Fabric documentation to help you write valid chaincodes.

  • Welcome to Hyperledger Fabric. The Key Concepts and Tutorials sections should be read before you write you own chaincode.

  • Go Programming Language. The Go compilers, tools, and libraries provide a variety of resources that simplify writing chaincodes.

  • Package shim. Package shim provides APIs for the chaincode to access its state variables, transaction context and call other chaincodes. This documents the actual syntax required for your chaincode.

Oracle Blockchain Platform provides downloadable samples that help you understand how to write chaincodes and applications. See What Are Chaincode Samples?

You can add rich-query syntax to your chaincodes to query the state database. See SQL Rich Query Syntax and CouchDB Rich Query Syntax.

Package and Zip a Go Chaincode

Once you've written your chaincode, place it in a ZIP file. You don't need to create a package for the Go chaincode or sign it — the Oracle Blockchain Platform install and instantiation process does this for you as described in Typical Workflow to Deploy Chaincodes.

If your chaincode has any external dependencies, you can place them in the vendor directory of your ZIP file.

Package and Zip a Node.js Chaincode

If you're writing a Node.js chaincode, you need to create a package.json file with two sections:
  • The scripts section declares how to launch the chaincode.

  • The dependencies section specifies the dependencies.

The following is a sample package.json for a Node.js chaincode:

{
	"name": "chaincode_example02",
	"version": "1.0.0",
	"description": "chaincode_example02 chaincode implemented in Node.js",
	"engines": {
		"node": ">=8.4.0",
		"npm": ">=5.3.0"
	},
	"scripts": { "start" : "node chaincode_example02.js" },
	"engine-strict": true,
	"license": "Apache-2.0",
	"dependencies": {
		"fabric-shim": "~1.3.0"
	}
}

Place the chaincode and package file in a zip file to install it on Oracle Blockchain Platform.

Package and Zip a Java Chaincode

If you're writing a Java chaincode, you can choose Gradle or Maven to build the chaincode.

If you're using Gradle, place the chaincode, build.gradle, and settings.gradle in a zip file to install it on Oracle Blockchain Platform. The following is a sample file list of a chaincode zip package:
Archive:  example_gradle.zip 
 Length      Date    Time    Name
---------  ---------- -----   ---- 
      610  02-14-2019 01:36   build.gradle
       54  02-14-2019 01:28   settings.gradle
        0  02-14-2019 01:28   src/
        0  02-14-2019 01:28   src/main/
        0  02-14-2019 01:28   src/main/java/
        0  02-14-2019 01:28   src/main/java/org/
        0  02-14-2019 01:28   src/main/java/org/hyperledger/
        0  02-14-2019 01:28   src/main/java/org/hyperledger/fabric/
        0  02-14-2019 01:28   src/main/java/org/hyperledger/fabric/example/
     5357  02-14-2019 01:28   src/main/java/org/hyperledger/fabric/example/SimpleChaincode.java
---------                     -------
     6021                     10 files
If you're using Maven, place the chaincode and pom.xml in a zip file to install it on Oracle Blockchain Platform. The following is a sample file list of a chaincode zip package:
Archive:  example_maven.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
     3313  02-14-2019 01:52   pom.xml
        0  02-14-2019 01:28   src/
        0  02-14-2019 01:28   src/chaincode/
        0  02-14-2019 01:28   src/chaincode/example/
     4281  02-14-2019 01:28   src/chaincode/example/SimpleChaincode.java
---------                     -------
     7594                     5 files

Testing a Chaincode

After you write your chaincode, then you need to test it. See:

Installing and Instantiating a Chaincode

Once you’ve tested your chaincode, you can deploy it following the information in Typical Workflow to Deploy Chaincodes.

Upgrading a Chaincode

A chaincode may be upgraded any time by changing its version. The chaincode name must be the same or it would be considered a totally different chaincode.

  1. Change the chaincode version

  2. Follow the instructions in Upgrade a Chaincode to install and upgrade the new version of the chaincode.