Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

黄河347
188 0 0
01 前言
6 G2 b% X; h4 r  b1 m8 ]在之前的技术视点文章中,我们介绍了目前本体主网支持的智能合约体系以及相应的智能合约开发工具 SmartX。很多小伙伴都想上手练一练。在本期的本体技术视点中,我们将正式开始讲述智能合约语法部分。+ \( O+ a9 j. N* F
本体的智能合约 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 支持基本的区块查询操作,如查询指定区块交易数等。同时,文末将提供视频讲解。. S. w6 Y. o9 {: {; ~7 S9 O
在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。' o; c! O/ s) t! X) r
02 Blockchain API 使用方法
* O' K8 U/ }& G. M  ?' z) y智能合约函数的引用与 Python 的引用如出一辙。开发者可以根据需要引入相应的函数。例如,下面语句引入了获取当前最新块高函数 GetHeight 和获取区块头函数 GetHeader。) k" f7 S+ V3 m- x
from ontology.interop.System.Blockchain import GetHeight, GetHeader7 r/ A8 v4 l# n; t
2.1 GetHeight3 m' B' c8 j' @; `
开发者可以使用 GetHeight 来获取当前最新块高,具体例子如下。在后面的例子中,为了节省空间,我们将省略 Main 函数,小伙伴在练习的时候可以根据需要加入。" I% v; J; y$ I: ]1 o! m. k+ o
from ontology.interop.System.Runtime import Notify
( `( f& J7 y* j! Ofrom ontology.interop.System.Blockchain import GetHeight
( k1 i% J+ t  g3 n; N2 o% ^def Main(operation):/ p0 n+ }3 L, a5 g, \3 P3 i
    if operation == 'demo':
( n, ?( l% D4 T) l: c  c        return demo()5 U) _& V' E* `
    return False9 j: Q$ V' E  [4 \
def demo():
5 L/ s6 z5 q/ a* ^/ U6 v4 w9 N    height=GetHeight()
  U4 b4 s4 q( ]& T    Notify(height) # 打印height
. X4 `! z4 d/ h$ G9 l& H9 T9 p; y8 v+ W    return height #在函数运行结束后返回height7 y1 r: `9 `+ F+ h( \
2.2 GetHeader
1 u4 r# K5 ?& C开发者可以使用 GetHeader 来获取区块头,参数是某个块的块高。具体例子如下:: A* C. v$ {1 @* R/ ^0 g) ?7 b* L
from ontology.interop.System.Runtime import Notify. U/ P" r' Z! C5 W# s# h& H
from ontology.interop.System.Blockchain import GetHeader
6 [' v! g* d; ~1 @4 Sdef demo():
- S% I, ?) A% W. f/ Q& Z    block_height=10
( _  ^0 s+ ?( E4 _8 v. x    header=GetHeader(block_height)
) A/ C6 @/ V- h& B) Q    Notify(header)
/ O' s8 r  q  b2 X  u6 Oreturn header" W! V$ F2 X' W# R! W
2.3 GetTransactionByHash8 J$ p$ ]" F9 y4 _* k( b& R  u$ W
开发者可以使用 GetTransactionByHash 函数通过交易哈希获取交易。交易哈希以 bytearray 的格式,作为参数传入 GetTransactionByHash。这个函数的关键在于如何转换将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。  Q/ a" F9 y' `$ z2 M
我们以16进制格式的交易哈希为例,实现将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。示例哈希如下:1 e% t6 q; @& L. R7 `; h
9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1
2 {9 ~$ w! \: c8 }2 h, F9 w3 p首先,将该交易哈希反序得到:' p  r/ i. l: U" |$ G
c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279( I" c- c. E2 _- W- r, f3 c
开发者可以通过 SmartX 提供的转换工具 Hex Number(little endian)  Number 实现这一步。& {3 r4 Y) f' ]7 @- M% r/ `/ H3 j
然后,将其转成 bytearray 格式:# C% B5 I- q: U. i. t* b7 W9 W
{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}" n- {1 [* W+ e% Q; d
开发者可以通过 SmartX 提供的转换工具 String  Byte Array 实现这一步。
8 N' b# w4 I5 D/ ^" o& n最后,将得到的 bytearray 转换成相应的字符串:
- \0 L( Y- ~  [" X\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
# I% y. z) F* G" k  W  T  x/ tGetTransactionByHash 函数通过交易哈希获取交易的例子如下:/ C0 d* Q2 P- f# P( h7 S, z
from ontology.interop.System.Blockchain import GetTransactionByHash. \3 {7 K; b/ D7 ^5 y$ |- r
def demo():$ `3 @8 s8 `8 ?# {
    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"   
4 ^& M1 p; C3 H. a9 V1 y: W    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")( x! _+ A1 L+ S
    tx=GetTransactionByHash(tx_hash)' G6 A5 n2 F8 k0 B6 s8 b
    return tx
  g' L8 W+ c- W2 C) N$ ^% S4 e2.4 GetTransactionHeight
+ [7 ~* t9 C8 `: U3 k' g开发者可以使用 GetTransactionHeight 函数通过交易哈希获取交易高度。我们还是以上个例子中的哈希为例:
9 M7 J: p7 n) Y: @from ontology.interop.System.Blockchain import  GetTransactionHeight" o) `9 ?) @" T3 ~
def demo():( t. ^9 ^/ ]1 p2 r' `
    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"   
1 }) k) o  M: w! C! d/ X    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")
! Z! l" `$ m7 @, h! Z5 T    height=GetTransactionHeight(tx_hash)
3 T# _2 c  e+ _2 W* K- j    return height
% D+ J" T* C6 p" Y- |2.5 GetContract
9 C8 Q# G8 R( X4 G4 ?' I( z3 m开发者可以使用 GetContract 函数通过合约哈希获取合约。其中,合约哈希的转换过程与上面讲到的交易哈希转换过程一致。. O2 a# H4 B- `! V& H
from ontology.interop.System.Blockchain import GetContract4 i0 k7 G5 A* D2 g+ N
def demo():
" J& @5 S8 O8 @' g/ z6 C    # contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"   
$ S+ s* ~1 m- U& E! h& N    contract_hash=bytearray(b"\xfa\x98\x96\x21\x32\x32\xbb\xf0\x9f\x23\x91\xfa\xef\x95\x9b\xff\xa5\x75\x1a\xd8")
3 Y( i- k# e& K2 E* v& q    contract=GetContract(contract_hash)+ W' Y) D7 C  c6 H  @
    return contract- W2 W  U* |9 `
2.6 GetBlock, ]& p; b, c6 f' X2 Q
开发者可以使用 GetBlock 函数获取区块。有两种方法可以获取指定区块:
( x: [( g! S: o* N通过块高获取区块:: G6 b6 s) X, o+ O% o9 x
& Z  }* y+ Q8 {
from ontology.interop.System.Blockchain import GetBlock5 c$ Z- t$ ~4 R: D$ @8 C1 z- Z
def demo():, e4 m' o" Y# ^$ \
    block=GetBlock(1408)$ I) R$ s# b; I$ {5 k
    return block: w4 B* j7 v1 \1 ?% T1 O
2. 通过区块哈希获取区块:
7 o/ l+ i/ E0 l4 l9 ?from ontology.interop.System.Blockchain import GetBlock8 y' ~- e/ a) [# x! D
def demo():   
7 ]( L, \5 o9 e. 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')* y4 M: k0 [# R5 z
    block=GetBlock(block_hash)* N% B0 w4 L; T- p2 x
03 Block API 使用方法
( J) b. T  F+ x3 n  TBlock API 中可供引用的函数有三个,它们分别是 GetTransactions、GetTransactionCount 和 GetTransactionByIndex。我们依次介绍下这三个函数。4 H$ b; }+ Y+ J
3.1 GetTransactionCount! U$ }) U+ C  w. ^% {
开发者可以使用 GetTransactionCount 函数获取指定区块的交易数量。% L8 _$ x" C8 a+ u
from ontology.interop.System.Block import GetTransactionCount
9 u6 ^& Q# t" q! y- w& ddef demo():" d5 m) T% q- v9 Z  [
    block=GetBlock(1408)
5 c) Q' l7 x3 n) l    count=GetTransactionCount(block)
" v$ `. b4 ]/ M- a    return count
9 O1 _' P7 E. ]( o8 R**
8 R' P6 A2 B5 _3.2 GetTransactions**
6 q# a, P  |8 Z+ l- W" W2 r; w开发者可以使用 GetTransactions 函数获取获取指定区块的所有交易。
3 X5 N  u8 N  u6 e* m- n# S/ Z7 tfrom ontology.interop.System.Blockchain import GetBlock
7 C/ M/ Z7 [: a" {) O6 efrom ontology.interop.System.Block import GetTransactions % t, r2 f' ^0 y9 y
def demo():
# F* K4 J( }' F: i  m8 [& s* {    block=GetBlock(1408)7 k5 h. u' W( {. a* O: {) G( {# M8 X
    txs=GetTransactions(block)
1 G' _5 e$ @' C- ?; P6 C. R    return txs
. q  E8 s- x& ^& v" n3.3 GetTransactionByIndex
0 r3 \, T, s* u: S开发者可以使用 GetTransactionByIndex 函数获取指定区块的指定交易。7 b* b- M) o( }
from ontology.interop.System.Blockchain import GetBlock$ e& d" D$ v2 U* s5 Y! e3 V2 p
from ontology.interop.System.Block import GetTransactionByIndex* g( `3 E# n: l  N' V
def demo():
( v* o  y5 l, @1 Y" v    block=GetBlock(1408)
: O% [" |3 _2 S$ C    tx=GetTransactionByIndex(block,0) # index starts from 0.- h4 U( n3 x- c+ p
    return tx
3 F2 k4 q. ^5 V$ B3 [04 后记- j( W$ V# A5 D# L5 g  U6 e
Blockchain & Block API 在智能合约中起到查询区块链数据和区块数据的作用,是智能合约最不可缺少的一部分。在后面的技术视点中,我们将讨论如何使用其它 API,探讨它们和本体区块链的交互。、
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

黄河347 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1