文章目录
前言
本文大量摘抄了一些互联网的文章 ,主要是为了方便查询,如有侵权,请及时与本文联系,本文仅仅介绍了 以太坊私服的搭建和调试, 不涉及合约.
如果你想发布一个币种,那就要部署智能合约,那么可以直接跳过这个章节,从我的另外一个章节开始
https://mp.csdn.net/postedit/81784750
centos环境准备
以root账号登录
su
#输入root 密码
更新软件
如果你是开发人员的最好更新一下
$ yum update -y && yum install git wget bzip2 vim gcc-c++ ntp epel-release nodejs cmake -y
安装golang
注意:
- 请用1.9.2以上版本,否则后期编译go ethereum的时候会出错。
- 如果已经安装了 go 先卸载go (自行baidu)
- 检查一下go是否安装过 ,也可在之后安装后检查一下go的版本
go version
- 下载&安装
wget https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz
解压到/usr/local
tar -C /usr/local -zxvf go1.10.3.linux-amd64.tar.gz
如果想要卸载
go -rf 安装目录
如果已经安装过go ,一定要用 vi /etc/profile
方式修改配置,
一般自定义的配置在 /etc/profile 文件最下方。
export GOPATH=/root/gowork export GOROOT=/usr/local/go export PATH=$PATH:$GOROOT/bin
如果是新安装go 用以下方法配置环境变量
echo "export GOPATH=/root/gowork" >> /etc/profile echo "export GOROOT=/usr/local/go" >> /etc/profile echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile
更新环境变量
source /etc/profile
安装git
yum remove git yum –y install git
go-ethereum
mkdir /opt cd /opt git clone https://github.com/ethereum/go-ethereum.git cd /opt/go-ethereum make all
确认一下/etc/profile文件
cat /etc/profile
如果已经有相关配置,如果路径不一致,就需要手动修改
export PATH=$PATH:/opt/go-ethereum/build/bin
如果没有,可以用脚本直接添加到 /etc/profile
echo "export PATH=$PATH:/opt/go-ethereum/build/bin" >> /etc/profile
刷新环境变量
source /etc/profile
验证是否安装成功
geth version
搭建私有链
建私有链文件夹
mkdir -p /opt/eth/app1/node
一定要先创建账号
-
创建密码
tee /opt/eth/app1/pass.txt <<-'EOF' 111111 EOF
-
创建账号
geth --datadir=/opt/eth/app1/node --password /opt/eth/app1/pass.txt account new > account.txt
-
如果已经有账号了可以导入账号 < keyfile > 是指密钥文件
geth account import /opt/eth/app1/node/keystore/<keyfile> --datadir= /opt/eth/app1/node --password /opt/eth/app1/pass.txt >> account.txt
-
查看钱包地址
cat account.txt
注意虚拟机是非常难挖到矿的。所以我们先创建账号,然后把初始账号和账号预分配的GAS 配到创世块的配置文件中
配置创世块
- 配置genesis.json
genesis.json文件名可以自定义 ,把 之前 account.txt 中的账号 前面加上 ‘0x’ 后 放到allow 中,设置balance。 - 生成 创世块json配置文件 如下:
tee /opt/eth/app1/genesis.json <<-'EOF' { "config": { "chainId": 520, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0 }, "alloc" : {"0x7270522dbb86657b702c0d785e6be893971d2c8c":{"balance": "0x700000000000000000"}}, "coinbase" : "0x0000000000000000000000000000000000000000", "difficulty" : "0x400", "extraData" : "", "gasLimit" : "0x2fefd8", "nonce" : "0x0000000000000038", "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", "timestamp" : "0x00" } EOF
- 初始化传世块
geth --datadir "/opt/eth/app1/node" init /opt/eth/app1/genesis.json
制作服务脚本
-
独立模式的
tee /opt/eth/app1/seth-d.sh <<-'EOF' geth --identity "miner" --networkid 520 --datadir "/opt/eth/app1/node" --nodiscover --rpc --rpcapi "personal,db,eth,net,web3,miner" --rpcaddr "127.0.0.1" --rpcport "8486" --port "30303" --dev.period 1 console EOF
-
公开访问模式
tee /opt/eth/app1/seth.sh <<-'EOF' geth --identity "server" --networkid 520 --datadir "/opt/eth/app1/node" --rpc --rpcapi "db,eth,net,web3" --rpcaddr "0.0.0.0" --rpccorsdomain "*" --rpcport "8545" --port "30303" console EOF
-
提升运行权限
chmod 700 /opt/eth/app1/seth-d.sh chmod 700 /opt/eth/app1/seth.sh
运行
- 启动挖矿模式的服务脚本
/opt/eth/app1/seth-d.sh
- 启动合约部署模式的服务脚本
/opt/eth/app1/seth.sh
防火墙
-
配合geth需要开放的端口开启防火墙
systemctl start firewalld
-
放开端口
firewall-cmd --zone=public --add-port=8545/tcp --permanent firewall-cmd --zone=public --add-port=30303/tcp --permanent
连接
只有服务开启时才能,通过第二终端连接
geth attach ipc:/opt/eth/app1/node/geth.ipc
geth attach http://127.0.0.1:8545
参考
geth 常用指令
-
查询账号
geth --datadir "/opt/eth/app1/node" account list
-
查看 cionbase
eth.coinbase
-
查看账号余额
myAddress = "0x7270522dbb86657b702c0d785e6be893971d2c8c"
eth.getBalance(myAddress)
eth.getBalance(eth.coinbase)
eth.getBalance(eth.accounts[0])
-
查看账号
personal.listAccounts
-
新建账号,密码123456
personal.newAccount("123456")
记得把账号密码保存下来
“0x13adfe5d53042411376465a2b1b6a939358bc945” -
查看区块数量
eth.blockNumber
-
指定挖矿用户
miner.setEtherbase(eth.accounts[n])
-
开挖
在合约部署,交易的时候 如果不开启挖矿指令交易无法流通到达各个节点
miner.start(1)
-
停止挖矿
miner.stop()
-
解锁挖矿用户
在合约部署,交易的时候 ,必须解锁对应的用户
personal.unlockAccount(eth.coinbase)
##配置项简介
我们对配置项的内容进行一下简单的介绍。
- alloc
用来预置账号以及账号的以太币数量,因为私有链挖矿比较容易,所以不需要预置有币的账号,需要的时候自己创建即可以。实例代码如下:
“alloc”: {
“de1e758511a7c67e7db93d1c23c1060a21db4615”: {
“balance”: “1000”
},
“27dc8de9e9a1cb673543bd5fce89e83af09e228f”: {
“balance”: “1100”
},
“d64a66c28a6ae5150af5e7c34696502793b91ae7”: {
“balance”: “900”
} - nonce:
一个64位随机数,用于挖矿,和mixhash的设置需要满足以太坊的Yellow paper, 4.3.4.Block Header Validity (44)章节所描述的条件。 - difficulty:
设置计算区块的难度,如果数值过大,挖矿时间较长,在测试环境为节省算力和等带时间可设置较小值。 - mixhash
与nonce配合用于挖矿,由上一个区块的一部分生成的hash。和nonce的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。 - coinbase:
矿工账号,随便填写。 - timestamp:
设置创世块的时间戳。 - parentHash:
上一个区块的hash值,因为是创世块,所以这个值是0。 - extraData:
附加信息,随便填,可以填你的个性信息,必须为十六进制的字符串。 - gasLimit:
该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,因为是私有链,所以填最大。
geth其他参数:
- 进入后直接开挖
--mine --dev.period 1 console
必须有 - 密码解锁
--unlock 0 --password ./pass.txt
- 指定geth.ipc位置
--ipcpath "/root/Library/Ethereum/geth.ipc"
修改ipcpath路径 在~/Library/Ethereum/geth.ipc
geth attach ipc:/root/Library/Ethereum/geth.ipc
默认在节点目录下
geth attach ipc:./miner2/geth.ipc