Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

wk国际特价机票
90 0 0
01 导语1 B, O6 m* C9 y* i
上一期我们介绍了本体智能合约存储 API,相信很多小伙伴都明白了在本体上进行 Python 智能合约开发时如何调用相关 API 进行持久化存储。本期我们讨论如何使用 Runtime API(合约执行 API)。Runtime API 共有8个相关的 API,提供了合约执行时常用的接口,帮助开发者获取数据、转换数据以及验证数据。这8个 API 的简单描述如下:$ [  s! s1 L# K0 p1 j8 L

9 p" z. R, Q, A, c下面我们具体讲述一下这8个 API 的使用方法。在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。同样,在文章最后我们将给出这次讲解的所有源代码以及视频讲解。- K, l7 c; t: D6 q, C8 {2 K
02 Runtime API 使用方法$ U2 b8 b/ m4 i/ N7 Q
Runtime API 的引用分为两个路径,分别是 ontology.interop.System.Runtime 和 ontology.interop.Ontology.Runtime。其中,Ontology 路径下包含了新增的 API。下列语句引用了这几个 API。
' m$ a$ j, ], P( Q0 X9 P0 F4 mfrom ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize
/ R! g& ~4 q/ _from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHas9 A1 q) s; [$ e
2.1  Notify API
8 w7 C* o+ E% W% J+ J  k& |Notify 函数将事件推送到全网。如下例子中函数 Notify 将会返回 “hello world” 十六进制字符串,并将其推送全网。- E# q" l! ]8 c: N( R2 F. @
from ontology.interop.System.Runtime import Notify" B( Y6 L9 Q! p: C3 F
def demo():
1 z: N& |, {4 k7 u8 x3 }    Notify("hello world")
- I+ Q! l. F4 |. c5 A小伙伴可以在 Logs 中查看:: ]2 k' }' N$ C9 i+ v; ~$ J
# c" W0 Z, b0 H; T
2.2  GetTime API
, p9 o7 |5 ?1 Q/ i6 v; N% ^GetTime 函数返回当前时间戳,即返回调用该函数的 Unix 时间,其单位是秒。+ m8 `( P: _6 s
from ontology.interop.System.Runtime import GetTime: f3 u; p# j, I, e5 }% C; u
def demo():: N; u8 |  V9 R% Z3 m
    time=GetTime()
6 K3 V, q) A; m) G8 f, J# _    return time # 返回时间戳, 单位是秒( {" F# ^  e+ ~" n: i
2.3  GetCurrentBlockHash API
& V3 m/ n( K- g/ ?/ N1 P+ UGetCurrentBlockHash 函数返回当前区块的哈希值。
' g+ ^: G$ K6 j1 v( A0 o0 Qfrom ontology.interop.Ontology.Runtime import GetCurrentBlockHash4 c8 v( a: b3 y! x6 t! ?
def demo():
" h# A  t: r# y& q    block_hash = GetCurrentBlockHash()6 y* l  J- o8 k3 @  k
    return block_hash3 Y) ]# ?, p; w4 H0 }, W4 \4 v/ W
2.4  Serialize 和 Deserialize
5 ?& l' V/ `- p+ _# a% C9 Q5 l这是一对序列化和反序列化函数。其中,Serialize 函数将一个对象序列化成 byte array 对象,而 Deserialize 函数将 byte array 反序列化成原先对象。下面的代码片段实现了将传入参数序列化并存入合约的持久化存储中,同时也实现了从合约的持久化存储中取出数据并把它进行反序列化。
1 J% ~6 F' x, u/ @1 ^# Hfrom ontology.interop.System.Runtime import Notify, Serialize, Deserialize# l8 V0 W" q- `
from ontology.interop.System.Storage import Put, Get, GetContext
2 H! i1 c" }6 zdef Main(operation, args):( ~: e5 Z; ^7 {, e7 O0 U
    if operation == 'serialize_to_bytearray':5 g# V, L+ q. \5 v& d5 h& h' q
        data = args[0], p1 w2 q' R7 F
        return serialize_to_bytearray(data)% S- Z" b" s% s
    if operation == 'deserialize_from_bytearray':7 t5 Y1 v; U% F5 @4 z) K5 U- B- q
        key = args[0]) v: Y; @8 M% _) h. U( [  V
        return deserialize_from_bytearray(key)
9 n* }, s% u3 P% n6 B8 J, G. Z    return False0 k4 s: V& d' {6 O$ c; l: a
def serialize_to_bytearray(data):
: ?, W" ^1 G' q$ J& _    sc = GetContext()
7 n8 Z+ l- j* N  `    key = "1"0 j: H. e+ s0 }$ B1 F; ?- p8 F
    byte_data = Serialize(data) # 序列化传入的参数
; R) U( J9 V% L3 y, f    Put(sc, key, byte_data) # 将序列化后的数据存入区块链( W( [7 P: h8 l+ z7 Y! f) ^; r
def deserialize_from_bytearray(key):
7 K/ z" i2 N" I: n) I    sc = GetContext()8 G& @0 `3 Z" J7 ~4 B0 }
    byte_data = Get(sc, key) # 按key从区块链中取出数据( r6 H( p; Q4 F4 j5 i4 \
    data = Deserialize(byte_data) # 将bytearray数据反序列化成原先类型数据
, V  g, y! d4 x. ^, O/ I    return data
, y- y' L5 n3 w7 a; _2.5 Base58ToAddress & AdressToBase58
6 Y- ]6 _' k% c3 q8 R这是一对地址转换函数。其中,Base58ToAddress 函数将 base58 编码的地址转成 byte array 形式地址,而 AddressToBase58 则将 byte array 形式地址转成 base58 编码的地址。( ?. x8 [- J! m+ y
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58( v) ]+ k: R" m2 U
def demo():' D+ E# p% m1 M3 U& B, F' ~
    base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn"' C/ Z  ]7 {  F+ a7 a$ @( d
    addr=Base58ToAddress(base58_addr) # 将 base58 地址转成 bytearray形式地址! D6 }2 I: S4 D# j6 a" M
    Notify(addr)
$ i# v  `! P* E8 X; o- A3 o    base58_addr=AddressToBase58(addr) #将bytearray地址转为base58地址
: u# t( N. d  P6 T! ?    Notify(base58_addr)' x, g2 T# _2 W: A! F2 z
2.6  CheckWitness7 c" x1 Q8 q, T0 I; O# Q8 d+ }
CheckWitness(fromAcct) 函数有两个功能:' L8 g. U1 J0 u  x8 R7 ~1 j  W4 S1 W( s
验证当前的函数调用者是不是 fromAcct 。若是(即签名验证通过),则函数返回通过。3 l: T& d( w& u& A. |
检查当前函数调用者是不是一个合约。若是合约,且是从该合约发起去执行函数,则验证通过。即,验证 fromAcct 是不是 GetCallingScriptHash() 的返回值。其中,GetCallingScriptHash()  函数可以得到调用当前智能合约的合约哈希值。
2 x: d% w5 l- |8 n7 u) V3 hGetCallingScriptHash():
( w( @3 Z7 C  Qhttps://github.com/ontio/ontology-python-compiler/blob/master/ontology/interop/System/ExecutionEngine.py, Q  N5 s& ]( [, Z& b
from ontology.interop.System.Runtime import CheckWitness  N3 a: ]! W6 d1 `  u* C% _2 u) Q
from ontology.interop.Ontology.Runtime import Base58ToAddress
( ~% G& W7 a6 H2 Y0 y% M/ @1 z7 K+ Ydef demo():$ I+ W/ Q# y. [7 I9 j5 L
    addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z")
0 w; _% Q/ V. v1 V% Z( h    res=CheckWitness(addr) # 验证调用者地址是否为AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z
8 T! b4 N; J0 O+ z5 s: z    return res! a! Y; |  G; B# w
03 总结1 q& F& v. z/ S
本次技术视点中我们介绍了本体区块链的 Runtime API,该类 API 在本体 Python 智能合约中用处十分巨大。在下一期技术视点中,我们将介绍 Native API,探讨如何在本体智能合约中进行转账等操作。本期讲述的所有语法部分我们提供了中文视频,小伙伴们可以观看学习。
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    0

  • 主题

    2