Reader small image

You're reading from  Blockchain By Example

Product typeBook
Published inNov 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781788475686
Edition1st Edition
Languages
Concepts
Right arrow
Authors (3):
Bellaj Badr
Bellaj Badr
author image
Bellaj Badr

Bellaj Badr is an experienced security and software engineer who loves blockchain with a passion. Currently, he is the CTO at Mchain, a blockchain start-up that develops blockchain solutions for companies. Alongside his role as CTO, he acts as technical consultant, offering strategic and technical consulting to many companies worldwide. Aside from this, he is involved in many blockchain projects involving the establishment of new blockchain business-oriented protocols. Badr is a frequent speaker at developer conferences and is father to two angels.
Read more about Bellaj Badr

Richard Horrocks
Richard Horrocks
author image
Richard Horrocks

Richard Horrocks is a freelance Ethereum and full-stack developer based in the UK, and holds a BA and MSc in natural sciences from the University of Cambridge. He worked for many years as a technical lead for Cisco Systems, where he worked on the operating systems of carrier-grade routing hardware, before leaving the world of IT to work as an English teacher. The advent of cryptocurrency piqued his interest sufficiently to lead him back to IT, and, since 2015, he has been working with Ethereum and other cryptocurrencies. His specialist interests are cryptoeconomics and incentive layers, with a particular focus on mechanism design and token engineering. When not in front of a computer, he enjoys yoga and falling off motorbikes.
Read more about Richard Horrocks

Xun (Brian) Wu
Xun (Brian) Wu
author image
Xun (Brian) Wu

Xun (Brian) Wu is a senior blockchain architect and consultant. With over 20 years of hands-on experience across various technologies, including Blockchain, big data, cloud, AI, systems, and infrastructure, Brian has worked on more than 50 projects in his career. He has authored nine books, which have been published by O'Reilly, Packt, and Apress, focusing on popular fields within the Blockchain industry. The titles of his books include: Learn Ethereum (First Edition), Learn Ethereum (Second Edition), Blockchain for Teens, Hands-On Smart Contract Development with Hyperledger Fabric V2, Hyperledger Cookbook, Blockchain Quick Start Guide, Security Tokens and Stablecoins Quick Start Guide, Blockchain by Example, and Seven NoSQL Databases in a Week.
Read more about Xun (Brian) Wu

View More author details
Right arrow

Supply Chain on Hyperledger

In a previous chapter, we shed light on blockchain adoption in business by building an inter-business network using Quorum. In this chapter, we will continue in the same direction by introducing an enterprise blockchain solution called Hyperledger Fabric. Hyperledger (or the Hyperledger project) is one of the biggest projects in the blockchain industry. It is a global collaboration, hosted by the Linux foundation, and includes leaders in different sectors aiming to build a robust business-driven blockchain framework.

In this and the next chapter, you will develop, from a practical standpoint, a strong grasp of core Hyperledger, understand what Fabric is and how it works, and learn key vocabulary and concepts commonly used when discussing Hyperledger.

This chapter presents a first introduction to Hyperledger by building a blockchain-based supply chain...

Food industry supply chain

The main reason for classical supply chain inefficiency is the lack of transparency and reliable reporting. Many companies suffer from the lack of visibility regarding the entire supply chain of their products, and hence lose an immediate competitive advantage over competitors in their industries.

In the traditional supply chain models, the information about an entity is not fully transparent to others, which leads to inaccurate reports and a lack of interoperability. Emails and printed documents provide certain information, but still can’t contain fully detailed visibility and traceability information since the products across the entire supply chain are hard to trace. It is almost impossible for the consumer to know the true value of the product they purchased.

Since the blockchain is a transparent, immutable, and secure decentralized system...

Quick Hyperledger overview

As introduced in Chapter 7, Blockchains in Business, there are three main types of blockchain networks; public blockchains, consortium or federated blockchains, and private blockchains. Hyperledger is a blockchain framework which aims to help companies to build private or consortium permissioned blockchain networks, where multiple organizations can share the control and the permission to operate a node within the network.

Hyperledger is a set of open source tools and blockchain subprojects resulting from cross-industry collaboration. We will present two main components in this chapter – Hyperledger Fabric and Hyperledger Composer – while in the next chapter we will have the opportunity to discover other tools.

Hyperledger Fabric

...

Setting up the development environment

In this project, we will use Ubuntu (16.04, 64 bits) as the lab environment, and run the Hyperledger application in a virtual machine. For that, we recommend your system to have at least 4 GB of memory. To get started with Hyperledger Fabric, we need first to meet the following prerequisites.

Prerequisites

Fabric's Chaincode

Now, let's begin the fun part of the chapter writing smart contracts.

Chaincode is the smart contract that handles the business logic agreed to by members of the network. It is a piece of code that performs the logic operation in the blockchain network, and can be written in any conventional programming language and executed in a container. Currently, since Fabric 1.1, Golang and Javascript are the supported Chaincode languages. In this example, we will use Golang (Go) as the Chaincode language to implement our food supply chain example. Therefore, I recommend learning about the Go lanauge before starting. You can refer to the official documentation, or to Go playground (https://play.golang.org/), which is a web-based IDE where you can learn to code in Go online.

As creating the entire supply chain logic may not be within the scope of this...

Building a business network

Hyperledger Fabric has a built-in docker image to run peer nodes, and in order to set up a supply chain network we would typically use Docker Composer to launch various Fabric component containers. Before we run the Fabric network, we need to design the food supply chain network topology properly. In the chain, we have six types of entity: the raw food producer, a manufacturing processor, wholesalers, logistics operators, retailers, and consumers. For demonstration purposes we will define three different organizations to contain these entities, a single orderer, and a channel in our business network. The entities will interact with the blockchain application by invoking Chaincode in the Fabric network, updating the ledger world state, and writing transaction logs.

In this design, the organisation ORG1 hosts two peer nodes (peer0.org1.fsc.com and peer1...

Chaincode compilation and deployment

After setting up the network and writing the Chaincode, it's time to deploy the Chaincode. For that, we start by building and compiling the Chaincode using the following command: go build.

Many developers directly compile and install Chaincode from the project Chaincode folder, which isn't the best practice. Typically, we define GOPATH at /opt/gopath in the docker-compose-cli.yaml file, under the cli section:

cli:
container_name: cli
image: hyperledger/fabric-tools
tty: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_LOGGING_LEVEL=DEBUG
....
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME} ${DELAY}; sleep $TIMEOUT'
volumes:
- /var/run/:/host/var/run/
- ./Chaincode/:/opt/gopath/src/github.com/Chaincode...

Running the project

Throughout the previous sections we have gone over Chaincode deployment, network setup and many other Fabric features. In this section, we will use the Fabric tool to run an end-to-end application for our food supply chain application. I assume here that you have already downloaded the project code from the Packt website.

To run the network we provide a bash script—fscn.sh—that leverages the Docker images to bootstrap a Hyperledger Fabric network quickly. You can run it using: sudo ./fscn.sh –m up.

Once up, the script script.sh defined earlier will be executed. As a consequence, a series of events will be set in motion, including channel creation, the joining of all peers into the channel, the installation and instantiation of Chaincode in all peers, and the execution of different queries. These are all defined in script.sh, which is associated...

Summary

In this chapter, we have learned a lot about the Hyperledger project and, in particular, about Fabric. We have introduced, among other things, how to write and deploy a smart contract using Golang. You should now be comfortable enough to start writing your own smart contracts. We have also discovered how to build a web application that can interact with the Hyperledger Fabric API using the RPC protocol. At the end, we introduced an important tool—Hyperledger Composer—which can help us to build Fabric applications easily.

I hope you are not tired, since, in the next chapter, we will continue discovering Hyperledger by building another project—a letter of credit.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Blockchain By Example
Published in: Nov 2018Publisher: PacktISBN-13: 9781788475686
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Bellaj Badr

Bellaj Badr is an experienced security and software engineer who loves blockchain with a passion. Currently, he is the CTO at Mchain, a blockchain start-up that develops blockchain solutions for companies. Alongside his role as CTO, he acts as technical consultant, offering strategic and technical consulting to many companies worldwide. Aside from this, he is involved in many blockchain projects involving the establishment of new blockchain business-oriented protocols. Badr is a frequent speaker at developer conferences and is father to two angels.
Read more about Bellaj Badr

author image
Richard Horrocks

Richard Horrocks is a freelance Ethereum and full-stack developer based in the UK, and holds a BA and MSc in natural sciences from the University of Cambridge. He worked for many years as a technical lead for Cisco Systems, where he worked on the operating systems of carrier-grade routing hardware, before leaving the world of IT to work as an English teacher. The advent of cryptocurrency piqued his interest sufficiently to lead him back to IT, and, since 2015, he has been working with Ethereum and other cryptocurrencies. His specialist interests are cryptoeconomics and incentive layers, with a particular focus on mechanism design and token engineering. When not in front of a computer, he enjoys yoga and falling off motorbikes.
Read more about Richard Horrocks

author image
Xun (Brian) Wu

Xun (Brian) Wu is a senior blockchain architect and consultant. With over 20 years of hands-on experience across various technologies, including Blockchain, big data, cloud, AI, systems, and infrastructure, Brian has worked on more than 50 projects in his career. He has authored nine books, which have been published by O'Reilly, Packt, and Apress, focusing on popular fields within the Blockchain industry. The titles of his books include: Learn Ethereum (First Edition), Learn Ethereum (Second Edition), Blockchain for Teens, Hands-On Smart Contract Development with Hyperledger Fabric V2, Hyperledger Cookbook, Blockchain Quick Start Guide, Security Tokens and Stablecoins Quick Start Guide, Blockchain by Example, and Seven NoSQL Databases in a Week.
Read more about Xun (Brian) Wu