「这是我参与2022首次更文挑战的第3天,活动详情查看:2022首次更文挑战」。
新鲜出炉的以太坊开发环境搭建及私链搭建指南
本文收录于我的专栏:细讲区块链
今天我们继续我们的《细讲区块链》专栏,今天我们来介绍介绍以太坊开发环境的搭建。有人说区块链的开发环境搭建十分麻烦,但是根据本文操作,顺利的话,预计半个小时左右就能搭建好。跟着我一起来吧!我还会带着大家一起搭建一条公链的私链哦!
以太坊开发环境搭建
一、安装Linux虚拟机
我的虚拟机版本是:
- VMware® Workstation 16 Pro 16.2.2 build-19200509
- ubuntu-20.04.2.0-desktop-amd64
安装虚拟机比较简单,不是本文的重点,故在此不再着重讲述。
二、安装Git
我们先要将软件源换成国内的,这里我换成阿里云的软件源。
然后更新软件列表:
reganyue@ubuntu:~/Desktop$ sudo apt-get update
最后就是一行命令安装Git了咯。
reganyue@ubuntu:~/Desktop$ sudo apt-get install git
三、安装Geth
- 可以通过源码安装。通过Git在Github下载go-ethereum源码,然后进入源码目录,执行make geth安装geth。注意因为代码是Go编写的,所以我们需要安装Golang。
- 然后添加以太坊官方的APT源:
reganyue@ubuntu:~/Desktop$ sudo apt-get install software-properties-common
reganyue@ubuntu:~/Desktop$ sudo add-apt-repository -y ppa:ethereum/ethereum
3.安装以太坊客户端
先获取最新的软件列表:
reganyue@ubuntu:~/Desktop$ sudo apt-get update
Hit:1 http://mirrors.aliyun.com/ubuntu focal InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu focal-updates InRelease
Hit:3 http://mirrors.aliyun.com/ubuntu focal-backports InRelease
Hit:4 http://mirrors.aliyun.com/ubuntu focal-security InRelease
Hit:5 http://ppa.launchpad.net/ethereum/ethereum/ubuntu focal InRelease
Reading package lists... Done
安装ethereum:
reganyue@ubuntu:~/Desktop$ sudo apt-get install ethereum
私链搭建我们需要建立私链目录:
reganyue@ubuntu:~/Desktop$ mkdir eth-01
reganyue@ubuntu:~/Desktop$ cd eth-01/
reganyue@ubuntu:~/Desktop/eth-01$ mkdir private
reganyue@ubuntu:~/Desktop/eth-01$ cd private/
然后我们需要Defining the private genesis state,直译就是定义私有创世状态,和比特币的创世区块差不多。
它是一个json文件:
官方文档genesis.json是如下的内容
{
"config": {
"chainId": <arbitrary positive integer>,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"berlinBlock": 0,
"londonBlock": 0
},
"alloc": {},
"coinbase": "0x0000000000000000000000000000000000000000",
"difficulty": "0x20000",
"extraData": "",
"gasLimit": "0x2fefd8",
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x00"
}
然后将创世状态json文件放入我们的项目。
reganyue@ubuntu:~/Desktop/eth-01/private$ touch genesis.json
reganyue@ubuntu:~/Desktop/eth-01/private$ gedit genesis.json
我们可以看到红红的报错了吧!这里需要将chainId改为一个数字,这里我们改为0:
{
"config": {
"chainId": 0,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"berlinBlock": 0,
"londonBlock": 0
},
"alloc": {},
"coinbase": "0x0000000000000000000000000000000000000000",
"difficulty": "0x20000",
"extraData": "",
"gasLimit": "0x2fefd8",
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x00"
}
这里介绍一下这些配置文件的参数,其中chainId是区块链的网络ID,公网的ID是1,我们这里是私链所以填一个0,注意不同ID的网络节点无法连接。homesteadBlock、eip150Block、eip155Block、eip158Block等代表某个协议机制的升级区块的高度,homesteadBlock这里值为0,表示正在使用homestead版本。 alloc是预置账号以及账号的以太币数量,我们这里是私链暂时还用不上。coinbase这是矿工账号,可以随便填,因为以太中会默认把账号列表中的第一个账号当成挖矿账号。 difficulty是当前区块的挖矿难度,可以动态的进行调节。extraData:附加数据,可以随便填你想填的 。gasLimit是gas上限。nonce是随机数,主要用于挖矿,mixHash与nonce结合用于挖矿。parentHash是前区块的哈希值。timeStamp是区块的时间戳。
不然初始化就会报如下错误:
reganyue@ubuntu:~/Desktop/eth-01/private$ geth --datadir data init genesis.json
Fatal: invalid genesis file: invalid character '<' looking for beginning of value
然后我们初始化就成功了。 我们需要建立私链目录:
reganyue@ubuntu:~/Desktop$ mkdir eth-01
reganyue@ubuntu:~/Desktop$ cd eth-01/
reganyue@ubuntu:~/Desktop/eth-01$ mkdir private
reganyue@ubuntu:~/Desktop/eth-01$ cd private/
然后我们需要Defining the private genesis state,直译就是定义私有创世状态,和比特币的创世区块差不多。
它是一个json文件:
官方文档genesis.json是如下的内容
{
"config": {
"chainId": <arbitrary positive integer>,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"berlinBlock": 0,
"londonBlock": 0
},
"alloc": {},
"coinbase": "0x0000000000000000000000000000000000000000",
"difficulty": "0x20000",
"extraData": "",
"gasLimit": "0x2fefd8",
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x00"
}
然后将创世状态json文件放入我们的项目。
reganyue@ubuntu:~/Desktop/eth-01/private$ touch genesis.json
reganyue@ubuntu:~/Desktop/eth-01/private$ gedit genesis.json
我们可以看到红红的报错了吧!这里需要将chainId改为一个数字,这里我们改为0:
{
"config": {
"chainId": 0,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"berlinBlock": 0,
"londonBlock": 0
},
"alloc": {},
"coinbase": "0x0000000000000000000000000000000000000000",
"difficulty": "0x20000",
"extraData": "",
"gasLimit": "0x2fefd8",
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x00"
}
这里介绍一下这些配置文件的参数,其中chainId是区块链的网络ID,公网的ID是1,我们这里是私链所以填一个0,注意不同ID的网络节点无法连接。homesteadBlock、eip150Block、eip155Block、eip158Block等代表某个协议机制的升级区块的高度,homesteadBlock这里值为0,表示正在使用homestead版本。 alloc是预置账号以及账号的以太币数量,我们这里是私链暂时还用不上。coinbase这是矿工账号,可以随便填,因为以太中会默认把账号列表中的第一个账号当成挖矿账号。 difficulty是当前区块的挖矿难度,可以动态的进行调节。extraData:附加数据,可以随便填你想填的 。gasLimit是gas上限。nonce是随机数,主要用于挖矿,mixHash与nonce结合用于挖矿。parentHash是前区块的哈希值。timeStamp是区块的时间戳。
不然初始化就会报如下错误:
reganyue@ubuntu:~/Desktop/eth-01/private$ geth --datadir data init genesis.json
Fatal: invalid genesis file: invalid character '<' looking for beginning of value
然后我们初始化就成功了。
reganyue@ubuntu:~/Desktop/eth-01/private$ geth --datadir data init genesis.json
INFO [01-19|23:58:47.730] Maximum peer count ETH=50 LES=0 total=50
INFO [01-19|23:58:47.731] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [01-19|23:58:47.732] Set global gas cap cap=50,000,000
INFO [01-19|23:58:47.732] Allocated cache and file handles database=/home/reganyue/Desktop/eth-01/private/data/geth/chaindata cache=16.00MiB handles=16
INFO [01-19|23:58:47.753] Writing custom genesis block
INFO [01-19|23:58:47.753] Persisted trie from memory database nodes=0 size=0.00B time="2.926µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [01-19|23:58:47.753] Successfully wrote genesis state database=chaindata hash=d118bd..7a409f
INFO [01-19|23:58:47.758] Allocated cache and file handles database=/home/reganyue/Desktop/eth-01/private/data/geth/lightchaindata cache=16.00MiB handles=16
INFO [01-19|23:58:47.767] Writing custom genesis block
INFO [01-19|23:58:47.769] Persisted trie from memory database nodes=0 size=0.00B time="4.258µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [01-19|23:58:47.769] Successfully wrote genesis state database=lightchaindata hash=d118bd..7a409f
我们看一看初始化生成的data目录:
reganyue@ubuntu:~/Desktop/eth-01/private$ cd data/
reganyue@ubuntu:~/Desktop/eth-01/private/data$ ls
geth keystore
reganyue@ubuntu:~/Desktop/eth-01/private/data$
可以看到 geth 和 keystore 两个目录。
其中keystore是存储私钥等内容的, geth目录是用来存储区块链数据等
然后我们进入操作台:”
reganyue@ubuntu:~/Desktop/eth-01/private/data$ geth --datadir data console console 2>>tail.log
然后新建一个账户:
> personal.newAccount()
Passphrase:
Repeat passphrase:
"0x7ff9ed40ac707d8ec8a90145327c8756976ffa35"
启动私链节点
下面是老版本的启动命令:
geth --datadir "./db" --rpc --rpcaddr=0.0.0.0 --rpcport 8545 --rpccorsdomain "*" --rpcapi "eth,net,web3,personal,admin,shh,txpool,debug,miner" --nodiscover --maxpeers 30 --networkid 1981 --port 30303 --mine --minerthreads 1 --etherbase "0xcCE4c1093D63416Eb079BFfe50Dc9Ce590703c6B" console 2>>tail.log
这里的参数的意思如下:
--datadir 后面是区块链数据库存放位置。
--rpc 表示启动RPC通信,可以进行智能合约部署和调试。
--rpcaddr=0.0.0.0 :指定HTTP-RPC监听地址,默认localhost。
--rpcport 8545 : 指定HTTP-RPC监听端口,默认8545。
--rpccorsdomain "*" 指定远程域名,星号表示没有要求。
--rpcapi 后面是允许连接的RPC客户端
--nodiscover 就是关闭自动连接
--maxpeers 30 表示允许的最大连接数,这个参数默认是25个。我们这里设置的是30个。
--networkid 1981 表示设置当前区块链中网络ID,为了区分不同的网络,这里是1981.
--port 30303
--mine 表示开启挖矿,以太坊默认是CPU挖矿。
--minerthreads 1 表示挖矿的CPU线程数,默认是4。
--etherbase 是矿工账号。
--console是启动命令行,可以在geth中执行命令。
如果你是新版本我们直接这样:
geth --datadir "./data" --nodiscover console 2>>tail.log --dev --dev.period 1
> eth.accounts
["0x7ff9ed40ac707d8ec8a90145327c8756976ffa35"]
> miner.setEtherbase(eth.accounts[0])
true
> miner.start()
null
> eth.blockNumber
11
> eth.getBalance(eth.accounts[0])
22000000000000000000
> miner.stop()
null
然后我们上面可以在这条私链挖矿了!