Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

Python也可以编写智能合约:从NEO开始

天道唯
2383 0 0
NEO是一个非盈利的社区化的区块链项目,是利用区块链技术和数字身份进行资产数字化,利用智能合约对数字资产进行自动化管理,实现“智能经济”的一种分布式网络,被称为“中国以太坊”。本文将带领大家学习如何使用Python编写NEO智能合约。
* S$ k0 y( p0 ~; P
  ^/ J& D- v7 I+ K; l$ b5 c9 S2 b9 h4 B1 K! t" h
环境搭建% G" f( h  `# f4 v2 o8 a
基于macOS或Ubuntu,windows用户请自备虚拟机。
# k. R0 Y5 O. p. t: S& `8 v安装Docker及拉取镜像, t5 a# {3 D$ V( N) E3 A; Z
macOS:
& i0 x7 B" B( O" X$ v+ x1.点我去下载,下载后安装并启动,启动成功后任务栏会有个Docker的图标,点击可以查看运行状态6 O% C' ^! g# p$ |8 e6 M
$ t1 l1 P' p. g6 [8 _
2.拉取私链镜像,打开终端执行
6 W2 \/ o+ T9 @5 P; Y7 [  Idocker pull cityofzion/neo-privatenet, y3 ^8 k# r  t( m/ X

$ j/ J9 M2 p- KTip:由于国内环境问题,docker镜像的拉取往往会非常慢,所以我们可以配置一些国内的镜像加速,如DaoCloud的Docker 加速器,免费注册一下,按照说明配置即可,非常简单和方便。, _& @7 U/ t. C( ~
3.运行Docker容器,执行
4 ]8 k" r( V+ T5 p) c6 oUbuntu:0 b5 }- t8 q  @+ b0 g  x
1.安装Docker,执行' ~( f* `: I$ s. C
sudo apt install docker.io
7 h1 k9 h& T) C2.启动Docker,执行(安装成功后应该会默认已启动)
' B2 s2 f7 [2 A. H2 g/ qsudo service docker start' e* L- E2 \8 ?" T# N6 e/ B' f
3.拉取私有链网络镜像,执行( j( b: s) p5 t. J- B' u( d
sudo docker pull cityofzion/neo-privatenet
5 V! L3 w8 t1 I1 `4.运行docker容器,执行0 S) p) T5 s$ u# C. l5 a- X
sudo docker run --rm -d --name neo-privatenet -p 20333-20336:20333-20336/tcp -p 30333-30336:30333-30336/tcp cityofzion/neo-privatenet
* W/ R- S  M- j& V0 N. N4 K安装Python3和levelDB
+ k5 G# p4 v5 |1 tmacOS:
5 h6 a. H4 x3 z没有安装过Homebrew的请先执行下面的命令,已安装的忽略。
1 Y0 E8 U3 D/ m, W* `+ q1 s/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)": P+ M) z% v) X& ]3 S$ w
1.macOS是自带Python2的,我们需要安装Python3
) r( C' d0 B4 W. lbrew install python3
# ^. n8 ]$ q! y! E  r* O2.安装levelDB
2 O/ R: S, n7 z8 }( ibrew install leveldb2 l3 |' l6 `) B4 j+ a, |
Ubuntu:. `/ G+ \6 }1 o  b% [: z+ b
简单粗暴的依次执行下面命令:
% t/ j* h0 |  q, K0 ^sudo apt install software-properties-common python-software-properties
& K1 n  e  N4 Q$ k4 Xsudo add-apt-repository ppa:deadsnakes/ppa
/ g* q6 w5 C2 h  M& h- v& Y2 Hsudo apt update
/ _% }- M2 a4 V, z  gsudo apt install python3.6 python3.6-dev python3.6-venv python3-pip libleveldb-dev libssl-dev g++
1 f9 D( U  q- ]" p' X% W9 QTip:因为NEO最低需要Python3.6的环境,Homebrew是会安装最新版本的Python,而apt-get源上直接install python3安装的是python3.5,所以这里我们指定的python3.6。
- U- u0 `5 t0 ^  t8 Xneo-python; j; |, e7 i$ ^* M$ ]# f5 m5 T
从GitHub克隆neo-python项目  b3 o1 Y' A% Z3 Y1 C
git clone https://github.com/CityOfZion/neo-python.git9 D  A% V5 I( Y* B" d, T! j: E5 h
创建并进入虚拟环境" ]6 P1 o$ V- E  L5 j
cd neo-python
  A2 i: I9 e) v. \% D0 Qpython3 -m venv venv # 创建虚拟环境,Ubuntu这里使用 python3.6 -m venv venv. V4 M" T* ^+ X8 I% i! F
source venv/bin/activate # 进入虚拟环境,退出虚拟环境使用命令 deactivate
% B4 e% P4 H. S安装依赖包
% P% p( s9 c1 X; R' h' [2 Y0 Ppip install -r requirements.txt
) n0 f" I6 e. k# S, jpip install -e .
4 e. N9 ]0 m6 }启动连接到私有链网络,进入NEO cli命令行& y* x4 M) E9 h- O
np-prompt -p # 退出命令行 在命令行输入 quit
9 D  f* ^2 j0 Q+ x$ e; R/ T* D
8 X6 ]$ w% |1 k4 [; S4 o出现如下界面进入了NEO命令行,那么恭喜你已经成功搭建好所有环境,如果你报错了,请接着往下看。' p* K1 `, ^' z; m3 m5 d- w
np-prompt -p 可能出现的错误( F$ [/ v4 Y2 [% c& p
错误一
2 u* q' |# q6 m: m1 _9 {; C
" O8 l8 A. z3 x, f
Traceback (most recent call last):
6 f1 E4 D7 `$ Y! {. S4 i  File "/home/parallels/Code/neosc/neo-python/venv/bin/np-prompt", line 11, in 8 H/ S, [- l: h* N( ^9 G) E8 Q
    load_entry_point('neo-python', 'console_scripts', 'np-prompt')()
. ~8 _& {8 A8 L+ J+ \  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 480, in load_entry_point
  K: H) [  h7 _% s. X    return get_distribution(dist).load_entry_point(group, name)0 ]% b- s; H, r5 K
  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2693, in load_entry_point
- b; A# F1 z: k& I, C    return ep.load()) K" U$ E1 L8 D, S: F
  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2324, in load0 K3 Z% v& _6 I/ g. d3 ]
    return self.resolve()
. H7 N% E4 |5 \& e8 i9 N/ q  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2330, in resolve
/ F! U0 [; T+ ^& [! l$ N9 i. T    module = __import__(self.module_name, fromlist=['__name__'], level=0)3 Q5 D$ X: D+ D! f/ _
  File "/home/parallels/Code/neosc/neo-python/neo/bin/prompt.py", line 28, in 6 v; ?1 ?7 M" k1 Z# T  y
    from neo.Implementations.Wallets.peewee.UserWallet import UserWallet% N8 @# U, [! i4 v
  File "/home/parallels/Code/neosc/neo-python/neo/Implementations/Wallets/peewee/UserWallet.py", line 8, in
' R" H; N/ Z% [& _4 E( M    from neo.Wallets.Wallet import Wallet! u) s8 R9 W7 c3 I! l
  File "/home/parallels/Code/neosc/neo-python/neo/Wallets/Wallet.py", line 13, in 9 o. y# d& O! A- k) U
    from Crypto.Cipher import AES; w/ h$ {! ^0 T2 n2 Y! i7 F, ?
  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/Crypto/Cipher/__init__.py", line 3, in
4 V5 G4 Z7 T1 h5 _4 D    from Crypto.Cipher._mode_ecb import _create_ecb_cipher) ^  R) O' F+ Y* l" g% Z
  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/Crypto/Cipher/_mode_ecb.py", line 29, in 0 Q- E. Q8 R6 d$ H/ x! {1 o
    from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,0 w- w  d! o$ q. S9 S
  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/Crypto/Util/_raw_api.py", line 32, in / U3 i. ^: e9 G' a
    from Crypto.Util.py3compat import byte_string7 `# d( S3 Z0 z7 }
ImportError: cannot import name 'byte_string'1 Q) K: H7 a8 ^3 M; w; p
这是因为依赖包的版本问题,解决方案如下' X( S' C' H) m) i9 {
# 卸载依赖包
- T  \" }3 A, k) W) Y$ jpip uninstall pycrypto
0 l$ ^5 [8 O! ?  v- O/ epip uninstall pycryptodemo
( e$ }- t* p4 Z' q, _# 重新安装依赖包
# I, _. p: `! w" p' Z/ Q* Y& vpip install pycrypto5 `6 n' D' a5 @* f# v# D( \& U
pip install pycryptodemo7 u0 |% i+ I- _: I
pip install -e .! e9 T  z+ E9 ~2 N
错误二
) T7 _. e, a3 A4 M8 d1 m. l+ X; y......
7 o& s! \  Y6 Y+ |; VTraceback (most recent call last):
1 i2 {2 j2 ?; P: k  N  File "/home/parallels/Code/neosc/neo-python/venv/bin/np-prompt", line 11, in
* ~2 s3 {) K+ L& \. L* e    load_entry_point('neo-python', 'console_scripts', 'np-prompt')()) U- B7 ~+ t$ I9 w6 ^6 H7 [
  File "/home/parallels/Code/neosc/neo-python/neo/bin/prompt.py", line 1061, in main
/ d! y. J% ]0 o5 o& P4 T( M, _    settings.setup_privnet(args.privnet)' P, q& j- s9 }7 t* m
  File "/home/parallels/Code/neosc/neo-python/neo/Settings.py", line 228, in setup_privnet
. y# d8 r5 q6 ~    self.check_privatenet()& B4 O9 C9 m* d2 C
  File "/home/parallels/Code/neosc/neo-python/neo/Settings.py", line 314, in check_privatenet
* u$ V# P  \( c6 V2 D' V' \    version = client.get_version(); ^, ?8 s4 B  |. k* b
  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/neorpc/Client.py", line 283, in get_version# n* E$ m( h+ ]) j% T
    return self._call_endpoint(GET_VERSION, id=id, endpoint=endpoint)
) M9 [! E) H, Z" s4 t  File "/home/parallels/Code/neosc/neo-python/venv/lib/python3.6/site-packages/neorpc/Client.py", line 310, in _call_endpoint
* h" r; P  Y8 Y+ g    raise NEORPCException("Could not call method %s with endpoint: %s : %s " % (method, endpoint.addr, e))
6 q% G4 T: z1 w$ ?9 M7 aneorpc.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(': Failed to establish a new connection: [Errno 111] Connection refused',))
5 U1 U* m, a# F2 ~* o说明你没有启动docker容器并进行端口映射,请看第一部分安装Docker及拉取镜像
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

天道唯 小学生
  • 粉丝

    0

  • 关注

    2

  • 主题

    2