Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | Github | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

第 3 章 以太坊私链入门

目录

3.1. 软件安装与配置
3.1.1. Ubuntu
3.1.1.1. 安装 geth
3.1.1.2. 安装 solc
3.1.1.3. Node.js
3.1.2. CentOS 7
3.1.3. Windows
3.1.4. Mac OS
3.1.4.1. 安装 Node
3.1.4.2. Ethereum Wallet
3.1.5. 编译安装
3.1.6. Netkiller OSCM 一键安装
3.1.6.1. 1.8.10
3.1.6.2. 1.8.1
3.1.6.3. 1.8.10
3.1.7. 防止 geth 异常退出
3.2. 创世区块
3.2.1. 初始化创世区块
3.2.2. 创建主账号
3.2.3. 启动节点
3.2.4. 使用节点进行挖矿
3.2.4.1. 启动矿工开始挖矿
3.2.4.2. 停止挖矿
3.2.4.3. 查看所挖金额
3.2.5. 在创世链中制定矿工账号并为它充值
3.3. Blockchain explorer (区块链浏览器)
3.4. 单机多实例演示
3.4.1. 实例一
3.4.2. 实例二
3.4.3. 添加节点
3.4.4. 节点测试
3.5. 使用 pm2 启动以太坊

本文所用软件版本

3.1. 软件安装与配置

3.1.1. Ubuntu

3.1.1.1. 安装 geth

安装环境



Ubuntu 17.10

				
sudo apt upgrade -y
sudo apt install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt update
sudo apt install ethereum			
				
				
				
neo@netkiller ~ % geth version
Geth
Version: 1.8.10-stable
Git Commit: 4bb3c89d44e372e6a9ab85a8be0c9345265c763a
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.9.5
Operating System: linux
GOPATH=
GOROOT=/usr/lib/go-1.9
				
				

3.1.1.2. 安装 solc

				
sudo apt install solc -y
				
				
				
neo@netkiller ~ % solc --version
solc, the solidity compiler commandline interface
Version: 0.4.19+commit.c4cbbb05.Linux.g++				
				
				

3.1.1.3. Node.js

				
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs				
				
				

3.1.2. CentOS 7

			
yum update -y
yum install git wget bzip2 -y	
yum install golang -y

cd /usr/local/src
git clone https://github.com/ethereum/go-ethereum.git
cd go-ethereum/
gmake all
mv build /srv/go-ethereum

echo "export PATH=$PATH:$PWD/build/bin" >> /etc/profile
source /etc/profile		
			
			

上面安装版本是 unstable 版本,如果是生产环境请使用 Release 版本 https://github.com/ethereum/go-ethereum/tags

			
wget https://github.com/ethereum/go-ethereum/archive/v1.8.10.tar.gz
tar zxvf v1.8.10.tar.gz
cd go-ethereum-1.8.10/
gmake all

mv build /srv/go-ethereum-1.8.10
			
			

3.1.3. Windows

				
访问 https://geth.ethereum.org/downloads/
下载并安装 Geth for Windows				
				
			

3.1.4. Mac OS

				
brew update
brew upgrade				
brew tap ethereum/ethereum
brew install ethereum
brew install solidity
				
			

3.1.4.1. 安装 Node

				
brew install node
brew install npm
npm config set registry https://registry.npm.taobao.org
				
				

3.1.4.2. Ethereum Wallet

下载安装以太坊钱包(大陆网络可能下载有问题,需要翻墙)

https://github.com/ethereum/mist/releases/download/v0.9.3/Ethereum-Wallet-macosx-0-9-3.dmg

3.1.5. 编译安装

				
git clone https://github.com/ethereum/go-ethereum
sudo apt-get install -y build-essential golang
cd go-ethereum
make geth
				
			

3.1.6. Netkiller OSCM 一键安装

Netkiller OSCM 是由 Netkiller 制作的一套自动化安装脚本。

适用于 CentOS 7

3.1.6.1. 1.8.10

			
curl -s https://raw.githubusercontent.com/oscm/shell/master/blockchain/ethereum/centos/go-ethereum-1.8.10.sh | bash			
			
				

安装完成后使用下面命令进入控制台

			
[root@localhost ~]# su - ethereum
Last login: Sat Feb  3 00:23:52 EST 2018 on pts/0
			
[ethereum@localhost ~]$ geth attach
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.10-stable/linux-amd64/go1.8.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

> 
			
				

3.1.6.2. 1.8.1

				
curl -s https://raw.githubusercontent.com/oscm/shell/master/blockchain/ethereum/centos/go-ethereum-1.8.1.sh | bash
				
				

3.1.6.3. 1.8.10

				
curl -s https://raw.githubusercontent.com/oscm/shell/master/lang/gcc/gcc.sh | bash
curl -s https://raw.githubusercontent.com/oscm/shell/master/lang/golang/golang-1.10.2.sh | bash
curl -s https://raw.githubusercontent.com/oscm/shell/master/blockchain/ethereum/centos/go-ethereum-1.8.10.sh | bash
curl -s https://raw.githubusercontent.com/oscm/shell/master/blockchain/ethereum/systemd/private.sh | bash

curl -s https://raw.githubusercontent.com/oscm/shell/master/lang/node.js/binrary/node-v10.1.0.sh | bash
curl -s https://raw.githubusercontent.com/oscm/shell/master/lang/node.js/binrary/profile.d.sh | bash
curl -s https://raw.githubusercontent.com/oscm/shell/master/blockchain/ethereum/truffle/truffle.sh | bash
				
				

3.1.7. 防止 geth 异常退出

在同步主网的过程中,我们发现经常出现geth崩溃退出,为了防止异常退出,我们写了这个脚本。

		
[ethereum@netkiller ~]$ cat run.sh 
#!/bin/bash
for (( ; ; ))
do
	#geth --syncmode light --cache 2048 --maxpeers 200
	# geth --syncmode light --cache 2048 --maxpeers 200 --rpc --rpcaddr 0.0.0.0 --rpcport 7545 --rpcapi web3,admin,eth,personal --port 30303 2> /tmp/geth.log
	geth --datadir private --networkid 44444 --port 30302 --mine --rpc
	#geth --testnet --syncmode light --cache 2048 --maxpeers 200 --rpc 2> /tmp/geth.log
	sleep 10

done &