手把手教你从源代码开始搭建多节点以太坊私链(二)搭建第一个节点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sitebus/article/details/83347312

搭建第一个以太坊节点

go-ethereum客户端通常被称为geth,它是个命令行界面,执行在Go语言的运行时环境上,实现了完整以太坊节点。以太坊的官方网站地址为:https://ethereum.github.io/go-ethereum/

1. geth源代码下载

以太坊代码都托管在github上,它的代码经常更新,每次更新会加入一些新的东西,也可能改变代码的组织架构,所以最好选择一个稳定版本。这里以1.7.3 为例。
首先在GOPATH/src下创建github.com/ethereum目录:

mkdir -p $GOPATH/src/github.com/ethereum

然后进入上面创建的目录,下载go-ethereum项目源码,并取出我们想要的版本1.7.3的源代码:

cd $GOPATH/src/github.com/ethereum
git clone https://github.com/ethereum/go-ethereum.git
cd go-ethereum
git checkout v1.7.3

如果没有安装git,按照下面的命令快速进行安装。

apt install git

备注:实现了DPOS共识算法的美图版源代码从这里去clone。

git clone https://github.com/meitu/go-ethereum.git

2. 编译

cd $GOPATH/src/github.com/ethereum/go-ethereum
make geth

下面是make的正常输出结果。

build/env.sh go run build/ci.go install ./cmd/geth
>>> /usr/local/go/bin/go install -v ./cmd/geth
github.com/ethereum/go-ethereum/common/math
github.com/ethereum/go-ethereum/common/hexutil
github.com/ethereum/go-ethereum/rlp
···(省略很多行)
github.com/ethereum/go-ethereum/eth
github.com/ethereum/go-ethereum/les
github.com/ethereum/go-ethereum/ethstats
github.com/ethereum/go-ethereum/contracts/release
github.com/ethereum/go-ethereum/cmd/utils
github.com/ethereum/go-ethereum/cmd/geth
Done building.
Run "/root/golangwork/src/github.com/ethereum/go-ethereum/build/bin/geth" to launch geth.

把上述编译生成geth的目录,加入环境变量中。也建议直接加入启动配置文件.bashrc中。

#通过命令行加入geth的PATH
export PATH=$PATH:/root/golangwork/src/github.com/ethereum/go-ethereum/build/bin
#编辑启动配置文件,开机后自动加载这个路径
nano ~/.bashrc
#编辑完成后,重新加载环境变量到内存
source ~/.bashrc

3.验证编译结果

geth version

输出结果:

Geth
Version: 1.7.3-stable
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.9.7
Operating System: linux
GOPATH=/root/golangwork
GOROOT=/usr/local/go

4.启动节点

首先需要创建创世区块json文件。

mkdir /mygeth
cd /mygeth
nano genesis.json

创世区块genesis.json文件内容如下:

{
  "config": {
    "chainId": 100,
    "homesteadBlock": 0,
    "eip155Block": 0,
    "eip158Block": 0
  },
  "coinbase" : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x400",
  "extraData" : "",
  "gasLimit" : "0x2fefd8",
  "nonce" : "0x00",
  "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp" : "0x00",
  "alloc": { }
}
参数 创世区块配置参数说明
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 要求0x开头的32个字节内容。建议什么都不填,不然容易出错。
gasLimit 该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和。

用创世纪区块json区块初始化节点,并设置data目录:

geth --datadir /data/00 init genesis.json

执行以下命令 ,即可启动自己的公链。console参数表示启动后,启用命令行。

geth --networkid 100 --datadir /data/00 --rpc --rpcapi net,eth,web3,personal console 2>>geth.log
geth启动参数 参数说明
–nodiscover 关闭p2p网络的自动发现,需要手动添加节点,这样有利于我们隐藏私有网络
–datadir 区块链数据存储目录
–networkid 网络标识,私有链取一个大于4的随意的值
–rpc 启用HTTP-RPC服务,默认端口号8545
–rpcapi 提供HTTP-RPC服务的API接口
–rpcaddr HTTP-RPC服务的监听地址,默认为localhost,只能本地访问
–port 网络侦听端口(默认端口: 30303)
–maxpeers 网络节点的最大数量,默认为25
–maxpendpeers 待定尝试连接节点的最大数量,默认为0
–mine 允许挖矿
console 打开一个可交互的javascript环境

更多geth参数说明:https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options

console参数和 attach 的效果一样,但不建议直接使用 console 进入,否则,一旦退出程序 Geth 节点也随之关闭了。一般来讲,用后台运行方式启动geth。

使用attach命令进入geth控制台。

geth attach ipc:/data/00/geth.ipc

5. geth控制台节点基本操作命令

(1)查看账户列表

eth.accounts

(2)查看块高

eth.blockNumber

(3)创建账户

# 方式一,使用密码创建
> personal.newAccount("111111")
# 方式二,密码另行输入方式创建
> personal.newAccount()
Passphrase:
Repeat passphrase:

当创建账户完成之后,再进入 keystore 目录下就可以看到创建账户对应的加密私钥文件了。
(4)查看账户余额

eth.getBalance(address)

(5)解锁账户
如果转账的转出账户不解锁,转账会失败。解锁账户默认5分钟之后,重新锁定账户。

#方式一 使用密码作为参数
personal.unlockAccount(address,"111111")
#方式二 密码在提示下录入
personal.unlockAccount(address)

(6)转账

# 例子
eth.sendTransaction({from:address1,to:address2,value:web3.toWei(1,"ether")})

(7)锁住账户
需注意的是解锁账户是有时间限制的,默认解锁 5 分钟,可通过参数进行限制,建议解锁之后完成相应的操作马上把账户锁定。据说一些人由于 JSON-RPC 公网开放,结果在解锁期间被黑客调用转账接口将余额全部转走,导致资产严重损失。

eth.lockAccount(address)

(8)设置挖矿收益账户
设置挖矿收益账户,为正式挖矿做准备。

miner.setCoinbase(address)

(9)启动挖矿
如果没人挖矿,交易是不能最终被确定的。如上述所示,设置好挖矿收益账户之后,下面就可以正式开始挖矿了。

#设置挖矿线程数量开始挖矿(n为线程数量)
miner.start(n)
#以默认的8个线程方式启动挖矿
miner.start()

当在执行挖矿时日志会不停刷屏。
(10)停止挖矿

miner.stop()

现在节点就搭建成功了。


参考文章:【geth以太坊节点安装】http://blog.fens.me/bitcoin-install-geth/
区块链入门(2):搭建以太坊私有链,执行挖矿:https://www.cnblogs.com/zl03jsj/p/6858928.html

猜你喜欢

转载自blog.csdn.net/sitebus/article/details/83347312