Mastering Blockchain - Imran Bashir - E-Book

Mastering Blockchain E-Book

Imran Bashir

0,0
35,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 1354

Veröffentlichungsjahr: 2023

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Mastering Blockchain

Fourth Edition

Inner workings of blockchain, from cryptography and decentralized identities, to DeFi, NFTs and Web3

Imran Bashir

BIRMINGHAM—MUMBAI

Mastering Blockchain

Fourth Edition

Copyright © 2023 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Senior Publishing Product Manager: Devika Battike

Acquisition Editor – Peer Reviews: Gaurav Gavas

Project Editor: Meenakshi Vijay

Content Development Editor: Rebecca Robinson

Copy Editor: Safis Editing

Technical Editor: Karan Sonawane

Proofreader: Safis Editing

Indexer: Manju Arasan

Presentation Designer: Rajesh Shirsath

Developer Relations Marketing Executive: Vidhi Vashisth

First published: March 2017

Second edition: March 2018

Third edition: August 2020

Fourth edition: March 2023

Production reference: 2040723

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN: 978-1-80324-106-7

www.packt.com

Disclaimer: The information and viewpoints expressed in this book are those of the author and not necessarily those of any of the author’s employers or their affiliates.

This book is dedicated with immeasurable love and gratitude to my beloved father.The most affectionate, selfless, and hardworking man, who sacrificed everything for me.

Don’t worry, my son! Adversities come and go.

—Scientist Bashir Ahmed Khan

76 → 363 → 1245 → 1003 → 275 → 77 → 60 → 588 → 1 → 12 → 12 → 2215 →

Contributors

About the author

Imran Bashir has an MSc in Information Security from Royal Holloway, University of London. He has a background in software development, solution architecture, infrastructure management, information security, and IT service management. His current focus is on the latest technologies, such as blockchain and quantum computing. He is a member of the Institute of Electrical and Electronics Engineers (IEEE). He has worked in various senior technology roles for different organizations around the world.

I would like to thank Edward Doxey for his dedication and Packt for their support and guidance throughout this project. I also want to thank John Cornyn, who read the previous edition of this book and suggested some corrections, as well as the reviewers of this edition.

I want to thank my wife and children for their support while I was writing, especially during the time I was supposed to spend with them.

Finally, I want to thank my father, who has always been there for me and always will be. And my mother, whose encouragement makes difficult obstacles easy to surmount for me. My parents’ prayers and unconditional love for me have enabled me to achieve anything I desire.

Disclaimer: The information and viewpoints expressed in this book are those of the author and not necessarily those of any of the author’s employers or their affiliates.

About the reviewers

Brian Wu is a senior blockchain architect and consultant. Brian has over 20 years of hands-on experience across various technologies, including blockchain, big data, cloud, AI, systems, and infrastructure. He has worked on more than 50 projects in his career.

He has written nine books, published by O’Reilly, Packt, and Apress, on popular fields within the blockchain domain, including 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.

Shailesh B. Nair is a computer engineer and has worked in software development for the last 21 years. He has been involved in blockchain development for about 8 years using different blockchain frameworks, like Ethereum (L1, L2, L3), Substrate, Polkadot, Solana, Cosmos (IBC), CasperLabs, Tezos, etc using various programming languages like C++, Rust, Golang, Ocaml, and Haskell.

He has worked with many different crypto startups since 2014 and as a blockchain consultant, he has worked with Mina Protocol, FTX, and others.

I would like to thank my friends who have worked with me on projects related to blockchain.

Join us on Discord!

To join the Discord community for this book – where you can share feedback, ask questions to the author, and learn about new releases – follow the QR code below:

https://packt.link/ips2H

Contents

Preface

Who this book is for

What this book covers

To get the most out of this book

Get in touch

Blockchain 101

The growth of blockchain technology

Progress towards maturity

Rising interest

Distributed systems

CAP theorem

PACELC theorem

The history of blockchain

Bitcoin

Electronic cash

Introducing blockchain

Blockchain architecture

Blockchain by layers

Blockchain in business

Generic elements of a blockchain

Blockchain functionality

Benefits and features of blockchain

Limitations of blockchain technology

Types of blockchain

Distributed ledgers

Shared ledger

Public blockchains

Private blockchains

Semi-private blockchains

Permissioned ledger

Fully private and proprietary blockchains

Tokenized blockchains

Tokenless blockchains

Layer 1 blockchains

Monolithic and polylithic blockchains

Layer 2 blockchains

Sidechains

Summary

Decentralization

Introducing decentralization

Methods of decentralization

Disintermediation

Contest-driven decentralization

Quantifying decentralization

Benefits of decentralization

Evaluating requirements

Full-ecosystem decentralization

Storage

Communication

Computing power

Decentralization in practice

Smart contracts

Autonomous agents

Decentralized organizations

Decentralized autonomous organizations

Decentralized autonomous corporations

Decentralized autonomous societies

Decentralized applications

Criteria for a DApp

Operations of a DApp

Design of a DApp

Innovative trends

Decentralized web

Web 1

Web 2

Web 3

Summary

Symmetric Cryptography

Introducing cryptography

Services provided by cryptography

Cryptographic primitives

Keyless primitives

Random numbers

Hash functions

Symmetric key primitives

Message authentication codes

Secret key ciphers

Advanced Encryption Standard

Data Encryption Standard

How AES works

Encrypting and decrypting using AES

Summary

Asymmetric Cryptography

Foundational mathematics

Asymmetric cryptography

Public and private keys

Asymmetric cryptography algorithms

Integer factorization

Discrete logarithm

Elliptic curves

Integrated encryption scheme

Introducing RSA

Encrypting and decrypting with RSA

Introducing ECC

Mathematics behind ECC

Point addition

Point doubling

Point multiplication

The discrete logarithm problem

Generating keys with ECC

Digital signatures

RSA digital signature algorithms

Generating RSA digital signatures

The elliptic curve digital signature algorithm

Generating ECDSA digital signatures

Different types of digital signatures

Blind signatures

Multisignatures

Threshold signatures

Aggregate signatures

Ring signatures

Cryptographic constructs and blockchain technology

Homomorphic encryption

Secret sharing

Commitment schemes

Zero-knowledge proofs

zk-SNARKs

zk-STARKs

Zero-knowledge range proofs

Encoding schemes

Base64

base58

Verifiable random functions

Summary

Consensus Algorithms

Introducing consensus

Fault tolerance

FLP impossibility

Analysis and design

Model

Processes

Timing assumptions

Classification

Algorithms

CFT algorithms

Paxos

Raft

BFT algorithms

Practical Byzantine Fault Tolerance

Istanbul Byzantine Fault Tolerance

Tendermint

Nakamoto consensus

Variants of PoW

HotStuff

Choosing an algorithm

Finality

Speed, performance, and scalability

Summary

Bitcoin Architecture

Introducing Bitcoin

Cryptographic keys

Private keys in Bitcoin

Public keys in Bitcoin

Addresses

Typical Bitcoin addresses

Advanced Bitcoin addresses

Transactions

Coinbase transactions

The transaction lifecycle

Transaction validation

Transaction fees

The transaction data structure

Metadata

Inputs

Outputs

Verification

The Script language

Opcodes

Standard transaction scripts

Contracts

Transaction bugs

Blockchain

Structure

The genesis block

Stale and orphan blocks

Forks

Properties

Miners

Proof of Work (PoW)

Mining systems

CPU

GPU

FPGAs

ASICs

Mining pools

Network

Types of messages

Client software

Bloom filters

Wallets

Summary

Bitcoin in Practice

Bitcoin in the real world

Bitcoin payments

Innovation in Bitcoin

Bitcoin improvement proposals

Advanced protocols

Segregated Witness

Bitcoin Cash

Bitcoin Unlimited

Bitcoin Gold

Taproot

Extended protocols on top of Bitcoin

Colored coins

Counterparty

Altcoins from Bitcoin

Bitcoin client installation

Types of clients and tools

Setting up a Bitcoin node

Setting up the source code

Setting up bitcoin.conf

Starting up a node in the testnet

Starting up a node in regtest

Experimenting further with bitcoin-cli

Using the Bitcoin command-line tool

Using the JSON-RPC interface

Using the HTTP REST interface

Bitcoin programming

Summary

Smart Contracts

Introducing smart contracts

Definitions

Properties

Real-world application

Ricardian contracts

Smart contract templates

Oracles

Software-and network-assisted proofs

TLSNotary

TLS-N-based mechanism

Hardware device-assisted proofs

Android proof

Ledger proof

Trusted hardware-assisted proofs

Types of blockchain oracles

Inbound oracles

Outbound oracles

Cryptoeconomic oracles

Blockchain oracle services

Deploying smart contracts

The DAO

Advances in smart contract technology

Solana Sealevel

Digital Asset Modeling Language

Summary

Ethereum Architecture

Introducing Ethereum

Cryptocurrency

Keys and addresses

Accounts

Transactions and messages

MPTs

Transaction components

Recursive Length Prefix

Gas

Transaction types

Simple transactions

Contract creation transactions

Message call transactions

Messages

Transaction validation and execution

State and storage in the Ethereum blockchain

The world state

The account state

Transaction receipts

Ethereum virtual machine

Execution environment

The machine state

Blocks and blockchain

The genesis block

Block validation, finalization, and processing

Block difficulty mechanism

Nodes and miners

The consensus mechanism

Forks in the blockchain

The Ethereum network

Main net

Test nets

Private nets

Precompiled smart contracts

Programming languages

Solidity

Runtime bytecode

Opcodes

Wallets and client software

Wallets

Geth

Light clients

Supporting protocols

Whisper

Swarm

Summary

Ethereum in Practice

Ethereum payments

Innovations in Ethereum

Difficulty time bomb

EIP-1559

The merge and upcoming upgrades

Programming with Geth

Installing and configuring the Geth client

Creating a Geth new account

Querying the blockchain using Geth

Geth console

Geth attach

Geth JSON RPC API

Setting up a development environment

Connecting to test networks

Creating a private network

Starting up the private network

Experimenting with the Geth JavaScript console

Mining and sending transactions

Introducing Remix IDE

Interacting with the Ethereum Blockchain with MetaMask

Installing MetaMask

Creating and funding an account with MetaMask

Using MetaMask and Remix IDE to deploy a smart contract

Adding a custom network to MetaMask and connecting it with Remix IDE

Importing accounts into MetaMask using keystore files

Deploying a contract with MetaMask

Interacting with a contract through MetaMask using Remix IDE

Summary

Tools, Languages, and Frameworks for Ethereum Developers

Languages

The Solidity compiler

Installing solc

Experimenting with solc

Tools, libraries, and frameworks

Node.js

Ganache

ganache-cli

Ganache UI

Truffle

Drizzle

Other tools

Contract development and deployment

Writing smart contracts

Testing smart contracts

Deploying smart contracts

The Solidity language

Functions

Variables

Local variables

Global variables

State variables

Data types

Value types

Reference types

Control structures

Events

Inheritance

Libraries

Error handling

Summary

Web3 Development Using Ethereum

Interacting with contracts using Web3 and Geth

Deploying contracts

Using solc to generate ABI and code

Querying contracts with Geth

Interacting with Geth using POST requests

Interacting with contracts via frontends

Installing the web3.js JavaScript library

Creating a web3 object

Creating an app.js JavaScript file

Creating a frontend webpage

Calling contract functions

Creating a frontend webpage

Deploying and interacting with contracts using Truffle

Installing and initializing Truffle

Compiling, testing, and migrating using Truffle

Interacting with the contract

Using Truffle to test and deploy smart contracts

Deployment on decentralized storage using IPFS

Summary

The Merge and Beyond

Introduction

Ethereum after The Merge

The Beacon Chain

Beacon nodes

Consensus client

Execution client

Validator client

Proof-of-stake

P2P interface (networking)

The Merge

Sharding

The future roadmap of Ethereum

Summary

Hyperledger

Projects under Hyperledger

Distributed ledgers

Fabric

Sawtooth

Iroha

Indy

Besu

Libraries

Aries

Transact

Ursa

AnonCreds

Tools

Cello

Caliper

Domain-specific

Grid

Hyperledger reference architecture

Hyperledger design principles

Hyperledger Fabric

Key concepts

Membership service

Blockchain services

Smart contract services

APIs and CLIs

Components

Peers/nodes

Clients

Channels

World state database

Private data collections

Transactions

Membership Service Provider

Smart contracts

Crypto service provider

Applications

Chaincode implementation

The application model

Consensus mechanism

Transaction lifecycle

Fabric 2.0

New chaincode lifecycle management

New chaincode application patterns

Summary

Tokenization

Tokenization on a blockchain

Advantages of tokenization

Disadvantages of tokenization

Types of tokens

Fungible tokens

Non-fungible tokens

Stable tokens

Security tokens

Process of tokenization

Token offerings

Initial coin offerings

Security token offerings

Initial exchange offerings

Equity token offerings

Decentralized autonomous initial coin offering

Other token offerings

Token standards

ERC-20

ERC-223

ERC-777

ERC-721

ERC-884

ERC-1400

ERC-1404

ERC-1155

ERC-4626

Building an ERC-20 token

Building the Solidity contract

Deploying the contract on the Remix JavaScript virtual machine

Adding tokens in MetaMask

Emerging concepts

Tokenomics/token economics

Token engineering

Token taxonomy

Summary

Enterprise Blockchain

Enterprise solutions and blockchain

Success factors

Limiting factors

Requirements

Privacy

Performance

Access governance

Further requirements

Compliance

Interoperability

Integration

Ease of use

Monitoring

Secure off-chain computation

Better tools

Enterprise blockchain versus public blockchain

Enterprise blockchain architecture

Designing enterprise blockchain solutions

TOGAF

Architecture development method (ADM)

Blockchain in the cloud

Currently available enterprise blockchains

Enterprise blockchain challenges

Interoperability

Lack of standardization

Compliance

Business challenges

VMware Blockchain

Components

Consensus protocol

Architecture

VMware Blockchain for Ethereum

Quorum

Architecture

Nodes

Privacy manager

Cryptography

Privacy

Enclave encryption

Transaction propagation to transaction managers

Enclave decryption

Access control with permissioning

Performance

Pluggable consensus

Setting up a Quorum network with IBFT

Installing and running Quorum Wizard

Running a private transaction

Attaching Geth to nodes

Viewing the transaction in Cakeshop

Further investigation with Geth

Other Quorum projects

Remix plugin

Pluggable architecture

Summary

Scalability

What is scalability?

Blockchain trilemma

Methods for improving scalability

Layer 0 – multichain solutions

Polkadot

Layer 1 – on-chain scaling solutions

Layer 2 – off-chain solutions

Layer 2

Rollups

Data validity

Data availability

How rollups work

Types of rollups

Optimistic rollups

ZK-rollups

Technologies used for building ZK-rollups

ZK-ZK-rollups

Optimistic rollups vs ZK-rollups

Fraud and validity proof-based classification of rollups

Example

Layer 3 and beyond

Summary

Blockchain Privacy

Privacy

Anonymity

Confidentiality

Techniques to achieve privacy

Layer 0

Tor

I2P

Indistinguishability obfuscation

Homomorphic encryption

Secure multiparty computation

Trusted hardware-assisted confidentiality

Mixing protocols

CoinSwap

TumbleBit

Dandelion

Confidential transactions

MimbleWimble

Zkledger

Attribute-based encryption

Anonymous signatures

Zether

Privacy using Layer 2 protocols

Privacy managers

Privacy using zero-knowledge

Cryptographic Commitments

Zero-knowledge proofs

Building ZK-SNARKs

Example

Summary

Blockchain Security

Security

Blockchain layers and attacks

Hardware layer

Network layer

Blockchain layer

Attacks on transactions

Transaction replay attacks

Attacks on consensus protocols

Double-spending

Selfish mining

Forking and chain reorganization

Blockchain application layer

Smart contract vulnerabilities

DeFi attacks

Interface layer

Oracle attacks/oracle manipulation attacks

Attacks on wallets

Attacks on layer 2 blockchains

Cryptography layer

Attacking public key cryptography

Attacking hash functions

Key management-related vulnerabilities and attacks

ZKP-related attacks

Security analysis tools and mechanism

Formal verification

Formal verification of smart contracts

Model checking

Smart contract security

Oyente

Solgraph

Threat modeling

Regulation and compliance

Summary

Decentralized Identity

Identity

Digital identity

Centralized identity model

Federated identity model

Decentralized identity model

Self-sovereign identity

Components of SSI

Identity in Ethereum

Identity in the world of Web3, DeFi, and Metaverse

SSI-specific blockchain projects

Hyperledger Indy, Aries, Ursa, and AnonCreds

Other projects

Some other initiatives

Challenges

Summary

Decentralized Finance

Introduction

Financial markets

Trading

Exchanges

Orders and order properties

Order management and routing systems

Components of a trade

Trade lifecycle

Applications of blockchain in finance

Insurance

Post-trade settlement

Financial crime prevention

Payments

Decentralized finance

Properties of DeFi

DeFi layers

DeFi primitives

DeFi services

Asset tokenization

Decentralized exchanges

Flash loans

Derivatives

Money streaming

Yield farming

Insurance

Decentralized lending – lending and borrowing

Benefits of DeFi

Uniswap

Swap the token

Uniswap liquidity pool

Summary

Blockchain Applications and What’s Next

Use cases

IoT

IoT architecture

The physical object layer

The device layer

The network layer

The management layer

The application layer

Benefits of IoT and blockchain convergence

Implementing blockchain-based IoT in practice

Setting up Raspberry Pi

Setting up the first node

Setting up the Raspberry Pi node

Installing Node.js

Building the electronic circuit

Developing and running a Solidity contract

Government

Border control

Elections

Citizen identification

Health

Media

Blockchain and AI

Some emerging trends

Some challenges

Summary

Index

Landmarks

Cover

Index

Preface

The goal of this book is to teach the theory and practice of distributed ledger technology to anyone interested in learning this fascinating new subject. Anyone can benefit from this book, whether a seasoned technologist, student, business executive, or enthusiast. To this end, I aim to provide a comprehensive and in-depth reference of distributed ledger technology that serves the expert and is also accessible to beginners. I primarily focus on describing the core characteristics of blockchain so that readers can build a strong foundation on which to build further knowledge and expertise. The main topics include core blockchain principles, cryptography, consensus algorithms, distributed systems theory, and smart contracts. In addition, practical topics such as programming smart contracts in solidity, building blockchain networks, using blockchain development frameworks such as Truffle, and writing decentralized applications and descriptions constitute a significant part of this book. Moreover, many types of blockchains, related use cases, and cross-industry applications of blockchain technology are discussed in detail.

This book is a unique blend of theoretical principles and hands-on application. Readers will not only be able to understand the technical underpinnings of this technology, but they will also be able to write code for smart contracts and build blockchain networks. Practitioners can use this book as a reference, and it can also serve as a textbook for students wishing to learn this technology. Indeed, some institutions have adopted previous editions of this book as a primary textbook for their courses on blockchain technology.

This book has six new chapters on the latest topics in blockchain, including scalability, security, privacy, the Ethereum Merge, decentralized identity, and decentralized finance.

I hope that this work will serve well technologists, teachers, students, scientists, developers, business executives, and anyone who wants to learn this fascinating technology for many years to come.

Who this book is for

This book is for anyone who wants to understand blockchain technology in depth. It can also be used as a reference resource by developers who are developing applications for blockchain. It can also be used as a textbook for courses related to blockchain technology and cryptocurrencies, as well as being a learning resource for various examinations and certifications related to cryptocurrency and blockchain technology.

What this book covers

Chapter 1, Blockchain 101, introduces the basic concepts of distributed computing, which blockchain technology is based on. It also covers the history, definitions, features, types, and benefits of blockchains, along with various consensus mechanisms that are at the core of blockchain technology.

Chapter 2, Decentralization, covers the concept of decentralization and its relationship with blockchain technology. Various methods and platforms that can be used to decentralize a process or a system will also be introduced.

Chapter 3, Symmetric Cryptography, introduces the theoretical foundations of symmetric cryptography, which is necessary to understand how various security services such as confidentiality and integrity are provided.

Chapter 4, Asymmetric Cryptography, introduces concepts such as public and private keys, digital signatures, and hash functions with practical examples.

Chapter 5, Consensus Algorithms, covers the fundamentals of consensus algorithms and describes the design and inner workings of several consensus algorithms. It covers both traditional consensus protocols and blockchain consensus protocols.

Chapter 6, Bitcoin Architecture, covers Bitcoin, the first and largest blockchain. It introduces technical concepts related to Bitcoin cryptocurrency in detail.

Chapter 7, Bitcoin in Practice, covers the Bitcoin network, relevant protocols, and various Bitcoin wallets. Moreover, advanced protocols, Bitcoin trading, and payments are also introduced. Moreover, various Bitcoin clients and programming APIs that can be used to build Bitcoin applications are covered.

Chapter 8, Smart Contracts, provides an in-depth discussion on smart contracts. Topics such as the history, the definition of smart contracts, Ricardian contracts, Oracles, and the theoretical aspects of smart contracts are presented in this chapter.

Chapter 9, Ethereum Architecture, introduces the design and architecture of the Ethereum blockchain in detail. It covers various technical concepts related to the Ethereum blockchain and explains the underlying principles, features, and components of this platform in depth. Other topics covered are related to the Ethereum Virtual Machine, mining, and supporting protocols for Ethereum.

Chapter 10, Ethereum in Practice, covers the topics related to setting up private networks for Ethereum smart contract development and programming.

Chapter 11, Tools, Languages, and Frameworks for Ethereum Developers, provides a detailed practical introduction to the Solidity programming language and different relevant tools and frameworks that are used for Ethereum development.

Chapter 12, Web3 Development Using Ethereum, covers the development of decentralized applications and smart contracts using the Ethereum blockchain. A detailed introduction to the Web3 API is provided along with multiple practical examples and a final project.

Chapter 13, The Merge and Beyond, introduces the latest development in Ethereum, such as the Beacon Chain, sharding, and future upgrades.

Chapter 14, Hyperledger, presents a discussion about the Hyperledger project from the Linux Foundation, which includes different blockchain projects introduced by its members.

Chapter 15, Tokenization, introduces the topic of tokenization, stable coins, and other relevant ideas such as initial coin offerings and token development standards.

Chapter 16, Enterprise Blockchain, covers the use and application of blockchain technology in enterprise settings and covers DLT platforms such as Quorum.

Chapter 17, Scalability, is dedicated to a discussion of one of the challenges, that is, scalablity, faced by blockchain technology and how to address it. We focus on layer 2 solutions to address this problem, however, other solutions are also discussed.

Chapter 18, Blockchain Privacy, introduces the problem of lack of privacy in blockchains and explains various techniques to address this limitation. We cover solutions to achieve confidentiality and anonymity in blockchains using techniques such as ZK-SNARKs, mixers, and various other methods.

Chapter 19,Blockchain Security, introduces the various security challenges in blockchains and how to solve them. These include smart contract security, formal verification, security concerns, and best practices at each layer of the blockchain system.

Chapter 20, DecentralizedIdentity, covers one of the hottest topics in the blockchain world. Decentralized identity is a cornerstone of the Web3 ecosytem. In this chapter, we explore the methods, techniques, and ecosystems that underpin the Web3 and decentralized identity landscape.

Chapter 21, Decentralized Finance, covers the use and application of decentralized finance, its various aspects, the use cases of blockchain in fianance, and different DeFi protocols.

Chapter 22, Blockchain Applications and What’s Next, provides a practical and detailed introduction to the applications of blockchain technology in fields other than cryptocurrency, including the Internet of Things, government, media, and finance. It is aimed at providing information about the current landscape, projects, and research efforts related to blockchain technology.

Chapter 23, Alternative Blockchains, introduces alternative blockchain solutions and platforms as bonus content that is available online. It covers technical details and features of alternative blockchains and relevant platforms. This is a online chapter and you can read about it at the following link: https://packt.link/OceZK.

To get the most out of this book

In order to get the most out of this book, some familiarity with computer science and basic knowledge of a programming language is desirable.

Download the example code files

The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/Mastering-Blockchain-Fourth-Edition.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/.

Check them out!

Download the color images

We provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/5y4vk.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example: “Tapscript also enables easier future soft fork upgrades by using the new OP_SUCCESS opcode.”

A block of code is set as follows:

function () { throw; }

Any command-line input or output is written as follows:

"Please send 0.00033324 BTC to address 1JzouJCVmMQBmTcd8K4Y5BP36gEFNn1ZJ3".

Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: “ACCOUNTS & KEYS provides options to configure balance and the number of accounts to generate.”

Warnings or important notes appear like this.

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book’s title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you reported this to us. Please visit http://www.packtpub.com/submit-errata, click Submit Errata, and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http://authors.packtpub.com.

Share your thoughts

Once you’ve read Mastering Blockchain - Fourth Edition, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

Download a free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere? Is your eBook purchase not compatible with the device of your choice?

Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.

The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily

Follow these simple steps to get the benefits:

Scan the QR code or visit the link below

https://packt.link/free-ebook/9781803241067

Submit your proof of purchaseThat’s it! We’ll send your free PDF and other benefits to your email directly

1

Blockchain 101

It is very likely that if you are reading this book, you already have heard about blockchain and have some fundamental appreciation of its enormous potential. If not, then let me tell you that this is a technology that has promised to positively alter the existing paradigms of nearly all industries, including, but not limited to, the IT, finance, government, media, medical, and law sectors, by making them more efficient, trustworthy, and transparent.

This chapter introduces blockchain technology, its technical foundations, the theory behind it, and various technologies that have contributed to building what is known today as blockchain. The theoretical foundations of distributed systems are described first. Next, the precursors of Bitcoin are presented. Finally, blockchain technology is introduced. This approach is a logical way of understanding blockchain technology, as the roots of blockchain are in distributed systems and cryptography. We will be covering a lot of ground quickly here, but don’t worry—we will go over a great deal of this material in much greater detail as you move throughout the book.

In this chapter, we’ll focus on:

The growth of blockchain technologyDistributed systemsThe history of blockchainIntroducing blockchainTypes of blockchain

The growth of blockchain technology

With the invention of Bitcoin in 2008, the world was introduced to a new concept that revolutionized the whole of society. It was something that promised to have an impact upon every industry. This new concept was blockchain, the underlying technology that underpins Bitcoin.

Some describe blockchain as a revolution, whereas another school of thought believes that it is going to be more evolutionary, and it will take many years before any practical benefits of blockchain reach fruition. This thinking is correct to some extent, but, in my opinion, the revolution has already begun.

It is a technology that has an impact on current technologies too and possesses the ability to change them at a fundamental level.

Progress towards maturity

Around 2013, some ideas emerged regarding using blockchain technology for other applications instead of only cryptocurrencies. Then, in 2014, some research and experimentation began, which led to proofs of concept, further research, and full-scale trial projects between 2015 and 2017. In 2015, Ethereum was launched as the first smart contract programmable blockchain, which unlocked many possibilities. Interest in enterprise-grade blockchains originated around the same time. Around that time, we also saw several projects, such as Everledger, Quorum, and Corda.

In addition, the development of novel monolithic and multichain architectures rapidly evolved after 2018. Some key examples of novel protocols include Solana, Avalanche, and Polkadot, which we will discuss later in this book.

Currently, as of the second quarter of 2023, decentralized finance (DeFi), non-fungible tokens (NFTs), and tokenization in general are very popular applications of blockchain. They are already in mainstream usage by millions of users around the world who are engaging in DeFi services. It is expected that within three years or so, DeFi will stabilize into a mature mainstream technology. With all the activity and adoption in DeFi and NFT trading, we can say that to some extent blockchain is already part of our daily lives. Of course, further maturity is required, especially from a regulation and security perspective, but millions of users are already using blockchain regularly, either to make payments, trade NFTs, get loans, or play games.

See the following article for details on which platforms these millions are making use of: https://beincrypto.com/ethereum-defi-users-reach-new-highs-over-4m-growing-roughly-8x-in-a-year/

This trend is only expected to grow and, during 2022 and onwards, more and more research and development will be carried out. There will be more focus on the regulation and standardization of blockchain technology. Numerous projects are already production ready, and more adoption is expected in the coming years.

Progress in blockchain technology almost feels like the internet dot-com boom of the late 1990s.

Research in the scalability of blockchains, where blockchains can handle many transactions like traditional financial networks, has led to the development of layer 2 architectures and multi-chain architectures. Advancement in making zero-knowledge proofs practical helped tremendously in making layer 2 solutions a reality. Layer 2 solutions are under heavy research and development now, and many mechanisms have been introduced, such as Plasma, rollups, sidechains, Lightning Network, and many others.

As of 2022, blockchain is already something that many people use every day. With DeFi, NFTs, cryptocurrencies, and Metaverses, blockchain has attracted millions of users. Most of the attraction is because of financial incentives gained by the trading and investment of digital assets such as NFTs and other DeFi services; however, researchers and academics are also interested in studying and developing the theory of these applications and relevant protocols.

Undoubtedly, further maturation and the adoption of blockchain technology is expected in the coming years.

Rising interest

Interest in blockchain technology has risen quite significantly over the last few years. Once dismissed by some simply as “geek money” from a cryptocurrency point of view or as something that was just not considered worth pursuing, blockchain is now being researched by the largest companies and organizations around the world. Millions of dollars are being spent to adopt and experiment with this technology.

Also, the interest in blockchain within academia is astounding, and many educational establishments—including prestigious universities around the world—are conducting research and development on blockchain technology. There are not only educational courses being offered by many institutions, but academics are also conducting high-quality research and producing many insightful research papers on the topic. There are also several research groups and conferences worldwide that specifically focus on blockchain research. This research community is beneficial for the growth of the entire blockchain ecosystem. A simple online search of “blockchain research groups” would reveal hundreds, if not thousands, of these research groups.

There are also various consortiums, such as Enterprise Ethereum Alliance (EEA) at https://entethalliance.org and Hyperledger at https://www.hyperledger.org, that have been established for the research, development, and standardization of blockchain technology. Moreover, the Institute of Electrical and Electronics Engineers (IEEE) and the International Standards Organization (ISO) have also started their attempts to standardize various aspects of blockchain technology.

Many start-ups are providing blockchain-based solutions already. A simple trend search on Google reveals the immense scale of interest in blockchain technology over the last few years. Hot topics include decentralized autonomous organizations (DAOs), fully autonomous and transparent member-governed entities developed using smart contracts with no central authority. Meanwhile, NFTs, through which digital art is routinely bought and sold for millions of dollars, and Metaverses have been making the news.

A Metaverse is a computer-simulated three-dimensional environment. A convergence of our digital and real-world life, they provide a virtual world for the users to perform activities such as social interactions, engaging in business, and shopping. This virtual world is usually accessible via specialized hardware called VR headsets, enhancing the virtual world experience.

This is not a new concept; in the Web 2.0 days (the usual internet that we know and use daily), Second Life, World of Warcraft, and quite a few other similar platforms rose to prominence, as entities centrally owned by shareholders. In the Web 3.0 era (post blockchain), Decentraland and the Sandbox, among many others, are becoming popular, based on decentralized foundations and community governance.

In this book, we are going to learn what exactly blockchain technology is and how it can reshape businesses, multiple industries, and indeed everyday life by bringing about a plenitude of benefits such as decentralized trust, efficiency, cost savings, transparency, and security. We will also explore what decentralization is, smart contracts, and how solutions can be developed and implemented using blockchain platforms such as Ethereum.

While there are many benefits of blockchain technology, there are some challenges too that can cause hurdles in adoption and are being actively researched, such as scalability, privacy, and security. We’ll also take a critical look at blockchain and discuss its limitations and challenges.

We shall begin our exploration of blockchain by looking at distributed systems in the following section. This is a foundational paradigm on which blockchain is based, and we must have a good grasp of what distributed systems are before we can meaningfully discuss blockchain in detail.

Distributed systems

Understanding distributed systems is essential to our understanding of blockchain, as blockchain is a distributed system at its core. It is a distributed ledger that can be centralized or decentralized. A blockchain is originally intended to be and is usually used as a decentralized platform. It can be thought of as a system that has properties of both decentralized and distributed paradigms. It is a decentralized-distributed system.

A distributed system is a computing paradigm whereby two or more nodes work with each other in a coordinated fashion to achieve a common outcome. It is modeled in such a way that end users see it as a single logical platform. For example, Google’s search engine is based on a large distributed system; however, to a user, it looks like a single, coherent platform. It is composed of processes (nodes) and channels (communication channels) where nodes communicate by passing messages. A blockchain is a message-passing distributed system.

A node is an individual player (a computer) in a distributed system. All nodes can send and receive messages to and from each other. Nodes can be honest, faulty, or malicious, and they have memory and a processor. A node that exhibits arbitrary behavior is known as a Byzantine node after the Byzantine Generals problem.

The Byzantine Generals problem

In 1982, a thought experiment was proposed by Lamport et al. in their research paper, The Byzantine Generals Problem, which is available here: https://www.microsoft.com/en-us/research/publication/byzantine-generals-problem/

In this problem, a group of army generals who lead different parts of the Byzantine army is planning to attack or retreat from a city. The only way of communicating with them is via a messenger. They need to agree to strike at the same time to win. The issue is that one or more generals might be traitors who could send a misleading message. Moreover, the messenger could be captured by the city, resulting in no message delivery. Therefore, there is a need for a viable mechanism that allows agreement among the generals, even in the presence of the treacherous ones, and message loss, so that the attack can still take place at the same time. As an analogy for distributed systems, the generals can be considered as honest nodes, the traitors as Byzantine nodes (that is, nodes with arbitrary behavior), the messenger can be thought of as a channel of communication with the generals, and a captured messenger as a delayed or lost message. Several solutions were presented to this problem in the paper by Lamport et al. in 1982.

This type of inconsistent behavior of Byzantine nodes can be intentionally malicious, which is detrimental to the operation of the network. Any unexpected behavior by a node on the network, whether malicious or not, can be categorized as Byzantine.

A small-scale example of a distributed system is shown in the following diagram. This distributed system has six nodes, of which one (N4) is a Byzantine node, leading to possible data inconsistency. L2 is a link that is broken or slow, and this can lead to a partition in the network:

Figure 1.1: Design of a distributed system: N4 is a Byzantine node and L2 is broken or a slow network link

Two key challenges of a distributed system design are the coordination between nodes and fault tolerance. Even if some (a certain threshold dictated by the consensus protocol) of the nodes become faulty or network links break, the distributed system should be able to tolerate this and continue to work to achieve the desired result. This problem has been an active area of distributed system design research for many years, and several algorithms and mechanisms have been proposed to overcome these issues.

Distributed systems are challenging to design. It has been proven that a distributed system cannot have all three of the much-desired properties of consistency, availability, and partition tolerance simultaneously. This principle is known as the CAP theorem.

CAP theorem

The CAP theorem, also known as Brewer’s theorem, was introduced by Eric Brewer in 1998 as a conjecture. In 2002, it was proven as a theorem by Seth Gilbert and Nancy Lynch. The theorem states that any distributed system cannot have consistency, availability, and partition tolerance simultaneously:

Consistency is a property that ensures that all nodes in a distributed system have a single, current, and identical copy of the data. Consistency is achieved using consensus algorithms to ensure that all nodes have the same copy of the data. This is also called state machine replication (SMR). The blockchain is a means of achieving state machine replication.Availability means that the nodes in the system are up, accessible, and are accepting incoming requests and responding with data without any failures as and when required. In other words, data is available at each node and the nodes are responding to requests.Partition tolerance ensures that if a group of nodes is unable to communicate with other nodes due to network failures, the distributed system continues to operate correctly. This can occur due to network and node failures.

A Venn diagram is commonly used to visualize the CAP theorem, as shown below:

Figure 1.2: CAP theorem

The preceding diagram depicts that only two properties at a time can be attained; either AP, CA, or CP.

In summary:

If we opt for CP (consistency and partition tolerance), we sacrifice availability.If we opt for AP (availability and partition tolerance), we sacrifice consistency.If we opt for AC (availability and consistency), we sacrifice partition tolerance.

Note that AC does not really exist. The CAP theorem in practice means that in the case of a network partition, a distributed system is either available or consistent. As network partitions cannot be ignored, the choice is between consistency or availability when a network partition occurs.

We can explain this concept with the following example.

Let’s imagine that there is a distributed system with two nodes. Now, let’s apply the three theorem properties to this smallest of possible distributed systems with only two nodes:

Consistency is achieved if both nodes have the same shared state; that is, they have the same up-to-date copy of the data.Availability is achieved if both nodes are up and running and responding with the latest copy of data.Partition tolerance is achieved if, despite communication failure or delay between nodes, the network (distributed system) continues to operate.

Now think of a scenario where a partition occurs and nodes can no longer communicate with each other. If newly updated data comes in now, it can only be updated on one node. If the node accepts the update, then only one node in the network is updated, and consistency is lost. If the node rejects the update, that will result in a loss of availability. This means that either availability or consistency is unachievable due to the network partition. This is strange because somehow, blockchain manages to achieve all these properties, violating the theorem (especially in its most successful implementation, Bitcoin)—or does it?

In blockchains, consistency is sacrificed in favor of availability and partition tolerance. In this scenario, consistency (C) in the blockchain is not achieved simultaneously with partition tolerance (P) and availability (A), but it is achieved over time. This is called eventual consistency, where consistency is achieved due to validation from multiple nodes over time. There can be a temporary disagreement between nodes on the final state, but it is eventually agreed upon. For example, in Bitcoin, multiple transaction confirmations are required to achieve a good confidence level that transactions may not be rolled back in the future. Eventually, a consistent view of transaction history is available in all nodes. Multiple confirmations of a transaction over time provide eventual consistency in Bitcoin. For this purpose, the process of mining was introduced in Bitcoin. Mining is a process that facilitates the achievement of consensus by using the Proof of Work (PoW) algorithm. At a higher level, mining can be defined as a process that’s used to add more blocks to the blockchain. We will cover more on this later in Chapter 6, Bitcoin Architecture.

PACELC theorem

An extension of the CAP theorem called PACELC was first proposed by Daniel J. Abadi from Yale University. It states that, in addition to the three properties proposed by CAP, there are also tradeoffs between latency and consistency. It states that tradeoffs between consistency and latency always exist in replicated systems, whereas CAP is only applicable when there are network partitions. In other words, it means that even if no network partitions occur, under normal operation the tradeoff between consistency and latency exists. For example, some databases may choose to give up consistency for lower latency, and some databases could pay the availability and latency costs to achieve consistency. This is true for replicated systems and presents a more inclusive picture of consistency tradeoffs in distributed systems.

PACELC was formally proved in a paper available here: https://dl.acm.org/doi/10.1145/3197406.3197420

With a better understanding of distributed systems, let’s now talk about blockchain itself. First, we’ll begin with a brief rundown of the history of blockchain and Bitcoin.

The history of blockchain

Blockchain was introduced with the invention of Bitcoin in 2008. Its practical implementation then occurred in 2009. Bitcoin will be explored in great depth in Chapter 6, Bitcoin Architecture. However, it is essential to refer to Bitcoin here because without it, the history of blockchain is not complete.

Now we will look at the early history of computing and computer networks and will discuss how these technologies evolved and contributed to the development of Bitcoin in 2008:

1976 – Diffie–Hellman work on securely exchanging cryptographic keys.1978 – Invention of public key cryptography.1979 – Invention of Merkle trees (hashes in a tree structure) by Ralph C. Merkle.1980s – Development of TCP/IP.1980 – Protocols for public key cryptosystems, Ralph C. Merkle.1982 – Blind signatures proposed by David Chaum.1982 – The Byzantine Generals problem.1985 – Work on elliptic curve cryptography by Neal Koblitz and Victor Miller.1991 – Haber and Stornetta work on tamper-proofing document timestamps. This can be considered the earliest idea of a chain of blocks or hash chains.1992 – Cynthia Dwork and Moni Naor publish Pricing via Processing or Combatting Junk Mail. This is considered the first use of PoW.1993 – Haber, Bayer, and Stornetta upgraded the tamper-proofing of document timestamps system with Merkle trees.1995 – David Chaum’s Digicash system (an anonymous electronic cash system) started to be used in some banks.1998 – Bit Gold, a mechanism for decentralized digital currency, invented by Nick Szabo. It used hash chaining and Byzantine Quorums.1999 – Emergence of a file-sharing application mainly used for music sharing, Napster, which is a P2P network, but was centralized with the use of indexing servers.1999 – Development of a secure timestamping service for the Belgian project TIMESEC.2000 – Gnutella file-sharing network, which introduced decentralization.2001 – Emergence of BitTorrent and Distributed Hash Tables (DHTs).2002 – Hashcash by Adam Back.2004 – Development of B-Money by Wei Dei using Hashcash.2004 – Hal Finney, the invention of the reusable PoW system.2005 – Prevention of Sybil attacks by using computation puzzles, due to James Aspnes et al.2009 – Bitcoin (first blockchain).

These technologies contributed in some way to the development of Bitcoin, even if not directly; the work is relevant to the problem that Bitcoin solved.

Bitcoin

All previous attempts to create anonymous and decentralized digital currency were successful to some extent, but they could not solve the problem of preventing double spending in a completely trustless or permissionless environment. This problem was finally addressed by the Bitcoin blockchain, which introduced the Bitcoin cryptocurrency.

Bitcoin also solves the SMRproblem, introduced in 1978 by Leslie Lamport and formalized in 1980 by Fred Schneider. SMR is a scheme that’s used to implement a fault-tolerant service by replicating data (state) between nodes in a distributed system. Bitcoin solves the problem by allowing the replication of blocks at all correct nodes and ensuring consistency via its PoW mechanism. Here, the agreement is reached between nodes (or replicas) repeatedly to append new blocks to the blockchain.

Electronic cash

The concept of electronic cash (e-cash), or digital currency, is not new. Since the 1980s, e-cash protocols have existed that are based on a model proposed by David Chaum.

Just as understanding the concepts of distributed systems is necessary to comprehend blockchain technology, the idea of e-cash is also essential to appreciate the first, and astonishingly successful, application of blockchain, Bitcoin, and more broadly, cryptocurrencies in general. To create an effective e-cash system, two fundamental requirements need to be met: accountability and anonymity.

Accountability is required to ensure that cash is spendable only once (addressing the double-spending problem) and that it can only be spent by its rightful owner. The double-spending problem arises when the same money is spent twice. As it is quite easy to make copies of digital data, this becomes a big issue in digital currencies as you can make many copies of the same digital cash. Spending the same cash twice is known as the double-spending problem.

Anonymity is required to protect users’ privacy. With physical cash, it is almost impossible to trace back spending to the individual who actually paid the money, which provides adequate privacy should the consumer choose to hide their identity. In the digital world, however, providing such a level of privacy is difficult due to inherent personalization, tracing, and logging mechanisms in digital payment systems such as credit card payments. This is a required feature for ensuring the security and safety of the financial network, but it is also often seen as a breach of privacy.

This is because end users do not have any control over who their data might be shared with, even without their consent. Nevertheless, this is a solvable problem, and cryptography is used to address such issues. Especially in blockchain networks, the privacy and anonymity of the participants on the blockchain are sought-after features. David Chaum solved both problems during his work in the 1980s by using two cryptographic operations, namely, blind signatures and secret sharing. These terms and related concepts will be discussed in detail in Chapter 4, Asymmetric Cryptography. For the moment, it is sufficient to say that blind signatures allow the signing of a document without actually seeing it, and a secret sharing scheme enables the detection of double-spending.

In 2009, the first practical implementation of an e-cash system named Bitcoin appeared. The term cryptocurrency emerged later. For the very first time, it solved the problem of distributed consensus in a trustless network. It used public key cryptography with a PoW mechanism to provide a secure and decentralized method of minting digital currency. The key innovation is the idea of an ordered list of blocks composed of transactions that is cryptographically secured by the PoW mechanism to prevent double-spending in a trustless environment. This concept will be explained in greater detail in Chapter 6, Bitcoin Architecture.

Looking at all the technologies mentioned previously and their relevant history, it is easy to see how concepts from e-cash schemes and distributed systems were combined to create Bitcoin and what now is known as blockchain. This concept can also be visualized with the help of the following diagram:

Figure 1.3: The various ideas that supported the invention of Bitcoin and blockchain

With the emergence of e-cash covered, along with the ideas that led to the formation of Bitcoin and blockchain, we can now begin to discuss blockchain itself.

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:

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