Building Blockchain Apps This page intentionally left blank 99778800113355990077881188__bbooookk..iinnddbb xxxxiiiiii 1100//2222//1199 99::3366 PPMM Building Blockchain Apps Michael Juntao Yuan Boston • Columbus • New York • San Francisco • Amsterdam • Cape Town Dubai • London • Madrid • Milan • Munich • Paris • Montreal • Toronto • Delhi • Mexico City São Paulo • Sydney • Hong Kong • Seoul • Singapore • Taipei • Tokyo Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at [email protected] or (800) 382-3419. For government sales inquiries, please contact [email protected]. For questions about sales outside the U.S., please contact [email protected]. Visit us on the Web: informit.com/aw Library of Congress Control Number: 2019950977 Copyright © 2020 Pearson Education, Inc. Cover illustration by dencg/Shutterstock All rights reserved. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechani- cal, photocopying, recording, or likewise. For information regarding permissions, request forms and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearson.com/permissions. ISBN-13: 978-0-13-517232-2 ISBN-10: 0-13-517232-2 ScoutAutomatedPrintCode To Tony and Ju This page intentionally left blank 99778800113355990077881188__bbooookk..iinnddbb xxxxiiiiii 1100//2222//1199 99::3366 PPMM Contents Foreword xvii Acknowledgments xix About the Author xxi Part I Introduction 1 1 An Introduction to Blockchain 3 The Blockchain 3 The Collaborative Ledger 4 Cryptocurrency 5 Smart Contracts 5 A Trustless Network 6 New Ways of Collaborating 7 The Fat Protocol 7 In Code We Trust 8 Conclusion 9 2 Reaching Consensus 11 What Is Blockchain Consensus? 11 Proof of Work (PoW) 12 Proof of Stake (PoS) 12 Delegated Proof of Stake (DPoS) 14 Conclusion 14 3 Your First Blockchain App 15 Smart Contract 16 Front-End HTML 19 JavaScript and web3.js 19 In Action 21 Share Your Dapp 22 Conclusion 22 vviiiiii CCoonntteennttss Part II An Introduction to Ethereum 23 4 Getting Started 25 The BUIDL Way 26 Ethereum Mainnet 26 Ethereum Classic Mainnet 28 CyberMiles Mainnet 29 The Hard Way 29 Metamask Wallet 29 Remix 32 Web3 37 Conclusion 41 5 Concepts and Tools 43 Ethereum Wallet and Basic Concepts 43 Etherscan 45 The TestRPC 46 Interacting with Ethereum via GETH 48 Interacting with Ethereum via web3 49 Running an Ethereum Node 50 Running a Private Ethereum Network 51 Conclusion 53 6 Smart Contracts 55 “Hello, World!” Again 55 Learning Smart Contract Programming 57 Consensus vs. Nonconsensus Code 57 Data Structures 58 Function Parameters and Return Values 58 Payable Functions 58 Calling Other Contracts 59 Building and Deploying the Smart Contract 59 Solidity Tools 59 The BUIDL Integrated Development Environment (IDE) 60 The Remix IDE 61 Truffle Framework 62 Contents ix Calling Smart Contract Functions 65 The BUIDL IDE 65 The Remix IDE 65 GETH Console 67 A New Language 68 More Smart Contract Languages 71 Conclusion 72 7 Decentralized Applications (Dapps) 73 Dapp Stack 74 The web3 Library 75 External Services 75 Dapp Showcases 76 Uniswap 76 CryptoKitties 77 Gambling Games 78 Interactive Dapps 78 Conclusion 79 8 Alternatives to Dapps 81 JavaScript 81 The Full-Node Wallet 82 Raw Transactions 83 Python and Others 85 Conclusion 86 Part III Ethereum in Depth 87 9 Inside Ethereum 89 What Is Blockchain State? 89 Ethereum State 92 Data Structure 92 Trie (or Tree) 93 Standard Trie 93 Patricia Trie 95 Similarities between the Trie and Patricia Trie 96 Main Difference between the Trie and Patricia Trie 96