Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Learn Ethereum.: A practical guide to help developers set up and run decentralized applications with Ethereum 2.0
Learn Ethereum.: A practical guide to help developers set up and run decentralized applications with Ethereum 2.0
Learn Ethereum.: A practical guide to help developers set up and run decentralized applications with Ethereum 2.0
Ebook1,588 pages11 hours

Learn Ethereum.: A practical guide to help developers set up and run decentralized applications with Ethereum 2.0

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Ethereum is a blockchain-based, decentralized computing platform that allows you to run smart contracts. With this book, you’ll discover the latest Ethereum tools, frameworks, wallets, and layer 2, along with setting up and running decentralized applications for the complete, end-to-end development experience.
Learn Ethereum, 2nd Edition is a comprehensive overview of the Ethereum ecosystem, exploring its concepts, mechanisms, and decentralized application development process. You’ll delve into Ethereum's internals, technologies, and tools, including Ethereum 2.0 and the Ethereum Virtual Machine (EVM), gas, and its account systems. You’ll also explore Ethereum's transition to proof of stake, L1/L2 scaling solutions, DeFi protocols, and the current marketplace. Additionally, you’ll learn about EVM-compatible blockchains, connectivity techniques, and advanced topics such as sharding, off-chain scaling, DAOs, Metaverse, and NFTs.
By the end of this book, you’ll be well-equipped to write smart contracts and develop, test, and deploy DApps using various tools, wallets, and frameworks.

LanguageEnglish
Release dateAug 31, 2023
ISBN9781804617519
Learn Ethereum.: A practical guide to help developers set up and run decentralized applications with Ethereum 2.0
Author

Xun (Brian) Wu

Brian is a senior blockchain architect and consultant. He is a prolific writer on the subject of blockchain. As one of the prominent voices in the blockchain community, he has written 7 books on blockchain covering popular blockchain technologies like Hyperledger and Ethereum from beginner to advance levels. He has 18+ years of extensive hands-on experience with blockchain-based enterprise application design and development, big data, cloud computing, UI, and system infrastructure solutions. He has also successfully repurposed and integrated the blockchain-based applications into gaming, supply chain, digital asset tokenization platform as well as, cryptocurrency blockchain analytics industries. In addition to his strong background in the blockchain space, he has served as the tech lead for multiple key technology initiatives at leading financial institutions including J.P. Morgan, Citigroup, and Bank of America.

Read more from Xun (Brian) Wu

Related to Learn Ethereum.

Related ebooks

Security For You

View More

Related articles

Reviews for Learn Ethereum.

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Learn Ethereum. - Xun (Brian) Wu

    cover.png

    BIRMINGHAM—MUMBAI

    Learn Ethereum

    Copyright © 2023 Packt Publishing

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

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

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

    Group Product Manager: Reshma Raman

    Publishing Product Manager: Arindam Majumder

    Book Project Manager: Kirti Pisat

    Senior Editor: Nathanya Dias

    Technical Editor: Devanshi Ayare

    Copy Editor: Safis Editing

    Project Coordinator: Farheen Fathima

    Proofreader: Safis Editing

    Indexer: Rekha Nair

    Production Designer: Alishon Mendonca

    DevRel Marketing Coordinator: Nivedita Singh

    First published: September 2019

    Second edition: September 2023

    Production reference: 2230823

    Published by Packt Publishing Ltd.

    Grosvenor House

    11 St Paul’s Square

    Birmingham

    B3 1RB, UK.

    ISBN 978-1-80461-651-2

    www.packtpub.com

    Contributors

    About the authors

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

    He has authored nine books, which have been published by O’Reilly, Packt, and Apress, focusing on popular fields within the Blockchain industry. The titles of his books include: Learn Ethereum (First Edition), Learn Ethereum (Second Edition), Blockchain for Teens, Hands-On Smart Contract Development with Hyperledger Fabric V2, Hyperledger Cookbook, Blockchain Quick Start Guide, Security Tokens and Stablecoins Quick Start Guide, Blockchain by Example, and Seven NoSQL Databases in a Week.

    I would like to thank my family and friends for their patience and support throughout this endeavor.

    Zhihong Zou is an innovative technology leader with over 20 years of experience delivering cutting-edge enterprise solutions. He has led transformative solutions in telecommunications, healthcare, and government that leverage blockchain, AI/ML, and modern cloud architectures to solve complex business challenges. As an insightful visionary and trusted advisor, Zhihong is passionate about driving digital transformation and future-proofing organizations. He holds an M.Sc degree in computational mathematics and an M.Sc in computer science.

    Zhihong has authored multiple papers on numerical computing and two popular editions of the book Learn Ethereum. In his free time, Zhihong enjoys sharing his blockchain expertise as an advocate and writer.

    I would like to thank my family for their understanding, encouragement, support and patience throughout this journey.

    Dongying Song has been a noteworthy leader in Blockchain, Ethereum, big data, ML, data science, and the cloud application development space for over 8 years. As an experienced software engineer and data scientist, she has worked for top-tier banks and pharmaceutical and technology companies. Dongying holds a master's degree in statistics from Columbia University and majored in mathematics during her undergraduate years. Her recent activities focus on Blockchain, Ethereum, and smart contract developments.

    About the reviewer

    Mayukh Mukhopadhyay is a technology consultant with more than a decade of experience in designing business continuity solutions amidst of complex digital transformation programs. He did his M.Eng from Jadavpur University and MBA from IIT Kharagpur. He is currently pursuing Ph.D. (industry track) from IIM Indore. He has authored the book Ethereum Smart Contract Development and also served as the technical reviewer of the Ethereum Cookbook. In his leisure time, he peer-reviews various blockchain and business research papers for Scopus-indexed journals and IEEE conferences.

    I am perennially indebted to my daughter Abriti and spouse Mrittika for making sacrifices in every walk of life to support my academic endeavors.

    Table of Contents

    Preface

    Part 1: Blockchain and Ethereum Basics

    1

    Blockchain and Cryptocurrency

    Technical requirements

    Introducing blockchain technology

    Decentralized P2P networks

    How does blockchain work?

    Rehashing cryptography

    Public-key cryptography

    Cryptographic hash function

    Digital signatures

    Anatomizing a blockchain consensus mechanism

    What is consensus?

    Proof-of-work

    Proof-of-stake

    Forking

    Understanding Bitcoin and cryptocurrency

    Bitcoin basics

    What is a wallet?

    Transactions, UTXO, and account balances

    Genesis block and coin supply

    How does Bitcoin payment work?

    Limitations in Bitcoin

    Altcoins

    Overview of blockchain use cases in the industry and government

    Financial services

    Healthcare

    Blockchain use cases led by government

    Ushering in the world of Ethereum

    Smart contract

    EVM

    Account

    Summary

    2

    Ethereum Architecture and Ecosystem

    Technical requirements

    Introducing the Eth1 Architecture

    Ethereum – the world computer

    Web 2.0, Web3, to the Metaverse

    DApps

    Diving deep into Eth1

    Accounts

    Two types of accounts

    EOAs

    CAs

    Transactions and messages

    Smart contracts

    Ether and gas

    The EVM

    Addresses and wallets

    Examining mining in Eth 1.0

    Mining and the consensus protocol

    Ethereum transactions and block structure

    Transaction validation and block verification

    Understanding scaling challenges in Ethereum

    The blockchain scalability trilemma

    Modular blockchain architecture

    Ethereum scaling solutions

    Introducing Beacon Chains and Eth2

    PoS in Eth2

    How the beacon chain works

    Benefits from the transition to PoS

    Merging Eth1 and Eth2

    Merging Eth1 data into Eth2

    Scaling Ethereum with rollups

    Optimistic rollups

    ZK rollups

    Summary

    3

    Decentralized Finance

    Technical requirements

    Introducing decentralized finance

    Mastering Ethereum token standards

    Definition of cryptocurrency

    Crypto funding mechanism

    Ethereum token standards

    Analyzing stablecoins and MakerDAO Dai

    Understanding DeFi protocols

    Basic DeFi constructs

    Lending and borrowing

    Decentralized exchanges

    Decentralized derivatives and insurance

    Yield farming and DeFi continuum

    Making sense of cryptoeconomics

    DeFi after the collapse of FTX

    Summary

    4

    EVM-Compatible Blockchain Networks

    Technical requirements

    Understanding EVM blockchain ecosystems

    Introducing BSC

    Consensus mechanism in BSC

    Block creation and finality

    BNB governance

    BNB ecosystem and roadmap

    Scaling Ethereum with Polygon chains

    How Polygon PoS and Plasma work

    Block production and checkpoints

    Consensus mechanism in Polygon PoS

    Multi-chain solutions with Polygon Edge

    Polygon ecosystem and roadmap

    Diving deep into high-performance Avalanche chains

    Avalanche blockchain architecture overview

    Avalanche consensus mechanism

    Subnets and enterprise blockchains

    Governance in Avalanche chains

    Avalanche ecosystem

    Bridging interoperability gaps between blockchains

    Cross-chain integration challenges

    Common cross-chain bridge designs

    Trustless bridge solutions using Polkadot

    Generalized bridge solutions with Cosmos

    Decentralized oracle as a bridge

    Glancing over non-EVM blockchain networks

    TRON blockchain overview

    Introduction to Solana

    Summary

    5

    Deep Research and the Latest Developments in Ethereum

    Technical requirements

    Understanding the challenges in distributed systems

    The CAP theorem

    Horizontal scaling versus vertical scaling

    Scaling Ethereum

    Making sense of the Ethereum roadmap

    Pivotal to rollup-centric

    Overview of the post-merge Ethereum roadmap

    Sharding and data availability sampling

    Data availability sampling

    Discovering MEV and PBS

    Overview of MEV

    MEV implementation in Ethereum

    Proposer builder separation

    zkEVM and EVM improvements

    Overview of zk-SNARK

    Types of zkEVM implementations

    Workings of zkEVM

    Smart contract wallets and account abstraction

    Account abstraction and EIP 4337

    How a smart contract wallet works

    DAOs

    Introduction to DAOs

    DAO governance case study using Uniswap

    NFTs, Web3, and Metaverse

    Introduction to a world of NFTs, Web3, and Metaverse

    The current state of NFTs

    Web3 and the future of the internet

    The Metaverse, virtual reality, and future interactions

    Summary

    Part 2: Ethereum Development Fundamentals

    6

    Fundamentals of Solidity

    Technical requirements

    Introducing Solidity

    Tools for the Solidity development environment

    Learning about the fundamental programming structure in Solidity

    The layout of a Solidity source file

    State variables

    Built-in data types

    User-defined data types

    Functions

    The pure, view, and payable functions

    Constructor and destructor functions

    Fallback function

    Function overloading

    Function modifiers

    Events

    Global variables, contextual variables, and functions

    Enabling the contracts library

    Understanding inheritance, abstract contracts, and interfaces

    Interface

    Abstract contract

    Inheritance

    Multiple inheritance and the diamond problem

    Function overriding

    Examining smart contract execution under the hood

    Mastering advanced programming concepts in Solidity

    Smart contract security

    Keeping contracts simple and modular

    Use the checks-effects-interactions pattern

    DoS with block gas limit

    Handling errors in external calls

    Best practices in smart contracts

    Writing upgradable smart contracts

    Economic consideration of smart contract execution

    Types of smart contract

    Putting it all together – rental property leasing

    Summary

    7

    Web3 API Fundamentals

    Technical requirements

    Introducing Web 3.0

    Setting up an Ethereum environment

    Installing Geth

    Setting up a private Ethereum network

    Generating and configuring the genesis file

    Initializing the chain instance

    Starting a Geth node

    Connecting to Ethereum networks

    Learning the fundamentals of web3.js – the Ethereum JavaScript API

    web3.js project setup

    web3.js Account

    web3.js ABI

    Web3 providers

    Setting up the Ethereum testnet environment

    The web3.js Transaction API

    Getting started with web3.py

    Prerequisites

    Introducing Web3j – Web3 Java Ethereum DApp API

    Setting up a simple Spring Boot application

    Checking the balances of Ethereum accounts

    solc and the Web3j CLI

    Deploying smart contracts using Web3j

    Transacting with a smart contract

    Querying the state of a smart contract

    Summary

    8

    Developing Your Own Cryptocurrency

    Technical requirements

    Understanding token standards

    ERC-1155

    Batch transfers

    Batch balance

    Batch approval

    ERC-1155’s TokenReceiver

    Setting up an Ethereum development environment

    Working with Truffle

    Creating an ERC-20 token – MyERC20Token

    Creating basic token information

    Defining and implementing the ERC-20 interface

    Assigning an admin role

    Working with the whitelist function

    Locking and unlocking an account

    The mint and burn tokens

    Creating an ERC-721 token – DigitalArtERC721Token

    Designing the decentralized digital art marketplace

    Setting up the DigitalArtERC721Token project

    Creating the basic token information

    Defining and implementing the ERC-721 standard

    Defining the art and art transaction struct

    Creating a non-fungible digital art token

    Implementing the buyArt() function

    Implementing the resellArt() function

    Implementing the findArt() function

    Implementing the findMyArts() function

    Implementing the findAllPendingArt() function

    Getting all art transactions through getArtAllTxn()

    Creating an ERC-1155 token – ERC1155NFTToken

    Uploading NFT images

    Writing the ERC-1155 NFT token contract

    ERC-1155

    Summary

    Part 3: Ethereum Development Fundamentals

    9

    Smart Contract Development and Test Fundamentals

    Technical requirements

    Understanding Remix development fundamentals

    Working with the Solidity Compiler

    Testing

    Deploying and running transactions

    Debugging

    Understanding development using Truffle and unit testing

    The Truffle console and development features

    Running a Truffle migration

    Truffle unit testing

    Truffle Box

    Understanding Hardhat development and unit testing

    Hardhat Runner

    Hardhat Network

    Project structure

    Installation and environment setup

    Hardhat project setup

    Creating and compiling the ERC1155 contract

    Testing the contract

    Deploying the contract

    Security testing

    Static and dynamic analysis

    Working with a linter

    Summary

    10

    Writing a Frontend to Build the NFT Marketplace DApp

    Technical requirements

    Understanding DApps

    Creating your first NFT

    Set up a wallet

    Obtain Ether (ETH)

    Visit OpenSea

    Create NFT item

    Working with the Web3 JavaScript API

    Setting up a DApp development environment

    Installing Ganache desktop

    Creating a development workspace

    Setting up the project for our DApp

    Deploying a smart contract

    Installing MetaMask and connecting to Ganache

    Setting MetaMask Ganache accounts

    Building frontend UI components

    Setting up project dependencies

    Getting the instance of a deployed contract

    Building a navigation bar

    Running the digital art market DApp

    Publishing your art (the first account)

    Publishing your art (the second account)

    Summary

    11

    Ethereum Tools and Frameworks

    Technical requirements

    Understanding the Ethereum development tools and framework

    Layer 1 node connections

    Working with Infura

    Working with the Infura Ethereum API

    Using Remix with Infura

    Introducing Alchemy

    Testing tools

    The Ethereum client API

    Working with Ethereum storage

    Working with Swarm

    Getting to know the popular smart contract libraries

    Working with OpenZeppelin

    Setting up a dev environment

    Access control

    Programming languages and integrated development environments

    Vyper

    Summary

    Part 4: Production and Deployment

    12

    Setting Up Ethereum Private Chain

    Technical requirements

    Understanding private Ethereum networks

    Setting up a local private network

    Prerequisites

    Installation on MacOS with Homebrew

    Creating a private Ethash network using Besu

    Private enterprise Ethereum in production use

    Hyperledger

    Hyperledger Fabric

    Hyperledger Sawtooth

    Corda

    Quorum

    Resources for private enterprise Ethereum development

    Ethereum node services

    Ethereum clients

    Summary

    13

    Deployment of Your DApps

    A brief introduction to test networks

    Deploying a smart contract to a test network using Truffle

    Installing Truffle

    Deploying a smart contract to the Goerli testnet using Truffle

    Deploying a smart contract to the Sepolia testnet

    Deploying a smart contract to a test network using Hardhat

    Installing Hardhat

    Deploying a smart contract to the Goerli testnet using Hardhat

    Summary

    14

    Building Ethereum Wallets

    Technical requirements

    Understanding the wallet technology

    Understanding non-deterministic and deterministic wallets

    Mnemonic code words (BIP-39)

    Generating a private key in Ethereum

    Multiparty signature wallets

    Stealth addresses

    Basic stealth address protocol

    ISAP

    Dual-Key stealth address protocol

    Confidential transactions

    Creating an Ethereum wallet

    Creating a non-deterministic wallet

    Viewing the Keystore file generated by the geth command

    Creating a non-deterministic wallet with MyCrypto

    Creating an HD wallet

    Working with third-party Ethereum wallets

    Wallets on different platforms

    Smart contract wallets

    Hot and cold wallets

    Transferring funds between wallets

    Summary

    15

    Oracles, Technology, and Layer 2 in Practice

    Technical requirements

    Understanding oracles

    Examples of blockchain oracles

    Event data for prediction markets

    Blockchain network

    Types of oracles

    Working with oracles

    Introduction to Chainlink

    Chainlink VRF DiceRollGame

    Diving deep into cross-chain technology

    How does a cross-chain bridge work?

    Vulnerabilities in a cross-chain bridge

    Popular cross-chain bridges

    Cross-chain in practice

    Bridge tokens from Ethereum to Avalanche

    Bridging ETH from Ethereum to Polygon (MATIC)

    Working with Layer 2

    Optimistic rollups

    ZK rollups

    Writing a ZKP program using ZoKrates

    Summary

    Part 5: Conclusion

    16

    Conclusion

    Technical requirements

    Facing the challenges of Ethereum and blockchain

    Scalability and economic security

    User experience, privacy, and mainstream adoption

    Ethereum governance

    CBDC and government regulations

    Emerging trends in Ethereum and the blockchain ecosystem

    Application blockchain

    Enterprise blockchain and blockchain as a service

    The multi-chain world

    AML/CFT, KYC, and decentralized identity

    Blockchain-powered Metaverse

    Meeting the future of Ethereum

    Summary

    Index

    Other Books You May Enjoy

    Preface

    Welcome to the second edition of Learn Ethereum: A practical guide to help developers set up and run decentralized applications with Ethereum 2.0. This book is an indispensable resource for individuals seeking to understand and master the Ethereum blockchain platform. Within these pages, we embark on a captivating journey through the world of Ethereum, exploring its underlying principles and its potential for revolutionizing industries, and provide a step-by-step process for building Decentralized Applications (DApps). Whether you are a developer, entrepreneur, investor, or enthusiast, this comprehensive guide equips you with the necessary knowledge, tools, and skills to navigate the Ethereum ecosystem with confidence.

    Ethereum has transformed the execution of smart contracts as a blockchain and decentralized computing platform. This book establishes a solid foundation for comprehending the Ethereum ecosystem, starting with an introduction to blockchain, cryptography, and cryptocurrencies. We will explore vital concepts, such as consensus mechanisms, and mining processes. We will also dive into the architecture of Ethereum 2.0, Ethereum Virtual Machine (EVM), and layer 1/layer 2 (L1/L2) scaling solutions (like optimistic rollups and ZK rollups), as well as the transition to proof of stake (PoS) through the Beacon Chain.

    Moving forward, we will conduct an in-depth analysis of Decentralized Finance (DeFi), covering token standards, stablecoins, and various DeFi protocols to provide you with a comprehensive understanding of this thriving field. In addition, we will examine the significance of EVM compatibility, shedding light on prominent EVM-compatible blockchain networks, such as BNB Smart Chain, Polygon Chain, and Avalanche Chain. Furthermore, we will delve into advanced topics and the latest developments within the Ethereum ecosystem. We will extensively cover Ethereum’s plan for solving scaling challenges, with a focus on its end game of the rollup centric Ethereum roadmap.

    To facilitate practical application, we will devote a significant portion of this book to the Solidity programming language. Through an exploration of its fundamentals, contract patterns, exception handling, and more, we will empower you to develop your own Ethereum DApps. Moreover, we will guide developers in utilizing Ethereum web3 APIs with JavaScript, Python, and Java, enabling seamless interactions with the Ethereum blockchain.

    Throughout the book, we will present comprehensive guides that will lead you through the entire process of designing, developing, testing, deploying, and monitoring DApps. By creating ERC20, ERC721, and ERC1155 smart contracts, you will gain hands-on experience in building your own cryptocurrencies. To further solidify your understanding, we will provide a practical demonstration in the form of the Decentralized NFT marketplace, employing essential tools such as node.js, Web3, Truffle, and Hardhat.

    To expand your knowledge and proficiency, we explore a variety of Ethereum tools and frameworks, including IPFS, Infura, Alchemy, and QuickNode. Through practical examples, we will enhance your understanding of these tools. Additionally, we will discuss the distinctions between public and private blockchains, thus guiding you in deploying complete smart contract applications across various blockchain environments. Moreover, we will delve into wallet design, which will enable you to comprehend wallet functionality and construct secure wallet systems.

    Finally, will we delve into cutting-edge topics such as oracles, cross-chain solutions, and layer 2 technologies. We introduce the concept of oracles and demonstrate how they operate in the decentralized Web 3.0 ecosystem, reacting to real-world events and interacting with traditional systems. Additionally, we briefly explore Ethereum cross-chain bridge technology, which allows users to transmit tokens and arbitrary data between blockchain networks. Lastly, we will examine practical implementations of L2 technologies, equipping you with knowledge of the latest Ethereum advancements.

    By the end of this book, you will possess a comprehensive understanding of Ethereum, encompassing fundamental concepts, advanced topics, and the latest developments. Furthermore, you will have the ability to write smart contracts, and develop, test, and deploy DApps using a diverse array of tools, wallets, and frameworks

    Now, let us embark on this exciting journey into the Ethereum ecosystem. Together, we will unlock the potential of this groundbreaking technology and explore the possibilities it holds for the future.

    Who this book is for

    Learn Ethereum, Second Edition, is designed for developers, entrepreneurs, investors, and enthusiasts seeking to master the fundamentals of the Ethereum blockchain and build real-world DApps.

    Developers: Gain practical knowledge and hands-on experience in building DApps using Ethereum. This comprehensive guide caters to both beginners and experienced developers, providing essential tools and insights for enhancing your Ethereum development skills.

    Entrepreneurs: Discover the potential of blockchain technology and its applications across industries. Explore Ethereum’s capabilities and practical implementations to identify opportunities for leveraging this technology in your business ventures and gaining a competitive edge.

    Investors: Understand the underlying principles of Ethereum to make informed investment decisions in the cryptocurrency and blockchain space. Gain insights into Ethereum’s ecosystem and navigate the dynamic landscape of blockchain projects and tokens.

    Enthusiasts: Immerse yourself in the world of blockchain, cryptocurrencies, and the Ethereum ecosystem. This book offers a thorough exploration of these subjects, providing you with a deep understanding of Ethereum’s core concepts, its impact on industries, and the tools and frameworks involved in Ethereum development.

    Regardless of your background or expertise, Learn Ethereum, Second Edition, equips you with the knowledge, practical skills, and confidence to actively participate in Ethereum’s rapidly evolving ecosystem.

    What this book covers

    Chapter 1, Blockchain and Cryptocurrency, covers a comprehensive understanding of blockchain technologies, specifically focusing on the Ethereum ecosystem. Starting with basic concepts relating to Bitcoin, Ethereum, cryptocurrency, and blockchain, the book covers topics such as introducing blockchain technology, exploring cryptography, understanding the blockchain consensus mechanism, delving into Bitcoin and cryptocurrency, previewing blockchain use cases in various industries and government sectors, and introducing the world of Ethereum. By the end of this chapter, you will have gained the necessary knowledge to understand Ethereum accounts, forks, and the concept of mining.

    Chapter 2, Ethereum Architecture and Ecosystem, describes the architecture of Ethereum and helps you understand EVM, gas, and accounts, among other concepts. It also covers the fundamentals of ether mining. The chapter delves into how the Beacon Chain operates and how Ethereum implements the PoS consensus mechanism. Additionally, it explores Ethereum’s merge of Eth1 and Eth2. This chapter discusses the challenges of scaling Ethereum and provides an overview of various L1 and L2 scaling solutions, including optimistic rollups and ZK rollups. By the end of this chapter, you will have a solid understanding of the internals and diverse technologies within the Ethereum ecosystem.

    Chapter 3, Decentralized Finance, explores cryptocurrency and DeFi. It introduces Ethereum token standards, including fungible tokens and NFTs, and delves into stablecoins, with a focus on MakerDAO.The chapter then covers various DeFi categories such as lending, borrowing, exchanges, derivatives, fund management, lotteries, payments, and insurance. Prominent protocols within each category are highlighted, providing insights into the leading players in the DeFi ecosystem. Additionally, you will gain an understanding of the current state of the DeFi marketplace. This chapter concludes by offering a forward-looking perspective on the future of DeFi. This chapter provides you with essential knowledge of Ethereum token standards, stablecoins, and the diverse range of DeFi products and services. It equips you with insights into leading protocols and a glimpse into the future of this transformative financial landscape.

    Chapter 4, EVM-Compatible Blockchain Networks, offers an overview of several EVM-compatible blockchain networks, namely Binance Smart Chain, Polygon, and Avalanche. This chapter explores the functionalities and workings of each blockchain, including a detailed examination of concepts and strategies for connecting EVM-compatible chains and facilitating asset bridging across different networks. You will gain insights into the diverse ecosystem of EVM-compatible blockchains and learn about the mechanisms that enable interoperability and seamless asset transfers.

    Chapter 5, Deep Research and Latest Developments in Ethereum, delves into the ongoing research and advancements within the Ethereum platform. This chapter starts by looking at challenges and considerations in distributed systems in general and introduces schools of thoughts in scaling blockchain networks. It then discusses various phases of the Ethereum roadmap post the merge. This chapter explores cutting-edge scaling solutions such as Proto-Danksharding, Danksharding, Data Availability Sampling, Maximal Extractable Value (MEV), Proposer Builder Separation (PBS), providing insights to help you make sense of the rollup-centric Ethereum roadmap. Additionally, you will gain an understanding of key improvements in user experience, including the smart contract wallet and account abstraction. This chapter also delves into the concept of zkEVM and the current state of the zkEVM implementation. This chapter provides an outlook on the future of Decentralized Autonomous Organizations (DAOs), Web3, the metaverse, NFT platforms, and blockchain technology, offering you a glimpse into the exciting possibilities and developments that lie ahead.

    Chapter 6, Fundamentals of Solidity, provides a comprehensive exploration of Solidity, the leading programming language for smart contracts. You will gain a deep understanding of Solidity’s features and development tools. This chapter covers essential Solidity language fundamentals, including contract structure, patterns, and exception handling, along with smart contract security and best practices. This chapter also offers practical insights by showcasing a complete real-world smart contract developed in Solidity. You will learn how to functionally test your smart contracts and ensure their robustness. By the end of this chapter, you will have a solid foundation in Solidity and will be equipped with the knowledge and skills to build your own secure and functional smart contracts.

    Chapter 7, Web3 API Fundamentals, delves into the fundamental features of the Web3 API. This chapter provides an in-depth exploration of three key Web3 APIs: web3.js for Ethereum in JavaScript, web3.py for Ethereum in Python, and web3j for Ethereum DApp development in Java. Through practical examples, you will possess the knowledge and skills of how to leverage these APIs to interact with smart contracts deployed on the blockchain.

    Chapter 8, Developing Your Own Cryptocurrency, provides you with an overview of open-source smart contract libraries and delves into various ERC token standards. This chapter guides you through the process of creating your own cryptocurrencies using Solidity, starting with the ERC-20 token standard. You will learn how to develop a cryptocurrency called MyERC20Token based on the ERC-20 standard. Additionally, this chapter explores the creation of NFTs for a decentralized art marketplace, utilizing the ERC-721 standard to develop DigitalArtERC721Token. Furthermore, this chapter introduces another prominent NFT token standard, ERC1155, and provides insights into creating an ERC1155 NFT token. By the end of this chapter, you will have a comprehensive understanding of token standards, the setup of an Ethereum development environment, and the ability to create your own cryptocurrencies using various token standards.

    Chapter 9, Smart Contract Development and Test Fundamentals, focuses on providing you with practical insights into using development tools and conducting tests for smart contracts. This chapter begins by demonstrating the usage of Remix, a popular web-based IDE, for smart contract development and debugging. It further explores alternative options such as the Truffle suite and Hardhat as comprehensive development frameworks. You will also gain an understanding of smart contract unit testing by applying tests to the previously developed smart contract. This chapter emphasizes the importance of testing in ensuring the functionality and security of smart contracts. By engaging with these topics, you will strengthen your proficiency in Ethereum development and be well-equipped to develop, test, and secure your own smart contracts.

    Chapter 10, Writing a Frontend to Build the NFT Marketplace DApp, guides you through the process of creating a user interface (UI) for a decentralized digital art market DApp. This chapter introduces you the concept of DApps and their two-tier architecture, comprising a frontend UI layer and a smart contract backend on the blockchain. Having already learned about smart contract development and unit testing in the previous chapter, this chapter focuses on developing the UI component, which allows end users to interact with smart contracts. React, a widely used JavaScript framework, is employed for this purpose, along with the web3.js library, which facilitates communication with the Ethereum blockchain through its APIs. By following the chapter’s content, you will acquire the knowledge and skills needed to build the UI for a DApp using React and web3.js. You will gain an understanding of the essential steps involved in setting up the development environment, constructing frontend components, and ultimately running a fully functional decentralized digital art market DApp.

    Chapter 11, Ethereum Tools and Frameworks, provides you with an overview of the commonly-used tools and frameworks in Ethereum development. This chapter delves into the various tools and frameworks that are typically employed in Ethereum development, including those for smart contract compilation, deployment, and testing. Additionally, it explores frameworks that facilitate the development of decentralized applications.

    This chapter also covers storage options within the Ethereum ecosystem, including on-chain storage utilizing smart contracts, as well as off-chain storage using distributed file systems such as InterPlanetary File System (IPFS). You will gain an understanding of the available storage solutions and their applications. Furthermore, this chapter introduces popular smart contract libraries that offer pre-built functionalities and code templates. These libraries enable developers to streamline their development processes and enhance the security of their smart contracts.

    Chapter 12, Setting Up an Ethereum Private Chain, shifts our focus to private Ethereum networks. Private blockchains are primarily used by developers for testing purposes, offering advantages such as simplified testing without the need for node syncing or obtaining test ether. This chapter guides you through the process of setting up a private blockchain using Ethereum, highlighting the differences between public and private blockchains. Additionally, it explores the application of private blockchains in production use cases.

    Chapter 13, Deployment of Your DApps, focuses on the next step in the smart contract development cycle: deploying and testing contracts in an environment similar to the Ethereum main network. Testnets serve as platforms where developers can closely simulate the main network and test their contracts effectively. This chapter provides a comprehensive understanding of deploying smart contracts on popular testnets, namely the Goerli and Sepolia test networks. You will learn the step-by-step process of deploying your contracts to these testnets, enabling you to assess the functionality and behavior of your contracts in an environment that closely resembles the Ethereum main network. Furthermore, this chapter delves into monitoring smart contracts after deployment. You will gain insights into the tools and techniques used to monitor the performance and interactions of your deployed contracts. By following the content of this chapter, developers will be equipped with the knowledge and skills necessary to confidently deploy and monitor their smart contracts on testnets.

    Chapter 14, Build Ethereum Wallets, provides a comprehensive overview of Ethereum wallets and guides you on how to create your own wallets. This chapter delves deeper into the technology and functionality of Ethereum wallets. You will gain a solid understanding of Ethereum wallet concepts, including non-deterministic wallets and Hierarchical Deterministic (HD) wallets. This chapter explores the features of HD wallets, which offer enhanced security and convenience through the generation of a hierarchical tree of private keys. Additionally, you will explore advanced wallet features such as multiparty signatures, stealth addresses, and confidential transactions, which provide additional layers of privacy and security. This chapter also provides step-by-step guidance on creating an Ethereum wallet, empowering you to have full control over your wallet’s security and functionality. Furthermore, you will become familiar with popular third-party Ethereum wallets, broadening your knowledge of the available wallet options and their respective features. By the end of this chapter, you will have a comprehensive understanding of Ethereum wallets, the security features they offer, and the various options available for wallet creation and management.

    Chapter 15, Oracles, Cross-Chain, and Layer 2 in Practice, offers a comprehensive exploration of the fundamental concepts and technologies that enable interoperability and advanced functionalities within the decentralized Web 3.0 ecosystem. This chapter begins by providing you with a clear understanding of the crucial role played by oracles in retrieving and verifying external data, empowering smart contracts to respond and execute actions based on real-time information. Through practical examples, you will gain hands-on experience in integrating oracles into your smart contracts to access real-time token market prices.

    Continuing, this chapter delves into the intricacies of cross-chain bridge technology, which facilitates seamless communication and asset transfers between different blockchain networks. By comprehending the underlying mechanics of cross-chain bridges, you will grasp their significance in enabling the smooth interoperability of tokens and data across multiple chains. Furthermore, this chapter explores L2 technologies, which effectively tackle scalability challenges by providing off-chain solutions that enhance transaction throughput and minimize fees. You will be introduced to practical implementations of L2 technologies, comprehending your potential to significantly improve the overall efficiency and user experience of DApps.

    Upon concluding this chapter, you will possess a comprehensive understanding of oracles, cross-chain bridge technology, and L2 technologies. Equipped with this knowledge and the necessary tools, you will be empowered to leverage these technologies in your Ethereum projects, resulting in enhanced functionalities, seamless real-time data integration, and improved scalability.

    Chapter 16, Conclusion, is a summary of the entire book and the Ethereum blockchain technologies covered therein. It offers you a comprehensive understanding of Ethereum and its blockchain technologies. It addresses the challenges, explores the ecosystem, discusses the emerging trends in the blockchain and Ethereum ecosystem, and provides a glimpse into the future of Ethereum. It serves as a valuable resource for individuals seeking to grasp the fundamental concepts, opportunities, and advancements within the Ethereum blockchain space.

    To get the most out of this book

    Having a basic understanding of Ethereum frameworks, such as Remix and Truffle, will be beneficial for you. Additionally, familiarity with JavaScript is advantageous for comprehending the concepts presented in this book.

    If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

    Download the example code files

    You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Learn-Ethereum-Second-Edition. If there’s an update to the code, it will be updated in the GitHub repository.

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

    Conventions used

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

    Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles.

    Here is an example With web3.js installed, to instantiate web3, here is some typical JavaScript code.

    A block of code is set as follows:

    componentDidMount = async () => {

      const web3 = await getWeb3();

      const contractInstance = await getInstance(web3);

       ...

      this.setState({ contractInstance: contractInstance });

    }

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

    npm install -g create-react-app

    Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: Select cat-in-blockchain and click the PUBLISH button.

    Tips or important notes

    Appear like this.

    Get in touch

    Feedback from our readers is always welcome.

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

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

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

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

    Share Your Thoughts

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

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

    Download a free PDF copy of this book

    Thanks for purchasing this book!

    Do you like to read on the go but are unable to carry your print books everywhere?

    Is your eBook purchase not compatible with the device of your choice?

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

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

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

    Follow these simple steps to get the benefits:

    Scan the QR code or visit the link below

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

    Submit your proof of purchase

    That’s it! We’ll send your free PDF and other benefits to your email directly

    Part 1: Blockchain and Ethereum Basics

    In this Part, we will start with the concept of Blockchain and dive deep into Ethereum fundamentals, its architecture and its ecosystem. We will then introduce Decentralized Finance (DeFi) and analyze various DeFi protocols. We will discuss various Ethereum Virtual Machine (EVM) compatible Blockchains and cross-chain integration mechanisms. To keep you at the forefront of this evolving field, we will also bring you up to speed with the latest developments and most advanced research topics within the Ethereum community.

    This part comprises the following chapters:

    Chapter 1, Blockchain and Cryptocurrency

    Chapter 2, Ethereum Architecture and Ecosystem

    Chapter 3, Decentralized Finance

    Chapter 4, EVM-Compatible Blockchain Networks

    Chapter 5, Deep Research and the Latest Developments in Ethereum

    1

    Blockchain and Cryptocurrency

    It is a common belief that the bankruptcy filed by Lehman Brothers, a Wall Street banking giant, on September 15, 2008, triggered the global financial crisis in 2008-2009. Excessive risk exposure to the subprime mortgage and financial derivative markets by large banks almost brought down the global financial system. The crisis was the ultimate consequence of a fundamental breakdown of trust in the relationship between customers and the financial institutions that should have been serving them.

    Shortly after that, Satoshi Nakamoto, a mysterious and anonymous entity, published a whitepaper on October 31, 2008, called Bitcoin: A Peer-to-Peer Electronic Cash System, which is considered the origin of Bitcoin and all cryptocurrencies. Satoshi proposed a completely decentralized approach for Peer-to-Peer (P2P) payment without central banks or intermediaries. He outlined the principles and functions of what would be developed and introduced as Bitcoin in the following year.

    The central technology behind his invention is referred to as blockchain and has since evolved well beyond Bitcoin and digital payment. It is now a suite of technologies, forming the foundation of distributed ledgers and cryptocurrency. No one knows who or where Satoshi is, whether it is one individual or a group, but the whitepaper has profoundly changed money, digital and cryptocurrencies, business, and the world. You can learn more about the Bitcoin whitepaper authorship debate at https://www.judiciary.uk/wp-content/uploads/2022/08/Wright-v-McCormack-Judgment.pdf.

    The purpose of this book is to help you to understand blockchain technologies, introduce you to the tools and technologies of the Ethereum ecosystem, and get you started with developing smart contracts and end-to-end decentralized applications.

    In this chapter, we will start with basic concepts in Bitcoin, Ethereum, cryptocurrency, and blockchain.

    In this chapter, we will cover the following topics:

    Introducing blockchain technology

    Rehashing cryptography

    Anatomizing the blockchain consensus mechanism

    Understanding Bitcoin and cryptocurrency

    Overview blockchain use cases in the industry and government

    Ushering in the world of Ethereum

    Technical requirements

    All of the source code in this book can be found at the following GitHub link: https://github.com/PacktPublishing/Learn-Ethereum-Second-Edition/.

    Introducing blockchain technology

    You might have heard the parable of the blind men and the elephant. It is a folktale about six blind men’s individual descriptions of the same elephant based on their own perceptions from touching particular parts of the animal, each of them giving very different descriptions of what they think the creature looks like. It highlights the fact that different perspectives can lead to distinct viewpoints, emphasizing the limits of perception and the importance of a complete context.

    When Satoshi invented Bitcoin, the fundamental concept of its vision was to build a blockchain, a shared public ledger (longest Proof-of-Work (PoW) chain), that verifies and immutably records all transactions through a decentralized computer network (P2P network) and a consensus mechanism with computational proof. Satoshi thus came up with an elegant solution to the double-spend problem in digital money. A double-spend is an attack where someone tries to spend money in a transaction that isn’t actually available anymore as the money has already been spent.

    Blockchain is a new elephant in the digital world. To most of the public, blockchain is nothing but an obscure pseudonym for all cryptocurrencies, including Bitcoin, Ethereum, and more. So, what is blockchain? What does a blockchain look like? How does it work? Where can we use blockchain? Do you need a blockchain? There are many ways to describe a blockchain from different perspectives, but there is no universal definition of a blockchain.

    On the contrary, there are prevalent debates over the essential attributes or qualities of a blockchain. It is perceived as a new architecture using existing technologies, the next generation of the internet and web, a future database and distributed shared ledger, the new Napster (a P2P file-sharing system used in the 90s) with a pure decentralized P2P network, a cryptocurrency, a trustless secure transaction system, and so on. In reality, it is all of these. Only by combining all of these perspectives can we understand the whole picture of blockchain technologies and get a sense of the true potential of blockchain.

    The following picture illustrates different viewpoints of blockchain technology:

    Figure 1.1 – Different viewpoints on blockchain technologies

    Figure 1.1 – Different viewpoints on blockchain technologies

    So, what is a blockchain anyway? Think of blockchain as a new architecture paradigm and a new trust protocol. It is a computer science primitive forming the foundation of most cryptocurrencies and decentralized applications. It is a P2P transaction model that can enable two parties to transact in a way that is tamper-resistant and cryptographically proven. As the technology behind Bitcoin and other cryptocurrencies, blockchain is an open, distributed ledger that can be simultaneously used and shared within a large, decentralized, publicly accessible network.

    In essence, blockchain is a distributed shared ledger technology supported by three pillars, as shown in the following figure; these are P2P networks, cryptography, and a consensus mechanism:

    Figure 1.2 – Key components of blockchain

    Figure 1.2 – Key components of blockchain

    To understand how blockchain works, let’s start with the fundamental concepts and key building blocks of blockchain technologies. Then, we’ll discuss the key differences between centralized, distributed, and decentralized systems. We will then dive into the blockchain data structure and discuss how transactions, blocks, and chains are maintained and how the network reaches a consensus on the state of the chain, as well as how to secure the blockchain with cryptographic technologies.

    The following lists the key building blocks of blockchain technologies:

    Transactions: A transaction is a value transfer between two parties. It could be a transfer of money, tangible assets, or cryptocurrency. Transactions are broadcasted to the blockchain network. They are validated and verified by all nodes and collected into blocks. Once the block reaches a certain depth — in Bitcoin, this is six blocks — those transactions in the block can be considered irreversible.

    Block: All verified transaction records are collected into a data structure called a block. It has a header and body part, where the header contains a cryptographic hash of the previous block, a timestamp, and a Merkle tree root hash of all transactions in the block. The body is the container of transaction data. A Merkle tree is like the digital fingerprint of transactions in the block, which we will discuss extensively later in this section.

    The chain of block (blockchain): A blockchain is a linked list of a chain of blocks. Blocks are linked together using a cryptographic hash as the pointer to the previous block.

    Decentralized P2P network: It is a P2P network in which interconnected nodes share resources between themselves without the use of a central authority or some sort of intermediary.

    Consensus protocol: The consensus protocol in blockchain is a set of rules that all network nodes will enforce when considering the validity of a block and its transactions. The consensus mechanism is the process used by the network nodes to achieve agreement on the network state. It is a fault-tolerant mechanism to ensure the reliability and integrity of the network.

    Mining: Mining is the process by which network nodes in blockchain systems add new blocks to the blockchain and get rewarded with crypto-incentives.

    In the next section, we will discuss how P2P networks work.

    Decentralized P2P networks

    To explain how blockchain works, let’s look at the steps involved with the existing business model for completing a simple payment transaction.

    A customer, Alice, needs to pay $10 to Bob, who is in a geographically distant region from Alice and happens to have an account in the same bank as Alice. She can make the payment either by visiting a bank branch or using the web. Let’s say she tries to do it online through the bank’s web portal. She will need to authenticate herself using her username and password and then put the transfer order in and wait for the bank system to confirm whether the transaction is completed.

    As shown in the following diagram, in order to support such online banking activities in the traditional world, the bank has to establish an identity, access management system, and authenticate Alice’s login credentials. Behind the scenes, the bank needs to develop a bank web portal and a backend system to verify whether Alice has the right account with the bank and has enough money to pay Bob, upon which the bank can transfer $10 out of Alice’s account and put $10 in Bob’s account. The bank has to maintain a ledger to record the details of the transaction in a database and show the balance each person has.

    The following diagram shows a centralized bank system model:

    Figure 1.3 – Centralized bank system model

    Figure 1.3 – Centralized bank system model

    As the business grows, customers needs change with it. The traditional brick-and-mortar business model is being replaced by the digital banking and commerce model. This requires technological changes in the bank system too. Banks nowadays deploy a distributed system model to serve the ever-growing needs of their customers.

    The following diagram shows the distributed bank system model:

    Figure 1.4 – Distributed bank system model

    Figure 1.4 – Distributed bank system model

    The fundamental issue with the preceding centralized or distributed system model is the risk from single points of failure. Failure could come from malicious network attacks, system failures, or security and privacy breaches; it could come from business failures in the bank itself, which can cause millions of people to lose their homes due to the bankruptcy of big banks during a global financial crisis. It could happen due currency failure, such as the currency collapse in Venezuela, where the lifetime savings of average citizens suddenly became worthless overnight. Also, payments can be blocked due to government censorship.

    Satoshi Nakamoto believed that the root problem with the traditional fiat system is all the trust required to make it work. Citizens have to trust the central bank not to devalue the currency. Consumers have to trust the bank to manage their money. But history has shown again and again that this trust is often breached.

    Satoshi designed an elegant decentralized P2P electronic cash system, and the technology behind that, blockchain, is the solution, where transactions are maintained in a distributed shared ledger and replicated across a global P2P network. Security and privacy are ensured with cryptographic technologies, and transaction integrity is achieved through a consensus mechanism.

    The following diagram shows a decentralized bank system model:

    Figure 1.5 – Decentralized bank system model

    Figure 1.5 – Decentralized bank system model

    As new transactions are made, they are broadcasted to all network nodes, and over time all transactions that have occurred are sequenced together in the public ledger and made available on all replicated network nodes, as shown in the following diagram:

    Figure 1.6 – Decentralized public ledger

    Figure 1.6 – Decentralized public ledger

    Now that we understand the difference between centralized and decentralized models, let’s see how blockchain works.

    How does blockchain work?

    Using the previous example, as shown in the following diagram, let’s assume Alice wants to buy something from Bob and she agrees to pay Bob 10 bitcoins (BTC):

    Figure 1.7 – Money transfer between two parties

    Figure 1.7 – Money transfer between two parties

    Let’s walk through the high-level processes step by step to demonstrate how blockchain works:

    Create blockchain transactions: A transaction is a value transfer between two parties. When Alice sends 10 BTC to Bob, it will create a transaction with one or more inputs and two or more outputs, where the inputs reflect Alice’s account, and the outputs reflect which account(s) Alice intends to transfer to. The transaction is then digitally signed with Alice’s private key and broadcasted to the P2P network. The receiver will use the digital signature to verify the ownership of Alice’s funds. We will discuss digital signatures and cryptographic hash functions in detail in later sections.

    Validate the transactions and add to the transaction pool: Once the transaction is submitted to the blockchain network, the bookkeeper node (usually a full node in a P2P network that receives the transactions) will validate it according to protocol rules defined by the blockchain network. If the transaction is valid, the bookkeeper will add it to the transaction pool and relay the transaction to the peers in the network.

    Create the candidate blocks: Transactions in the transaction pool are collected into the block periodically. In a Bitcoin network, every 10 minutes, a subset of network nodes, called mining nodes or miners, will collect all valid transactions from the transaction pool and create the candidate blocks. The following diagram shows the structure of a candidate block:

    Figure 1.8 – Creation of candidate blocks

    Figure 1.8 – Creation of candidate blocks

    As illustrated in the preceding diagram, the high-level processes are as follows:

    The candidate block packages the recent valid transactions into the block structure based on block specifications.

    For each transaction in the package, it creates a cryptographic hash of the transaction data, recursively calculates the hash out of existing hashes, and creates a Merkle root of all transactions, as depicted in the following diagram:

    Figure 1.9 – Merkle tree

    Figure 1.9 – Merkle tree

    The miner node looks for the latest block on the blockchain and adds its hash to the block header of the candidate block as the reference from the candidate block it intends to link to.

    Mine the new block: Once the candidate block is created, the race starts for the chance to add new blocks and win the rewards. The process for such a race is called mining. The winning of the race is determined by the consensus mechanism. We will discuss different consensus mechanisms in later sections. In blockchain systems such as Bitcoin or Ethereum 1.0, the PoW consensus mechanism is applied to mining. Miners will keep trying to find a random number, the nonce in the block header structure, until the hash meets certain challenging conditions. For example, one such challenging condition is, the resulting block hash is smaller than a target number, or in some cases, the hash has a few leading zeros. In practice, every random number has the same chance to win the race, so practically, you can just start a loop through from 1 to 232 until it finds such a nonce, the unique hash meeting the condition. It requires huge CPU hashing power to find such a nonce. The challenging condition, called difficulty, can be adjusted based on the target number or bits in the block header structure. The difficulty in winning the race grows exponentially the smaller the target number is or the fewer bits are in the block header structure.

    Add a new block to the blockchain: The first winning node will announce the new block to the rest of the network for verification. Once the block is verified and approved by the majority of the network miners, it will be accepted and becomes the new head of the chain. Since all blocks are chained together by linking the hash to the previous block, any tampering with the ledger becomes impossible since it will require PoW on all previous transitions.

    All miners have the chance to solve the puzzle, but only the winning miner has the authority to add the block to the chain and claim the bounty. Once the new block is added to the blockchain, all in-progress miners will stop their mining efforts on the newly added block and start the race again on a new block.

    The following diagram summarizes the step-by-step process when new transactions are submitted to the blockchain network:

    Figure 1.10 – How blockchain works in one picture

    Figure 1.10 – How blockchain works in one picture

    Cryptography plays a critical role in maintaining the transaction state in the blockchain and ensuring immutability. Cryptography is not new. In the next section, we will go over some key concepts in cryptography.

    Rehashing cryptography

    Cryptography is the study of secure communication techniques that prevent third parties or the public from reading private messages and allow only the intended recipient of a message to view its contents. It is the cornerstone of information security, which serves as the basis for delivering secure business applications and services. Modern cryptography concerns itself with the following five objectives of information security:

    Confidentiality: This is the concept of preventing sensitive data from being accessible by any unauthorized entities

    Integrity: This means protecting sensitive data from unauthorized changes during transit from one party to another party

    Authentication: This is the process of ensuring that user identity is truly what the user claims it to be, whether the user is human or a system

    Authorization: This is the concept of determining what actions an authenticated user is allowed to perform

    Non-repudiation: When a user performs an action on data, the action must be bound with the user so that it can’t deny performing such actions

    Cryptography deals with the design of algorithms for encryption and decryption, which are intended to ensure the secrecy and authenticity of the messages or transactions in question. Let’s start with some key elements in modern cryptography:

    Encryption: This is the process of converting plain text or data into an unintelligible form, typically using a mathematical algorithm.

    Decryption: This is the process of reversing encryption, converting an encrypted message back into its original text and data.

    Hash: This is the process of converting any data block (arbitrary size or message) into a fixed-length hash code. A cryptographic hash function is a deterministic mathematical function performing such a conversion using cryptography, and it always maps to the same result for a given data block.

    Cryptography is the linchpin and one of the three pillars of blockchain technology, along with the consensus mechanism and P2P network. It is used in many different forms, including, for example, wallets (for proof of cryptocurrency ownership), transactions (for PoW consensus), and P2P communication. In the following subsections, we will go over key blockchain-related cryptography topics, including public-key cryptography, digital signatures, cryptographic hashing, and Merkle trees.

    Public-key cryptography

    Public-key cryptography is a form of cryptographic function in which encryption and decryption are performed using two different keys — one public and one private key. They are generated in pairs. It is also called asymmetric cryptography. The public key can be shared with the public, but the private key is meant to be a secret code only known by its owner.

    The keys are used in tandem too. Either of the two keys can be used in encryption, with the other one used for decryption. It is computationally improbable to determine the private key given only knowledge of the cryptographic algorithm and the public key.

    Public-key cryptography is mostly used to do the following three things:

    Secure the message transmission between two parties and ensure the confidentiality of messages or data

    Authenticate the sender and ensure the message is indeed sent from the sender

    Combine it with the cryptographic hashing function and provide a digital signature on a document before sending it to the receiver

    We will go over the first two here and discuss digital signatures in the following section:

    Public-key cryptography for confidentiality: In this case, as depicted in the following diagram, the receiver’s keys are used to encrypt messages between two parties during transmission. The sender (Alice) uses the receiver’s public key to encrypt a message, and the receiver (Bob), who holds their own private key in secrecy, can decrypt the messages using their private key:

    Figure 1.11 – Confidentiality with public key

    Figure 1.11 – Confidentiality with public key

    Public-key cryptography for authentication: In this case, as shown in the following diagram, the sender’s keys are used to authenticate the sender’s message. The sender uses its own private key to encrypt a message before sending it to the intended parties. The receiver can use the sender’s public key to confirm the message’s authenticity and decrypt it. The combination of this approach with the message’s cryptographic hashing function provides a digital signature, which we will discuss in the next section:

    Figure 1.12 – Authentication with public key

    Figure 1.12 – Authentication with public key

    Public-key cryptography is an essential technology underpinning wallets and transactions in the blockchain. We will discuss the Bitcoin wallet in the Understanding Bitcoin and cryptocurrency section.

    Cryptographic hash function

    A cryptographic hash function is an algorithm used to randomly convert a string of binary data into a condensed representation of a message — a message digest. Its output is called a hash value, digital fingerprint, digest, or checksum. It is deterministic and always results in the same hash value for a given message. It is capable of taking any size of data block and producing a fixed-size hash value that uniquely identifies the original data block. It is a one-way, irreversible function; the only way to recreate the input data is to try a brute-force approach with all possible values to see whether there is a match, which is almost computationally infeasible.

    Notable hash functions include MD5, SHA-1, SHA-2 and SHA-3. Although they are still widely in use, MD5 and SHA-1 are cryptographically broken due to collision attacks found in the algorithm, and are thus no longer recommended.

    Cryptographic functions have been widely used in blockchain technology, including the following:

    Merkle trees: As we showed earlier, when a miner node pulls transactions from the transaction pool, it packages them in a block, where the block header has a field referencing the Merkle root of all transactions.

    Block chaining: Blocks in the blockchain are chained together with a reference to the previous block using a cryptographic hash.

    PoW: The PoW consensus algorithm itself is a game in solving a cryptographic hash function. We will discuss it in more detail in the Understanding the blockchain consensus mechanism section.

    In addition to cryptographic hash functions, digital signatures have been broadly leveraged in blockchain networks too. We will discuss their usage in the next subsection.

    Digital signatures

    A digital signature is a set of algorithms for determining the authenticity and integrity of digital messages or documents. It assures the recipient that the message was indeed created by the expected sender and that the message was not altered during transmission. The sender cannot deny having sent the message.

    When Alice sends a document to Bob, she will follow certain steps to digitally sign the document, as shown in the following diagram:

    Figure 1.13 – Digital signature

    Figure 1.13 – Digital signature

    The steps to digitally sign the document are as follows:

    Calculate the message digest of the document Alice wants to send to Bob with a cryptographic hash function, usually any SHA-2 or SHA-3 algorithm.

    Encrypt the message digest with Alice’s private key, append the encrypted message digest to the original document, and send the combined message out.

    Once Bob receives the combined message from Alice, he will separate the encrypted message digest from the document itself. Bob will use Alice’s public key to decrypt the encrypted message digest.

    At the same time, Bob will calculate the message digest of the received document and compare the resulting message digest with the decrypted message digest to see whether there is a match. If yes, Bob is assured that the document originated from Alice without any tampering.

    In blockchain, a digital signature is a way to prove ownership of the underlying cryptocurrency or electronic coin. When Alice needs to pay Bob 10 BTC, she will digitally sign a hash of the previous transaction, which can prove that Alice has ownership of the 10 BTC.

    In summary, cryptography is one of three foundational pillars in blockchain technology. Public-key cryptography is the basis for blockchain wallets and transactions, and the cryptographic hash

    Enjoying the preview?
    Page 1 of 1