Learn Ethereum - Xun (Brian) Wu - E-Book

Learn Ethereum E-Book

Xun (Brian) Wu

0,0
51,59 €

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

Mehr erfahren.
Beschreibung

Ethereum is a blockchain-based, decentralized computing platform that allows running smart contracts. This book provides a basic overview of how Ethereum works, its ecosystem, mining process, and the consensus mechanism. It also demonstrates a step-by-step approach for building decentralized applications.
This book begins with the very basics of Blockchain technology. Then it dives deep into the Ethereum architecture, framework and tools in its ecosystem. It also provides you an overview of ongoing research on Ethereum, for example, Layer 1 and 2 scaling solution, Stablecoin, ICO/STO/IEO, etc. Next, it explains Solidity language in detail, and provides step-by-step instructions for designing, developing, testing, deploying, and monitoring decentralized applications.
In addition, you’ll learn how to use Truffle, Remix, Infura, Metamask, and many other Ethereum technologies. It’ll also help you develop your own cryptocurrency by creating ERC20, and ERC721 smart contracts from scratch. Finally, we explain private blockchains, and you learn how to interact with smart contracts through wallets.

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

EPUB

Seitenzahl: 502

Veröffentlichungsjahr: 2019

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



Learn Ethereum
Build your own decentralized applications with Ethereum and smart contracts
Xun (Brian) Wu
Zhihong Zou
Dongying Song
BIRMINGHAM - MUMBAI

Learn Ethereum

Copyright © 2019 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.

Commissioning Editor:Pravin DhandreAcquisition Editor:Joshua NadarContent Development Editor:Nazia ShaikhSenior Editor: Ayaan HodaTechnical Editor:Joseph SunilCopy Editor:Safis EditingProject Coordinator:Kirti PisatProofreader: Safis EditingIndexer:Manju ArasanProduction Designer:Joshua Misquitta

First published: September 2019

Production reference: 1200919

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78995-411-1

www.packt.com

Packt.com

Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Fully searchable for easy access to vital information

Copy and paste, print, and bookmark content

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

Contributors

About the authors

Xun (Brian) Wu has over 17 years of extensive hands-on experience in design and development with blockchain, big data, the cloud, UIs, and system infrastructure. Brian is a co-author of BlockchainQuick Start Guide, Hyperledger Cookbook, Security Tokens and Stablecoins Quick Start Guide, Blockchain By Example, and Seven NoSQL Databases in a Week, and is a technical reviewer for more than 50 books for Packt. He owns several patents in the blockchain area. He holds a master's degree in computer science from NJIT. He lives in New Jersey with his two beautiful daughters, Bridget and Charlotte.

I would like to thank my parents, wife, and children for their patience and support throughout this endeavor.

Zhihong Zou has more than 20 years of software architecture, design, and development experience in the telecommunication and healthcare industries. As a seasoned enterprise and solution architect, and a thought leader in blockchain, business process management, big data, AI, and machine learning, he has leveraged emerging technologies to solve complex real-world business problems. He holds a master's degree in computational mathematics and a master's degree in computer science. He has published several papers on numerical computation in renowned academic journals.

I would like to thank my wife Yufang and my son Kevin for their understanding, encouragement, support, and patience throughout this journey. I appreciate all the sacrifices you made so that I could complete the book.

Dongying Song has more than four years of extensive hands-on experience in blockchain, Ethereum, big data, machine learning, and data science. As an experienced software engineer and data scientist, she has worked for a top-tier bank and pharmaceutical companies. Dongying holds a master's degree in statistics from Columbia University and majored in mathematics during her undergraduate years. Her recent activities have focused on blockchain, Ethereum, and smart contract developments.

About the reviewers

Voith Mascarenhas is a software engineer by profession who is skilled in the Python programming language. He discovered Ethereum in early 2017 through his colleagues. Fascinated by the complexity of Ethereum and its promise for decentralized governance, he has made it his new hobby project. He has been contributing to Ethereum's Python ecosystem ever since discovering it and can also be found helping beginners in public chat groups. He recommends this book to any software engineer planning to dive deep into Ethereum.

I would like to thank Christoph Burgdorf for recommending to Packt that I review this book. I would also like to thank Packt for giving me this wonderful opportunity. Not only was it a pleasure reading this book, but it was also a great learning opportunity.

Christoph Burgdorf works as a software engineer at Ethereum. He is part of a team that develops and maintains many different projects of the Python Ethereum ecosystem. His main focus is on the Trinity client, a Python node for Ethereum 1 and 2. Before joining Ethereum, Christoph spent several years traveling the world as the co-founder of thoughtram to perform training on Angular, a web framework by Google to which he also contributed. When he's not writing code, Christoph likes spending time with his family, going camping, cycling, and traveling the world.

Sumit Chauhan is a speaker, advisor, and a CEO with more than 19 years of experience in delivering enterprise solutions focused on global growth and digital transformations with emerging technologies such as blockchain and AI. He has worked in various technical roles for different technology companies across the globe. He is currently a co-founder of Datopic Technologies, which works on building decentralized white label products with big data, machine learning, and blockchain technologies for cybersecurity, Internet of Things, and FinTech companies.

NikolaTchouparovis a co-founder and CEO at Moneyfold Ltd in the UK, which is the world's first regulation-compliant stablecoin provider. He has been engaged in the blockchain and cryptocurrency space since 2013. In his previous career, Nikola implemented trading and risk management systems at banks around the world.

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Title Page

Copyright and Credits

Learn Ethereum

About Packt

Why subscribe?

Contributors

About the authors

About the reviewers

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Conventions used

Get in touch

Reviews

Section 1: Blockchain and Ethereum Basics

Blockchain and Cryptocurrency

Technical Requirements

Introducing blockchain technology

Decentralized P2P network

How does blockchain work?

Rehashing cryptography

Public key cryptography

Cryptographic hash function

Digital signature

Anatomizing a blockchain consensus mechanism

What is consensus?

Proof-of-work

How PoW works

Targets and difficulty

Incentives and rewards

Double-spend issues

Advantages and disadvantages

Proof-of-stake

Forking

Hard fork

Soft fork

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?

Bitcoin transaction and block structure

Transaction validation and block verification

Limitations in Bitcoin

Altcoins

Ushering in the world of Ethereum

Smart contract

Ethereum Virtual Machine

Account

Summary

Ethereum Architecture and Ecosystem

Technical Requirements

Introducing Ethereum architecture

Ethereum – the world computer

The world of decentralization

Diving deep into Ethereum

Account

Two types of accounts

Externally owned account

Contract account

Transactions and messages

Smart contract

Ether and gas

The Ethereum Virtual Machine

Address and wallet

Addresses in Ethereum

Ethereum wallet

Wallet tools

Understanding mining in Ethereum

Mining and consensus protocol

Ethereum transaction and block structure

Transaction validation and block verification

Shift to proof-of-stake

Working with tools and technologies in Ethereum ecosystem

Ethereum client

The Web3 Server API

DApp development tools

Summary

Deep Research on Ethereum

Technical Requirements

Understanding challenges in distributed systems

The CAP theorem

Horizontal scaling versus vertical scaling

Scaling Ethereum

Blockchain scalability trilemma

Ethereum scaling solutions

Block size

Altcoins

On-chain solutions

Off-chain solutions

ZK-SNARK

State channel with Raiden

Ethereum side chain with Plasma

Ethereum sharding and Casper

PoS consensus in Ethereum

Following up on EVM and lower-level protocol improvements

Implementing Ethereum 2.0 roadmap

Introducing cryptoassets and Ethereum token standards

Initial coin offerings

STO and IEO

Ethereum token standards

Fungible and non-fungible tokens

ERC-20

ERC-721

Stablecoin

Dai stablecoin

Making sense of cryptoeconomics

Working with decentralized data and content storage

Swarm

IPFS

BigchainDB

Decentralized messaging with Whisper

Summary

Section 2: Blockchain Development Cycle

Solidity Fundamentals

Technical Requirements

Introducing Solidity

Tools for the Solidity development environment

Browser-based IDEs

Standalone IDE with Solidity plugins

Command-line development management tools

Learning the fundamental programming structure in Solidity

The layout of a Solidity source file

Pragma

Comments

Import

Structure of a contract

State variables

Built-in data types

User-defined data type

Functions

Input parameters

Output parameters

Access modifiers

The pure, view, and payable functions

Constructor and destructor functions

Fallback function

Function overloading

Function modifiers

Events

Global variables, contextual variables, and functions

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

Keep contracts simple and modular

Use the checks-effects-interactions pattern

DoS with block gas limit

Handle errors in external calls

Best practices in smart contracts

Access restriction

State machine

Writing upgradable smart contracts

Contract proxy and delegate

Eternal storage

Economic consideration of smart contract execution

Putting it all together – rental property leasing

Summary

Developing Your Own Cryptocurrency

Technical Requirements

Understanding token standards

Setting up an Ethereum development environment

Working with Truffle

Creating an ERC-20 token

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 ERC 721 token – the DigitalArt token

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()

Summary

Section 3: Ethereum Implementations

Smart Contract Development and Test Fundamentals

Technical Requirements

Understanding Remix development fundamentals

Working with the Solidity compiler

Analysis

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

Security testing

Static and dynamic analysis

MythX

Securify

Working with a linter

Working with solhint

Summary

Writing UI for the DApps

Technical requirements

Knowing about DApps

Working with 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

The ART GALLERY page

The PUBLISH YOUR ARTS page

The MY WALLET INFO page

Running the decentralized digital art market DApp

Publishing your art (the first account)

My wallet info (the first account)

Publishing your art (the second account)

My wallet info (the second account)

Buying art (the second account)

Reselling art

Summary

Ethereum Tools and Frameworks

Technical requirements

Understanding the Ethereum development tools and frameworks

Working with Infura

Working with the Infura Ethereum API

Using Remix with Infura

The Ethereum client API

Working with Web3j

Ethereum storage

Knowing the IPFS protocol

Installing IPFS

Starting an IPFS node

Running an IPFS example

Publishing a simple page in the command line

Publishing and querying IPFS via Infura

Working with Swarm

Installing Swarm

Installing Geth

Running an example of Swarm

Ethereum messages – Whisper

Whisper protocol

Whisper envelopes

Whisper message

Whisper example

Popular smart contract libraries

Working with OpenZeppelin

Setting up a dev environment

Access control

Math

Token

Utils

Summary

Section 4: Production and Deployment

Creating an Ethereum Private Chain

Technical requirements

Understanding a private and permissioned blockchain

Setting up a local private Ethereum blockchain

Private blockchains without mining

Setting up the environment

Configuring the custom genesis file

Running Geth

Private blockchains with mining

Setting up an environment

Configuring the custom genesis file

Running Geth

Creating a new account

Mining on a local private network

Using optional flags with new chains

Commands

Ethereum options

Developer chain options

API and console options

Networking options

Transaction pool options

Introducing the popular private blockchains in the industry

Hyperledger

Hyperledger Fabric

Hyperledger Sawtooth

Corda

Quorum

Private blockchains use cases

Financial services

Payments

Audit and assurance

Healthcare

Medical records

Medical costs

Drugs

Food supply chain

Summary

Deployment of Your Smart Contract

Technical requirements

Deploying smart contracts with testnet

Deploying a smart contract to the Goerli testnet with MyCrypto

Creating a MetaMask account

Getting test ether

Deploying the smart contract

Viewing deployed smart contracts on Etherscan

Interacting with the deployed smart contract

Deploying a smart contract to the Ropsten testnet with MyEtherWallet

Accessing or creating a new wallet on MyEtherWallet

Getting test ether

Deploying the smart contract

Monitoring smart contracts

Ethereum developer APIs

Further reading on uncle nodes

Summary

Building Ethereum Wallets

Technical requirements

Understanding the wallet technology

Understanding non-deterministic and deterministic wallets

Mnemonic code words (BIP-39)

Stretching mnemonic code words to the seed of the deterministic wallet

HD wallets (BIP-32)

Deriving a tree of keypairs

Deriving wallet structure

HD wallet path

Multipurpose HD wallets (BIP-43)

Multi-currency and multi-account wallets (BIP-44)

Generating a private key in Ethereum

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 a non-deterministic wallet with MyEtherWallet

Creating an HD wallet

Working with third-party Ethereum wallets

Wallets on different platforms

Multi-signature wallets

Smart contract wallets

Hot and cold wallets

Transferring funds between wallets

Summary

Section 5: Conclusion

Conclusion

Technical requirements

Facing the challenges of Ethereum and blockchain

Consensus protocol and scalability

UI/UX, usability, and design thinking

Ethereum governance

Government regulations

Mainstream adoption

A few more words about privacy

Glancing over the Ethereum ecosystem

Tools and infrastructure

Decentralized applications

AML and KYC

Emerging technology fusion – blockchain, AI/ML, and IoT

Internet of blockchains

Blockchain meets AI and ML

Smart things on a decentralized network

Meeting the future of Ethereum

Summary

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

Ethereum is a blockchain and decentralized computing platform that allows the execution of smart contracts.

This book provides a basic overview of the Ethereum ecosystem, the concept behind Ethereum, and the mechanism of Ethereum, and demonstrates a step-by-step approach in building decentralized applications. It offers a quick guide, allows a reader who does not have in-depth or systematic knowledge on the topic to master the following skills.

This book begins with the very basics of Ethereum, blockchain, the Solidity language, and cryptocurrency. The book will simplify cryptography for you and then demonstrate how it can be used to secure ether, cryptographic data structures, advanced encryption standard (AES), hashes, private keys, and more. The book will then demonstrate how mining works and how miners make their profits. After this, the book will cover concepts such as proof of work, proof of stake, and smart contracts. The book will cover smart contracts in details, including concepts such as the Solidity programming language, token standards, and more. The book will cover how to build UIs for applications in Node.js and web3.js, explore Ethereum wallets, and look at building real-time scalable Ethereum applications. In addition, the book covers use cases highlighting the potential applications of Ethereum across sectors such as banking, financial services, healthcare, insurance, and real estate. By reviewing various Ethereum tools and frameworks with examples, readers will learn Truffle, Remix, MetaMask, IPFS, SWARM, Infura, Whisper, OpenZeppelin, and many other Ethereum technologies.

By the end of this book, you will learn about Ethereum decentralized application development, testing, deployment, developer tools, frameworks, more advanced blockchain concepts.

Who this book is for

This book appeals to all those developers who want to go beyond the theory and possibilities of cryptocurrencies, blockchain, and Ethereum. Those who wish to build secure, transactional, and third-party independent applications will benefit from the practical examples in the book.

What this book covers

Chapter 1, Blockchain and Cryptocurrency, covers the basics of blockchain, discusses how blockchain is disrupting existing technology, how cryptocurrency came into the picture, and how the Ethereum consensus mechanism works. We will also learn how cryptography makes the Ethereum blockchain secure. By the end of the chapter, you should understand Ethereum accounts, forks, and the concept of mining.

Chapter 2, Ethereum Architecture and Ecosystem, describes the architecture of Ethereum and helps you to understand the Ethereum Virtual Machine (EVM), Gas, accounts, and more. It also covers the basic concepts of Ether mining. We look at initial coin offerings (ICOs) and the Ethereum token economy. We will also explore various tools and technologies in the Ethereum ecosystem.

Chapter 3, Deep Research on Ethereum, teaches you about ongoing research on the Ethereum platform in the field of ICOs, sharding EVM improvements, economics, low-level protocol improvements, and decentralized storage.

Chapter 4, Solidity Fundamentals, deals with the basic features of the Solidity programing language. We cover Solidity's development tools and learn about various Solidity language fundamentals, including the structure of contracts, contract patterns, and exception handling. We cover smart contract security and best practices.

Chapter 5, Developing Your Own Cryptocurrency, teaches you how to write your own cryptocurrency using smart contracts. We look at smart contract open source libraries and review the ERC 20 token standard. Finally, we develop our first cryptocurrency token.

Chapter 6, Smart Contract Development and Test Fundamentals, shows you how to use Remix to develop and debug smart contracts. We explore various options for the Truffle suite tool. We cover smart contract unit tests by using these development tools.

Chapter 7, Writing UIs for DApps, shows you various methods to deploy smart contracts. There will be examples to demonstrate everything. We look at the web3.js API and write client-side Node.js code to call smart contracts.

Chapter 8, Ethereum Tools and Frameworks, covers the tracking of data inside smart contracts.

Chapter 9, Creating an Ethereum Private Chain, explains the difference between public and private blockchains. It covers how to set up private blockchains on Ethereum, including the options flags we can use with new chains. It also presents private blockchains in production usages.

Chapter 10, Deployment of Your Smart Contract, shows you how to deploy smart contracts to various blockchain environments.

Chapter 11, Building Ethereum Wallets, looks at the concepts of Ethereum wallets. Then, we create a hierarchical deterministic (HD) wallet. We also discuss popular third-party Ethereum wallets.

Chapter 12, Conclusion, is a summary of the entire book and the Ethereum blockchain technologies covered therein.

To get the most out of this book

You need to have some basic knowledge of Ethereum frameworks such as Truffle. Knowledge of JavaScript will also be useful.

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packt.com

.

Select the

Support

tab.

Click on

Code Downloads

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Learn-Ethereum. In case there's an update to the code, it will be updated on the existing GitHub repository.

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

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: http://www.packtpub.com/sites/default/files/downloads/9781789954111_ColorImages.pdf.

Get in touch

Feedback from our readers is always welcome.

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

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

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.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.

Section 1: Blockchain and Ethereum Basics

Since its inception, blockchain has fundamentally changed how people can transfer values such as ether and establish trust between parties without any intermediary. In this section, we will learn all about blockchain. We will also learn about the architecture and ecosystem that Ethereum functions on.

This section comprises of the following chapters:

Chapter 1

,

Blockchain and Cryptocurrency

Chapter 2

,

Ethereum Architecture and Ecosystem

Chapter 3

,

Deep Research on Ethereum

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 in subprime mortgage and financial derivatives by large banks almost brought down global financial systems. 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 technologies behind his invention are called blockchain and have 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 what Satoshi is, if it is one individual or a group, but its paper is profoundly changing money, digital- and cryptocurrencies, business, and the world.

The purpose of this book is to help you to understand blockchain technologies, introduce you to the tools and technologies in the Ethereum ecosystem, and get you started with developing smart contracts and end-to-end decentralized applications. 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

Ushering in the world of Ethereum

Technical Requirements

For all the source code of this book, please refer the following GitHub link: github.com/Packt-Publishing/Learn-Ethereum.

Introducing blockchain technology

You might have heard the parable of the blind men and an elephant. It is a folk tale about each of six blind men's individual descriptions of the same elephant based on their own touch and feel of the animal. It highlights the fact that different perspectives may lead to distinct viewpoints, emphasizing the limits of perception and the importance of a complete context.

When Satoshi invented Bitcoin, the fundamental concept in its vision was to build a blockchain, a shared public ledger (longest proof-of-work (PoW) chain), that verifies and records immutably all transactions through a decentralized computer network (P2P network) and aconsensus mechanismwith computational proof. Satoshi came up with an elegant solution solving thedouble-spendproblem of electronic monies. A double-spend is an attack when someone tries to spend money through a transaction that isn't actually available anymore as the money was already spent before.

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? Although there are many ways to describe a blockchain, mainly from different perspectives, 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 with 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, or a trustless secure transaction system, and so on. It is all of them. Only by combining all of them can we understand the whole picture of blockchain technologies and get a sense of the true potential of blockchain.

The following diagram illustrates different viewpoints of blockchain technology:

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 screenshot; these are P2P networks, cryptography, and a consensus mechanism:

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.

Following is a list of 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 6 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.

The chain of block (blockchain)

: A blockchain is a linked list of a chain of blocks.

Blocks are linked together using a cryptography hash as the pointer to the previous block.

Decentralized P2P network

: It is a P2P network in which interconnected nodes share resources amongst each other 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.

Decentralized P2P network

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

A customer, Alice, needs to pay $10 to Bob, who 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 that 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 and 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 to 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:

As the business grows, customers' needs change too. The traditional brick and mortar business model is being replaced by the digital banking and commerce model. This requires technology 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:

The fundamental issue with the preceding centralized or distributed system model is a single point 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 to currency failure itself, such as the currency collapse in Venezuela, where the lifetime savings of average citizens suddenly became worthless overnight. Also, the payment may be blocked due to government censorship.

Satoshi Nakamoto believed that the root problem with traditional fiat currency 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 trust is often breached.

Satoshidesigned 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:

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 in all replicated network nodes, as shown in the following diagram:

Now that we understand the between centralized and decentralized models, let's 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 or 10 Bitcoins (BTC):

Let's walk through the high-level processes step-by-step, demonstrating how the blockchain works:

Create blockchain transactions:

A transaction is a value transfer between two parties

. When Alice sends

$

10 or 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 the business and technical 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:

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

The candidate block

packages 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:

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, 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 2

32

until it finds such a nonce. 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 expon

entially

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 tip 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 chains 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:

Now you know how works. 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 the authenticity of the messages or transactions. Let's start with some key elements in modern cryptography:

Encryption

: This is the process of converting plain text or data into an unintelligent 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 a key cornerstone of blockchain technology, along with the consensus mechanism and decentralization. It is used in many different forms, including, for example, wallets (for proof of cryptocurrency ownership), transactions (for PoW consensus), and P2P communication. In this section, 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 calledasymmetric 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 most used in three ways, to:

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 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 digital signature is used to encipher 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:

Public key cryptography for authentication:

In this case, as shown in the following diagram, the signature is 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:

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.

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.

Digital signature

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:

These steps are as follows:

Calculate the message digest of the document Alice wants to send to Bob with a cryptographic hash function, usually MD5 or any SHA 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 function is a key element underpinning the PoW consensus mechanism. A digital signature is used as proof of ownership of the underline electronic coins or cryptocurrency.

In the next section, we will introduce and look at a blockchain consensus mechanism in detail and discuss how cryptography technologies are leveraged to reach consensus among decentralized parties.

Anatomizing a blockchain consensus mechanism

A fundamental problem in large-scale distributed systems is how to achieve overall system reliability in the presence of failures. Systems need to be fault-tolerant. This requires a process for distributed, often heterogeneous systems to reach a consensus and agree on the network state, whether it is a database commit or an action to take. In this section, we will discuss two types of consensus algorithms, PoW and proof-of-stake (PoS).

What is consensus?

Consensus in a blockchain is the process by which a network of mutually distrusted nodes reaches an agreement on the global state of the chain of blocks. In blockchain, transactions or data are shared and distributed across the network. Every node has the same copy of the blockchain data. Consensus allows all of the network nodes to follow the same rules to validate transactions and add new blocks to the chain, and therefore allows it to maintain uniformity in all of the copies of a blockchain.

Sometimes, it is also called a consensus mechanism or consensus algorithm. A consensus mechanism focuses on the process and approach of how to reach an agreement. A consensus algorithm is a formal procedure or computer program for solving a consensus problem, based on conducting a sequence of specified actions. It is designed to achieve reliability in a network involving multiple nodes. Consensus algorithms ensure that the next block in a blockchain is fully validated and secured. Multiple kinds of consensus algorithms currently exist, each with different fundamental processes. Different blockchain platforms may implement different consensus mechanism. In this section, we will focus on the following two popular algorithms, show how they work, and discuss the pros and cons of each mechanism:

PoW

:

This consensus algorithm was introduced by Satoshi and commonly adopted by many other blockchains, including Ethereum. The

PoW

is the mining process with the purpose of finding an answer to a cryptographic hashing problem. To do so,

the miner has to follow the block selection rules to locate the previous block and use the hash from the previous block header, together with the Merkle root of current transactions in the new block, t

o solve the hashing problem. It requires considerable computations and hashing power. In Bitcoin, block selection rules that specify the longest chain wins.

PoS

: This consensus algorithm aims to select miners based on the various combinations of random selection based on the miners' wealth or age (the stake). Instead of miners competing to solve energy consuming cryptographic hash functions, the network will instead use a pool of

validators

. Validators are nodes that are willing to

stake

their cryptocurrency on the blocks of transactions that they claim should be added to the public blockchain.

Proof-of-work

Proof-of-work, also referred to as PoW, is the most popular consensus algorithm used by blockchain and cryptocurrencies such as Bitcoin and Ethereum, each one with its own differences. We will talk about the specific implementation of PoW in Bitcoin and Ethereum in later sections.

How PoW works

In general, PoW is like a race between miners to solve a cryptographic puzzle ; upon solving the puzzle, they win the chance to add the block to the chain and get rewarded. As shown in the following screenshot, miners collect all pending transactions from the decentralized network and compete with each other to solve the puzzle. Whoever solves the puzzle will generate a block and push that block into the network for verification from other nodes, after which, the other nodes can add that block to their own copy of the blockchain:

The cryptographic puzzle that miners race to solve is identifying the value of the nonce. A nonce is an attribute in the block header structure. In the beginning, each miner guesses a number to start with, checking whether the resulting hash value is less than the blockchain specific target. Bitcoin uses the SHA-256 algorithm for this. SHA-256 outputs a fixed-length number. Every number between 0 to 232 has the same chance to solve the puzzle, therefore a practical approach is to loop through from 0 to 232 until a number can meet the criteria, as shown in the following diagram:

Once a miner finds the nonce, the results, including the previous block's hash value; the collection of transactions; the Merkle root of all transactions in the block; and the nonce are broadcasted to the network for verification. Upon being notified, the other nodes from the network automatically check whether the results are valid. If the results are valid, they add the block to their copies of the blockchain, stop the mining work in hand, and move on to the next block.

Targets and difficulty

A target is a blockchain-specific 256-bit number that the network sets up for all miners. The SHA-256 hash of a block's header—the nonce plus the rest of the block header—must be lower than or equal to the current target for the block to be accepted by the network.

The difficulty of a cryptographic puzzle depends on the number of leading zeros in the target. The lower the target, the more difficult it is to generate a block. Adding leading zeros in the target number will increase the difficulty of finding such a nonce exponentially. As you can imagine, the higher the difficulty setting, the more difficult it will be to evaluate the nonce. Adding one leading zero in the target will reduce by 50% the chance of finding the nonce. The difficulty is decided by the blockchain network itself. The basic rule of thumb is to set the difficulty proportionally to the total effort on the network. If the number of miner nodes doubles, the difficulty will also double. The difficulty is periodically adjusted to keep the block time around the target time. In Bitcoin, it is 10 minutes.

Incentives and rewards

The winner of the cryptographic puzzle needs to expend huge energy and crucial CPU time to find the nonce and win the chance to create new blocks in the blockchain. The reward for such actions depends on the blockchain itself. In a Bitcoin blockchain, the winner will be rewarded with Bitcoin, the cryptocurrency in the Bitcoin blockchain.

The PoW consensus is a simple and yet reliable mechanism to maintain the state of the blockchain. It is simple to implement. It is a democratic lottery-based system that lets you participate in the game of mining and get the rewards, where every node can join and higher CPU power may not translate into higher rewards. Currently, the winning miner is rewarded with 12.5 BTC for each block created in the Bitcoin blockchain.

Double-spend issues

Satoshi's original intention in using a PoW mechanism is to solve double-spend issues and ensure the integrity of the global state of the Bitcoin blockchain network. Let's say Alice sends 10 BTC to Bob, and at the same time or later on she pays Catherine the same 10 BTC. We could end up with the following three situations:

The first transaction goes through the PoW and is added to the blockchain when the second transaction is submitted. In this case, the second one will be rejected when miners pull it from the transaction pool and validate it against all parent blocks.

Both transactions are submitted simultaneously and both go into the unconfirmed pool of transactions. In this case, only the first transaction gets a confirmation and will be added in the next block. Her second transaction will not be confirmed as per validation rules.

Both get confirmed and are added into competing blocks. This happens when miners take both transactions from the pool and put them into competing blocks. The competing blocks form a temporary fork on the blockchain. Whichever transaction gets into the longest chain will be considered valid and spent, and the other one within the block on the short chain will be recycled. When it is reprocessed, it will be rejected since it is already spent. In this case, it may take a few blocks to get the other one recognized as the double-spent one.

Advantages and disadvantages

However, there are a few drawbacks with the PoW algorithm, thanks to the economic cost of maintaining the blockchain network safety:

Energy consumption:

PoW

consensus, which uses a network of powerful computers to secure the network, is extremely expensive and energy-intensive. Miners need to use specialized hardware with high computing capacity in order to perform mining and get rewards. A large amount of electricity is required to run these mining nodes continuously. Some people also claim these cryptographic hash calculations are useless as they can't produce any business value. At the end of 2018, the Bitcoin network across the Globe used more power than Denmark.

Vulnerability:

PoW consensus is vulnerable to 51% attacks, which means, in theory, dishonest miners could gain a majority of hashing power and manipulate the blockchain to their advantage.

Centralization:

Winning a mining game requires specified and expensive hardware, typically an ASIC type of machine. Expenses grow unmanageable, and mining becomes possible only for a small number of sophisticated miners. The consequence of this is a gradual increase in the centralization of the system, as it becomes a game of riches.

On the flip side, it requires huge computing power and electricity to take over the PoW-based blockchain. Therefore, PoW is perceived as an effective way to prevent Denial-of-Service (DoS) and Distributed Denial-of-Service (DDoS) attacks on the blockchain.

Proof-of-stake

As opposed to PoW consensus, where miners are rewarded for solving cryptographic puzzles, in the PoS consensus algorithm, a pool of selected validators take turns proposing new blocks. The validator is chosen in a deterministic way, depending on its wealth, also defined as a stake. Anyone who deposits their coins as a stake can become a validator. The chance to participate may be proportional to the stakes they put in. Let's say, Alice, Bob, Catherine, and David put in 40 Ether, 30 Ether, 20 Ether, and 10 Ether stakes to participate respectively; they will get a 40%, 30%, 20%, and 10% chance of being selected as the block creator.

The following is how it works in the PoS consensus mechanism. As shown in the following diagram, the blockchain keeps track of a set of validators, sometimes also called block creators or forgers. At any time, whenever new blocks need to be created, the blockchain randomly selects a validator. The selected validator verifies the transactions and proposes new blocks for all validators to agree on. New blocks are then voted on by all current validators. Voting power is based on the stake the validator puts in. Whoever proposes invalid transactions or blocks or votes maliciously, which means they intentionally compromise the integrity of the chain, may lose their stakes. Upon the new blocks being accepted, the block creator can collect the transaction fee as the reward for the work of creating new blocks: