A Beginners Guide to Building Blockchain Solutions Bikramaditya Singhal

Gautam Dhameja Priyansu Sekhar Panda

A Beginner's Guide to Building Blockchain Solutions

Bikramaditya Singhal, Gautam Dhameja and Priyansu Sekhar Panda

Beginning Blockchain

A Beginner’s Guide to Building Blockchain Solutions


Bikramaditya Singhal Bangalore, Karnataka, India

Gautam Dhameja Berlin, Berlin, Germany

Priyansu Sekhar Panda Bangalore, Karnataka, India

ISBN 978-1-4842-3443-3 e-ISBN 978-1-4842-3444-0

Library of Congress Control Number: 2018945613

© Bikramaditya Singhal, Gautam Dhameja, Priyansu Sekhar Panda 2018

"Standard Apress"

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made.

The publisher makes no warranty, express or implied, with respect to the material contained herein.

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@, or visit Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.


Beginning Blockchain is a book for those willing to learn about the technical fundamentals of Blockchain, practical implications, and hands-on development aspects of Blockchain applications. Adequate history, background, and theoretical aspects are covered to help you build a solid foundation for your Blockchain journey, and appropriate development aspects are covered with coding examples to help you jumpstart on Blockchain assignments. The first chapter introduces you to the Blockchain world and sets the context. The second chapter dives deeper into the core components of Blockchain. The third chapter is focused on Bitcoin’s technical concepts so what was discussed in the second chapter could be demonstrated with Bitcoin as a cryptocurrency use case of Blockchain. The fourth chapter is dedicated to the Ethereum Blockchain in an effort to demonstrate how Blockchain could be programmed for many more use cases and not limited to just cryptocurrencies. The fifth chapter is where you get the hang of Blockchain development with examples on basic transactions in both Bitcoin and Ethereum. The sixth chapter, as the final chapter, demonstrates the end-to-end development of a decentralized application (DApp). By the end of this chapter, you will have been equipped with enough tools and techniques to address many real-world business problems with applicable Blockchain solutions. Start your journey toward limitless possibilities.

Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at 3443-3 . For more detailed information, please visit .


We'd like to thank Nikhil and Divya for their cooperation and support all through and many thanks to Navin for his thorough technical review of this book. We also thank all who have directly or indirectly contributed to this book.

Table of Contents

Chapter 1: Introduction to Blockchain

Backstory of Blockchain

What is Blockchain?

Centralized vs. Decentralized Systems Centralized Systems Decentralized Systems

Layers of Blockchain Application Layer Execution Layer Semantic Layer Propagation Layer Consensus Layer

Why is Blockchain Important? Limitations of Centralized Systems Blockchain Adoption So Far

Blockchain Uses and Use Cases



Distributed Consensus Mechanisms

Gas and Transaction Cost




About the Authors and About the Technical Reviewer

About the Authors

Bikramaditya Singhal

is a Blockchain expert and AI practitioner with experience working in multiple industries. He is proficient in Blockchain, Bitcoin, Ethereum, Hyperledger, cryptography, cyber security, and data science. He has extensive experience in training and consulting on Blockchain and has designed many Blockchain solutions. He worked with companies such as WISeKey, Tech Mahindra, Microsoft India, Broadridge, and Chelsio Communications, and he also cofounded a company named Mund Consulting that focuses on big data analytics and artificial intelligence. He is an active speaker at various conferences, summits, and meetups. He has also authored a book entitled Spark for Data Science.

Gautam Dhameja

is a Blockchain application consultant based out of Berlin, Germany. For most of this decade, he has been developing and delivering enterprise software including Web and Mobile apps,

Cloud-based hyper-scale IoT solutions, and more recently, Blockchain-based decentralized applications (DApps). He possesses a deep understanding of the decentralized stack, cloud solutions architecture, and object- oriented design. His areas of expertise include Blockchain, Cloud-based enterprise solutions, Internet of Things, distributed systems, and native and hybrid mobile apps. He is also an active blogger and regularly speaks at tech conferences and events.

Priyansu Sekhar Panda

is a research engineer at Underwriters Laboratories, Bangalore, India. He has worked with other IT companies such as Broadridge, Infosys Limited, and Tech Mahindra. His areas of expertise include Blockchain, Bitcoin, Ethereum, Hyperledger, game theory, IoT, and artificial intelligence. Priyansu’s current research is on building next-gen applications leveraging Blockchain, IoT, and AI. His major research interests include building Decentralized 5 4 Autonomous Organizations

(DAO), and the security, scalability, and consensus of Blockchains.

About the Technical Reviewer

Navin K Manaswi

has been developing AI solutions/products with the use of cutting-edge technologies and sciences related to artificial intelligence for many years. Having worked for consulting companies in Malaysia, Singapore, and the Dubai Smart City project, he has developed a rare skill of delivering end-to-end artificial intelligence solutions. He built solutions for video intelligence, document intelligence, and human-like chatbots in his own company. Currently, he solves B2B problems in verticals of healthcare, enterprise applications, industrial IoT, and retail in the Symphony AI incubator as Deep Learning-AI Architect. Through this book, he wants to democratize cognitive computing and services for everyone, especially developers, data scientists, software engineers, database engineers, data analysts, and CXOs.

© Bikramaditya Singhal, Gautam Dhameja, Priyansu Sekhar Panda 2018 Bikramaditya Singhal, Gautam Dhameja and Priyansu Sekhar Panda, Beginning Blockchain,

1. Introduction to Blockchain

Bikramaditya Singhal: , Gautam Dhameja’ and Priyansu Sekhar Panda‘

(1) Bangalore, Karnataka, India (2) Berlin, Berlin, Germany

Blockchain is the new wave of disruption that has already started to redesign business, social and political interactions, and any other way of value exchange. Again, it is not just a change, but a rapid phenomenon that is already in motion. As of this writing, more than 40 top financial institutions and many different firms across industries have started to explore blockchain to lower transaction cost, speed up transaction time, reduce the risk of fraud, and eliminate the middleman or intermediary services. Some are trying to reimagine legacy systems and services to take them to a new level and also come up with new kinds of service offerings.

We will cover blockchain in greater detail throughout the book. You can follow through the chapters in the order presented if you are new to blockchain or pick only the ones relevant to you. This chapter explains what blockchain is all about, how it has evolved, and its importance in today’s world with some uses and use cases. It gives an outside-in perspective to you to be able to delve deeper into blockchain.

Backstory of Blockchain

One of the first known digital disruptions that laid the foundation of the Internet was TCP/IP (Transmission Control Protocol/Internet Protocol) back in the 1970s. Prior to TCP/IP, it was the era of circuit switching, which required dedicated connection between two parties for communication to happen. TCP/IP came up with its packet switching design, which was more open and peer-to-peer with no need to preestablish a dedicated line between parties.

When the Internet was made accessible to the public through the World Wide Web (WWW) in the early 1990s, it was supposed to be more open and peer-to- peer. This is because it was built atop the open and decentralized TCP/IP. When any new technology, especially the revolutionary ones, hits the market, either they fade away on their own, or they create such an impact that they become the accepted norm. People adapted to the WWW revolution and leveraged the benefits it had to offer in every possible way. As a result, the World Wide Web started shaping up in a way that might not have been the exact way it was imagined. It could have been more open, more accessible, and more peer-to-peer. Many new technologies and businesses started to build on top of it and it became what it is today—more centralized. Slowly and gradually, people get used to what technology offers. People are just fine if an international transaction takes days to settle, or it is too expensive, or it is less reliable.

Let us take a closer look at the banking system and its evolution. Starting from the olden days of barter system till fiat currencies, there was no real difference between a transaction and its settlement because they were not two separate entities. As an example, if Alice had to pay $10 to Bob, she would just hand over a $10 note to Bob and the transaction would just get settled there. No bank was needed to debit $10 from Alice’s account and credit the same to Bob’s account or to serve as a system of trust to ensure Alice does not cheat Bob. However, transacting directly with someone who is physically not present close by was difficult. So, banking systems evolved with many

more service offerings and enabled transactions from every corner of the world. With the help of the Internet, geography was no more a limitation and banking became easier than ever. Not just banking for that matter: the Internet facilitated many different kinds of value exchange over the web.

Technology enabled someone from India to make a monetary transaction with someone in the United Kingdom, but with some cost. It takes days to settle such transactions and is expensive as well. A bank was always needed to impose trust and ensure security for such transactions between two or more parties. What if technology could enable trust and security without these intermediary and centralized systems? Somehow, this part (of technology imposing trust) was missing all through, which resulted in development of centralized systems such as banks, escrow services, clearing houses, registrars and many other such institutions. Blockchain proves to be that missing piece of the Internet revolution puzzle that facilitates a trustless system in a cryptographically secured way.

Satoshi Nakamoto, the pseudonymous name by which the world knows him, must have felt that the monetary systems were not touched by the technological revolution since the 1980s. Banks formed the centralized institutions that maintained the transaction records, governed interactions, enforced trust and security, and regulated the whole system. The whole of commerce relies on these financial institutions, which serve as the trusted third parties to process payments. The mediation of financial institutions increases cost and time to settle a transaction, and also limits the transaction sizes. The mediation was needed to settle disputes, but that meant that completely nonreversible transaction was never possible. This resulted in a situation where trust was needed for someone to transact with another. Certainly, this bureaucratic system had to change to keep up with the economy’s expected digital transformation. So, Satoshi invented a cryptocurrency called Bitcoin that was enabled by the underlying technology— blockchain. Bitcoin is just one monetary use case of blockchain that addresses the inherent weakness of trust-based models. We will delve deeper into the fundamentals of both Bitcoins and blockchain in this book.

What is Blockchain?

The Internet has revolutionized many aspects of life, society, and business. However, we learned in the previous section that how people and organizations execute transactions with one another has not changed much in the past couple of decades. Blockchain is believed to be the component that completes the Internet puzzle and makes it more open, more accessible, and more reliable.

To understand blockchain, you have to understand it from both a business perspective and technical perspective. Let us first understand it in a business transaction context to get the “what” of it, and then look into the technicality to understand the “how” of it in the following chapters.

Blockchain is a system of records to transact value (not just money!) in a peer-to-peer fashion. What it means is that there is no need for a trusted intermediary such as banks, brokers, or other escrow services to serve as a trusted third party. For example, if Alice pays to Bob $10, why would it go through a bank? Take a look at Figure 1-1.

~ allt


Paid Bob $10 4

! mans Bob

{) Pay Bob $10

Transacting through Trusted Intermediary; Bank


od = o

{) Alice Pays $10 to Bob /Bob gets $10

= Peer-to-Peer Transaction: Blockchain

= E>

Figure 1-1 Transaction through an intermediary vs. peer-to-peer transaction

Let us look at a different example now. A typical stock transaction happens in seconds, but its settlement takes weeks. Is it desirable in this digital age? Certainly not! Figure 1-2 demonstrates the current situation.



Stocks settlement through clearing houses

Figure 1-2 Stocks trading through an intermediary clearing house

If someone wants to buy some stocks from a company or a person, they can just directly buy it from them with instant settlement, with no need for brokers, clearing houses, or other financial institutions in between. A decentralized and peer-to- peer solution to such a situation can be represented as in Figure 1-3.

» Company-A Py


Company-B tf


Peer-to-Peer trading: Blockchain

Figure 1-3 Peer-to-peer stock trading

Please note that transaction and settlement are not two different entities in a blockchain setting! The transactions are analogous to, say, fiat currency transactions where if someone pays another a $10 note, they do not own it anymore and that $10 note is physically transferred to the new owner.

Now that you understand blockchain from a functional perspective, at a high level, let us look into it a little technically, and the reason for naming it “blockchain” becomes clearer. We will see “What” it is technically and leave the “How” it works to Chapter 2.

Read the following statements and do not worry if the concepts do not fit together well for your complete understanding. You may want to revisit it, but be patient till you finish reading this book.

e Blockchain is a peer-to-peer system of transacting values with no trusted third parties in between.

e It is a shared, decentralized, and open ledger of

transactions. This ledger database is replicated across a large number of nodes.

e This ledger database is an append-only database and

cannot be changed or altered. It means that every entry is a permanent entry. Any new entry on it gets reflected on all copies of the databases hosted on different nodes.

e There is no need for trusted third parties to serve as

intermediaries to verify, secure, and settle the transactions.

e It is another layer on top of the Internet and can coexist with other Internet technologies.

e Just the way TCP/IP was designed to achieve an open system, blockchain technology was designed to enable true decentralization. In an effort to do so, the creators of Bitcoin open-sourced it so it could inspire many decentralized applications.

A typical blockchain may look as shown in Figure 1-4.

Previous Block Previous Bloc Previnus Slock Previous Block

Transactions Transact ons fire = |arisactions


‘wae Block-2 Block-3 Block-4 Block-1265 Block-1

Figure 1-4 The blockchain data structure

Every node on the blockchain network has an identical copy of the blockchain shown in Figure 1-4, where every block is a collection of transactions, hence the name. As you can see, there are two major parts in every block. The “header” part links back to the previous block in the chain. What it means is that every block header contains the hash of the previous block so that no one can alter any transaction in the previous block. We will look into further details of this concept in the following chapters. The other part of a block is the “body content” that has a validated list of transactions, their amounts, the addresses of the parties involved, and some more details. So, given the latest block, it is feasible to access all the previous blocks in a blockchain.

Let us consider a practical example and see how the transactions take place and the ledger gets updated across the network, to see how this system works:

Assume that there are three candidates—Alice, Bob, and Charlie—who are doing some monetary transactions among each other on a blockchain network. Let us go through the transactions step by step to understand blockchain’s open and decentralized features.


Let us assume that Alice had $50 with her, which is the genesis of all transactions and every node is aware of it, as shown in Figure 1-5.

Genesis Alien: tas $50 Alice Ceresis Mire hin SM Dave

Figure 1-5 The genesis block






fhice: bas Sst


Alice has $50

Alice makes a transaction by paying $20 to Bob. Observe how the blockchain gets updated at each node, as shown in

Figure 1-6.

Geronis a Alloa say: S20 to Boh

Alica has 550

Alica -> Bod: $20



Alice has Soi

Allee > Bob: $20


Figure 1-6 The first transaction






Alive has bbU


Boh S20)

Bob makes another transaction by paying $10 to Charlie and the blockchain gets updated as shown in Figure 1-7.

- Genesis Alles say: S20 to Bok | ; _—_—_—_——~ Alicea has So

Aline » Aah: $90

=) La

Dob-» Charlie: $10 Gob-> Charlie: $10

ts Medaes o

Genesis Genesis

Alice has $50

Aline has S50

Mige => Bab: $20

o => q


E> arya eS


Figure 1-7 The second transaction

Please note that the transaction data in the blocks is immutable. All transactions are fully irreversible. Any change would result in a new transaction, which would get validated by all contributing nodes. Every node has its own copy of blockchain.

If there are many questions popping up in your mind, such as “What if Alice pays the same amount to Dave to double- spend the same amount, or what if she is making a payment without having enough funds in her account?,” “How is the security ensured?,” and so on, that is wonderful! We are going to cover those details in the following chapters.

Centralized vs. Decentralized Systems

The very reason we are looking into the debate on centralization vs. decentralization is because blockchain is designed to be decentralized, defying the centralized design. However, the terns decentralized and centralized are not always clear. They are very poorly defined and misleading in many places. The reason is that there is almost no system that is purely centralized or decentralized. Most of the concepts and examples in this section are inspired from the notes of Mr. Vitalik Buterin, the founder of Ethereum blockchain.

What is a distributed system then? Just so it does not mess with the current discussion, let us understand it first and take it off the list. Please note that whether a system is centralized or decentralized, it can still be distributed. A centralized distributed system is one in which there is, say, a master node responsible for breaking down the tasks or data and distribute the load across nodes. On the other hand, a decentralized distributed system is one where there is no “master” node as such and yet the computation may be distributed. Blockchain is one such example, and we will look at many diagrammatic representations of it later in this book. Figure 1-8 is a pictorial representation of how a centralized distributed system may look.


- x & x i x. ne = Y MEISE ook ma ee ss gf Network XN Bias 2 4 | \ ~s je a \ , / 1 \ ~ “4 x 4 + Node-1 Node-2 Node-3 Node-4 Node-N

Figure 1-8 A distributed system with centralized control

This representation is similar to Hadoop implementation, as an example. Though the computation is faster in such designs because of distributed computing, it also suffers from limitations due to centralization .

Let us resume our discussion on centralization vs. decentralization. It is extremely important to note that for a system to be centralized/decentralized is not just limited to the technical architecture. What we intend to say is that a system

can be centralized or decentralized technically, but may not be so logically or politically. Let us take a look at these different perspectives to be able to design a system correctly based on the requirement:

Technical Architecture: A system can be centralized or decentralized from a technical architecture point of view. What we consider is how many physical computers (or nodes) are used to design a system, how many node failures it can sustain before the whole system goes down, etc.

Political perspective: This perspective indicates the control that an individual, or a group of people, or an organization as a whole has on a system. If the computers of the system are controlled by them, then the system is naturally centralized. However, if no specific individual or groups control the system and everyone has equal rights on the system, then it is a decentralized system in a political sense!

Logical perspective: A system can be logically centralized or decentralized based on how it appears to be, irrespective of whether it is centralized or decentralized technically or politically. An alternative analogy could be that if you vertically cut a system (say of computing devices) in half with every half having service providers and consumers, if they can operate as independent units they are decentralized and centralized otherwise.

All the aforementioned perspectives are crucial in designing a real-life system and calling it centralized or decentralized. Let us discuss some of the examples mixing these perspectives to clear up any confusion you might have:

e If you look at the corporates, they are architecturally

centralized (one head office), they are politically centralized (governed by a CEO or the board), and they are logically centralized as well. (You can’t really split them in halves.)

¢ Our language of communication is decentralized from every perspective—architecturally, politically, as well as logically. For two people to communicate with each other, in general, their language is neither politically influenced

nor logically dependent on the language of communication of other people.

e The torrent systems such as BitTorrent are also decentralized from every perspective. Any node can be a provider or a consumer, so even if you cut the system into halves, it still sustains.

e The Content Delivery Network on the other hand is

architecturally decentralized, logically also decentralized, but is politically centralized because it is owned by corporates. An example is Amazon CloudFront.

e Let us consider blockchain now. The objective of blockchain was to enable decentralization. So, it is architecturally decentralized by design. Also, it is decentralized from a political viewpoint, as nobody controls it. However, it is logically centralized, as there is one common agreed state and the whole system behaves like a single global computer.

Let us explore these terms separately and have a comparative view to be able to appreciate why blockchain is decentralized by design.


As the name suggests, a centralized system has a centralized control with all administrative authority. Such systems are easy to design, maintain, impose trust, and govern, but suffer from many inherent limitations, as follows:

e They have a central point of failure, so are less stable. e They are more vulnerable to attack and hence less secured. ¢ Centralization of power can lead to unethical operations.

e Scalability is difficult most of the time.

A typical centralized system may appear as shown in Figure 1-9.


Afome =" Server rene 4 Client-2 Client-3

Figure 1-9 A centralized system


As the name suggests, a decentralized system does not have a centralized control and every node has equal authority. Such systems are difficult to design, maintain, govern, or impose trust. However, they do not suffer from the limitations of conventional centralized systems. Decentralized systems offer the following advantages:

e They do not have a central point of failure, so more stable and fault tolerant

e Attack resistant, as no central point to easily attack and hence more secured

e Symmetric system with equal authority to all, so less scope of unethical operations and usually democratic in nature

A typical decentralized system may appear as shown in Figure 1-10.

Node-5 Node-6

Figure 1-10 A decentralized system

Please note that a distributed system can also be decentralized. An example would be blockchain! However, unlike common distributed systems, the task is not subdivided and delegated to nodes, as there is no master who would do that in blockchain. The contributing nodes do not work on a portion of the work, rather, the interested nodes (or the ones chosen at random) perform the entire work. A typical decentralized and distributed system, which is effectively a peer-to-peer system, may appear as shown in Figure 1-11.

Figure 1-11 A decentralized and peer-to-peer system

Layers of Blockchain

As of this writing, the public blockchain variants such as Ethereum are in the process of maturing, and building complex applications on top of these blockchains may not be a good idea. Keep in mind that blockchain is never just a piece of technology, but a combination of business principles, economics, game theory, cryptography, and computer science engineering. Most of the real-world applications are quite complex in nature, and it is advisable to build blockchain solutions from the ground up.

The purpose of this section is only to provide you with a bird’s eye view of various blockchain layers, and delve deeper into the core fundamentals in the following chapters. To start with, let us just recollect our basic understanding of the TCP/IP protocol stack. The layered approach in the TCP/IP stack is actually a standard to achieve an open system. Having abstraction layers not only helps in understanding the stack better, but also helps in building products that are compliant to the stack to achieve an open system. Also, having the layers

abstract from each other makes the system more robust and easy to maintain. Any change to any of the layers doesn’t impact the other layers. Again, the TCP/IP analogy is not to be confused with the blockchain layers. TCP/IP is a communication protocol that every Internet application uses, and so is blockchain.

Enter the blockchain. There are no agreed global standards yet that would clearly segregate the blockchain components into distinct layers. A layered heterogeneous architecture is needed, but for now that is still in the future. So, we will try to formulate blockchain layers to be able to understand the technology better and build a comparative analogy between hundreds of blockchain/Cryptocurrency variants out there in the market. Take a look at the high-level, layered representation of blockchain in Figure 1-12.

Application Layer is Execution Layer #. Semantic Layer aR Propagation Layer OF Consensus Layer eS

Figure 1-12 Various layers of blockchain

You may be wondering why five layers and why not more granular layers, or fewer layers. Obviously, there cannot be too many or too few layers; it is going to be a trade-off driven among complexity, robustness, adaptability, etc., to name a few. The purpose again is not really to standardize blockchain

technology, but to build a better understanding. Please keep in mind that all these layers are present on all the nodes.

In Chapter 6 of this book, we will be building a decentralized application from scratch and learning how blockchain functions on all these layers with a practical use case.


Because of the characteristics of blockchain, such as immutability of data, transparency among participants, resilience against adversarial attacks etc., there are multiple applications being built. Certain applications are just built in the application layer, taking for granted any available “flavor” of blockchain, and some applications are built in the application layer and are interwoven with other layers in blockchain. This is the reason the application layer should be considered a part of blockchain.

This is the layer where you code up the desired functionalities and make an application out of it for the end users. It usually involves a traditional tech stack for software development such as client-side programming constructs, scripting, APIs, development frameworks, etc. For the applications that treat blockchain as a backend, those applications might need to be hosted on some web servers and that might require web application development, server-side programming, and APIs, etc. Ideally, good blockchain applications do not have a client-server model, and there are no centralized servers that the clients access, which is just the way Bitcoin works.

You probably have heard or already learned about the off- chain networks. The idea is to build applications that wouldn’t use blockchain for anything and everything, but use it wisely. In other words, this concept is to ensure that the heavy lifting is done at the application layer, or bulky storage requirements are taken care of off the chain so that the core blockchain is light and effective and the network traffic is not too much.


The Execution Layer is where the executions of instructions ordered by the Application Layer take place on all the nodes in a blockchain network. The instructions could be simple instructions or a set of multiple instructions in the form of a smart contract . In either case, a program or a script needs to be executed to ensure the correct execution of the transaction. All the nodes in a blockchain network have to execute the programs/scripts independently. Deterministic execution of programs/scripts on the same set of inputs and conditions always produces the same output on all the nodes, which helps avoid inconsistencies.

In the case of Bitcoins, these are simple scripts that are not Turing-complete and allow only a few set of instructions. Ethereum and Hyperledger, on the other hand, allow complex executions. Ethereum’s code or its smart contracts written in solidity gets compiled to Bytecode or Machine Code that gets executed on its own Ethereum Virtual Machine. Hyperledger has a much simpler approach for its chaincode smart contracts. It supports running of compiled machine codes inside docker images, and supports multiple high-level lanuages such as Java and Go.


The Semantic Layer is a logical layer because there is an orderliness in the transactions and blocks. A transaction, whether valid or invalid, has a set of instructions that gets through the Execution Layer but gets validated in the Semantic Layer. If it is Bitcoin, then whether one is spending a legitimate transaction, whether it is a double-spend attack, whether one is authorized to make this transaction, etc., are validated in this layer. You will learn in the following chapters that Bitcoins are actually present as transactions that represent the system state. To be able to spend a Bitcoin, you have to consume one or more previous transactions and there is no notion of Accounts. This means that when someone makes a transaction, they use one of the previous transactions where they had received at least the amount they are spending now. This transaction must be validated by all the nodes by traversing previous

transactions to see if it is a legitimate transaction. Ethereum, on the other hand, has the system of Accounts. This means that the account of the one making the transaction and that of the one receiving it both get updated.

In this layer, the rules of the system can be defined, such as data models and structures. There could be situations that are a little more complex compared with simple transactions. Complex instruction sets are often coded into smart contracts. The system’s state gets updated when a smart contract is invoked upon receiving a transaction. A smart contract is a special type of account that has executable code and private states. A block usually contains a bunch of transactions and some smart contracts. The data structures such as the Merkle tree are defined in this layer with the Merkle root in the block header to maintain a relation between the block headers and the set of transactions in a block (usually Key-Value storage on disk). Also, the data models, storage modes, in-memory/disk based processing, etc. can be defined in this logical layer.

Apart from the aforementioned, it is the semantic layer that defines how the blocks are linked with each other. Every block in a blockchain contains the hash of the previous block, all the way to the genesis block. Though the final state of the blockchain is achieved by the contributions from all the layers, the linking of blocks with each other needs to be defined in this layer. Depending on the use case, you might want to code up an additional functionality in this layer.


The previous layers were more of an individual phenomenon: not much coordination with other nodes in the system. The Propagation Layer is the peer-to-peer communication layer that allows the nodes to discover each other, and talk and sync with each other with respect to the current state of the network. When a transaction is made, we know that it gets broadcast to the entire network. Similarly, when a node wants to propose a valid block, it gets immediately propagated to the entire network so that other nodes could build on it, considering it as the latest block. So, transaction/block propagation in the

network is defined in this layer, which ensures stability of the whole network. By design, most of the blockchains are designed such that they forward a transaction/block immediately to all the nodes they are directly connected to, when they get to know of a new transaction/block.

In the asynchronous Internet network , there are often latency issues for transaction or block propagation. Some propagations occur within seconds and some take more time, depending on the capacity of the nodes, network bandwidth, and a few more factors.


The Consensus Layer is usually the base layer for most of the blockchain systems. The primary purpose of this layer is to get all the nodes to agree on one consistent state of the ledger. There could be different ways of achieving consensus among the nodes, depending on the use case. Safety and security of the blockchain is accertained in this layer. In Bitcoin or Ethereum, the consensus is achieved through proper incentive techniques called “mining.” For a public blockchain to be self-sustainable, there has to be some sort of incentivization mechanisms that not only helps in keeping the network alive, but also enforces consensus . Bitcoin and Ethereum use a Proof of Work (PoW) consensus mechanism to randomly select a node that can propose a block. Once that block is proposed and propagated to all the nodes, they check to see if it a valid block with all legitimate transactions and that the PoW puzzle was solved properly; they add this block to their own copy of blockchain and build further on it. There are many different variants of consensus protocols such as Proof of Stake (PoS), deligated PoS (dPoS), Practical Byzantine Fault Tolerance (PBFT), etc., which we will cover in great detail in the following chapters.

Why is Blockchain Important?

We looked at the design aspects of centralized and decentralized systems and got some idea of the technical

benefits of decentralized systems over centralized ones. We also learned about different layers of blockchain. Blockchain, being a decentralized peer-to-peer system, has some inherent benefits and complexities. Keep in mind that it is not a silver bullet that can address all the problem areas in the world, but there are specific cases where it is the need of the hour. There are also scenarios where blockchainizing the existing solution makes it more robust, transparent, and secured. However, it can as well lead to disaster if not done the right way! Let us now keep a business and functional perspective in mind and analyze blockchain.


If you take a quick glance at the software evolution landscape, you will see that many software solutions have a centralized design. The reason is not just because they are easy to develop and maintain, but because we are used to such a design to be able to trust the system. We always need a trusted third party who can assure we are not being cheated or becoming victims of a scam. Without a prior business relationship, it is difficult to trade with someone or even scale up. One would probably not do business with someone they have never known.

Let us take an example to understand it better. Today when we order something from Amazon, we feel safe and assured of the item’s delivery. The producer of the item is someone and the buyer is someone else. Then what role is being played by Amazon here? It is there as an enabler functioning as a trusted intermediary, and also to take some cut of the transaction. The buyer trusts the seller where the trust relation is actually imposed by such trusted third parties. What blockchain proposes is that, in the modern digital era, we do not really need a third party in between to impose trust, and the technology has matured enough to handle it. In blockchain, trust is an inherent part of the network by default, which we will explore more in upcoming chapters.

Let us quickly learn a few downsides of a conventional centralized system:

e Trust issues e Security issue e Privacy issue—data sale privacy is being undermined

e Cost and time factor for transactions

Some of the advantages of decentralized systems over centralized systems could be:

e Elimination of intermediaries

e Easier and genuine verification of transactions e Increased security with lower cost

e Greater transparency

e Decentralized and immutable


Blockchain came along with Bitcoin, a digital cryptocurrency, in 2009 via a simple mailing list. Soon after it was launched, people could realize its true potential beyond just cryptocurrency. Some companies came up with different flavors of blockchain offerings such as Ethereum, Hyperledger, etc. Microsoft and IBM came up with SaaS (Software as a Service) offerings on their Azure and Bluemix cloud platforms, respectively. Different start-ups were formed, and many established companies took blockchain initiatives that focused on solving some business problems that were not easily solved before.

It is too late now to just say that blockchain has tremendous potential to disrupt almost every industry in some way or the other; the revolution has already started. It has hugely impacted the financial services market. It is difficult to name a global bank or finance entity not exploring blockchain. Apart from the financial market, initiatives have already been/are already being taken in areas such as media and entertainment,

energy trading, prediction markets, retail chains, loyalty rewards systems, insurance, logistics and supply chains, medical records, and also government and military applications.

As of this writing, the current situation is such that many start-ups and companies are able to see how a blockchain- based system can really address some pain areas and become beneficial in many ways. However, designing the right kind of blockchain solution is quite challenging. There are some really great ideas for a blockchain-based product or solution, but it is equally difficult to either build them or implement them. There are some use cases that can only be built on a public blockchain. Designing a self-sustainable blockchain with a proper mining ecosystem is difficult, and when it comes to the existing public blockchains to build non-cryptocurrency applications there is none other than Ethereum. Whether a blockchain application is to be built in the Application Layer only and use the underlying layers as they are, or the application needs to be built from the ground up, is something difficult to decid