搭建以太坊私有链笔记

一、搭建环境

.准备工作

  • 操作系统: mac OS
  • 安装homebrew,如果没有安装可以通过执行以下命令进行安装: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 安装Geth,执行以下命令:
brew tap ethereum/ethereum
brew install ethereum

二、配置节点

1.创建节点目录

由于在一台电脑上部署多个节点,所以需要创建多个目录来保存各自节点的账号、区块链数据

mkdir eth
cd eth
mkdir enode1
mkdir enode2

2. 创建账号

为每个节点各自创建一个账号.执行account new命令后会提示你输入密码,也可以不输入直接回车跳过即可,生成账号后会给出对应的地址,记录下新生成的账号地址,方便后续配置创世快使用。如果没记住也可以通过eth.accounts命令查询

geth --datadir ./enode1/data account new
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase: 
Repeat passphrase: 
Address: {c43881d20bb2a2b6d72801b4bc04cdcc35950a8b}
geth --datadir ./enode2/data account new
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase: 
Repeat passphrase: 
Address: {0cfd3ab46d552d30f8b1c4f1b08a90f40192de97}

3.配置创世块

创世块是区块链中的第一个区块,可以通过puppeth来生成创世区块,Puppeth是geth中自带的工具,不用单独安装,运行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)
> My-Private-Chain

输入私链名称后,会出现二级菜单,现在2:配置一个新的创世快

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
> 
  • Ethash - proof-of-work(PoW) :工作量证明,通过算力达成共识 (以太坊就是使用这种方式)
  • Clique - proof-of-authority(PoA): 权威证明、通过预先设定的权威节点来负责达成共识 (不消耗算力,一般用于私有链测试开发)
  • 如果选择Pow的共识方法,直接输入1,回车即可。
  • 如果选择PoA的共识方法,输入2后会提示让你选择处快的间隔时间,一般测试开发使用可以设置相对的将处快时间设置较少5秒即可,然后会让你选择哪个账户来作为权威生成区块(至少有一个)
How many seconds should blocks take? (default = 15)
> 5
Which accounts are allowed to seal? (mandatory at least one)
>0x0cfd3ab46d552d30f8b1c4f1b08a90f40192de97

选择好共识机制后会让你指定给那些账号初始化ether(至少有一个),输入我们刚才创建的账户地址回车即可。

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

选择输入私有链的网络ID,任意数字即可(不能为1,1是公链),也可以不输入会给定一个随机数作为私有链的网络ID

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

创世区块中是允许你输入一些个人信息的,可以选择输入也可以直接按回车跳过

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

输入以后会回到主菜单,我们重新选择2

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 = my-private-chain.json)
> 
INFO [02-09|14:56:33] Exported existing genesis block 

这样就完成了创世区块文件的配置了,直接退出puppeth即可。

4.初始化节点

运行geth --datadir [data目录] init [创世区块配置文件]命令来初始化各个节点

geth --datadir enode1/data init my-private-chain.json 
geth --datadir enode2/data init my-private-chain.json 

5.启动节点

启动私有链有很多可选参数,可以根据自己的需求使用不同的参数来启动,常见的参数有:

  • --datadir

私有链数据存放目录,包括Ethereum使用的Leveldb数据

  • --nodiscover

既然是私有链,就要尽可能的避免和其他节点的自动交互。这里的设置为除非手工添加其他节点,否则不会自动发现。

  • --rpc

是否打开RPC接口

  • --rpcport

RPC监听端口,默认是8545,如果一台电脑上多个节点则必须设置不同的端口

  • --rpcapi

在RPC接口上打开哪些功能,私有链可以尽情开放,但是注意公链上这一部分的设置一定要小心。因为第三方可以通过调用你的这个RPC来实现你打开的所有功能。

  • --rpcaddr

RPC链接地址,默认是localhost,如果想让别的电脑或Html或web3J访问则必须设置为本机IP地址

  • --rpccorsdomain

设置哪些URL可以连接你的RPC接口。

  • --identity

给当前节点起名,标记节点

  • --networkid

私有链的网络ID,用于区分不同的网络,1代表是公链,刚才我们在创世区块配置文件中已经设置,所以值应该是刚才设置的: 1234

  • --nat

根据自己网络情况选择是否添加

  • --port

geth的网络端口,默认是30303,如果一台电脑上多个节点一定设置不同的端口

新打开两个终端,分别来启动各自的节点

geth --identity "enode1" --rpc --rpcport "8121" --rpccorsdomain "*" --datadir ./enode1/data --port "2001" --nodiscover --rpcaddr "192.168.1.128" --rpcapi "db,eth,net,web3,personal"

geth --identity "enode2" --rpc --rpcport "8122" --rpccorsdomain "*" --datadir ./enode2/data --port "2002" --nodiscover --rpcaddr "192.168.1.128" --rpcapi "db,eth,net,web3,personal"

好了,到此为止,私有链的节点算是起来了,可以通过Geth JavaScript控制台来进行操作了,具体步骤下一篇笔记再做总结。

你可能感兴趣的:(搭建以太坊私有链笔记)