笔者将在接下来的一段时间内将发表以太坊和智能合约系列文章(共5篇),其文章列表为:
1. 以太坊单节点私有链环境的搭建(当前文章)
主要描述环境的搭建以及账户、收益以及挖矿相关知识和命令
2. 以太坊多节点私有链环境的搭建
主要描述多节点的互联以及交易、区块信息(给出一个节点间交易的例子)
3. 以太坊区块链浏览器的搭建
主要描述区块链浏览器的搭建以及在浏览器中查看区块和交易信息
4. 在以太坊系统下构建第一个智能合约
主要描述truffle的使用,以及如何在以太坊多节点系统中构建智能合约
5. 在以太坊系统下构建可升级的智能合约
主要描述如何编写可升级智能合约代码
本文将介绍《以太坊单节点私有链环境的搭建》,其主要内容包含go环境的搭建、以太坊私有链环境的搭建以及以太坊账户、挖矿以及挖矿收益账号管理等内容。
基本信息
- 操作系统: ubuntu16.04 x86_64
- 系统内存: 大于等于2G
- go 版 本: go1.10.2
- geth版本: 1.8.8-unstable(github最新版)
安装go
下载安装包
[email protected]-virtual-machine:~#curl -O https://storage.googleapis.com/golang/go1.10.2.linux-amd64.tar.gz
删除已存在目录
如果目录/usr/local/go/不存在,跳过该步骤
[email protected]-virtual-machine:~#rm -rf /usr/local/go/
解压安装包
[email protected]-virtual-machine:~#tar -C /usr/local -xzf go1.10.2.linux-amd64.tar.gz
添加到环境变量中
[email protected]-virtual-machine:~#echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bashrc[email protected]-virtual-machine:~#source ~/.bashrc
查看go版本
[email protected]-virtual-machine:~# go versiongo version go1.10.2 linux/amd64
恭喜您,成功安装go
安装beth(源码安装)
(以下操作是在ubuntu root权限下进行)
下载安装包
[email protected]-virtual-machine:~#git clone https://github.com/ethereum/go-ethereum
编译
[email protected]-virtual-machine:~#cd go-ethereum[email protected]-virtual-machine:~#make geth
创建软连接
[email protected]-virtual-machine:~#ln -s /usr/bin/geth /root/go-ethereum/build/bin/geth
查看geth版本
[email protected]-virtual-machine:~# geth versionGethVersion: 1.8.8-unstableArchitecture: amd64Protocol Versions: [63 62]Network Id: 1Go Version: go1.10.2Operating System: linuxGOPATH=/root/go-ethereum/build/_workspace/GOROOT=/usr/local/go
恭喜您,成功安装geth
挖取第一个区块
创建创世币配置文件
(私有节点networkid为18)
[email protected]-virtual-machine:~#mkdir -p /opt/data/18[email protected]-virtual-machine:~#cd /opt/data[email protected]-virtual-machine:/opt/data#vi genesis.json{"config": {"chainId": 18,"homesteadBlock": 0,"eip155Block": 0,"eip158Block": 0},"alloc" : {},"coinbase" : "0x0000000000000000000000000000000000000000","difficulty" : "0x20000","extraData" : "","gasLimit" : "0x2fefd8","nonce" : "0x0000000000000042","mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000","parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000","timestamp" : "0x00"}
初始化创世配置文件
[email protected]-virtual-machine:/opt/data# geth --datadir ./18/ init genesis.jsonINFO [05-22|17:16:01] Maximum peer count ETH=25 LES=0 total=25INFO [05-22|17:16:01] Allocated cache and file handles database=/opt/data/18/geth/chaindata cache=16 handles=16INFO [05-22|17:16:02] Writing custom genesis blockINFO [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.00BINFO [05-22|17:16:02] Successfully wrote genesis state database=chaindata hash=5e1fc7…d790e0INFO [05-22|17:16:02] Allocated cache and file handles database=/opt/data/18/geth/lightchaindata cache=16 handles=16INFO [05-22|17:16:02] Writing custom genesis blockINFO [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.00BINFO [05-22|17:16:02] Successfully wrote genesis state database=lightchaindata hash=5e1fc7…d790e0
启动geth进程
(私有节点networkid为18, 当前系统IP为192.168.226.129)
[email protected]-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 consoleINFO [05-22|17:52:31] Maximum peer count ETH=25 LES=0 total=25INFO [05-22|17:52:31] Starting peer-to-peer node instance=Geth/v1.8.8-unstable/linux-amd64/go1.10.2INFO [05-22|17:52:31] Allocated cache and file handles database=/opt/data/18/geth/chaindata cache=768 handles=512INFO [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}"INFO [05-22|17:52:31] Disk storage enabled for ethash caches dir=/opt/data/18/geth/ethash count=3INFO [05-22|17:52:31] Disk storage enabled for ethash DAGs dir=/root/.ethash count=2INFO [05-22|17:52:31] Initialising Ethereum protocol versions="[63 62]" network=18INFO [05-22|17:52:31] Loaded most recent local header number=0 hash=5e1fc7…d790e0 td=131072INFO [05-22|17:52:31] Loaded most recent local full block number=0 hash=5e1fc7…d790e0 td=131072INFO [05-22|17:52:31] Loaded most recent local fast block number=0 hash=5e1fc7…d790e0 td=131072INFO [05-22|17:52:31] Regenerated local transaction journal transactions=0 accounts=0INFO [05-22|17:52:31] Starting P2P networkingINFO [05-22|17:52:31] RLPx listener up self="enode://66f9244769af7a5ef938919152a27eedcaa76a59bc5bfb026744f2c78079be31e[email protected][::]:28000?discport=0"INFO [05-22|17:52:31] IPC endpoint opened url=/opt/data/18/geth.ipcINFO [05-22|17:52:31] HTTP endpoint opened url=http://192.168.226.129:8545 cors=* vhosts=localhostWelcome to the Geth JavaScript console!instance: Geth/v1.8.8-unstable/linux-amd64/go1.10.2modules: 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>
账号管理
新建账号
> personal.newAccount()Passphrase:Repeat passphrase:"0x858be5f4a7bc914491fde7d4317ef000a06ec8cf"> personal.newAccount()Passphrase:Repeat passphrase:"0x3c0bd74a4c1e2a1d0eb0f8c7aecfbb3ce73d81cf"
查看账号
> eth.accounts["0x858be5f4a7bc914491fde7d4317ef000a06ec8cf", "0x3c0bd74a4c1e2a1d0eb0f8c7aecfbb3ce73d81cf"]
查看余额
> eth.getBalance(eth.accounts[0])0> eth.getBalance(eth.accounts[1])0```shell#启动挖矿一个区块奖励5个ether(需要等待数据同步结束后进行挖矿)```shell> miner.start(2)INFO [05-22|17:25:38] Commit new mining work number=1 txs=0 uncles=0 elapsed=117.252µsINFO [05-22|17:25:44] Successfully sealed new block number=1 hash=5e8e23…52835fINFO [05-22|17:25:44] mined potential block number=1 hash=5e8e23…52835fINFO [05-22|17:25:44] Commit new mining work number=2 txs=0 uncles=0 elapsed=130.663µsINFO [05-22|17:25:47] Successfully sealed new block number=2 hash=a6e5f4…27c2ecINFO [05-22|17:25:47] mined potential block number=2 hash=a6e5f4…27c2ecINFO [05-22|17:25:47] Commit new mining work number=3 txs=0 uncles=0 elapsed=101.336µsINFO [05-22|17:25:51] Successfully sealed new block number=3 hash=b2cc95…41a6eaINFO [05-22|17:25:51] mined potential block number=3 hash=b2cc95…41a6eaINFO [05-22|17:25:51] Commit new mining work number=4 txs=0 uncles=0 elapsed=94.601µs
停止挖矿
> miner.stop()true
查看收益
账号0的收益为65ether,账号1的收益为0ether,下面将描述如何将挖矿收益保存到账号1中
> eth.getBalance(eth.accounts[0])65000000000000000000> web3.fromWei(eth.getBalance(eth.accounts[0]),'ether')65> eth.getBalance(eth.accounts[1])0
修改保存挖矿收益的账号
查看当前账号情况(默认为账号0)
> eth.coinbase"0x858be5f4a7bc914491fde7d4317ef000a06ec8cf"> eth.accounts["0x858be5f4a7bc914491fde7d4317ef000a06ec8cf", "0x3c0bd74a4c1e2a1d0eb0f8c7aecfbb3ce73d81cf"]
修改当前默认账号为账号1
> miner.setEtherbase(eth.accounts[1])true> eth.coinbase"0x3c0bd74a4c1e2a1d0eb0f8c7aecfbb3ce73d81cf"
再次挖矿
> miner.start(2)INFO [05-22|17:31:59] Updated mining threads threads=2INFO [05-22|17:31:59] Transaction pool price threshold updated price=18000000000null> INFO [05-22|17:31:59] Starting mining operationINFO [05-22|17:31:59] Commit new mining work number=15 txs=0 uncles=0 elapsed=150.12µsINFO [05-22|17:32:00] Successfully sealed new block number=15 hash=fe8a0f…627a7dINFO [05-22|17:32:00] block reached canonical chain number=10 hash=d5bd8a…568773INFO [05-22|17:32:00] mined potential block number=15 hash=fe8a0f…627a7dINFO [05-22|17:32:00] Commit new mining work number=16 txs=0 uncles=0 elapsed=122.366µs
再次停止挖矿
> miner.stop()true
查看账号1的余额情况
> eth.getBalance(eth.accounts[1])30000000000000000000
使用exit退出geth程序
版权声明:B链网原创,严禁修改。转载请注明作者和原文链接
作者:雨后的蚊子
原文链接: http://www.360bchain.com/article/151.html