美文网首页
python智能合约编程 -- 如何编译pyeos

python智能合约编程 -- 如何编译pyeos

作者: learnforever01 | 来源:发表于2017-12-20 07:55 被阅读784次

pyeos的编译和eos类似,只是为了支持python,需要安装一些额外的库和工具,下面来介绍下具体过程。

1. 下载pyeos代码

git clone https://www.github.com/learnforpractice/pyeos
cd pyeos
git submodule update --init --recursive

2. checkout python代码

cd libraries/python
git checkout v3.6.4-cmake

3. ubuntu下pyeos的编译

安装leveldb库,编译pyeos所需的工具cython以及库readline

sudo apt-get install libleveldb-dev
sudo apt-get install python3-pip 
sudo apt-get install libreadline-dev
python3 -m pip install cython

3.1 编译python

这里要编译的python在目录libraries/python,是用来在调用eos api的。
首先切换到libraries/python目录,然后执行下面的命令:

./configure --prefix=$(pwd)/dist --enable-shared
make -j2
make install
export LD_LIBRARY_PATH=$(pwd)/dist/lib:$LD_LIBRARY_PATH

请将make -j2中的2改成CPU的核心的数量,以加快编译速度

3.2 编译pyeos

下面就可以开始编译pyeos了
转到主目录,执行下面的命令:

./eosio_build.sh

过程中要你输入密码,接下来就是等待了,如果没有error之类的提示,编译就成功了
如果代码有改动,不必再执行./build.sh ubuntu,只要cd到build目录执行下面的命令就可以了:

make -j2

请将2改成CPU的核心的个数,以加快编译的速度。

4. mac OS X 平台下pyeos的编译

安装leveldb,编译pyeos所需的工具cython

brew install leveldb
brew install python3
python3 -m pip install cython

4.1 编译python

mac OS X下的编译和ubuntu下一致,这里不再复述.

4.2 编译pyeos

转到主目录,执行下面的命令:

./eosio_build.sh

过程中要你输入密码,接下来就是等待了,如果没有error之类的提示,编译就成功了
如果代码有改动,不必再执行./build.sh darwin,只要cd到build目录执行下面的命令就可以了:

make -j8

请将8改成CPU的核心的个数,以加快编译的速度。

5. 运行pyeos

5.1 先来了解几个运行参数:

  1. -i
    进入python的交互模式,在交互模式下可以输入任意的python代码.进入模式时行开头会有>>>提示符,表示等待命令输入。
  2. --manual-gen-block
    手动产生块,就是调用eosapi.produce_block()才会产生新块,这个参数在调试时非常有用。
  3. --debug
    进入调试模式,在这个模式下有许多针对调试的调整,如支持在智能合约代码里设置断点调试程序,如块产生不再有会产生超时异常,执行智能合约也不会有时间限制等等。

5.2 接下来看下如何执行命令:

执行下面的命令设置python环境变量,请将~/dev/pyeos修改成正确的pyeos代码所在目录

export PYTHONHOME=~/dev/pyeos/libraries/python/dist
export LD_LIBRARY_PATH=../../libraries/python/dist/lib:$LD_LIBRARY_PATH

cd 到build/programs,用下面的命令创建config-dir目录:

mkdir config-dir

再在config-dir下创建config.ini,内容如下

# Track only transactions whose scopes involve the listed accounts. Default is to track all transactions.
# filter_on_accounts = 

# Limits the maximum time (in milliseconds) processing a single get_transactions call.
#get-transactions-time-limit = 3

# File to read Genesis State from
genesis-json = ../../genesis.json


# Minimum size MB of database shared memory file
shared-file-size = 1024

 # 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 = eosio

private-key = ["EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV","5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"]


# 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

几个要注意的参数如下:

  • genesis-json 指定的是创世块的配置文件
  • producer-name 用于指定producer账号,这些账号已经在genesis.ini中配置,如果是首次运行程序,程序会根据genesis.ini自动创建创世块
  • plugin 用于指定要加载的插件

接下来在build/programs目录执行下面的命令来启动应用程序

./pyeos/pyeos -i --manual-gen-block --debug --data-dir=config-dir

如果你看到类似下面的输出,那么程序就运行成功了

1343982ms thread-0   main.cpp:135                  interactive_console  ] start interactive python.
>>> 

下篇来介绍下如何在python中调用钱包等api接口。

相关文章

网友评论

      本文标题:python智能合约编程 -- 如何编译pyeos

      本文链接:https://www.haomeiwen.com/subject/zcmwwxtx.html