版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yujuan110/article/details/83721440
我的系统:CentOS7 64位
环境需要:Go 1.9以上版本
geth工具
WARN [11-04|09:34:47.411] System clock seems off by -10h15m43.047110838s, which can prevent network connectivity
WARN [11-04|09:34:47.411] Please enable network time synchronisation in system settings.
一.环境搭建
1.安装Go
下载源码 https://golang.org/dl/
找到适合自己操作系统的版本,必须1.9版本以上。我安装的是1.9.5
解压到/usr/local下
tar -zxvf go1.9.5.linux-amd64.tar.gz -C /usr/local/
创建软链接
ln -s /usr/local/go/bin/bo /usr/bin/go
测试是否成功
go version
#go version go1.9.5 linux/amd64
2.安装geth工具
下载geth源码
git clone https://github.com/ethereum/go-ethereum.git
解压到/usr/local/下
unzip -d /usr/local/ go-ethereum-master.zip
编译源码
cd /usr/local/go-ethereum
make
成功的话会出现如下:
Done building.
Run "/usr/local/go-ethereum-master/build/bin/geth" to launch geth.
创建软链接 这样我们可以直接用geth命令,而不用输入路径
ln -s /usr/local/go-ethereum-master/build/bin/geth /usr/bin/geth
二.搭建私有链
1.创建用户
mkdir geth
cd geth
进入geth控制台
geth --datadir db --nodiscover console
创建用户
> personal.newAccount()
Passphrase:
Repeat passphrase:
"0x12844bb3206f10a331557bffb7c8d34ee4ca8b65"
> personal.newAccount()
Passphrase:
Repeat passphrase:
"0xa6faa81cad6a3b038d9a51db80cedbe65184c7e2"
查看用户余额
> eth.getBalance(eth.accounts[0])
0
> eth.getBalance(eth.accounts[1])
0
给用户创建别名
> yujuan=eth.accounts[0]
"0x12844bb3206f10a331557bffb7c8d34ee4ca8b65"
> hcb=eth.accounts[1]
"0xa6faa81cad6a3b038d9a51db80cedbe65184c7e2"
可以看到在db目录下,有个keystore目录,里面已经存了我们刚刚创建的两个账户。
[root@localhost mychain]# tree
.
├── geth
│ ├── chaindata
│ │ ├── 000001.log
│ │ ├── CURRENT
│ │ ├── LOCK
│ │ ├── LOG
│ │ └── MANIFEST-000000
│ ├── LOCK
│ ├── nodekey
│ ├── nodes
│ │ ├── 000001.log
│ │ ├── CURRENT
│ │ ├── LOCK
│ │ ├── LOG
│ │ └── MANIFEST-000000
│ └── transactions.rlp
├── geth.ipc
└── keystore
├── UTC--2018-11-04T16-02-52.181405978Z--12844bb3206f10a331557bffb7c8d34ee4ca8b65
└── UTC--2018-11-04T16-03-15.055116889Z--a6faa81cad6a3b038d9a51db80cedbe65184c7e2
2.传世区块配置文件
cd mychain
vim gensis.json
在gensis.json中输入如下内容
{
"alloc": {
"0xc9228294cc6bc3e3fcdba0f5d393d68f920c7789": {
"balance": "999000000000000000000"
}
},
"config":{
"chainId":15,
"homesteadBlock":0,
"eip155Block":0,
"eip158Block":0
},
"nonce":"0x0000000000000001",
"mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x01",
"coinbase":"0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x777573686f756865",
"gasLimit":"0xffffffff"
}
2.初始化
准备好创世区块配置文件后,需要初始化区块链,将上面的传世区块信息写入到区块链中。
新建一个目录用来存放区块链数据
mkdir db
进入geth目录,开始初始化区块
geth --datadir "./db" init gensis.json
–datadir 指定数据存放目录
成功的话可以看到如下结果
[root@localhost geth]# geth --datadir "./db" init gensis.json
WARN [11-04|09:17:30.515] Sanitizing cache to Go's GC limits provided=1024 updated=324
INFO [11-04|09:17:30.520] Maximum peer count ETH=25 LES=0 total=25
INFO [11-04|09:17:30.523] Allocated cache and file handles database=/home/blockchain/geth/db/geth/chaindata cache=16 handles=16
INFO [11-04|09:17:30.548] Writing custom genesis block
INFO [11-04|09:17:30.549] Persisted trie from memory database nodes=1 size=149.00B time=111.297µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [11-04|09:17:30.550] Successfully wrote genesis state database=chaindata hash=647de5…06c17e
INFO [11-04|09:17:30.550] Allocated cache and file handles database=/home/blockchain/geth/db/geth/lightchaindata cache=16 handles=16
INFO [11-04|09:17:30.562] Writing custom genesis block
INFO [11-04|09:17:30.563] Persisted trie from memory database nodes=1 size=149.00B time=1.13319ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [11-04|09:17:30.563] Successfully wrote genesis state database=lightchaindata hash=647de5…06c17e
并且在db目录中会生成geth和keystore两个文件夹,此时目录结构如下:
[root@localhost geth]# tree ./
./
├── db
│ ├── geth
│ │ ├── chaindata
│ │ │ ├── 000001.log
│ │ │ ├── CURRENT
│ │ │ ├── LOCK
│ │ │ ├── LOG
│ │ │ └── MANIFEST-000000
│ │ └── lightchaindata
│ │ ├── 000001.log
│ │ ├── CURRENT
│ │ ├── LOCK
│ │ ├── LOG
│ │ └── MANIFEST-000000
│ └── keystore
└── gensis.json
5 directories, 11 files
chaindata是存放区块数据,keystore是存放账户数据