Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

黄河347
91 0 0
01 前言
: {9 @: b: |9 R2 ~' I* l* O在之前的技术视点文章中,我们介绍了目前本体主网支持的智能合约体系以及相应的智能合约开发工具 SmartX。很多小伙伴都想上手练一练。在本期的本体技术视点中,我们将正式开始讲述智能合约语法部分。
5 ?& O' E  G" i* s% c本体的智能合约 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 支持基本的区块查询操作,如查询指定区块交易数等。同时,文末将提供视频讲解。
8 E- |  \" I# g* B! O$ y, i在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。
4 W' f% ^# Y3 i2 R7 R2 c02 Blockchain API 使用方法
, L' q3 ]4 {5 r! q, E2 |- q4 S智能合约函数的引用与 Python 的引用如出一辙。开发者可以根据需要引入相应的函数。例如,下面语句引入了获取当前最新块高函数 GetHeight 和获取区块头函数 GetHeader。
8 _. Q7 n# X! }3 cfrom ontology.interop.System.Blockchain import GetHeight, GetHeader
2 o7 X2 j1 k/ j  ~# F2 p2.1 GetHeight
7 _% Q' [5 |& S3 q" p开发者可以使用 GetHeight 来获取当前最新块高,具体例子如下。在后面的例子中,为了节省空间,我们将省略 Main 函数,小伙伴在练习的时候可以根据需要加入。5 u9 x2 [4 Y6 z2 ?1 K& x
from ontology.interop.System.Runtime import Notify' [6 _6 j, X; @3 T1 q3 \; m
from ontology.interop.System.Blockchain import GetHeight- w4 D& D: f1 `, ]- x
def Main(operation):2 @( {! Z9 t, {4 w3 w
    if operation == 'demo':
+ u( L$ I, ]. h6 {% ~3 y7 l        return demo()
! ^2 i/ N1 d3 f$ u8 u6 w! Q# U: R    return False
) \; l& c( P" F! ]0 _  n+ Xdef demo():
& Z: O  t! ?1 r' V    height=GetHeight()! I7 n( V. K  R$ l1 J
    Notify(height) # 打印height
" k/ e9 h* [. ?; D2 r6 v% t7 @: U    return height #在函数运行结束后返回height) @' [0 Q$ A$ i$ `! U: O) U
2.2 GetHeader
1 h  |% f; a1 T, E开发者可以使用 GetHeader 来获取区块头,参数是某个块的块高。具体例子如下:+ c. _1 `- m7 W/ a
from ontology.interop.System.Runtime import Notify
- V& S/ _: U4 Q9 M4 e4 ufrom ontology.interop.System.Blockchain import GetHeader( f* U3 `6 j. r5 |, a4 D
def demo():
9 \; L9 Z$ D4 s" y( Q9 Z0 ^    block_height=10
" U+ I! d6 E. Y; ^3 `# v    header=GetHeader(block_height)
, ?+ E4 y, Q6 g: `" _2 B% j5 d$ [    Notify(header)
$ f- A4 q  M6 o# ^# q+ hreturn header+ r( K& e8 P8 e* P8 H1 W6 }! ?; g9 |
2.3 GetTransactionByHash
* d! R9 h% ]) Z) s开发者可以使用 GetTransactionByHash 函数通过交易哈希获取交易。交易哈希以 bytearray 的格式,作为参数传入 GetTransactionByHash。这个函数的关键在于如何转换将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。. {% r: _4 k# q
我们以16进制格式的交易哈希为例,实现将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。示例哈希如下:
0 A6 F) u7 Y7 O7 `. m9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1
! v6 U' L& T5 N# D' k+ b首先,将该交易哈希反序得到:7 A8 s$ T0 ?9 W7 [# Z( l
c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279( r0 v: ?9 t7 c8 L" z) K
开发者可以通过 SmartX 提供的转换工具 Hex Number(little endian)  Number 实现这一步。
5 d4 _; _# V9 N" l8 a然后,将其转成 bytearray 格式:
% Y2 U0 A; N: S& m) D( 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}" F: P1 A3 n0 q4 ^8 L
开发者可以通过 SmartX 提供的转换工具 String  Byte Array 实现这一步。7 b  |5 C& k( y& j  a. }" m* v
最后,将得到的 bytearray 转换成相应的字符串:
, {+ c  w: L3 R- V' K8 i/ q# 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\x9f8 n; E! b/ b8 \5 R: Q, ^0 f
GetTransactionByHash 函数通过交易哈希获取交易的例子如下:1 H9 d. ~' V& Z  k
from ontology.interop.System.Blockchain import GetTransactionByHash2 E% F  r# M3 ?0 r3 d
def demo():
, U4 }3 S3 L" ^    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"    2 D: K3 J) ]2 |0 v+ M5 S& T3 ]! M
    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 @& e  a- [. v/ l: |
    tx=GetTransactionByHash(tx_hash)
: u& {! p; D% t0 C# A5 W: G* N    return tx
- \" r- C6 B. z) A% {( i2.4 GetTransactionHeight
8 W1 `: W! j  P开发者可以使用 GetTransactionHeight 函数通过交易哈希获取交易高度。我们还是以上个例子中的哈希为例:, x9 ^5 J- N9 k% S4 O
from ontology.interop.System.Blockchain import  GetTransactionHeight% d& H9 t2 \# o
def demo():$ N% I5 f! B: x% |
    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"   
' p2 s3 T) p) B+ e& d3 i/ J5 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")) y1 x- A7 J; j; z
    height=GetTransactionHeight(tx_hash)
" G& z+ c3 C7 q. ^    return height
2 f0 A% K+ V, t/ G  ?8 b, o2.5 GetContract( c/ y$ o6 w- _1 ~" T
开发者可以使用 GetContract 函数通过合约哈希获取合约。其中,合约哈希的转换过程与上面讲到的交易哈希转换过程一致。$ C  l; Y' |; B- K
from ontology.interop.System.Blockchain import GetContract
3 V# M) j! t0 O; Zdef demo():9 w! V  [8 d8 |
    # contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"   
4 X2 m5 ^1 P- v3 M& E9 y/ B/ o    contract_hash=bytearray(b"\xfa\x98\x96\x21\x32\x32\xbb\xf0\x9f\x23\x91\xfa\xef\x95\x9b\xff\xa5\x75\x1a\xd8")
7 F) g) S! X4 H- V! Z' @( T    contract=GetContract(contract_hash)
( F; j$ p- |# g, p  P0 x    return contract( g. o- l6 n& Q* d) C
2.6 GetBlock" M1 X7 V! Z% Q% ?1 }
开发者可以使用 GetBlock 函数获取区块。有两种方法可以获取指定区块:
) E" O' x2 W) V9 {通过块高获取区块:4 ^" m7 I$ d4 L# A/ N

3 X& C: [, r7 u( S5 s0 g+ zfrom ontology.interop.System.Blockchain import GetBlock
* h6 v8 D" c9 f) J" I/ Hdef demo():. `8 T$ U3 g) {
    block=GetBlock(1408): O6 }  w4 x, @; Y+ q
    return block- X4 T, S! e0 K
2. 通过区块哈希获取区块:4 E/ i4 q& v$ n* P' y$ F2 l
from ontology.interop.System.Blockchain import GetBlock1 ?6 |5 ^0 s- U0 N, ?
def demo():   
1 Z# j' N8 @8 U- M4 D5 l    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')
0 ^) X% q6 A$ j* C9 W    block=GetBlock(block_hash): G( l, V+ L, x5 p1 ]
03 Block API 使用方法: {' P7 u( H6 V7 @0 @( [: z6 v9 f
Block API 中可供引用的函数有三个,它们分别是 GetTransactions、GetTransactionCount 和 GetTransactionByIndex。我们依次介绍下这三个函数。
* I5 F4 z0 X0 ]3.1 GetTransactionCount
- P, r/ a% s6 O# B开发者可以使用 GetTransactionCount 函数获取指定区块的交易数量。
- e0 ]0 f) G* J$ _+ Hfrom ontology.interop.System.Block import GetTransactionCount% [5 [! ^7 [  `1 R9 p& J
def demo():
) A8 s/ l+ U+ [    block=GetBlock(1408)
1 w( Z9 f. ]* T+ M    count=GetTransactionCount(block). b6 Y( M" Z) d% x  P5 F
    return count
+ l  ~, V. ?- `" s7 O% l2 J, @**
7 Y0 X. I4 [6 A( V; z0 c7 z" u& ^$ l3.2 GetTransactions**& D) o& R$ q7 r9 {' H: \0 P1 @
开发者可以使用 GetTransactions 函数获取获取指定区块的所有交易。
$ q1 J9 e2 I, x4 p0 N7 Hfrom ontology.interop.System.Blockchain import GetBlock
( E/ W" J( d4 J: c3 _from ontology.interop.System.Block import GetTransactions ! Y1 w# \! g/ C% z
def demo():
9 E! s! @2 r0 I* y# h    block=GetBlock(1408)
( R0 N/ K& w# R8 e! O    txs=GetTransactions(block)
6 H9 {3 Q3 e! D6 p3 n* @    return txs  L, {( {/ C* I" B/ M
3.3 GetTransactionByIndex4 E% Q2 O% `2 n+ b  F, S# g; p: c
开发者可以使用 GetTransactionByIndex 函数获取指定区块的指定交易。
3 u  y: _7 C  g2 {9 L/ W9 Sfrom ontology.interop.System.Blockchain import GetBlock
! F3 f" F" c+ s# efrom ontology.interop.System.Block import GetTransactionByIndex5 o" i+ G# d9 G0 Y& o5 q; n
def demo():* T- Z% Z$ o; X9 j4 r' {* b* }
    block=GetBlock(1408)4 `; }. ?1 H$ [% r6 ~
    tx=GetTransactionByIndex(block,0) # index starts from 0.; H7 Z1 }$ ?1 M6 L. ?
    return tx
# Z( U* h0 M- L( v( Y9 h! s04 后记
$ F$ T4 ^/ L% V+ eBlockchain & Block API 在智能合约中起到查询区块链数据和区块数据的作用,是智能合约最不可缺少的一部分。在后面的技术视点中,我们将讨论如何使用其它 API,探讨它们和本体区块链的交互。、
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

黄河347 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1