Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

Python智能合约执行API知多少?

wk国际特价机票
91 0 0
01 导语
6 \, z) Z  C0 l4 u1 d' u; ]- W上一期我们介绍了本体智能合约存储 API,相信很多小伙伴都明白了在本体上进行 Python 智能合约开发时如何调用相关 API 进行持久化存储。本期我们讨论如何使用 Runtime API(合约执行 API)。Runtime API 共有8个相关的 API,提供了合约执行时常用的接口,帮助开发者获取数据、转换数据以及验证数据。这8个 API 的简单描述如下:2 d( T$ k8 _  T) Z
! |1 `, j( b" v, t6 y+ \
下面我们具体讲述一下这8个 API 的使用方法。在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。同样,在文章最后我们将给出这次讲解的所有源代码以及视频讲解。
' m+ t& ]6 n( t. J3 t" q# p* @4 ~02 Runtime API 使用方法
3 R" p4 C# b% D9 t) [4 v- jRuntime API 的引用分为两个路径,分别是 ontology.interop.System.Runtime 和 ontology.interop.Ontology.Runtime。其中,Ontology 路径下包含了新增的 API。下列语句引用了这几个 API。
* d4 ?/ T6 p/ Z, C) P' Afrom ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize
+ s% G% x& d2 d& T, G$ ufrom ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHas4 `! O0 _, H( c
2.1  Notify API# F5 L) A2 r* S( N
Notify 函数将事件推送到全网。如下例子中函数 Notify 将会返回 “hello world” 十六进制字符串,并将其推送全网。$ H, d1 R$ D. R  y
from ontology.interop.System.Runtime import Notify( ^6 r* v7 V' Q7 p5 }( K
def demo():) i' @/ D6 S2 o+ m$ S: X
    Notify("hello world")
2 e! P$ h5 x8 N" |2 ?& r" s/ V- k小伙伴可以在 Logs 中查看:! Y  z% `9 x. ]7 _

3 [  O# Y  ~- r& d! b$ j7 S7 V' ^2.2  GetTime API
2 w" _' A2 d; v2 \. d# m& A, B$ WGetTime 函数返回当前时间戳,即返回调用该函数的 Unix 时间,其单位是秒。0 H3 d" }- f3 V3 O
from ontology.interop.System.Runtime import GetTime
/ j8 {7 E+ h; S* s" J3 Z. ddef demo():
4 j( J5 T. q& _, t% ]    time=GetTime()9 _% J( k; ~  a" H
    return time # 返回时间戳, 单位是秒
! q. f, E4 o3 r1 P) X+ n' H" R5 q2.3  GetCurrentBlockHash API
5 n6 n: v. v+ U( }GetCurrentBlockHash 函数返回当前区块的哈希值。7 {  r5 r& Q& X8 g$ S3 J# P7 u$ `
from ontology.interop.Ontology.Runtime import GetCurrentBlockHash/ d  S. Z6 R5 T
def demo():
& [" B  |  h4 T    block_hash = GetCurrentBlockHash()& ~" x% u0 l- K$ c
    return block_hash
8 L! t! B( {& H6 }6 \+ I2.4  Serialize 和 Deserialize
: g. M4 V4 A) M$ b2 q$ u这是一对序列化和反序列化函数。其中,Serialize 函数将一个对象序列化成 byte array 对象,而 Deserialize 函数将 byte array 反序列化成原先对象。下面的代码片段实现了将传入参数序列化并存入合约的持久化存储中,同时也实现了从合约的持久化存储中取出数据并把它进行反序列化。
# K7 N7 T1 [5 u+ l0 Q: gfrom ontology.interop.System.Runtime import Notify, Serialize, Deserialize) L4 ~% r( ]0 |4 w
from ontology.interop.System.Storage import Put, Get, GetContext
% b+ a4 F0 |% j) i" v! E$ w3 |def Main(operation, args):% g! {) F* c* H- }
    if operation == 'serialize_to_bytearray':
+ Y! S7 D( m8 w# _, c        data = args[0], z' ~  Q2 G7 T& _
        return serialize_to_bytearray(data)
, j. [. |  ]' q$ O    if operation == 'deserialize_from_bytearray':
' i1 N5 E9 D' R' k3 d6 D5 s, k        key = args[0]; I8 v7 o) [3 F# Z9 X8 o
        return deserialize_from_bytearray(key)4 L/ ^6 v( U* F; x
    return False
; A: \8 b# ]3 c( Y$ M- n, n/ hdef serialize_to_bytearray(data):
  r2 e5 F' U7 U: p$ v    sc = GetContext()
' D7 o7 M& g; a2 [$ ^    key = "1"
% ?% S& Q4 A/ g$ W/ V1 j    byte_data = Serialize(data) # 序列化传入的参数
/ N5 V% o, F% _! i! J    Put(sc, key, byte_data) # 将序列化后的数据存入区块链4 |6 x% c8 I1 ~7 u5 U
def deserialize_from_bytearray(key):
, `4 `& J2 m  X0 o' P7 o    sc = GetContext()
8 q; ]2 I* b3 R! ^    byte_data = Get(sc, key) # 按key从区块链中取出数据
9 D# s) ]( X4 A, ]( m" I* F    data = Deserialize(byte_data) # 将bytearray数据反序列化成原先类型数据
+ U5 R, g% d6 ?8 \4 Y    return data
$ I- @* Z3 P& R4 b& J2.5 Base58ToAddress & AdressToBase58# B+ r4 n2 K. w  |
这是一对地址转换函数。其中,Base58ToAddress 函数将 base58 编码的地址转成 byte array 形式地址,而 AddressToBase58 则将 byte array 形式地址转成 base58 编码的地址。4 W. \7 [0 N5 c( v  I5 M
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58
1 k9 V# I. Q, ?def demo():
0 _* O! l, d! x% ~) G    base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn"
8 h6 z" L2 u9 q# R    addr=Base58ToAddress(base58_addr) # 将 base58 地址转成 bytearray形式地址
) P  v, R- z! h7 T5 O) _    Notify(addr)
0 q7 w: b* w  F. H6 f. F( t    base58_addr=AddressToBase58(addr) #将bytearray地址转为base58地址
1 ?  A9 N8 a6 w! d. C* f/ p- z    Notify(base58_addr)
# p4 y$ E( M8 W; T8 r& {8 h  v# Q2.6  CheckWitness
" t7 [" c$ E: }  lCheckWitness(fromAcct) 函数有两个功能:
( b* M5 ^, M( v验证当前的函数调用者是不是 fromAcct 。若是(即签名验证通过),则函数返回通过。8 k6 W/ L+ G5 r9 R" y
检查当前函数调用者是不是一个合约。若是合约,且是从该合约发起去执行函数,则验证通过。即,验证 fromAcct 是不是 GetCallingScriptHash() 的返回值。其中,GetCallingScriptHash()  函数可以得到调用当前智能合约的合约哈希值。
+ c0 n! B* o- ^$ I# |. d% XGetCallingScriptHash():
) I0 f0 f, w1 S$ l- {  r8 w  z6 u9 i; Y$ shttps://github.com/ontio/ontology-python-compiler/blob/master/ontology/interop/System/ExecutionEngine.py
' o! y! M" D7 X  Ofrom ontology.interop.System.Runtime import CheckWitness
* ~$ [) Q6 A5 l- y: F$ r* Gfrom ontology.interop.Ontology.Runtime import Base58ToAddress
, e2 T6 Q4 Q$ s% j# ]" edef demo():
1 ^$ m' ~6 q6 m/ V    addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z")
2 Z) E( M& X, \, ]- |0 i* l    res=CheckWitness(addr) # 验证调用者地址是否为AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z! p7 B9 P0 x0 F7 ~7 p1 I6 s
    return res
, ^1 {; P( A+ a' z7 b. Q03 总结
/ o  d0 ^* a8 l本次技术视点中我们介绍了本体区块链的 Runtime API,该类 API 在本体 Python 智能合约中用处十分巨大。在下一期技术视点中,我们将介绍 Native API,探讨如何在本体智能合约中进行转账等操作。本期讲述的所有语法部分我们提供了中文视频,小伙伴们可以观看学习。
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

wk国际特价机票 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    2