以太坊私有链搭建
搭建环境:
Ubuntu16.04.1 amd64
Vmware Workstation
参考:
GitHub以太坊官方项目Go-Ethereum
精通以太坊
Go-Ethereum/wiki
0x00-安装前准备
①将source list源更换为阿里云。
②常用工具安装1
2
3$ sudo apt-get install git curl wget -y
$ sudo apt-get install openssh-*
$ sudo /etc/init.d/ssh restart
0x01-Install Go环境
注意:Ubuntu的版本/go的版本/安装的途径(manager 如apt-get等或者source code)
官网
笔者使用源代码安装方式
Building from source code
下载,解压至/usr/local目录1
2$ wget https://dl.google.com/go/go1.11.linux-amd64.tar.gz
$ sudo tar -C /usr/local -xvf go1.11.linux-amd64.tar.gz
设置GOPATH和PATH
1 | $ mkdir -p ~/go; echo "export GOPATH=$HOME/go" >> ~/.bashrc |
设置用户环境1
$ source ~/.bashrc
至此源码安装完成。
Go:GitHub项目
Go:Github/wiki/Ubuntu
Go:其他源码安装
Installing from PPA
Install-for-Ubuntu16.04-LTS1
2
3$ sudo add-apt-repository ppa:longsleep/golang-backports
$ sudo apt-get update
$ sudo apt-get install golang-go
try following maybe old
1 | $ sudo apt-get install golang-go |
unsing following command 安装指定版本
1 | $ sudo add-apt-repository ppa:gophers/archive |
若想安装最新版使用下面命令
1 | $ sudo snap install --classic go |
Hello world by Go
Go语言实例
创建一个hello.go文件,并粘贴以下测试代码1
2
3
4
5$ cd $GOPATH
$ mkdir src && cd src
$ touch hello.go
$ nano hello.go
测试代码1
2
3
4
5package main
import "fmt"
func main(){
fmt.Println("Hello, 世界")
}
and then
1 | $ go run hello.go |
0x02-Building Go Ethereum
注意:安装的途径(manager 如 apt-get等或者source code)/geth的版本
笔者使用git clone下载的代码进行部署
Building from source code
注:请确保已安装Go和C语言编译器,请参考0x01
官方源代码安装1
$ git clone https://github.com/ethereum/go-ethereum.git
未科学上网下载速度较慢
笔者先在本机下载好之后在复制到虚拟机1
2
3
4$ git clone https://github.com/ethereum/go-ethereum.git
文件较大,先压缩再scp
$ tar -czvf go-ethereum.tar.gz go-ethereum/
$ scp go-ethereum.tar.gz username@vmip:$GOPATH/src/
回到虚拟机1
2
3
4
5
6
7
8
9
10
11$ cd $GOPATH/src/
$ tar -xvf go-ethereum.tar.gz
$ cd go-ethereum
$ make geth
build/env.sh go run build/ci.go install ./cmd/geth
>>> /usr/local/go/bin/go install -ldflags -X main.gitCommit=bfce00385f1c8dab222b7ddab6c336177a5ae731 -v ./cmd/geth
github.com/ethereum/go-ethereum/vendor/golang.org/x/sys/unix
................................
github.com/ethereum/go-ethereum/cmd/geth
Done building.
Run "/home/sun/go/src/go-ethereum/build/bin/geth" to launch geth.
添加geth终端命令1
2$ echo "export PATH=$PATH:$HOME/go/src/go-ethereum/build/bin" >> ~/.bashrc
$ source ~/.bashrc
start your node.
已添加geth到终端命令
可以直接运行geth启动节点1
$ geth
或者
运行build/bin/geth,当前路径为:go/src/go-ethereum/1
$ ./build/bin/geth
1 | INFO [09-12|18:57:43.187] Maximum peer count ETH=25 LES=0 total=25 |
Installing from 包管理器
下面两种方式,如果没有科学上网,速度较慢。1
2
3$ sudo apt-get install git -y
$ go get -d github.com/ethereum/go-ethereum
$ go install github.com/ethereum/go-ethereum/cmd/geth
或者
在Ubuntu 上使用包管理器 PPAs安装,先添加库
1 | $ sudo apt-get install software-properties-common |
此时便可安装最新稳定版的 Go Ethereum1
2$ sudo apt-get update
$ sudo apt-get install ethereum
如果你想尝鲜使用开发版本,则只需执行:1
2$ sudo apt-get update
$ sudo apt-get install ethereum-unstable
0x03-搭建以太坊私有链
远航|以太坊测试私链的搭建
Private network
搭建以太坊私有链主要有以下事项
- 自定义创世块(Genesis Block)
- 自定义数据存放位置
- 自定义网络ID
- 关闭节点发现(Node Discovery)(推荐)
自定义创世区块
首先需要为私有链定义一个创世状态,使用 JSON 文件定义,例如取名为:genesis.json
我在用户根目录,新建json文件,写入以下定义1
2$ touch genesis.json
$ nano genesis.json
an example of a custom genesis.json file1
2
3
4
5
6
7
8
9
10
11
12
13
14{
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"difficulty": "200000000",
"gasLimit": "2100000",
"alloc": {
"7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" },
"f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000" }
}
}
在创世区块配置文件中,有四个参数必须要指定的:
- config:区块链的相关参数
- chainId:防止重放攻击(replay attack,简单来说就是未经授权的用户来假扮交易的发送者)
- homesteadBlock:Homestead是以太坊的第二个发行版本(Frontier是第一个发行版本),值为0表示的使用的是该版本的以太坊
- difficult:初始挖矿难度
- gasLimit:每一个区块所消耗的gas上限
- alloc:可以往地址中预先分配以太币
生成创世区块
要创建使用上述genesis块的数据库,运行以下命令。这将导入并设置链的规范生成块1
$ geth --datadir path/to/custom/data/folder init genesis.json
- –datadir:指定区块链的数据目录(默认为~/.ethereum)
- init:创世块初始化JSON文件的目录
笔者Demo,如果之前使用过geth命令则会报错已存在database,请删除后重新运行。1
$ geth --datadir ~/.ethereum/ init genesis.json
1 | INFO [09-18|17:42:20.031] Maximum peer count ETH=25 LES=0 total=25 |
Future runs of geth on this data directory will use the genesis block you have
defined.
1 | $ geth --datadir path/to/custom/data/folder --networkid 15 |
启动以太坊私有测试链,创建私有网络
相关启动参数
启动geth有如下主要参数(可以使用geth –help进行查看):
- –nodiscover:关闭节点的可发现性,可以防止使用了相同network id和创世块的节点连接到你的区块链网络中(只能通过手动来添加节点)
- –maxpeers 0:指定网络中的最多节点数
- –rpc:启用RPC服
- –rpcapi “db,eth,net,web3”:指定启用的RPC API
- –rpcport “8080”:指定RPC的端口
- –rpccorsdomain:指定哪些URL可以连接到你的节点
- –datadir:以太坊区块链的数据目录
- –port:连接到其它节点的网络监听端口
- –identity “FirstNode”:指定节点名称
- console:启动geth控制台程序
启动geth
使用以下命令来启动geth:
但如何设置网络ID????1
$ geth --identity "ETH-MainNode" --rpc --rpcport "6060" --rpccorsdomain "*" --datadir "path/to/custom/data/folder" --port "30303" --nodiscover --maxpeers 5 --rpcapi "admin,db,eth,debug,miner,net,shh,txpool,personal,web3" --networkid 3131 console
geth command简介
0x04 安装Mist钱包
Mist GitHub
安装前准备
在Linux安装.zip安装包需要先安装:libgconf2-41
apt-get install libgconf2-4
安装nodejs
1 | $ wget https://nodejs.org/dist/v8.9.1/node-v8.9.1-linux-x64.tar.xz |
安装Meteor
1 | $ curl https://install.meteor.com/ | sh |
下为终端过程,略。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 7786 0 7786 0 0 2260 0 --:--:-- 0:00:03 --:--:-- 2260
Downloading Meteor distribution
Retrying download in 5 seconds...
######################################################################## 100.0%
Meteor 1.7.0.5 has been installed in your home directory (~/.meteor).
Writing a launcher script to /usr/local/bin/meteor for your convenience.
This may prompt for your password.
[sudo] password for sun:
To get started fast:
$ meteor create ~/my_cool_app
$ cd ~/my_cool_app
$ meteor
Or see the docs at:
docs.meteor.com
安装Yarn package manager
1 | $ curl -o- -L https://yarnpkg.com/install.sh | bash |
下为终端过程,略。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 7041 0 7041 0 0 2745 0 --:--:-- 0:00:02 --:--:-- 2745
Installing Yarn!
> Downloading tarball...
[1/2]: https://yarnpkg.com/latest.tar.gz --> /tmp/yarn.tar.gz.l22qYRpji8
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 91 100 91 0 0 73 0 0:00:01 0:00:01 --:--:-- 73
100 608 0 608 0 0 192 0 --:--:-- 0:00:03 --:--:-- 386
100 915k 100 915k 0 0 46827 0 0:00:20 0:00:20 --:--:-- 70029
[2/2]: https://yarnpkg.com/latest.tar.gz.asc --> /tmp/yarn.tar.gz.l22qYRpji8.asc
100 95 100 95 0 0 335 0 --:--:-- --:--:-- --:--:-- 335
100 612 0 612 0 0 767 0 --:--:-- --:--:-- --:--:-- 597k
100 832 100 832 0 0 722 0 0:00:01 0:00:01 --:--:-- 722
> Verifying integrity...
gpg: Signature made Sat 04 Aug 2018 03:53:36 AM CST using RSA key ID B6FF4DE3
gpg: Good signature from "Yarn Packaging <yarn@dan.cx>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg: There is no indication that the signature belongs to the owner.
Primary key fingerprint: 72EC F46A 56B4 AD39 C907 BBB7 1646 B01B 86E5 0310
Subkey fingerprint: E219 30C4 D0A4 AA46 1858 1F7A E074 D16E B6FF 4DE3
> GPG signature looks good
> Extracting to ~/.yarn...
> Adding to $PATH...
> We've added the following to your /home/sun/.bashrc
> If this isn't the profile of your current shell then please add the following to your correct profile:
export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
> Successfully installed Yarn 1.9.4! Please open another terminal where the `yarn` command will now be available.
安装 MIST
官网zip压缩包直接使用
https://github.com/ethereum/mist/releases1
2
3
4
5
6
7
8
9
10
11
12
13$ wget https://github.com/ethereum/mist/releases/download/v0.11.1/Mist-linux64-0-11-1.zip
$ mkdir mist
$ unzip ./Downloads/Mist-linux64-0-11-1.zip -d mist/
$ yarn
yarn install v1.9.4
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
Done in 0.04s.
附,详情见文末-其他记录-
下为另一种方式git clone from github并yarn,可以参考的链接:
GitHub-mist安装:https://github.com/ethereum/mist
csdn-mist安装:https://blog.csdn.net/Vinsuan1993/article/details/77144263
以失败告终,因错误内容较多,不在此处显示。置于文末。
0x05-mist连接geth私有网络
执行私有网络1
$ geth --datadir path/to/custom/data/folder init genesis.json
- –datadir:指定区块链的数据目录(默认为~/.ethereum)
- init:创世块初始化JSON文件的目录
连接私有网络1
$ ./mist --rpc path/to/custom/data/folder/geth.ipc
下为笔者操作1
2./mist --rpc ~/.ethereum/geth.ipc
geth --datadir ~/.ethereum/ init genesis.json
至此连接私有网络完成。
后续继续参考:
如何搭建以太坊私有链
基于以太坊构建私有区块链网络教程指南
其他记录
Stick Notes
2018年9月6日15点36分
博客园
Go-Ethereum官方GitHub
geth下载
ethereum/mist
http://ethereum.mochain.info/basic/install-geth.html#on-ubuntu
https://www.bookstack.cn/read/N-blog/book-4.15.md
golang下载
Go-Ethereum-wiki
Building-Ethereum-wiki
go-ethereum install-for-Ubuntu
安装MIST from GitHub
git clone from github并yarn,可以参考的链接:
GitHub-mist安装:https://github.com/ethereum/mist
csdn-mist安装:https://blog.csdn.net/Vinsuan1993/article/details/77144263
以失败告终1
$ git clone https://github.com/ethereum/mist.git
1 | Cloning into 'mist'... |
1 | $ cd mist |
1 | The program 'yarn' is currently not installed. You can install it by typing: |
1 | $ export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH" |
1 | yarn install v1.9.4 |
安装失败,重新运行1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94$ yarn
yarn install v1.9.4
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@1.2.4: The platform "linux" is incompatible with this module.
info "fsevents@1.2.4" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
warning " > gulp-babel@7.0.1" has unmet peer dependency "babel-core@6 || 7 || ^7.0.0-alpha || ^7.0.0-beta || ^7.0.0-rc".
warning " > gulp-spawn-mocha@3.3.1" has unmet peer dependency "istanbul@^0.4.3".
[4/4] Building fresh packages...
[1/10] ⢀ sha3
[2/10] ⠠ secp256k1
[3/10] ⠠ keccak
[4/10] ⠠ websocket
error /home/sun/mist/node_modules/sha3: Command failed.
Exit code: 1
Command: node-gyp rebuild
Arguments:
Directory: /home/sun/mist/node_modules/sha3
Output:
gyp info it worked if it ends with ok
gyp info using node-gyp@3.6.2
gyp info using node@8.11.4 | linux | x64
gyp http GET https://nodejs.org/download/release/v8.11.4/node-v8.11.4-headers.tar.gz
gyp WARN install got an error, rolling back install
gyp ERR! configure error
gyp ERR! stack Error: socket hang up
gyp ERR! stack at TLSSocket.onHangUp (_tls_wrap.js:1137:19)
gyp ERR! stack at Object.onceWrapper (events.js:313:30)
gyp ERR! stack at emitNone (events.js:111:20)
gyp ERR! stack at TLSSocket.emit (events.js:208:7)
gyp ERR! stack at endReadableNT (_stream_readable.js:1064:12)
gyp ERR! stack at _combinedTickCallback (internal/process/next_tick.js:138:11)
gyp ERR! stack at process._tickCallback (internal/process/next_tick.js:180:9)
gyp ERR! System Linux 4.15.0-29-generic
gyp ERR! command "/home/sun/nodejs/bin/node" "/home/sun/nodejs/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/sun/mist/node_modules/sha3
yarn install v1.9.4
[1/4] Resolving packages...
success Already up-to-date.
$ git submodule update --recursive && yarn task pack-wallet && (cd interface && yarn)
yarn run v1.9.4
$ gulp pack-wallet
Mist version: 0.11.2
Electron version: 1.8.4
Many gulp tasks can be run in wallet mode using: --wallet
To specify a platform (default: all) use: --linux --win
[14:54:20] Using gulpfile ~/mist/gulpfile.js
[14:54:20] Starting 'pack-wallet'...
(node:13780) UnhandledPromiseRejectionWarning: Error: /home/sun/mist/meteor-dapp-wallet/build could not
you run "git submodule update --recursive?"
at del.then (/home/sun/mist/gulpTasks/building.js:81:13)
at <anonymous>
(node:13780) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either
inside of an async function without a catch block, or by rejecting a promise which was not handled with
jection id: 1)
(node:13780) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, p
ions that are not handled will terminate the Node.js process with a non-zero exit code.
[14:54:20] The following tasks did not complete: pack-wallet
[14:54:20] Did you forget to signal async completion?
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
$ sudo apt-get install build-essential
yarn install v1.9.4
[1/4] Resolving packages...
success Already up-to-date.
$ git submodule update --recursive && yarn task pack-wallet && (cd interface && yarn)
yarn run v1.9.4
$ gulp pack-wallet
Mist version: 0.11.2
Electron version: 1.8.4
Many gulp tasks can be run in wallet mode using: --wallet
To specify a platform (default: all) use: --linux --win
[14:54:20] Using gulpfile ~/mist/gulpfile.js
[14:54:20] Starting 'pack-wallet'...
(node:13780) UnhandledPromiseRejectionWarning: Error: /home/sun/mist/meteor-dapp-wallet/build could not
you run "git submodule update --recursive?"
at del.then (/home/sun/mist/gulpTasks/building.js:81:13)
at <anonymous>
(node:13780) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either
inside of an async function without a catch block, or by rejecting a promise which was not handled with
jection id: 1)
(node:13780) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, p
ions that are not handled will terminate the Node.js process with a non-zero exit code.
[14:54:20] The following tasks did not complete: pack-wallet
[14:54:20] Did you forget to signal async completion?
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
可能存在的问题,没有科学上网有的资源无法下载。【后附图】