以太坊单节点私有链环境的搭建

笔者将在接下来的一段时间内将发表以太坊和智能合约系列文章(共5篇),其文章列表为:

1. 以太坊单节点私有链环境的搭建(当前文章)
  主要描述环境的搭建以及账户、收益以及挖矿相关知识和命令
2. 以太坊多节点私有链环境的搭建
  主要描述多节点的互联以及交易、区块信息(给出一个节点间交易的例子)
3. 以太坊区块链浏览器的搭建
  主要描述区块链浏览器的搭建以及在浏览器中查看区块和交易信息
4. 在以太坊系统下构建第一个智能合约
  主要描述truffle的使用,以及如何在以太坊多节点系统中构建智能合约
5. 在以太坊系统下构建可升级的智能合约
  主要描述如何编写可升级智能合约代码

  本文将介绍《以太坊单节点私有链环境的搭建》,其主要内容包含go环境的搭建、以太坊私有链环境的搭建以及以太坊账户、挖矿以及挖矿收益账号管理等内容。

基本信息

  • 操作系统: ubuntu16.04 x86_64
  • 系统内存: 大于等于2G
  • go 版 本: go1.10.2
  • geth版本: 1.8.8-unstable(github最新版)

安装go

下载安装包

 
 
  1. root@cc-virtual-machine:~#curl -O https://storage.googleapis.com/golang/go1.10.2.linux-amd64.tar.gz

删除已存在目录

如果目录/usr/local/go/不存在,跳过该步骤

 
 
  1. root@cc-virtual-machine:~#rm -rf /usr/local/go/

解压安装包

 
 
  1. root@cc-virtual-machine:~#tar -C /usr/local -xzf go1.10.2.linux-amd64.tar.gz

添加到环境变量中

 
 
  1. root@cc-virtual-machine:~#echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bashrc
  2. root@cc-virtual-machine:~#source ~/.bashrc

查看go版本

 
 
  1. root@cc-virtual-machine:~# go version
  2. go version go1.10.2 linux/amd64

恭喜您,成功安装go

安装beth(源码安装)

(以下操作是在ubuntu root权限下进行)

下载安装包

 
 
  1. root@cc-virtual-machine:~#git clone https://github.com/ethereum/go-ethereum

编译

 
 
  1. root@cc-virtual-machine:~#cd go-ethereum
  2. root@cc-virtual-machine:~#make geth

创建软连接

 
 
  1. root@cc-virtual-machine:~#ln -s /usr/bin/geth /root/go-ethereum/build/bin/geth

查看geth版本

 
 
  1. root@cc-virtual-machine:~# geth version
  2. Geth
  3. Version: 1.8.8-unstable
  4. Architecture: amd64
  5. Protocol Versions: [63 62]
  6. Network Id: 1
  7. Go Version: go1.10.2
  8. Operating System: linux
  9. GOPATH=/root/go-ethereum/build/_workspace/
  10. GOROOT=/usr/local/go

恭喜您,成功安装geth

挖取第一个区块

创建创世币配置文件

(私有节点networkid为18)

 
 
  1. root@cc-virtual-machine:~#mkdir -p /opt/data/18
  2. root@cc-virtual-machine:~#cd /opt/data
  3. root@cc-virtual-machine:/opt/data#vi genesis.json
  4. {
  5. "config": {
  6. "chainId": 18,
  7. "homesteadBlock": 0,
  8. "eip155Block": 0,
  9. "eip158Block": 0
  10. },
  11. "alloc" : {},
  12. "coinbase" : "0x0000000000000000000000000000000000000000",
  13. "difficulty" : "0x20000",
  14. "extraData" : "",
  15. "gasLimit" : "0x2fefd8",
  16. "nonce" : "0x0000000000000042",
  17. "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  18. "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  19. "timestamp" : "0x00"
  20. }

初始化创世配置文件

 
 
  1. root@cc-virtual-machine:/opt/data# geth --datadir ./18/ init genesis.json
  2. INFO [05-22|17:16:01] Maximum peer count ETH=25 LES=0 total=25
  3. INFO [05-22|17:16:01] Allocated cache and file handles database=/opt/data/18/geth/chaindata cache=16 handles=16
  4. INFO [05-22|17:16:02] Writing custom genesis block
  5. INFO [05-22|17:16:02] Persisted trie from memory database nodes=0 size=0.00B time=3.881µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
  6. INFO [05-22|17:16:02] Successfully wrote genesis state database=chaindata hash=5e1fc7d790e0
  7. INFO [05-22|17:16:02] Allocated cache and file handles database=/opt/data/18/geth/lightchaindata cache=16 handles=16
  8. INFO [05-22|17:16:02] Writing custom genesis block
  9. INFO [05-22|17:16:02] Persisted trie from memory database nodes=0 size=0.00B time=4.567µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
  10. INFO [05-22|17:16:02] Successfully wrote genesis state database=lightchaindata hash=5e1fc7d790e0

启动geth进程

(私有节点networkid为18, 当前系统IP为192.168.226.129)

 
 
  1. root@cc-virtual-machine:/opt/data# geth --datadir ./18 --networkid 18 --port 28000 --rpc --rpcaddr 192.168.226.129 --rpcport 8545 --rpcapi 'db,net,eth,web3' --rpccorsdomain '*' --nodiscover console
  2. INFO [05-22|17:52:31] Maximum peer count ETH=25 LES=0 total=25
  3. INFO [05-22|17:52:31] Starting peer-to-peer node instance=Geth/v1.8.8-unstable/linux-amd64/go1.10.2
  4. INFO [05-22|17:52:31] Allocated cache and file handles database=/opt/data/18/geth/chaindata cache=768 handles=512
  5. INFO [05-22|17:52:31] Initialised chain configuration config="{ChainID: 18 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Constantinople: <nil> Engine: unknown}"
  6. INFO [05-22|17:52:31] Disk storage enabled for ethash caches dir=/opt/data/18/geth/ethash count=3
  7. INFO [05-22|17:52:31] Disk storage enabled for ethash DAGs dir=/root/.ethash count=2
  8. INFO [05-22|17:52:31] Initialising Ethereum protocol versions="[63 62]" network=18
  9. INFO [05-22|17:52:31] Loaded most recent local header number=0 hash=5e1fc7d790e0 td=131072
  10. INFO [05-22|17:52:31] Loaded most recent local full block number=0 hash=5e1fc7d790e0 td=131072
  11. INFO [05-22|17:52:31] Loaded most recent local fast block number=0 hash=5e1fc7d790e0 td=131072
  12. INFO [05-22|17:52:31] Regenerated local transaction journal transactions=0 accounts=0
  13. INFO [05-22|17:52:31] Starting P2P networking
  14. INFO [05-22|17:52:31] RLPx listener up self="enode://66f9244769af7a5ef938919152a27eedcaa76a59bc5bfb026744f2c78079be31ee53d362ab37c486aba8ee399e8f8b2e539b682fa776ba6b3fd17f7c0a1e5467@[::]:28000?discport=0"
  15. INFO [05-22|17:52:31] IPC endpoint opened url=/opt/data/18/geth.ipc
  16. INFO [05-22|17:52:31] HTTP endpoint opened url=http://192.168.226.129:8545 cors=* vhosts=localhost
  17. Welcome to the Geth JavaScript console!
  18. instance: Geth/v1.8.8-unstable/linux-amd64/go1.10.2
  19. modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
  20. >

账号管理

新建账号

 
 
  1. > personal.newAccount()
  2. Passphrase:
  3. Repeat passphrase:
  4. "0x858be5f4a7bc914491fde7d4317ef000a06ec8cf"
  5. > personal.newAccount()
  6. Passphrase:
  7. Repeat passphrase:
  8. "0x3c0bd74a4c1e2a1d0eb0f8c7aecfbb3ce73d81cf"

查看账号

 
 
  1. > eth.accounts
  2. ["0x858be5f4a7bc914491fde7d4317ef000a06ec8cf", "0x3c0bd74a4c1e2a1d0eb0f8c7aecfbb3ce73d81cf"]

查看余额

 
 
  1. > eth.getBalance(eth.accounts[0])
  2. 0
  3. > eth.getBalance(eth.accounts[1])
  4. 0
  5. ```shell
  6. #启动挖矿
  7. 一个区块奖励5个ether(需要等待数据同步结束后进行挖矿)
  8. ```shell
  9. > miner.start(2)
  10. INFO [05-22|17:25:38] Commit new mining work number=1 txs=0 uncles=0 elapsed=117.252µs
  11. INFO [05-22|17:25:44] Successfully sealed new block number=1 hash=5e8e2352835f
  12. INFO [05-22|17:25:44] mined potential block number=1 hash=5e8e2352835f
  13. INFO [05-22|17:25:44] Commit new mining work number=2 txs=0 uncles=0 elapsed=130.663µs
  14. INFO [05-22|17:25:47] Successfully sealed new block number=2 hash=a6e5f427c2ec
  15. INFO [05-22|17:25:47] mined potential block number=2 hash=a6e5f427c2ec
  16. INFO [05-22|17:25:47] Commit new mining work number=3 txs=0 uncles=0 elapsed=101.336µs
  17. INFO [05-22|17:25:51] Successfully sealed new block number=3 hash=b2cc9541a6ea
  18. INFO [05-22|17:25:51] mined potential block number=3 hash=b2cc9541a6ea
  19. INFO [05-22|17:25:51] Commit new mining work number=4 txs=0 uncles=0 elapsed=94.601µs

停止挖矿

 
 
  1. > miner.stop()
  2. true

查看收益

账号0的收益为65ether,账号1的收益为0ether,下面将描述如何将挖矿收益保存到账号1中

 
 
  1. > eth.getBalance(eth.accounts[0])
  2. 65000000000000000000
  3. > web3.fromWei(eth.getBalance(eth.accounts[0]),'ether')
  4. 65
  5. > eth.getBalance(eth.accounts[1])
  6. 0

修改保存挖矿收益的账号

查看当前账号情况(默认为账号0)

 
 
  1. > eth.coinbase
  2. "0x858be5f4a7bc914491fde7d4317ef000a06ec8cf"
  3. > eth.accounts
  4. ["0x858be5f4a7bc914491fde7d4317ef000a06ec8cf", "0x3c0bd74a4c1e2a1d0eb0f8c7aecfbb3ce73d81cf"]

修改当前默认账号为账号1

 
 
  1. > miner.setEtherbase(eth.accounts[1])
  2. true
  3. > eth.coinbase
  4. "0x3c0bd74a4c1e2a1d0eb0f8c7aecfbb3ce73d81cf"

再次挖矿

 
 
  1. > miner.start(2)
  2. INFO [05-22|17:31:59] Updated mining threads threads=2
  3. INFO [05-22|17:31:59] Transaction pool price threshold updated price=18000000000
  4. null
  5. > INFO [05-22|17:31:59] Starting mining operation
  6. INFO [05-22|17:31:59] Commit new mining work number=15 txs=0 uncles=0 elapsed=150.12µs
  7. INFO [05-22|17:32:00] Successfully sealed new block number=15 hash=fe8a0f627a7d
  8. INFO [05-22|17:32:00] block reached canonical chain number=10 hash=d5bd8a568773
  9. INFO [05-22|17:32:00] mined potential block number=15 hash=fe8a0f627a7d
  10. INFO [05-22|17:32:00] Commit new mining work number=16 txs=0 uncles=0 elapsed=122.366µs

再次停止挖矿

 
 
  1. > miner.stop()
  2. true

查看账号1的余额情况

 
 
  1. > eth.getBalance(eth.accounts[1])
  2. 30000000000000000000

使用exit退出geth程序

版权声明:B链网原创,严禁修改。转载请注明作者和原文链接

作者:雨后的蚊子

原文链接: http://www.360bchain.com/article/151.html

猜你喜欢

转载自blog.csdn.net/Tostick/article/details/80423982