区块链与数字货币

EOS 学习框架整理

2018-06-02  本文已影响0人  廖全磊LesterLiao

一、环境搭建

安装参考官方文档

EOS三个组件:

接下来,我们将构建这些EOSIO组件,并将它们部署在一个主机,通过单个节点对网络(testnet)进行测试与配置。

构建源码

recursive参数会将所有子组件自动克隆下来,最终我们会在本地得到全部完整的源码。

git clonehttps://github.com/EOSIO/eos --recursive
cdeos && ./eosio_build.sh

构建时间较长,最终构建成功的页面如下:

[100%] Built target cleos
Scanning dependencies of target nodeos
[100%] Building CXX object programs/nodeos/CMakeFiles/nodeos.dir/main.cpp.o
[100%] Linking CXX executable chain_test
[100%] Linking CXX executable nodeos
[100%] Built target chain_test
[100%] Built target nodeos


     _______  _______  _______ _________ _______
    (  ____ \(  ___  )(  ____ \\__   __/(  ___  )
    | (    \/| (   ) || (    \/   ) (   | (   ) |
    | (__    | |   | || (_____    | |   | |   | |
    |  __)   | |   | |(_____  )   | |   | |   | |
    | (      | |   | |      ) |   | |   | |   | |
    | (____/\| (___) |/\____) |___) (___| (___) |
    (_______/(_______)\_______)\_______/(_______)

    EOS.IO has been successfully built. 0:32:57

    To verify your installation run the following commands:

    /home/liuwenbin/opt/mongodb/bin/mongod -f /home/liuwenbin/opt/mongodb/mongod.conf &
    cd /home/liuwenbin/eos/build; make test

    For more information:
    EOS.IO website: https://eos.io
    EOS.IO Telegram channel @ https://t.me/EOSProject
    EOS.IO resources: https://eos.io/resources/
    EOS.IO wiki: https://github.com/EOSIO/eos/wiki

跑单元测试

cd build && maketest

这一步是为了验证源码功能完整度,耗时也较久。

安装命令

sudo make install

命令会被安装在/usr/local。执行完这个命令以后,我们可以在系统任何位置进行命令启用。

启动一个单独节点

构建完成后,会在build/programs/目录中出现nodeos文件夹,这是我们要启动节点的工具。通过以下命令启动你自己的独立节点区块链

cd programs/nodeos && ./nodeos -e -p eosio --plugineosio::wallet_api_plugin --plugineosio::chain_api_plugin --plugineosio::account_history_api_plugin

这条命令中,可执行文件./nodeos后面有很多参数,好看的是后面的plugin是启动时对插件的配置,剩下的参数配置我们会在接下来介绍到。启动以后,日志打印出来相关信息:

3562788ms thread-0   chain_plugin.cpp:125          plugin_initialize    ] initializing chain plugin
3562797ms thread-0   block_log.cpp:120             open                 ] Log is nonempty
3562798ms thread-0   block_log.cpp:123             open                 ] my->head->block_num(): 19 
3562798ms thread-0   block_log.cpp:129             open                 ] Index is nonempty
3562805ms thread-0   wallet_plugin.cpp:41          plugin_initialize    ] initializing wallet plugin
3562805ms thread-0   http_plugin.cpp:247           plugin_initialize    ] configured http to listen on 172.168.10.6:8888
3562805ms thread-0   wallet_api_plugin.cpp:118     plugin_initialize    ] 
*************************************
*                                   *
*  --   Wallet NOT on localhost  -- *
*  - Password and/or Private Keys - *
*  - are transferred unencrypted. - *
*                                   *
*************************************

3562806ms thread-0   net_plugin.cpp:2822           plugin_initialize    ] Initialize net plugin
3562806ms thread-0   net_plugin.cpp:2843           plugin_initialize    ] host: 0.0.0.0 port: 9876 
3562806ms thread-0   net_plugin.cpp:2915           plugin_initialize    ] my node_id is fe08589261bb3e388dc5373ccc1e0289aa2b92377bd007da6aeee95fe219ac4d
3562806ms thread-0   http_plugin.cpp:225           operator()           ] configured http with Access-Control-Allow-Headers : *
3562806ms thread-0   http_plugin.cpp:219           operator()           ] configured http with Access-Control-Allow-Origin: *
3562806ms thread-0   main.cpp:94                   main                 ] nodeos version cd979827
3562806ms thread-0   main.cpp:95                   main                 ] eosio root is /home/demos/.local/share
3562807ms thread-0   chain_plugin.cpp:253          plugin_startup       ] starting chain in read/write mode
3562807ms thread-0   chain_plugin.cpp:258          plugin_startup       ] Blockchain started; head block is #20, genesis timestamp is 2018-03-01T12:00:00.000
3562807ms thread-0   producer_plugin.cpp:381       plugin_startup       ] producer plugin:  plugin_startup() begin
3562807ms thread-0   producer_plugin.cpp:388       plugin_startup       ] Launching block production for 1 producers.
3562807ms thread-0   producer_plugin.cpp:401       plugin_startup       ] producer plugin:  plugin_startup() end
3562807ms thread-0   http_plugin.cpp:285           plugin_startup       ] start listening for http requests
3562807ms thread-0   wallet_api_plugin.cpp:74      plugin_startup       ] starting wallet_api_plugin
3562807ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/wallet/create
3562807ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/wallet/create_key
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/wallet/get_public_keys
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/wallet/import_key
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/wallet/list_keys
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/wallet/list_wallets
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/wallet/lock
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/wallet/lock_all
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/wallet/open
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/wallet/set_timeout
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/wallet/sign_transaction
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/wallet/unlock
3562808ms thread-0   chain_api_plugin.cpp:62       plugin_startup       ] starting chain_api_plugin
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/chain/abi_bin_to_json
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/chain/abi_json_to_bin
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/chain/get_account
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/chain/get_block
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/chain/get_code
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/chain/get_currency_balance
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/chain/get_currency_stats
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/chain/get_info
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/chain/get_producers
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/chain/get_required_keys
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/chain/get_table_rows
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/chain/push_block
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/chain/push_transaction
3562808ms thread-0   http_plugin.cpp:325           add_handler          ] add api url: /v1/chain/push_transactions
3562808ms thread-0   net_plugin.cpp:2927           plugin_startup       ] starting listener, max clients is 25

下面来逐一分析一下这个日志内容,可以看出EOS启动私链节点是通过插件实现的,在启动私链前,要对插件进行初始化配置,启动各依赖组件处理器。下面来列举一下主要插件内容:

以上出现的所有插件亦可理解为组件。

接下来就是正常出块了,由于我们本地启动的节点一定是具备出块权的(目前只有一个节点未涉及共识),这些块是不包含任何交易信息的,出块速度很快。

停止

断开私链直接按下复制键(Ctrl+C)即可,日志中也有体现:

3571259ms thread-0   net_plugin.cpp:2954           plugin_shutdown      ] shutdown..
3571259ms thread-0   net_plugin.cpp:2957           plugin_shutdown      ] close acceptor
3571259ms thread-0   net_plugin.cpp:2960           plugin_shutdown      ] close 0 connections
3571259ms thread-0   net_plugin.cpp:2968           plugin_shutdown      ] exit shutdown
3571259ms thread-0   fork_database.cpp:94          close                ] states.size(): 2 
3571260ms thread-0   controller.cpp:218            ~controller_impl     ] db.revision(): 37 head->block_num: 37 blog.read_head()->block_num(): 36 

可以看到私链停止时,都是通过net_plugin插件来操作,操作的方法是与plugin_startup对应的plugin_shutdown,步骤为:

配置

EOS环境启动以后,可以在本地目录:~/.local/share/eosio/nodeos/ 找到链相关文件:

.
├── config
│   ├── config.ini
│   ├── config.ini.bak
│   ├── genesis.json
│   └── genesis.json.bak
└── data
    ├── blocks
    │   ├── blocks.index
    │   ├── blocks.log
    │   └── unconfirmed
    │       ├── shared_memory.bin
    │       └── shared_memory.meta
    └── shared_mem
        ├── forkdb.dat
        ├── shared_memory.bin
        └── shared_memory.meta

5 directories, 11 files

根目录下包含config和data两个目录,data目录中存储了区块运行时数据,日志以及共享内存相关数据,我们重点来看config文件夹中的内容:

genesis.json

{
  "initial_timestamp": "2018-03-01T12:00:00.000",
  "initial_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
  "initial_configuration": {
    "base_per_transaction_net_usage": 100,
    "base_per_transaction_cpu_usage": 500,
    "base_per_action_cpu_usage": 1000,
    "base_setcode_cpu_usage": 2097152,
    "per_signature_cpu_usage": 100000,
    "per_lock_net_usage": 32,
    "context_free_discount_cpu_usage_num": 20,
    "context_free_discount_cpu_usage_den": 100,
    "max_transaction_cpu_usage": 10485760,
    "max_transaction_net_usage": 104857,
    "max_block_cpu_usage": 104857600,
    "target_block_cpu_usage_pct": 1000,
    "max_block_net_usage": 1048576,
    "target_block_net_usage_pct": 1000,
    "max_transaction_lifetime": 3600,
    "max_transaction_exec_time": 0,
    "max_authority_depth": 6,
    "max_inline_depth": 4,
    "max_inline_action_size": 4096,
    "max_generated_transaction_count": 16
  },
  "initial_chain_id": "0000000000000000000000000000000000000000000000000000000000000000"
}

可以看到初始化时间戳,初始化key,以及初始链id,链配置。其中链配置又包含了基础每笔交易的网络使用size、cpu使用size,每个方法、每个setcode、每个签名的cpu使用size,每个锁的网络使用size,空闲期间的cpu使用度折扣上下文,交易的cpu、网络使用度的最大值,区块的最大网络使用size,目标区块的网络使用size,交易最大存活生命周期长度、执行时间,权限深度的最大值,最大内联深度,最大内联操作size,交易的最大生成数量。

上面对genesis.json创世块描述文件进行了平铺直叙,我们可以看到,链时间,链key,链id都比较常见,而细致入微到标识了每个方法、每个签名等等的资源分配,这是很令人惊奇的。说明了

EOS对资源的控制是非常看中的。

config.ini

这是一个全局配置文件,就像java的property文件一样。这里面的配置会被细分到是由哪一个插件来使用的,例如针对http_plugin配置的地址端口号等等,我们也可以通过手动修改这些配置来控制链的一些表现。config.ini这个全局配置文件就是开放给外部人员,作为各种功能的静态变量配置,功能开关等工具使用。下面针对配置项逐一分析:

属于account_history_plugin插件的配置

属于chain_plugin插件的配置

属于faucet_testnet_plugin配置

属于http_plugin配置

属于mongo_db_plugin配置

属于net_plugin配置

属于producer_plugin配置

属于wallet_plugin配置

配置中出现的所有time的单位一般都是毫秒。

启动命令参数

配置文件加启动命令

上面我们通过命令

./nodeos -e -p eosio --genesis-timestamp2018-04-13T12:00:00.000--plugineosio::wallet_api_plugin --plugineosio::chain_api_plugin --plugineosio::account_history_api_plugin

启动了本地EOS环境。下面我们针对这个启动脚本的使用参数进行学习:

所以我在config.ini针对以上命令进行静态配置。

enable-stale-production=trueproducer-name= eosio

plugin的配置方式:

# 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
# This will be used by the validation step below, to view account history
plugin = eosio::account_history_api_plugin

配置结束以后,由于上面我们也执行了命令安装(sudo make install),下面我们可以直接在任何位置使用命令

nodeos

即可启动与之前命令相同的EOS本地环境。

指定配置文件地址

我们可以在机器中维护多套config.ini 以及 genesis.json文件,然后启动EOS环境时通过参数

--config-dir:指定地址用来加载配置文件,绝对路径或应用程序相对路径。

指定运行时数据地址

我们也可以通过启动参数指定运行时数据的存储位置。

--data-dir:指定地址用来存放运行时数据,日志以及共享内存相关数据,绝对路径或应用程序相对路径。

其实config-dir和data-dir就是映射的上面的~/.local/share/eosio/nodeos/的内容,我在上面使用树形结构列举了出来,他们通过启动参数均可指定新的位置。

二、EOS节点自带智能合约研究

三、EOSJS学习

上一篇下一篇

猜你喜欢

热点阅读