avatarAlex Roan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2922

Abstract

Fabric is a permissioned network</b>. This means that you cannot join the network unless you have been granted permission to do so. Ethereum is a permissionless network because anyone can join and interact with it.</li><li><b>There’s no underlying currency like Ether on Hyperledger Fabric</b>. Assets and the consensus protocol are defined by the participants.</li><li>There are two types of node on the Fabric network: <b>Peer nodes and Order nodes</b>. Peer nodes verify and execute transactions submitted to the network, Order nodes order and propagate these throughout the network.</li><li>The state of the Ethereum network is derived from the history of all transactions stored on the Blockchain. While this is also true with Hyperledger Fabric, an <b>SQL database sits alongside the Blockchain</b> which stores the latest state.</li><li><b>Ethereum has Smart Contracts. Hyperledger Fabric has a similar concept called Chaincode</b>, and a number of languages can be used to implement them.</li></ul><h1 id="12de">Smart Contract vs Chaincode</h1><p id="9b4d">The <a href="https://github.com/hyperledger/fabric-samples">Hyperledger Fabric Samples</a> (we’re using v1.2.0) project provides a sample called <i>Fabcar</i>. It contains Chaincode deployed to the Fabric Blockchain which stores an array of car information. It provides the ability to create new cars, and to query the list of cars. We’ll use the code from that project and compare it to an equivalent Solidity contract.</p><p id="36b3">Since we’re coming from an Ethereum standpoint, let’s start there.</p><h2 id="abf7">Solidity</h2><p id="a614">Figure 1 shows the Fabcar Smart Contract.</p> <figure id="d9d0"> <div> <div>

            <iframe class="gist-iframe" src="/gist/alexroan/d2c6ef51d47e567eff05cbe8a203266b.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="ba58">The contract has an array called <code>cars</code> made up of the <code>Car</code> struct. The constructor pushes a bunch of cars to the array, and the rest of the contract exposes functions that add and change the array: <code>createCar()</code> and <code>changeCarOwner()</code>. The cars array is public so that it is queryable externally. Simple stuff!</p><h2 id="2fe5">Hyperledger</h2><p id="164e">Solidity is the proprietary language for developing Smart Contracts on Ethereum. Hyperledger offers the flexibility of a number of existing general purpose languages to write Chaincode with. This is great for onboarding developers from other disciplines, however is has the drawback of making Chaincode more complex than Smart Contract code.</p><p id="67ab">Hyperledger supports Go, Node, and a number of other languages. I’m going to use Go and Node for this comparison. Figure 2 shows the Go implementati

Options

on and Figure 3 shows the Node JS implementation.</p> <figure id="1a08"> <div> <div>

            <iframe class="gist-iframe" src="/gist/alexroan/c51e4a8c31bf691ff25a83901133e6b2.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure>
    <figure id="3b8d">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/alexroan/49ad9f418c95e26e544b8aab9a3e5909.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="9ce6">Both implementations exhibit the same functionality on Hyperledger Fabric as our Solidity file does on Ethereum, but as you can see takes a lot more code.</p><p id="12dc">Let’s analyse Figure 3: <i>fabcar.js</i>.</p><p id="987b">The <code>Init()</code> function on line 15 is called when the Chaincode is instantiated. As the comment suggests, best practice dictates that any initialization (like setting state values) is implemented in a separate function. <code>InitLedger()</code> on line 55 does this, which we call separately once the chaincode is deployed. In contrast to Solidity, initialization occurs in the constructor.</p><p id="a8f6">When calling functions, it’s important to note that the Chaincode has a single entry point <code>Invoke()</code>, which uses the parameters given to determine which function to call. When interacting with our Solidity Smart Contract, we can call each function directly from Web3.</p><p id="87b2">You’ll notice a variable called shim is appears throughout the code. This references an interface which allows the Chaincode to interact with the Peer node validating the transaction. It basically ensures that the chaincode correctly interacts with the Blockchain.</p><h1 id="29ba">Conclusion</h1><p id="6c3c">Fabric allows us to use multiple languages to write Chaincode. However, this flexibility has the drawback of making Chaincode more complex than Ethereum Smart Contracts because of the extra boilerplate code needed to interact with the underlying Blockchain.</p><div id="fcd2" class="link-block">
      <a href="https://readmedium.com/blockchain-development-resources-b44b752f3248">
        <div>
          <div>
            <h2>Blockchain Development Resources To Follow Right Now</h2>
            <div><h3>A list of resources to learn Blockchain, Ethereum, and DApp development</h3></div>
            <div><p>medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*GZlVe27SOc44fcIYztY5Qw.jpeg)"></div>
          </div>
        </div>
      </a>
    </div></article></body>

An Introduction to Hyperledger Fabric for Ethereum Developers

How to write your first Hyperledger Chaincode smart contract

Prerequisites: Understanding of Blockchain, Ethereum Smart Contracts, and Go or NodeJS

Image by clintadair on Unsplash

Introduction

In my opinion, Ethereum is the perfect entry point to begin learning about Blockchain Development and building Decentralised Applications. The ecosystem is growing, documentation is great and the developer community is large with respect to the rest of the Blockchain space.

Once you’ve grasped the basics of developing on Ethereum, you’ll naturally start to cast your net wider and begin to look at other Blockchain solutions and use cases.

This article explains the differences between writing Smart Contracts on Ethereum using Solidity, and writing Chaincode on the Hyperledger Fabric Blockchain.

Solidity Development vs Hyperledger Development

As a Smart Contract developer, you’re probably not as concerned about the underlying Blockchain (or at least not to the extent) as an Ethereum Core developer is. However, it helps to have a basic understanding to avoid certain pitfalls that you won’t stumble across developing “traditional stack” applications (see random number generation).

In contrast, Hyperledger Fabric development will likely require you to delve deeper into the Blockchain layer. This may seem a little daunting at first, especially if Smart Contracts and Solidity are still new to you. Fortunately, the structure of Fabric helps us out a little bit here. Because of its modular design, we can break each module down and focus on them in isolation.

Why Hyperledger?

At this point it’s unlikely you’ll have thought about spinning up your own Blockchain. Why would you? You can write DApps on Ethereum for the whole world to participate in.

For Blockchain to really grow, Big Business needs to get on board, and there are not many big businesses happy with making their data public. The public Ethereum network is not a great fit since all data on the Ethereum is, by definition, public. Hyperledger Fabric allows anyone to spin up their own Blockchain and fence it off from the outside world in minutes.

5 Core Functions Of Hyperledger Fabric

There are 5 core functional differences we need to understand about Fabric:

  • Hyperledger Fabric is a permissioned network. This means that you cannot join the network unless you have been granted permission to do so. Ethereum is a permissionless network because anyone can join and interact with it.
  • There’s no underlying currency like Ether on Hyperledger Fabric. Assets and the consensus protocol are defined by the participants.
  • There are two types of node on the Fabric network: Peer nodes and Order nodes. Peer nodes verify and execute transactions submitted to the network, Order nodes order and propagate these throughout the network.
  • The state of the Ethereum network is derived from the history of all transactions stored on the Blockchain. While this is also true with Hyperledger Fabric, an SQL database sits alongside the Blockchain which stores the latest state.
  • Ethereum has Smart Contracts. Hyperledger Fabric has a similar concept called Chaincode, and a number of languages can be used to implement them.

Smart Contract vs Chaincode

The Hyperledger Fabric Samples (we’re using v1.2.0) project provides a sample called Fabcar. It contains Chaincode deployed to the Fabric Blockchain which stores an array of car information. It provides the ability to create new cars, and to query the list of cars. We’ll use the code from that project and compare it to an equivalent Solidity contract.

Since we’re coming from an Ethereum standpoint, let’s start there.

Solidity

Figure 1 shows the Fabcar Smart Contract.

The contract has an array called cars made up of the Car struct. The constructor pushes a bunch of cars to the array, and the rest of the contract exposes functions that add and change the array: createCar() and changeCarOwner(). The cars array is public so that it is queryable externally. Simple stuff!

Hyperledger

Solidity is the proprietary language for developing Smart Contracts on Ethereum. Hyperledger offers the flexibility of a number of existing general purpose languages to write Chaincode with. This is great for onboarding developers from other disciplines, however is has the drawback of making Chaincode more complex than Smart Contract code.

Hyperledger supports Go, Node, and a number of other languages. I’m going to use Go and Node for this comparison. Figure 2 shows the Go implementation and Figure 3 shows the Node JS implementation.

Both implementations exhibit the same functionality on Hyperledger Fabric as our Solidity file does on Ethereum, but as you can see takes a lot more code.

Let’s analyse Figure 3: fabcar.js.

The Init() function on line 15 is called when the Chaincode is instantiated. As the comment suggests, best practice dictates that any initialization (like setting state values) is implemented in a separate function. InitLedger() on line 55 does this, which we call separately once the chaincode is deployed. In contrast to Solidity, initialization occurs in the constructor.

When calling functions, it’s important to note that the Chaincode has a single entry point Invoke(), which uses the parameters given to determine which function to call. When interacting with our Solidity Smart Contract, we can call each function directly from Web3.

You’ll notice a variable called shim is appears throughout the code. This references an interface which allows the Chaincode to interact with the Peer node validating the transaction. It basically ensures that the chaincode correctly interacts with the Blockchain.

Conclusion

Fabric allows us to use multiple languages to write Chaincode. However, this flexibility has the drawback of making Chaincode more complex than Ethereum Smart Contracts because of the extra boilerplate code needed to interact with the underlying Blockchain.

Ethereum
Hyperledger Fabric
Smart Contracts
Programming
Blockchain
Recommended from ReadMedium