(一)MAC搭建以太坊私有链环境
2019-11-23 本文已影响0人
八爪星球
如何在MAC系统上搭建一个以太坊的私有链!
1、安装MAC包管理神器HomeBrew
官网地址:https://brew.sh/
Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能,首先安装Homebrew。
打开终端命令行,运行下面的URL
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
(命令会更改,建议上官网复制)
检测
1 brew -v
2 Homebrew 2.1.16
2、用Homebrew安装go语言版本的以太坊客户端geth
geth是go版本客户端,是目前主流的链接以太坊网络的客户端。
1 brew tap ethereum/ethereum
2 brew install ethereum
测试是否安装成功
1 geth --help //能成功显示输出帮助,则表示已经成功安装
3、搭建私有链
准备创世区块的json文件:genesis.json
{
"config": {
"chainId": 628, //值自定义
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc" : {},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x20000",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}
初始化,创建创世区块
- 创建genesis.json文件
mkdir TestChainDemo
cd TestChainDemo
vi genesis.json
- 创建私有链
#这个init 务必要加上
geth --datadir data0 init genesis.json
- 执行效果
运行上面的命令,会读取genesis.json文件,根据其中的内容,将创世区块写入到区块链中。如果看到以下的输出内容,说明初始化成功了。
[ddgod ~/eth/TestChainDemo]$ geth --datadir data0 init genesis.json
INFO [07-16|07:49:26] Maximum peer count ETH=25 LES=0 total=25
INFO [07-16|07:49:26] Allocated cache and file handles database=/Users/duke/ethStudy/myPrivateNet/node1/geth/chaindata cache=16 handles=16
INFO [07-16|07:49:26] Writing custom genesis block
INFO [07-16|07:49:26] Persisted trie from memory database nodes=0 size=0.00B time=8.302µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [07-16|07:49:26] Successfully wrote genesis state database=chaindata hash=942f59…a2588a
INFO [07-16|07:49:26] Allocated cache and file handles database=/Users/duke/ethStudy/myPrivateNet/node1/geth/lightchaindata cache=16 handles=16
INFO [07-16|07:49:26] Writing custom genesis block
INFO [07-16|07:49:26] Persisted trie from memory database nodes=0 size=0.00B time=1.506µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [07-16|07:49:26] Successfully wrote genesis state database=lightchaindata hash=942f59…a2588a
初始化成功后,会在数据目录data0中生成geth和keystore两个文件夹,此时目录结构如下:
data0
├── geth
│ ├── chaindata
│ │ ├── 000001.log
│ │ ├── CURRENT
│ │ ├── LOCK
│ │ ├── LOG
│ │ └── MANIFEST-000000
│ └── lightchaindata
│ ├── 000001.log
│ ├── CURRENT
│ ├── LOCK
│ ├── LOG
│ └── MANIFEST-000000
└── keystore
四、启动私有链节点
geth --datadir data0 --networkid 1024 console
上面命令的主体是geth console,表示启动节点并进入交互式控制台,--datadir选项指定使用data0作为数据目录,--networkid选项后面跟一个数字,这里是1024,表示指定这个私有链的网络id为1024。网络id在连接到其他节点的时候会用到,以太坊公网的网络id是1,为了不与公有链网络冲突,运行私有链节点的时候要指定自己的网络id。
运行上面的命令后,就启动了区块链节点并进入了Javascript Console:
INFO [07-16|07:50:02] IPC endpoint opened url=/Users/duke/ethStudy/myPrivateNet/node1/geth.ipc
Welcome to the Geth JavaScript console!
instance: Geth/v1.8.11-stable-dea1ce05/darwin-amd64/go1.10.3
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
> INFO [07-16|07:50:02] Mapped network port proto=tcp extport=30301 intport=30301 interface="UPNP IGDv1-IP1"
>
>