EOS源码编译与私有链搭建(ubuntu)

1、下载源码

git clone https://github.com/EOSIO/eos --recursive

由于EOS包含了一些子模块(源码libraries目录下的一些模块),所以指定了--recursive选项

 

2、运行eos/scripts目录下的eosio_build.sh进行编译

./eosio_build.sh

由于编译过程会出现一些编译错误,先对eosio_build_ubuntu.sh进行一些修改

 

(1)屏蔽物理内存大于7G的限制

(2)MongoDB下载配置错误,原因是MongoDB也在不断变化,我这里修改为下载最新版本,最新版本可到官网中查询https://www.mongodb.com/download-center/community

EOS源码编译与私有链搭建(ubuntu)_第1张图片

然后把eosio_build_ubuntu.sh中的版本和下载地址都修改成上图对应的数据

 

EOS源码编译与私有链搭建(ubuntu)_第2张图片

 

3、编译大约需要几个小时,成功后进入build目录并安装

cd eos/build
make install

EOS源码编译与私有链搭建(ubuntu)_第3张图片

 

4、安装完后,需要设置环境变量,以便以后直接启动

# 在/etc/bash.bashrc文件最后一行添加,重启
export PATH="/root/opt/eosio/bin:$PATH"

 

5、启动单节点私有链

(1)创建config.ini和genesis.json文件,文件放在/root/.local/share/eosio/nodeos/config目录下

#genesis.json

{
  "initial_timestamp": "2018-03-02T12:00:00.000",
  "initial_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
#此处的initial_key与EOS主目录中Cmakelist.txt中EOSIO_ROOT_KEY值一致
  "initial_configuration": {
    "max_block_net_usage": 1048576,
    "target_block_net_usage_pct": 1000,
    "max_transaction_net_usage": 524288,
    "base_per_transaction_net_usage": 12,
    "net_usage_leeway": 500,
    "context_free_discount_net_usage_num": 20,
    "context_free_discount_net_usage_den": 100,
    "max_block_cpu_usage": 100000,
    "target_block_cpu_usage_pct": 500,
    "max_transaction_cpu_usage": 50000,
    "min_transaction_cpu_usage": 100,
    "max_transaction_lifetime": 3600,
    "deferred_trx_expiration_window": 600,
    "max_transaction_delay": 3888000,
    "max_inline_action_size": 4096,
    "max_inline_action_depth": 4,
    "max_authority_depth": 6
  },
  "initial_chain_id": "0000000000000000000000000000000000000000000000000000000000000000"
}
#config.ini

blocks-dir = "blocks"
http-server-address = 127.0.0.1:8888
p2p-listen-endpoint = 0.0.0.0:9876
p2p-server-address = localhost:9876
agent-name = "EOS Test Agent"
allowed-connection = any
enable-stale-production = true
max-transaction-time = 30
producer-name = eosio
signature-provider = EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV=KEY:5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
#此处公钥与genesis.json文件中的公钥一致,私钥与eos/programs/eosio-launcher中main.cpp的private_key_type私钥一致

plugin = eosio::chain_api_plugin
plugin = eosio::history_api_plugin
plugin = eosio::chain_plugin
plugin = eosio::producer_plugin
plugin = eosio::http_plugin
plugin = eosio::net_plugin
plugin = eosio::net_api_plugin

(3)启动nodeos,成功启动便会产生区块

EOS源码编译与私有链搭建(ubuntu)_第4张图片

 

6、启动多节点私有链

(1)创建三个文件夹,分别是node1、node2、node3,分别放置每个节点的config.ini和genesis.json文件 ,其中每个节点的genesis.json和上面单节点的完全一样,config.ini略有不同

#node1 config.ini

blocks-dir = "blocks"
#其他节点可通过http8888端口对该节点进行RPC访问
http-server-address = 127.0.0.1:8888

#监听peer连接的端口
p2p-listen-endpoint = 0.0.0.0:9876

#peer节点可通过9876端口与该节点进行连接
p2p-server-address = localhost:9876

agent-name = "EOS Test Agent"
allowed-connection = any
enable-stale-production = true
max-transaction-time = 30
producer-name = eosio
signature-provider = EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV=KEY:5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3

plugin = eosio::chain_api_plugin
plugin = eosio::history_api_plugin
plugin = eosio::chain_plugin
plugin = eosio::producer_plugin
plugin = eosio::http_plugin
plugin = eosio::net_plugin
plugin = eosio::net_api_plugin
#node2 config.ini

blocks-dir = "blocks"

#其他节点可通过http8889端口对该节点进行RPC访问
http-server-address = 127.0.0.1:8889

#监听peer连接的端口
p2p-listen-endpoint = 0.0.0.0:9877

#peer节点可通过9877端口与该节点进行连接
p2p-server-address = localhost:9877

#连接到9876这个端口,即与node1进行p2p连接
p2p-peer-address = localhost:9876

agent-name = "EOS Test Agent"
allowed-connection = any
enable-stale-production = true
max-transaction-time = 30

producer-name = inita
signature-provider = EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV=KEY:5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3

plugin = eosio::chain_api_plugin
plugin = eosio::history_api_plugin
plugin = eosio::chain_plugin
plugin = eosio::producer_plugin
plugin = eosio::http_plugin
plugin = eosio::net_plugin
plugin = eosio::net_api_plugin
#node3 config.ini

blocks-dir = "blocks"

#其他节点可通过http8890端口对该节点进行RPC访问
http-server-address = 127.0.0.1:8890

#监听peer连接的端口
p2p-listen-endpoint = 0.0.0.0:9878

#peer节点可通过9878端口与该节点进行连接
p2p-server-address = localhost:9878

#连接到9877这个端口,即与node2进行p2p连接
p2p-peer-address = localhost:9877

agent-name = "EOS Test Agent"
allowed-connection = any
enable-stale-production = true
max-transaction-time = 30

producer-name = initb
signature-provider = EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV=KEY:5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3

plugin = eosio::chain_api_plugin
plugin = eosio::history_api_plugin
plugin = eosio::chain_plugin
plugin = eosio::producer_plugin
plugin = eosio::http_plugin
plugin = eosio::net_plugin
plugin = eosio::net_api_plugin

(2)启动节点1,在node1目录输入下面命令

nodeos --data-dir ./ --config-dir ./

这时可看到节点1成为出块者

EOS源码编译与私有链搭建(ubuntu)_第5张图片

(3)创建钱包,以下命令生成一个default名字的钱包,保存好钱包密码,钱包文件默认保存在/root/eosio-wallet

cleos wallet create  --to-console

(4)解锁钱包,钱包的功能是存放私钥,存放前要解锁钱包

cleos wallet unlock

(5)往钱包导入私钥

cleos wallet import --private-key 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3

(6)创建账户inita和initb,分别对应节点2和节点3中config.ini的producer-name,从结果可看成创建账户会往链上发送交易

cleos create account eosio inita EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
cleos create account eosio initb EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV

EOS源码编译与私有链搭建(ubuntu)_第6张图片

EOS中账户、钱包、私钥、公钥的关系如下图

EOS源码编译与私有链搭建(ubuntu)_第7张图片

(7)部署系统eosio.bios合约,从结果也可看出部署合约也会向链上发送交易

cd eos/build/unittests
cleos set contract eosio ./contracts/eosio.bios/

EOS源码编译与私有链搭建(ubuntu)_第8张图片

(8)让节点2和节点3互相出块

cleos push action eosio setprods '{"schedule":[{"producer_name":"inita","block_signing_key":"EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"},{"producer_name":"initb","block_signing_key":"EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"}]}' -p eosio

(9)在节点2和节点3的目录下分别启动两个节点

nodeos --data-dir ./ --config-dir ./

启动节点后便可看到两个节点互相出块了

EOS源码编译与私有链搭建(ubuntu)_第9张图片

而此时节点1也不再出块,而成为接收者了

EOS源码编译与私有链搭建(ubuntu)_第10张图片

CTR+C可正确退出节点,以后启动节点如果先启动节点1,节点1会堵塞,直到出块节点启动

如果节点没有正确退出,导致再次启动出现脏数据错误,可通过这样启动

nodeos --data-dir ./ --config-dir ./ --hard-replay-blockchain

 

你可能感兴趣的:(区块链)