连接到EOS主网

在之前的文章中,我已经编译出来了EOS程序,但是光编译出程序还是没法愉快地玩耍的,要让我的EOS连接到主网上,才可以进行好多好玩的操作。


(图源 :pixabay)

在进一步操作之前,我把之前编译出来的程序都拷贝到一个新用户的bin目录下,这样我就省却输入大串路径名的麻烦了,并且我可以随便搞,不怕一不小心rm -rf *删除掉我之前辛苦编译出来的东西,毕竟编一次耗时挺长滴。

生成默认config.ini

如何连接上主网呢?我们来运行一下nodeos试试看。

哦,我同台VPS上另外一个节点正在运行,所以出现地址被占用错误,不过这都不重要,重要的是首次运行nodeos它帮我生成了默认的配置文件。

eosio root is /home/eosmain/.local/share

其中eosmain是我创建的测试用户,也就是当前用户,而配置文件就在以下路径下:

~/.local/share/eosio/nodeos/config

配置文件名称为config.ini,详情可以参考EOS开发站上的Configuration说明

我们首先要解决的是端口冲突问题(不像我这么折腾的话不用改的)

http-server-address = 127.0.0.1:5678
p2p-listen-endpoint = 0.0.0.0:5679

  • 启用chain_api_plugin
    重新运行nodeos,在另外的窗口中执行如下命令:

cleos -u http://127.0.0.1:5678 get info

提示我如下错误信息:

Error 3110001: Missing Chain API Plugin
Ensure that you have eosio::chain_api_plugin added to your node's configuration!
Error Details:
Chain API plugin is not enabled

缺啥补啥,在config.ini中加上如下语句:

plugin = eosio::chain_api_plugin

指定mainnet-genesis.json

再次执行nodeos, 再次执行cleos -u http://127.0.0.1:5678 get info

我们会得到如下信息,在这里我们需要注意的是:

"chain_id": "cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f",

而主网的Chain ID为: aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906,所以我们运行的姿势是不对的,哪里不对呢?答案是链接主网需要在首次运行时指定:genesis-json,这玩意上哪找呢?

最后我在这里找到一份,拿去不谢:
https://github.com/EOS-Mainnet/eos/blob/mainnet-1.0.5/mainnet-genesis.json
使用如下代码下载上述mainnet-genesis.json

wget https://github.com/EOS-Mainnet/eos/raw/mainnet-1.0.5/mainnet-genesis.json

执行如下命令:

nodeos --genesis-json mainnet-genesis.json

提示如下出错信息:

Genesis state can only be set on a fresh blockchain.

解决的方法很粗暴,删掉原来的区块链数据即可

rm -rf ~/.local/share/eosio/nodeos/data/
(或者在nodeos后边加上--delete-all-blocks参数)

来执行一下新命令:
nodeos --genesis-json mainnet-genesis.json --delete-all-blocks

添加p2p-peer-address

再来看看Chain ID, 已经是主网ID了

然而我的块为啥一直是一个呀,这是单机版主网吗?原来我还没有添加种子节点

https://github.com/EOS-Mainnet/eos 中找到一份种子节点列表:
Peer List Location: https://docs.google.com/spreadsheets/d/1K_un5Vak3eDh_b4Wdh43sOersuhs0A76HMCfeQplDOY/edit#gid=0

Google嘛,不存在的,你懂的,于是爱国上网的我就看到这一堆不明所以东东(什么文字?)

不过表格还是看得懂的,copy出来点贴我config.ini中

p2p-peer-address = eu-west-nl.eosamsterdam.net:9876
p2p-peer-address = p2p.mainnet.eosgermany.online:9876
p2p-peer-address = 54.38.250.15:19878
p2p-peer-address = p2p.genereos.io:9876
p2p-peer-address = mainnet.eospay.host:19876
p2p-peer-address = 130.211.59.178:9876
p2p-peer-address = 54.153.59.31:9999
p2p-peer-address = 94.130.250.22:9806  
p2p-peer-address = peer.main.alohaeos.com:9876
p2p-peer-address = peer.eosn.io:9876
p2p-peer-address = prod.mainnet.eos.cybex.io:9888
p2p-peer-address = p2p-1.eosnetwork.io:9876
p2p-peer-address = p.jeda.one:3322
p2p-peer-address = eosbattles.com:9877
p2p-peer-address = 34.226.76.22:9876
p2p-peer-address = mainnet.eosoasis.io:9876
p2p-peer-address = node.eosflare.io:1883
p2p-peer-address = mainnet.eoscalgary.io:5222
p2p-peer-address = eos-p2p.worbli.io:33981
p2p-peer-address = 18.188.38.175:9876
p2p-peer-address = 18.221.255.38:9876
p2p-peer-address = eos.staked.us:9870
p2p-peer-address = peering.dutcheos.io:9876
p2p-peer-address = 18.188.4.97:9876
p2p-peer-address = 18.191.125.105:9876 
p2p-peer-address = boot.eostitan.com:9876
p2p-peer-address = eosboot.chainrift.com:9876
p2p-peer-address = dc1.eosemerge.io:9876
p2p-peer-address = m.eosvibes.io:9876
p2p-peer-address = node1.eosphere.io:9876
p2p-peer-address = node2.eosphere.io:9876
p2p-peer-address = 45.33.60.65:9820 
p2p-peer-address = p2p.eosio.cr:1976
p2p-peer-address = p2p.eosio.cr:5418
p2p-peer-address = eno.eosvan.io:19866

我在想是不是选择地理位置临近的p2p-peer-address会更好呢?,懒得弄了。

重新执行nodeos(注意,不需要指定 --genesis-json mainnet-genesis.json --delete-all-blocks 啦)

再次执行:

cleos -u http://127.0.0.1:5678 get info

耶ChainID正确,也开始同步区块啦。

总结

尽管写得很多,但是大部分是为了测试和了解的需要。通过学习,连接到EOS主网无外乎几个操作:

  • 执行nodeos生成默认config.ini
  • 对config.ini进行必要的编辑
    • plugin = eosio::chain_api_plugin
    • 添加p2p-peer-address列表
  • 下载mainnet-genesis.json文件
  • 第一次正式启动时指定--genesis-json mainnet-genesis.json --delete-all-blocks

注:本文仅为本人的学习和探索,仅供参考。
使用文中代码及操作导致任何损失,本人概不负责!

相关连接

H2
H3
H4
3 columns
2 columns
1 column
6 Comments