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, complete updates when triggered by applications, and respond to queries. Chaincodes can also post events that allow applications to be notified and complete 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. Read the Key Concepts and Tutorials sections 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, get transaction context, and call other chaincodes. The package shim documentation describes the actual syntax that is 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 a Go Chaincode

After you've written your chaincode, compress it to a file in ZIP format. 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).

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

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.

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 a Node.js Chaincode

If you're writing a Node.js chaincode, you must 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:
  • The package.json file 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 file, the server.js file must be in the root directory.

Compress the chaincode and package file in ZIP format to install it on Oracle Blockchain Platform.

Package 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, compress the chaincode, build.gradle, and settings.gradle to a file in ZIP format to install it on Oracle Blockchain Platform. The following list shows sample files in 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, compress the chaincode and pom.xml to a file in ZIP format to install it on Oracle Blockchain Platform. The following list shows sample files in 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

Test your chaincode after you write it. See the following topics:

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

Upgrading a Chaincode

Upgrade a deployed chaincode by following the steps in Upgrade a Chaincode (Hyperledger Fabric v2.x).