CentOS7搭建以太坊私有链

 一、 环境准备:Win10 64位安装 VM VirtualBox,操做系统版本:javascript

CentOS-7-x86_64-Everything-1611.iso(7.71G)。html

切换root帐号,方便安装程序前端

二、 安装Golang 1.9以上版本,yum安装的能够是1.8.3,因此要手动下载安装java

国内镜像:https://studygolang.com/dllinux

#cd /usr
#wget https://studygolang.com/dl/golang/go1.10.1.linux-amd64.tar.gz
# tar -C /root -xzf go1.10.1.linux-amd64.tar.gz
# cd /root; vi ~/.bashrc
export GOPATH=/root/Go
export GOROOT=/root/go
export PATH=$PATH:$GOROOT/bin
# source ~/.bashrc
# go version
go version go1.10.1 linux/amd64

三、 安装go版本的以太坊源码git

#cd /usr
#git clone https://github.com/ethereum/go-ethereum
#cd go-ethereum
#make geth

四、 初始化一个创世区块github

初始化创世区块时,要先建立一个genesis.json文件,utf-8编码,内容以下:golang

{
  "config": {
        "chainId": 15,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "coinbase" : "0x0000000000000000000000000000000000000000",
    "difficulty" : "0x40000",
    "extraData" : "",
    "gasLimit" : "0xffffffff",
    "nonce" : "0x0000000000000042",
    "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp" : "0x00",
    "alloc": { }
}

  

参数名称web

参数描述json

mixhash

与nonce配合用于挖矿,由上一个区块的一部分生成的hash。注意他和nonce的设置须要知足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。

nonce

nonce就是一个64位随机数,用于挖矿,注意他和mixhash的设置须要知足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。

difficulty

设置当前区块的难度,若是难度过大,cpu挖矿就很难,这里设置较小难度

alloc

用来预置帐号以及帐号的以太币数量,由于私有链挖矿比较容易,因此咱们不须要预置有币的帐号,须要的时候本身建立便可以。

coinbase

矿工的帐号,随便填

timestamp

设置创世块的时间戳

parentHash

上一个区块的hash值,由于是创世块,因此这个值是0

extraData

附加信息,随便填,能够填你的个性信息

gasLimit

该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,由于咱们是私有链,因此填最大。

  

接下来,咱们使用geth init ./inti.json --datadir "./chain"命令,来进行创世区块的初始化,当前区块链网络数据存放的位置会保存在chain目录中:

#cd /usr/go-ethereum/bulid/bin
# ./geth  --datadir "./chain" init genesis.json

 

五、 启用私有链

 

参数名称

参数描述

datadir

设置当前区块链网络数据存放的位置

console

启动命令行模式,能够在Geth中执行命令

nodiscover

私有链地址,不会被网上看到

使用如下命令,启用私有链:

./geth --datadir "./chain" --nodiscover console 2>>eth_output.log

./geth \
  --datadir "./chain" \
  --nodiscover \
  console 2>>eth_output.log    

 在当前目录执行tail -f eth_output.log,能够看到输出日志。 

后面章节中的命令,都是在启动私有链后的Geth javascript console中操做 

六、 账户的添加和查看

查看账户,能够看到当前账户是空的

> web3.eth.accounts
[] 
 建立账户的方式有两种,第一种建立账户时直接初始化密码
> web3.personal.newAccount("123456")
"0x741d379e702f95ea8fdf96df9d8aa34e31b011e9"
"0xe0b0bf3b64e238814dede73eca9f16e51f386819" 

其中返回的0x741d379e702f95ea8fdf96df9d8aa34e31b011e9是账户,123456是账户的密码

  
  第二种方法是先建立帐户,而后输入密码

> web3.personal.newAccount()
Passphrase:
Repeat passphrase:
"0x3b0ec02b4193d14abdc9cc5b264b5e3f39624d70"

  这时咱们再查看账户,可以看到刚才建立的两个账户已经存在了

> web3.eth.accounts
["0xbe323cc4fde114269a9513a27d3e985f82b9e25d", "0x3b0ec02b4193d14abdc9cc5b264b5e3f39624d70"] 

七、 用到的命令汇总:

./geth --datadir "./chain" --nodiscover console 2>>eth_output.log
web3.eth.accounts
web3.personal.newAccount("123456") 
miner.start(
1) //办公电脑挖矿较慢 // miner.start(1) 返回 null时,执行如下2语句: eth.coinbase miner.setEtherbase(eth.coinbase) miner.stop() web3.eth.getBalance("0x741d379e702f95ea8fdf96df9d8aa34e31b011e9") acc0 = web3.eth.accounts[0] acc1 = web3.eth.accounts[1] acc2 = web3.eth.accounts[2] web3.eth.getBalance(acc0) web3.fromWei(web3.eth.getBalance(acc0)) 

八、 Linux命令

查看当前端口:netstat –ntlp

杀死指定进程:kill -9 pid

查看日志:tail -f eth_output.log

 

九、 参考教程:http://www.cnblogs.com/lion.net/p/7809862.html

相关文章
相关标签/搜索