Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

黄河347
90 0 0
01 前言9 H' i( g* A/ n# u# S& g
在之前的技术视点文章中,我们介绍了目前本体主网支持的智能合约体系以及相应的智能合约开发工具 SmartX。很多小伙伴都想上手练一练。在本期的本体技术视点中,我们将正式开始讲述智能合约语法部分。" v0 p, a: g4 d) x& [6 D
本体的智能合约 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 支持基本的区块查询操作,如查询指定区块交易数等。同时,文末将提供视频讲解。
# y! l) M. N/ T# q8 U, b在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。6 R  x7 x  \" T
02 Blockchain API 使用方法1 `; y  A8 Z. q6 ~2 _" \. h
智能合约函数的引用与 Python 的引用如出一辙。开发者可以根据需要引入相应的函数。例如,下面语句引入了获取当前最新块高函数 GetHeight 和获取区块头函数 GetHeader。  {3 _/ x' y& i! `* e& Y# `) l
from ontology.interop.System.Blockchain import GetHeight, GetHeader
7 F& ]) W# F: M( H2 u' W/ B2.1 GetHeight* n. z; ^8 a, Q; Z" s4 V" P* z
开发者可以使用 GetHeight 来获取当前最新块高,具体例子如下。在后面的例子中,为了节省空间,我们将省略 Main 函数,小伙伴在练习的时候可以根据需要加入。
+ C. i8 o5 q' K! x6 Xfrom ontology.interop.System.Runtime import Notify; Z7 J3 p/ G7 D+ i+ X* F
from ontology.interop.System.Blockchain import GetHeight
! g& S6 c" z& C" ]" ?/ mdef Main(operation):4 w/ r4 g6 b* b' A6 Q
    if operation == 'demo':
5 E% ~) ]8 z" ]# p8 J9 |        return demo()
5 }# y! w! g) D; X; R" s. k# `    return False' Z- E$ Y4 F4 D! H2 \; s' T& r
def demo():
) g1 _) u' H) a0 Q; q    height=GetHeight()
; C4 B0 c4 \8 X2 J    Notify(height) # 打印height) X: X+ r! X  K8 M8 x% P" H' z
    return height #在函数运行结束后返回height
* G  @+ b' a7 @/ y2.2 GetHeader; C* N: g3 E' V! u9 U
开发者可以使用 GetHeader 来获取区块头,参数是某个块的块高。具体例子如下:0 d( t7 l8 Z; A
from ontology.interop.System.Runtime import Notify# g! Y7 q6 k2 n+ W
from ontology.interop.System.Blockchain import GetHeader
0 U  e# b6 u% [9 c) Ydef demo():
. D. u7 D* [: u$ Z. z- l* \7 F    block_height=105 \0 v/ R) c2 _
    header=GetHeader(block_height) " P3 A# p2 ^% j* b9 B
    Notify(header)& u0 O  q' m' N! D" y
return header6 j$ T8 k; J8 n9 `% E
2.3 GetTransactionByHash
  G: b, c1 H' K' j' |6 K, n/ }/ F开发者可以使用 GetTransactionByHash 函数通过交易哈希获取交易。交易哈希以 bytearray 的格式,作为参数传入 GetTransactionByHash。这个函数的关键在于如何转换将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。
9 z, Y7 X2 d" R8 a1 _# h我们以16进制格式的交易哈希为例,实现将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。示例哈希如下:
" }! S$ |7 C+ Z% |' C9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c15 _9 P3 f! @; b# b6 X5 f
首先,将该交易哈希反序得到:
( U4 |/ e) ?( X, \9 xc1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279  [+ @* G) Y' A# g# V: u5 Q
开发者可以通过 SmartX 提供的转换工具 Hex Number(little endian)  Number 实现这一步。" }  J, R9 X0 X* m) ?# {: P
然后,将其转成 bytearray 格式:
$ w! @3 J' p2 R* H{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}
6 z4 j; I: H. g, s; z( w- U开发者可以通过 SmartX 提供的转换工具 String  Byte Array 实现这一步。) v) K9 c; o6 }0 J( z% E% ]
最后,将得到的 bytearray 转换成相应的字符串:
$ A3 v( ]. @+ x$ |\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
3 M3 l6 e6 V$ Q' O! YGetTransactionByHash 函数通过交易哈希获取交易的例子如下:* D* q: n  |2 z
from ontology.interop.System.Blockchain import GetTransactionByHash
0 Y, h& I7 ^+ |$ l/ b+ f; B' j) mdef demo():
  u8 [! |7 Z% E1 Z& g9 l    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"    . z1 ]% b# X7 N7 V
    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"); e6 h: t9 ]0 f) b4 V
    tx=GetTransactionByHash(tx_hash)
! O$ y+ d5 C8 `' D  d- H. i    return tx& O  T$ c: E0 R5 x3 G4 c% N
2.4 GetTransactionHeight
+ b$ B4 m5 U0 i; o, V: W: L8 H开发者可以使用 GetTransactionHeight 函数通过交易哈希获取交易高度。我们还是以上个例子中的哈希为例:& y" s5 Y) p% h2 }- D7 O
from ontology.interop.System.Blockchain import  GetTransactionHeight
4 @. Q! E" X& d+ e" edef demo():8 q2 F! G/ S  x3 M
    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"    $ ]# w) R; \3 W5 D
    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")
2 _' u1 ?! a$ D- O. U9 X4 r' t. F7 A    height=GetTransactionHeight(tx_hash)' j# G  _+ w* \1 k+ w2 |3 X2 q
    return height$ N- m* i. X8 t: C0 V/ `1 G+ a
2.5 GetContract
5 t7 b# }7 J/ N! j$ n开发者可以使用 GetContract 函数通过合约哈希获取合约。其中,合约哈希的转换过程与上面讲到的交易哈希转换过程一致。2 I, n: p$ P$ w1 C: v# @' D
from ontology.interop.System.Blockchain import GetContract
1 s* |4 K% i: M6 O9 Gdef demo():' h/ A3 n% J! v6 j5 T  D
    # contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"    * q2 u. l  [+ t7 N
    contract_hash=bytearray(b"\xfa\x98\x96\x21\x32\x32\xbb\xf0\x9f\x23\x91\xfa\xef\x95\x9b\xff\xa5\x75\x1a\xd8")  A5 F5 k9 M# P. o
    contract=GetContract(contract_hash)4 k4 g. W3 \6 ^5 n
    return contract& v1 R1 O7 m- H8 l& e* I
2.6 GetBlock
( _; P1 c6 _* |7 c开发者可以使用 GetBlock 函数获取区块。有两种方法可以获取指定区块:% [1 _# D5 I" z  |$ w+ {
通过块高获取区块:) w2 N' _% G% U4 S5 q9 _

% a0 ^# p5 V' O: s  efrom ontology.interop.System.Blockchain import GetBlock9 e5 [- f& f5 |3 ^$ ?
def demo():
# U, L* r3 t  t$ j6 d    block=GetBlock(1408)3 }9 M2 ?0 o1 ?5 U% {& q8 @( y1 m
    return block6 S: E6 E7 h4 t. @  Q' L
2. 通过区块哈希获取区块:, n& R! z* y5 _  |8 n
from ontology.interop.System.Blockchain import GetBlock. Q3 h) p6 l* `+ C
def demo():    3 z4 y: \- G4 W! J, 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'), N" X+ o/ ]: @6 l# j
    block=GetBlock(block_hash)
4 O9 u8 N$ [4 A/ m' f03 Block API 使用方法
" g' ~5 H3 M( D4 q. s1 dBlock API 中可供引用的函数有三个,它们分别是 GetTransactions、GetTransactionCount 和 GetTransactionByIndex。我们依次介绍下这三个函数。8 S: j2 }$ Q. B( M) W9 ~
3.1 GetTransactionCount
. ~; K6 n( a$ d' O- |! d  r开发者可以使用 GetTransactionCount 函数获取指定区块的交易数量。9 g8 Y+ r; [  V! T/ ~6 z1 D
from ontology.interop.System.Block import GetTransactionCount/ {6 S* R3 t; Q" C# Q
def demo():5 I: s: ?3 f5 X% _) }2 H% b6 m
    block=GetBlock(1408)
2 {, N. w( \7 x2 s6 d    count=GetTransactionCount(block)
/ Z- @9 y- N$ O4 a$ _  m    return count5 j% ~2 G$ X0 }7 V" _! U$ }; V) G% {
**
  ]& V% |" Y: c$ f. ^' N3.2 GetTransactions**
/ m* G# ~( ?5 b2 b: r" m开发者可以使用 GetTransactions 函数获取获取指定区块的所有交易。
3 q% [' e+ C, afrom ontology.interop.System.Blockchain import GetBlock
0 d! K7 V- h1 M9 }from ontology.interop.System.Block import GetTransactions
: s5 D& S) h. s1 ^+ vdef demo():! \$ c% @5 m" t- L, {; v
    block=GetBlock(1408)
7 J* Y+ K0 c9 z5 _    txs=GetTransactions(block)( |; r6 h/ Z& B% J4 D" r! k
    return txs
! z* `" R, r! B' F' Q3.3 GetTransactionByIndex
. g' k0 S/ D4 M* c9 m) G# {8 ]开发者可以使用 GetTransactionByIndex 函数获取指定区块的指定交易。
# E5 L7 ?- F; lfrom ontology.interop.System.Blockchain import GetBlock- V: b/ g, b  j  K) _: I) p& }/ j
from ontology.interop.System.Block import GetTransactionByIndex, O; ^; J$ c5 u- F! V4 l) ~
def demo():8 j& g. v0 Q# }
    block=GetBlock(1408)8 {, @1 f- B& D+ f6 f! D
    tx=GetTransactionByIndex(block,0) # index starts from 0./ h; L8 \  Y/ r
    return tx
. M4 g8 g! L0 g  p04 后记
3 F6 [2 _2 K$ B' K4 i2 ^Blockchain & Block API 在智能合约中起到查询区块链数据和区块数据的作用,是智能合约最不可缺少的一部分。在后面的技术视点中,我们将讨论如何使用其它 API,探讨它们和本体区块链的交互。、
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

黄河347 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1