Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

黄河347
190 0 0
01 前言2 ~9 C+ U% t3 E; N
在之前的技术视点文章中,我们介绍了目前本体主网支持的智能合约体系以及相应的智能合约开发工具 SmartX。很多小伙伴都想上手练一练。在本期的本体技术视点中,我们将正式开始讲述智能合约语法部分。
2 ^: P3 S/ H$ p" Y/ G本体的智能合约 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 支持基本的区块查询操作,如查询指定区块交易数等。同时,文末将提供视频讲解。3 Y" |  |9 Z# v1 B. J) P
在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。
1 M8 X6 M* ~( o% F! f( K02 Blockchain API 使用方法1 |" \& a! B( C: b# Z; {: K
智能合约函数的引用与 Python 的引用如出一辙。开发者可以根据需要引入相应的函数。例如,下面语句引入了获取当前最新块高函数 GetHeight 和获取区块头函数 GetHeader。- o& V& S- L4 u( s# p9 ~
from ontology.interop.System.Blockchain import GetHeight, GetHeader/ y0 D6 U: s5 v
2.1 GetHeight4 u7 T- W, B  [. F! Z
开发者可以使用 GetHeight 来获取当前最新块高,具体例子如下。在后面的例子中,为了节省空间,我们将省略 Main 函数,小伙伴在练习的时候可以根据需要加入。2 r: n- c; q" O: g0 r5 k
from ontology.interop.System.Runtime import Notify* f5 u# C$ k, P/ A4 g
from ontology.interop.System.Blockchain import GetHeight
5 \0 P  z! ^: E4 u% a- e, y' I2 vdef Main(operation):9 `3 K8 q% l9 L! [  u9 g: A( U
    if operation == 'demo':
- k% e9 E- S- G2 D1 d        return demo()6 F# s9 {6 E( d/ g
    return False
$ `6 p5 o$ A3 kdef demo():
  G( _/ w$ }/ m3 S' {    height=GetHeight()  N9 V. |! Z% x& x- N
    Notify(height) # 打印height
7 n1 H5 \8 L9 K2 j    return height #在函数运行结束后返回height# y6 `1 }# y- r5 ]& v; I
2.2 GetHeader- S/ H: f: u9 \, {- L* f/ B
开发者可以使用 GetHeader 来获取区块头,参数是某个块的块高。具体例子如下:5 ^% C  \/ }  M
from ontology.interop.System.Runtime import Notify
  U, T2 c/ g, ^; Z3 ufrom ontology.interop.System.Blockchain import GetHeader
! D" G8 Y; m2 R' cdef demo():
8 V, S! E1 w. [7 x- {% N( `    block_height=10
; g; N, Q( V0 P+ G    header=GetHeader(block_height) " l5 j+ @  P6 \7 _& A
    Notify(header), t$ ~, z8 R8 }; b" O
return header: ]0 x7 ~+ j, O' @: Y. ~4 S
2.3 GetTransactionByHash
+ C5 N3 @+ R, c1 k7 s# t开发者可以使用 GetTransactionByHash 函数通过交易哈希获取交易。交易哈希以 bytearray 的格式,作为参数传入 GetTransactionByHash。这个函数的关键在于如何转换将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。/ Y1 e! o% j: A( C
我们以16进制格式的交易哈希为例,实现将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。示例哈希如下:
$ z  W! m1 X! ?& N( V# }# J9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1
; F+ F! x5 o  f, @1 u- t首先,将该交易哈希反序得到:0 f/ z8 @9 h/ W* c& F0 s: m0 ~5 g2 ?
c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a2791 z+ ]3 D' z- y; W. c( E
开发者可以通过 SmartX 提供的转换工具 Hex Number(little endian)  Number 实现这一步。0 T" u2 l/ D  w, R  l! f) o
然后,将其转成 bytearray 格式:
! B2 y4 u9 @+ }& R{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}
# _$ L: T* Q1 a& O8 q; W* j开发者可以通过 SmartX 提供的转换工具 String  Byte Array 实现这一步。! \) {' ~% K* N6 l( x
最后,将得到的 bytearray 转换成相应的字符串:
) B8 R" f5 v. h9 A4 u\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; {# G5 D3 x, c' ?4 K
GetTransactionByHash 函数通过交易哈希获取交易的例子如下:% p. z/ M) y9 B! \  X
from ontology.interop.System.Blockchain import GetTransactionByHash' j- w/ I3 g! }/ |6 K
def demo():9 v  u# }, D$ f9 a6 k" |$ l
    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"   
/ z: d' K0 i7 U  \) 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")
6 u- N1 ?* Q( y$ h3 ?! U' X4 [    tx=GetTransactionByHash(tx_hash)* ~0 R9 o0 g/ @4 b: @  b  `
    return tx
" F0 E6 ]" ?& X8 O6 ^+ q2.4 GetTransactionHeight. z2 g7 y2 A: p. h, Y4 d
开发者可以使用 GetTransactionHeight 函数通过交易哈希获取交易高度。我们还是以上个例子中的哈希为例:
! _' E8 T; T* S  z( dfrom ontology.interop.System.Blockchain import  GetTransactionHeight
0 ~! y; h) |/ F) e/ Cdef demo():
, G3 P2 w2 P! L) ~: C$ ?    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"   
4 e% ]1 @, U7 G) f2 [2 G% K    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 H8 W$ N" H" Y1 W- r% P2 [# T
    height=GetTransactionHeight(tx_hash)
; c0 L  [6 `9 a/ X. a    return height
- l3 @* a& C# t6 ~) I2.5 GetContract
  Z' V4 q: x6 @" O! R开发者可以使用 GetContract 函数通过合约哈希获取合约。其中,合约哈希的转换过程与上面讲到的交易哈希转换过程一致。
$ G- C) E8 c1 d6 R" \& ifrom ontology.interop.System.Blockchain import GetContract8 ?6 W8 U2 P7 f) [
def demo():
" u: l1 {) ~( r* |$ o    # contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"   
! `6 f7 L' N1 _# y    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( X+ h0 l$ S( J4 m
    contract=GetContract(contract_hash), y) m+ {7 @, t# q
    return contract
- J1 ]5 r, D( ]5 z2.6 GetBlock' g- @; ]  U( L& M9 e
开发者可以使用 GetBlock 函数获取区块。有两种方法可以获取指定区块:
: r! I6 e. y& e! P' M5 g  T! u. X通过块高获取区块:
' `( n0 I7 a1 K3 L( M! ]
; }& B  M5 `! t- ^from ontology.interop.System.Blockchain import GetBlock
8 j: K* K) c+ K7 |! bdef demo():
8 C$ \3 V8 S5 ]+ ?    block=GetBlock(1408)
4 q3 u" t. J( H3 G' D  `8 X& f    return block
( k! G; W) o" X- z- ^2. 通过区块哈希获取区块:# h3 H1 n; l  X* y# w% r3 m
from ontology.interop.System.Blockchain import GetBlock
7 Z/ s- B1 E" G) Q1 y6 r- ?def demo():    ; F5 m. v4 l5 v$ 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')( V" c6 t% p" n: D% i/ Q1 B0 }6 l
    block=GetBlock(block_hash)& |" x# h2 T' @
03 Block API 使用方法1 n  U4 q7 J6 D+ p; l
Block API 中可供引用的函数有三个,它们分别是 GetTransactions、GetTransactionCount 和 GetTransactionByIndex。我们依次介绍下这三个函数。4 p7 Y6 h6 V5 R0 T0 _
3.1 GetTransactionCount
4 @$ P! s' M, ~) n开发者可以使用 GetTransactionCount 函数获取指定区块的交易数量。' L8 y* M& i; P, g
from ontology.interop.System.Block import GetTransactionCount
4 _7 P1 J$ Y- X  o+ ydef demo():$ Y$ ^6 v  h. s! ^
    block=GetBlock(1408)5 B8 K( _4 g- R% y- \' d8 P
    count=GetTransactionCount(block)
0 j5 L5 A0 g  ?$ p. m0 w' Z    return count9 Q- I6 C/ g2 u. v
**
# H4 P! H/ q- m3.2 GetTransactions**
- R( R* T9 G3 X开发者可以使用 GetTransactions 函数获取获取指定区块的所有交易。
( @. w& Z! z+ H. {5 Wfrom ontology.interop.System.Blockchain import GetBlock% G! p' V3 m9 ?. [! `5 X  r2 l
from ontology.interop.System.Block import GetTransactions
9 L+ v/ @5 b$ M" z" a3 ?def demo():
5 T0 D: A. M/ z9 P8 G! k, ~6 W( u- j    block=GetBlock(1408)
1 x$ Y" L: q) n: X% o4 F    txs=GetTransactions(block)
, G, w1 b9 E9 G  i: w/ o9 s5 z9 y( g    return txs
0 M1 f1 \8 H* F3.3 GetTransactionByIndex5 k1 g$ Y9 O7 I, W! H( L) _' V
开发者可以使用 GetTransactionByIndex 函数获取指定区块的指定交易。
8 T* k( ~' w: J* }9 e- o: [from ontology.interop.System.Blockchain import GetBlock' W% r0 K5 I* d# M' m$ K
from ontology.interop.System.Block import GetTransactionByIndex
8 g- p- R3 j  i4 P* a' L1 @def demo():
- {0 }% Y6 I8 T- Q5 T4 c    block=GetBlock(1408)$ L; g# Q$ w, ?6 B, {
    tx=GetTransactionByIndex(block,0) # index starts from 0.
; I, ?7 ^8 K4 n5 s) f1 q4 B0 ]% |; X    return tx. F: t/ X4 J0 D7 s7 v
04 后记
4 D; `4 ~" h, d; `  @% ~Blockchain & Block API 在智能合约中起到查询区块链数据和区块数据的作用,是智能合约最不可缺少的一部分。在后面的技术视点中,我们将讨论如何使用其它 API,探讨它们和本体区块链的交互。、
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

黄河347 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1