ebook img

Ethereum for Dummies PDF

274 Pages·2019·12.19 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Ethereum for Dummies

Ethereum ™ by Michael G. Solomon Ethereum™ For Dummies® Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com Copyright © 2019 by John Wiley & Sons, Inc., Hoboken, New Jersey Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. Ethereum is a trademark of Ethereum Foundation. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit https://hub.wiley.com/community/support/dummies. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com. Library of Congress Control Number: 2019936125 ISBN 978-1-119-47412-8 (pbk); ISBN 978-1-119-47411-1 (ebk); ISBN 978-1-119-47406-7 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 Contents at a Glance Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Part 1: Getting to Know Blockchain and Ethereum . . . . . . . . . . 5 CHAPTER 1: Introducing Ethereum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 CHAPTER 2: Learning about Blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 CHAPTER 3: Exploring Use Cases for Ethereum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Part 2: Setting Up Your Ethereum Development Ethereum™ Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 CHAPTER 4: Examining the Ethereum Ecosystem and Development Lifecycle . . . . . 59 CHAPTER 5: Getting and Configuring Ethereum Development Tools . . . . . . . . . . . . . 77 CHAPTER 6: Establishing an Ethereum Wallet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Part 3: Building Ethereum Distributed Blockchain Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 CHAPTER 7: Building Your First Ethereum Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 CHAPTER 8: Learning about Smart Contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 CHAPTER 9: Writing Your Own Smart Contracts with Solidity . . . . . . . . . . . . . . . . . . . 147 Part 4: Testing and Deploying Ethereum Apps . . . . . . . . . . . . . 173 CHAPTER 10: Testing Ethereum Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 CHAPTER 11: Deploying and Maintaining Ethereum Apps . . . . . . . . . . . . . . . . . . . . . . 191 CHAPTER 12: Integrating Non-Blockchain Apps with Ethereum . . . . . . . . . . . . . . . . . . 205 Part 5: The Part of Tens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 CHAPTER 13: Ten Free Ethereum Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 CHAPTER 14: Ten Design Principles for Distributed Blockchain Apps . . . . . . . . . . . . . 229 CHAPTER 15: Top Ten Ethereum Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Table of Contents INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Foolish Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Icons Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Beyond the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 PART 1: GETTING TO KNOW BLOCKCHAIN AND ETHEREUM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 CHAPTER 1: Introducing Ethereum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Describing Blockchain Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Introducing Ethereum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Exploring Ethereum’s Consensus, Mining, and Smart Contracts . . . . . 11 Buying, Spending, and Trading Ether . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Getting Started with DAO and ICO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Exploring the Ethereum Ecosystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Delving into Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Building Blockchain Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 CHAPTER 2: Learning about Blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Exploring Distributed Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Digging into distributed processing . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Exploring problems with distributed processing . . . . . . . . . . . . . . . 24 Presenting some solutions to distributed processing problems . . . 27 Examining the Bitcoin Solution to the Distributed Dilemma . . . . . . . . 28 Describing Blockchains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Examining blockchain details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Protecting blockchain visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Building Blockchains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Agreeing to add blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Making blocks immutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Reviewing the building process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Keeping all blockchains consistent . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Understanding How Blockchains and Databases Store Data Differently . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Storing data in a traditional database . . . . . . . . . . . . . . . . . . . . . . . . 36 Storing data in a blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Effectively Using Blockchains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Transferring value without trust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Reducing transaction costs by eliminating middlemen . . . . . . . . . . 39 Table of Contents v Increasing efficiency through direct interaction . . . . . . . . . . . . . . . . 40 Maintaining complete transaction history . . . . . . . . . . . . . . . . . . . . . 40 Increasing resilience through replication . . . . . . . . . . . . . . . . . . . . . . 41 Providing transparency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 CHAPTER 3: Exploring Use Cases for Ethereum . . . . . . . . . . . . . . . . . . . . 43 Diving Into Ethereum Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Exploring Financial Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Banking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Creating Ethereum escrow applications . . . . . . . . . . . . . . . . . . . . . . 48 Examining ICOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Establishing Digital Identity Management . . . . . . . . . . . . . . . . . . . . . . . . 49 Managing individual and device identities . . . . . . . . . . . . . . . . . . . . . 50 Reducing fraud and identity theft . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Examining the ERC-725 standard and beyond . . . . . . . . . . . . . . . . . 51 Examining Industry Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Healthcare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Energy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Supply chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Enabling Effective Governance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Tax payment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Government spending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Voting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Policy development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Notary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 PART 2: SETTING UP YOUR ETHEREUM DEVELOPMENT ENVIRONMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Examining the Ethereum Ecosystem and CHAPTER 4: Development Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Exploring the Ethereum Blockchain Block Structure . . . . . . . . . . . . . . . 60 Describing Smart Contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Introducing Solidity, the Language of Smart Contracts . . . . . . . . . . . . . 66 Working with the Ethereum Virtual Machine . . . . . . . . . . . . . . . . . . . . . 67 Fueling Your Code with Gas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Surveying Tools for Developing, Testing, and Deploying Ethereum Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Ethereum blockchain client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Development and testing blockchain . . . . . . . . . . . . . . . . . . . . . . . . . 71 Compiler and testing framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Source code editor/IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Describing the Ethereum Development Lifecycle . . . . . . . . . . . . . . . . . . 73 Introducing Smart Contract Development Tools . . . . . . . . . . . . . . . . . . 74 vi Ethereum For Dummies Getting and Configuring Ethereum CHAPTER 5: Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Examining Why Multiple Ethereum Development Tools Are Available . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Downloading, Installing, and Configuring All the Pieces . . . . . . . . . . . . 79 Installing the blockchain client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Installing the test blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Installing the testing environment . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Installing the IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 CHAPTER 6: Establishing an Ethereum Wallet . . . . . . . . . . . . . . . . . . . . . 95 Unlocking the Secrets of an Ethereum Wallet . . . . . . . . . . . . . . . . . . . . . 96 Examining the Types of Ethereum Wallets . . . . . . . . . . . . . . . . . . . . . . . 96 Sorting out software wallets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Handling hardware wallets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Perusing paper wallets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Choosing an Ethereum Wallet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100 Software wallets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Hardware wallets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102 Paper wallets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Installing MetaMask, an Ethereum Wallet . . . . . . . . . . . . . . . . . . . . . . .104 PART 3: BUILDING ETHEREUM DISTRIBUTED BLOCKCHAIN APPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 CHAPTER 7: Building Your First Ethereum Apps . . . . . . . . . . . . . . . . . . 109 Validating Your Ethereum Development Environment . . . . . . . . . . . . 110 Creating a Truffle project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Editing the Truffle config file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Exploring the Ganache Test Environment . . . . . . . . . . . . . . . . . . . . . . . 113 Designing Simple Smart Contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Coding Your First Smart Contract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116 Running Your First Smart Contract . . . . . . . . . . . . . . . . . . . . . . . . . . . . .118 Writing your code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Compiling your code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .119 Deploying your code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120 Invoking your code’s functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Paying as You Go . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124 CHAPTER 8: Learning about Smart Contracts . . . . . . . . . . . . . . . . . . . . 125 Introducing Supply Chain and Common Challenges . . . . . . . . . . . . . . 126 Describing supply chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Explaining difficulties when implementing a supply chain . . . . . . 127 Table of Contents vii Examining How Blockchain Can Help Resolve Supply Chain Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128 Describing a Blockchain Supply Chain Solution . . . . . . . . . . . . . . . . . . 129 Paying for supply chain services . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Managing assets on the supply chain . . . . . . . . . . . . . . . . . . . . . . . 130 Digging into Solidity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Describing Basic Smart Contract Syntax . . . . . . . . . . . . . . . . . . . . . . . . 133 Declaring valid compiler version . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Commenting your code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Importing external code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Defining your smart contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Handling Data in Solidity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Learning about Computation and Gas . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Exploring Access Modes and Visibility of Smart Contract Functions and Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Controlling Execution Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .144 Handling Errors and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Writing Your Own Smart Contracts CHAPTER 9: with Solidity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Reviewing Supply Chain Design Specification . . . . . . . . . . . . . . . . . . . .148 Payment token smart contract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Supply chain smart contract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Creating New Smart Contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 ERC-20 token interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 ERC-20 token smart contract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Supply chain smart contract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Coding Primary Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 ERC-20 token functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Supply chain functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Using Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163 Defining events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Triggering events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Introducing Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Designing for Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Implementing Minimal Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 PART 4: TESTING AND DEPLOYING ETHEREUM APPS . . . . 173 CHAPTER 10: Testing Ethereum Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Understanding Ethereum dApp Testing . . . . . . . . . . . . . . . . . . . . . . . . 176 Writing tests from the beginning . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Choosing the right test blockchain . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Learning the steps in the testing lifecycle . . . . . . . . . . . . . . . . . . . . 177 Testing for software quality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .177 viii Ethereum For Dummies

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.