Updated on 2022-09-08 GMT+08:00

Chaincode Structure

This section uses the Java language as an example. A chaincode is a Java project. After creating the project, you can perform operations such as function development.

Notes and Constraints

The Java chaincode is supported only by Fabric v2.2 and later versions.

Chaincode Interface

A chaincode is invoked using the start function in the shim package. During chaincode development, define a class to extend ChaincodeBase. The following methods are overridden during the extension:

public class SimpleChaincodeSimple extends ChaincodeBase { 
    @Override 
    public Response init(ChaincodeStub stub) { 
    }
 
    @Override 
    public Response invoke(ChaincodeStub stub) { 
    } 
}
  • init is called to initialize data during chaincode instantiation or update.
  • Invoke is called to update or query the ledger. The service logic for responding to the call or query needs to be implemented in this method.

Chaincode Structure

The Java chaincode structure is as follows:

package main 
 
//You only need to configure the required packages in Maven or Gradle. The packages are imported automatically.
import org.hyperledger.fabric.shim.ChaincodeBase; 
import org.hyperledger.fabric.shim.ChaincodeStub; 
 
public class SimpleChaincodeSimple extends ChaincodeBase { 
    @Override 
    public Response init(ChaincodeStub stub) { 
        //Implement the processing logic for chaincode initialization or update in this method.
        //stub APIs can be flexibly used during compilation.
    }
 
    @Override 
    public Response invoke(ChaincodeStub stub) { 
        //Implement the processing logic for responding to the call or query in this method.
        //stub APIs can be flexibly used during compilation.
    } 

    //Main function. The shim.Start() method needs to be invoked.
    public static void main(String[] args) { 
        new SimpleChaincode().start(args); 
    } 
}