Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

wk国际特价机票
62 0 0
01 导语8 \) _, [- N. O6 o7 i7 a+ [
上一期我们介绍了本体智能合约存储 API,相信很多小伙伴都明白了在本体上进行 Python 智能合约开发时如何调用相关 API 进行持久化存储。本期我们讨论如何使用 Runtime API(合约执行 API)。Runtime API 共有8个相关的 API,提供了合约执行时常用的接口,帮助开发者获取数据、转换数据以及验证数据。这8个 API 的简单描述如下:
: Q8 u$ M& Q: z' v) H7 y. \! ^2 L
下面我们具体讲述一下这8个 API 的使用方法。在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。同样,在文章最后我们将给出这次讲解的所有源代码以及视频讲解。
# j0 K9 W* d1 ?02 Runtime API 使用方法# ^7 H9 p3 e# e
Runtime API 的引用分为两个路径,分别是 ontology.interop.System.Runtime 和 ontology.interop.Ontology.Runtime。其中,Ontology 路径下包含了新增的 API。下列语句引用了这几个 API。
" ]2 Y8 O7 @) j1 @8 Q3 yfrom ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize$ S: d' ]; b; z* b! W( N. S
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHas6 F% ^% E* H$ r  u
2.1  Notify API4 `. P' J6 h. P! {3 ]/ }' @: e
Notify 函数将事件推送到全网。如下例子中函数 Notify 将会返回 “hello world” 十六进制字符串,并将其推送全网。
# O3 c7 j% s! vfrom ontology.interop.System.Runtime import Notify0 g3 {/ I* A8 ~+ Y% g
def demo():
0 z0 m) ~% n, b8 p  a7 d% l3 T    Notify("hello world")
- {1 q3 x' D, W6 x小伙伴可以在 Logs 中查看:
% t" o2 J0 z. A0 |0 o5 N' v$ B9 `3 m# \5 T  W$ a/ f4 [7 p
2.2  GetTime API
7 j: C/ O; L; jGetTime 函数返回当前时间戳,即返回调用该函数的 Unix 时间,其单位是秒。
0 g$ R, S8 Z- u# A) j( r8 }from ontology.interop.System.Runtime import GetTime' X4 N; r8 Y8 v5 ]8 K# b- Y) u
def demo():
  x- h- h( y' U  m1 _4 U    time=GetTime()
9 |6 a' ?& ?; Z) q9 C4 A    return time # 返回时间戳, 单位是秒
: @  \1 u- S) d2.3  GetCurrentBlockHash API; f$ l1 w7 @" j9 a+ M
GetCurrentBlockHash 函数返回当前区块的哈希值。) h2 [$ k" D5 G; z" O5 N# V
from ontology.interop.Ontology.Runtime import GetCurrentBlockHash
, S: E9 e2 h& G( j, X4 i; R! |def demo():2 ]& y% {" F  t4 m
    block_hash = GetCurrentBlockHash()6 V4 f! {1 a- S
    return block_hash& G& G5 ]5 U: v
2.4  Serialize 和 Deserialize
* M7 W$ d3 O2 E" N这是一对序列化和反序列化函数。其中,Serialize 函数将一个对象序列化成 byte array 对象,而 Deserialize 函数将 byte array 反序列化成原先对象。下面的代码片段实现了将传入参数序列化并存入合约的持久化存储中,同时也实现了从合约的持久化存储中取出数据并把它进行反序列化。
- W" a4 v) e$ x2 ]# U' Afrom ontology.interop.System.Runtime import Notify, Serialize, Deserialize
# Y/ v0 `6 x/ Q/ l. ofrom ontology.interop.System.Storage import Put, Get, GetContext
* G* v  o& w/ P* M1 Ddef Main(operation, args):+ V  u: b" b3 Y
    if operation == 'serialize_to_bytearray':
) H1 R! x! k; E7 I9 ^        data = args[0]
! C1 u+ Z4 J# Q# k+ U1 ]  s        return serialize_to_bytearray(data)
1 _: F; b3 X- R( n( P" X9 B  t    if operation == 'deserialize_from_bytearray':
' Y6 z4 B% J2 I5 h        key = args[0]; B6 e1 q; u" _/ O
        return deserialize_from_bytearray(key)2 k1 \3 Y% F0 y8 \; w! K
    return False$ L  t! K' Y' z4 R) U+ M
def serialize_to_bytearray(data):
6 L0 e: S4 w& p1 l( j    sc = GetContext()9 i  v' e; ?% c' h
    key = "1"
& X+ o% f$ Z6 y    byte_data = Serialize(data) # 序列化传入的参数5 _8 y& k; t8 y
    Put(sc, key, byte_data) # 将序列化后的数据存入区块链7 m7 L& s) N* ]
def deserialize_from_bytearray(key):
3 P3 s. U# w. S% T    sc = GetContext()4 o# b9 w( ^! z4 W4 [
    byte_data = Get(sc, key) # 按key从区块链中取出数据
4 b2 c) M5 \" a5 V) h1 V$ T    data = Deserialize(byte_data) # 将bytearray数据反序列化成原先类型数据/ W1 `1 y, j  L9 \" ]
    return data
4 t9 u: I: p% I" G9 Y2.5 Base58ToAddress & AdressToBase581 ?: R2 W& t. ]0 I: E5 x" q
这是一对地址转换函数。其中,Base58ToAddress 函数将 base58 编码的地址转成 byte array 形式地址,而 AddressToBase58 则将 byte array 形式地址转成 base58 编码的地址。
& E# G( [# \8 \- W3 Efrom ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58
+ Y# W9 [+ P& m" D- R3 t4 Fdef demo():
8 g" p1 F: a- h& c/ u9 C% v    base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn"
3 D- r7 w) N' f! S# K) Y+ ]    addr=Base58ToAddress(base58_addr) # 将 base58 地址转成 bytearray形式地址8 q: u( P$ R& J7 U
    Notify(addr)
% y, v6 D6 [8 h3 [5 @3 C    base58_addr=AddressToBase58(addr) #将bytearray地址转为base58地址
2 B: Y/ m# _* ^- G7 g# o8 p% `    Notify(base58_addr)) \9 H. b" ~+ C4 \  I5 U
2.6  CheckWitness
5 N0 b( E- q6 B0 U9 YCheckWitness(fromAcct) 函数有两个功能:
& R9 @' T' |/ V验证当前的函数调用者是不是 fromAcct 。若是(即签名验证通过),则函数返回通过。
  e7 I) W. L8 \) `& v3 ]5 C检查当前函数调用者是不是一个合约。若是合约,且是从该合约发起去执行函数,则验证通过。即,验证 fromAcct 是不是 GetCallingScriptHash() 的返回值。其中,GetCallingScriptHash()  函数可以得到调用当前智能合约的合约哈希值。
6 T! y) q$ U8 n- R7 u4 g: wGetCallingScriptHash():9 I& i$ q+ i" }( V$ E4 M
https://github.com/ontio/ontology-python-compiler/blob/master/ontology/interop/System/ExecutionEngine.py% P+ l& D: f5 b* z* [
from ontology.interop.System.Runtime import CheckWitness
" p% x' d4 w7 Qfrom ontology.interop.Ontology.Runtime import Base58ToAddress4 u; |8 z3 H) [% }9 r/ L
def demo():  w* w# g# V4 t5 c- @0 l
    addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z"); a! C" d) u9 Q) C; x' X# j. a
    res=CheckWitness(addr) # 验证调用者地址是否为AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z. q5 Q* c& m8 N8 Y
    return res
+ [8 f9 P8 Z3 X; i+ s; M03 总结3 |% ]5 q! E2 D3 I+ N6 E
本次技术视点中我们介绍了本体区块链的 Runtime API,该类 API 在本体 Python 智能合约中用处十分巨大。在下一期技术视点中,我们将介绍 Native API,探讨如何在本体智能合约中进行转账等操作。本期讲述的所有语法部分我们提供了中文视频,小伙伴们可以观看学习。
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    0

  • 主题

    2