小坚的技术博客

Windows下以太坊私有链搭建

本文作者:陈进坚
博客地址:https://jian1098.github.io
CSDN博客:https://blog.csdn.net/c_jian
简书:https://www.jianshu.com/u/8ba9ac5706b6
联系方式:jian1098@qq.com

Geth的下载安装

本文在64位windows10环境下搭建,其他系统或许会略有不同。首先在官方网站https://geth.ethereum.org/downloads/ ,国内镜像http://ethfans.org/wikis/Ethereum-Geth-Mirror下载geth的windows端,下载完成后一直点下一步安装即可。

准备创世区块文件

在任何一个目录下创建 privatechain 文件夹,这里在D盘下创建。然后在privatechain 文件夹中创建data文件夹和genesis.json文件,打开genesis.json文件将以下代码复制到文件中并保存

1
2
3
4
5
6
7
8
9
10
11
12
{
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x400",
"alloc": {},
"coinbase": "0x3333333333333333333333333333333333333333",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0x8000000",
"config": {}
}

初始化创世区块

打开cmd,进入到privatechain目录下然后执行下面的命令即可完成创世区块的初始化

1
2
3
4
5
6
7
8
9
10
11
$ geth --datadir data init genesis.json 

INFO [10-24|14:12:56] Maximum peer count ETH=25 LES=0 total=25
INFO [10-24|14:12:56] Allocated cache and file handles database=D:\\Ethereum\\data\\geth\\chaindata cache=16 handles=16
INFO [10-24|14:12:56] Writing custom genesis block
INFO [10-24|14:12:56] Persisted trie from memory database nodes=0 size=0.00B time=0s gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [10-24|14:12:56] Successfully wrote genesis state database=chaindata hash=6231b0 …a0300b
INFO [10-24|14:12:56] Allocated cache and file handles database=D:\\Ethereum\\data\\geth\\lightchaindata cache=16 handles=16
INFO [10-24|14:12:56] Writing custom genesis block
INFO [10-24|14:12:56] Persisted trie from memory database nodes=0 size=0.00B time=0s gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [10-24|14:12:56] Successfully wrote genesis state database=lightchaindata hash=6231b0…a0300b

启动私有链

打开cmd,进入到privatechain目录下然后执行下面的命令即可启动私有链,命令中的ip地址请自行换成你本机的ip,–datadir换成自己的私有链所在的目录

1
$ geth --port 3000 --networkid 15 --maxpeers=3 --rpc --rpcport 8545 --rpcaddr 127.0.0.1 --rpccorsdomain "*"  --rpcapi="db,eth,web3,personal,admin,txpool,net" --gcmode=archive console

得到以下信息,表示已经启动私有链并进入geth console

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
INFO [10-24|14:16:13] Maximum peer count                       ETH=3 LES=0 total=3
INFO [10-24|14:16:13] Starting peer-to-peer node instance=Geth/v1.8.3-stable-329ac18e/windows-amd64/go1.10
INFO [10-24|14:16:13] Allocated cache and file handles database=D:\\Ethereum\\data\\geth\\chaindata cache=768 handles=1024
WARN [10-24|14:16:13] Upgrading database to use lookup entries
INFO [10-24|14:16:13] Database deduplication successful deduped=0
INFO [10-24|14:16:13] Initialised chain configuration config="{ChainID: <nil> Homestead: <nil> DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: <nil> EIP158: <nil> Byzantium: <nil> Constantinople: <nil> Engine: unknown}"
INFO [10-24|14:16:13] Disk storage enabled for ethash caches dir=D:\\Ethereum\\data\\geth\\ethash count=3
INFO [10-24|14:16:13] Disk storage enabled for ethash DAGs dir=C:\\Users\\Jian\\AppData\\Ethash count=2
INFO [10-24|14:16:13] Initialising Ethereum protocol versions="[63 62]" network=15
INFO [10-24|14:16:13] Loaded most recent local header number=0 hash=6231b0…a0300b td=1024
INFO [10-24|14:16:13] Loaded most recent local full block number=0 hash=6231b0…a0300b td=1024
INFO [10-24|14:16:13] Loaded most recent local fast block number=0 hash=6231b0…a0300b td=1024
INFO [10-24|14:16:13] Regenerated local transaction journal transactions=0 accounts=0
INFO [10-24|14:16:13] Starting P2P networking
INFO [10-24|14:16:16] UDP listener up self=enode://9ab0834740439ca962b4e2b43d85f555cff7585c0b4bf3a076f16a95ad6037beab7f8b9f2b9691c60c128cf212f38fe469930016cde71574b84733457ed45813@[::]:3000
INFO [10-24|14:16:16] RLPx listener up self=enode://9ab0834740439ca962b4e2b43d85f555cff7585c0b4bf3a076f16a95ad6037beab7f8b9f2b9691c60c128cf212f38fe469930016cde71574b84733457ed45813@[::]:3000
INFO [10-24|14:16:16] IPC endpoint opened url=\\\\.\\pipe\\geth.ipc
INFO [10-24|14:16:16] HTTP endpoint opened url=http://192.168.1.168:8545 cors=* vhosts=localhost
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.3-stable-329ac18e/windows-amd64/go1.10
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

>

启动私有链之后就可以在控制台中进行账号操作和挖矿等相关操作了 ,启动命令加上 –gcmode archive 表示将区块信息及时保存,不然重启geth后区块数据可能会丢失,还原到上次启动之前的状态。

注意:如果是云服务器,要提供rpc服务给其他电脑使用的话,rpcaddr要填写云服务器的内网地址。

账号和挖矿等操作请参见我的另一篇文章:Ubuntu以太坊私有链搭建

图形化客户端

除了使用命令行操作,以太坊还提供了图形界面MistEthereum Wallet,两者功能上相同,下载其中一个即可

下载地址:https://github.com/ethereum/mist/releases/ ,windows建议下载.zip免安装压缩版,解压就可以用了。exe后缀的启动程序时好像会一直卡在“Downloading new node”的启动页面。

国内无法访问的话可以到以太坊爱好者的网站下载:http://ethfans.org/wikis/Wallet-Mirror

安装完成打开程序,第一次打开需要一段时间。客户端默认是同步公链区块的,所以想要在图形界面进入私有链,必须先在命令行启动私有链再打开客户端。进入私有链客户端启动界面的右上角会有PRIVATE-NET标记,进入程序后中间也有Private-net的标记。

如果要连接远程的服务器,那需要在命令行填写参数的方式启动,进入到MistEthereum Wallet的安装目录,执行下面的命令即可

1
./mist.exe --rpc http://Your_Server_IP:8545
-------------本文结束感谢您的阅读-------------
🐶 您的支持将鼓励我继续创作 🐶