32,36 €
An expert guide to implementing fast, secure, and scalable decentralized applications that work with thousands of users in real time
Key Features
Book Description
Ethereum is one of the commonly used platforms for building blockchain applications. It's a decentralized platform for applications that can run exactly as programmed without being affected by fraud, censorship, or third-party interference.
This book will give you a deep understanding of how blockchain works so that you can discover the entire ecosystem, core components, and its implementations. You will get started by understanding how to configure and work with various Ethereum protocols for developing dApps. Next, you will learn to code and create powerful smart contracts that scale with Solidity and Vyper. You will then explore the building blocks of the dApps architecture, and gain insights on how to create your own dApp through a variety of real-world examples. The book will even guide you on how to deploy your dApps on multiple Ethereum instances with the required best practices and techniques. The next few chapters will delve into advanced topics such as, building advanced smart contracts and multi-page frontends using Ethereum blockchain. You will also focus on implementing machine learning techniques to build decentralized autonomous applications, in addition to covering several use cases across a variety of domains such as, social media and e-commerce.
By the end of this book, you will have the expertise you need to build decentralized autonomous applications confidently.
What you will learn
Who this book is for
This book is for anyone who wants to build fast, highly secure, and transactional decentralized applications. If you are an Ethereum developer looking to perfect your existing skills in building powerful blockchain applications, then this book is for you. Basic knowledge of Ethereum and blockchain is necessary to understand the concepts covered in this book.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 552
Veröffentlichungsjahr: 2019
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 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.
Commissioning Editor: Sunith ShettyAcquisition Editor:Winston ChristopherContent Development Editor:Nathanya DiasTechnical Editor:Joseph SunilCopy Editor: Safis EditingProject Coordinator:Kirti PisatProofreader: Safis EditingIndexer:Rekha NairGraphics:Jisha ChirayilProduction Coordinator:Aparna Bhagat
First published: April 2019
Production reference: 1250419
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78953-137-4
www.packtpub.com
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.
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
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.
Merunas Grincalaitis, born in Lithuania and based in Spain since he was 4, has worked with blockchain companies all around the world, helping them create ICOs, dApps, MVPs, technical whitepapers, web apps, and audits so that they improve the Ethereum ecosystem with decentralized solutions that return people's trust in themselves.
He created one of the first books about Ethereum development, named Ethereum Developer - Learn Solidity From Scratch, which sold a lot of copies worldwide, as a quick guide to provide a bridge for programmers interested in the world of Ethereum and smart contract programming.
Currently, he's working on providing free learning resources through his Medium blog to all developers looking for expert guidance.
Narendranath Reddy is an experienced full-stack software engineer and Hyperledger Fabric expert with a proven track record of helping enterprises to build production-ready, blockchain-backed applications. He is an experienced innovator and a creative thinker. He has won four hackathons on blockchain and is a keynote speaker, regularly speaking about blockchain and distributed ledgers. He is currently working as a Hyperledger Fabric engineer at Consensys, Dubai, and previously worked as a blockchain developer at Blockgemini, Dubai, and a software developer at UST Global, Trivandrum, and Spain, Madrid.
Iqbal Singh is the CEO and founder of Immanent Solutions, a management consulting and blockchain solutions firm based in Chandigarh, India. He is also the chief blockchain architect for the Blockchain Solutions Asia 2018 conference. Iqbal possesses 15 years' extensive hands-on experience in blockchain, IT, IoT, AI, automation, and the RIA industry. Iqbal has provided business solutions for Bitcoin, Ethereum, Ripple, and R3's Corda blockchain platform. He heads 30 professional teams working in the fields of data science, algorithms, cryptography (SHA256, X11, and Script), blockchain, ICOs, Coin, ERC20, Exchange, BTC, and ETH Expert Architect.
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.
Title Page
Copyrights and Credits
Mastering Ethereum
About Packt
Why subscribe?
Packt.com
Contributors
About the author
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 - Ethereum Refresher
Blockchain Architecture
Beyond Ethereum
Explaining Ethereum
Ethereum's history
Ethereum's development
The EEA
The EEA's vision
The EEA membership
The EEA architecture
Understanding the Ethereum blockchain
The reasoning behind a decentralized blockchain
The blockchain as a set of nodes
The CAP theorem
Introducing the Ethereum Virtual Machine
Introducing the state machine
The P2P network
Consensus rules
Proof-of-work
Proof-of-stake
Workings of smart contracts
Smart contract deployment process
Ethereum transactions
Essential smart contract programming
Solidity
File structure
Variables
Visibility of variables
Uints
Addresses
Strings and bytes
Structs
Enums
Booleans
Arrays
Mappings
Data location
Events
Modifiers
Functions
The fallback function
Vyper
Vyper by example
Summary
Ethereum Ecosystems
Introducing the Ethereum chain specification
The genesis object
Blockchain technology
Application layer
Infrastructure layer
Mining layer
Peer-to-peer layer
Understanding DEVP2P
Blockchain consensus
Introducing blockchain consensus
Envisioning the future
The nothing-at-stake problem
Blockchain economics
Understanding inflation
Evaluating cryptocurrency economics
Determining the value of cryptocurrencies
Ethereum-based tokens
Blockchain metrics
Number of transactions
Address growth
Ether market capitalization
Developer adoption
Miner activity
Summary
Ethereum Assets
Ethereum protocol implementations
Protocol implementations
Understanding Geth
Creating accounts
Downloading the blockchain
Mining with your CPU
Parity
Downloading the blockchain
Creating accounts
Mining with your CPU
Best scenario for each protocol
Smart contract development
Managing accounts
Other use cases
INFURA essentials
Understanding INFURA
Inner workings of INFURA
Using INFURA's dashboard
Decentralized autonomous organizations
Explaining DAOs
Operating within a DAO
Tooling for DAOs
Miscellanea and concerns
Understanding Ethereum improvement proposals
Differentiating the usage of Truffle and IDEs
Understanding smart contract limitations
Creating your own private Ethereum network
Setting up a private blockchain on Mac
Setting up a private blockchain on Windows
Choosing a wallet for Ethereum
Understanding the concept of an Ethereum wallet
Explaining online wallets
Choosing MyEtherWallet
Using MetaMask
Understanding hardware wallets
Using Ledger
Using Trezor
Learning to use multi-signature wallets
Using Etherscan for transactions
Understanding block explorers
Using Etherscan for analyzing transactions
Creating accounts
Creating accounts with MetaMask
Creating an account with Coinbase
Summary
Section 2: Decentralized Application Development Workflow
Mastering Smart Contracts
Getting up to speed
Planning the marketplace idea
Designing the code in Solidity
Deploying your smart contract with Truffle
The EVM in depth
Explaining the EVM
Operation of smart contracts in the EVM
Upgradable and scalable smart contracts
Upgradable smart contracts
Storage smart contracts
Upgradable smart contracts
Proxy smart contracts
Scalable smart contracts
Using state channels for scalable smart contracts
Gas and data-efficient smart contracts
Making gas-efficient smart contracts
Making data-efficient smart contracts
Smart contract security analysis
Techniques to secure a smart contract
Summary
Mastering dApps
Introducing dApp architecture
Installing Ethereum and Truffle
Setting up and configuring Ethereum and Truffle
Installing the required packages
Setting up webpack
Setting up the source folder
Creating dApps
Creating the smart contract
Creating user interfaces
Connecting smart contracts to web applications
Designing the setup form
Implementing the setup function
Summary
Tools, Frameworks, Components, and Services
Using developer tools
Development frameworks
Integrated development environments
Testnet faucets
Understanding Ethereum communication tools
Frontend Ethereum APIs
Backend Ethereum APIs
Application binary interface tools
Making sense of the Ethereum infrastructure
Ethereum clients
Ethereum storage
Ethereum messaging
Learning Ethereum testing and securing tools
Understanding monitoring utilities
Using security testing utilities
Understanding auditing tools
Getting important open source libraries
ZeppelinOS
Using the 0xprotocol
Building a relayer
Becoming market makers
Aragon
AragonPM
AragonOS
AragonJS
AragonUI
AragonCLI
Summary
Deployment on Testnet
Using Ropsten for smart contract development
Features of Ropsten
Getting Ropsten Ether
Understanding Rinkeby with  proof-of-authority
Describing proof-of-authority
Getting Rinkeby Ether
Using Kovan for smart contract development
Getting Kovan Ether
Introducing the Mainnet
Summary
Various dApps Integrations
Better React applications
Organizing components properly
Generating components dynamically
Starting up projects faster
Scalable Node.js backends with NGINX
Creating a Node.js server
Getting a hosting solution
Setting up your server on a VPS hosting
Getting a domain name
Setting up NGINX
Adding SSL security
Better web3.js dApps
Setting up a fixed web3.js version
Creating helper functions
Promisifying your functions
Listening to events with web3.js
Building your own Oracles
Building a random generation Oracle
Improving your development workflow
Summary
Decentralized Exchanges Workflow
Introducing decentralized exchanges
Cons of DAXs
Pros of DAXs
Essential trading terms
Understanding the trading and matching engine
Managing cryptocurrency wallets and cold storage
Building the user interface
Building the Ethereum backend
Finishing the dApp
Summary
Section 3: Ethereum Implementations
Machine Learning on the Ethereum Blockchain
Understanding machine learning
Decentralized machine learning marketplaces
Building a smart contract machine learning marketplace
The prediction function 
The cost function
The optimization algorithm
The train function
Summary
Creating a Blockchain-based Social Media Platform
Understanding decentralized social media
The initial concept
Creating the user interface
Configuring the webpack style
Setting up the initial structure
Rendering hashtags
Improving the appearance
Building the smart contract
Planning the design process
Setting up the data structures
Documenting the future functions
Implementing the adding content function
Creating the promotion engine
Implementing the getter functions
Finishing the dApp
Setting up the smart contract instance
Decentralizing your data
Creating the hashtag component
Creating the hashtag getter
Creating the publishing functionality
Summary
Creating a Blockchain-Based E-Commerce Marketplace
Creating the user interface
Planning the marketplace interface
Setting up the index page
Configuring the webpack dev server
Creating the Header component
Creating the Home component
Creating the Product component
Creating the Sell component
Creating the Orders component
Understanding ERC-721 tokens
Explaining the ERC-721 functions
The ERC-721 smart contract
Developing the e-commerce smart contract
Creating the publish function
Creating the buy function
Creating the mark orders function
Creating the getter functions
Deploying the smart contract
Finishing the dApp
Setting up the contract instance
Updating the index file
Updating the Buy component
Updating the Sell component
Updating the Orders component
Summary
Other Books You May Enjoy
Leave a review - let other readers know what you think
Ethereum is one of the most commonly used platforms for building blockchain applications. It's a decentralized platform for applications that can run exactly as programmed without being affected by fraud, censorship, or third-party interference.
This book will give you a deep understanding of how blockchains work so that you can discover the entire ecosystem, core components, and implementations. You will get started by learning how to configure and work with various Ethereum protocols for developing dApps. Next, you will learn how to code and create powerful smart contracts that scale with Solidity and Vyper. You will then explore the building blocks of dApps architecture and gain insights into creating your own dApp through a variety of real-world examples. The book will even explain how to deploy your apps on multiple Ethereum instances with the required best practices and techniques. The next few chapters will delve into advanced topics such as building advanced smart contracts and multi-page frontends using Ethereum. You will also learn how to implement machine learning techniques to build decentralized autonomous applications, in addition to several use cases across a variety of domains such as social media and e-commerce.
By the end of this book, you will have the expertise you need to build decentralized autonomous applications confidently.
This book is for anyone who wants to build fast, highly secure, and transactional decentralized applications. If you are an Ethereum developer looking to perfect your existing skills in building powerful blockchain applications, then this book is for you. Basic knowledge of Ethereum and blockchain is necessary to understand the concepts covered in this book.
Chapter 1, Blockchain Architecture, covers basic blockchain knowledge, which is essential before starting to make sure we understand the requirements that will be used across the book. We will cover the history of Ethereum from the beginning and end up in more complicated topics regarding development.
Chapter 2, Ethereum Ecosystems, shows us the infrastructure and setup of a blockchain. We will conduct a deeper analysis on how blockchains work so that we understand the entire ecosystem. We will also learn about the Ethereum components and their various implementations.
Chapter 3, Ethereum Assets, shows us how to configure and work with various implementations of the Ethereum protocol before developing applications in it. There are several flavors, which can be used interchangeably for development, testing, and deployment. Later, we will look at the Modified Ghost implementation, computation, and Turing completeness to understand the Ethereum workflow better. It is also important to understand and use the tools and services in the Ethereum ecosystem.
Chapter 4, Mastering Smart Contracts, teaches us advanced smart contracts that go beyond normal use cases. It is useful because it will help us understand advanced techniques, methods, and tools to improve our Solidity and Vyper coding skills in order to create powerful smart contracts that scale.
Chapter 5, Mastering dApps, introduces you to the decentralized app (dApp) architecture and its building blocks. Then we will create one dApp step by step from scratch, from a blank application to writing smart contracts, creating crypto wallets, and unit testing them. We will show you how to create specialized UIs for these apps. We will also show you how to use other testing services, such as Truffle, to build seamless dApps. We will then show you how to deploy your apps on multiple Ethereum instances, such as testrpc, private chain, test chain, and main net.
Chapter 6, Tools, Frameworks, Components, and Services, introduces us to several important tools available for Ethereum developers to create powerful dApps and smart contracts that scale and that are secure enough to serve millions of potential users.
Chapter 7, Deployment on Testnet, shows us how to deploy our blockchain applications on testnet. Ethereum blockchain applications can be deployed on Rinkeby-testnet. Testnet provides test gathers, with which developers can test all smart contracts and the behavior of dApps. Gas calculation and other factors can be tested on testnet.
Chapter 8, Various dApps Integrations, moves ahead and shows you how to create more efficient dApps with workflows that will help us to develop code more quickly and safely. We will understand all the aspects related to decentralized web app development including web client code using React; backend code with Node.js, Solidity, or Vyper; and using web3.js for dApp communication.
Chapter 9, Decentralized Exchanges Workflow, shows you how to create complex decentralized exchanges (DAXs) by understanding the intricacies of such complex systems in an easy-to-assimilate language that covers each section in great detail.
Chapter 10, Machine Learning on the Ethereum Blockchain, uses the smart contract in blockchains to automatically validate the solution, so there will be no debate about whether the solution was correct or not. Users who submit the solutions won't have counterparty risk that they won't get paid for their work. Contracts can be created easily by anyone with a dataset, even programmatically by software agents.
Chapter 11, Creating a Blockchain-Based Social Media Platform, goes through the steps required to create a solid decentralized social media platform that uses Swarm for distributed storage of resources, such as videos, while leveraging the trustless nature of the blockchain to guarantee the privacy of each individual.
Chapter 12, Creating a Blockchain-Based E-Commerce Marketplace, goes through the steps required to set up a decentralized marketplace for buying, selling, renting, and exchanging goods, similar to OpenBazaar. We will be build a unique marketplace with scalability in mind.
Chapter 13, Creating a Decentralized Bank and Lending Platform, creates a decentralized bank that uses Ethereum for managing and storing crypto in a safe environment. We will use cold storage for added security and lending systems with collateral default protections in place. This is an online chapter and can be found at the following link: https://www.packtpub.com/sites/default/files/downloads/Creating_a_Decentralized_Bank_and_Lending_Platform.pdf.
You need to have basic knowledge of what blockchains and Ethereum are.
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.packt.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 & Errata
.
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 at https://github.com/PacktPublishing/Mastering-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 at https://github.com/PacktPublishing/. Check them out!
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/9781789531374_ColorImages.pdf.
There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "They use a library called 0x.js that allows you to interact with relayers with a high-level, clean interface that feels great to use."
A block of code is set as follows:
pragma solidity 0.5.0;contract Example { uint256 myStateVariable; string myOtherStateVariable; function example(){ uint256 thisIsNotAStateVariable; }}
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
function example(string memory myText) public {
require(bytes(myText)[0] != 0);
}
Any command-line input or output is written as follows:
$ git clone https://github.com/merlox/dapp
Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Go to the Droplets sections and click on Create Droplet."
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.packt.com/submit-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.
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.
In this section, you'll develop a solid understanding of how to program better smart contracts and dApps while reviewing assumptions to improve the psychology behind code.
The following chapters are included in this section:
Chapter 1
,
Blockchain Architecture
Chapter 2
,
Ethereum Ecosystem
Chapter 3
,
Ethereum Assets
Blockchain architecture covers the fundamentals when it comes to understanding how the blockchain works internally. It is essential to be able to work on different projects that use different areas of Ethereum, because once you have a solid understanding about how everything works together, your mind will begin to see things differently. You will get a high-level overview of what happens in the blockchain when you use it and when you program for it. The moving parts of this complex ecosystem will begin to make so much sense once you go through this chapter, because you'll receive a high-level overview of how smart contracts work and how they are related to the underlying structure.
In this chapter, we will cover the following topics:
Beyond Ethereum
The EEA
Understanding the Ethereum blockchain
A high-level overview of how smart contracts work
Essential smart contract programming
You probably understand what Ethereum is, but just to make sure that we are on the same page, it's important that you have some background knowledge about what Ethereum really is in order to progress further without too many distractions.
Ethereum is, first and foremost, a blockchain. Ethereum is a technology that runs on many computers and provides its consumers with a guarantee that they are trusting a solid system that will work as expected.
A web of thousands of computers connected all over the world are called nodes and they allow others to get the information they need while trusting the code with the goal of decentralizing the internet as we know it.
Why is decentralization so important for the internet? Because we have come to a point where a few big companies control the information that you and I can produce or consume.
Governments have so much power that they are getting out of control with their rules. They are biased toward what benefits them and their governors. And it's understandable—whenever some entity is at the top of the food chain, it is inevitable that they end up controlling the entire system below it sooner or later.
Ethereum's goal is to create a censorship-resistant and open platform that allows people to trust smart contracts that enforce rules that cannot be controlled by third-party entities.
When you publish a smart contract, you have a 100% guarantee that the code will run at any point and nobody will be able to interfere with it, unless the rules of it say so.
Ethereum was described in Vitalik Buterin's 2013 whitepaper, which can be found at: https://github.com/ethereum/wiki/wiki/White-Paper. He talked about the need of a scripting language that would run on top of Bitcoin, since he was involved in Bitcoin Magazine and he understood the limitations of the Bitcoin blockchain.
He saw an opportunity to create a platform that would run on decentralized technology to create new types of applications.
Not many believed in his vision, so he decided to create an entire new blockchain by himself with a small team that saw the potential in Vitalik's ideas. He founded the Ethereum Switzerland group and decided to run an Initial Coin Offering (ICO) in July 2014, where he sold Ether in exchange for Bitcoin, raising a total of about $18 million dollars.
He created the smart contracts technology, which is basically programs that run by themselves without requiring a trusted entity to execute them. They are always available, and they run without failure.
The fact that Ethereum provided a system that allows people to create their own applications on top of a blockchain is what made it successful. Before Ethereum, there was no simple way to create decentralizedapplications (dApps) in a decentralized platform. Bitcoin has a protocol to create simple applications using opcodes with a programming language called Script, but it's not capable of much since it is very low level and it's limited by the block size.
The development of Ethereum was planned to be done in four different stages, with major changes in each one:
Frontier
Homestead
Metropolis
Serenity
It was used to deliver and research innovative solutions as they were required, with a hard fork for functionality that is not backward compatible. In 2015, Frontier was launched as the first version of Ethereum. A year later, Homestead was launched, which included many improvements and made Ethereum a capable system with enough power to process smart contracts.
One of the biggest ICOs conducted on top of Ethereum was the decentralized autonomous organization ICO, also known as the DAO, which raised $150 million dollars with contributions from more than 11,000 people. The problem is that it got hacked by an unknown group of individuals that moved the funds to a different DAO. Interestingly enough, a group of programmers known as the White Hat Group saw the hack happening and extracted as many funds as possible into a separate decentralized organization known as the White Hat DAO, where they stored people's money to distribute them later.
This event originated a heated debate in the community that caused Ethereum to be divided in two groups, where some believed in the fact that Ethereum must be immutable and shouldn't be modified, while others believed in a hard fork to revert the damage done.
That was the beginning of Ethereum Classic and Ethereum as we know it. Ethereum Classic has a noticeable smaller user base, but it preserves the initial immutability ideals that they consider essential for a blockchain. In March 2017, several companies joined efforts to create the Ethereum Enterprise Alliance (EEA), which is currently a non-profit organization made of more than 500 members whose goal is as follows:
In other words, they created a group of people collaborating on solutions for future blockchains to come, so that they are better, faster, and more capable.
It suffered from several hacks, where millions of dollars were stolen. They had to do a hard fork to save people's funds and have a notorious price volatility, but the future looks bright and it continues, improve as demand increases.
The EEA (entethalliance.org) is one of the most exciting projects being developed by the core Ethereum team, because they intend to help companies from all over the world to benefit from decentralized technology. By learning about this project, you'll be well positioned when it comes to working as an EEA expert.
In this section, we will cover the following topics:
The EEA's vision
The EEA membership
The EEA architecture
Vitalik funded the organization because he received a huge demand from executives to create software that could be used in big companies to handle demanding dApps. Those companies now want to build a private version of Ethereum to fulfill their research and development needs.
What's interesting about this initiative is that they work with hundreds of companies to research and develop solutions that are shared across them. For instance, if a company member of the EEA creates a new implementation protocol for better and faster dApps, they will share it with the other members so that they can also benefit from this cutting-edge research, while together growing the Ethereum ecosystem.
The EEA's four big public goals that they envision they will achieve in the longer term are as follows:
Be an open source standard, not a product
:
They only work with open source code that can be shared with anybody publicly without restrictions in order to spread development advances that may or may not help others improve their blockchain products. You see, they are a non-profit organization that wants to move blockchain (as we know it) further by combining the efforts of many companies interested in private blockchain solutions.
Address enterprise development requirements
:
The EEA helps companies incorporate the new innovations that are being discovered by others for free, so that they can enjoy the benefits of the latest requirements.
Share improvements between public and private Ethereum
:
They want to improve what they are building by taking improvements from the public blockchain so that they can evolve faster while keeping a great product in mind.
Leverage existing standards
:
When it comes to the blockchain technology, much is left to research and discover. Many problems regarding scalability, security, and transparency are being studied, since this type of decentralized structure is new to modern computing. So, the idea is to learn from existing standards, such as proof-of-stake (PoS), to improve faster than anyone else.
In essence, they are trying to fulfill the demands that many companies are making regarding private enterprise and fast blockchains for their personal applications.
What's interesting about the EEA is that any company is free to join by just filling a form on their website to become a member of the group for a yearly cost. This openness helps many individuals stay relevant with the new improvements on the Ethereum blockchain.
Here's the breakdown in yearly costs to become a member:
Less than 50
employees
: $3,000 per year
Between 51 and 500 employees: $10,000 per year
Between 501 and 5,000 employees: $15,000 per year
More than 5,000 employees: $25,000 per year
Non-profit organizations only pay $3,000 dollars per year, regardless of company size. By becoming a member of the Alliance, you can enjoy a series of general benefits, such as being able to participate in discussions, voting, open source code, meetups, and the prestige that you get from having the EEA logo in your website. As a big blockchain company, it makes sense to become a member just for the reputation that you'll get by becoming a member with the EEA logo on your website.
The benefits depend on the type of company you are in, and you can see them in the following section:
Class A members are the directors that run the companies associated with the Alliance. They have priorities when reaching decisions and have access to all the benefits.
Class B members are those that have a normal company, so they get standard benefits without exclusiveness.
Class C members are lawyers and legal firms that are in charge of the legal challenges when it comes to the research done in the EEA.
Class D members are non-profit companies and academic institutions. They get basic benefits for the lowest price without voting rights.
The EEA has a powerful pool of about 500 companies, including giants such as Intel, Microsoft, and J.P. Morgan. If you run a decent-sized company, you may be interested in becoming a member of the EEA just to be a part of this revolution when it comes to enterprise blockchains.
The enterprise grade applications built with EEA's tools have a very interesting structure that builds on top of the existing Ethereum structure. They developed the Enterprise Ethereum Architecture Stack (EEAS), which is a design that specifies how this new type of Ethereum blockchain should work in a private setting, with features designed for privacy. The team working on the Ethereum Enterprise project details functions of this type of private blockchain without regard to the underlying technology below it, such as software code, APIs, and communication protocols. What the EEA intends to do is focus on creating what's described in their specification so that people can enjoy private blockchains for their companies.
The tooling will benefit from innovative solutions exclusive to the Enterprise Ethereum blockchain, such as the following:
Hardware security module
(
HSM
): This is a physical computing device that safely stores digital keys, such as your private cryptocurrency keys, with maximum security features. For instance, Ledger and Trezor are hardware wallets that are also called HSMs, because they provide security in a physical device for your blockchain private keys.
Permissioning and authentication
: This is useful in order to give users specific roles with limited access to certain areas in a more structured fashion.
Enterprise management systems
: This is used to help companies control their private blockchain internal workings.
Oracles
: These are helpful for communicating with external services for custom smart contracts written on top of the enterprise private blockchain. They are essential to exchange key information with the outer world.
Regarding privacy and scaling, we know that many different teams are working on creating unique solutions that could benefit the entire community. However, we know that, initially, the blockchain will use two main systems to scale the capacity of existing dApps:
Off-chain transactions with trusted execution
: Protocols such as plasma and state channels are being developed to leverage off-chain transactions that can be executed locally by users' computers to reduce the computing load of the main blockchain.
Private transactions
: Transactions that use zero-knowledge proofs, ring signatures, and many other famous protocols to guarantee the privacy of the data being exchanged with the public blockchain. This is an important aspect that companies demand because they have private data and internal processes that can't be shared publicly. We cannot know which solution they will choose, so it's up to them to decide. They will also implement private code execution that will allow companies to execute certain smart contract transactions in a secure environment, where the users will see an encoded hash.
The EEA is building three additional elements on top of the existing core blockchain:
On-chain private state
: This is a separate storage compartment where they will store private states of smart contracts. It will provide us, the developers, truly private variables and store functions that we'll be able to use for keeping information secure and unseen by the public. It's very powerful for enterprise-grade dApps.
Trusted execution
: These are systems that will provide a trusted execution environment where the code will be executed without interfering with public processes.
Private consensus
: Companies will have the ability to reach agreements using their own private blockchain for their own systems, similar to voting systems that we can see in the
Decentralized autonomous organizations
section in:
Chapter 3
,
Ethereum Assets
.
Lastly, the blockchain network layer will benefit from Enterprise P2P, which will be used to exchange transactions in smaller networks of nodes that are set up by the company, so that they enjoy the benefits of faster processing speeds and confirmation times. Enterprise blockchains will be able to interact with other networks, including the public blockchain to exchange information.
The Ethereum blockchain is a complex system made of several important components that work together to achieve an impressive platform that everybody can use to create unstoppable applications. You'll learn the intricacies of the internal workings of the blockchain to gain a greater understanding about how it's made.
At the most basic level, the Ethereum blockchain is a set of connected blocks that contain information that many nodes share so that there's an unchangeable data structure to keep information permanently. The goal of any blockchain is to preserve information without the possibility of changing it or deleting it to avoid censorship or manipulation by external entities.
As such, the Ethereum blockchain builds upon that concept by implementing several well-known tools, thanks to Bitcoin and previous research to create programs that run on top of those blocks.
To understand how the Ethereum blockchain works internally, we have to understand each component that makes up a blockchain. First, I'll give you a high-level overview of the blockchain as a set of nodes, and then we'll go through each component from higher to lower levels in the pyramid that makes the blockchain.
Ethereum is a decentralized platform, which means that two or more nodes work together in a coordinated fashion to achieve a common outcome in a way that the end user sees as a single operation. A node performs various types of functions depending on the role it decides to take. It can propose and validate transactions with mining software to achieve consensus while securing the blockchain using the proof-of-work(PoW) protocol or be a light-weight node that performs payment verification and many other tasks that can be done with limited resources.
The Ethereum blockchain is based on Bitcoin, since the system created by Satoshi Nakamoto is a very robust decentralized solution. In fact, it uses a system to execute decentralized code using opcodes that can process basic instructions on top of a network of hundreds of thousands of computers in a safe manner. They must be simple to preserve security above all.
Although both blockchains are quite similar in their systems, they have noticeable differences, such as the following:
Bitcoin and Ethereum use PoW to generate blocks, while Ethereum intends to move to PoS as the block generation system in the future to avoid wasting computational power.
Ethereum uses the Ethash algorithm, while Bitcoin uses SHA256 for processing transactions and generating blocks.
Ethereum's block time is about 15 seconds, which is about 100 times faster than Bitcoin's. This gives people better confirmation times.
The Ethereum blockchain is decentralized and distributed, which means that the connected nodes have several relationships with each other independently, while running the same software at different locations. This is very important to guarantee the unstoppable aspect of the blockchain. Here what each term means:
Decentralized network
: This is a network of computers running without a single point of control, no entity controls the entire system, and e
ach node is connected to other nodes directly in a
peer-to-peer
(
P2P
) relationship
. If the blockchain wasn't decentralized, some government or entity could locate the main controller node and stop the entire system immediately.
Distributed network
: This is a network where many different computers run on different locations with the same software. If the blockchain wasn't distributed, some entity could go to where all the nodes are located and stop the entire operation because they are all in the same place, so none are safe from such an attack.
You can see in the following diagram how the structure of these types of technologies connect nodes together so that they can communicate with complete security, in their own way, since it's often a confusing point when trying to understand the differences between distributed and decentralized systems:
Those two characteristics give the Ethereum network the capacity to work without having to rely on anybody, because thousands of nodes are able to communicate between each other in a secured and independent fashion. Notice that Ethereum is a completely open blockchain, meaning that anybody is free to join and participate in the decisions that take place, such as block creation and transaction processing. It allows people to join the network of nodes as a participant without any requirements, since the entire structure is secured by cryptographically-safe algorithms.
Nodes can work together or become malicious by executing unexpected code. Those that exhibit arbitrary behavior are known as Byzantine nodes. The main goal is to create a system where nodes cooperate even in the worst situations, with fault-tolerance protections in place to avoid breaking the entire structure. But how do they work together if the nodes making the blockchain can behave randomly because they are Byzantine nodes?
At the end of the day, it's a group of computers working voluntarily toward the same objective. Nothing stops them from doing unexpected actions. That's where one of the most interesting problems that blockchain is facing lies. There are several solutions, but research is still being done to obtain a perfect balance between performance and security in a decentralized system.
The CAP theorem, introduced originally by Eric Brewer in 1998, states that any distributed system cannot have all three of these properties simultaneously:
Consistency
: This is a capability of the network of nodes to keep a consistent copy of the blockchain data in their computers at the same time.
Availability
: This means that the system of connected nodes is online and available at all times while being accessible by all users to respond to requests without failure when required to do so.
Partition tolerance
: If a group of nodes of the entire network stops working or loses connection for any reason, the system should not be affected, and it should continue working properly as if nothing happened.
It has been proven that a distributed or decentralized system cannot have all three of these properties at the same time, which is interesting because Ethereum, just like Bitcoin, achieves all of them at what appears to be the same time. The truth is that consistency is not achieved at the same time as partition tolerance and availability, but at a later time. You see, in order to have order in such a diverse variety of computers, we can only hope to achieve the same level of data over time whenever the blockchain grows at an established pace. It lags behind the others. This is called eventual consistency, where the goal is achieved as a result of validating multiple nodes over time. For this reason, the concept of mining was introduced in Bitcoin and Ethereum to agree on a consensus with the PoW protocol.
Eventually, Ethereum plans to move to PoS, which consists of the idea that a node or user maintains a stake, a certain number of Ether or any type of valuable investment in the system, so that the negative consequences of any malicious activity would outweigh the benefits of attacking the network.
For instance, if I want to become a miner to earn some Ether in exchange for my time and resources, I have to lock 100 Ether in a smart contract that runs the PoS protocol. If I decide to validate invalid blocks or transactions, and someone notices my malicious behavior via several security mechanisms, I would lose all those 100 Ether and wouldn't be able to attack again. The reward for processing a block successfully would be a percentage of the resources invested, for example, 0.1 ETH. This forces nodes to cooperate and act responsibly to avoid losing a big stake, even if they agree to attack the system.
Alternatively, delegated proof-of-stake (DPoS) could be used in later versions of Ethereum. It consists of delegating the validation of a transaction to other nodes by voting. It is used in the BitShares blockchain.
The Ethereum Virtual Machine (EVM) is a virtual machine that allows code to be executed with limitations regarding gas costs and price, where each individual interacting with it must pay a fee to protect the network from spamming attacks, so that many decentralized nodes can interact with each other using the same software. It processes bytecode that gets generated with assembly code, which, in turn, uses instruction called operational codes (opcodes). It's a Turing complete computer.
When I say Turing complete, I mean that the smart contract programming languages running on top of Ethereum have the following properties:
They have unlimited access to
random access memory
(
RAM
)
They can make decisions based on the information available in memory
They can run forever with the help of
while
,
for
, and
recursive
loops
They can use functions
What this means, is that smart contracts are capable of executing any operation that you give them, given enough time and resources. This is important to understand to avoid confusions when someone says that Ethereum is a Turing complete blockchain.
The state machine is a mechanism that keeps track of the state changes that occur on the blockchain. For instance, a normal day has two simple states, either day or night. A state machine would record the situation of each day at every moment so that when the sun goes down, the state of the day changes to night. It is the same thing with the days of the week. Each day can be one out of seven different states, such as Monday or Friday. Whenever it changes at 12 am, the state that keeps track of the day of the week gets updated in the state machine.
The state machine enforces consensus rules to make sure that users are processing valid transactions in a Byzantine resistant system:
A P2P network
: It connects the participants and propagates the transactions and blocks of verified transactions. This is the network used by the nodes of the blockchain to propagate information between them to achieve consensus.
An incentivization scheme
: In the case of Ethereum, that scheme is PoW for creating an economically secure state machine. Ethereum developers plan to move to a PoS system where the users will process transactions using a passive system of transaction verification based on the number of ETH that the miner locks at that moment.
An open source client
: This client is the one used by the nodes to interact with the blockchain. In Ethereum, we have Geth, Parity, and many others that allow you to connect to the blockchain for mining and processing transactions and all sorts of tasks after downloading the blockchain.
The blockchain runs on top of a P2P network, where nodes are connected to each other to exchange data and state updates. Because of this technology, we are able to interact directly with other computers in order to process orders so that we all agree on the block generation system. It allows miners to be rewarded for completing PoW challenges.
When we say consensus, we talk about a group of techniques used in systems with many participants to agree on decisions that benefit the whole underlying system. While voting gives the power to decide to a few selected individuals that fulfill a set of requirements, consensus takes in consideration each and every one of the participants to agree on the global way of thinking.
Each algorithm that implements any form of consensus when it comes to blockchain technology must provide the following features:
To agree on decisions that benefit the whole system
: The idea is to take choices that not only benefit the individual, but the entire network, so that everybody has a better platform.
To allow open participation
: Every person should be completely free to join and make decisions that they believe will be positive.
To be secure enough so that malicious actors can't prejudice the system
: All the consensus agreements have to be toward the betterment of the systems, where malicious users can't have enough power to decide for many others.
The problem with reaching consensus started with what's known as the Byzantine Generals Problem, a problem that consists on the fact that many computers can't easily agree on a predetermined order. Some computers will receive the order late, others will ignore it because they don't benefit from fulfilling it, while others will follow the order as best as they can.
In essence, consensus rules are mandatory to achieve a global state that all agree with, while being rewarded for participating in a beneficial manner in the decision-making process.
This is a method to guarantee that the result of a task was hard to achieve. Why do we want to make processing tasks difficult? To reward those that are best at completing it. In a blockchain, it costs processing power, which is just hardware, energy, and time, to process transactions made by individuals using the network. It is used to generate blocks in a simple process:
The miner proposes a new block that contains a hash number made of the header of the most recent block and the nonce counter.
Then, the miner compares the hash to the target value which is determined by the mining difficulty.
If the hash is of the target difficulty, the user gets rewarded with the solution by getting ETH. If not, the nonce gets incremented until a hash is generated with the desired solution.
Since the miner will be competing with many others, there must be a system to make sure that the block times are consistent, because we want to keep generating blocks with the same periodicity even when new nodes join the mining network. To guarantee similar block times, the mining difficulty was created.
PoS is a new algorithm to reach consensus between nodes in a decentralized blockchain that focuses on removing the high computing performance requirements from PoW while still keeping the network safe from attacks and malicious behavior. The way it works is based on locking Ether as a stake and validating blocks with the risk of losing your stake if you misbehave. Here's the process:
People that want to participate in the PoS process are called
validators
. They start by locking a specific number of coins as their stake (for instance, 100 Ether) that they can't touch while they are mining. It should be expensive enough to guarantee that groups of people don't agree on attacking the system with the risk of losing their stake if they're unsuccessful.
Then, they start receiving transactions from people using the Ethereum blockchain. Validators run programs to validate that the transactions they are receiving are valid so that they can create new blocks by grouping transactions in order. When they have enough transactions to cover the gas limit per block, they place bets on those blocks – for instance, 20 Ether from the initial 100 Ether staked.
The block with the most bets is selected as the winner.
When the winning block is selected from the pool of generated blocks, the miners that bet on that valid block get a percentage of the total gas used in that block based on their bets. They are not paid in block rewards but in transaction fees. For instance, if the block selected has 100 transactions accumulating a total of 0.5 Ether in transaction costs, that 0.5 Ether will have to be distributed between all the validators that bet on that block. If that block has a sum bet of 1,000 Ether from 10 users and you bet 20 ether, you'd get a 2% of 0.5 Ether, since that's how much you bet for that block. In total, you'd earn 0.01 Ether.
You don't lose any money when betting for blocks, it's just an indicator of how much you trust the validity of that block. It may seem a small reward considering that there aren't block rewards, just fees, but you must consider that these blocks are being generated in a matter of seconds. Maybe even one second per block, which ends up generating a lot of money over the day.
It looks great in theory, but there's a major roadblock that's stopping PoS from being completely viable. It's called the Nothing at Stake problem and is shown in the following scenario, where there is a main chain and a new chain being created with PoS:
When it comes to PoW, you can mine on any fork that you desire without any risk, since you can place bets on every block that looks good to you. What's stopping you from deciding to put all your eggs in a new chain, thus generating a hard fork?
If we were using PoW, you couldn't move to a new chain without having to spend lots of processing power and time generating new blocks for as long as people decide to accept it as a new blockchain. You'd be mining for many months with a very slim chance of creating a new chain that could be accepted as a new coin, worth much less money because of its reduced use.
But with PoS, you can simply diversify your portfolio and bet on all the blocks that look appealing without consequences, since you won't be losing your stake as long as you're validating good transactions. You'd be generating blocks on several different blockchains without risks. This kind of possibility could end up creating hundreds of different blockchains, since people could be mining on all of them simultaneously. That's why it's called the Nothing at Stake problem, because you don't lose anything when participating in the mining process.
This is one of the main reasons why Ethereum has been unable to switch fully to PoS in recent times.
Researchers at Ethereum have been working on solving this problem for a long time. One of the proposals, known as Casper, intends to solve it by punishing users that mine on new chains mindlessly by taking their stake out of them, so that users focus on betting for the longest chain. Casper also punishes nodes that don't actively participate on the network so that they don't start consuming resources without providing value.
At the end of the day, PoW has to improve or change completely to become sustainable, given that many large mining pools are starting to gain too much power. We are at a point where four mining companies can achieve about 60% of the total transaction power, giving them the choice to run a 51% attack and force all users to mine on their own terms with their own corrupted blockchains. PoS is here to stay and bring balance back once again.
Smart contracts are blockchain applications that can execute code and do a diverse variety of tasks. These are programs that the miners execute when mining blocks. They are secure and unstoppable agreements that are automatically executed and enforced. You are probably familiar with them, since this book is aimed at Ethereum developers; however, if you don't know how to use them or if you want to learn more about them, you can rest assured that you'll understand everything because of detailed explanations that you'll be given at the appropriate times.
Unlike traditional paper contracts, smart contracts don't have real-world legal consequences on Ethereum, and they can't be enforced by a legal entity when they break. Instead, they rely on the principle that code is law, which means that the code is the one governing the behaviors of the contract. You can't execute functions that are not defined in the smart contract and you must respect each function's requirements.
What's interesting about smart contracts is that they allow the creation of unstoppable applications that will keep the data and functionalities running on top of the blockchain regardless of whether the underlying web interface is not available or is censured for whatever reasons. Smart contracts open the world to a new type of application that you can deploy and forget, knowing that it will work under any circumstances.
There's an ongoing debate about whether the code is acceptable as a real contract in a court of law. One side thinks that smart contracts have to go further and be enforced legally for a more general view of applications, while the other side thinks that the code as law is enough to guarantee the security and enforcement of the agreements.
Smart contracts are programs that run on top of the blockchain in the software layer. They allow developers to create decentralized trustless programs that have certain functions for managing transactions between individuals. Instead of relying on a centralized server, they are the database and the server in decentralized applications.
Inherently, smart contracts must be limited in their capacity given that we are executing code that will stay as a transaction on the blockchain permanently, meaning that every function you execute gets registered in the blockchain without having the option to undo what's been done. This imposes some natural limitations, since you can't just modify the public database that every node of the Ethereum will have to download without some restrictions. Every change has to be downloaded by all the peers of the network.
