- CATALOG -
Happy Coding
读 Mastering Ethereum

(笔记迁移 @ 2020年)

区块链上不应该只有数字货币。

Ethereum was conceived at a time when people recognized the power of the Bitcoin model, and were trying to move beyond cryptocurrency applications.

Ethereum’s founders were thinking about a blockchain without a specific purpose, that could support a broad variety of applications by being programmed.

Ethereum has memory that stores both code and data, and it uses the Ethereum blockchain to track how this memory changes over time.

目录

  1. What Is Ethereum? https://github.com/ethereumbook/ethereumbook/blob/develop/01what-is.asciidoc
  2. Ethereum Basics https://github.com/ethereumbook/ethereumbook/blob/develop/02intro.asciidoc 【metaMask转账,在remix上smart contract开发、使用】
  3. Ethereum Clients https://github.com/ethereumbook/ethereumbook/blob/develop/03clients.asciidoc 【安装节点,还有一些操作,需要等到数据同步完成】
  4. Cryptography https://github.com/ethereumbook/ethereumbook/blob/develop/04keys-addresses.asciidoc 【密码学 公私钥】
  5. Wallets https://github.com/ethereumbook/ethereumbook/blob/develop/05wallets.asciidoc 【for user-facing】
  6. Transactions https://github.com/ethereumbook/ethereumbook/blob/develop/06transactions.asciidoc 【开始接触smart contract】
  7. Smart Contracts and Solidity https://github.com/ethereumbook/ethereumbook/blob/develop/07smart-contracts-solidity.asciidoc

solidity文档是学习智能合约的好参考。https://solidity.readthedocs.io/en/v0.5.5/

笔记

两种账户类型:

  1. The type of account you created in the MetaMask wallet is called an externally owned account (EOA)
  2. A contract account has smart contract code, which a simple EOA can’t have. Furthermore, a contract account does not have a private key. Instead, it is owned (and controlled) by the logic of its smart contract code: the software program recorded on the Ethereum blockchain at the contract account’s creation and executed by the EVM.

节点分两种:

  1. full node client (需要大磁盘服务器)
  2. remote client/ wallet (e.g., MetaMask)

安装Ethereum节点。不安装也行,安装MetaMask插件后,每个chrome tab都有了web3的上下文。可以使用web3执行创建合约,转账等操作。

以安装Geth为例(Ethereum)。

下载源代码

git clone git@github.com:ethereum/go-ethereum.git

OR

go get -d github.com/ethereum/go-ethereum

编译并配置文件

cd $GOPATH/src/github.com/ethereum/go-ethereum

make geth

geth dumpconfig > geth.toml

# modify geth.toml if nesserary

geth --config geth.toml

reference:

  1. https://github.com/ethereumbook/ethereumbook/blob/develop/03clients.asciidoc
  2. https://github.com/ethereum/go-ethereum/blob/master/README.md

以太坊的transaction应该是多了个data字段。除了金额,还能承载一些东西。

A transaction with only value is a payment. A transaction with only data is an invocation. A transaction with both value and data is both a payment and an invocation. A transaction with neither value nor data—well that’s probably just a waste of gas! But it is still possible.

image-20200625085657502

MetaMask利用的是Web3的能力。打开chrome console就能拿到上下文。直接使用Web3的能力。

Web3 JS API文档: https://github.com/ethereum/wiki/wiki/JavaScript-API

智能合约,如果转账地址是contract address,非EOA, data字段就会被EVM解析成合约调用。

For now, let’s assume your transaction is delivering data to a contract address. In that case, the data will be interpreted by the EVM as a contract invocation. Most contracts use this data more specifically as a function invocation, calling the named function and passing any encoded arguments to the function.

data字段

image-20200625085721182

image-20200625085734912

image-20200625085741766

  1. function selector = 方法hash的前4个字节,
  2. function arguments = hex(input argument)
  3. payload = pad32(function selector, function arguments)

往合约地址,包含data的交易,只是执行合约。

往0x0地址(除了合约地址、EOA地址,所以0x0是第三种账户),包含data的交易,是合约的创建。

向非EOA地址,发送value,就是销毁ETH。

安装solc(编译合约代码成二进制)

https://solidity.readthedocs.io/en/v0.4.21/installing-solidity.html

solc --bin Faucet.sol


src = web3.eth.accounts[0]

faucet_code = ...

web3.eth.sendTransaction({from: src, to: 0, data: faucet_code, gas: 113558, gasPrice: 200000000000}, function(error, result){
   if(!error)
       console.log(JSON.stringify(result));
   else
       console.error(error);
})

Last modified on 2018-10-29