以太坊私有链搭建笔记

in #ethereum7 years ago

最近学习以太坊私有链记录下来的笔记,与遇到一些坑爹的地方~

Mac上安装以太坊客户端

brew tap ethereum/ethereum 
brew install ethereum 

安装完成后,执行geth version查看版本信息,geth是客户端的命令。

但是,我这边卡在了brew install ethereum这一步,速度十分慢,所以这里我选择了从github上面下载ethereum的go-ethereum源码,然后自己编译的。具体步骤如下:

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

详细下载方法可参考ethereum的github官网

搭建私有链

安装完成geth之后,就可以搭建私有链,搭建私有链需要有以下几点要求。

  • 首先保证节点之间是可以互通互联的
  • 需要有相同的创世纪文件
  • 需要有相同的networkid
创世纪文件配置

这边的创世纪文件,由于geth版本的不同,配置不太一样,在geth 1.8版本中,创世纪文件的配置如下:

{
    "config": {
        "chainId": 777,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "difficulty": "131072",
    "gasLimit": "2100000",
    "alloc": {
        "0db459d44280be8d8999b32cc1b22228ad803fa1" : {"balance":"1000"}
    }
}
初始化
geth --datadir "dir_name" init init.json
启动节点
geth  --identity "newEth" --rpc --rpccorsdomain "*" --datadir "dir_name" --port 30303 --rpcapi "personal,db,eth,net,web3" --networkid 999  --rpcport 8080  console
遇到的问题

上面的要求中提到了,如果私有链要联通,需要满足三个条件。这边我在自己Mac 电脑上面尝试的时候,遇到了问题,之前在看别人教学视频,他没有突出出来,如果使用admin.addPeer()添加节点时,一定要使用相同的创世文件,即要使用两次初始化命令,且使用完全相同的创世文件,否则,addPeer即使返回了true,可能节点也不会联通。

还有在不同的电脑或者服务器之间admin.addPeer()节点时,需要将如下的enode中的@与:之间的[::]换为具体的ip

> admin.nodeInfo
{
    enode: "enode://d680ccaa426d51482afb61510cdae4eb8104cda7ac44472caebe96ab10a531820467fe7e5bfce1f3a125c622cd6577d115a65dfbcc3baadf3889cdf0f1fb7025@[::]:30303",
    ...
}

创世文件中字段含义

  • alloc:创建的账号的公钥,可以分配足够的钱
  • nonce:nonce就是一个64位随机数,用于挖矿,注意他和mixhash的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity
  • mixhash:与nonce配合用于挖矿,由上一个区块的一部分生成的hash。注意它和nonce的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity
  • difficulty:设置当前区块的难度,如果难度过大,cpu挖矿就很难,这里设置较小难度
  • coinbase:矿工账号
  • timestamp:设置创世块的时间戳
  • parentHash:上一个区块的hash值,因为是创世块,所以这个值是0
  • extraData:附加信息,随便填,可以填你的个性信息
  • gasLimit:该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,因为是私有链,所以填最大

输出console字段含义

  • number:挖出来的第几个块
  • Commit new mining work:开始产生新块
  • Successfully sealed new block:计算完毕,完成了一个新块
Sort:  

Congratulations @clownqiang! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You made your First Comment

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

Upvote this notification to help all Steemit users. Learn why here!

Congratulations @clownqiang! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @clownqiang! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.029
BTC 62153.21
ETH 2411.09
USDT 1.00
SBD 2.64