Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

黄河347
216 0 0
01 前言& z7 X! @8 m" n/ ?* o+ V5 N% f
在之前的技术视点文章中,我们介绍了目前本体主网支持的智能合约体系以及相应的智能合约开发工具 SmartX。很多小伙伴都想上手练一练。在本期的本体技术视点中,我们将正式开始讲述智能合约语法部分。/ e4 n  S5 z2 f, 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 支持基本的区块查询操作,如查询指定区块交易数等。同时,文末将提供视频讲解。1 W+ X; i& P4 n
在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。
5 V% {: S" Y  d1 t; y: v5 ^02 Blockchain API 使用方法* P' z* p8 s8 E, a% Z3 o, l
智能合约函数的引用与 Python 的引用如出一辙。开发者可以根据需要引入相应的函数。例如,下面语句引入了获取当前最新块高函数 GetHeight 和获取区块头函数 GetHeader。
& h+ \$ s8 D1 u3 _) Ufrom ontology.interop.System.Blockchain import GetHeight, GetHeader
4 Q3 L) ?+ r$ a) V/ e2.1 GetHeight, e8 W$ b, p( y' X' S& a% V4 N
开发者可以使用 GetHeight 来获取当前最新块高,具体例子如下。在后面的例子中,为了节省空间,我们将省略 Main 函数,小伙伴在练习的时候可以根据需要加入。- Q4 Q, ~" O! }  d# I1 ?
from ontology.interop.System.Runtime import Notify8 G# u8 v, ?) b! b+ v" O
from ontology.interop.System.Blockchain import GetHeight3 c; R( I. t1 J( [" U
def Main(operation):
5 k- I; A6 }4 _* h1 W6 K    if operation == 'demo':! B1 @" [- ^( a+ T
        return demo()
5 `& T6 Z' O* r( }    return False
* \% V5 c! W/ N5 Hdef demo():
0 p0 V& t1 `6 z4 a; n- V" |3 q" \    height=GetHeight()9 F) k# v4 I! s7 j- S
    Notify(height) # 打印height1 x  E: c# {7 d2 N0 O& @- M
    return height #在函数运行结束后返回height
9 }( i5 o7 n+ F6 Q! c# [: s2.2 GetHeader
( c: b+ f5 E4 k9 b开发者可以使用 GetHeader 来获取区块头,参数是某个块的块高。具体例子如下:
" I9 v+ ^9 U- q9 \from ontology.interop.System.Runtime import Notify
1 l4 J8 _3 g5 J9 ]* ]4 b3 m0 Z9 Tfrom ontology.interop.System.Blockchain import GetHeader' {2 T5 p6 d  Y3 K. z3 L7 y
def demo():/ `: H1 N; B! t/ d
    block_height=10% {* T% o! U5 z/ D7 M# g  R. }
    header=GetHeader(block_height)
# Z  P$ `8 x% I' X0 l4 ?- B- `    Notify(header)- T, j" v  w% J- o* _
return header8 ~6 F/ u) Q* y) M
2.3 GetTransactionByHash
6 Z; h) V3 {% X开发者可以使用 GetTransactionByHash 函数通过交易哈希获取交易。交易哈希以 bytearray 的格式,作为参数传入 GetTransactionByHash。这个函数的关键在于如何转换将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。
- M4 ], N# A+ v2 F3 b9 O我们以16进制格式的交易哈希为例,实现将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。示例哈希如下:
& I' B) E, {  m! L# ?8 H) C0 a; E9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1
  E; k: F6 t* V7 M5 g: u+ C- N首先,将该交易哈希反序得到:$ B8 d" q$ a8 d
c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279
* Z5 ?( Y) r! L; k; o开发者可以通过 SmartX 提供的转换工具 Hex Number(little endian)  Number 实现这一步。
9 i, T( I, E0 l" E4 B8 [3 |/ r) n然后,将其转成 bytearray 格式:
" n) @. C1 Y4 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  j! M2 i; F0 e- A2 O
开发者可以通过 SmartX 提供的转换工具 String  Byte Array 实现这一步。
# i' U8 B. p. E5 r3 Z最后,将得到的 bytearray 转换成相应的字符串:+ `" S! u+ e; N
\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
, j, y; J9 q* u' R1 yGetTransactionByHash 函数通过交易哈希获取交易的例子如下:. c+ U: l8 a* f, h
from ontology.interop.System.Blockchain import GetTransactionByHash9 P' S0 F1 G. o3 S
def demo():  J" n; }% W2 m+ i5 X! l0 {/ h6 u3 ]
    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"   
) v3 |1 _. x. t! k4 o    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")7 ~& T3 K4 C9 E1 k4 ^+ ]& A
    tx=GetTransactionByHash(tx_hash)
9 o! O" Q$ f, G9 @* n8 N. R    return tx
6 i1 x: O: A. C7 D2.4 GetTransactionHeight  D: R8 m9 a1 x
开发者可以使用 GetTransactionHeight 函数通过交易哈希获取交易高度。我们还是以上个例子中的哈希为例:4 o8 C6 d5 ~( O* {* `. p: I' M
from ontology.interop.System.Blockchain import  GetTransactionHeight& M- \) ~" R$ G9 A
def demo():
5 D6 n, F4 D+ M) A$ _    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"    4 N2 e+ d1 _* v9 _& T* P! {
    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")
% H% E8 I& B) e- l/ v    height=GetTransactionHeight(tx_hash)
6 i9 I$ O  O! |    return height4 I  Q" `4 s* j1 A4 p  G
2.5 GetContract
5 C* W) a  U( S& G- ?开发者可以使用 GetContract 函数通过合约哈希获取合约。其中,合约哈希的转换过程与上面讲到的交易哈希转换过程一致。
2 ?2 {! k' A/ }; xfrom ontology.interop.System.Blockchain import GetContract
  M' ~% \. e( ^def demo():
1 i7 \; [0 x- `# M    # contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"   
* {* n: q$ i5 R; R6 B6 W7 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")
* m! f2 W$ h1 D  b" x; ~3 [2 U    contract=GetContract(contract_hash)& v* u$ [, Z. Y- f9 y# P
    return contract6 M5 E3 r$ e6 z" a3 w
2.6 GetBlock0 z+ g0 V7 J( _- }! Y& W4 C" y
开发者可以使用 GetBlock 函数获取区块。有两种方法可以获取指定区块:
% s& R. h' m- J0 I+ R6 N通过块高获取区块:
( P) m/ u* T1 Y( T  S7 h) R5 @
% t7 O5 U8 b4 Z% o  R9 K7 @from ontology.interop.System.Blockchain import GetBlock! u) R' B% v0 j1 I  r, k0 {- M' R
def demo():1 O0 o9 G. i; q1 K- X9 Q* H6 w
    block=GetBlock(1408)9 C4 s/ C  _# D
    return block( N2 }3 Q+ b+ r$ v* w1 t5 @( E
2. 通过区块哈希获取区块:
# S# c8 G, a; M, `1 Vfrom ontology.interop.System.Blockchain import GetBlock
! o- X1 c4 N+ E; y& p% Bdef demo():   
! |9 R8 ^  c: ~; n+ ?# Q    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')
, \* ?  z8 i3 V+ T+ W6 x    block=GetBlock(block_hash)
. p' X8 |- o& d. b# q& o7 g4 h* C03 Block API 使用方法  ^0 H' O  _0 f. e; H) b
Block API 中可供引用的函数有三个,它们分别是 GetTransactions、GetTransactionCount 和 GetTransactionByIndex。我们依次介绍下这三个函数。* @+ F# \7 J: K; k
3.1 GetTransactionCount
6 M6 c# _0 f! D. b/ m开发者可以使用 GetTransactionCount 函数获取指定区块的交易数量。' ]' G# w( |1 o& B+ \
from ontology.interop.System.Block import GetTransactionCount# ^( x, |  K) s2 E$ S+ p% b3 O! k
def demo():
3 q8 C0 Z6 l9 f$ u4 N. C3 q5 M    block=GetBlock(1408)
) k3 i7 D/ L/ a  G1 m    count=GetTransactionCount(block)3 u4 J1 [& u! \( ]% m5 ^& A
    return count
- e) r, ^/ O  j6 r  m' ~**
# K4 t0 Z: s7 b% a# [3.2 GetTransactions**
3 |6 N; e; G- ^7 |+ P2 p% S$ n! ?, E开发者可以使用 GetTransactions 函数获取获取指定区块的所有交易。
" k0 f  w  z" R4 Y1 k0 I  U( Gfrom ontology.interop.System.Blockchain import GetBlock
1 S- |3 k6 L1 h4 Kfrom ontology.interop.System.Block import GetTransactions 3 J% S1 p8 r) c5 h6 c
def demo():
4 f; m  E" e  A0 I# I/ b: ^3 l    block=GetBlock(1408)
: N9 @2 i. T$ a7 t4 [$ u, {    txs=GetTransactions(block). _: W. q* A* v7 G3 q1 O
    return txs
% e: L4 {# L3 \4 k4 W0 a5 Q+ w3.3 GetTransactionByIndex
4 K% f% J8 o' I# D( R5 H开发者可以使用 GetTransactionByIndex 函数获取指定区块的指定交易。
2 |% \, q) B( \5 Rfrom ontology.interop.System.Blockchain import GetBlock
: y) j' f( c! ?from ontology.interop.System.Block import GetTransactionByIndex
7 A& G4 ~+ i7 ?  Y6 H, ^def demo():+ b9 w' f1 V- K  s
    block=GetBlock(1408)
- E2 |1 o1 N* q$ J8 [9 l    tx=GetTransactionByIndex(block,0) # index starts from 0.2 V% A3 S; Z2 T4 y
    return tx
. K' ]" A& {, }0 g04 后记" z  p+ z4 f8 `
Blockchain & Block API 在智能合约中起到查询区块链数据和区块数据的作用,是智能合约最不可缺少的一部分。在后面的技术视点中,我们将讨论如何使用其它 API,探讨它们和本体区块链的交互。、
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

黄河347 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1