Book Image

Mastering Blockchain - Fourth Edition

By : Imran Bashir
5 (3)
Book Image

Mastering Blockchain - Fourth Edition

5 (3)
By: Imran Bashir

Overview of this book

Blockchain is the backbone of cryptocurrencies, it has had a massive impact in many sectors, including finance, supply chains, healthcare, government, and media. It’s also being used for cutting edge technologies such as AI and IoT. This new edition is thoroughly revised to offer a practical approach to using Ethereum, Hyperledger, Fabric, and Corda with step-by-step tutorials and real-world use-cases to help you understand everything you need to know about blockchain development and implementation. With new chapters on Decentralized Finance and solving privacy, identity, and security issues, as well as bonus online content exploring alternative blockchains, this is an unmissable read for everyone who wants to gain a deep understanding of blockchain. The book doesn’t shy away from advanced topics and practical expertise, such as decentralized application (DApp) development using smart contracts and oracles, and emerging trends in the blockchain space. Throughout the book, you’ll explore blockchain solutions beyond cryptocurrencies, such as the IoT with blockchain, enterprise blockchains, and tokenization, and gain insight into the future scope of this fascinating and disruptive technology. By the end of this blockchain book, you will have gained a thorough comprehension of the various facets of blockchain and understand the potential of this technology in diverse real-world scenarios.
Table of Contents (24 chapters)
23
Index

Introducing blockchain

In 2008, a groundbreaking paper, entitled Bitcoin: A Peer-to-Peer Electronic Cash System, was written on the topic of peer-to-peer e-cash under the pseudonym of Satoshi Nakamoto.

No one knows the actual identity of Satoshi Nakamoto. After introducing Bitcoin in 2009, Nakamoto remained active in the Bitcoin developer community until 2011, before handing over Bitcoin development to its core developers and simply disappearing.

The paper introduced the term chain of blocks, later to evolve into “blockchain”, where a chronologically ordered sequence of blocks containing transactions is produced by the protocol. The paper is available at https://bitcoin.org/bitcoin.pdf.

There are some different ways that blockchain may be defined; the following are two of the most widely accepted definitions:

  • Layman’s definition: Blockchain is an ever-growing, secure, shared recordkeeping system in which each user of the data holds a copy of the records, which can only be updated if a majority of parties involved in a transaction agree to update.
  • Technical definition: Blockchain is a peer-to-peer, distributed ledger that is cryptographically secure, append-only, immutable (extremely hard to change), and updateable only via consensus among peers.

Now, let’s examine things in some more detail. We will look at the keywords from the technical definition one by one:

  • Peer-to-peer: The first keyword in the technical definition is peer-to-peer, or P2P. This means that there is no central controller in the network, and all participants (nodes) talk to each other directly. This property allows transactions to be conducted directly among the peers without third-party involvement, such as by a bank.
  • Distributed ledger: Dissecting the technical definition further reveals that blockchain is a “distributed ledger,” which means that a ledger is spread across the network among all peers in the network, and each peer holds a copy of the complete ledger.
  • Cryptographically secure: Next, we see that this ledger is “cryptographically secure,” which means that cryptography has been used to provide security services that make this ledger secure against tampering and misuse. These services include non-repudiation, data integrity, and data origin authentication.
  • Append-only: Another property that we encounter is that blockchain is “append-only,” which means that data can only be added to the blockchain in time-sequential order. This property implies that once data is added to the blockchain, it is almost impossible to change that data and it can be considered practically immutable. In other words, blocks added to the blockchain cannot be changed, which allows the blockchain to become an immutable and tamper-proof ledger of transactions.

    A blockchain can be changed in rare scenarios where collusion against the blockchain network by bad actors succeeds in gaining more than 51% of the authority. There may also be some legitimate reasons to change data in the blockchain once it has been added, such as the “right to be forgotten” or “right to erasure” (also defined in the GDPR ruling: https://gdpr-info.eu/art-17-gdpr/). The right to be forgotten is the right that mandates personal data about a person to be removed from internet records, organizational records, and other associated services. However, those are individual cases that need to be handled separately and that require an elegant technical solution.

  • Updatable via consensus: The most critical attribute of a blockchain is that it is updateable only via consensus. This is what gives it the power of decentralization. In this scenario, no central authority is in control of updating the ledger. Instead, any update made to the blockchain is validated against strict criteria defined by the blockchain protocol and added to the blockchain only after consensus has been reached among a majority of participating peers/nodes on the network. To achieve consensus, there are various consensus algorithms that ensure all parties agree on the final state of the data on the blockchain network and resolutely agree upon it to be true.

Having detailed the primary features of blockchain, we are now able to begin to look at its actual architecture.

Blockchain architecture

We’ll begin by looking at how blockchain acts as a layer within a distributed peer-to-peer network.

Blockchain by layers

Blockchain can be thought of as a layer of a distributed peer-to-peer network running on top of the internet, as can be seen in the following diagram. It is analogous to SMTP, HTTP, or FTP running on top of TCP/IP:

Graphical user interface  Description automatically generated with low confidence

Figure 1.4: The layered architectural view of a generic blockchain

Now we’ll discuss all these elements one by one:

  • The lowest layer is the Network layer, which is usually the internet and provides a base communication layer for any blockchain.
  • A P2P (peer-to-peer) network runs on top of the Network layer, which consists of information propagation protocols such as gossip or flooding protocols.
  • After this comes the Cryptography layer, which contains crucial cryptographic protocols that ensure the security of the blockchain. These cryptographic protocols play a vital role in the integrity of blockchain processes, secure information dissemination, and blockchain consensus mechanisms. This layer consists of public key cryptography and relevant components such as digital signatures and cryptographic hash functions. Sometimes, this layer is abstracted away, but it has been included in the diagram because it plays a fundamental role in blockchain operations.
  • Next comes the Consensus layer, which is concerned with the usage of various consensus mechanisms to ensure agreement among different participants of the blockchain. This is another crucial part of the blockchain architecture, which consists of various techniques such as SMR, proof-based consensus mechanisms, or traditional (from traditional distributed systems research) Byzantine fault-tolerant consensus protocols.
  • We then have the Execution layer, which can consist of virtual machines, blocks, transactions, and smart contracts. This layer, as the name suggests, provides execution services on the blockchain, and performs operations such as value transfer, smart contract execution, and block generation. Virtual machines such as Ethereum Virtual Machine (EVM), Ethereum WebAssembly (ewasm), and Zinc VM provide an execution environment for smart contracts to execute.
  • Finally, we have the Applications layer, which is composed of smart contracts, decentralized applications, DAOs, and autonomous agents. This layer can effectively contain all sorts of various user-level agents and programs that operate on the blockchain. Users interact with the blockchain via decentralized applications.

All these concepts will be discussed in detail later in this book in various chapters. Next, we’ll look at blockchain from more of a business-oriented perspective.

Blockchain in business

The current traditional business model is centralized. For example, for cash transfers, banks act as a central trusted third party. In financial trading, a central clearing house acts as a trusted third party between two or more trading parties. From a business standpoint, a blockchain can be defined as a platform where peers can exchange value using transactions without the need for a centrally trusted arbitrator (a trusted third party). This concept is compelling, and, once you absorb it, you will realize the enormous potential of blockchain technology. This disintermediation allows blockchain to be a decentralized mechanism where no single authority controls the network. Immediately, we can see a significant benefit of decentralization here, because if no banks or central clearing houses are required, then it naturally leads to cost savings, faster transaction speeds, transparency, and more trust. Moreover, in the payment business, blockchain can be used to facilitate cross-border and local payments in a decentralized and secure manner.

We’ve now looked at what blockchain is at a fundamental level. Next, we’ll go a little deeper and look at some of the elements that comprise a blockchain.

Generic elements of a blockchain

Now, let’s walk through the generic elements of a blockchain. You can use this as a handy reference section if you ever need a reminder about the different parts of a blockchain. More precise elements will be discussed in the context of their respective blockchains in later chapters, for example, the Ethereum blockchain. The structure of a generic blockchain can be visualized with the help of the following diagram:

A picture containing calendar  Description automatically generated

Figure 1.5: Generic structure of a blockchain

Elements of a generic blockchain are described here one by one. These are the elements that you will come across in relation to blockchain:

  • Address: Addresses are unique identifiers used in a blockchain transaction to denote senders and recipients. An address is usually a public key or derived from a public key.
  • Transaction: A transaction is the fundamental unit of a blockchain. A transaction represents a transfer of value from one address to another.
  • Block: A block is composed of multiple transactions and other elements, such as the previous block hash (hash pointer), timestamp, and nonce. A block is composed of a block header and a selection of transactions bundled together and organized logically. A block contains several elements, which we introduce as follows:
    • A reference to a previous block is also included in the block unless it is a genesis block. This reference is the hash of the header of the previous block. A genesis block is the first block in the blockchain that is hardcoded at the time the blockchain was first started. The structure of a block is also dependent on the type and design of a blockchain.
    • A nonce is a number that is generated and used only once. A nonce is used extensively in many cryptographic operations to provide replay protection, authentication, and encryption. In blockchain, it’s used in PoW consensus algorithms and for transaction replay protection. A block also includes the nonce value.
    • A timestamp is the creation time of the block.
    • Merkle root is a hash of all the nodes of a Merkle tree. In a blockchain block, it is the combined hash of the transactions in the block. Merkle trees are widely used to validate large data structures securely and efficiently. In the blockchain world, Merkle trees are commonly used to allow the efficient verification of transactions. Merkle root in a blockchain is present in the block header section of a block, which is the hash of all transactions in a block. This means that verifying only the Merkle root is required to verify all transactions present in the Merkle tree instead of verifying all transactions one by one.
    • In addition to the block header, the block contains transactions that make up the block body. A transaction is a record of an event, for example, the event of transferring cash from a sender’s account to a beneficiary’s account. A block contains transactions, and its size varies depending on the type and design of the blockchain. For example, the Bitcoin block size is limited to one megabyte, which includes the block header of 80 bytes and transactions.

The following structure is a simple block diagram that depicts a generic block:

A picture containing table  Description automatically generated

Figure 1.6: The generic structure of a block

Generally, there are just a few attributes that are essential to the functionality of a block: the block header, which is composed of the hash of the previous block’s header, the timestamp, nonce, Merkle root, and the block body that contains the transactions. There are also other attributes in a block, but generally, the components introduced in this section are usually available in a block:

  • Peer-to-peer network: As the name implies, a peer-to-peer network is a network topology wherein all peers can communicate with each other directly and send and receive messages.
  • The scripting or programming language: Scripts or programs perform various operations on a transaction to facilitate various functions. For example, in Bitcoin, transaction scripts are predefined in a language called Script, which consists of sets of commands that allow nodes to transfer bitcoins from one address to another. Script is a limited language in the sense that it only allows essential operations that are necessary for executing transactions, but it does not allow arbitrary program development.

    Think of the scripting language as a calculator that only supports standard preprogrammed arithmetic operations. As such, the Bitcoin Script language cannot be called “Turing complete.” In simple words, a Turing complete language means that it can perform any computation. It is named after Alan Turing, who developed the idea of a Turing machine, which can run any algorithm, however complex. Turing-complete languages need loops and branching capabilities to perform complex computations. Therefore, Bitcoin’s scripting language is not Turing complete, whereas Ethereum’s Solidity language is.

    To facilitate arbitrary program development on a blockchain, a Turing-complete programming language is needed, and it is now a very desirable feature to have for blockchains. Think of this as a computer that allows the development of any program using programming languages.

  • Virtual machine: This is an extension of the transaction script introduced previously. A virtual machine allows Turing-complete code to be run on a blockchain (as smart contracts), whereas a transaction script is limited in its operation. However, virtual machines are not available on all blockchains. Various blockchains use virtual machines to run programs such as EVM and Chain Virtual Machine (CVM). EVM is used in the Ethereum blockchain, while CVM is a virtual machine developed for and used in an enterprise-grade blockchain called “Chain Core.”
  • State machine: A blockchain can be viewed as a state transition mechanism whereby a state is modified from its initial form to the next one by nodes on the blockchain network as a result of transaction execution.
  • Smart contracts: These programs run on top of the blockchain and encapsulate the business logic to be executed when certain conditions are met. These programs are enforceable and automatically executable. The smart contract feature is not available on all blockchain platforms, but it is now becoming a very desirable feature due to the flexibility and power that it provides to blockchain applications. Smart contracts have many use cases, including but not limited to identity management, capital markets, trade finance, record management, insurance, and e-governance. Smart contracts will be discussed in more detail in Chapter 8, Smart Contracts.
  • Node: A node in a blockchain network performs various functions depending on the role that it takes on. A node can propose and validate transactions and perform mining to facilitate consensus and secure the blockchain. This goal is achieved by following a consensus protocol (most commonly PoW). Nodes can also perform other functions, such as simple payment verification (lightweight nodes), validation, and many other functions depending on the type of blockchain used and the role assigned to the node. Nodes also perform a transaction signing function. Transactions are first created by nodes and then also digitally signed by nodes using private keys as proof that they are the legitimate owner of the asset that they wish to transfer to someone else on the blockchain network. This asset is usually a token or virtual currency, such as Bitcoin, but it can also be any real-world asset represented on the blockchain by using tokens. There are also now standards related to tokens; for example, on Ethereum, there are ERC20, ERC721, ERC777, and a few others that define the interfaces and semantics of tokenization.

A high-level diagram of blockchain architecture highlighting the key elements mentioned previously is shown as follows:

Diagram  Description automatically generated

Figure 1.7: Generic structure of a blockchain network

The preceding diagram shows a four-node blockchain network (at the top), each maintaining a chain of blocks, virtual machine, state machine, and address. The blockchain is then further magnified (middle) to show the structure of the chain of blocks, which is again magnified (bottom) to show the structure of a transaction. Note that this is a generic structure of a blockchain; we will see specific blockchains structures in detail in the context of Ethereum and Bitcoin blockchains later in this book.

Blockchain functionality

We have now defined and described blockchain. Now, let’s see how a blockchain works. Nodes are either miners that create new blocks and mint cryptocurrency (coins) or block signers that validate and digitally sign the transactions. A critical decision that every blockchain network must make is to figure out which node will append the next block to the blockchain. This decision is made using a consensus mechanism.

Consensus is a process of achieving agreement between distrusting nodes on the final state of data. To achieve consensus, different algorithms are used. It is easy to reach an agreement in a centralized network (in client-server systems, for example), but when multiple nodes are participating in a distributed system and they need to agree on a single value, it becomes quite a challenge to achieve consensus. This process of attaining agreement on a common state or value among multiple nodes is known as distributed consensus. If faults are allowed, then we call such a mechanism fault tolerant distributed consensus, where despite the failure of some nodes, agreement is reached between them.

Now, we will look at how a blockchain validates transactions and creates and adds blocks to grow the blockchain, using a general scheme for creating blocks:

  1. Transaction is initiated: A node starts a transaction by first creating it and then digitally signing it with its private key. A transaction can represent various actions in a blockchain. Most commonly, this is a data structure that represents the transfer of value between users on the blockchain network. The transaction data structure usually consists of some logic of transfer of value, relevant rules, source and destination addresses, and other validation information. Transactions are usually either a cryptocurrency transfer (transfer of value) or a smart contract invocation that can perform any desired operation. A transaction occurs between two or more parties. This will be covered in more detail in specific chapters on Bitcoin and Ethereum later in the book.
  2. Transaction is validated and broadcast: A transaction is propagated (broadcast) usually by using data-dissemination protocols, such as the Gossip protocol, to other peers that validate the transaction based on preset validity criteria. Before a transaction is propagated, it is also verified to ensure that it is valid.
  3. Find new block: When the transaction is received and validated by special participants called miners on the blockchain network, it is included in a block, and the process of mining starts. This process is also sometimes referred to as “finding a new block.” Here, nodes called miners race to finalize the block they’ve created by a process known as mining.
  4. New block found: Once a miner solves a mathematical puzzle (or fulfills the requirements of the consensus mechanism implemented in a blockchain), the block is considered “found” and finalized. At this point, the transaction is considered confirmed. Usually, in cryptocurrency blockchains such as Bitcoin, the miner who solves the mathematical puzzle is also rewarded with a certain number of coins as an incentive for their effort and the resources they spent in the mining process.
  5. Add new block to the blockchain: The newly created block is validated, transactions or smart contracts within it are executed, and it is propagated to other peers. Peers also validate and execute the block. It now becomes part of the blockchain (ledger), and the next block links itself cryptographically back to this block. This link is called a hash pointer.

This process can be visualized in the diagram as follows:

Diagram, schematic  Description automatically generated

Figure 1.8: How a block is generated

This completes the basic introduction to blockchain. In the next section, you will learn about the benefits and limitations of this technology.

Benefits and features of blockchain

Numerous advantages of blockchain technology have been discussed in many industries and proposed by thought leaders around the world who are participating in the blockchain space. The notable benefits of blockchain technology are as follows:

  • Simplification of current paradigms: The current blockchain model in many industries, such as finance or health, is somewhat disorganized. In this model, multiple entities maintain their own databases and data sharing can become very difficult due to the disparate nature of the systems. However, as a blockchain can serve as a single shared ledger among many interested parties, this can result in simplifying the model by reducing the complexity of managing the separate systems maintained by each entity.
  • Faster dealings: In the financial industry, especially in post-trade settlement functions, blockchain can play a vital role by enabling the quick settlement of trades. Blockchain does not require a lengthy process of verification, reconciliation, and clearance because a single version of agreed-upon data is already available on a shared ledger between financial organizations.
  • Cost-saving: As no trusted third party or clearing house is required in the blockchain model, this can massively reduce overhead costs in the form of the fees, which are paid to such parties.
  • Smart property: It is possible to link a digital or physical asset to the blockchain in such a secure and precise manner that it cannot be claimed by anyone else. You are in full control of your asset, and it cannot be double-spent or double-owned. Compare this with a digital music file, for example, which can be copied many times without any controls.

    While it is true that many Digital Rights Management (DRM) schemes are being used currently along with copyright laws, none of them are enforceable in the way a blockchain-based DRM can be. Blockchain can provide digital rights management functionality in such a way that it can be enforced fully: if you own an asset, no one else can claim it unless you decide to transfer it. This feature has far-reaching implications, especially in DRM and e-cash systems where double-spend detection is a crucial requirement.

  • Decentralization: This is a core concept and benefit of blockchain. There is no need for a trusted third party or intermediary to validate transactions; instead, a consensus mechanism is used to agree on the validity of transactions.
  • Transparency and trust: As blockchains are shared and everyone can see what is on the blockchain, this allows the system to be transparent. As a result, trust is established. This is more relevant in scenarios such as the disbursement of funds or benefits where personal discretion in relation to selecting beneficiaries needs to be restricted.
  • Immutability: Once the data has been written to the blockchain, it is extremely difficult to change it back. It is not genuinely immutable, but because changing data is so challenging and nearly impossible, this is seen as a benefit to maintaining an immutable ledger of transactions and is especially useful in audit and compliance scenarios.
  • High availability: As the system is based on thousands of nodes in a peer-to-peer network, and the data is replicated and updated on every node, the system becomes highly available. Even if some nodes leave the network or become inaccessible, the network continues to work, thus making it highly available. This redundancy results in high availability.
  • Highly secure: All transactions on a blockchain are cryptographically secured and thus provide network integrity. Any transactions posted from the nodes on the blockchain are verified based on a predetermined set of rules. Only valid transactions are selected for inclusion in a block. The blockchain is based on proven cryptographic technology that ensures the integrity and availability of data. Generally, confidentiality is not provided due to the requirements of transparency. This limitation is the leading barrier to its adoption by financial institutions and other industries that require the privacy and confidentiality of transactions. As such, the privacy and confidentiality of transactions on the blockchain are being researched very actively, and advancements are already being made. It could be argued that, in many situations, confidentiality is not needed, and transparency is preferred. For example, with Bitcoin, confidentiality is not an absolute requirement; however, it is desirable in some scenarios. A more recent example is Zcash (https://z.cash), which uses zero-knowledge proofs to provide a platform for conducting anonymous transactions. Other security services, such as non-repudiation and authentication, are also provided by blockchain, as all actions are secured using private keys and digital signatures.
  • Platform for smart contracts: Smart contracts are automated, autonomous programs that reside on the blockchain network and encapsulate the business logic and code needed to execute a required function when certain conditions are met. This is indeed a revolutionary feature of blockchain, as it provides flexibility, speed, security, and automation for real-world scenarios that can lead to a completely trustworthy system with significant cost reductions. Smart contracts can be programmed to perform any application-level actions that blockchain users need and according to their specific business requirements.

Not all blockchains have a mechanism to execute smart contracts; however, this is a very desirable feature. However, note that some blockchains may not incorporate smart contract functionality on purpose, citing the reason that hardcoded executions are faster without the complexities of general-purpose smart contracts.

Limitations of blockchain technology

As with any technology, some challenges need to be addressed to make a system more robust, useful, and accessible. Blockchain technology is no exception. In fact, much effort is being made in both academia and industry to overcome the challenges posed by blockchain technology.

The most sensitive blockchain problems are as follows:

  • Scalability: Currently, blockchain networks are not as scalable as, for example, current financial networks. This is a known area of concern and a very ripe area for research.
  • Regulation: Due to its decentralized nature, regulation is almost impossible on blockchain. This is sometimes seen as a barrier toward adoption because, traditionally, due to the existence of regulatory authorities, consumers have a certain level of confidence that if something goes wrong they can hold someone accountable. However, in blockchain networks, no such regulatory authority and control exists, which is an inhibiting factor for many consumers.

    Note that there are attempts to regulate these networks, including the legalization of cryptocurrency exchanges in the US under the Bank secrecy act, so that these exchanges adhere to requirements such as anti-money laundering (AML), Counter Financing of Terrorism (CFT), and enforce strict record keeping and reporting to the authorities. Also, a promising use case enabled by blockchain is central bank digital currency (CBDC), which is a centralized and regulated form of digital currency issued by a central bank of a country.

  • Privacy: Privacy is a concern on public blockchains such as Bitcoin where everyone can see every single transaction. This transparency is not desirable in many industries, such as the financial, law, or medical sectors. This is also a known concern and a lot of valuable research with some very impressive solutions has already been developed. A promising technology called zero-knowledge proofs is being utilized on blockchains to provide privacy. Further research continues to make these technologies better and more and more practical and mainstream.
  • Relatively immature technology: As compared to traditional IT systems that have benefited from decades of research and evolution, blockchain is a comparatively new technology and requires research to achieve maturity. Even though core aspects of blockchains with the latest and most novel chains, such as Solana, Polkadot, and Avalanche, are very much mature; however, some features need to be improved further for example user experience, developer experience, security, and interoperability. From another angle, applications such as DeFi and Metaverses also need further maturity in terms of regulation, governance, and security.
  • Interoperability: This problem is twofold. First is the interoperability of blockchains with enterprise and legacy systems, and second is the interoperability between different blockchains (cross-chain interoperability). Both these aspects are important to consider because blockchains cannot exist in silos; they must be able to communicate with one another, as well as enterprise networks and legacy systems, to enable enterprises to fully benefit from the technology. This is an active area of research, with many types of solutions becoming available in recent years, such as bridges, hub blockchains, and multi-chain hetereogeneous chains.
  • Adoption: Often, blockchain is seen as a nascent technology. Even though this perspective has changed rapidly in the last few years, there is still a long way to go before the mass adoption of this technology. Some aspects, such as scalability, security, regulation, and customer confidence, need to be addressed before further adoption. Customer confidence can decrease due to security issues leading to loss of funds. Such problems may inhibit some users from joining the network, who would otherwise be happy to join if this security problem didn’t exist. However, note that despite these problems, DeFi is becoming more and more popular, but it might discourage cautious customers.

All these issues and possible solutions will be discussed in detail later in this book.

You now know the basics of blockchain and its benefits and limitations. Now, let’s look at the various types of blockchain that exist.