Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

黄河347
215 0 0
01 前言6 |/ B% k5 M9 l( ~7 l, `
在之前的技术视点文章中,我们介绍了目前本体主网支持的智能合约体系以及相应的智能合约开发工具 SmartX。很多小伙伴都想上手练一练。在本期的本体技术视点中,我们将正式开始讲述智能合约语法部分。" q( c) I; b9 _) E- 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 支持基本的区块查询操作,如查询指定区块交易数等。同时,文末将提供视频讲解。2 A: [" C2 p* I( W
在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。- w0 A: \0 U" w6 A' ]( }3 {
02 Blockchain API 使用方法: R, ?9 S3 m5 N2 ]: ?
智能合约函数的引用与 Python 的引用如出一辙。开发者可以根据需要引入相应的函数。例如,下面语句引入了获取当前最新块高函数 GetHeight 和获取区块头函数 GetHeader。7 w5 m/ P( O; h
from ontology.interop.System.Blockchain import GetHeight, GetHeader* Y2 Z1 W( F7 g) |+ h3 w
2.1 GetHeight: Q* n$ ~7 d5 ]1 x% a% p
开发者可以使用 GetHeight 来获取当前最新块高,具体例子如下。在后面的例子中,为了节省空间,我们将省略 Main 函数,小伙伴在练习的时候可以根据需要加入。
1 R! D( v9 O8 S7 Y8 D- Ufrom ontology.interop.System.Runtime import Notify
) S0 E  ~% W, Y& X; k) wfrom ontology.interop.System.Blockchain import GetHeight
, e  \; @) B  l3 Q+ H2 [def Main(operation):% E$ M: g) D' v. P
    if operation == 'demo':& l8 v# f1 G, v, c. k3 _/ T5 w
        return demo()8 A1 @2 l2 G3 p0 r5 l& z
    return False- S5 w$ g+ o" t
def demo():
" t7 [: p  X( z. ?! B3 H8 q4 O    height=GetHeight()) S+ ]: R4 i7 y
    Notify(height) # 打印height
# v5 `3 a$ E$ N9 e% ?    return height #在函数运行结束后返回height
) z3 h9 k6 z; }) Y/ z7 U2.2 GetHeader8 r$ I# P# s6 y
开发者可以使用 GetHeader 来获取区块头,参数是某个块的块高。具体例子如下:% P$ a" k3 B1 _- ~# Q1 E! T9 Y
from ontology.interop.System.Runtime import Notify1 c2 x% \# q/ Y+ {7 ^9 _2 z$ z" o
from ontology.interop.System.Blockchain import GetHeader; o6 }& q9 R3 Y) }0 k( M; V
def demo():
  w& f, z  V3 O" n4 T& e5 G    block_height=10
/ m' k0 K7 \; W' A- Y    header=GetHeader(block_height) , H  T$ @  l5 I
    Notify(header)$ ?/ P2 [% ]2 e4 X8 n
return header
; L) Q1 g; Q" C3 l2.3 GetTransactionByHash
$ }% w7 T$ Q9 z3 M, Q% d7 U/ F开发者可以使用 GetTransactionByHash 函数通过交易哈希获取交易。交易哈希以 bytearray 的格式,作为参数传入 GetTransactionByHash。这个函数的关键在于如何转换将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。" b7 n6 |" y5 E# k* _4 y* P0 k  n/ h+ I
我们以16进制格式的交易哈希为例,实现将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。示例哈希如下:
8 x5 ?/ S0 z* ]$ @; H: p" [9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1" K. k1 z6 D$ v; T8 a1 E0 x" o6 @& B
首先,将该交易哈希反序得到:
8 d! `3 u* D1 A9 F, Qc1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279
' Y  B5 B2 `/ n' r% T5 E( Q$ m开发者可以通过 SmartX 提供的转换工具 Hex Number(little endian)  Number 实现这一步。! K- D: D( p" R) E% F
然后,将其转成 bytearray 格式:
3 A* K2 }$ Z# r$ X7 a{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}
" G# Q+ Q7 Q6 H' y: w开发者可以通过 SmartX 提供的转换工具 String  Byte Array 实现这一步。: r& t! t# `5 W$ ~# T3 _
最后,将得到的 bytearray 转换成相应的字符串:1 l5 K6 {7 w( C/ m& _+ 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
3 \, m, k1 N: k% cGetTransactionByHash 函数通过交易哈希获取交易的例子如下:5 d& h+ m' _1 l. y9 I& o
from ontology.interop.System.Blockchain import GetTransactionByHash% K9 y/ E$ Z- ?
def demo():, m1 i5 l' B0 U/ @- T4 K' r
    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"   
* u6 M6 W: k3 D4 K( }  q. a! L    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")
$ \" `6 L9 h5 u) Z- A: ?    tx=GetTransactionByHash(tx_hash)9 `& ]3 X" m! k, y" v
    return tx
9 j" Q+ ~1 P: b9 I& G: P2.4 GetTransactionHeight: b$ S  R. z; }
开发者可以使用 GetTransactionHeight 函数通过交易哈希获取交易高度。我们还是以上个例子中的哈希为例:- N" u6 H" i# u& r1 b# S/ [3 w$ F8 s# S
from ontology.interop.System.Blockchain import  GetTransactionHeight
4 U* a% O( J% N) @# C" i6 {def demo():: g2 ~7 q& G5 a- ?6 H/ J  x& Z0 n  v
    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"   
# v: M4 k# c) L% |5 K0 Q8 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")3 o8 ?- v1 H4 b
    height=GetTransactionHeight(tx_hash)( U: \/ i. g1 u! j% |. c
    return height
9 B) z4 I( J5 `" }- V; }2.5 GetContract' M1 r7 [: J0 Y& l3 V, C; R& S+ o, h
开发者可以使用 GetContract 函数通过合约哈希获取合约。其中,合约哈希的转换过程与上面讲到的交易哈希转换过程一致。9 a% `* n! z+ |
from ontology.interop.System.Blockchain import GetContract1 c* A: p5 g( _5 \' q, @( e/ @
def demo():
$ z4 W9 U( b5 G( R    # contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"   
) a( n$ N# ~) i  d# x  u' f* R    contract_hash=bytearray(b"\xfa\x98\x96\x21\x32\x32\xbb\xf0\x9f\x23\x91\xfa\xef\x95\x9b\xff\xa5\x75\x1a\xd8")
1 d7 u/ T0 }/ V+ l4 S2 S    contract=GetContract(contract_hash); r: v9 Y, z. k# S
    return contract
8 M5 K9 s1 M; o. u+ v: u0 B- b2.6 GetBlock
; B$ @/ m+ R6 F' X8 x开发者可以使用 GetBlock 函数获取区块。有两种方法可以获取指定区块:
6 _. z0 k1 C/ O3 ~% e通过块高获取区块:" A# B0 e: V- J  X2 k
/ `- t/ Y' K. G/ v* H
from ontology.interop.System.Blockchain import GetBlock7 j; D: v4 r' U: a+ H, W
def demo():. V0 O6 i* q8 Y# @6 E
    block=GetBlock(1408)
0 K1 p, }0 D3 s    return block8 x( X6 z) g2 y( R( V8 ~. w! u7 t
2. 通过区块哈希获取区块:) `8 @9 Y! C, G% L
from ontology.interop.System.Blockchain import GetBlock2 L# u9 J0 d" G
def demo():    0 n! K( w4 j$ Z4 e, E3 d. p
    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')
& c! }" R/ _$ L, Q, Z6 s    block=GetBlock(block_hash)
; h4 F3 x1 d* S' p: T* ~5 d03 Block API 使用方法5 S6 J; v' P+ _( L" S- _
Block API 中可供引用的函数有三个,它们分别是 GetTransactions、GetTransactionCount 和 GetTransactionByIndex。我们依次介绍下这三个函数。2 B) e: z5 _% ?5 l8 T0 v* U
3.1 GetTransactionCount9 P, J; N) C& u
开发者可以使用 GetTransactionCount 函数获取指定区块的交易数量。
% L$ d: G* b- Afrom ontology.interop.System.Block import GetTransactionCount- k% s6 P# h) w4 f$ f; i
def demo():& A8 T0 x" D7 K9 N
    block=GetBlock(1408)
$ `2 K5 g3 q6 D  n$ j* o. b, _    count=GetTransactionCount(block)1 _5 {+ f/ V4 ?( ~
    return count
1 V" Y# Y5 S1 v4 z' X, Y**' @( G) n% c3 b* H4 V, m
3.2 GetTransactions**
% g/ |. @3 [8 i开发者可以使用 GetTransactions 函数获取获取指定区块的所有交易。
  E6 {( n$ w( m! Dfrom ontology.interop.System.Blockchain import GetBlock
1 b1 s4 ^2 ]8 c& ^; _from ontology.interop.System.Block import GetTransactions
0 N3 c2 A8 Z! A# j; wdef demo():( G+ F3 d  H7 a
    block=GetBlock(1408)
" q% {5 r$ W: Q. h    txs=GetTransactions(block)
" ^" i. w2 I1 w2 a0 Z5 N    return txs
( {- }* c$ P/ R8 \. z3 X2 l3.3 GetTransactionByIndex4 s# ]& Z3 U& h7 W8 J0 U
开发者可以使用 GetTransactionByIndex 函数获取指定区块的指定交易。4 _; W( f) j* T
from ontology.interop.System.Blockchain import GetBlock/ Y" S# N0 A/ b, E4 i! M# Z
from ontology.interop.System.Block import GetTransactionByIndex
( o3 X9 t! k& C. ]2 @1 rdef demo():
/ k) v8 ~. _. v    block=GetBlock(1408)* x* P" u+ ?" h* O: K
    tx=GetTransactionByIndex(block,0) # index starts from 0.
( J. k! ]( `9 v! x  x* L) w. c    return tx+ s* n8 t, m+ p: t
04 后记
# R$ b) f) b4 I2 E9 r6 X* ^# ^2 Y6 tBlockchain & Block API 在智能合约中起到查询区块链数据和区块数据的作用,是智能合约最不可缺少的一部分。在后面的技术视点中,我们将讨论如何使用其它 API,探讨它们和本体区块链的交互。、
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

黄河347 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1