Using Hyperledger Fabric To Setup Ethereum Smart Contracts

Statistics show that big business is increasingly moving to embrace enterprise blockchain. Some of these businesses require Ethereum smart contracts, but these are in the realm of public blockchain!

How can small to medium sized companies code to Ethereum? Well, Hyperledger Fabric has recently come to the rescue!

In this article, I will explain the process of using Hyperledger Fabric to setup Ethereum smart contracts.

Contents

Why enterprise blockchain?
Hyperledger Fabric: A key enterprise blockchain
Pros and cons of Hyperledger Fabric
The popularity of Hyperledger Fabric
Hyperledger Fabric tutorial
Ethereum smart contracts in Fabric
Fabric embraces “Ethereum Virtual Machine” (EVM)
How to set up Ethereum smart contracts on Fabric?

Why enterprise blockchain?

Let’s take a step back and understand why Enterprises weren’t using Ethereum smart contracts so far. Ethereum is a public blockchain, and enterprises can’t quite use this. This is due to the following reasons:

  • Unrestricted access: Public blockchains like Bitcoin or Ethereum are open for all to join. Enterprises can only allow trusted participants in their blockchain network.
  • Lack of data privacy: Every participant in a public blockchain can view all transactions in it. Enterprises operate under stringent data privacy regulations, therefore, they need to ensure data privacy. Participants with legitimate requirements of viewing certain data alone should see it.
  • Scalability challenges: Popular public blockchains like Bitcoin don’t scale well. Transaction throughput is low, moreover, transaction confirmation takes long. This is the result of their high decentralized security and the energy-hungry “Proof of Work” (POW) consensus algorithm. Read more about POW in “Proof of Work vs Proof of Stake comparison”.

Big businesses need blockchain networks with the following characteristics:

  • Permissioned access: Only trusted parties can join the network.
  • Data privacy: Only the participants with appropriate roles should see sensitive corporate information.
  • The network should offer high scalability and transaction throughput.
  • Processes, methods, and tools (PM&T) to easily implement, maintain, operate, and enhance the network.

This drives them to use enterprise blockchains like Hyperledger Fabric and R3 Corda. Read more about it in “Public vs private (permissioned) blockchain comparison”.

Hyperledger Fabric: A key enterprise blockchain

The blockchain technology is new, and enterprise blockchains are even more recent additions. There are a few reputed enterprise blockchain platforms. Hyperledger Fabric, also called “Fabric” is a prominent one. Let’s review a few quick facts, as follows:

Download Our Project Specification Template

  • Fabric is a permissioned blockchain, and this project is part of “Hyperledger Consortium”.
  • Hyperledger Consortium is an institution that develops processes, methods, open-source tools, and frameworks to increase the adoption of enterprise blockchains.
  • Linux Foundation leads this consortium.
  • Technology giants like IBM, Cisco, Intel, SAP, Accenture, etc. contribute to the various. Hyperledger projects.
  • Enterprises from various other verticals also contribute to these projects. American Express, Airbus, Deutsche Bank are just a few examples. View their “members” page for more details.
  • Hyperledger Consortium has various projects like Indy, Sawtooth, etc. You can view their “Projects” page to know about these. Many observers consider Fabric as the most matured of them.
  • Fabric is essentially an open-source framework, and you can use it to implement a permissioned blockchain in your enterprise.
  • The first release of Fabric was in 2016. IBM and Digital Asset had together built the first version.

Pros and cons of Hyperledger Fabric

Fabric offers multiple advantages, as follows:

  • A modular architecture: Building an enterprise blockchain isn’t easy. Developers and infrastructure architects need to devote significant efforts to build the infrastructure, protocol programs, etc. Fabric has a modular architecture. Pluggable components like consensus algorithm, identity management, etc. help developers to quickly implement the blockchain network.
  • Ease of building a permissioned blockchain: Fabric makes it easy to implement permissioned blockchain with their “Membership Service Provider” (MSP) feature, which is also called a “Certificate Authority” (CA). Organizations implementing Fabric can assign different roles like “Client”, “Endorser”, etc. to nodes.
  • Performance and scalability: Fabric doesn’t use POW, and the consensus algorithm incorporates the typical transaction workflow in an enterprise. This makes the blockchain scalable and performant.
  • Fabric allows smart contracts. These are called “chaincodes”. Developers can code them in Java, “Golang”, and Node.js. Read more about chaincodes in “Hyperledger?—?Chapter 8 | What is Hyperledger Fabric Chaincode ?”.
  • “Channels” for confidential data: Some transactions in enterprises are highly confidential. Fabric ensures greater security for these by using channels, which uses data-partitioning.
  • Fabric enables rich queries.
  • “Hardware Security Model” (HSM): Fabric uses additional hardware-based security for digital signatures.
  • Vibrant community: Technology giants, enterprises from different verticals, and a growing developer community is contributing to the growth of Fabric.

Read about these advantages in “Pros and cons of Hyperledger Fabric for blockchain networks”.

As Fabric is still new, there are a few challenges. These are as follows:

  • There is a limited number of proven use cases.
  • It’s not easy to find skilled Fabric developers.

The popularity of Hyperledger Fabric

Fabric is an increasingly popular enterprise blockchain platform. Read more about its’ popularity in “10 most popular & promising blockchain platforms”.

Cloud computing giants are making it easier for businesses to adopt it. They offer their blockchain platforms, using which businesses can adopt Fabric. A few examples are as follows:

  • IBM offers its IBM blockchain platform.
  • AWS provides Fabric on their cloud.
  • SAP cloud platform offerings include Fabric.
  • Microsoft also provides Fabric on their Azure.

Read more about this in “How much does it cost to build a blockchain project?”.

Hyperledger Fabric tutorial

As the demand for Fabric developers grow, new learning resources are emerging. Developers can use the following resources:

Programmers can also use this Blockgeeks Hyperledger Fabric tutorial.

Ethereum smart contracts in Fabric

So long, Fabric allowed developers to code chaincodes, but not Ethereum smart contracts. Ethereum smart contracts are important in the history of blockchain technology. This is due to the following reasons:

  • Bitcoin has always been a “Peer-to-Peer”(P2P) network for transferring Bitcoin without the intervention of 3rd parties like banks. It didn’t cater to any other use case.
  • Ethereum introduced smart contracts, i.e., open-source code with “If-Then-Else” statements that transferred cryptographic assets based on fulfillment of predetermined conditions.
  • These smart contracts execute autonomously, and they are stored transparently in the Ethereum blockchain.
  • No one can tamper with them after deployment. I have explained this in “How to Deploy Smart Contract on Ethereum?”.
  • Their execution is irreversible.
  • These transformative characteristics of smart contracts enabled entrepreneurs and developers to build decentralized business models using the Ethereum blockchain.
  • This significantly improved the adoption of blockchain technology, as the wave of ICOs has shown.

You see, blockchain skills are niche. This is true for Ethereum as well as for enterprise blockchains like Fabric. However, Ethereum enjoys considerable mindshare due to the popularity of smart contracts, written either in Solidity. There is also Vyper, a relatively new language to write Ethereum smart contracts.

A perceived disadvantage of Fabric was that it didn’t offer the ability to write Ethereum smart contracts. That’s changing now, as Fabric is now allowing developers to do this.

Fabric embraces “Ethereum Virtual Machine” (EVM)

To enable development of Ethereum smart contracts on Fabric, the Fabric project has incorporated the following:

Component #1: “Ethereum Virtual Machine” (EVM)

Ethereum is a collection of protocols that allow developers to create “Decentralized Applications” (DApps). “Ethereum Virtual Machine” (EVM) assumes the central position in this collection of protocols. This executes the code the developers write as part of their DApps. Read more about EVM in “What is Ethereum?”.

Fabric has incorporated Hyperledger Burrow. This is another Hyperledger project, which provides a blockchain client. It also provides a smart contractor interpreter, modeled after EVM. Read more about Hyperledger Burrow here. Developers can create chaincode on it.

Read How We Helped a Marketing Company to Build a Back-Office Custom Ads Dashboard

Component #2: Accounts

Ethereum features the following types of accounts:

  • “Externally Owned Accounts” (EOAs): On the Ethereum blockchain network, these are the user accounts. These represent the public address of users and their Ether (ETH) balance. Users control these with their private keys. Read more about EOAs in “Blockchain basics 02: a complete view on Ethereum blockchain”.
  • “Contract Accounts” (CAs): These are controlled by smart contracts. These contain the runtime EVM bytecode for a contract.

At the time of writing, there is no known plan of any Hyperledger Fabric coin. Therefore, Fabric will generate EOAs on the fly. Fabric will store CAs on the chain.

Component #3: Ethereum client

Ethereum developers use the “TestRPC” Ethereum client. Read more about it in their GitHub repository. Fabric EVM implementation uses the JSON RPC API partially.

Ethereum developers also use “Web3.js”, i.e., a tool to communicate with the Ethereum blockchain. The Fabric EVM implementation uses Fab3, which allows partial implementation of the JSON RPC API. You can read more about Fab3 in this tutorial on GitHub.

Component #4: “Gas”

“Gas” is a kind of fee. Users need to pay enough gas to execute any operation on the Ethereum blockchain network. Transactions that require more computation requires higher gas than the ones with less computing requirements.

A transaction won’t execute if users provide insufficient gas. Ethereum users pay for gas with Ether (ETH), i.e., the native cryptocurrency of the platform. Read this Ethereum stack exchange Q&A thread for more information. In case the Fabric EVM implementation, a large amount of gas is hardcoded.

How to set up Ethereum smart contracts on Fabric?

I will now explain the process to set up Ethereum smart contracts on Fabric, as follows:

Step #1: Install the required tools

You need to install the following tools:

  • cURL: It’s a tool for transferring files from or to a server. Install it following the instructions here.
  • Docker: Developers increasingly use “Docker” to create, deploy, and run applications using containers. Docker helps programmers to create a complete package for an app, including libraries and dependencies. They can then deploy it on their choice of platform. Read the installation instructions on the Docker website. The installation process includes “Docker Compose”, i.e., a tool to define and run multi-container apps.
  • Programming languages: You are setting up Ethereum smart contracts on Fabric to use in your application. Developers typically write apps on Fabric using “Go” or Node.js. You need to install either of them. Check the Go programming language website for installation instructions. Fabric currently supports Node.js v8.9, and you can install it using their instructions.

Read more in this Fabric prerequisites web page.

Step #2: Install binaries, samples, and Docker images

Fabric documentation already includes sample applications, and developers can use these to expedite their project. Fabric also provides binaries and Docker images for these. You will find these useful to fast-track your projects. Read “Install Samples, Binaries and Docker Images” for more details.

Step #3: Install Fabric EVM chaincode

Now, you need to install the Hyperledger Burrow-based EVM chaincode. This will enable you to interact with the Ethereum smart contracts you will write, using Fabric. Follow the detailed instructions in this GitHub repository.

Step #4: Use “Remix” IDE to write Solidity smart contracts

Remix is a web-based “Integrated Development Environment” (IDE) for Ethereum smart contract development. You don’t need to install anything, just start writing Solidity smart contracts on Remix.

Step #5: Initial configuration of Fabric EVM chaincode

From this point onwards, I will use a guide that Rahul Bairathi, a Fabric expert has written. The article explains how to code Ethereum smart contracts using Fabric. You can read it in “Solidity smart contract on Hyperledger Fabric”. I will refer to it as “reference article”.

After you have cloned the sample projects from GitHub as I have mentioned above, you need to do the following configurations:

  • Navigate to the “first-network” folder, and modify the Docker compose “Command Line Interface” (CLI) file. This is to map the folder where you have stored the code you had cloned from GitHub.
  • Start the network.
  • Navigate to the CLI Docker container and install the EVM chaincode.
  • Instantiate the EVM chaincode.

Follow the commands in the “reference article”.

Step #6: Write Solidity smart contracts

The next step involves writing Solidity smart contracts using the Remix IDE. If your team guidance, they can get check out this Remix documentation.

Step #7: Compile and deploy smart contracts

You need to take the following steps:

  • Compile the smart contract on Remix.
  • Copy the Bytecode. You need it to deploy the smart contract. Read more about it in this Ethereum stack exchange Q&A thread.
  • Paste the bytecode in any text editor. This will be in JSON format.
  • Deploy the smart contract using the “peer chaincode invoke” process. Your team can find the syntax of this command in this Fabric documentation page.

Read the “reference article” for more details.

Planning to build blockchain solutions using Fabric and Ethereum smart contracts?

Fabric and Ethereum have significant documentation. However, these are niche skills. Building blockchain solutions using Ethereum smart contract on Fabric is a complex project. Consider engaging a reputed software development partner. Read our guide “How to find the best software development company?” before you select a partner.

Download Our Project Specification Template

Aran Davies

Blockchain Expert | Developer | Writer | Photographer
Aran Davies

Latest posts by Aran Davies (see all)