Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

wk国际特价机票
157 0 0
01 导语
8 \) P; D) U& @/ w8 e0 q上一期我们介绍了本体智能合约存储 API,相信很多小伙伴都明白了在本体上进行 Python 智能合约开发时如何调用相关 API 进行持久化存储。本期我们讨论如何使用 Runtime API(合约执行 API)。Runtime API 共有8个相关的 API,提供了合约执行时常用的接口,帮助开发者获取数据、转换数据以及验证数据。这8个 API 的简单描述如下:
9 u- m6 }/ W, o1 F
9 |2 X; N# t& W  B6 U下面我们具体讲述一下这8个 API 的使用方法。在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。同样,在文章最后我们将给出这次讲解的所有源代码以及视频讲解。- n/ M$ n/ L( x$ T9 Z
02 Runtime API 使用方法
4 S% p- s/ C9 z" o8 K' ORuntime API 的引用分为两个路径,分别是 ontology.interop.System.Runtime 和 ontology.interop.Ontology.Runtime。其中,Ontology 路径下包含了新增的 API。下列语句引用了这几个 API。' f& k. k$ y) r
from ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize
( |& A- k! z* s4 \6 Jfrom ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHas/ V  E; o  g6 r& t/ A
2.1  Notify API
7 J% L3 o+ t' s( [( ~Notify 函数将事件推送到全网。如下例子中函数 Notify 将会返回 “hello world” 十六进制字符串,并将其推送全网。4 c+ r2 O/ o) C  y0 s' x7 q+ Z/ i
from ontology.interop.System.Runtime import Notify
, ~4 w5 `  o( @9 n( E" Sdef demo():
7 X5 H) u1 b; F/ L9 m    Notify("hello world")3 t' r" c: I  {+ X. u5 s% b
小伙伴可以在 Logs 中查看:! p$ R) h9 d  d; Q# F6 R8 e3 |% f

) d# x% q- n7 p2 O+ A# j2.2  GetTime API* Z% o3 @) t' K! i2 E/ S+ ^$ q
GetTime 函数返回当前时间戳,即返回调用该函数的 Unix 时间,其单位是秒。
* Y0 F" O3 O5 b+ i1 Ffrom ontology.interop.System.Runtime import GetTime
( ~0 J' O3 @( udef demo():* j7 V- M8 K7 p; K
    time=GetTime()
/ h: g4 B' Y+ x. B& {7 n9 S8 N    return time # 返回时间戳, 单位是秒& v) w+ S& C, J' W2 d) R# u
2.3  GetCurrentBlockHash API
, L- n9 d  J; W" z2 O" YGetCurrentBlockHash 函数返回当前区块的哈希值。
1 E8 A' D+ ], m$ a& z. z- rfrom ontology.interop.Ontology.Runtime import GetCurrentBlockHash
" z9 Q9 M7 O* A1 [7 zdef demo():
, C! ?, z' q3 L. j' v    block_hash = GetCurrentBlockHash()
3 t1 b( B3 Q5 r) F  H    return block_hash
* @% K# o+ R) f5 K4 O$ z$ }0 r6 _; r2.4  Serialize 和 Deserialize
0 s5 [( \/ B% t- x这是一对序列化和反序列化函数。其中,Serialize 函数将一个对象序列化成 byte array 对象,而 Deserialize 函数将 byte array 反序列化成原先对象。下面的代码片段实现了将传入参数序列化并存入合约的持久化存储中,同时也实现了从合约的持久化存储中取出数据并把它进行反序列化。. }$ l0 @5 F2 h( ?7 x" S4 I% T8 j
from ontology.interop.System.Runtime import Notify, Serialize, Deserialize. t  n# g% [" N7 u5 F8 x1 S+ p2 q
from ontology.interop.System.Storage import Put, Get, GetContext
8 Z6 K. K% F( A- f" ldef Main(operation, args):2 e- L( P+ R4 \* s# y, s; w9 m( i
    if operation == 'serialize_to_bytearray':6 b  t7 C9 z* {! Z" w
        data = args[0]
' I1 q% J" _3 ^$ i        return serialize_to_bytearray(data)
; b; R1 ~: T2 M    if operation == 'deserialize_from_bytearray':
. ^& ^1 w2 W1 f: z  l' `        key = args[0]% q, s7 U  `+ |  y* r7 U
        return deserialize_from_bytearray(key)0 D: d) W# G0 o$ v2 F8 w
    return False/ T2 Z% J( ^0 }% s+ C
def serialize_to_bytearray(data):. Q+ P$ S, r$ X* t* ^9 ]
    sc = GetContext()4 H) A* _# B4 a- S
    key = "1"% q; J5 ]/ o# J9 A; d
    byte_data = Serialize(data) # 序列化传入的参数
8 r& P, x& B( ~( V" A7 C    Put(sc, key, byte_data) # 将序列化后的数据存入区块链2 [7 [- [) X. i9 h0 S! D
def deserialize_from_bytearray(key):+ j2 o5 z5 ~' y  q" d4 `! O
    sc = GetContext()( Y  C8 K# ]+ I+ O# d
    byte_data = Get(sc, key) # 按key从区块链中取出数据
& |% S2 q; o- F! Z$ C% Y8 ]    data = Deserialize(byte_data) # 将bytearray数据反序列化成原先类型数据
6 x2 K! E3 `1 w1 R5 o    return data0 N$ f2 A/ t, k. o1 y4 y3 {1 |# E
2.5 Base58ToAddress & AdressToBase58
0 A( P+ P- G  V: @& a& B0 R' C9 ^0 S这是一对地址转换函数。其中,Base58ToAddress 函数将 base58 编码的地址转成 byte array 形式地址,而 AddressToBase58 则将 byte array 形式地址转成 base58 编码的地址。
- c- V, n: h4 j$ C- m5 kfrom ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, o, C0 F3 I, t
def demo():( I& q; I$ x* {
    base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn"9 w: \& y2 @& w4 e6 T- n) t# W
    addr=Base58ToAddress(base58_addr) # 将 base58 地址转成 bytearray形式地址+ B6 X7 f+ J$ B+ H" c
    Notify(addr)
0 {' R: d+ L5 m9 R; `    base58_addr=AddressToBase58(addr) #将bytearray地址转为base58地址
# C9 o' y( F# A    Notify(base58_addr)
; K0 t$ a2 ^" {2.6  CheckWitness  v( l. j8 |# p1 H6 d7 D* {
CheckWitness(fromAcct) 函数有两个功能:
# I1 L3 P8 U8 n* w验证当前的函数调用者是不是 fromAcct 。若是(即签名验证通过),则函数返回通过。
. r2 m; f$ K4 G- q) @6 `检查当前函数调用者是不是一个合约。若是合约,且是从该合约发起去执行函数,则验证通过。即,验证 fromAcct 是不是 GetCallingScriptHash() 的返回值。其中,GetCallingScriptHash()  函数可以得到调用当前智能合约的合约哈希值。
4 u' A4 G' a8 @5 M: WGetCallingScriptHash():5 z+ s# j; W$ l! }) t
https://github.com/ontio/ontology-python-compiler/blob/master/ontology/interop/System/ExecutionEngine.py
* T4 w  W0 G! j. N; Zfrom ontology.interop.System.Runtime import CheckWitness
' F+ ~  t6 V* i( bfrom ontology.interop.Ontology.Runtime import Base58ToAddress
- f. t1 @; z  N; [6 r0 Xdef demo():
$ y9 m  S7 a9 v1 {; L7 B    addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z")
. b7 O6 J/ e2 U0 Z1 Y: a8 r    res=CheckWitness(addr) # 验证调用者地址是否为AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z% p  K% _9 [3 a7 i( K
    return res
9 m' G; z# f/ M$ e+ B2 V! N7 z* _( }& X03 总结
/ [5 v9 t8 E; ?8 o3 w. n7 p  Q本次技术视点中我们介绍了本体区块链的 Runtime API,该类 API 在本体 Python 智能合约中用处十分巨大。在下一期技术视点中,我们将介绍 Native API,探讨如何在本体智能合约中进行转账等操作。本期讲述的所有语法部分我们提供了中文视频,小伙伴们可以观看学习。
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    0

  • 主题

    2