Hyperledger Fabric implementation — Part 1

Hyperledger Fabric implementation V1.3: Chaincode using Node.js and Golang

pradeep padmarajaiah
Coinmonks
Published in
3 min readApr 18, 2018

--

In-order to build an Blockchain application on Fabric V1.3, three layers needs to be implemented

  1. Chaincode layer (interact to Ledger)
  2. SDK layer(Interact between application and Chaincode)
  3. Application layer (Interact between SDK and Application)

Chaincode can be written using GoLang or Node oe Java language in V1.3

To write a smart contract, Developer should be aware of three main classes Shim, ChaincodeInterface, ChaincodeStub .

Current article provides an overview of these classes and a general template for node.js code for all the Smartcontract or Chaincode implementation.

fabric-shim npm package: Heart for smart contract implementation

Package contains a Shim class which is used to register the chaincode with the target peer,listen to the incoming request and process transaction proposals or execute queries.

All the methods in Shim class are static.

  • start(chaincode-class): Start the chaincode process, register the chaincode with the target peer(pass the address of target peer as program arguement : — peer.address).
  • success(return-value) : return a success response object status code 200 and an optional success return-value content of type Buffer.
  • error(return-value) : return a success response object status code 200 and an optional error return-value content of type Buffer.

Chain code program must implement Chaincode interface. In node.js, It is defined as ChaincodeInterface class

Chaincode Interface has two methods

init() : Called to instantiate or upgrade the chaincodeinvoke() : Called to interact with Ledger (query/update/create/delete the asset)

Both methods accepts ChaincodeStub class as an method parameter.

ChaincodeStub

ChaincodeStub class contains the method to fetch the information of the incoming request like function name, arguments as array of the function passed,channel id ,CRUD(Create/Read/Update/Delete) the state varaible

Commonly used method in ChaincodeStub class

  • getFunctionAndParameters() : returns a global type definition FunctionAndParameters which contains two properties :

* fcn : function name to invoke

* params : arrays of arguments for the defined function

  • getState(key) : returns value of the key
  • putState(key, value) : Writes/updates the state variable key & value to the state store
  • getTxID() : returns transaction ID for the current request

Smart contract template for node

const shim = require(‘fabric-shim’);const PradeepChaincode = class {
async Init(stub) {
return …;
}
async Invoke(stub) {
return …;
}
};shim.start(new PradeepChaincode())

Details of the above template

  • Step 1 :
    fabric-shim package is imported.
  • Step 2 :
    Define the Chaincode class and methods (Init to instantiate/upgrade , Invoke to transact with Ledger)
  • Step 3 :
    Start the chaincode process and listen for incoming request

Smart contract template for Golang

package mainimport (“fmt”“github.com/hyperledger/fabric/core/chaincode/shim”“github.com/hyperledger/fabric/protos/peer”)//SmartContract : name of the smart contracttype SmartContract struct{}// Init : Method to initialize or update the chaincodefunc (s *SmartContract) Init(APIstub shim.ChaincodeStubInterface) peer.Response {return shim.Success(nil)}// Invoke : Method to transaction between the ledgerfunc (s *SmartContract) Invoke(APIstub shim.ChaincodeStubInterface) peer.Response {return shim.Error(“Invalid Smart Contract function name.”)}func main() {err := shim.Start(new(SmartContract))if err != nil {fmt.Printf(“Error Creating the new smart contract SmartContract : %s”, err)}}

Resources

--

--