以太坊私链教程

2021年2月10日

ganache

Base HD Path: m/44’/60’/0’/0/{account_index}

Base HD Path

HD是Hierarchical Deterministic的缩写,用来形容电子钱包,即Hierarchical Deterministic Wallets。[1]

ganache有钱包的功能,钱包可以管理一系列的地址。钱包需要一个密码(密钥)来开启。输入密码,返回一系列地址,这是一个学问。Path表示derivation path,说明钱包是从密码推导出地址的。我记住我的密码,登录不同的电子钱包软件,推导出的地址必须相同。所以必须有个推导标准,derivation path是这标准的一部分。访问以太坊的默认路径正是m/44’/60’/0’/0。其他货币或区块链的默认路径可见HD Wallets and Derivation Paths: Explained

geth --datadir data account new
INFO [02-18|20:44:54.818] Maximum peer count                       ETH=50 LES=0 total=50
Your new account is locked with a password. Please give a password. Do not forget this password.
Password:
Repeat password:

Your new key was generated

Public address of the key:   0x514679132e1b3e35b4B57D045cf6B0e773c8B76A
Path of the secret key file: data0\keystore\UTC--2021-02-18T12-44-56.832637200Z--514679132e1b3e35b4b57d045cf6b0e773c8b76a

- You can share your public address with anyone. Others need it to interact with you.
- You must NEVER share the secret key with anyone! The key controls access to your funds!
- You must BACKUP your key file! Without the key, it's impossible to access account funds!
- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!

查看版本

> web3.version
'1.2.9'

于是可以找到对应的文档:https://web3js.readthedocs.io/en/v1.2.9/web3-eth.html

如果跟着《Ethereum Development Walkthrough (Part 1: Smart contracts)》,你需要把pragma solidity改成^0.4.22,因为0.4.22才引入了constructor关键字[2]pragma solidity ^0.4.22意思大家都懂,但pragma solidity 0.4.22表示只能用0.4.22[3]

据说web3.eth.accounts被废弃,要改用web3.eth.getAccounts().then(function(acc){ accounts = acc })[4]

> WrestlingInstance.wrestle({from: account0, value: web3.utils.toWei('2', "ether")})
{
  tx: '0x51f2a0190f63b676ebb629e94001086d9b8cd5531798f0e9f3de7ee79af30694',
  receipt: {
    transactionHash: '0x51f2a0190f63b676ebb629e94001086d9b8cd5531798f0e9f3de7ee79af30694',
    transactionIndex: 0,
    blockHash: '0xd2dc4a08e37e210f2093c1eaaa0220cc48bc95b46ade41e627b38de8886a4542',
    blockNumber: 6,
    from: '0x1c462089cb21e890c9776b02b6fb899c5996ea84',
    to: '0x79e0aaf09b2c4b0ed6509eeeace034c6f16f2670',
    gasUsed: 53294,
    cumulativeGasUsed: 53294,
    contractAddress: null,
    logs: [],
    status: true,
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
    rawLogs: []
  },
  logs: []
}

用puppeth生成genesis.json

如果puppeth在Powershell或cmd乱码,可以用Git Bash打开,或者将就着看。

Which accounts are allowed to seal? (mandatory at least one)
> 0xe360A2c8b2639385a365e09B9F81D9C16b43A2ca
> 0x

Which accounts should be pre-funded? (advisable at least one)
> 0xe360A2c8b2639385a365e09B9F81D9C16b43A2ca

 1. Modify existing configurations
 2. Export genesis configurations
 3. Remove genesis configuration
> 2

Which folder to save the genesis specs into? (default = current)
  Will create mynet.json, mynet-aleth.json, mynet-harmony.json, mynet-parity.json
>
INFO [02-18|13:10:00.060] Saved native genesis chain spec          path=mynet.json
ERROR[02-18|13:10:00.060] Failed to create Aleth chain spec        err="unsupported consensus engine"
ERROR[02-18|13:10:00.060] Failed to create Parity chain spec       err="unsupported consensus engine"
INFO [02-18|13:10:00.071] Saved genesis chain spec                 client=harmony path=mynet-harmony.json

What would you like to do? (default = stats)
 1. Show network stats
 2. Manage existing genesis
 3. Track new remote server
 4. Deploy network components
>

初始化区块链
geth –datadir ./data init mynet.json

> eth.account
[“0xe360a2c8b2639385a365e09b9f81d9c16b43a2ca”]
> eth.blockNumber
0
> eth.getBalance(eth.accounts[0])
9.04625697166532776746648320380374280103671755200316906558262375061821325312e+74
> web3.fromWei(eth.getBalance(eth.accounts[0]))
9.04625697166532776746648320380374280103671755200316906558262375061821325312e+56

puppeth预先给0xe360a2c8b2639385a365e09b9f81d9c16b43a2ca充了很多值,看不清楚。我们创建另一个账户方便查看余额。

> personal.newAccount()
Passphrase:
Repeat passphrase:
“0xf42f414d308126073b01d80666a056655a4a3f55”

挖矿

一个矿不一定要包含交易。[5]

genesis.json

截至2021年2月,ethereum没有针对genesis.json的全面的文档,只能看源代码。[6]

{
  "config": {
    "chainId": 15,
    "homesteadBlock": 0, //0启用Homestead hard-fork。https://github.com/ethereum/EIPs/blob/master/EIPS/eip-606.md
    "eip150Block": 0, //0表示启用EIP150 hard-fork。https://github.com/ethereum/EIPs/blob/master/EIPS/eip-150.md
    "eip155Block": 0, //0表示启用EIP155 hard-fork。https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
    "eip158Block": 0, //0表示启用EIP158 hard-fork。https://github.com/ethereum/EIPs/blob/master/EIPS/eip-158.md
    "byzantiumBlock": 0, //0表示启用byzantium hard-fork。
    "constantinopleBlock": 0, //0表示启用constantinople hard-fork。
    "petersburgBlock": 0, //0表示启用petersburg hard-fork。
    "clique": {
      "period": 5,
      "epoch": 30000
    }
  },
  "difficulty": "1",
  "gasLimit": "8000000",
  "extradata": "0x00000000000000000000000000000000000000000000000000000000000000007df9a875a174b3bc565e6424a0050ebc1b2d1d820000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "alloc": {
    "7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" },
    "f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000" }
  }
}

EIP编号不是连续的,它是EIP editor(s?)赋予的。

所有的feature和hard fork都要启用。如果有些没启用,以后万一出问题了就很难查找原因。《Mapping the Most Important Ethereum Forks》画出了homestead分叉、Spurious Dragon(EIP150,EIP155,EIP158)分叉、Byzantium分叉、Constantinople分叉、petersburg分叉。Constantinople分叉和petersburg分叉在相同的区块高度实施,但内容不同,在此不细讲。

clique是一种共识算法(参见https://github.com/ethereum/go-ethereum/tree/e74bd587f730fcdb5a9b625390da8aa85a2cbbc8/consensus)。

Geth的官方文档Private Networks声称“We strongly recommend ‘clique’ for new private network deployments because it is much less resource intensive than proof-of-work.” 但是在clique共识算法上挖矿没有奖励[7]

GPU挖矿
要用 https://github.com/ethereum-mining/ethminer

CPU挖矿

用geth就可以了。

> eth.blockNumber
0
> personal.unlockAccount("0x856631E58A9F2f9cf0C9b680aC0B4407F706C9E9")
Unlock account 0x856631E58A9F2f9cf0C9b680aC0B4407F706C9E9
Passphrase:
true
> miner.start()
null
> miner.stop()
null
> eth.blockNumber
7

看到一不小心就挖了7个矿。

https://web3js.readthedocs.io/en/v1.2.11/web3-eth-personal.html#newaccount

personal.newAccount()

. geth搭建以太坊私链及常用操作. . 2019-07-19 [2021-02-18].

参考资料

  1. MyEtherWallet. HD Wallets and Derivation Paths: Explained. . 2019-04-12 [2021-02-21].
  2. Aniket. Solidity 0.4.22 Enhancements. . 2018-04-18 [2021-02-22].
  3. Sergi Juanati . Solidity version mismatch. . 2021-02-22 [2021-02-23].
  4. Yash Shukla. Undefined results for “web3.eth.accounts” in truffle console. . 2019-01-27 [2021-02-22].
  5. Alex Svanevik. Why All These Empty Ethereum Blocks?. . 2018-10-02 [2021-02-17].
  6. Richard Horrocks. The meaning/specification of “config” in genesis.json. . 2017-05-01 [2021-02-17].
  7. Samuel Dare. Private Clique network, where can I get block sealer and block reward?. . 2019-12-28 [2021-02-10].