主页 > imtoken钱包下载官网 > 以太坊单节点私链环境搭建

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

imtoken钱包下载官网 2023-07-19 05:14:28

2021-09-07

作者将在接下来的一段时间内发表关于以太坊和智能合约的系列文章(共5篇)。 文章列表如下:

一、以太坊单节点私链环境搭建(当前文章)

主要描述搭建环境、账户、收益、挖矿相关的知识和命令

2. 以太坊多节点私有链环境搭建

主要描述多节点互联、交易、区块信息(举节点间交易的例子)

3.以太坊区块链浏览器搭建

主要描述区块链浏览器的搭建以及在浏览器中查看区块和交易信息

4.构建以太坊系统下的第一个智能合约

主要讲述truffle的使用以及如何在以太坊多节点系统中构建智能合约

5. 在以太坊系统下构建可升级的智能合约

主要描述如何编写可升级的智能合约代码

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

本文将介绍《以太坊单节点私链环境搭建》,主要包括go环境搭建、以太坊私链环境搭建、以太坊账户、挖矿和挖矿收益账户的管理。

基本信息 Install go 下载安装包

[emailprotected]-virtual-machine:~#curl -O https://storage.googleapis.com/golang/go1.10.2.linux-amd64.tar.gz 删除现有目录

如果目录/usr/local/go/不存在以太坊节点收益,则跳过此步骤

[emailprotected]-virtual-machine:~#rm -rf /usr/local/go/ 解压安装包

[emailprotected]-virtual-machine:~#tar -C /usr/local-xzf go1.10.2.linux-amd64.tar.gz 添加到环境变量

[emailprotected]-virtual-machine:~#echo "export PATH=$PATH:/usr/local/go/bin">>~/.bashrc[emailprotected]-virtual-machine:~#source ~/.bashrc 查看去版本

[emailprotected]-virtual-machine:~# go version go version go1.10.2 linux/amd64

恭喜你安装成功

安装beth(源码安装)

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

下载安装包

[emailprotected]-virtual-machine:~#git clone https://github.com/ethereum/go-ethereum compile

[emailprotected]-virtual-machine:~#cd go-ethereum[emailprotected]-virtual-machine:~#make geth 创建软连接

[emailprotected]-virtual-machine:~#ln -s /usr/bin/geth /root/go-ethereum/build/bin/geth 查看geth版本

[emailprotected]-virtual-machine:~# geth versionGethVersion:1.8.8-unstableArchitecture: amd64ProtocolVersions:[6362]NetworkId:1GoVersion: go1.10.2OperatingSystem: linuxGOPATH=/root/go-ethereum/build/_workspace/GOROOT=/usr /本地/去

恭喜你已经成功安装geth

挖掘第一个区块以创建创世币配置文件

(私有节点networkid为18)

[emailprotected]-virtual-machine:~#mkdir -p /opt/data/18[emailprotected]-virtual-machine:~#cd /opt/data[emailprotected]-virtual-machine:/opt/data#vi genesis。 json{"config":{"chainId":18,"homesteadBlock":0,"eip155Block":0,"eip158Block":0},"alloc":{},"coinbase":"0x0000000000000000000000000000000000000000","难度" :"0x20000","extraData":"","gasLimit":"0x2fefd8","nonce":"0x0000000000000042","mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x00 "} 初始化Genesis配置文件

[emailprotected]-virtual-machine:/opt/data# geth --datadir ./18/ init genesis.jsonINFO [05-22|17:16:01]最大对等点计数 ETH=25 LES=0 total=25INFO [05 -22|17:16:01]分配的缓存和文件句柄数据库=/opt/data/18/geth/chaindata cache=16 handles=16INFO [05-22|17:16:02]编写自定义创世块信息[05 -22|17:16:02]来自内存数据库节点的持久化 trie=0 大小=0.00B 时间=3.881µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00BINFO [05-22|17:16 :02]成功写入创世状态数据库=chaindata hash=5e1fc7...d790e0INFO [05-22|17:16:02]分配的缓存和文件句柄数据库=/opt/data/18/geth/lightchaindata cache=16 handles= 16INFO [05-22|17:16:02]编写自定义创世块信息[05-22|17:16:02]来自内存数据库节点的持久 trie=0 大小=0.00B 时间=4.567µs gcnodes=0 gcsize=0.00 B gctime =0s livenodes=1 livesize=0.00BINFO [05-22|17:16:02]成功写入创世状态数据库=lightchaindata hash=5e1fc7…d790e0 启动geth进程

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

[emailprotected]-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]最大对等计数 ETH=25 LES=0 总计=25INFO [05-22|17:52:31]启动对等节点实例=Geth/v1.8.8-不稳定/linux-amd64/ go1.10.2 INFO [05-22|17:52:31] 分配的缓存和文件句柄数据库=/opt/data/18/geth/chaindata cache=768 handles=512 INFO [05-22| 17:52:31]初始化链配置配置=“{ChainID: 18 Homestead: 0 DAO: DAOSupport: false EIP150: EIP155: 0 EIP158: 0 Byzantium: Constantinople: Engine: unknown}” INFO [05-22|17:52 :31] 为 ethash 缓存启用的磁盘存储 dir=/opt/data/18/geth/ethash count=3 信息 [05-22|17:52:31] 为 ethash DAG 启用的磁盘存储 dir=/root/。

ethash count=2INFO [05-22|17:52:31]InitialisingEthereum 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]加载最近的本地完整块数=0 hash=5e1fc7…d790e0 td=131072INFO [05-22|17:52:31]加载最近的本地快速块number=0 hash=5e1fc7…d790e0 td=131072INFO [05-22|17:52:31]Regeneratedlocal transaction journal transactions=0 accounts=0INFO [05-22|17:52:31]Starting P2P networking INFO [05-22] |17:52:31]RLPx 侦听器 up self="en​​ode://66f9244769af7a5ef938919152a27eedcaa76a59bc5bfb026744f2c78079be31e[emailprotected][::]:28000?discport=0"INFO [05-22|17:52end1openpoint/url=3 /geth。

ipcINFO [05-22|17:52:31] HTTP 端点打开 url=http://192.168.226.129:8545 cors=* vhosts=localhost 欢迎使用 GethJavaScript 控制台!instance:Geth/v1.8.8 -unstable/linux- amd64/go1.10.2 模块: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> account management new account

> personal.newAccount() 密码:重复密码:“0x858be5f4a7bc914491fde7d4317ef000a06ec8cf”> personal.newAccount() 密码:重复密码:“0x3c0bd74a4c1e2a1d0eb0f8c7aecfbb3f7cf7 查看帐户”3d

> eth.accounts["0x858be5f4a7bc914491fde7d4317ef000a06ec8cf","0x3c0bd74a4c1e2a1d0eb0f8c7aecfbb3ce73d81cf"] 查看余额

> eth.getBalance(eth.accounts[0])0> eth.getBalance(eth.accounts[1])0```shell#开始挖一个区块奖励5个以太币(需要等待数据同步结束开始挖矿)```shell> miner.start(2)INFO [05-22|17:25:38]提交新的挖矿工作 number=1 txs=0 uncles=0 elapsed=117.252µsINFO [05-22| 17:25:44] 成功封存新区块 number=1 hash=5e8e23…52835fINFO [05-22|17:25:44] 挖掘潜在区块 number=1 hash=5e8e23…52835fINFO [05-22|17:25:44 ]提交新的挖矿工作数= 2 txs=0 uncles=0 elapsed=130.663µsINFO [05-22|17:25:47]成功密封新区块数=2 hash=a6e5f4…27c2ecINFO [05-22|17:25: 47] 挖出的潜在区块数=2 hash =a6e5f4…27c2ecINFO [05-22|17:25:47]Commitnew mining work number=3 txs=0 uncles=0 elapsed=101.336µsINFO [05-22|17:25:51] ]Successfullysealednew block number=3 hash=b2cc95 …41a6eaINFO [05-22|17:25:51] 挖出的潜在区块数=3 hash=b2cc95…41a6eaINFO [05-22|17:25:51]Commit new mining work number= 4 txs=0 叔叔=0 经过=94.6 01 µ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", "0x3c0bd74a4c1e2a1d0eb0f8cf",13d0eb0f8cf71修改当前账号

> miner.setEtherbase(eth.accounts[1]) true > eth.coinbase "0x3c0bd74a4c1e2a1d0eb0f8c7aecfbb3ce73d81cf" 再次开采

> miner.start(2) INFO [05-22|17:31:59]更新的挖掘线程线程=2INFO [05-22|17:31:59]交易池价格阈值更新价格=18000000000null> INFO [05-22 |17:31:59]开始挖矿操作 INFO [05-22|17:31:59]Commitnew 挖矿工作数量=15 txs=0 uncles=0 elapsed=150.12µsINFO [05-22|17:32:00]Successfullysealednew块号=15 hash=fe8a0f…627a7dINFO [05-22|17:32:00] 块达到规范链号=10 hash=d5bd8a…568773INFO [05-22|17:32:00] 挖掘的潜在块号=15 hash =fe8a0f…627a7dINFO [05-22|17:32:00]Commitnew mining work number=16 txs=0 uncles=0 elapsed=122.366µs stop mining again

> miner.stop()true 查看账户1的余额

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

使用exit退出geth程序