NEO区块链-Python编写智能合约(一)环境搭建

文章目录

NEO是一个非盈利的社区化的区块链项目,是利用区块链技术和数字身份进行资产数字化,利用智能合约对数字资产进行自动化管理,实现“智能经济”的一种分布式网络,被称为“中国以太坊”。本文将带领大家学习如何使用Python编写NEO智能合约。
更多介绍移步NEO官网和 NEO 白皮书

感谢NEO Workshop Steve 叶凡的分享

环境搭建

基于macOS或Ubuntu,windows用户请自备虚拟机。

安装Docker及拉取镜像

macOS:
1.点我去下载,下载后安装并启动,启动成功后任务栏会有个Docker的图标,点击可以查看运行状态


2.拉取私链镜像,打开终端执行

docker pull cityofzion/neo-privatenet

ShellCopy

Tip:由于国内环境问题,docker镜像的拉取往往会非常慢,所以我们可以配置一些国内的镜像加速,如DaoCloudDocker 加速器,免费注册一下,按照说明配置即可,非常简单和方便。


3.运行Docker容器,执行

docker run --rm -d --name neo-privatenet -p 20333-20336:20333-20336/tcp -p 30333-30336:30333-30336/tcp cityofzion/neo-privatenet

ShellCopy

  • Ubuntu:
    1.安装Docker,执行
sudo apt install docker.io

ShellCopy

2.启动Docker,执行(安装成功后应该会默认已启动)

sudo service docker start

ShellCopy

3.拉取私有链网络镜像,执行

sudo docker pull cityofzion/neo-privatenet

ShellCopy

4.运行docker容器,执行

sudo docker run --rm -d --name neo-privatenet -p 20333-20336:20333-20336/tcp -p 30333-30336:30333-30336/tcp cityofzion/neo-privatenet

ShellCopy


安装Python3和levelDB

  • macOS:
    没有安装过Homebrew的请先执行下面的命令,已安装的忽略。
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

ShellCopy

1.macOS是自带Python2的,我们需要安装Python3

brew install python3

ShellCopy

2.安装levelDB

brew install leveldb

ShellCopy

  • Ubuntu:
    简单粗暴的依次执行下面命令:
sudo apt install software-properties-common python-software-properties
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.6 python3.6-dev python3.6-venv python3-pip libleveldb-dev libssl-dev g++

ShellCopy

Tip:因为NEO最低需要Python3.6的环境,Homebrew是会安装最新版本的Python,而apt-get源上直接install python3安装的是python3.5,所以这里我们指定的python3.6


neo-python

  • 从GitHub克隆neo-python项目
git clone https://github.com/CityOfZion/neo-python.git

ShellCopy

  • 创建并进入虚拟环境
cd neo-python
python3 -m venv venv # 创建虚拟环境,Ubuntu这里使用 python3.6 -m venv venv
source venv/bin/activate # 进入虚拟环境,退出虚拟环境使用命令 deactivate

ShellCopy

  • 安装依赖包
pip install -r requirements.txt
pip install -e .

ShellCopy

  • 启动连接到私有链网络,进入NEO cli命令行
np-prompt -p # 退出命令行 在命令行输入 quit

ShellCopy

出现如下界面进入了NEO命令行,那么恭喜你已经成功搭建好所有环境,如果你报错了,请接着往下看。

  • np-prompt -p 可能出现的错误
    错误一
Traceback (most recent call last):
  File "/home/parallels/Code/neosc/neo-python/venv/bin/np-prompt", line 11, in <module>
    load_entry_point('neo-python', 'console_scripts', 'np-prompt')()
  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 480, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2693, in load_entry_point
    return ep.load()
  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2324, in load
    return self.resolve()
  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2330, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/home/parallels/Code/neosc/neo-python/neo/bin/prompt.py", line 28, in <module>
    from neo.Implementations.Wallets.peewee.UserWallet import UserWallet
  File "/home/parallels/Code/neosc/neo-python/neo/Implementations/Wallets/peewee/UserWallet.py", line 8, in <module>
    from neo.Wallets.Wallet import Wallet
  File "/home/parallels/Code/neosc/neo-python/neo/Wallets/Wallet.py", line 13, in <module>
    from Crypto.Cipher import AES
  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/Crypto/Cipher/__init__.py", line 3, in <module>
    from Crypto.Cipher._mode_ecb import _create_ecb_cipher
  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/Crypto/Cipher/_mode_ecb.py", line 29, in <module>
    from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/Crypto/Util/_raw_api.py", line 32, in <module>
    from Crypto.Util.py3compat import byte_string
ImportError: cannot import name 'byte_string'

PythonCopy

这是因为依赖包的版本问题,解决方案如下

# 卸载依赖包
pip uninstall pycrypto
pip uninstall pycryptodemo
# 重新安装依赖包
pip install pycrypto
pip install pycryptodemo
pip install -e .

ShellCopy

错误二

......
Traceback (most recent call last):
  File "/home/parallels/Code/neosc/neo-python/venv/bin/np-prompt", line 11, in <module>
    load_entry_point('neo-python', 'console_scripts', 'np-prompt')()
  File "/home/parallels/Code/neosc/neo-python/neo/bin/prompt.py", line 1061, in main
    settings.setup_privnet(args.privnet)
  File "/home/parallels/Code/neosc/neo-python/neo/Settings.py", line 228, in setup_privnet
    self.check_privatenet()
  File "/home/parallels/Code/neosc/neo-python/neo/Settings.py", line 314, in check_privatenet
    version = client.get_version()
  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/neorpc/Client.py", line 283, in get_version
    return self._call_endpoint(GET_VERSION, id=id, endpoint=endpoint)
  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/neorpc/Client.py", line 310, in _call_endpoint
    raise NEORPCException("Could not call method %s with endpoint: %s : %s " % (method, endpoint.addr, e))
neorpc.Client.NEORPCException: Could not call method getversion with endpoint: http://127.0.0.1:30333 : HTTPConnectionPool(host='127.0.0.1', port=30333): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f14dee4b9b0>: Failed to establish a new connection: [Errno 111] Connection refused',))

PythonCopy

说明你没有启动docker容器并进行端口映射,请看第一部分安装Docker及拉取镜像

下一篇将带领大家编写和部署智能合约到区块链上

原文:https://blog.gallifrey.cn/?p=419&from=groupmessage&isappinstalled=0


NEOFANS 微博:https://www.weibo.com/neofanscommunity

NEOFANS Telegram群:t.me/NEOfansCN

发表评论

Top