- 中文翻译 https://github.com/inoutcode/ethereum_book
- 英文:https://github.com/ethereumbook/ethereumbook
- Outline
- Preface
- Chapter 1 What Is Ethereum?
- Compared to Bitcoin
- Components of a Blockchain
- A peer-to-peer (P2P) network connecting participants and propagating transactions and blocks of verified transactions, based on a standardized “gossip” protocol
- Messages, in the form of transactions, representing state transitions
- A set of consensus rules, governing what constitutes a transaction and what makes for a valid state transition
- A state machine that processes transactions according to the consensus rules
- A chain of cryptographically secured blocks that acts as a journal of all the verified and accepted state transitions
- A consensus algorithm that decentralizes control over the blockchain, by forcing participants to cooperate in the enforcement of the consensus rules
- A game-theoretically sound incentivization scheme (e.g., proof-of-work costs plus block rewards) to economically secure the state machine in an open environment
- One or more open source software implementations of the above (“clients”)
- The Birth of Ethereum
- [[Ethereum Prehistory]]
- And on July 30, 2015, the first Ethereum block was mined.
- Ethereum’s Four Stages of Development
- Ethereum: A General-Purpose Blockchain
- Ethereum’s Components
- Ethereum and Turing Completeness
- Turing Completeness as a “Feature”
- Implications of Turing Completeness
- From General-Purpose Blockchains to Decentralized Applications (DApps)
- Smart contracts on a blockchain
- A web frontend user interface
- A decentralized (P2P) storage protocol and platform
- A decentralized (P2P) messaging protocol and platform
- The Third Age of the Internet
- Ethereum’s Development Culture: move fast and break
- Why Learn Ethereum?
- What This Book Will Teach You
- Chapter 2 Ethereum Basics
- Ether Currency Units
- 1 ether = 10^8 wei
- Choosing an Ethereum Wallet
- Control and Responsibility
- Getting Started with MetaMask
- Introducing the World Computer
- Ethereum Virtual Machine (EVM)
- Externally Owned Accounts (EOAs) and Contracts
- A Simple Contract: A Test Ether Faucet
- Ether Currency Units
- Chapter 3 Ethereum Clients
- Ethereum Networks
- Should I Run a Full Node?
- Full Node Advantages and Disadvantages
- Public Testnet Advantages and Disadvantages
- Local Blockchain Simulation Advantages and Disadvantages
- Running an Ethereum Client
- [[geth]]
- [[parity]]
- The First Synchronization of Ethereum-Based Blockchains
- Remote Ethereum Clients
- Ethereum Networks
- Chapter 4 Cryptography
- Keys and Addresses
- Public Key Cryptography and Cryptocurrency
- first published in the 1970s by Martin Hellman, Whitfield Diffie, and Ralph Merkle
- lecture 9: Security and Cryptography from [[The Missing Semester of Your CS Education 2020 1]]
- Private Keys
- Public Keys
- Cryptographic Hash Functions
- Ethereum Addresses
- Ethereum Address Formats
- Inter Exchange Client Address Protocol ICAP
- Hex Encoding with Checksum in Capitalization (EIP-55)
- Chapter 5 Wallets
- Wallet Technology Overview
- Chapter 6 Transactions
- The Structure of a Transaction
- The Transaction Nonce
- 2 funtions
- the usability feature of transactions being included in the order of creation
- transaction duplication protection
- Keeping Track of Nonces
- Gaps in Nonces, Duplicate Nonces, and Confirmation
- Concurrency, Transaction Origination, and Nonces 并行就是很困难的
- 2 funtions
- Transaction Gas
- Transaction Recipient
- Transaction Value and Data
- Special Transaction: Contract Creation
- Digital Signatures
- The Signature Prefix Value (v) and Public Key Recovery
- Transaction Propagation
- Recording on the Blockchain
- Multiple-Signature (Multisig) Transactions
- Chapter 7 Smart Contracts and Solidity
- What Is a Smart Contract?
- immutable computer programs that run deterministically in the context of an Ethereum Virtual Machine as part of the Ethereum network protocol—i.e., on the decentralized Ethereum world computer.
- Life Cycle of a Smart Contract
- Introduction to Ethereum High-Level Languages
- declarative or functional language:
- [[Haskell]] SQL
- imperative or procedural language:
- C++ [[java]]
- hybird: they encourage declarative programming but can also be used to express an imperative programming paradigm
- [[python]] [[lisp]] [[javascript]]
- exist languages
- LLL A functional (declarative) programming language, with Lisp-like syntax. It was the first high-level language for Ethereum smart contracts but is rarely used today.
- Serpent A procedural (imperative) programming language with a syntax similar to Python. Can also be used to write functional (declarative) code, though it is not entirely free of side effects.
- [[Solidity]] A procedural (imperative) programming language with a syntax similar to JavaScript, C++, or Java. The most popular and frequently used language for Ethereum smart contracts.
- [[Vyper]] A more recently developed language, similar to Serpent and again with Python-like syntax. Intended to get closer to a pure-functional Python-like language than Serpent, but not to replace Serpent.
- Bamboo A newly developed language, influenced by Erlang, with explicit state transitions and without iterative flows (loops). Intended to reduce side effects and increase auditability. Very new and yet to be widely adopted.
- declarative or functional language:
- Building a Smart Contract with Solidity
- [[Solidity]]
- The Ethereum Contract ABI
- Selecting a Solidity Compiler and Language Version
- Selecting a Solidity Compiler and Language Version
- What Is a Smart Contract?
pragma solidity >=0.6.0;- Programming with Solidity - Data Types - __Boolean (bool)__ - Boolean value, true or false, with logical operators ! (not), && (and), || (or), == (equal), and != (not equal). - __Integer (int, uint)__ - Signed (int) and unsigned (uint) integers, declared in increments of 8 bits from int8 to uint256. Without a size suffix, 256-bit quantities are used, to match the word size of the EVM. - __Fixed point (fixed, ufixed)__ - Fixed-point numbers, declared with (u)fixed__M__x__N__ where __M__ is the size in bits (increments of 8 up to 256) and __N__ is the number of decimals after the point (up to 18); e.g., ufixed32x2. - __Address__ - A 20-byte Ethereum address. The address object has many helpful member functions, the main ones being balance (returns the account balance) and transfer (transfers ether to the account). - __Byte array (fixed)__ - Fixed-size arrays of bytes, declared with bytes1 up to bytes32. - __Byte array (dynamic)__ - Variable-sized arrays of bytes, declared with bytes or string. - __Enum__ - User-defined type for enumerating discrete values: enum NAME {LABEL1, LABEL 2, ...}. - __Arrays__ - An array of any type, either fixed or dynamic: uint32[][5] is a fixed-size array of five dynamic arrays of unsigned integers. - __Struct__ - User-defined data containers for grouping variables: struct NAME {TYPE1 VARIABLE1; TYPE2 VARIABLE2; ...}. - __Mapping__ - Hash lookup tables for __key__ => __value__ pairs: mapping(KEY_TYPE => VALUE_TYPE) NAME. - __Time units__ - The units seconds, minutes, hours, and days can be used as suffixes, converting to multiples of the base unit seconds. - __Ether units__ - The units wei, finney, szabo, and ether can be used as suffixes, converting to multiples of the base unit wei. - Predefined Global Variables and Functions - Contract Definition - contract - interface - library - Functions
javascript
function FunctionName([parameters]) {public|private|internal|external}
[pure|view|payable] [modifiers] [returns (return types)]- Contract Constructor and selfdestruct
javascript
pragma ^0.4.22
contract MEContract {
constructor () {
// This is the constructor
}
}
selfdestruct(address recipient);
- Adding a Constructor and selfdestruct to Our Faucet Example
```javascript
// Version of Solidity compiler this program was written for
pragma solidity ^0.6.0;
// Our first contract is a faucet!
contract Faucet {
address owner;
// Initialize Faucet contract: set owner
constructor() {
owner = msg.sender;
}
[...]
}
// Contract destructor
function destroy() public {
require(msg.sender == owner);
selfdestruct(owner);
}```
- Function Modifiers
```javascript
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function destroy() public onlyOwner {
selfdestruct(owner);
}```
- Contract Inheritance
- Error Handling (assert, require, revert)
- Events
```javascript
contract Faucet is Mortal {
event Withdrawal(address indexed to, uint amount);
event Deposit(address indexed from, uint amount);
[...]
}
// Give out ether to anyone who asks
function withdraw(uint withdraw_amount) public {
[...]
msg.sender.transfer(withdraw_amount);
emit Withdrawal(msg.sender, withdraw_amount);
}
// Accept any incoming amount
receive () external payable {
emit Deposit(msg.sender, msg.value);
}```
- Calling Other Contracts (send, call, callcode, delegatecall)
- Addressing an existing instance
- Raw call, delegatecall
- Gas Considerations
- Chapter 8 Smart Contracts and Vyper
- Vulnerabilities and Vyper
- Finding The Greedy, Prodigal, and Suicidal Contracts at Scale https://arxiv.org/abs/1802.06038
- __Suicidal contracts__ Smart contracts that can be killed by arbitrary addresses
- __Greedy contracts__ Smart contracts that can reach a state in which they cannot release ether
- __Prodigal contracts__ Smart contracts that can be made to release ether to arbitrary addresses
- Comparison to Solidity
- Modifiers: no modifiers in Vyper
- Function and Variable Ordering
- Compilation
- Protecting Against Overflow Errors at the Compiler Level
- Reading and Writing Data
- Chapter 9 Smart Contract Security
- Security Best Practices
- Security Risks and Antipatterns
- Chapter 10 Tokens
- Chapter 11 Oracles
- Chapter 12 Decentralized Applications (DApps)
- Chapter 13 The Ethereum Virtual Machine
- Chapter 14 Consensus
- Links
- https://remix.ethereum.org/ 在线编译
- Truffle
- Truffle is a development environment, testing framework and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier.
- https://github.com/trufflesuite/truffle