Leap BlockChain

以太坊多节点私链搭建

2018-03-20  本文已影响1121人  藤条星辰

以太坊多节点私链搭建

区块链大火,配置个以太坊私链玩玩,了解一下这个疯狂的时代,不至于被割韭菜😂

环境准备

生成私链创世块的配置

1.创建账户

# 创建privateNet目录
>mkdir privateNet

# 创建账户
>geth --datadir node0 account new
Your new account is locked with a password. Please give a password.
Passphrase:
Repeat passphrase:
Address: {57cbfdb91c9f78fc3b0ec995c44325e63d6e0a74}
# 密码记录到文件中
>echo node0 > node0/password

在其他节点服务器上,按上述说明创建账户,例如生成:Address: {d1be0a55270232c2dc03ee453871f43cc045e906}

2.生成genesis文件

genesis文件定义了私链的第一个块,这里我们使用puppeth工具来生成

>puppeth
+-----------------------------------------------------------+
| Welcome to puppeth, your Ethereum private network manager |
|                                                           |
| This tool lets you create a new Ethereum network down to  |
| the genesis block, bootnodes, miners and ethstats servers |
| without the hassle that it would normally entail.         |
|                                                           |
| Puppeth uses SSH to dial in to remote servers, and builds |
| its network components out of Docker containers using the |
| docker-compose toolset.                                   |
+-----------------------------------------------------------+

Please specify a network name to administer (no spaces, please)
> privateNet
Sweet, you can set this via --network= privateNet next time!

INFO [03-20|15:25:21] Administering Ethereum network            name= privateNet
WARN [03-20|15:25:21] No previous configurations found         path=/   root/.puppeth/privateNet

What would you like to do? (default = stats)
1. Show network stats
2. Configure new genesis
3. Track new remote server
4. Deploy network components
> 2

Which consensus engine to use? (default = clique)
1. Ethash - proof-of-work
2. Clique - proof-of-authority
> 2

How many seconds should blocks take? (default = 15)
> 5

Which accounts are allowed to seal? (mandatory at least one)
> 0xd1be0a55270232c2dc03ee453871f43cc045e906
> 0x57cbfdb91c9f78fc3b0ec995c44325e63d6e0a74
> 0x

Which accounts should be pre-funded? (advisable at least one)
> 0xd1be0a55270232c2dc03ee453871f43cc045e906
> 0x57cbfdb91c9f78fc3b0ec995c44325e63d6e0a74
> 0x


Specify your chain/network ID if you want an explicit one (default =    random)
> 105

Anything fun to embed into the genesis block? (max 32 bytes)
>

What would you like to do? (default = stats)
1. Show network stats
2. Manage existing genesis
3. Track new remote server
4. Deploy network components
> 2

1. Modify existing fork rules
2. Export genesis configuration
> 2

Which file to save the genesis into? (default = testnet.json)
> genesis.json
INFO [03-20|15:29:24] Exported existing genesis block

What would you like to do? (default = stats)
1. Show network stats
2. Manage existing genesis
3. Track new remote server
4. Deploy network components
> ^C

genesis.json部分字段说明

字段 说明
chainId 链id
nonce nonce就是一个64位随机数,用于挖矿,注意他和mixhash的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件
mixhash 与nonce配合用于挖矿,由上一个区块的一部分生成的hash。注意他和nonce的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件
difficulty 设置当前区块的难度,如果难度过大,cpu挖矿就很难,这里设置较小难度
alloc 用来预置账号以及账号的以太币数量
coinbase 矿工的账号
timestamp 设置创世块的时间戳
parentHash 上一个区块的hash值,因为是创世块,所以这个值是0
extraData 附加信息,随便填,可以填你的个性信息
gasLimit 该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和

启动私链网络

1. 启动节点node0

2. 启动节点node1

3. 建立节点之间的连接

4. 开启RPC服务

>admin.startRPC("IP0", 8545, "*", "eth,net,web3,admin,personal")

这样我们就可以用钱包(例如:metamask)连接私链了

也可以在启动节点的时候,命令添加相关参数直接打开服务

>geth --networkid 105 --rpc --rpccorsdomain "*" --rpcaddr IP0 --rpcapi "db,eth,net,personal,web3" --datadir node0 --port 30000 --nodiscover --unlock '0' --password ./node0/password console

参数说明

字段 说明
--rpc 开启rpc连接功能
--rpcport 指定rpc端口,truffle部署时需要连接这个端口
--rpccorsdomain 这个可以指示什么URL能连接到你的节点来执行RPC定制端任务
--datadir 数据文件
--port 节点服务端口
--rpcapi 支持的rpcapi
--networkid 链id,对应创始区块的chainId
console 进入命令行模式

挖矿

在每个节点的console输入如下命令,启动挖矿:

> miner.start()
INFO [03-20|17:09:03] Transaction pool price threshold updated price=18000000000
INFO [03-20|17:09:03] Starting mining operation

信任节点的管理

我们私链使用的是POA共识算法,下面做简单介绍

1. POA的特点

2. POA的工作流程

3. 添加、删除信任节点

上一篇 下一篇

猜你喜欢

热点阅读