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 installation and deployment process does this for you as described in Typical Workflow to Deploy Chaincodes (Hyperledger Fabric v2.x) or Typical Workflow to Deploy Chaincodes (Hyperledger Fabric v1.4.7).

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

Vendor the Shim for Go Chaincodes (Hyperledger Fabric v2.x)

The Go chaincode shim dependency, which was previously included with earlier versions of Hyperledger Fabric, is not included with Hyperledger Fabric v2.x. The shim must now be vendored (imported) to Go chaincodes before they are installed on a peer running Hyperledger Fabric v2.x.

You can use Go modules or a third-party tool such as govendor to vendor the chaincode shim and update it to the version that works with Hyperledger Fabric v2.x.

For more information, see Chaincode shim changes (Go chaincode only) and Upgrade Chaincodes with vendored shim in the Hyperledger Fabric documentation. For more information about Go modules, see Go Modules Reference.

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"
	}
}
The packaging rules for a Node.js chaincode are:
  • package.json must be in the root directory.
  • The entry JavaScript file can be located anywhere in the package.
  • If "start" : "node <start>.js" isn't specified in the package.json, server.js must be in the root directory.

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 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 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 Deploying a Chaincode

After you’ve tested your chaincode, you can deploy it by following the information in Typical Workflow to Deploy Chaincodes (Hyperledger Fabric v2.x) or Typical Workflow to Deploy Chaincodes (Hyperledger Fabric v1.4.7).

Upgrading a Chaincode

You can upgrade a deployed chaincode by following the steps in Upgrade a Chaincode (Hyperledger Fabric v2.x) or Upgrade a Chaincode (Hyperledger Fabric v1.4.7).