With Case Studies and Code Samples in Solidity
Debajani Mohanty
Book Details
Price
|
3.00 USD |
---|---|
Pages
| 281 p |
File Size
|
6,715 KB |
File Type
|
PDF format |
ISBN-13 (electronic) ISBN-13 (pbk)
| 978-1-4842-4075-5 978-1-4842-4074-8 |
Copyright
| 2018 by Debajani Mohanty |
Debajani Mohanty is a senior architect with
NIIT Technologies Ltd in Delhi, NCR, and has
almost 17 years of experience in the industry.
She has been involved in large projects and
has built many scalable enterprise B2B and
B2C products in the travel, e-governance,
e-commerce, and BFSI domains. Writing
complex technical articles in easy-tounderstand
language is a forte that has earned
her close to 10,000 followers on social media.
Debajani is the author of the bestseller BlockChain: From Concept to
Execution, which has been translated to other languages such as German.
Debajani is an honorary faculty member at Amity University Online. She is
also a NASSCOM event speaker and on the international panels of mentors
at Kerala Blockchain Academy, the first blockchain academy in India.
Debajani is also a woman activist and writer. She has been felicitated
by Nobel Peace Prize winner Mr. Kailash Satyarthi for her outstanding
contributions to women empowerment in the field of literature.
About the Technical Reviewer
Pon ArunKumar Ramalingam is an IT
cognitive applications development consultant
who has extensive experience in building
full stack applications, using the Ethereum
blockchain, implementing machine learning,
and implementing enterprise resource planning
suites of applications. He runs his own strategic
sourcing and consulting company serving clients
in the areas of human capital management,
campus solutions, financials, and supply chain.
At ETHDenver, he won a hackathon on building decentralized
Ethereum applications for societal cause, addressing the needs of street
artists. He has worked for several fortune 500 companies and startups in
North America and Asia Pacific. His career with Hexaware Technologies
and Ford Motor Company brought him a lot of accolades for the services
that he provided while relentlessly innovating ways to use emerging
technology to solve real-world customer problems. His mastery in the
emerging Ethereum blockchain and machine learning technologies has
been showcased in various meetup presentations.
He is a wonderful team player, developer, and solution design architect
and is a certified service-oriented architect professional. He has a BS in
Engineering degree in computer science from the Sri Krishna College of
Engineering and Technology at Bharathiar University in Coimbatore, Tamil
Nadu, India. He currently lives in Concord, North Carolina.
Preface
Since writing my first book on the blockchain technology, I have been
invited to many discussions and have been approached by many startups
to discuss how to initiate decentralized applications or products on either
blockchain or distributed ledger technology.
Interestingly, most people I have met have little understanding of the
capabilities and limitations of the blockchain technology. The hype has
caused business leaders to spend time investigating use cases that are
not necessarily good fits for a blockchain. Industry is still not sure how
businesses can get benefits from this blockchain tsunami. To be precise,
most blockchain experts today mint money from the market either by
offering training programs or by working on initial coin offerings because
business leaders are skeptical about blockchain technology and its
potential and are still waiting for the right moment to go ahead with its
implementation.
Unlike others, I am a late starter in the world of blockchains. In my
first book, BlockChain: From Concept to Execution, I summarized most
of the blockchain frameworks available on the market and where you
can use them. However, as I took a deeper dive into the architecture and
implementation details of these frameworks, I ran into a few roadblocks
learning how to use them in real life, and I couldn’t find much information
available in books, via training programs, or on web sites. I expected to
find a straightforward, comprehensive tutorial that covered all the basic
necessities, such as verifying proofs and setting up tests, that would help a
novice like me, but I came up empty-handed. As a result, I decided to write
a book to help out others looking for the same details.
This book that I finally ended up writing is a perfect amalgamation of
learning to use the blockchain technology at the same time as evaluating
profitable, salable business use cases and their implementations using
Ethereum, the most widely used blockchain framework in today’s market.
With step-by-step tutorials, examples, and pictorial representations
and flowcharts, the book is suitable for even novices because of its
easy readability. At the same time, it has many case studies with basic
implementation details across verticals that will give architects and
business leaders some vision of how and where this technology can be
used to earn maximum profitability. Readers can use the sample code,
enhance it per their respective business needs, and gradually develop and
test decentralized applications before going to production.
Table of Contents
About the Author
About the Technical Reviewer
Acknowledgments
Preface
Guidelines to Use the Book
Chapter 1: The World of Blockchains
Why Bitcoin Took the Market by Storm .................................................................2
Blockchains and Smart Contracts, the Need of the Hour ......................................3
Introduction to Blockchains ..................................................................................5
Business Problem .................................................................................................5
Fully Distributed Model ....................................................................................6
Fully Centralized Model ...................................................................................7
DLT, or the Decentralized Peer-to-Peer Model .................................................9
DLT vs. Blockchains ............................................................................................12
Benefits of Blockchains ......................................................................................13
Blockchain Transactions and Blocks .............................................................14
Block Header .................................................................................................15
Merkle Tree ....................................................................................................16
Double Spending ...........................................................................................18
Blockchain Hashing .......................................................................................19
Public and Private Keys .................................................................................20
Consensus ...........................................................................................................21
Proof of Work .................................................................................................22
Proof of Stake ................................................................................................23
Delegated Proof of Stake ...............................................................................24
Proof of Authority ...........................................................................................24
Practical Byzantine Fault Tolerance ...............................................................24
Directed Acyclic Graphs .................................................................................25
Forks in Blockchains ...........................................................................................25
Soft Fork ........................................................................................................26
Hard Fork .......................................................................................................27
Forks in Ethereum .........................................................................................27
Types of Visibilities in Blockchain Networks .......................................................28
Public Blockchains ........................................................................................29
Private Blockchains .......................................................................................29
Consortium or Federated Blockchains ..........................................................29
Advantages of Ethereum .....................................................................................30
Limitations of Ethereum ......................................................................................31
Leading Blockchain and DLT Protocols ...............................................................31
Quorum ..........................................................................................................32
Ripple ............................................................................................................32
Hyperledger Fabric ........................................................................................32
R3 Corda ........................................................................................................32
MultiChain .....................................................................................................33
Symbiont .......................................................................................................33
OpenChain .....................................................................................................33
Cardano .........................................................................................................33
IOTA ...............................................................................................................34
EOS ................................................................................................................34
Hashgraph .....................................................................................................34
Most Ambitious Ethereum Projects in Production ...............................................35
Chapter 2: Ethereum Architecture
Bitcoin vs. Ethereum ...........................................................................................37
Turing Complete ..................................................................................................38
Ethereum Virtual Machine ...................................................................................38
Consensus Mechanism .......................................................................................39
Decentralized Autonomous Organization ............................................................40
Smart Contracts ..................................................................................................40
Solidity ................................................................................................................41
Gas ......................................................................................................................41
Where Does Data Get Stored in Ethereum? ........................................................43
Ethereum Accounts .............................................................................................43
Storage Cost .......................................................................................................44
The Entire Ethereum Ecosystem .........................................................................44
Interplanetary File System ............................................................................45
Swarm ...........................................................................................................49
IPFS vs. Swarm .............................................................................................49
Whisper .........................................................................................................50
Cryptocurrency or Token? ...................................................................................52
Ether ..............................................................................................................52
Wei .................................................................................................................52
How to Get Ether ............................................................................................53
Private Ethereum Blockchains ............................................................................54
Chapter 3: Basic Solidity Programming
Prerequisites .......................................................................................................55
Remix Browser ....................................................................................................55
Deploying Contracts in Remix .............................................................................57
Solidity File Details .............................................................................................59
Extension .......................................................................................................59
File Storage ...................................................................................................59
Application Binary Interface ..........................................................................59
Import Statement ..........................................................................................62
Version ...........................................................................................................62
Variables ........................................................................................................63
By Value .........................................................................................................63
By Reference .................................................................................................65
Solidity Comments ........................................................................................67
Function .........................................................................................................67
Constructor ....................................................................................................70
Visibility .........................................................................................................72
Getter and Setter ...........................................................................................72
Error Handling: throw, revert(), assert(), require() ..........................................74
Function with No Gas Cost ............................................................................76
Data Storage ..................................................................................................78
Events ............................................................................................................79
Object-Oriented Approach ...................................................................................80
Encapsulation ................................................................................................80
Inheritance ....................................................................................................83
Polymorphism ................................................................................................85
Abstraction ....................................................................................................85
Function Overloading .....................................................................................88
Libraries ..............................................................................................................88
End a Contract ....................................................................................................89
Solidity, Bytecode, and Opcode ...........................................................................91
Assembly Language ............................................................................................92
Running on Remix ...............................................................................................93
Debugging on Remix ...........................................................................................97
Running on the solc Compiler ...........................................................................102
Unit Testing .......................................................................................................102
Embark ........................................................................................................103
Truffle ..........................................................................................................103
Dapp ............................................................................................................103
Populus ........................................................................................................103
Chapter 4: Deploying Smart Contracts
Local Ethereum Testing with Ganache ..............................................................105
Public Ethereum Testing with the Ropsten Testnet ...........................................109
Using MetaMask ..........................................................................................110
Deploying the Contract ................................................................................115
Deploying on a Private Network ........................................................................121
Installing ......................................................................................................122
Getting Started ............................................................................................123
Mining Ether ................................................................................................129
Deploying on the Network ...........................................................................130
Deploying on the Cloud .....................................................................................130
Deploying a Private Ethereum Blockchain on Microsoft Azure ....................131
Amazon AWS and IBM Bluemix ...................................................................138
Chapter 5: Integration with the UI
Introduction to Web3.js .....................................................................................139
Installing Node and Web3.js ..............................................................................140
Writing a Smart Contract ..................................................................................142
Writing the Front-End Code ...............................................................................143
Testing Through the Screen ..............................................................................148
Testing Through Remix ......................................................................................149
Chapter 6: Advanced Programming in Oraclize and IPFS,
and Best Practices
Oraclize .............................................................................................................152
Example .......................................................................................................154
Trying the Oraclize IDE ................................................................................157
Encrypting Data with a Python Script ..........................................................159
Recursive Time-Based Queries ...................................................................161
Oraclize Real-Life Implementations ............................................................161
ChainLink .....................................................................................................162
Storing Larger Content on IPFS .........................................................................162
Benefits of IPFS ...........................................................................................162
Locally Configuring IPFS ..............................................................................163
IPNS .............................................................................................................172
Ethereum Best Practices ...................................................................................173
Enterprise Smart Contracts .........................................................................174
Version .........................................................................................................174
Naming Conventions ...................................................................................174
Visibility Call ................................................................................................175
Delegate Call ...............................................................................................175
Simplicity and Modularity ............................................................................175
Overflow and Underflow ..............................................................................175
External Calls ...............................................................................................176
Race Conditions ...........................................................................................176
Reentrancy ..................................................................................................176
Timestamp Dependence ..............................................................................178
Transaction Ordering ...................................................................................178
Token Standards ..........................................................................................178
Unit Testing ..................................................................................................178
Smart Contract Auditing ..............................................................................179
Security Tools ..............................................................................................179
Chapter 7: Frameworks: Truffle and Embark
Truffle ................................................................................................................181
Install Truffle ................................................................................................182
Create a Truffle Project ................................................................................182
Unit Testing ..................................................................................................185
OpenZeppelin: Securing Solidity Code ..............................................................190
Truffle Road Map ...............................................................................................190
Embark ..............................................................................................................191
Install Embark ..............................................................................................191
Create an Embark Project ............................................................................191
Unit Testing ..................................................................................................195
Chapter 8: Testing Strategy for Ethereum Dapps
Blockchains and Testing ...................................................................................198
Functional and Nonfunctional Testing ...............................................................198
Standard Functional Testing ........................................................................198
Standard Nonfunctional Testing ..................................................................199
Specialized Testing ......................................................................................200
Chapter 9: Ethereum Use Cases
Initial Coin Offering ...........................................................................................204
ICO Road Map ..............................................................................................204
Ethereum Request for Comment Standards ................................................208
EIP ...............................................................................................................215
Microfinance .....................................................................................................216
Solution .......................................................................................................217
Smart Contract Rules ..................................................................................221
Live Implementation ....................................................................................222
Real Estate ........................................................................................................222
Solution .......................................................................................................222
Ethereum Advantages ..................................................................................226
Live Implementation ....................................................................................227
Travel ................................................................................................................228
Solution .......................................................................................................228
Ethereum Advantages ..................................................................................229
Live Implementations ..................................................................................230
Car Insurance ....................................................................................................230
Solution .......................................................................................................230
Ethereum Advantages ..................................................................................232
Live Implementations ..................................................................................233
Legal .................................................................................................................233
Solution .......................................................................................................233
Ethereum Advantages ..................................................................................234
Education ..........................................................................................................235
Solution .......................................................................................................235
Ethereum Advantages ..................................................................................236
Live Implementations ..................................................................................236
Healthcare .........................................................................................................237
Solution .......................................................................................................237
Ethereum Advantages ..................................................................................238
Live Implementations ..................................................................................239
Secure Voting and Digital Identity .....................................................................240
Solution .......................................................................................................240
Ethereum Advantages ..................................................................................242
Live Implementations ..................................................................................243
Chapter 10: Ethereum: What Lies Ahead
The Evolution of Ethereum ................................................................................245
Olympic, May 2015 ......................................................................................246
Frontier, July 2015 .......................................................................................246
Homestead, March 2016 .............................................................................246
Byzantium Metropolis Phase I, October 2017 ..............................................246
Constantinople Metropolis Phase II, Slated for 2018 ...................................246
Serenity, Slated for 2018 .............................................................................247
Scaling of Ethereum ..........................................................................................247
Casper Proof of Stake ..................................................................................247
Sharding ......................................................................................................248
Raiden Network ...........................................................................................249
Plasma .........................................................................................................250
Internet of Blockchains: Polkadot, Cosmos, Coco ........................................250
Governance .......................................................................................................251
Conclusion ........................................................................................................253
References ........................................................................................................253
Index
Guidelines to Use the Book
Ethereum for Architects and Developers is an excellence book on Ethereum
that explores the entire Ethereum ecosystem with step-by-step examples
and plenty of theory, labs, and live use cases.
Ethereum today is the most widely used blockchain framework on
the market; however, the main issues for learning Ethereum are the
lack of trainers and its inadequate documentation. To learn Ethereum,
developers, architects, and business leaders have to collect data from
different web sites, blogs, articles, and YouTube videos. Ethereum for
Architects and Developers should fill that vacuum by providing content
suitable to all stakeholders consolidated in one place. On this reading
journey, you will be introduced to blockchain concepts, decentralized
applications, Ethereum architecture, Solidity smart contract programming
with examples, and finally instructions on testing, debugging, and
deploying smart contracts. Best practices to write contracts are explained
in the most efficient way with ample examples to guide developers to write
high-quality contracts. Later chapters will be most beneficial for business
leaders and architects as the book will cover different business verticals
such as finance, travel, supply chain, insurance, land registry, and more
with flowcharts, diagrams, and sample code that stakeholders can refer to,
enhance, and deploy in live projects.
The book will be useful for readers of every background who are
eager to develop Ethereum decentralized applications, want to learn
its architecture, or are interested in exploring different use cases that
can be implemented using the Ethereum blockchain framework across
business verticals. By the end of the book, readers will have enough
information about how the optimal usage of Ethereum can create value
for their business processes by eliminating middleman costs and bringing
transparency to the creation of deduplicated, fraud-proof data storage for
the smoother execution of business.