Download as pdf or txt
Download as pdf or txt
You are on page 1of 67

Blockchain Essentials: Core Concepts

and Implementations 1st Edition


Ramchandra Sharad Mangrulkar
Visit to download the full and correct content document:
https://ebookmass.com/product/blockchain-essentials-core-concepts-and-implementa
tions-1st-edition-ramchandra-sharad-mangrulkar/
Blockchain
Essentials
Core Concepts and Implementations

Ramchandra Sharad Mangrulkar
Pallavi Vijay Chavan
Blockchain Essentials
Ramchandra Sharad Mangrulkar • Pallavi Vijay Chavan

Blockchain Essentials
Core Concepts and Implementations
Ramchandra Sharad Mangrulkar Pallavi Vijay Chavan
Mumbai, Maharashtra, India Mumbai, Maharashtra, India

ISBN-13 (pbk): 978-1-4842-9974-6 ISBN-13 (electronic): 978-1-4842-9975-3


https://doi.org/10.1007/978-1-4842-9975-3

Copyright © 2024 by Ramchandra Sharad Mangrulkar and Pallavi Vijay Chavan


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned,
specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in
any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by
similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of
a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the
trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such,
is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors
nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher
makes no warranty, express or implied, with respect to the material contained herein.

Managing Director, Apress Media LLC: Welmoed Spahr


Acquisitions Editor: Smriti Srivastava
Development Editor: Laura Berendson
Coordinating Editor: Shaul Elson

Cover designed by eStudioCalamar


Cover image by [email protected]

Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004, U.S.A. Phone 1-
800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media,
LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance
Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected]; for reprint, paperback, or audio rights,
please e-mail [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also
available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/
bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub (https://
github.com/Apress). For more detailed information, please visit https://www.apress.com/gp/services/source-code.

Paper in this product is recyclable


To our cherished daughter, Mansi:
Your unwavering support, encouragement, and constant
sympathy were invaluable during the creation of this book.
Without you, it would have been completed in half the time.
Inhalt

1 Introduction to Blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Blockchain Myths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Blockchain and Decentralization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 What Is Blockchain? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.5 Disruptive Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.6 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.6.1 Milestones in Blockchain Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.7 Features of Blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.8 Present Growth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.9 Predicted Market . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.10 Blockchain Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.10.1 Public . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.10.2 Private . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.10.3 Federated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.10.4 Hybrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.10.5 Difference Between Public and Private Blockchains . . . . . . . . . . . . . . . . . . . 14
1.11 Blockchain Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.11.1 Hardware/Infrastructure Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.11.2 Data Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.11.3 Network Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.11.4 Consensus Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.11.5 Application and Presentation Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.12 A Block and Its Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.12.1 A Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.12.2 Block Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.12.3 Ledger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.12.4 Distributed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.12.5 Transparency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.12.6 Confirmation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.12.7 Proof of Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.12.8 Block Awards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.12.9 Transactions and UTXOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.12.10 Consensus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

vii
viii Contents

1.13 Scaling Blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23


1.13.1 Issues in Scaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.13.2 Off-Chain Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.13.3 Sharding in Blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.14 Blockchain DApps and Usecases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.15 Laboratory Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.15.1 Program for Implementing Blockchain in Python . . . . . . . . . . . . . . . . . . . . . 26
1.15.2 Program for Mining a New Block in Blockchain and Printing It . . . . . . . . . 28
1.15.3 Program for Creating Four Blocks in Blockchian and Printing
and Traversing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
1.15.4 Implementing Blockchain and Printing All Fields as per Etherscan.io . . . . . 32
1.15.5 Implementing Blockchain and UTXo in Python . . . . . . . . . . . . . . . . . . . . . . . 35
1.15.6 Explanation of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
1.15.7 Implementation of PoW Algorithm in Python . . . . . . . . . . . . . . . . . . . . . . . . 37
1.15.8 Implementation of PoS Algorithm in Python . . . . . . . . . . . . . . . . . . . . . . . . . 40
1.15.9 Program to Fetch the Latest Block Information from Ethereum
Blockchain Using Etherscan API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
1.15.10 Explanation of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
1.16 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
1.17 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
1.17.1 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
1.17.2 Short Answer Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
1.17.3 Long Answer Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
1.17.4 Practical Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
1.17.5 Answer Set of MCQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2 Essentials of Blockchain Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.1 Cryptography Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.1.1 Hash Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.2 Hash Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.2.1 Properties of Hash Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.2.2 Hash Pointers and Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.2.3 Tampering Is Computationally Challenging . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.2.4 Role of Hashes in Blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
2.3 Secure Hash Algorithm (SHA) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.3.1 SHA Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.3.2 Hashing Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
2.4 Public Key Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
2.4.1 Secure Hash Algorithm-3 (Keccak) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.5 Merkle Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.5.1 Merkle Tree Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.5.2 Role of Merkle Tree in Blockhain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.5.3 Structure of Merkle Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
2.5.4 Merkle Proof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
2.5.5 Proof of Membership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
2.5.6 Proof of Nonmembership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Contents ix

2.5.7 Advantages of Merkle Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60


2.5.8 Applications of Merkle Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2.5.9 Merkle Tree Proof of Reserves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2.6 Public Key Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2.6.1 Public and Private Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2.6.2 Public Key Encryption Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2.6.3 Digitally Signed Transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
2.6.4 Digital Signing in Blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
2.7 Laboratory Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2.7.1 Program in Python that Demonstrates the Use of Hashlib Library
to Generate the SHA-3 Hash of a Message . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2.7.2 Python Program that Takes a String and the Desired Number of Leading
Zeros from the User and Outputs the Input String, the Nonce Value for
Which the Leading Zeros Puzzle Is Solved, and the Corresponding Hash
Generated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
2.7.3 Program to Create Hash Code from Given Input String . . . . . . . . . . . . . . . . . 65
2.7.4 Program in Python that Demonstrates How to Use the SHA-256 Hash
Function and Its Application in a Simple Blockchain . . . . . . . . . . . . . . . . . . 66
2.7.5 Write a Program in Python to Verify Hash Properties . . . . . . . . . . . . . . . . . . 67
2.7.6 Program to Demonstrate a Simple Implementation of a Blockchain Using
Hash Codes as a Chain of Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
2.7.7 Program to Demonstrate the Mining Process in Blockchain . . . . . . . . . . . . . 70
2.7.8 Program to Create a Merkle Tree in Blockchain . . . . . . . . . . . . . . . . . . . . . . . 72
2.7.9 Program to Prove Membership and Nonmembership in a Merkle Tree
Blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
2.7.10 Explanation of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
2.7.11 Program to Demonstrate How to Prove the Membership and
Nonmembership of an Element in a Merkle Tree Blockchain . . . . . . . . . . . . 75
2.7.12 Program in Python that Demonstrates RSA Digital Signature Scheme . . . . 76
2.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
2.9 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
2.9.1 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
2.9.2 Short Answer Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
2.9.3 Long Answer Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
2.9.4 Practical Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
2.9.5 Programming Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
3 Bitcoin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
3.1 What Is Bitcoin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
3.2 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
3.3 Predicted Market . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
3.4 Wallet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
3.4.1 Bitcoin Wallets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
3.4.2 Custodial Wallet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
3.4.3 Noncustodial Wallet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
3.4.4 Software Wallet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
3.4.5 Hardware Wallet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
3.4.6 Features of Digital Wallet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
x Contents

3.4.7 Difference Between Digital Wallet and Bank Accounts . . . . . . . . . . . . . . . . . 87


3.4.8 Top Digital Wallet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.5 Digital Keys and Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
3.5.1 Private Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
3.5.2 Public Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
3.6 Addresses in Bitcoin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
3.7 Transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
3.7.1 Transaction Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
3.7.2 Creating Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
3.7.3 Broadcasting Transactions to the Bitcoin Network . . . . . . . . . . . . . . . . . . . . . 91
3.7.4 Propagating Transactions on the Bitcoin Network . . . . . . . . . . . . . . . . . . . . . 92
3.7.5 Data Structures for Transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
3.7.6 Types of Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
3.7.7 Transaction Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
3.8 Digital Signature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
3.9 Mining and Consensus in Bitcoin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
3.9.1 Mining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
3.9.2 Consensus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
3.9.3 Decentralized Consensus in Bitcoin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
3.9.4 Mining and Racing in Bitcoin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
3.9.5 Cost of Mining in Bitcoin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
3.9.6 Consensus Attacks in Bitcoin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
3.10 Forking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
3.10.1 Hard Fork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
3.10.2 Soft Fork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
3.11 Laboratory Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
3.11.1 Program to Generate Private Keys Securely on a Hardware Wallet . . . . . . . 102
3.11.2 Program to Generate Public-Private Key Pairs, Encrypting and Storing
Private Keys Securely and Signing Transactions Using the Private Key . . . 103
3.11.3 Program to Demonstrate Some of the Features of a Digital Wallet . . . . . . . . 104
3.11.4 Program to Compare the Features of Popular Digital Wallets, Rank Them
Based on User Reviews and Ratings, and Recommend a Digital Wallet
Based on User Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
3.11.5 Program to Deploy a Smart Contract to Blockchain Using a Tool Like
Remix IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
3.11.6 Program that Measures the Transaction Throughput of EOA–EOA
Transactions and CA–CA Transactions Using Various Gas Limits
on the Ethereum Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
3.11.7 Program that Uses Web3 to Categorize Ethereum Addresses as EOA or
Contract Addresses and Evaluates Its Accuracy and Performance on a
Large Dataset of Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
3.11.8 Program that Simulates the Life Cycle of a Transaction on the Ethereum
Network and Measures the Time and Resources Required . . . . . . . . . . . . . . 111
3.11.9 Program for Implementing ECDSA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
3.11.10 Program to Create a Bitcoin Transaction and Sign It with a SIGHASH
Flag Using the bitcoinlib Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
3.11.11 Program for Bitcoin Mining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Contents xi

3.11.12 Program that Demonstrates How to Identify 51% Attacks


on a Blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
3.11.13 Program to Demonstrate the Concept of Forking . . . . . . . . . . . . . . . . . . . . . . 116
3.11.14 Program to Detect and Deal with 51% Attacks in the Bitcoin
Blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
3.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
3.13 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
3.13.1 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
3.13.2 Short Answer Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
3.13.3 Long Answer Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
4 Ethereum Blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
4.1 Overview of Ethereum Blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
4.1.1 Key Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
4.1.2 EVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
4.2 History of Ethereum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
4.2.1 Ledger to State Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
4.2.2 Ethereum Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
4.3 Smart Contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
4.4 Challenges in Implementing Smart Contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
4.4.1 Smart Contract Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
4.4.2 Introducing Solidity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
4.4.3 Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
4.5 Ethereum Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
4.6 Ethereum Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
4.6.1 Transaction Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
4.7 Gas and Transaction Fees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
4.7.1 Addressing Gas Fees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
4.7.2 Factors Affecting Gas Price . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
4.7.3 Calculating Gas Costs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
4.7.4 Gas Fee Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
4.7.5 Implications of Base Fee . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
4.7.6 Transaction Cost Predictability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
4.7.7 Future with PoS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
4.7.8 Gas Fees and Orchid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
4.7.9 Example 1: Wallet-to-Wallet Transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
4.7.10 Example 2: Deploying a Simple Contract . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
4.7.11 Avoiding Ethereum Gas Fees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
4.8 Laboratory Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
4.8.1 Solidity Program for Displaying Hello Message . . . . . . . . . . . . . . . . . . . . . . . 141
4.8.2 Program for Demonstrating Simple Increment and Decrement
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
4.8.3 Smart Contract Development with Solidity . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
4.8.4 Implementing Security Measures in Smart Contracts . . . . . . . . . . . . . . . . . . . 145
4.8.5 Developing an ERC-20 Token . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
4.8.6 Building a Simple DApp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
4.8.7 Interacting with Off-Chain Data Using Oracles . . . . . . . . . . . . . . . . . . . . . . . 151
xii Contents

4.8.8
Program to Demonstrate a Basic Example of Smart Contract Interaction
and Ownership Management on Ethereum Blockchain . . . . . . . . . . . . . . . . . 153
4.8.9 Program to Create a Decentralized Blind Auction Smart Contract on the
Ethereum Blockchain, Enabling Participants to Place Concealed Bids,
Reveal Them, and Determine the Highest Bidder While Ensuring Secure
Fund Management and Transparent Auction Outcomes. This Contract
Facilitates a Trustless and Tamper-Resistant Auction Mechanism,
Promoting Fairness and Efficiency in Auction Processes . . . . . . . . . . . . . . . . 155
4.8.10 Program to Showcase the Vulnerability of Reentrancy Attacks in a Smart
Contract Context and Demonstrate the Implementation of a Solution
Using a Reentrancy Guard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
4.9 Mist Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
4.9.1 Guidlines for Using Mist Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
4.9.2 Mist and Geth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
4.9.3 Geth’s Role . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
4.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
4.11 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
4.11.1 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
4.11.2 Long Answer Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
5 Hyperledger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
5.1 Introduction to Hyperledger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
5.1.1 The Purpose of Hyperledger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
5.2 Hyperledger Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
5.2.1 Infrastructure Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
5.2.2 Framework Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
5.2.3 Tool Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
5.3 Hyperledger Community and Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
5.4 Hyperledger Smart Contracts (Chaincode) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
5.5 The Functioning of Hyperledger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
5.5.1 Contributor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
5.5.2 Endorser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
5.5.3 Consenter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
5.5.4 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
5.5.5 Advantages of Hyperledger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
5.5.6 Limitations of Hyperledger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
5.6 Hyperledger Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
5.6.1 Comparison of Hyperledger with Other Blockchain Frameworks . . . . . . . . 174
5.6.2 Hyperledger Fabric in Blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
5.6.3 Consensus in Hyperledger Fabric . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
5.7 Hyperledger Consortiums and Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
5.8 Hyperledger and Blockchain as a Service (BaaS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
5.8.1 Hyperledger Adoption Through BaaS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
5.8.2 Advantages and Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
5.9 Laboratory Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
5.9.1 Program to Demonstrate Interaction with a Hyperledger Fabric
Blockchain Network Using the Hyperledger Fabric JavaScript SDK . . . . . . 182
Contents xiii

5.9.2
Program to Demonstrate How Hyperledger Fabric Could Be Used in a
Healthcare Context to Manage Patient Medical Records . . . . . . . . . . . . . . . . 184
5.9.3 Program to Demonstrate the Implementation of a Basic Government
Application Using Hyperledger Fabric . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
5.9.4 Program to Demonstrate Finance Application Using Hyperledger
Fabric . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
5.9.5 Program to Demonstrate the Implementation of a Finance and Payments
System Using Hyperledger Fabric . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
5.9.6 Explanation of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
5.9.7 Program to Demonstrate Simple Interoperability Using the Hyperledger
Fabric JavaScript SDK to Interact with the Network and Demonstrate
How Two Different Smart Contracts Can Work Together . . . . . . . . . . . . . . . 194
5.9.8 Program to Demonstrate Smart Contract Modeling with Composer and
Docker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
5.9.9 Program for Demonstrating Hyperledger Caliper, a Benchmarking Tool
That Measures the Performance of Hyperledger Blockchain Applications
Under Various Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
5.9.10 Running Caliper Benchmarks with Docker . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
5.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
5.11 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
5.11.1 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
5.11.2 Short Answer Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
5.11.3 Long Answer Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
5.11.4 Programming Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
6 Case Studies Using Blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
6.1 Blockchain – The Technology for Document Management . . . . . . . . . . . . . . . . . . . . . . 203
6.1.1 The Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
6.1.2 Introduction and Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
6.1.3 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
6.1.4 Use Case Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
6.1.5 Solution Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
6.1.6 Implementation Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
6.1.7 Smart Contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
6.1.8 Data Management and Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
6.1.9 Interoperability and Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
6.1.10 User Experience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
6.1.11 Results and Benefits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
6.1.12 Challenges and Lessons Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
6.1.13 Future Enhancements and Scalability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
6.1.14 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
6.2 Case Study 2: Blockchain in the Food Supply Chain . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
6.2.1 Introduction and Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
6.2.2 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
6.2.3 Use Case Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
6.2.4 Solution Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
6.2.5 Implementation Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
6.2.6 Smart Contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
xiv Contents

6.2.7 Data Management and Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210


6.2.8 Interoperability and Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
6.2.9 User experience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
6.2.10 Results and Benefits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
6.2.11 Challenges and Lessons Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
6.2.12 Future Enhancements and Scalability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
6.2.13 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
6.3 Case Study 3: Blokchain in the Insurance Industry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
6.3.1 Introduction and Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
6.3.2 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
6.3.3 Use Case Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
6.3.4 Solution Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
6.3.5 Implementation Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
6.3.6 Smart Contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
6.3.7 Data Management and Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
6.3.8 Interoperability and Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
6.3.9 User Experience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
6.3.10 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
6.3.11 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
6.4 Case Study 4: India’s Income Tax Department’s Simplification of Tax Procedures . . . 215
6.4.1 Introduction and Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
6.4.2 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
6.4.3 Use Case Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
6.4.4 Solution Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
6.4.5 Implementation Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
6.4.6 Smart Contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
6.4.7 Tax Authority Interaction (Not Implemented in This Simplified
Example) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
6.4.8 Event Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
6.4.9 Data Management and Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
6.4.10 Interoperability and Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
6.4.11 User Experience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
6.4.12 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
6.4.13 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
6.5 Case Study 5: Retail Banking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
6.5.1 Introduction and Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
6.5.2 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
6.5.3 Use Case Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
6.5.4 Solution Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
6.5.5 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
6.5.6 Data Management and Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
6.5.7 Network Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
6.5.8 Incident Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
6.5.9 Interoperability and Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
6.5.10 User Experience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
6.5.11 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
6.5.12 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
6.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Contents xv

6.7 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226


6.7.1 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
6.7.2 Short/Long Answer Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
7 Beyond Blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
7.1 Blockchain for the Metaverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
7.2 Emergence of the Metaverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
7.3 Understanding the Metaverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
7.4 Metaverse Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
7.4.1 Spatial Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
7.4.2 Metaverse Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
7.5 Metaverse Through Immersive Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
7.5.1 Challenges in Metaverse Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
7.6 Blockchain’s Role in the Metaverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
7.6.1 Why Blockchain Technology Is Crucial for the Metaverse . . . . . . . . . . . . . . 236
7.6.2 Interoperability and Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
7.6.3 Security and Trust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
7.6.4 Monetization and Incentives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
7.7 Digital Scarcity and Ownership of Virtual Assets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
7.8 Building Trust and Security in the Decentralized Metaverse . . . . . . . . . . . . . . . . . . . . . 238
7.8.1 Trustless Nature of the Metaverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
7.8.2 Zero Trust Security in the Metaverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
7.9 Data Hub for Crypto, DeFi, NFT, Metaverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
7.9.1 Real-Time Data Streaming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
7.10 Digital Trust Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
7.10.1 Diverse Applications of Digital Trust Networks . . . . . . . . . . . . . . . . . . . . . . . 240
7.10.2 Peer-to-Peer Marketplaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
7.10.3 Platform Ecosystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
7.10.4 Zero Trust Security Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
7.10.5 Digital Identity Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
7.10.6 Decentralized Autonomous Organizations . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
7.11 Beyond Cryptocurrency: Transforming ESG, Digital Assets,
and Financial Markets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
7.11.1 Environmental, Social, and Governance (ESG) . . . . . . . . . . . . . . . . . . . . . . . 242
7.11.2 Digital Assets and Currency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
7.11.3 Central Bank Digital Currencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
7.11.4 Blockchain Modernizing Financial Markets . . . . . . . . . . . . . . . . . . . . . . . . . . 242
7.11.5 Blockchain and AI: A Synergy for Trust and Intelligence . . . . . . . . . . . . . . . 243
7.11.6 Data Analysis and Predictive Insights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
7.11.7 Smart Contract Automation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
7.11.8 Enhanced Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
7.11.9 Scalability and Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
7.12 The Future of Banks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
7.12.1 Instant and Efficient Cross-Border Payments . . . . . . . . . . . . . . . . . . . . . . . . . 244
7.12.2 Streamlined Trade Finance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
7.12.3 Innovative Revenue Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
xvi Contents

7.13 Blockchain and Sustainable Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244


7.13.1 Renewable Energy Trading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
7.13.2 Environmental Conservation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
7.14 Tangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
7.15 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
7.16 Short/Long Answer Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248

Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
About the Authors

Dr. Ramchandra Mangrulkar is a Professor in the Department


of Information Technology in the Dwarkadas J. Sanghvi College
of Engineering in Mumbai, India. He holds various memberships
in professional organizations such as IEEE, ISTE, ACM, and
IACSIT. He completed his Doctor of Philosophy (Ph.D.) in Com-
puter Science and Engineering from S.G.B. Amravati University
in Maharashtra and Master of Technology (MTech) in Computer
Science and Engineering from the National Institute of Technology,
Rourkela. Dr. Mangrulkar is proficient in several technologies and
tools, including Microsoft’s Power BI, Power Automate, Power
Query, and Power Virtual Agents, Google’s Dialog Flow, and Over-
leaf. With over 22 years of combined teaching and administrative
experience, Dr. Mangrulkar has established himself as a knowl-
edgeable and skilled professional in his field. He has also obtained
certifications like Certified Network Security Specialist Interna-
tional CyberSecurity Institute (ICSI) – Certified Network Security
Specialist (CNSS) from ICSI, UK. Dr. Mangrulkar has an extensive
publication record, with 95 publications including refereed/peer-
reviewed international journal publications, book chapters with
international publishers (including ones indexed in Scopus), and
international conference publications.
Dr. Pallavi Vijay Chavan is Associate Professor at Ramrao Adik
Institute of Technology, D. Y. Patil “Deemed-to-be-University,”
Navi Mumbai, MH, India. She has been in academia for the past
17 years working in the area of computing theory, data science,
and network security. In her academic journey, she has published
research in data science and security with reputable publishers
such as Springer, Elsevier, CRC Press, and Inderscience. She has
published 2 books, over 7 book chapters, more than 10 interna-
tional journal papers, and over 30 international conference papers.
Presently she serves as advisor to five Ph.D. research scholars in
related fields. She completed her Ph.D. at Rashtrasant Tukadoji
Maharaj Nagpur University, Nagpur, MH, India, in 2017. In 2003,

xvii
xviii About the Authors

she earned the first merit position at Nagpur University for a B.E. in
Computer Engineering. She is the recipient of research grants from
University Grants Commission (UGC), Council of Scientific &
Industrial Research (CSIR), and University of Mumbai. She serves
as a reviewer for Elsevier and Inderscience journals. Her motto is
“Teaching is a mission.”
About the Technical Reviewer

Dr. Parikshit Mahalle is a senior member of the Institute of


Electrical and Electronics Engineers and Professor, Dean of Re-
search and Development and -Chair of the Department of Arti-
ficial Intelligence and Data Science at Vishwakarma Institute of
Information Technology, Pune, India. He completed his Ph.D. at
Aalborg University, Denmark, and continued as a postdoctoral
researcher at Communication, Media & Information Technologies,
Copenhagen, Denmark. He has over 23 years of teaching and
research experience. He is a former member of the Board of Studies
in Computer Engineering and former chairman of the Department
of Information Technology at Savitribai Phule Pune University
and various universities and autonomous colleges throughout India.
He owns 15 patents, has published over 200 research studies and
papers (2950+ Google Scholar, 1550+ H index-25 and Scopus
citations, 438 H index-8, Web of Science, and H index-10 ci-
tations), and has authored/edited 56 books with Springer, CRC
Press, Cambridge University Press, and others. He is editor in
chief for IGI Global’s International Journal of Rough Sets and
Data Analysis and Inderscience’s International Journal of Grid
and Utility Computing, is a member of the editorial review board
for IGI Global’s International Journal of Ambient Computing and
Intelligence, and serves as a reviewer for various reputable journals
and conferences. His research interests include machine learning,
data science, algorithms, Internet of Things, identity management,
and security. He currently advises eight Ph.D. students in the areas
of IoT and machine learning, and six students have successfully
defended their Ph.D. under his supervision from Savitribai Phule
Pune University. He is also the recipient of the Best Faculty Award
by Sinhgad Institutes and Cognizant Technology Solutions. He has
delivered over 200 lectures at the national and international levels.

xix
Preface

Blockchain has become the buzzword of the day. Developers are focusing on more user-friendly
applications with the help of blockchain, achieving decentralization and a trustless environment
without third-party involvement. This includes diverse concepts and tools that play major roles in
developing crypto-based applications in various programming languages. The distributed ledger and
smart contracts involved reveal the importance of blockchain in creating immutable and transparent,
cryptographically secure record-keeping of transactions. The programming approach helps to shed
light on the core concepts of blockchain and relevant applications in easy steps. This helps to motivate
learners to become part of the solution to most of the applications demanding trustless and inde-
pendent autonomous systems. The identification and examination of blockchain technology beyond
cryptocurrency will help to investigate alternative solutions using many blockchain-supportive tools.
The main purpose of this book is to present the difficult concepts of blockchain technology in very
accessible and easy-to-understand language using a programming approach so that learners can easily
grasp the key concepts arising from the emerging notion of blockchain technology. Another purpose
of this book is to make available the experience of academia and industry to the target audience
through hands-on programming.
This book presents the concepts of blockchain technology in a concise manner with clear and easy
examples using trending blockchain programming languages. The book fills a gap of address issues
surrounding the practical implementation of blockchain concepts using case studies. The book also
highlights the usefulness of blockchain technology beyond its current applications.

Mumbai, India Ramchandra Sharad Mangrulkar


September 2023 Pallavi Vijay Chavan

xxi
Acknowledgements

We extend our sincere gratitude to the dedicated contributors and accomplished researchers in the
field of blockchain for their invaluable contributions and pioneering work.

xxiii
Introduction to Blockchain
1

Readers of this book are likely to have some knowledge and basic idea about the enormous potential
of the trending, decentralizing, and trustworthy technology called blockchain. This technology
represents an innovation in the digital ecosystem that has significantly impacted trusted computing
activities, resulting in an enhanced level of protection from cyber security threats.
This chapter lays out the fundamentals of blockchain technology, presenting its theoretical
background, historical milestones, and present growth trends. Further, the conceptual view of a block
in blockchain and the types of blockchain are described. The chapter discusses the basic skill set and
libraries required to start doing “blockchain programming,” which is a key objective of this book. The
chapter ends with a few examples and their implementation in Python.

1.1 Prerequisites

The prerequisites for blockchain technology include:

• Understanding of cryptography: Cryptography is the foundation of blockchain technology. A basic


understanding of cryptographic concepts, such as hashing, public-key encryption, and digital
signatures, is necessary.
• Distributed systems: Blockchain is a distributed system that runs on multiple nodes. Therefore, it
is essential to have a good understanding of distributed systems to build and deploy blockchain
applications.
• Data structures and algorithms: Blockchain technology relies on complex data structures such as
Merkle trees and algorithms such as consensus algorithms. Understanding of these concepts is
crucial for building a robust blockchain system.
• Networking and security: Blockchain technology requires a good understanding of networking
protocols, such as TCP/IP, HTTP, and HTTPS. Additionally, a solid understanding of security
concepts, such as firewalls, encryption, and authentication, is necessary to develop secure
blockchain applications.
• Smart contracts: Smart contracts are self-executing contracts with the terms of the agreement
between buyer and seller directly written into lines of code. Knowledge of smart contract
programming languages, such as Solidity, is necessary for building decentralized applications.

© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2024 1
R. S. Mangrulkar, P. Vijay Chavan, Blockchain Essentials,
https://doi.org/10.1007/978-1-4842-9975-3_1
2 1 Introduction to Blockchain

• Business and economics: Blockchain technology is disrupting traditional business models and
creating new opportunities. Understanding the economics of blockchain and how it can be applied
to business is essential for leveraging its potential.
• Legal and regulatory dimensions: Blockchain technology operates in a regulatory gray area in
many countries, and regulations are constantly evolving. Understanding the legal and regulatory
environment in which blockchain operates is critical for creating compliant and successful
blockchain applications.

1.2 Blockchain Myths

Blockchain is an emerging technology. The following list dispels some of the myths surrounding
blockchain:

• Blockchain is the same thing as Bitcoin (or any other cryptocurrency)


There is a misleading idea that if you learn blockchain technology, you will become a good trader!
This is untrue. Blockchain is not equivalent to any cryptocurrency, whether Bitcoin or any other
currencies on the market like Altcoins. In fact, blockchain is a technology, whereas Bitcoin is
a cryptocurrency that makes use of blockchain technology. Blockchain has many applications
outside the crypto world. Blockchain technology provides a full support system for developing
cryptocurrencies, whereas Bitcoin is a fundamental application that builds on this emerging
blockchain technology.
• Blockchain can solve all security issues
Blockchain cannot be used to definitively eliminate corruption or fraudulent activities.
Blockchain’s many applications have been developed by players in the various models governing
economies around the world. Blockchain cannot all issues related to security. Solving all societal
issues using blockchain is a formidable challenge. Thus, careful consideration needs to be given
as to which societal issues should be addressed using blockchain.
• Blockchain is the only possible technology
Blockchain is not necessarily the best technology for solving your problems; they might be better
solved employing technology that does not use blockchain. It is possible that many different
existing technologies would yield better results in terms of security without the use of blockchain.
• Blockchain and distributed databases are similar technologies
Blockchain and distributed databases are different technologies. Blockchain is not a distributed
databases. Blockchain is not designed to store and secure data. Blockchain and distributed
databases are two different technologies, each with its own merits and demerits and different
potential to solve different problems. Both are essential, and one cannot easily replace the other.

1.3 Blockchain and Decentralization

Blockchain technology emerged to solve most of the issues in decentralization. Decentralization


refers to the distribution of power or authority away from a single central entity to multiple individuals
or groups. In the context of technology, it refers to systems or networks that operate without a central
authority controlling them. Figure 1-1 gives an overview of centralized and decentralized systems.
Decentralization is required to address trust issues, that is, the different parties involved do not
trust each other, but they should cooperate. The network of different entities such as businesses,
individuals, government, private- and public-sector organizations, with their own interests, can come
1.4 What Is Blockchain? 3

Figure 1-1 Overview: centralized and decentralized systems

Figure 1-2 Blocks and chain in blockchain

together and cooperate with each other to solve societal issues. Decentralization and blockchain work
together to create a secure, transparent, and tamper-proof system that operates without the need for a
central authority.

1.4 What Is Blockchain?

Blockchain is an append-only, immutable, never-ending chain of data where data, once added, cannot
be deleted or modified, achieving a tamper-proof system. The immutable property of blockchain
means no one can change it. Its append-only nature ensures that no one can erase data once they are
written in the blockchain. This append-only nature of blockchain makes it a never-ending but fully
traceable system. Figure 1-2 shows the basic idea of blocks and chain in blockchain.
Every individual player maintains a copy of the blockchain, removing the need for central
administration or centralization. The addition of information to the existing blockchain happens in
the form of a new block appended at the end while at the same time ensuring that all copies of the
local blockchain available to the different network players must also be updated in the same order.
This will ensure data consistency in the blockchain, and all copies will be the same. This doubtlessly
will require an additional authentication and validation mechanism, but at a superficial level, everyone
will have an updated copy of the blockchain.
The data structure in blockchain consists of a chain of blocks linked together with the help of
current and previous pointers. These two fields store the hashed data of the contents of the block, the
previous pointer stores the hashed data of the previous block, and the current pointer stores the hashed
data of the current block.
The data are stored in the blockchain in a transparent way and are available to everyone, allowing
anyone to validate and verify the data as and when required.

Definition 1.1 Blockchain is a decentralized, immutable, append-only public ledger.


4 1 Introduction to Blockchain

1.5 Disruptive Technology

Clayton Christensen introduced the idea of disruptive technologies in a 1995 Harvard Business
Review article. Disruptive technology refers to any innovation that disrupts an existing market or
industry, displacing established products or services and creating new markets and opportunities.
These technologies often have a transformative effect on society, leading to changes in business
models, consumer behavior, and even cultural norms.
Not all innovations are disruptive technologies. It is the process rather than product or services.
Blockchain is a sustaining innovation rather than a disruptive innovation in the financial sector.
Disruptive technologies typically emerge from unexpected sources and are often initially dismissed
as inferior or irrelevant by established players in the market. However, as they gain momentum and
become more widely adopted, they can completely change the competitive landscape and reshape
entire industries.
The following are examples of disruptive technologies:

• Ecommerce: The rise of ecommerce in the 2000s disrupted traditional brick-and-mortar retail,
creating new opportunities for businesses to sell products and services online.
• Personal computers: The development of personal computers in the 1970s and 1980s disrupted the
established mainframe computer industry, creating new markets and opportunities for businesses
and individuals.
• Social media: The emergence of social media in the 2010s disrupted traditional media and
advertising industries, leading to the rise of new platforms for content creation and distribution.
• Digital photography: The advent of digital photography in the 1990s disrupted the traditional film
photography industry, leading to the demise of many established companies and the emergence of
new players in the market.

Blockchain is considered a disruptive technology for several reasons:

• Decentralization: One of the key features of blockchain technology is its ability to operate
in a decentralized manner, without the need for intermediaries such as banks or government
institutions. This eliminates the need for trust in centralized institutions, which can be slow,
expensive, and prone to corruption.
• Immutable and transparent: Blockchain technology is immutable and transparent, meaning that
once data are added to a blockchain, it cannot be modified or deleted. This creates a high degree of
trust in the data stored on the blockchain and eliminates the need for intermediaries to verify data.
• Security: Blockchain technology is secured by cryptographic algorithms that make it virtually
impossible to tamper with the data stored on the blockchain. This creates a high degree of security
for transactions and other data stored on the blockchain.
• Smart contracts: Smart contracts are self-executing contracts with the terms of the agreement
between buyer and seller being directly written into lines of code. This eliminates the need for
intermediaries to execute and enforce contracts, which can be slow, expensive, and prone to errors.
• Tokenization: Blockchain technology enables the creation and exchange of digital assets, or tokens,
which can represent anything of value, such as currency, property, or ownership rights. This creates
new opportunities for businesses to generate value and disrupt traditional business models.
1.6 History 5

1.6 History

Blockchain, a technology with the potential to become the foundation of global record-keeping
systems, was introduced a mere decade ago by anonymous individuals associated with the digital
currency Bitcoin, under the pseudonym Satoshi Nakamoto. Despite its relatively recent inception,
blockchain has quickly gained recognition as a transformative innovation, poised to revolutionize
various industries through its decentralized and secure nature.

1.6.1 Milestones in Blockchain Development

The subsection discusses some of the significant milestones in the development of blockchain
technology (Figure 1-3).

1. 2008 – The publication of Bitcoin’s whitepaper by Satoshi Nakamoto marked the groundbreaking
introduction of the cryptocurrency. This event revolutionized the financial landscape, ushering in
a new era of decentralized digital currency. The whitepaper laid the foundation for a peer-to-peer
electronic cash system that would eventually disrupt traditional monetary systems worldwide.
2. 2009 – The inaugural Bitcoin transaction between Satoshi Nakamoto and Hal Finney stands as
a significant milestone in cryptocurrency history. This historic event symbolized the practical
application and transferability of Bitcoin as a digital currency. The transaction showcased the

Figure 1-3 Blockchain timeline


6 1 Introduction to Blockchain

potential of Bitcoin as a decentralized payment system, setting the stage for its widespread
adoption and subsequent impact on the financial industry.
3. 2011 – Namecoin’s launch marked a groundbreaking moment as it became the first alternative
cryptocurrency to utilize blockchain technology. This pioneering step opened the door for a
multitude of innovative blockchain-based digital assets. Namecoin’s introduction demonstrated
the potential for decentralized systems beyond traditional currencies, paving the way for the
development of various blockchain applications and cryptocurrencies.
4. 2013 – Vitalik Buterin’s creation of Ethereum unleashed a revolutionary platform enabling
the creation of smart contracts and decentralized applications (dApps). Ethereum’s emergence
introduced a new paradigm in blockchain technology, empowering developers to build complex
applications on a decentralized network. Buterin’s vision laid the foundation for a vibrant
ecosystem of dApps, fueling innovation and transforming industries through the power of
decentralized computing.
5. 2015 – The formation of the Enterprise Ethereum Alliance united leading corporations and
blockchain startups, fostering collaboration in the advancement of blockchain technology. This
alliance served as a catalyst for exploring the potential of Ethereum in various industries and
promoting blockchain adoption on a global scale. The Enterprise Ethereum Alliance aimed
to accelerate innovation, establish industry standards, and drive the mainstream integration of
blockchain solutions across sectors.
6. 2016 – The Hyperledger Project, initiated by the Linux Foundation, set out to develop open-
source blockchain software specifically tailored to enterprise applications. This strategic launch
brought together industry leaders and technologists to collaborate on building scalable and
interoperable blockchain solutions. By providing a collaborative platform, the Hyperledger
Project aimed to accelerate the adoption of blockchain technology among businesses, fostering
transparency, efficiency, and trust in enterprise operations.
7. 2017 – The cryptocurrency market witnessed an unprecedented surge in value, primarily led by
Bitcoin, accompanied by an explosive growth in initial coin offerings (ICOs). This phenomenon
resulted in widespread frenzy and speculation, attracting investors seeking to capitalize on
the potential returns of digital assets. The soaring value of cryptocurrencies and the ICO
boom reshaped the financial landscape, bringing both opportunities and risks while fueling the
development of innovative blockchain projects worldwide.
8. 2018 – Blockchain-based platforms like IBM’s Food Trust have emerged as transformative
solutions for supply chain management, enabling enhanced transparency and traceability within
the food industry. By leveraging blockchain technology, these platforms offer a secure and
immutable record of every step in the supply chain, promoting accountability and reducing fraud.
The adoption of such blockchain solutions has the potential to revolutionize the way we track and
verify the origins, quality, and safety of food products, ensuring consumer confidence and driving
industry-wide improvements.
9. 2019 – Facebook’s launch of the Libra cryptocurrency encountered substantial regulatory scrutiny
and widespread resistance from governments worldwide. The ambitious project aimed to create
a global digital currency, but concerns over data privacy, monetary sovereignty, and potential
risks to the financial system led to intense pushback. The Libra initiative highlighted the
complex challenges and regulatory hurdles that arise when tech giants venture into the realm
of cryptocurrencies and sparked discussions on the future of digital currencies in a regulated
environment.
10. 2020 – Major financial institutions like JP Morgan and Goldman Sachs have embraced blockchain
technology, recognizing its potential for efficiency and security in financial operations. Simulta-
neously, numerous countries have launched their own central bank digital currencies (CBDCs),
1.7 Features of Blockchain 7

aiming to leverage the benefits of blockchain and enhance their monetary systems. This combined
trend showcases the growing acceptance and integration of blockchain technology within the
traditional financial sector, paving the way to transformative changes in how transactions and
currencies are managed globally.
11. 2021 – In a historic move, El Salvador became the first country to officially adopt Bitcoin as legal
tender in 2021. This decision enabled businesses to utilize Bitcoin for paying employee salaries
and established its acceptance as a valid payment method throughout the country. El Salvador’s
embrace of Bitcoin as a form of currency marked a significant milestone in the mainstream
acceptance and integration of cryptocurrencies into national economies.
12. 2022 – The year 2022 witnessed notable blockchain growth, particularly in the emergence of
national cryptocurrencies. This concept revolved around the idea of CBDCs, where central banks
opted to develop their own digital coins instead of relying on decentralized cryptocurrencies.
This trend highlighted a shift toward more centralized control over digital currencies, with central
banks exploring the benefits and challenges of issuing their own blockchain-based currencies.
13. 2023 – The year 2023 has witnessed a notable focus on environmentally friendly blockchains,
facilitated by carbon offsetting practices and energy-conscious network architectures. The
adoption of greener blockchains will be made more feasible through the utilization of eco-friendly
algorithms like proof of stake. These developments signify a growing commitment to reducing
the environmental impact of blockchain technology and promoting sustainable practices within
the industry.

1.7 Features of Blockchain

The remarkable attention and interest surrounding blockchain technology can be attributed to several
key factors (Figure 1-4).

1. Immutable
Immutability lies at the core of blockchain technology, rendering it an unchangeable and enduring
network. By operating through a network of nodes, the blockchain ensures that once a transaction
is recorded, it becomes permanent and resistant to modification. This immutability characteristic
establishes the blockchain as a secure and trustworthy ledger, bolstering confidence in its integrity
and authenticity.

Figure 1-4 Features of blockchain


8 1 Introduction to Blockchain

2. Distributed
Transparency is a fundamental feature of blockchain technology, as all network participants
possess a copy of the ledger, ensuring complete visibility. By employing a public ledger,
the blockchain offers comprehensive information regarding participants and transactions. The
distributed computational power across multiple computers enhances the efficiency and reliability
of the network, leading to improved outcomes in terms of security and consensus.
3. Decentralized
Blockchain technology operates as a decentralized system, devoid of a central authority, whereby
numerous nodes collaborate to authenticate and validate transactions. Every node within a
blockchain network possesses an identical copy of the ledger, ensuring consistency and eliminating
the need for a central point of control. This decentralized architecture enhances the security,
resilience, and transparency of the network, making it resistant to single points of failure or
manipulation.
4. Secure
In a blockchain, each record undergoes individual encryption, bolstering the overall security of
the network. The absence of a central authority does not grant unrestricted access to add, update,
or delete data on the network. Cryptographic hashing assigns a unique identity to every piece of
information on the blockchain, ensuring the integrity and immutability of the data. Each block
contains a distinctive hash along with the hash of the preceding block, creating cryptographic
links between blocks. Modifying the data would require changing all the hash IDs, an exceedingly
challenging and practically infeasible task.
5. Consensus
Consensus plays a vital role in blockchain networks by enabling efficient and impartial decision-
making. It involves the use of algorithms that allow a group of active nodes to reach swift and
reliable agreements, ensuring the smooth operation of the system. Although nodes may lack trust in
one another, they rely on the consensus algorithm at the heart of the network to facilitate consensus.
Various consensus algorithms exist, each with its own advantages and disadvantages. A consensus
algorithm is essential for any blockchain to maintain its value and integrity.
6. Unanimous
In a blockchain network, agreement on the validity of records is crucial before their inclusion.
When a node intends to add a block, it requires majority consensus through voting, ensuring that the
block can be added to the network. Unauthorized addition, modification, or deletion of information
is prevented. Updates to records occur simultaneously, rapidly propagating throughout the network.
Therefore, any changes without the consent of the majority of nodes are practically impossible due
to the stringent consensus requirements in place.
7. Smart Contract
Smart contracts are agreements whose provisions are encoded in computer code and automatically
execute. Without intermediaries, they automate, facilitate, and enforce contractual agreements.
Smart contracts augment a blockchain with programmable capabilities, allowing actions and
transactions to be activated automatically when predefined conditions are met. This function
improves the efficacy and independence of blockchain applications, such as financial services and
supply chain management.
1.9 Predicted Market 9

1.8 Present Growth

The growth of blockchain technology has continued to accelerate in recent years. Here are some
examples of its present growth:

• Investment: According to a report by CB Insights, global investment in blockchain startups has


increased steadily, with over USD .8.2 billion raised across 342 deals in 2021 alone.
• Enterprise adoption: Major corporations, including IBM, Walmart, and Visa, are investing in and
implementing blockchain technology for supply chain management, payment processing, and
other applications.
• Cryptocurrency adoption: Cryptocurrencies such as Bitcoin and Ethereum have seen significant in-
creases in adoption and investment. In 2021, the total market capitalization of all cryptocurrencies
surpassed USD 2 trillion.
• Government interest: Several governments around the world are exploring the use of blockchain
technology for various applications, including the development of CBDCs and voting systems.
• NFTs: The emergence of nonfungible tokens (NFTs) on blockchain platforms has created a new
market for digital assets and has the potential to revolutionize the art, music, and gaming industries.
• Increased scalability: The development of new blockchain technologies, such as sharding and
layer-2 solutions, is addressing the issue of scalability, making it possible to process more
transactions per second and enabling more widespread adoption.

1.9 Predicted Market

The market for blockchain technology is expected to continue to grow in the coming years. The
market size of blockchain technology globally was valued at USD 10.02 billion in 2022. It is projected
to experience significant growth at a compound annual growth rate (CAGR) of 87.7% from 2023 to
2030. This growth can be attributed to the rising venture capital funding in companies involved in
blockchain technology.

• Etherium will dominate


In 2023, Ethereum is poised to become the leading blockchain platform, driven by updates such
as the Merge and the Shanghai upgrade. These enhancements will enhance usability, performance,
and scalability, particularly with the implementation of proto-danksharding. As a result, Ethereum
will solidify its position as the preeminent player in the blockchain industry.
• Ethereum Staking
In 2023, Ethereum has emerged as the leading platform for staking, with over $20 billion staked,
driving innovation in this field. EigenLayer, a notable project, offers “security as a service” to other
blockchain platforms, leveraging Ethereum’s staked security to enhance their own. EigenLayer’s
upcoming EigenDA protocol aims to introduce a restaking mechanism in 2023, further advancing
staking innovation. With growing demand for ETH staking and the development of new solutions,
Ethereum will solidify its role as a global settlement layer for the Web3 ecosystem.
• Evolution of NFTs In the coming year, NFTs will expand beyond digital art, driven by major brands
like Starbucks. NFT rewards programs will inspire other commercial leaders to follow suit. The
fusion of physical and digital experiences will fuel NFT adoption. Projects lacking adaptation and
utility may fail, while recognized ones like CryptoPunks could thrive. The widespread commercial
use of NFTs will shape their future.
• Future of Tech Crypto
10 1 Introduction to Blockchain

Cryptocurrency enables trading and investment, while “tech crypto” prioritizes peer-to-peer
networks and global software for transactions. The shift toward tech crypto includes decentralized
finance and nonfinancial decentralized applications. Growing adoption of tech crypto will bolster
its importance, paving the way to the next bull market and providing stability amid market
fluctuations.
• Reputation Management in Web3
Decentralized identity and reputation systems will be vital for Web3 transactions in 2023, allowing
reputation transfer and holistic identity views. Projects like Intuition are leading the way by
leveraging attested data for a deeper understanding of identity. These systems will be fundamental
to Web3, enabling global decentralized coordination and supporting diverse interactions and
transactions.
• Bitcoin’s Market Challenges
Bitcoin’s market share is likely to be challenged in the coming year due to various factors. The lack
of daily utility compared to other tokens and ecosystems with higher commercial use diminishes
Bitcoin’s appeal. Criticism related to environmental concerns and the energy-intensive proof-of-
work system adds to the challenges. Bitcoin’s failure to serve as a risk-off digital gold hedge may
hinder its progress, creating an opportunity for a more utility-driven layer 1 asset in the next bull
run.
• Web 3.0 Gaming
In 2023, Web 3.0 gaming is set to overcome its early flaws and integrate Web3 utility and gaming
aesthetics more seamlessly. The industry’s focus will shift toward gameplay-centered studios,
moving away from token-centric projects. Game projects will leverage advanced technologies to
enhance gameplay experiences and drive growth in the Web 3.0 gaming market. The upcoming
year holds promise for Web 3.0 gaming to engage the global gaming community of three billion
players and reshape its negative reputation.

1.10 Blockchain Types

Blockchain is a digital ledger technology that provides a secure and transparent way of storing and
sharing data. There are different types of blockchain, each with its unique features and characteristics
(Figure 1-5).
Some of the blockchain types are categorized into permissioned and permissionless; they overlap
is illustrated in Figure 1-6.
The following subsections present the characteristics of various categories of blockchain and
provides examples of each.

Figure 1-5 Types of blockchain


1.10 Blockchain Types 11

Figure 1-6 Permissionless vs. permissioned blockchain

1.10.1 Public

A public blockchain is a type of blockchain technology that is open to anyone, and anyone with
Internet facilities is eligible to participate. It operates in a decentralized manner, allowing participants
to validate blocks and send transactions without the need for permission from a central authority.
Public blockchains often use two major consensus algorithms: proof of work (PoW) and proof of
stake (PoS).
They are characterized by their openness, transparency, and lack of central authority. Nodes can
join and leave a network freely, and all nodes can verify new data added to the blockchain.
Public blockchains employ incentive mechanisms to ensure the correct operation of the system.
They are permissionless, meaning anyone can access the blockchain without requiring permission,
and the ledger is shared and transparent. Participants in a public blockchain can remain anonymous,
as real names and identities are not necessary. Public blockchains offer users greater freedom and
flexibility in how they use the platform, without the limitations imposed by regulations.

Characteristics of Public Blockchain

• Permissionless and open to all


• Cooperation among all nodes to verify data
• Use of incentive mechanism-based protocols
• Shared and transparent ledger
• Secure with 51% rule
• Anonymous and hidden user identity
• Absence of regulations or limitations on participant
• No way to track transactions
12 1 Introduction to Blockchain

Examples of Public Blockchain

Examples of public blockchains include Bitcoin (BTC), Ethereum (ETH), Ripple (XRP), Litecoin
(LTC), Cardano (ADA), and Stellar (XLM) are examples of public blockchains.

1.10.2 Private

Private blockchains are blockchain networks that are permissioned and accessible only to a specific
group of people or organizations. Unlike public blockchains, where anyone can participate in the
network, private blockchains are designed to restrict access to certain authorized users. Private
blockchains are often used by organizations to build secure and private networks that can improve
efficiency and reduce costs.

Characteristics of Private Blockchain

• Private blockchains operate in a closed network and have permissions managed by an organization.
• They are suitable for specific use cases where organizations want to exert control over access and
network parameters.
• The advantages of private blockchains include faster transaction speeds, better scalability, and
customization options.
• However, private blockchains go against decentralization and distributed ledger principles. They
rely on centralized nodes, which can create challenges in establishing trust and compromise
security.
• Use cases for private blockchains include supply chain management, asset ownership verification,
and internal voting systems.
• Private blockchains provide organizations with control and customization but may sacrifice the
decentralization and security offered by public blockchains.

Examples of Private Blockchain

Hyperledger Fabric, Corda, Quorum, Multichain, and R3 Corda Enterprise are examples of private
blockchains.

1.10.3 Federated

A federated blockchain is a type of blockchain network that operates under a federated consensus
model. It involves a consortium of organizations or nodes that work together to validate transactions
and maintain the blockchain. Federated blockchains are permissioned, meaning access and participa-
tion are restricted to authorized entities within the consortium.
In a federated blockchain, the consensus mechanism is typically based on a select group of
nodes that form the federation. These nodes are responsible for validating transactions and reaching
consensus on the state of the blockchain. Unlike public blockchains, federated blockchains are more
centralized as the decision-making power lies with the participating entities.
1.10 Blockchain Types 13

Federated blockchains offer advantages such as improved scalability, faster transaction speeds,
and enhanced privacy and security compared to public blockchains. They are suitable for use cases
where a consortium of organizations needs to collaborate and share data while maintaining control
and privacy.

Characteristics of Federated Blockchain

• Permissioned and accessible only to authorized entities within a consortium


• Consensus achieved through a select group of nodes forming the federation
• Improved scalability and faster transaction speeds compared to public blockchains
• Enhanced privacy and security features
• More centralized decision-making compared to public blockchains
• Suitable for consortium-based collaborations and data sharing

Examples of Federated Blockchain

IBM Blockchain Platform, Ripple, Quorum (Enterprise Ethereum), Corda Enterprise, and Hyper-
ledger Fabric Consortium Networks are examples of federated blockchains.

1.10.4 Hybrid

A hybrid blockchain is a combination of both public and private blockchains, offering the benefits
of both models. It allows for the interoperability of different blockchain networks and enables
the exchange of data and assets between them. Hybrid blockchains provide flexibility in terms of
transparency, control, and scalability.
In a hybrid blockchain, certain parts of the network are public, allowing for open participation
and transparency, while other parts are private, providing restricted access and enhanced privacy.
The integration of public and private blockchains enables organizations to leverage the advantages
of public networks for certain use cases while maintaining control and privacy for sensitive data or
operations.
The hybrid model offers the ability to customize the level of decentralization and privacy based
on specific requirements. It provides a balance between transparency and confidentiality, making
it suitable for various applications, such as supply chain management, healthcare, finance, and
government sectors.

Characteristics of Hybrid Blockchain

• Combination of public and private blockchains


• Flexibility in terms of transparency and control
• Interoperability between different blockchain networks
• Customization of decentralization and privacy
• Suitable for applications with varying requirements
14 1 Introduction to Blockchain

Table 1-1 Comparison of Public and Private Blockchains


Basis of Comparison Public Blockchain Private Blockchain
Access Permissionless Permissioned
Network actors Don’t know each other Know each other
Decentralized vs. centralized Decentralized More centralized
Order of magnitude Lower Higher
Native token Yes Not necessary
Speed Slow Fast
Transactions per second Fewer More
Security More secure Less secure
Energy consumption More Less
Consensus algorithms Proof of work, proof of stake, etc. Proof of elapsed time, raft, etc.
Attacks Risk of collision or 51% attack No minor collision, known validators
Effects Disrupt current business models Reduce transaction costs and data redundancies
Examples Bitcoin, Ethereum, etc. R3, EWF, B3i, Corda

Examples of Hybrid Blockchain

Dragonchain, Ardor, Wanchain, XinFin, and MultiChain are examples of hybrid blockchain plat-
forms.

1.10.5 Difference Between Public and Private Blockchains

Let us differentiate between public and private blockchains with respect to a few criteria as given in
Table 1-1.

1.11 Blockchain Framework

A blockchain framework is a set of protocols, rules, and standards that define the structure and
operations of a blockchain network. A blockchain framework has the following five major layers
(Figure 1-7).

1.11.1 Hardware/Infrastructure Layer

The bottom-most layer of the blockchain is the hardware or infrastructure layer. It involves servers
hosted in data centers that store the content of the blockchain. These servers provide the necessary
resources for the blockchain network to function effectively. Some important features of this layer are
as follows:

• The top layer of the blockchain is the hardware or infrastructure layer, consisting of servers hosted
in data centers.
• The servers store the content of the blockchain and provide necessary resources.
• Client–server architecture is different from blockchain’s peer-to-peer (P2P) network.
1.11 Blockchain Framework 15

Figure 1-7 Blockchain framework

• Blockchain utilizes a P2P network of computers to calculate, validate, and record transactions in a
shared ledger.
• Transactions are organized into blocks in an ordered format.
• The end result is a distributed database that tracks all data, transactions, and relevant information.
• Nodes, which are computers in the P2P network, play a crucial role in the blockchain.
• Nodes verify transactions, group them into blocks, and roadcast them to the network.
• Once consensus is reached, nodes update their local copies of the ledger and commit the block to
the blockchain network.
• Any device that connects to the blockchain network becomes a node and contributes to the
network’s functioning.

1.11.2 Data Layer

By combining the use of linked lists, Merkle trees, and digital signatures, blockchain technology
establishes a secure and transparent structure for storing and verifying data. Some of the important
features of the data layer are as follows:

• The blockchain’s data structure relies on a linked list, which consists of blocks with transactions
and pointers to previous blocks.
• Pointers in the linked list refer to the location of other variables and maintain the sequential order
of blocks.
• The blockchain employs a Merkle tree, a binary tree of hashes, to ensure security, integrity, and
immutability.
• The Merkle tree holds important information such as the hash of the previous block, date, nonce,
block version number, and current difficulty goal.
• Transactions on the blockchain are digitally signed, providing authentication and integrity.
• The digital signature allows anyone with the corresponding public key to verify the authenticity of
a transaction.
• Data encryption further enhances security and protects the sender’s or owner’s identity.
• The structure of a block on the blockchain is determined by the data layer.
16 1 Introduction to Blockchain

1.11.3 Network Layer

The network layer, also known as the P2P layer, is responsible for facilitating communication between
nodes in a blockchain network. It enables transactions, block propagation, and discovery among the
nodes. Some important features of the network layer are as follows:

• The network layer focuses on maintaining the validity of the blockchain network’s current state by
enabling effective communication, synchronization, and information propagation between nodes.
• In a P2P network, distributed nodes collaborate to achieve a common goal, and in the context of
blockchain, they perform tasks related to transactions and contribute to the overall functioning of
the network.
• There are two types of nodes: full nodes and light nodes. Full nodes handle crucial functions
such as mining, enforcing consensus rules, and validating transactions. Light nodes, on the other
hand, have limited capabilities and primarily store the blockchain headers, allowing them to send
transactions but with fewer responsibilities compared to full nodes.

1.11.4 Consensus Layer

The consensus layer is a critical component of blockchain platforms like Ethereum, Hyperledger, and
others. It plays a fundamental role in the functioning of these platforms. Some of the notable feature
of the consensus layer are as follows:

• The consensus layer validates blocks, ensures correct ordering, and achieves agreement among
participants.
• In a distributed P2P network, the consensus layer establishes essential agreements and rules for
maintaining integrity and security.
• Consensus ensures agreement on the validity and order of transactions, preventing manipulation
and maintaining fairness.
• The consensus layer maintains power distribution and decentralization, preventing a single entity
from controlling the blockchain.
• It enables collective decision-making and agreements among network participants.

1.11.5 Application and Presentation Layer

This is the nearest layer from user perspective. Some of the important features of this layer are:

• The application and presentation layer is the user-facing part of the blockchain that provides a
graphical user interface (GUI) and allows users to interact with the network.
• It includes execution layer and application layer protocols, such as smart contracts, scripts, and
frameworks.
• Users can communicate with the blockchain network through various applications like wallets,
social media apps, browsers, and NFT platforms.
• These applications interact with the blockchain network using application programming interfaces
(APIs).
• The semantic layer within this layer is where transaction validations and executions take place,
ensuring the integrity and accuracy of transactions.
1.12 A Block and Its Structure 17

• One key characteristic of these applications is their decentralized data storage, which sets them
apart from traditional applications.
• Decentralized data storage provides secure and tamper-proof storage of data, enhancing the overall
security and trustworthiness of the blockchain network.
• Users can access and manage their data securely within these applications, knowing that their
information is stored in a decentralized and immutable manner.

1.12 A Block and Its Structure

A block is an essential component of a blockchain, typically comprising a collection of transactions.


Its structure consists of a preamble that stores metadata such as a timestamp and reference to the
previous block (known as the parent block) and a body that contains the actual transaction data. The
unique cryptographic hash of each block ensures the chain’s integrity. Blocks are chained together in
chronological order to create a secure and immutable ledger.

1.12.1 A Block

A blockchain block is a fundamental component of a blockchain database that serves as a data


structure for permanently recording transaction data in a cryptocurrency blockchain. It contains a
collection of the most recent transactions that have not yet been validated by the network. Once
the data within a block are validated, the block is considered closed, and a new block is created to
accommodate and validate new transactions.
The significance of a block lies in its role as a secure and immutable storage unit. Once information
is written into a block, it becomes a permanent part of the blockchain and cannot be altered or removed
without detection.

1.12.2 Block Structure

The structure of a block in a blockchain can vary depending on the specific implementation and type
of blockchain. However, in general, a block typically consists of several components (Figure 1-8).

• Block header: The block header contains metadata about the block, including the block number,
timestamp, and the hash of the previous block in the chain.
• Nonce: A nonce is a random number generated by miners in order to solve the cryptographic
puzzle required to add a block to a blockchain.
• Transaction data: The transaction data section contains the actual data that are being added to the
blockchain. This can include information such as the sender and recipient addresses, the amount
of cryptocurrency being transferred, and any additional data related to the transaction.
• Block hash: The block hash is a unique identifier that represents the contents of the block. It
is generated by hashing the block header and transaction data using a specific cryptographic
algorithm.
• Merkle tree: The Merkle tree is a data structure used to efficiently store and verify large amounts
of transaction data. The transactions are hashed and combined in pairs to form a series of hashes,
which are then combined until a single root hash is produced.
18 1 Introduction to Blockchain

Figure 1-8 Block structure

1.12.3 Ledger

In the context of blockchain, a ledger is a digital record-keeping system that records all transactions
made on the network. The ledger maintains a permanent and tamper-proof record of all transactions.
Since a blockchains commonly track transactions, they are often referred to as ledgers or some times
distributed ledgers.
The ledger is decentralized; no central authority controls it. Instead, all nodes on the network have
a copy of the ledger. The copy is updated in real time as long as new transactions are made. All nodes
can view the transactions and their associated data.
The ledger is usually maintained using cryptographic algorithms, such as hashing, ensuring the
integrity and security of the data. Each block on the blockchain contains a hash of the previous block,
creating a chain of blocks that are linked together. This creates an immutable ledger that cannot be
altered or tampered with without the consensus of the network. To say that anyone can operate a node
means the blockchain can be stored in a distributed manner called a distributed ledger. This makes it
very difficult for an attacker, who must make changes to a number of copies across the network. This
causes the prevention of a denial-of-service (DoS) attack, since there is no single point of failure.
The ledger can also be permissioned or permissionless. In a permissioned blockchain, access to
the ledger is restricted to authorized participants, whereas in a permissionless blockchain, anyone can
participate in the network and view the ledger.
The ledger provides a transparent, secure, and tamper-proof record of all transactions made on the
network.
1.12 A Block and Its Structure 19

1.12.4 Distributed

Distributed refers to the way in which data are stored and processed across a network of computers
or nodes. The workload is shared across multiple nodes, rather than being centralized in a single
location.
Allowing anyone to operate a node means the blockchain can be stored in a distributed manner.
Each node has a copy of the entire blockchain, and each node is responsible for verifying transactions
and maintaining the integrity of the network. Distributed architecture provides several benefits,
including the following:

• Resource sharing:
In a distributed system, computers connect and share resources like software and hardware. Re-
source sharing involves remote access to components, reducing costs and improving convenience.
Data sharing ensures consistency and facilitates information exchange.
• Heterogeneity:
Distributed systems are diverse, with varying hardware, programming languages, networks, and
implementations, all working together efficiently.
• Scalability:
Scalability in distributed systems involves accommodating more users and computers, without
altering components but designing them to handle growth effectively.
• Concurrency:
The concurrency property of distributed systems enables simultaneous execution of multiple ac-
tivities across different machines, managed by a common system, allowing for parallel processing.
• Fault tolerance:
Distributed systems enhance fault tolerance and availability through software recovery and
hardware redundancy, ensuring reliable operation despite failures.
• Openness:
Openness in distributed systems refers to their capability to adapt and enhance hardware and
software components as needed, enabling seamless integration of new components through
standardized interfaces. This ensures compatibility and allows for resource sharing services.

1.12.5 Transparency

Transparency refers to the ability of anyone on a network to view and verify transactions and other
data on the blockchain.
In a blockchain network, transactions are public and visible to all nodes on the network. This
means that anyone can view the transaction data, including the sender, receiver, amount, and all other
relevant details.
The transparency of the blockchain provides several benefits, as follows:

• Trust:
The transparency of blockchain instills trust by allowing anyone to verify the validity and proper
execution of transactions.
• Accountability:
The public nature of blockchain ensures accountability for all participants’ actions on the network.
20 1 Introduction to Blockchain

• Fraud prevention:
The transparency of blockchain makes it more difficult for bad actors to commit fraud or engage
in other malicious activities on a network.
• Efficiency: The transparency of blockchain improves network efficiency by eliminating intermedi-
aries and reducing transaction costs.

1.12.6 Confirmation

Block confirmation refers to the process of validating and adding transactions to a blockchain. This
verification involves solving mathematical puzzles, ensuring transaction integrity, and preventing
double-spending. Nodes compete to solve these puzzles, and the first node to succeed is rewarded
for its efforts. Validated transactions are added to a block, which is then appended to the blockchain
through the process of mining.
Block confirmation is crucial for maintaining the integrity and security of the blockchain.
Once a transaction is confirmed and added to the blockchain, it becomes permanent and cannot
be altered. This process eliminates the need for intermediaries, enhances network efficiency, and
reduces transaction costs. Confirmation helps prevent fraudulent activities and ensures trust within
the blockchain network.

1.12.7 Proof of Work

Proof of work (PoW) is a consensus algorithm used in most cryptocurrencies to prevent double-
spending. It requires users to solve complex mathematical puzzles to validate transactions and add
them to the blockchain. PoW ensures transaction integrity and prevents fraud, making it necessary for
maintaining a secure and trustworthy network.
In PoW, transactions are grouped into blocks, and miners use computing power to hash block
data and find solutions to puzzles. The hashing process creates a unique fingerprint for each block,
making it impossible to reverse-engineer the original data. Miners play a guessing game, modifying
a nonce value until they find a hash that meets protocol conditions. Successful miners are rewarded
with cryptocurrency and can broadcast the new block to the network, where other participants update
their blockchains accordingly.
The difficulty of finding valid hashes increases with network hash rate, ensuring a controlled block
discovery rate. While mining can be computationally expensive, the potential rewards incentivize
miners to contribute their resources to secure the blockchain.

1.12.8 Block Awards

Block rewards, also known as mining rewards, are a type of incentive provided to miners in a
blockchain network as compensation for their efforts in verifying transactions and adding new blocks
to the blockchain.
The block reward consists of newly generated coins and transaction fees given to miners for
securing the network. As an example, the current reward is 6.25 coins per block, which undergoes
a halving event every four years to limit the total supply. While the block reward remains stable,
transaction fees can fluctuate. Transactions are initiated through a wallet and transmitted to a
decentralized network of nodes, which validate and authenticate the transaction details. Nodes play
1.12 A Block and Its Structure 21

a crucial role in securing blockchain networks, and mining nodes assemble validated transactions
into candidate blocks. Miners are rewarded with a block reward for solving the PoW algorithm and
providing confirmed financial settlement.

1.12.9 Transactions and UTXOs

Transactions are the fundamental building blocks that represent the transfer of cryptocurrency or
other digital assets from one user to another. A transaction typically includes information such as
the sender’s address, the recipient’s address, the amount of cryptocurrency being transferred, and a
transaction fee.
Nodes validate transactions before adding them to the blockchain. Unspent transaction outputs
(UTXOs) are created with each transaction, representing unspent outputs associated with specific
addresses. UTXOs enhance privacy and security by making it difficult to trace cryptocurrency flow
and preventing theft from unauthorized addresses.
In a UTXO-based blockchain, each transaction creates one or more UTXOs, which represent
unspent outputs of a transaction. Each UTXO is associated with a specific address and can only
be spent by the owner of that address.
For example, suppose Alice wants to send 1 Bitcoin to Bob. Alice would create a transaction that
includes the details of the transfer, including Bob’s address and the amount being transferred. The
transaction would create a new UTXO associated with Bob’s address for the amount of Bitcoin being
transferred. Alice’s UTXO associated with her address would also be created, but it would be marked
as spent.
When Bob wants to spend the Bitcoin he received from Alice, he creates a new transaction that
includes the details of the transfer, including the recipient’s address and the amount being transferred.
The transaction would reference the UTXO associated with Bob’s address that was created when he
received the Bitcoin from Alice. The UTXO associated with Alice’s address would still be marked as
spent. It is more difficult for a malicious actor to steal cryptocurrency from another user’s address.

Examples

• Consider Alice’s desire to make a purchase from an online store that accepts cryptocurrency. She
initiates a transaction by sending the requisite cryptocurrency amount to the store’s wallet address.
The transaction details include the wallet address of the recipient, the payment amount, and a
transaction fee. Once confirmed and added to the blockchain, this transaction establishes a UTXO
associated with the store’s address that represents the payment received. This UTXO can now be
spent at the store.
• Imagine a decentralized application (DApp) that rewards ecosystem participation with a native
token. When a user interacts with a decentralized application and earns tokens, a transaction is
generated to transmit the tokens to the user’s wallet address. This transaction establishes a UTXO
representing the received tokens that is linked to the user’s address. Whenever the user wishes
to use these tokens within the DApp or transmit them to another user, they can reference the
UTXO associated with their address in a subsequent transaction. This mechanism ensures that
token transfers within the DApp’s ecosystem are secure and traceable.
22 1 Introduction to Blockchain

Nodes

In a blockchain network, a node is a computer or device that is connected to the network and
participates in the validation and propagation of transactions and blocks. Nodes play a critical role in
the security and decentralization of a blockchain network.
There are several different types of nodes, as described in what follows.
A blockchain network contains different types of nodes with distinct roles:

• Full nodes: These maintain a complete copy of the blockchain, validate transactions and blocks,
and propagate information to other nodes.
• Mining nodes: These nodes solve complex mathematical problems to add new blocks to the
blockchain and receive rewards for their work.
• Light nodes: Also known as thin clients, they rely on full nodes to validate transactions and blocks,
without maintaining a complete copy of the blockchain.
• Master nodes: Found in some networks, these nodes perform additional functions like network
governance, enhanced security, and enabling advanced features.

Nodes play a crucial role in blockchain networks by facilitating communication and validation
through a P2P network. They contribute to the security and decentralization of the network without
the need for a centralized authority. Additionally, nodes can participate in governance processes, such
as voting on network changes. By acting as nodes, users actively support the overall integrity and
functioning of blockchain networks.

1.12.10 Consensus

Consensus refers to the process by which participants on a blockchain network agree on the state
of the ledger. In a decentralized network, there is no central authority to make decisions. Instead,
participants on the network must come to a consensus on which transactions are valid and which
blocks should be added to the chain. Consensus mechanisms, such as proof of work or proof of stake,
are used to ensure that all nodes on a network agree on the state of the ledger.

1.12.10.1 Types of Consensus


In a blockchain network, consensus refers to the process of achieving agreement among network
participants about the state of the blockchain. Consensus is critical for the security and integrity of
the network as it ensures that all nodes have a consistent view of the blockchain and prevents double-
spending and other types of fraudulent activity.
Blockchain networks use several different mechanisms to achieve consensus, including:
PoW: This is the most widely known consensus mechanism used by networks such as Bitcoin and
Ethereum. In a PoW system, miners compete to solve complex mathematical problems in order to
add new blocks to the blockchain. The first miner to solve the problem and add the block is rewarded
with newly created cryptocurrency and transaction fees. The difficulty of the problem adjusts over
time to maintain a consistent rate of block creation.
Proof of stake (PoS): In a PoS system, network participants “stake” their cryptocurrency as
collateral to be selected to validate new blocks. Validators are chosen based on the amount of
cryptocurrency they have staked and are rewarded with transaction fees for adding new blocks to
the blockchain. The idea behind PoS is that it is less resource-intensive than PoW as it does not
require miners to solve complex mathematical problems.
1.13 Scaling Blockchain 23

Delegated proof of stake (DPoS): DPoS is a variant of PoS used by networks such as EOS and
BitShares. In a DPoS system, token holders vote to elect a smaller group of validators (known as
“delegates”) to validate transactions and add new blocks to the blockchain. The delegates are rewarded
with transaction fees, and token holders can vote to replace delegates that are not performing their
duties satisfactorily.
Byzantine fault tolerance (BFT): BFT is a consensus mechanism that is used in some blockchain
networks, such as Hyperledger Fabric. In a BFT system, network participants communicate with each
other to reach agreement on the state of the blockchain. The system is designed to tolerate a certain
degree of malicious behavior (known as the “Byzantine fault”), such as nodes intentionally trying to
disrupt the network.

1.13 Scaling Blockchain

Scalability in blockchain refers to expanding the transaction capacity of a network while maintaining
security, speed, and decentralization. Overcoming this challenge is crucial as most existing blockchain
networks have limitations in transaction throughput.
There are various approaches to scaling blockchain, which include

• Segregated Witness (SegWit): This protocol upgrade separates signature data from transaction
data, thereby increasing the capacity of a blockchain network.
• Sharding: Through sharding, a blockchain network is divided into smaller subsets called shards,
enabling independent transaction processing and enhancing overall network capacity.
• Layer 2 solutions: These solutions build upon existing blockchain networks to boost transaction
throughput without requiring changes to the underlying blockchain protocol. Examples include
Lightning Network and Plasma.
• PoS: This is an alternative consensus mechanism to the traditional PoW used by most blockchain
networks. PoS reduces the computational power needed for mining and thus increases transaction
throughput.
• Off-chain transactions: These transactions occur outside a blockchain network, reducing the load
on the network and increasing transaction throughput.
• Sidechains: Sidechains are separate blockchain networks connected to a main blockchain. They
enhance transaction throughput by independently processing transactions and then settling them
on the main blockchain.

1.13.1 Issues in Scaling

Scaling blockchain is a complex and ongoing challenge that involves addressing technical and
nontechnical issues. Here are some of the key concerns:

• Decentralization: Maintaining decentralization is crucial while scaling a network since centralizing


it for higher transaction throughput could compromise security and trust.
• Security: Scaling blockchain networks can introduce security risks, as larger networks become
more vulnerable to attacks. It is vital to maintain network security during capacity expansion.
• Interoperability: With the emergence of multiple blockchain networks, ensuring interoperability
among them is important. Interoperability provides opportunities for scaling blockchain networks,
but coordination between different networks is necessary.
24 1 Introduction to Blockchain

• Governance: Governance plays a critical role in the success of blockchain networks, and it becomes
more complex as networks scale. Establishing clear governance structures ensures a secure,
decentralized, and efficient network.
• Energy consumption: Blockchain networks consume significant computational power for transac-
tion validation and block creation. Scaling a network could lead to increased energy consumption,
which is not sustainable in the long run.
• Adoption: Adoption is vital for blockchain networks, and scaling can present adoption barriers. As
a network expands, it becomes more complex and challenging for new users to navigate.
• Regulations: Different jurisdictions have varying regulations for blockchain technology, which
can hinder scaling efforts. Implementing clear regulatory frameworks is essential for the legal and
efficient operation of a network.

Addressing these issues requires collaboration among blockchain developers, industry leaders, and
regulators to ensure the long-term viability and success of blockchain technology.

1.13.2 Off-Chain Computation

Off-chain computation involves conducting computations outside a blockchain network using


separate computational resources or infrastructure. Its benefits include reducing the load on the
blockchain network, increasing transaction throughput, and reducing transaction fees.
Several techniques facilitate off-chain computation in blockchain:

• State channels: State channels are off-chain payment channels that allow multiple transactions
between two parties without recording them on the blockchain. They enhance transaction
throughput and reduce fees by enabling direct transactions.
• Sidechains: Sidechains are separate blockchain networks connected to the main blockchain. They
provide a space for developers to experiment with new features and functionalities without
compromising the security or performance of the main network.
• Plasma: Plasma is a framework for creating scalable blockchain networks by using a treelike
structure of sidechains. Plasma allows for off-chain computation by enabling developers to create
customized sidechains that can process transactions independently of the main network.
• Trusted execution environments (TEEs): TEEs are secure computing environments that can run
code in isolation from the rest of the system. TEEs can be used to perform off-chain computations
securely and efficiently.

Off-chain computation in blockchain offers benefits such as higher transaction throughput, lower
transaction fees, and improved scalability. However, it comes with challenges regarding security,
interoperability, and governance. Striking a balance between on-chain and off-chain computation is
crucial to ensuring the long-term viability and success of blockchain technology.

1.13.3 Sharding in Blockchain

Sharding is a technique used to scale blockchain networks by dividing them into smaller subsets called
shards. Each shard operates independently and processes its own transactions, leading to increased
overall transaction throughput. Sharding addresses scalability issues by distributing the transaction
load among different shards.
1.15 Laboratory Work 25

Implementing sharding involves partitioning a network and assigning transactions to specific


shards based on certain criteria. Coordination between shards is crucial to maintaining network
security and decentralization. Communication and synchronization mechanisms are necessary to
ensure consistent views and propagate changes across shards.
Although sharding offers benefits, it also introduces challenges. Security and consistency can be
more difficult to maintain as each shard operates independently. Governance and interoperability
issues may arise due to varying rules and regulations among shards.
Sharding is a promising approach to scaling blockchain networks, but it requires careful planning
and execution.

1.14 Blockchain DApps and Usecases

Blockchain DApps are applications that run on a blockchain network, typically using smart contracts
to execute code and perform actions on the network. DApps are decentralized, meaning they are not
controlled by a central authority or organization, and they are transparent, meaning that their code
and data are publicly visible and auditable.
There are several use cases for blockchain DApps, including the following:

• Financial applications: Blockchain DApps can be used for a wide range of financial applications,
such as payment systems, remittances, lending platforms, and asset tokenization.
• Supply chain management: Blockchain DApps can be used to track and manage supply chains,
providing greater transparency and accountability in the movement of goods and products.
• Identity verification: Blockchain DApps can be used for identity verification and authentication,
creating a secure and decentralized system for managing personal data and credentials.
• Voting systems: Blockchain DApps can be used for secure and transparent voting systems,
ensuring that votes are recorded and counted accurately.
• Gaming and entertainment: Blockchain DApps can be used for gaming and entertainment
applications, such as decentralized marketplaces for in-game items and virtual assets.
• Healthcare: Blockchain DApps can be used for managing healthcare data and records, providing a
secure and transparent system for storing and sharing sensitive medical information.

Blockchain DApps have the potential to transform a wide range of industries and applications,
providing greater security, transparency, and efficiency. However, developing and deploying DApps
on blockchain networks can be complex and challenging, requiring specialized skills and expertise.
As the technology continues to evolve, it is likely that we will see even more innovative use cases for
blockchain DApps in the future.

1.15 Laboratory Work

This section presents the implementation of basic concepts in blockchain using Python.
Another random document with
no related content on Scribd:
The Project Gutenberg eBook of A memoir of

Sir John Drummond Hay, P.C., K.C.B.,

G.C.M.G., sometime minister at the court of


Morrocco
This ebook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this ebook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

Title: A memoir of Sir John Drummond Hay, P.C., K.C.B.,


G.C.M.G., sometime minister at the court of Morrocco

Author: Louisa Annette Edla Drummond-Hay Brooks


Alice Emily Drummond-Hay

Release date: October 12, 2023 [eBook #71860]

Language: English

Original publication: London: John Murray, 1896

Credits: Galo Flordelis (This file was produced from images


generously made available by The Internet Archive and
the HathiTrust Digital Library)

*** START OF THE PROJECT GUTENBERG EBOOK A MEMOIR


OF SIR JOHN DRUMMOND HAY, P.C., K.C.B., G.C.M.G.,
SOMETIME MINISTER AT THE COURT OF MORROCCO ***
A MEMOIR OF

SIR JOHN HAY DRUMMOND HAY


Oxford
HORACE HART, PRINTER TO THE UNIVERSITY
Barraud’s Photo. Walker & Boutall Ph. Sc.

J. H. Drummond Hay
A MEMOIR OF

SIR JOHN DRUMMOND HAY


P.C. K.C.B. G.C.M.G.

S O M E T I M E M I N I S T E R AT T H E C O U RT O F
MOROCCO BASED ON HIS JOURNALS
AND CORRESPONDENCE

WITH A PREFACE BY
SIR FRANCIS W. DE WINTON K.C.M.G.

PORTRAITS & ILLUSTRATIONS

LONDON
JOHN MURRAY ALBEMARLE STREET
1896
P R E FA C E

On his retirement from public service in 1886, Sir John Hay


Drummond Hay, at the instance of many friends, undertook to set
down the recollections of his life. Some of these notes were
published in Murray’s Magazine in 1887 under the title of ‘Scraps
from my Note-book’; others were laid by to be incorporated in a
complete volume. The work was, however, interrupted by an
accident to one of his eyes which rendered it impossible for him to
write. For a time he confined himself to dictating to my sister, who
acted as his amanuensis, quaint stories and detached incidents
connected with the Moors, intending to resume the continuous tale of
his life when his sight grew stronger. But, shortly after the recovery of
his eyesight, and before he had proceeded much further in
‘unwinding the skein of his memories,’ he was prostrated by a severe
illness, followed by influenza, of which he died in 1893.
It has fallen therefore to my sister, Miss Drummond Hay, and
myself, his two daughters, to endeavour to unite, to the best of our
ability, these scattered notes and memoranda, and to add to them
such details as could be supplied from our own recollections. In this
task we have been naturally somewhat restricted. In the first place,
we have been obliged to omit from the memoirs of one who lived and
died so recently much that might have been published twenty years
hence. In the second place, as we have been necessarily debarred
from using any official documents except those published in the Blue
Books, our work can scarcely do full justice to the life of a public
servant. These restrictions have not lightened our task; and, had it
not been for the kindly help and advice of friends, we should have
had still greater difficulty in tracing, from my father’s notes and
private correspondence, the course of his lifelong labours in
Morocco.
The main portion of Sir John’s letters are addressed to his mother
—to whom he was a devoted son—and, later, to his eldest sister,
Mrs. Norderling, who was during her lifetime the sympathetic and
intelligent sharer of his confidences. Except with his mother and
sister he carried on but little private correspondence, principally on
account of his sight, which was enfeebled after an illness in 1859.
But he wrote occasionally to friends, several of whom preserved and
have kindly lent us his letters. Some of these have been utilised, and
for all of them our thanks are most gratefully tendered.
On my father’s account of his school days at the Academy in
Edinburgh and at the Charterhouse in London, on his early life at
Tangier, or on his apprenticeship to diplomacy at Constantinople
under Lord Ponsonby and the great Elchi, it is unnecessary to dilate.
The recollections and impressions of boyhood and youth break off
when more serious work presses on him after his appointment as
Consul General in Morocco in 1845. Though considered very young
for such a post, for he was only twenty-eight, his training in Egypt
and Turkey well qualified him for a position which was destined to
give scope to a character eulogised by one of his chiefs as vigorous,
temperate, and straightforward. He was aided by his great facility in
writing and speaking foreign languages, as at that time he had
perfect command of Turkish, Italian, Spanish, French, and Arabic;
and to the end he retained his fluency in the last three.
From the moment that he was appointed Consul General in
Morocco, his letters are animated by the one great aim on which his
public career was concentrated—the increase and consolidation of
British influence in Morocco. British interests, he believed, could best
be furthered by the encouragement of commerce, by the
amelioration of the condition of the Moors, and also by personally
gaining the respect of the people. Extracts from his diary of 1846
tend to show how he set himself to attain these objects; and his
endeavours bore good fruit. The account of the arrest of piracy in Rif,
through his intervention, may be taken as an instance of his direct
personal influence in dealing with the wild mountain tribes.
His power of talking familiarly in their own tongue with natives of
every degree was of great advantage to him in gaining a personal
hold on the people, and many illustrations might be quoted from the
stories which he tells of his meetings with various governors of the
provinces through which he travelled in the course of his frequent
journeys to the Court at the cities of Marákesh, Fas, Meknes, or
Rabát. In fact his purity of motive, tenacity of purpose, his ever ready
and shrewd advice, won the respect and good opinion of the people
of Morocco. Implicit confidence was placed in him by high and low
alike.
On his influence with successive Sultans it is unnecessary to
enlarge. The offer of Sultan Sid Mohammed to place in his hands the
entire control of the foreign affairs of Morocco speaks for itself. The
story of Benabu, again, relates how the latter entrusted untold gold
to my father’s keeping, assured that his treasure would reach its
destination, though no witness or written paper attested to the
transaction. Lastly, to take an instance in humble life, we may point
to the pitiful faith placed in him by a wretched Rifian criminal when
on the point of surrendering himself to the authorities.
Nor were this reliance in his uprightness and this respect for his
judgment confined to the Moors. During the war between Spain and
Morocco, when he alone of all the Foreign Representatives
remained at his post, he was appealed to by the Spanish friars to
protect their church and its sacred contents from the insults of the
angry Moslems. Their confidence was not misplaced: his protection
secured the sacred building from the slightest injury.
The Spanish war at first seemed likely to check the development
of trade in Morocco at the moment when it was on the point of
revival. The promise of prosperity was due to the Commercial
Convention negotiated by my father in 1856, a convention which an
old resident in Morocco, one well qualified to appreciate its value,
has termed the Magna Charta of that country. But when peace was
declared, the result of the contest proved eventually to be rather a
blessing than a curse. The necessity of raising a loan to pay the war
indemnity impelled the Sultan to ask help from Great Britain, thus
enabling my father the more forcibly to impress upon H.S.M. the
necessity of introducing into the administration of the Customs
reforms which immediately and substantially increased the revenues
of Morocco.
Yet in spite of the good results which in this instance followed the
acceptance of his advice, the apathetic and ignorant Moors could
rarely be induced to take active steps in the path of reform. It was
only under the pressure of necessity that any advance was made.
This lethargy did not, however, proceed from any want of plain
speaking on my father’s part. As is shown by the account of his
private interview with Sultan Sid Mohammed at Marákesh in 1872,
he indicated to that potentate, in the clearest and most emphatic
language, the debased condition of his realm, and the iniquities of
the system under which his subjects were governed.
But it was not with the recalcitrant Moorish Government alone that
my father had to contend. His later letters recount his failure to put a
check on the abuses caused by the protection of natives by
foreigners, and the consequent downfall of his hope that the end of
his career might be signalised by another and more extended
commercial treaty. The Moorish Government was not inclined to
promote foreign trade, contending that greater facilities for
commerce would inevitably cause an influx of alien traders, each of
whom would have his native agents and servants under the
protection of a foreign flag, and that such protected subjects, not
being immediately amenable to the native authorities, would only
increase friction, lessen the Sultan’s authority, and diminish the
exchequer.
Her Majesty’s Government recognised my father’s value by
repeated promotion, and honours were bestowed on him under
various administrations; but he was given to understand that his
services could not be spared from the country where, it may be said,
he was an acknowledged power. Indeed, the principal aim of my
father’s life during his long career in Morocco—the preponderance of
British influence over that of all other nations—may be said to have
been attained and maintained during his tenure of office. In 1885, the
last year of his official life, he writes with reference to his unceasing
anxiety that neither France nor any other country should by any
means obtain a footing in Morocco, ‘As a sentinel of the Straits, I fire
my gun, as a warning, when I know of a move to obtain that object.’
Sir Francis de Winton, in his kindly and graceful introduction,
touches on the expedition to the lower slopes of the Atlas made by
the Mission, of which he was a member, in 1872, when the heights to
the eternal snow were climbed by Capt. Sawle and Mr. Drummond
Hay, and when the cordial reception offered by the wild natives left a
pleasant impression on my father and his party. He also refers to my
father in the light of a sportsman. The latter’s recollections of many
of the happiest days of his life spent in pursuit of wild boar and other
game were noted by himself, and some of them have been
embodied in this work. A keen and hard rider, an unerring shot in his
earlier days, before his eyesight was impaired, and of almost
reckless courage, he was well fitted to become the elected leader
and head of the native hunters. Under his rule sport flourished in the
environs of Tangier, the ground allotted for the purpose by the Sultan
was properly guarded, and the close season strictly observed: it was
then that pigsticking in Morocco reached its highest perfection, and
gave pleasure to many of every rank and condition, whether
Europeans or natives.
It is doubtful whether sport could again flourish in the environs of
Tangier as it did in my father’s day. An increasing armed European
population, the introduction of weapons of precision, and the
denudation of the woods, render such a prospect unlikely. His stories
therefore of narrow escapes and exciting days may prove of interest
to the lover of the chase; and to some, who in those bygone years
shared his sport, may perhaps recall the memory of pleasant times
spent with him in the field.
L. A. E. BROOKS.
INTRODUCTION

To this memoir of the late Sir John Hay Drummond Hay I have
been asked by his daughters to write a few introductory lines.
My acquaintance with Sir John began in the year 1870. At that
time I was quartered at Gibraltar, being on the staff of Sir William
Fenwick Williams of Kars, who was then Governor of the fortress.
They were old comrades, Sir John and Sir Fenwick, having
served together in Constantinople, and the friendship begun in
Turkey was continued at the gates of the Mediterranean. Often and
often Sir John and Lady Drummond Hay, with their two daughters,
visited the Convent at Gibraltar; and in return the doors of the
Legation at Tangier were ever open, and always gave us a hearty
welcome.
It was between 1870 and 1875 that this intercourse took place,
and to me it is filled with happy recollections. The quaint old town of
Tangier, full of the decaying influences of Moslem rule, yet keeping
up the struggle of life after an existence of over a thousand years;
racial and religious differences, civilisation and barbarism, struggling
along together, while Jews and Arabs, unchanged for five hundred
years, jostled with Christendom of the present day. It was a strange
medley: and out of it all stands one figure prominent, nay pre-
eminent, in the history of Morocco during the past forty years.
I do not think Sir John’s reminiscences sufficiently convey the
enormous influence he wielded in the empire, so called, of Morocco.
Throughout the Sherifian dominions his name was known and
respected; and after the Emperor and the Sheríf of Wazan, his was
the most powerful influence in the state. His long residence in the
country; his intimate acquaintance with the manners and customs of
the people; his perfect knowledge of Arabic; his love of justice; his
absolute fearlessness; his keen appreciation of their sports and
amusements, in which he often joined; not only made him the trusted
friend of the late Emperor and his predecessors, but also the chosen
friend of the people.
In the many expeditions in which I had the good fortune to be one
of Sir John’s companions, I had abundant opportunities of observing
the power he possessed over the different tribes with whom we
came in contact; and especially among the hill tribes of Jebel Musa,
who occupy the country between Tangier and Tetuan. These people
held him in great esteem, and often sought his advice and counsel in
their tribal differences; thus enabling him to be of service to the
Emperor in the constant struggles between that ruler and his people.
A description of Morocco and its government has often been
essayed by various writers; but no one could give an adequate idea
of Sir John’s influence who had not personally witnessed his
intercourse with the discordant elements which constitute the
government of that country.
On one occasion I had the pleasure of being on Sir John’s staff
when he paid a visit to the Emperor, who was then residing at
Marákesh. What a pleasant journey it was! The daily ride, the
evening camp, our first view of the great Atlas range of mountains,
the entry into Marákesh, our reception by the Sultan, and the six
weeks we spent in the city but little known to Europeans; and it was,
perhaps, the events of that journey which impressed one more than
anything as to the individuality and power of the British
Representative.
By many Sir John will also be remembered as an ardent
sportsman. Whether he was organising a boar-hunt, or a day after
partridge, or enjoying a run with the Calpe hounds, there was always
the same keen interest, the thorough enjoyment of sport, which
characterised the man. Under his guidance you were always sure of
finding boar, or of getting a good bag of partridge; and it was through
Sir John that, some twelve miles South of Tangier, where the ground
was favourable, the exciting sport of pigsticking was introduced into
Africa. Well do I remember after a day’s sport the evening camp fire,
round which we gathered after dinner, when Sir John would tell us of
some of his earlier hunting recollections. He was an excellent story-
teller, keeping his audience in a state of the deepest interest to the
end; and then, with a merry twinkle in his eye, he would finish his
narrative by a description of some ludicrous incident in which he was
often the chief actor, and no one joined more heartily in the laughter
which followed than he himself.
It is not possible, within the short space of an introduction, to give
more than a mere outline of the personality of Sir John Drummond
Hay. His recollections furnish the true index to his character. In them
are reflected the sterling honesty, the integrity, and the courage and
capacity of the man who, though working in a country but little known
and full of prejudice and fanaticism, made England respected and
trusted. He belonged to that band of the men of Great Britain who
serve their country wherever they are placed, and who, while mindful
of her interests and her honour, gain the good will of the rulers and
the people to whom they are accredited.
In conclusion, I shall ever remember him as a friend whom I
respected, and for whom I always had a true affection; and when
asked to write these few lines, while wishing the duty had fallen to an
abler pen than mine, I felt that, having been honoured with his
friendship, I might, in affectionate remembrance of that friendship,
write this brief tribute to his memory.
F. DE WINTON.
CONTENTS

CHAP. PAGE

Preface v
Introduction xiii
I. Boyhood. 1816-1834 1
II. Waiting for Employment—José Maria. 1834 11
III. Alexandria. 1840 20
IV. Constantinople and Lord Ponsonby. 1840 30
V. Constantinople with Sir Stratford Canning. 1841 49
VI. Mission to Tangier 66
Political Agent at Tangier and First Mission to
VII.
Marákesh. 1845-1846 76
VIII. Visit to Salli and Departure for Marákesh. 1846 97
IX. Residence at Marákesh and Return to Tangier. 1846 108
X. Difficulties of Mr. Hay’s Position at Tangier. 1846-1854 133
XI. Life at Tangier 142
XII. Second Mission to Marákesh. 1855 167
XIII. Benabu. 1857 184
XIV. Mr. Hay’s Position at Tangier. 1858 198
XV. The War with Spain. 1859-1862 205
XVI. Sir John Hay’s Home at Tangier. 1862 221
XVII. Third Mission to Marákesh. 1863 230
XVIII. João, the Portuguese Gunsmith 241
XIX. Fourth Mission to Marákesh. 1872 264
XX. Ascent of the Atlas Mountains 288
XXI. Mission to Fas in 1875 307
XXII. 1876-1879 318
XXIII. Third Mission to Fas. 1879-1880 329
XXIV. 1881-1884 338
XXV. Last Year of Official Life. 1885 354
XXVI. Out of harness 365
INDEX 399

You might also like