Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

黄河347
189 0 0
01 前言
: f! F, U  Y' h$ E+ \$ C在之前的技术视点文章中,我们介绍了目前本体主网支持的智能合约体系以及相应的智能合约开发工具 SmartX。很多小伙伴都想上手练一练。在本期的本体技术视点中,我们将正式开始讲述智能合约语法部分。+ |$ L0 g) \) t9 [6 F& x
本体的智能合约 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 支持基本的区块查询操作,如查询指定区块交易数等。同时,文末将提供视频讲解。
, O/ L' A& l- t$ _在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。
9 I' z1 e5 z" E* g6 ~02 Blockchain API 使用方法5 Q$ T' q- u$ j2 V3 ~
智能合约函数的引用与 Python 的引用如出一辙。开发者可以根据需要引入相应的函数。例如,下面语句引入了获取当前最新块高函数 GetHeight 和获取区块头函数 GetHeader。
  [. ?2 r% R3 J9 ^/ d+ ufrom ontology.interop.System.Blockchain import GetHeight, GetHeader: O2 h# F3 |) i. F1 u" P( B
2.1 GetHeight
) d& Y0 U, E- S开发者可以使用 GetHeight 来获取当前最新块高,具体例子如下。在后面的例子中,为了节省空间,我们将省略 Main 函数,小伙伴在练习的时候可以根据需要加入。8 S4 G7 _- E' O, Q+ H6 ~+ [: n
from ontology.interop.System.Runtime import Notify
9 B' k0 @4 m( x/ |. h/ l! X# Afrom ontology.interop.System.Blockchain import GetHeight
- d* |: N0 A) T6 @% ?def Main(operation):
3 s( n  Z! s5 [5 s2 N# x+ e6 q! \    if operation == 'demo':
+ R5 k& r: a. C9 y) y  y+ ]+ d        return demo()
! K3 P; I0 x4 s! U- {% t$ [    return False
. k# C( ?1 Z6 M3 |def demo():
, l* Q1 {4 j% G9 ~" {$ Q; N    height=GetHeight()
/ C4 _  w5 p/ R+ n* e- J4 L# Q    Notify(height) # 打印height; t5 g7 {( e" p) F/ d; @2 Y+ {
    return height #在函数运行结束后返回height
4 L4 V; a; T" l2.2 GetHeader
- s" x: t6 h' L3 m1 U开发者可以使用 GetHeader 来获取区块头,参数是某个块的块高。具体例子如下:, t! Z* R1 d' L! k* V
from ontology.interop.System.Runtime import Notify9 H3 ?3 M# Q+ p7 G. z2 O$ @& E
from ontology.interop.System.Blockchain import GetHeader$ \$ g: I- R3 h" v& ~0 Q
def demo():
+ \0 {$ J+ `8 l/ y2 C* f    block_height=10
# F3 t9 F. n  _* {1 d    header=GetHeader(block_height)
$ U" M! h6 _6 L0 D9 a    Notify(header)) _2 ]% ~4 R0 I
return header
+ K, j# x( Z1 ^2.3 GetTransactionByHash4 G2 n& V* Z' K* L' b9 [
开发者可以使用 GetTransactionByHash 函数通过交易哈希获取交易。交易哈希以 bytearray 的格式,作为参数传入 GetTransactionByHash。这个函数的关键在于如何转换将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。+ u" Q% I: d3 W
我们以16进制格式的交易哈希为例,实现将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。示例哈希如下:8 m6 l8 C1 x% h( y* I' O+ K
9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1+ C6 L! h0 \; Z; W1 F0 E
首先,将该交易哈希反序得到:2 E4 N! V! c  N' A
c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a2795 \* z7 x  [4 I8 L) p
开发者可以通过 SmartX 提供的转换工具 Hex Number(little endian)  Number 实现这一步。1 Y0 i9 U( p* x. I/ R) T5 ^+ e) m
然后,将其转成 bytearray 格式:' m( T: W9 |! v) t5 L0 Z
{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}
+ e" H3 k$ H+ e% O- O0 y开发者可以通过 SmartX 提供的转换工具 String  Byte Array 实现这一步。
. e6 ?' B9 E  r( r- a/ K, X0 M) I最后,将得到的 bytearray 转换成相应的字符串:8 R7 p$ T! Y3 E3 j; X* P
\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; X- ]1 r  l! }
GetTransactionByHash 函数通过交易哈希获取交易的例子如下:* w* n% f  P5 D3 |6 D
from ontology.interop.System.Blockchain import GetTransactionByHash
* w) ]5 M# Q* [8 [1 Ydef demo():/ i# P: d$ D7 b5 a3 J, N" R
    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"   
) @2 e2 z5 X+ o) U( 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")+ l# I( R! b* [( ~! M9 t/ D" ]9 Y
    tx=GetTransactionByHash(tx_hash)
+ J. p+ O# \" K5 N    return tx& w* C+ ?  _7 _  j4 T) f
2.4 GetTransactionHeight/ G  e2 ~5 T/ x2 P! J* [7 x* G! i
开发者可以使用 GetTransactionHeight 函数通过交易哈希获取交易高度。我们还是以上个例子中的哈希为例:' U2 t* U3 A6 N! ^/ y/ B
from ontology.interop.System.Blockchain import  GetTransactionHeight7 R: e1 G% r1 [* I
def demo():
6 \& ^3 I( C% J    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"    8 h$ _! L" u" r8 e
    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")+ Y$ f) I; ^4 Z8 R8 _+ k+ h
    height=GetTransactionHeight(tx_hash)0 K, k- S  R2 ?  b, q) H. C% j
    return height1 i1 W+ S; A  Z8 c7 \- t5 M) }
2.5 GetContract0 \! |9 [6 _( S
开发者可以使用 GetContract 函数通过合约哈希获取合约。其中,合约哈希的转换过程与上面讲到的交易哈希转换过程一致。$ u3 D- ~. s! x+ n* k9 W
from ontology.interop.System.Blockchain import GetContract9 |; D! p- x' Z- r' j; Y! a
def demo():+ J4 Z9 \3 F) D0 _, o* J& z+ o
    # contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"   
# D- d# F/ o/ {& K$ N+ v5 `, j    contract_hash=bytearray(b"\xfa\x98\x96\x21\x32\x32\xbb\xf0\x9f\x23\x91\xfa\xef\x95\x9b\xff\xa5\x75\x1a\xd8")# F' T* j4 i. V: m
    contract=GetContract(contract_hash)
6 r4 S& W' K8 }: R: [    return contract
( _7 @; f! v# V* S: \2.6 GetBlock
( \- c; a& e8 @开发者可以使用 GetBlock 函数获取区块。有两种方法可以获取指定区块:& n& A% t6 k1 g5 y# {7 b, z+ Y
通过块高获取区块:
( A% B2 C0 l2 H1 p/ _
; U# \+ |; p8 u4 R  R; d/ Wfrom ontology.interop.System.Blockchain import GetBlock* ^" {+ G, W( E" N* J/ Q
def demo():
  |/ Q0 j- s9 H5 ^( t# }3 p+ j    block=GetBlock(1408)
' {7 r5 A' c4 D/ S. u- A( F4 `- r    return block
" Q+ {& |* d% r2. 通过区块哈希获取区块:
7 ~  ^- h7 c! ], `4 Nfrom ontology.interop.System.Blockchain import GetBlock
' ?4 {: D% {! _  h* n* edef demo():   
/ V# ^9 j$ x4 A% D    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')
3 d  p, M# _/ P# {+ J4 A    block=GetBlock(block_hash)
* D! r& g9 x4 P& t; @; u' M03 Block API 使用方法2 |9 K- C4 m2 y! F
Block API 中可供引用的函数有三个,它们分别是 GetTransactions、GetTransactionCount 和 GetTransactionByIndex。我们依次介绍下这三个函数。; J5 {6 N0 R3 f* n3 U
3.1 GetTransactionCount; C9 R, J$ ^- w$ y) d: r9 [
开发者可以使用 GetTransactionCount 函数获取指定区块的交易数量。" o' L0 ]! b" E* p1 x( _  x
from ontology.interop.System.Block import GetTransactionCount
; ^/ d# U  o2 x* B! ddef demo():
) f" G: `, B; P7 f! @& X  ]    block=GetBlock(1408)0 v+ ]& m$ ^7 ?: L3 E
    count=GetTransactionCount(block)( B. `& L0 R6 @1 C, `0 Z: g7 G# }
    return count0 M% C0 D& K7 t+ I/ z; A& E
**
8 l1 l  P: p% J# |3.2 GetTransactions**1 `7 K* @# E8 w
开发者可以使用 GetTransactions 函数获取获取指定区块的所有交易。
) B$ o( Z: O2 l5 h" Efrom ontology.interop.System.Blockchain import GetBlock
, U% [5 j  G: V4 r9 U; bfrom ontology.interop.System.Block import GetTransactions * Y6 D+ p4 M" {0 C8 `# j! \# g. L
def demo():  t' i) c# ~+ s4 W: D; U" J$ F
    block=GetBlock(1408)4 q; V$ M) l1 P% m
    txs=GetTransactions(block)# Q/ @% x6 D( b" }! c( G, p+ H( `
    return txs
0 y- q* \" x; B+ A! R  L3.3 GetTransactionByIndex
1 A. u# ?, J2 E开发者可以使用 GetTransactionByIndex 函数获取指定区块的指定交易。
# D3 `6 l" z% ]from ontology.interop.System.Blockchain import GetBlock* u6 U. ?. i7 l! l% A% w' E
from ontology.interop.System.Block import GetTransactionByIndex
" z4 O# j) u7 ~* o; b7 {* Qdef demo():* L' _' K9 d2 @* r
    block=GetBlock(1408)
5 D- s8 b& A# ~# V( z' \2 y( s    tx=GetTransactionByIndex(block,0) # index starts from 0.  j+ v5 T( _% }+ o
    return tx
# [$ a+ ^. ?' J) e$ K. Y# ]04 后记4 f5 j% N5 i3 y* c0 H8 J
Blockchain & Block API 在智能合约中起到查询区块链数据和区块数据的作用,是智能合约最不可缺少的一部分。在后面的技术视点中,我们将讨论如何使用其它 API,探讨它们和本体区块链的交互。、
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

黄河347 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1