Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

Python智能合约开发?看这一篇就够了

黄河347
195 0 0
01 前言
. Q, z* o4 d  O在之前的技术视点文章中,我们介绍了目前本体主网支持的智能合约体系以及相应的智能合约开发工具 SmartX。很多小伙伴都想上手练一练。在本期的本体技术视点中,我们将正式开始讲述智能合约语法部分。
6 @% Y3 J, u- y- G: f7 r4 p* x% a本体的智能合约 API 分为7个模块,分别是 Blockchain & Block API、Runtime API、Storage API、Native API、Upgrade API、Execution Engine API 以及 Static & Dynamic Call API。本期我们将介绍 Blockchain & Block API,这是本体智能合约体系中最基础的部分。其中,Blockchain API 支持基本的区块链查询操作,如获取当前块高等;Block API 支持基本的区块查询操作,如查询指定区块交易数等。同时,文末将提供视频讲解。
, p* {! B: O2 K  p) c) v% X) b6 w在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。5 [. D  M+ ^# w; c7 L) |6 ]( B
02 Blockchain API 使用方法. O; R3 G9 {* i: }' a
智能合约函数的引用与 Python 的引用如出一辙。开发者可以根据需要引入相应的函数。例如,下面语句引入了获取当前最新块高函数 GetHeight 和获取区块头函数 GetHeader。
( o& ~/ E! U* q2 \$ ~9 s" b) _from ontology.interop.System.Blockchain import GetHeight, GetHeader
  l0 V& }) |: M3 |- A. I! U7 p2.1 GetHeight1 P7 x# r! l1 j; W# Z2 p: y
开发者可以使用 GetHeight 来获取当前最新块高,具体例子如下。在后面的例子中,为了节省空间,我们将省略 Main 函数,小伙伴在练习的时候可以根据需要加入。
, Y& c& W. @) u8 Ofrom ontology.interop.System.Runtime import Notify
) ^( e" w: P+ g, ^. sfrom ontology.interop.System.Blockchain import GetHeight
( `' u2 E. G/ C/ i' ]def Main(operation):8 c2 }9 d6 M& Q% ^1 N4 G; r: w
    if operation == 'demo':
9 J( r$ r: r2 ?7 |        return demo()
2 u$ Y! h" r8 S/ r3 ~; J    return False0 X$ y, {/ i9 y
def demo():
2 d) f' ]8 F, V# F6 \9 o    height=GetHeight()6 R" I2 N0 f7 O
    Notify(height) # 打印height
7 T: h) @7 m3 B  S, H2 O. N4 `8 d: F    return height #在函数运行结束后返回height
$ ^! s, D8 Z; L/ L( R2.2 GetHeader9 b% _- z7 U5 |4 r- ]" y
开发者可以使用 GetHeader 来获取区块头,参数是某个块的块高。具体例子如下:$ }: ~: \2 r. i
from ontology.interop.System.Runtime import Notify
) O3 h0 u! }: t. Z: u2 M0 yfrom ontology.interop.System.Blockchain import GetHeader
" B% C. k7 b) @/ ]9 b1 Udef demo():
/ K8 u: x: g5 M7 i5 n    block_height=10- N# U+ i; X! }; Q: y
    header=GetHeader(block_height) , M1 l" O5 f. A
    Notify(header)& S% `8 Z# Q# s/ g& D( I8 _
return header3 \( q  y2 l9 m3 y
2.3 GetTransactionByHash
7 I. x' S* k5 L6 ~开发者可以使用 GetTransactionByHash 函数通过交易哈希获取交易。交易哈希以 bytearray 的格式,作为参数传入 GetTransactionByHash。这个函数的关键在于如何转换将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。
: w2 K, h0 A' X; {我们以16进制格式的交易哈希为例,实现将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。示例哈希如下:
4 K  }' f, e# ]9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1
; s( L7 d: ?+ n8 k5 U+ g首先,将该交易哈希反序得到:
' w3 H! ~0 T! f4 `9 h$ @c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279
+ t! d, g5 H. `+ Z2 |7 x% t  d8 S开发者可以通过 SmartX 提供的转换工具 Hex Number(little endian)  Number 实现这一步。
/ {% B! |2 v. u; X( ~1 A然后,将其转成 bytearray 格式:& T' Y: n3 y3 q7 @
{0xc1,0x89,0x0c,0x4d,0x73,0x06,0x26,0xdf,0xaa,0x94,0x49,0x41,0x9d,0x66,0x25,0x05,0xea,0xb3,0xbd,0xa2,0xe1,0xf0,0x1f,0x89,0x46,0x3c,0xc1,0xa4,0xa3,0x0a,0x27,0x9f}
0 q; {) l& R+ W: e) c( J0 `开发者可以通过 SmartX 提供的转换工具 String  Byte Array 实现这一步。* Z' s6 d- ?$ k% n: ]7 o
最后,将得到的 bytearray 转换成相应的字符串:
8 N  R4 q6 \, g\xc1\x89\x0c\x4d\x73\x06\x26\xdf\xaa\x94\x49\x41\x9d\x66\x25\x05\xea\xb3\xbd\xa2\xe1\xf0\x1f\x89\x46\x3c\xc1\xa4\xa3\x0a\x27\x9f7 a2 S6 {+ C2 Q. L. b( B
GetTransactionByHash 函数通过交易哈希获取交易的例子如下:6 Y+ h' n& [! V: w( T
from ontology.interop.System.Blockchain import GetTransactionByHash: ?: o3 r8 v. ]" t+ \' n) j
def demo():
! O5 {4 n8 p2 ^) f" U    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"   
! c, a9 k3 H* c2 X- L, z' N9 g    tx_hash=bytearray(b"\xc1\x89\x0c\x4d\x73\x06\x26\xdf\xaa\x94\x49\x41\x9d\x66\x25\x05\xea\xb3\xbd\xa2\xe1\xf0\x1f\x89\x46\x3c\xc1\xa4\xa3\x0a\x27\x9f")
7 O) H/ g1 f! D4 t* w    tx=GetTransactionByHash(tx_hash); X7 d8 K: U1 S& S7 z
    return tx' h% {+ J! z+ v0 F: Y
2.4 GetTransactionHeight/ ]8 d  c. Q; I" I. J: y* X* e- V5 V$ T
开发者可以使用 GetTransactionHeight 函数通过交易哈希获取交易高度。我们还是以上个例子中的哈希为例:- q& c% ^. ~7 g. H2 ~. u
from ontology.interop.System.Blockchain import  GetTransactionHeight
# _( z. D2 E% ~3 Gdef demo():: O1 T6 |  q) U. F3 X! G
    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"   
1 }! `: d8 Z+ O" K$ I& d( r4 X    tx_hash=bytearray(b"\xc1\x89\x0c\x4d\x73\x06\x26\xdf\xaa\x94\x49\x41\x9d\x66\x25\x05\xea\xb3\xbd\xa2\xe1\xf0\x1f\x89\x46\x3c\xc1\xa4\xa3\x0a\x27\x9f")
+ }( o, g- B  \, K' D, h- A, }    height=GetTransactionHeight(tx_hash)4 G7 D* R7 C- M+ T8 D3 ~
    return height
( O/ q4 i: v# q' {2.5 GetContract
8 X6 k4 B2 R; S" T开发者可以使用 GetContract 函数通过合约哈希获取合约。其中,合约哈希的转换过程与上面讲到的交易哈希转换过程一致。* t  @$ y7 o" h0 F, M& R9 ?( k
from ontology.interop.System.Blockchain import GetContract8 E/ }8 o+ Y5 m- ?& }
def demo():4 ]' }/ g; p" B( h: M/ t
    # contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"   
* k+ i: _2 ~# C% T, V    contract_hash=bytearray(b"\xfa\x98\x96\x21\x32\x32\xbb\xf0\x9f\x23\x91\xfa\xef\x95\x9b\xff\xa5\x75\x1a\xd8")
; {& n" ]7 i. D) O    contract=GetContract(contract_hash)! s3 x1 }8 z2 m. T6 N
    return contract# J4 L+ Q* M) f' ]3 ~( N
2.6 GetBlock  M6 V, ?0 W; @/ _3 `5 u% U% W
开发者可以使用 GetBlock 函数获取区块。有两种方法可以获取指定区块:, S+ m+ h# S0 l* _
通过块高获取区块:
' r+ Z$ ^; f6 p+ d6 e, }- N
, J& z, ]: q. j6 R2 {, p$ }1 mfrom ontology.interop.System.Blockchain import GetBlock
; u; b2 o8 Q% gdef demo():
: R; Y/ I2 Q7 k. ]( W+ Q  f    block=GetBlock(1408)( J# [& p7 T# g1 z) p% |5 o
    return block
( F( r' r; ?! O# p2. 通过区块哈希获取区块:
& g/ O  x! S. Y" \! S0 Yfrom ontology.interop.System.Blockchain import GetBlock, s. M0 c9 ^$ e* G* Z
def demo():    ( P: M0 E6 u) C
    block_hash=bytearray(b'\x16\xe0\xc5\x40\x82\x79\x77\x30\x44\xea\x66\xc8\xc4\x5d\x17\xf7\x17\x73\x92\x33\x6d\x54\xe3\x48\x46\x0b\xc3\x2f\xe2\x15\x03\xe4')
) [, O$ M1 i. `! l% f, ]7 @! R+ k    block=GetBlock(block_hash)3 w" c/ w9 L2 f; p
03 Block API 使用方法5 I4 @. i" }' W1 q& |% P
Block API 中可供引用的函数有三个,它们分别是 GetTransactions、GetTransactionCount 和 GetTransactionByIndex。我们依次介绍下这三个函数。
( g% Y1 |" _: _2 b- O2 L+ f& F3.1 GetTransactionCount
  V1 |& U$ c% w; i! N) A开发者可以使用 GetTransactionCount 函数获取指定区块的交易数量。& C+ Y- i5 P' k: ]
from ontology.interop.System.Block import GetTransactionCount
* @3 k" D3 j; e. o: j' I. q" Cdef demo():
1 e  e; B3 @6 z# X" X0 L) C( M    block=GetBlock(1408)
& n( m; K/ y! W4 u. w    count=GetTransactionCount(block)
" F2 U' S* }( B' h& s5 T! X/ Z    return count: J+ f1 _8 ~2 f6 S! |. @+ I
**% ]) ~! u% Y' m7 K2 Y- Q) q
3.2 GetTransactions**( Q* J  M& P$ R- T/ C
开发者可以使用 GetTransactions 函数获取获取指定区块的所有交易。
6 w4 b- j5 G4 d1 m, Kfrom ontology.interop.System.Blockchain import GetBlock
/ F. T: e6 K  h# H2 @from ontology.interop.System.Block import GetTransactions   Y9 k5 e$ B8 N& Q! a: U& Y: v8 A+ |
def demo():
& D! ?3 z4 ]. x& t2 f( L    block=GetBlock(1408)2 @, R  J& E. N0 a
    txs=GetTransactions(block)! p7 j$ z# y4 h+ l, N% p9 u
    return txs
4 k, }* ?: u) u+ B: J3.3 GetTransactionByIndex0 G0 C+ ]/ I0 W6 J8 ^, q
开发者可以使用 GetTransactionByIndex 函数获取指定区块的指定交易。; O/ @4 E# j2 ~& f  |; e
from ontology.interop.System.Blockchain import GetBlock  {) F) ?  O+ S4 ^: F% X+ h
from ontology.interop.System.Block import GetTransactionByIndex3 T1 r1 I  g( O2 z) s6 G) h" R
def demo():& g0 H" o. U8 G
    block=GetBlock(1408)
, n1 K$ q( H) T- B# S7 C    tx=GetTransactionByIndex(block,0) # index starts from 0." e4 d- H4 U" V# m% `4 Q
    return tx/ F* P8 b/ k8 c5 P4 N, B; B# B
04 后记
: w4 v! o( D7 |Blockchain & Block API 在智能合约中起到查询区块链数据和区块数据的作用,是智能合约最不可缺少的一部分。在后面的技术视点中,我们将讨论如何使用其它 API,探讨它们和本体区块链的交互。、
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

黄河347 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1