Protobuf in Hyperledger Fabric

In this article, I’ll explain the technique used in serializing structured data using Protobuf in Hyperledger Fabric.

Image for post
Image for post

Chaincode and SmartContract

In hyperledger fabric, Chaincode is a specific program that handles the core business logic agreed by the group of participants in a blockchain network. Hyperledger fabric also uses a data format technique called SmartContract defined for a specific set of a data model within a Chaincode. Chaincode can have multiple sets of SmartContract to work as a transaction logic that controls the different data models. In simple words, SmartContract governs the transactions, whereas Chaincode governs how smartcontract are packaged for deployment.

Example: If a few user records need to be stored into the ledger then we need to have a SmartContract defining all required data fields of a single user.

User SmartContract

In this SmartContract, there are ID, Email, Name, Mobile, Age data fields relates to a single user record. Similarly, if we need to store the financial records for each user then we will have another smartcontract called Financial.

Financial SmartContract

So, these two smartcontracts will be deployed into the Chaincode. And will handle the transaction logic for both ledger state — World State, Blockchain.

SmartContract mainly performs Put, Get, Delete and GetHistory in the world state.

  1. PutState It creates new objects per distinct key or will overwrite to the existing objects.
  2. GetState — It retrieves the object for a distinct key from the ledger state.
  3. DelState — It removes the object from the world state of the ledger.
  4. GetHistoryForKey — Returns all the transaction history records for a key across time.

All the records are stored in the CouchDB as a world state record. The objects are stored as key-value pair in JSON data format. CouchDB is faster in querying JSON collections from the database. In Blockchain state, all these records are stored in the byte and it’s immutable.


Protocol buffer (short as protobuf) is Google’s serializing structured data with the language-neutral, platform-neutral, extensible mechanism. The serialize structured data is compiled in bytes and smaller, faster, simpler compared to traditional data formats like XML or JSON.

Why use Protobuf?

As we have seen, there is two smartcontracts User and Financial to store the information into the ledger of the same user, which means User stores the basic bio information and Financial stores the banking details of the user.

But, if we look into the smartcontracts in terms of querying purpose there is no relation among two data collection. We cannot define the same ID as key for User and Financial data model because ledger data stores in key-value pair and for the same key the information will be overridden.

Image for post
Image for post
Both records will be stored with two different IDs in the ledger state

To solve this problem, Protobuf provides a faster and flexible solution. We just need to write a .proto file describing the data structure, in this case, the Financial data structure we want to store.

So, the byte result of the protobuf message format invokes directly to the User SmartContract and removing Financial SmartContract completely.

How Protobuf works?

We will see here, how to setup protobuf compiler and generating protobuf message format.


First, we need to follow a few installation processes to use protobuf-compiler.

$ go get
$ go get
$ go get -u
$ export PATH=$PATH:$GOPATH/bin

Now, install the protobuf-compiler

$ sudo apt  install protobuf-compiler

Now, type protoc’ in the command line. It should say ‘Missing input file’, which means the protobuf-compiler is successfully installed.


First, we need to create a financial.proto file. It consists of a message format of type Financial, which has four fields, bank name, ifsc code, account number, created date.


Compile the proto file to generate the protobuf data model file for Financial message format.

$ protoc --go_out=. *.proto

You can see a protobuf file has generated as financial.pb.go. This file is a compatibility data model with the proto package and it will be used to convert the proto message format into bytes.


Now, we will create an extra data field financial in the User smartcontract.

Financial message format reference

While adding the user records into the ledger, you can also add financial message format reference into the same User smartcontract.

Parsing Protobuf

Parsing protobuf data is very simple, as it stores the record in bytes, it can easily be unmarshal using the Financial proto reference.


Protobuf makes simpler in data processing and formatting. It structures the data with specially generated source code to easily write and read data within the same smartcontract.



So, this is the basic overview of implementing Protobuf into a SmartContract in Hyperledger Fabric.

I hope you find this article useful :-)


Written by

Working on various software development projects, [Android, Go, Blockchain, Node.js, MongoDB, JavaScript, Kubernetes, Docker]

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store