解析EOS.IO - 2. 从编译环境说起

468 阅读3分钟

当前版本的EOS.IO还处于2.0,3.0仍然处于测试阶段。 鉴于目前按照官方wiki推荐是Dawn 2.0,那就暂时还是以2.0作为基础。

编译环境

获取代码

获取EOS.IO的全部代码很简单: $ git clone https://github.com/eosio/eos --recursive

--recursive在这里的作用简单是同时吧所有的submodule也clone下来。

编译

看一下编译脚本:

这个地方我要批评一下EOS团队,Wiki里的脚本名称还是老的“build.sh”,而现在已经是“eosio-build.sh”了,这种地方其实是阻碍小白入门的,还需要团队的注意。

用法很简单,有两个参数可以选择,一个是构架,可以选ubuntu或者是darwin,还有一个是编译模式,完全安装依赖包编译和只编译EOS,如果第二个参数不加是完全编译。

$ ./build.sh ${architecture} ${optional_mode}

看一下里面的主要代码命令: $ cmake -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DCMAKE_CXX_COMPILER=${CXX_COMPILER} -DCMAKE_C_COMPILER=${C_COMPILER} -DWASM_LLVM_CONFIG=${WASM_LLVM_CONFIG} -DBINARYEN_BIN=${BINARYEN_BIN} -DOPENSSL_ROOT_DIR=${OPENSSL_ROOT_DIR} -DOPENSSL_LIBRARIES=${OPENSSL_LIBRARIES} .. make -j4

很基础的cmake指令编译C++工程。

接下来我们来跑一下: ./eosio-build.sh darwin

因为选择了完全编译,所以需要点时间,大概也就30多分钟,可以泡杯茶或者做杯咖啡

EOS的依赖包有:

  • Clang 4.0.0
  • CMake 3.5.1
  • Boost 1.64
  • OpenSSL
  • LLVM 4.0
  • secp256k1-zkp (Cryptonomex branch)
  • binaryen

Docker

熟悉后端的朋友应该比较了解Docker了,对于不了解的朋友,你可以简单的把Docker理解成一个虚拟机,只是这个虚拟机的目的是把你所需要的软件运行环境打包,方便其他人可以一模一样的完整复制你当前的运行环境,从而解决了环境设置这个软件开发中的大问题。

Docker的下载你可以从这里得到

接下去,你需要的是编译eos的docker镜像 $ git clone https://github.com/EOSIO/eos.git --recursive $ cd eos/Docker $ docker build . -t eosio/eos

这里的第一步其实你之前编译里面做过了就不用再做了,因为不过是重新clone的git上的eos repo。

好了,这里你可以再去泡杯茶或者做杯咖啡,因为还要30多分钟。

在等待的时候,你需要看看我其他的一篇文章,介绍eos中编译好的一些程序和用到的一些工具。

. . . . .

经过了漫长的等待之后,你会发现,出错了!

卧槽,出错了

是的,你没看错,确实是会提示你编译错误。

是不是很绝望, 是不是想放弃, 是不是觉得他们team是SB?

没错,我也是这么认为的,因为issue里这个问题好像问的人蛮多的了,结果就是不修复,我也是服气的。

既然如此,那就先暂时跳过Docker,之后再来看。

创建并启动一个单节点的测试网络

这里我真的又想批评EOS团队了,Wiki能不能好好更新一下。

简单来说,就是前面在你编译完成之后,在eos文件夹下面会出现一个build文件夹,进一步讲,你去build/programs/eosd目录下面尝试执行eosiod程序,./eosiod,然后,你会看到错误信息:

olddonkeydeMacBook-Pro:eosiod olddonkey$ ./eosiod
1773574ms thread-0   chain_plugin.cpp:96           plugin_initialize    ] initializing chain plugin
1773574ms thread-0   http_plugin.cpp:141           plugin_initialize    ] host: 127.0.0.1 port: 8888 
1773575ms thread-0   http_plugin.cpp:144           plugin_initialize    ] configured http to listen on 127.0.0.1:8888
1773575ms thread-0   net_plugin.cpp:2425           plugin_initialize    ] Initialize net plugin
1773575ms thread-0   net_plugin.cpp:2445           plugin_initialize    ] Setting net_plugin logging level to info
1773575ms thread-0   net_plugin.cpp:2471           plugin_initialize    ] host: 0.0.0.0 port: 9876 
1773576ms thread-0   net_plugin.cpp:2547           plugin_initialize    ] my node_id is 1cabd93a155df081bd4de63523de8cb3b28d877635024e7c7eac43885e40e001
1773576ms thread-0   main.cpp:85                   main                 ] eosiod version df878838
1773576ms thread-0   main.cpp:89                   main                 ] 10 assert_exception: Assert Exception
fc::exists( my->genesis_file ): unable to find genesis file '', check --genesis-json argument
    {"f":""}
    thread-0  chain_plugin.cpp:176 plugin_startup

    {"my->genesis_file.generic_string()":""}
    thread-0  chain_plugin.cpp:197 plugin_startup
Segmentation fault: 11

别怕,这个是正常的,也是我们所需要的。

现在你的eosiod文件夹下面就应该有一个data-dir文件夹,其中有一个配置文件config.ini,打开并配置它。

这里又有个坑,按照项目的Wiki,需要添加的配置是

# Load the testnet genesis state, which creates some initial block producers with the default key
genesis-json = /path/to/eos/source/genesis.json
 # Enable production on a stale chain, since a single-node test chain is pretty much always stale
enable-stale-production = true
# Enable block production with the testnet producers
producer-name = inita
producer-name = initb
producer-name = initc
producer-name = initd
producer-name = inite
producer-name = initf
producer-name = initg
producer-name = inith
producer-name = initi
producer-name = initj
producer-name = initk
producer-name = initl
producer-name = initm
producer-name = initn
producer-name = inito
producer-name = initp
producer-name = initq
producer-name = initr
producer-name = inits
producer-name = initt
producer-name = initu
# Load the block producer plugin, so you can produce blocks
plugin = eosio::producer_plugin
# Wallet plugin
plugin = eosio::wallet_api_plugin
# As well as API and HTTP plugins
plugin = eosio::chain_api_plugin
plugin = eosio::http_plugin

但是这里的文件显然是老的格式的,如果需要添加新的格式的话

# Load the testnet genesis state, which creates some initial block producers with the default key
genesis-json = 这里换成项目根目录下的genesis.json文件的路径
# Enable block production with the testnet producers
producer-name = inita
producer-name = initb
producer-name = initc
producer-name = initd
producer-name = inite
producer-name = initf
producer-name = initg
producer-name = inith
producer-name = initi
producer-name = initj
producer-name = initk
producer-name = initl
producer-name = initm
producer-name = initn
producer-name = inito
producer-name = initp
producer-name = initq
producer-name = initr
producer-name = inits
producer-name = initt
producer-name = initu
# Load the block producer plugin, so you can produce blocks
plugin = eosio::producer_plugin
# Wallet plugin
plugin = eosio::wallet_api_plugin
# As well as API and HTTP plugins
plugin = eosio::chain_api_plugin
plugin = eosio::http_plugin

好了,然后,再次运行eosiod,如果没啥意外的话你就可以看到eosio deamon跑起来了。

olddonkeydeMacBook-Pro:eosiod olddonkey$ ./eosiod 
587632ms thread-0   chain_plugin.cpp:96           plugin_initialize    ] initializing chain plugin
587635ms thread-0   wallet_plugin.cpp:35          plugin_initialize    ] initializing wallet plugin
587635ms thread-0   http_plugin.cpp:141           plugin_initialize    ] host: 127.0.0.1 port: 8888 
587636ms thread-0   http_plugin.cpp:144           plugin_initialize    ] configured http to listen on 127.0.0.1:8888
587637ms thread-0   net_plugin.cpp:2425           plugin_initialize    ] Initialize net plugin
587637ms thread-0   net_plugin.cpp:2445           plugin_initialize    ] Setting net_plugin logging level to info
587638ms thread-0   net_plugin.cpp:2471           plugin_initialize    ] host: 0.0.0.0 port: 9876 
587639ms thread-0   net_plugin.cpp:2547           plugin_initialize    ] my node_id is 874db12c21ae5a8e74cb02d48f3bd63da85ee96f3ba102de56cfa10db15c778b
587641ms thread-0   main.cpp:85                   main                 ] eosiod version df878838
587649ms thread-0   block_log.cpp:92              open                 ] Opening block log at /Users/olddonkey/Documents/EOS/eos/build/programs/eosiod/data-dir/blocks/blocks.log
587671ms thread-0   chain_plugin.cpp:188          plugin_startup       ] starting chain in read/write mode
587671ms thread-0   chain_plugin.cpp:193          plugin_startup       ] Blockchain started; head block is #0, genesis timestamp is 2017-03-30T12:00:00.000
587671ms thread-0   producer_plugin.cpp:152       plugin_startup       ] producer plugin:  plugin_startup() begin
587671ms thread-0   producer_plugin.cpp:157       plugin_startup       ] Launching block production for 21 producers.

*******************************
*                             *
*   ------ NEW CHAIN ------   *
*   -  Welcome to EOSIO!  -   *
*   -----------------------   *
*                             *
*******************************

Your genesis seems to have an old timestamp
Please consider using the --genesis-timestamp option to give your genesis a recent timestamp

587672ms thread-0   producer_plugin.cpp:167       plugin_startup       ] producer plugin:  plugin_startup() end
587672ms thread-0   http_plugin.cpp:156           plugin_startup       ] start processing http thread
587672ms thread-0   http_plugin.cpp:213           plugin_startup       ] start listening for http requests
587674ms thread-0   http_plugin.cpp:218           plugin_startup       ] http io service exit
587674ms thread-0   wallet_api_plugin.cpp:70      plugin_startup       ] starting wallet_api_plugin
587675ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/create
587675ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/get_public_keys
587675ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/import_key
587675ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/list_keys
587675ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/list_wallets
587675ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/lock
587675ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/lock_all
587675ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/open
587675ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/set_timeout
587675ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/sign_transaction
587675ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/unlock
587675ms thread-0   chain_api_plugin.cpp:62       plugin_startup       ] starting chain_api_plugin
587676ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/chain/abi_bin_to_json
587676ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/chain/abi_json_to_bin
587676ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/chain/get_account
587676ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/chain/get_block
587676ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/chain/get_code
587676ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/chain/get_currency_balance
587676ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/chain/get_currency_stats
587676ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/chain/get_info
587676ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/chain/get_required_keys
587676ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/chain/get_table_rows
587676ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/chain/push_block
587676ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/chain/push_transaction
587676ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/chain/push_transactions
587676ms thread-0   net_plugin.cpp:2559           plugin_startup       ] starting listener, max clients is 25
588011ms thread-0   producer_plugin.cpp:227       block_production_loo ] Not producing block because it isn't my turn, its eosio
588500ms thread-0   producer_plugin.cpp:227       block_production_loo ] Not producing block because it isn't my turn, its eosio
589005ms thread-0   producer_plugin.cpp:227       block_production_loo ] Not producing block because it isn't my turn, its eosio
589501ms thread-0   producer_plugin.cpp:227       block_production_loo ] Not producing block because it isn't my turn, its eosio
590005ms thread-0   producer_plugin.cpp:227       block_production_loo ] Not producing block because it isn't my turn, its eosio
590503ms thread-0   producer_plugin.cpp:227       block_production_loo ] Not producing block because it isn't my turn, its eosio
591000ms thread-0   producer_plugin.cpp:227       block_production_loo ] Not producing block because it isn't my turn, its eosio
591500ms thread-0   producer_plugin.cpp:227       block_production_loo ] Not producing block because it isn't my turn, its eosio
592000ms thread-0   producer_plugin.cpp:227       block_production_loo ] Not producing block because it isn't my turn, its eosio
592505ms thread-0   producer_plugin.cpp:227       block_production_loo ] Not producing block because it isn't my turn, its eosio
593000ms thread-0   producer_plugin.cpp:227       block_production_loo ] Not producing block because it isn't my turn, its eosio
593500ms thread-0   producer_plugin.cpp:227       block_production_loo ] Not producing block because it isn't my turn, its eosio
594000ms thread-0   producer_plugin.cpp:227       block_production_loo ] Not producing block because it isn't my turn, its eosio
594505ms thread-0   producer_plugin.cpp:227       block_production_loo ] 

可能出现的问题

  • 如果遇到这样的错误St9exception: content of memory does not match data expected by executable

请重启eosiod,使用eosiod --resync

  • 想知道现在用的是什么版本的eosiod?

eosc -H ${eosd_host} -p ${eosd_port} get info,然后可以在server_version部分看到当前连接的版本。