Tải bản đầy đủ (.pdf) (965 trang)

MasteringBlockchainSecondEdition code

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (27.98 MB, 965 trang )


Mastering Blockchain
Second Edition

Distributed ledger technology, decentralization, and smart contracts explained

Imran Bashir


BIRMINGHAM - MUMBAI



Mastering Blockchain Second Edition
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or
alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
Acquisition Editors: Ben Renow-Clarke, Suresh M Jain
Project Editor: Suzanne Coutinho
Content Development Editor: Alex Sorrentino
Technical Editor: Bhagyashree Rai
Indexer: Tejal Daruwale Soni
Graphics: Tom Scaria
Production Coordinator: Aparna Bhagat
First published: M arch 2017
Second edition: M arch 2018


Production reference: 1290318
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78883-904-4
www.packtpub.com


mapt.io

Mapt is an online digital library that gives you full access to over 5,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
Mapt is fully searchable
Copy and paste, print, and bookmark content


PacktPub.com
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.PacktPub.com and as a print book customer, you
are entitled to a discount on the eBook copy. Get in touch with us at for more

details.
At www.PacktPub.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 author
Imran Bashir has an M.Sc. in Information Security from Royal Holloway, University of London, and
has a background in software development, solution architecture, infrastructure management, and IT
service management. He is also a member of the Institute of Electrical and Electronics Engineers
(IEEE) and the British Computer Society (BCS).
Imran has sixteen years' of experience in the public and financial sectors. He worked on large scale
IT projects in the public sector before moving to the financial services industry. Since then, he has
worked in various technical roles for different financial companies in Europe's financial capital,
London. He is currently working for an investment bank in London as Vice President in the
Technology department.
I would like to thank the talented team at Packt, including Ben Renow-Clarke, Suzanne Coutinho, Alex Sorrentino, Gary
Schwartz, and Bhagyashree Rai, who provided prompt guidance and valuable feedback throughout this project. I am also
extremely thankful to the reviewer, Pranav Burnwal, who provided constructive and very useful feedback that helped me
tremendously to improve the material in this book.
I thank my wife and children for putting up with my all-night and weekend-long writing sessions.
Above all, I would like to thank my parents, whose blessings have made everything possible for me.


About the reviewer
Pranav Burnwal has a background in Research and Development, and he has been working with
cutting-edge technologies for the past few years. The technologies he works on range from
blockchain, big data, analytics (log and data), cloud, to message queues, NoSQL, web servers, and so

on. He has worked across various domains ranging from BFSI, HLS, FMCG, and automobiles to
name a few.
Pranav is an active community member in multiple communities. He is the Regional Head for
Blockchain Education Network (BEN), a registered NGO and a worldwide network of people of
blockchain. He has also organized multiple meetups and a start-up weekend in India.
Pranav has also been an active trainer in the blockchain space for an exciting period of three years
now, for an audience ranging from junior developers to senior VPs. This has also given him insights
into how people understand a new and complex technology, which helped him frame this book in the
best interest of the readers.


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
Mastering Blockchain Second Edition
Packt Upsell
Why subscribe?
PacktPub.com
Contributors
About the author
About the reviewer
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

1.

Blockchain 101
The growth of blockchain technology
Distributed systems
The history of blockchain and Bitcoin
Electronic cash
Blockchain
Blockchain defined
Peer-to-peer
Distributed ledger
Cryptographically-secure
Append-only
Updateable via consensus
Generic elements of a blockchain
How blockchain works
How blockchain accumulates blocks
Benefits and limitations of blockchain
Tiers of blockchain technology
Features of a blockchain
Types of blockchain

Distributed ledgers
Distributed Ledger Technology
Public blockchains
Private blockchains
Semiprivate blockchains
Sidechains
Permissioned ledger
Shared ledger


Fully private and proprietary blockchains
Tokenized blockchains
Tokenless blockchains
Consensus
Consensus mechanism
Types of consensus mechanisms
Consensus in blockchain
CAP theorem and blockchain
Summary

2.

Decentralization
Decentralization using blockchain
Methods of decentralization
Disintermediation
Contest-driven decentralization
Routes to decentralization
How to decentralize
The decentralization framework example

Blockchain and full ecosystem decentralization
Storage
Communication
Computing power and decentralization
Smart contracts
Decentralized Organizations
Decentralized Autonomous Organizations
Decentralized Autonomous Corporations
Decentralized Autonomous Societies
Decentralized Applications (DApps)
Requirements of a Decentralized Application
Operations of a DApp
DApp examples
KYC-Chain
OpenBazaar
Lazooz
Platforms for decentralization
Ethereum
MaidSafe
Lisk
Summary

3.

Symmetric Cryptography
Working with the OpenSSL command line
Introduction
Mathematics
Set
Group

Field
A finite field
Order
An abelian group


Prime fields
Ring
A cyclic group
Modular arithmetic
Cryptography
Confidentiality
Integrity
Authentication
Entity authentication
Data origin authentication
Non-repudiation
Accountability
Cryptographic primitives
Symmetric cryptography
Stream ciphers
Block ciphers
Block encryption mode
Electronic Code Book
Cipher Block Chaining
Counter mode
Keystream generation mode
Message authentication mode
Cryptographic hash mode
Data Encryption Standard

Advanced Encryption Standard
How AES works
Summary

4.

Public Key Cryptography
Asymmetric cryptography
Integer factorization
Discrete logarithm
Elliptic curves
Public and private keys
RSA
Encryption and decryption using RSA
Elliptic Curve Cryptography
Mathematics behind ECC
Point addition
Point doubling
Discrete logarithm problem in ECC
RSA using OpenSSL
RSA public and private key pair
Private key
Public key
Exploring the public key
Encryption and decryption
Encryption
Decryption
ECC using OpenSSL



ECC private and public key pair
Private key
Private key generation
Hash functions
Compression of arbitrary messages into fixed-length digest
Easy to compute
Preimage resistance
Second preimage resistance
Collision resistance
Message Digest
Secure Hash Algorithms
Design of Secure Hash Algorithms
Design of SHA-256
Design of SHA-3 (Keccak)
OpenSSL example of hash functions
Message Authentication Codes
MACs using block ciphers
Hash-based MACs
Merkle trees
Patricia trees
Distributed Hash Tables
Digital signatures
RSA digital signature algorithm
Sign then encrypt
Encrypt then sign
Elliptic Curve Digital Signature Algorithm
How to generate a digital signature using OpenSSL
ECDSA using OpenSSL
Homomorphic encryption
Signcryption

Zero-Knowledge Proofs
Blind signatures
Encoding schemes
Financial markets and trading
Trading
Exchanges
Orders and order properties
Order management and routing systems
Components of a trade
The underlying instrument
General attributes
Economics
Sales
Counterparty
Trade life cycle
Order anticipators
Market manipulation


Summary

5.

Introducing Bitcoin
Bitcoin
Bitcoin definition
Bitcoin – a bird's-eye view
Sending a payment to someone
Digital keys and addresses
Private keys in Bitcoin

Public keys in Bitcoin
Addresses in Bitcoin
Base58Check encoding
Vanity addresses
Multisignature addresses
Transactions
The transaction life cycle
Transaction fee
Transaction pools
The transaction data structure
Metadata
Inputs
Outputs
Verification
The script language
Commonly used opcodes
Types of transactions
Coinbase transactions
Contracts
Transaction verification
Transaction malleability
Blockchain
The structure of a block
The structure of a block header
The genesis block
Mining
Tasks of the miners
Mining rewards
Proof of Work (PoW)
The mining algorithm

The hash rate
Mining systems
CPU
GPU
FPGA
ASICs
Mining pools
Summary

6.

Bitcoin Network and Payments
The Bitcoin network
Wallets
Non-deterministic wallets


Deterministic wallets
Hierarchical Deterministic wallets
Brain wallets
Paper wallets
Hardware wallets
Online wallets
Mobile wallets
Bitcoin payments
Innovation in Bitcoin
Bitcoin Improvement Proposals (BIPs)
Advanced protocols
Segregated Witness (SegWit)
Bitcoin Cash

Bitcoin Unlimited
Bitcoin Gold
Bitcoin investment and buying and selling bitcoins
Summary

7.

Bitcoin Clients and APIs
Bitcoin installation
Types of Bitcoin Core clients
Bitcoind
Bitcoin-cli
Bitcoin-qt
Setting up a Bitcoin node
Setting up the source code
Setting up bitcoin.conf
Starting up a node in testnet
Starting up a node in regtest
Experimenting with Bitcoin-cli
Bitcoin programming and the command-line interface
Summary

8.

Alternative Coins
Theoretical foundations
Alternatives to Proof of Work
Proof of Storage
Proof of Stake (PoS)
Various stake types

Proof of coinage
Proof of Deposit (PoD)
Proof of Burn
Proof of Activity (PoA)
Nonoutsourceable puzzles
Difficulty adjustment and retargeting algorithms
Kimoto Gravity Well
Dark Gravity Wave
DigiShield
MIDAS
Bitcoin limitations


Privacy and anonymity
Mixing protocols
Third-party mixing protocols
Inherent anonymity
Extended protocols on top of Bitcoin
Colored coins
Counterparty
Development of altcoins
Consensus algorithms
Hashing algorithms
Difficulty adjustment algorithms
Inter-block time
Block rewards
Reward halving rate
Block size and transaction size
Interest rate
Coinage

Total supply of coins
Namecoin
Trading Namecoins
Obtaining Namecoins
Generating Namecoin records
Litecoin
Primecoin
Trading Primecoin
Mining guide
Zcash
Trading Zcash
Mining guide
Address generation
GPU mining
Downloading and compiling nheqminer
Initial Coin Offerings (ICOs)
ERC20 tokens
Summary

9.

Smart Contracts
History
Definition
Ricardian contracts
Smart contract templates
Oracles
Smart Oracles
Deploying smart contracts on a blockchain
The DAO

Summary

10.

Ethereum 101
Introduction
The yellow paper
Useful mathematical symbols


Ethereum blockchain
Ethereum – bird's eye view
The Ethereum network
Mainnet
Testnet
Private net
Components of the Ethereum ecosystem
Keys and addresses
Accounts
Types of accounts
Transactions and messages
Contract creation transaction
Message call transaction
Messages
Calls
Transaction validation and execution
The transaction substate
State storage in the Ethereum blockchain
The world state
The account state

Transaction receipts
Ether cryptocurrency / tokens (ETC and ETH)
The Ethereum Virtual Machine (EVM)
Execution environment
Machine state
The iterator function
Smart contracts
Native contracts
Summary

11.

Further Ethereum
Programming languages
Runtime bytecode
Opcodes and their meaning
Arithmetic operations
Logical operations
Cryptographic operations
Environmental information
Block information
Stack, memory, storage, and flow operations
Push operations
Duplication operations
Exchange operations
Logging operations
System operations
Blocks and blockchain
The genesis block
The block validation mechanism

Block finalization


Block difficulty
Gas
Fee schedule
Forks in the blockchain
Nodes and miners
The consensus mechanism
Ethash
CPU mining
GPU mining
Benchmarking
Mining rigs
Mining pools
Wallets and client software
Geth
Eth
Pyethapp
Parity
Light clients
Installation
Eth installation
Mist browser
Geth
The geth console
Funding the account with bitcoin
Parity installation
Creating accounts using the parity command line
APIs, tools, and DApps

Applications (DApps and DAOs) developed on Ethereum
Tools
Supporting protocols
Whisper
Swarm
Scalability, security, and other challenges
Trading and investment
Summary

12.

Ethereum Development Environment
Test networks
Setting up a private net
Network ID
The genesis file
Data directory
Flags and their meaning
Static nodes
Starting up the private network
Running Mist on private net
Deploying contracts using Mist
Block explorer for private net / local Ethereum block explorer
Summary


13.

Development Tools and Frameworks
Languages

Compilers
Solidity compiler (solc)
Installation on Linux
Installation on macOS
Integrated Development Environments (IDEs)
Remix
Tools and libraries
Node version 7
EthereumJS
Ganache
MetaMask
Truffle
Installation
Contract development and deployment
Writing
Testing
Solidity language
Types
Value types
Boolean
Integers
Address
Literals
Integer literals
String literals
Hexadecimal literals
Enums
Function types
Internal functions
External functions

Reference types
Arrays
Structs
Data location
Mappings
Global variables
Control structures
Events 
Inheritance
Libraries
Functions
Layout of a Solidity source code file
Version pragma
Import
Comments
Summary

14.

Introducing Web3
Web3
Contract deployment


POST requests
The HTML and JavaScript frontend
Installing web3.js
Example
Creating a web3 object
Checking availability by calling any web3 method

Contract functions
Development frameworks
Truffle
Initializing Truffle
Interaction with the contract
Another example
An example project – Proof of Idea
Oracles
Deployment on decentralized storage using IPFS
Installing IPFS
Distributed ledgers
Summary

15.

Hyperledger
Projects under Hyperledger
Fabric
Sawtooth Lake
Iroha
Burrow
Indy
Explorer
Cello
Composer
Quilt
Hyperledger as a protocol
The reference architecture
Requirements and design goals of Hyperledger Fabric
The modular approach

Privacy and confidentiality
Scalability
Deterministic transactions
Identity
Auditability
Interoperability
Portability
Rich data queries
Fabric
Hyperledger Fabric
Membership services
Blockchain services
Consensus services
Distributed ledger
The peer to peer protocol
Ledger storage


Chaincode services
Components of the fabric
Peers
Orderer nodes
Clients
Channels
World state database
Transactions
Membership Service Provider (MSP)
Smart contracts
Crypto service provider
Applications on blockchain

Chaincode implementation
The application model
Consensus in Hyperledger Fabric
The transaction life cycle in Hyperledger Fabric
Sawtooth Lake
PoET
Transaction families
Consensus in Sawtooth
The development environment – Sawtooth Lake
Corda
Architecture
State objects
Transactions
Consensus
Flows
Components
Nodes
The permissioning service
Network map service
Notary service
Oracle service
Transactions
Vaults
CorDapp
The development environment – Corda
Summary

16.

Alternative Blockchains

Blockchains
Kadena
Ripple
Transactions
Payments related
Order related
Account and security-related
Interledger
Application layer


Transport layer
Interledger layer
Ledger layer
Stellar
Rootstock
Sidechain
Drivechain
Quorum
Transaction manager
Crypto Enclave
QuorumChain
Network manager
Tezos
Storj
MaidSafe
BigchainDB
MultiChain
Tendermint
Tendermint Core

Tendermint Socket Protocol (TMSP)
Platforms and frameworks
Eris
Summary

17.

Blockchain – Outside of Currencies
Internet of Things
Physical object layer
Device layer
Network layer
Management layer
Application layer
IoT blockchain experiment
First node setup
Raspberry Pi node setup
Installing Node.js
Circuit
Government
Border control
Voting
Citizen identification (ID cards)
Miscellaneous
Health
Finance
Insurance
Post-trade settlement
Financial crime prevention
Media

Summary

18.

Scalability and Other Challenges


×