Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

wk国际特价机票
178 0 0
01 导语' x8 s0 a( C4 o7 N" u
上一期我们介绍了本体智能合约存储 API,相信很多小伙伴都明白了在本体上进行 Python 智能合约开发时如何调用相关 API 进行持久化存储。本期我们讨论如何使用 Runtime API(合约执行 API)。Runtime API 共有8个相关的 API,提供了合约执行时常用的接口,帮助开发者获取数据、转换数据以及验证数据。这8个 API 的简单描述如下:3 K5 ?, R6 j6 _. X! g8 Z* [- u4 |
* `' Z, Q4 G( V$ M3 u& N
下面我们具体讲述一下这8个 API 的使用方法。在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。同样,在文章最后我们将给出这次讲解的所有源代码以及视频讲解。
& A! V1 ^7 V' e# k02 Runtime API 使用方法6 ~1 [6 Y- p6 q  B# s
Runtime API 的引用分为两个路径,分别是 ontology.interop.System.Runtime 和 ontology.interop.Ontology.Runtime。其中,Ontology 路径下包含了新增的 API。下列语句引用了这几个 API。
0 u+ t8 c8 c8 E; Q4 k9 c5 \from ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize  ?$ E5 x4 I2 }: x* E2 W
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHas
  L$ K! W9 C  |9 W( c! X, Y2.1  Notify API
7 d" b. @! F! b; F0 JNotify 函数将事件推送到全网。如下例子中函数 Notify 将会返回 “hello world” 十六进制字符串,并将其推送全网。" r' O# F  n/ Z5 r% q; O% f( p7 ~
from ontology.interop.System.Runtime import Notify
0 e  P0 \/ B. C$ l2 Tdef demo():
, ?3 P; h5 N9 p# z. n5 I3 I+ [  p& K    Notify("hello world")6 S0 f, a2 x9 y& @" N2 Z
小伙伴可以在 Logs 中查看:3 i2 V; D9 M. [( ?
, n' D' _- J( ~1 l7 P+ D: l# T: b/ n
2.2  GetTime API% o' F3 v9 d4 D
GetTime 函数返回当前时间戳,即返回调用该函数的 Unix 时间,其单位是秒。
  Q9 C( ^& Y& w1 [! z% ~6 Nfrom ontology.interop.System.Runtime import GetTime
+ X% F: l% Q; \3 y# k  ydef demo():
; N8 ^" C* F0 p    time=GetTime(), L* Z$ n/ t# K) S7 }3 r4 ~% ?
    return time # 返回时间戳, 单位是秒
& b! f2 R* z$ @) H2.3  GetCurrentBlockHash API
+ T5 ~$ |9 d1 K7 V' j4 LGetCurrentBlockHash 函数返回当前区块的哈希值。1 s8 b1 E) D2 `
from ontology.interop.Ontology.Runtime import GetCurrentBlockHash
0 j* A0 r; w3 H, ]. xdef demo():
$ ]) A; I* e# [3 u0 Q1 o! O    block_hash = GetCurrentBlockHash()" `( e& E1 m4 Z& r5 [% ]6 @
    return block_hash# u& @, V4 e; v  b
2.4  Serialize 和 Deserialize
$ ]! ?; Z' q" Z! _0 A' j( D这是一对序列化和反序列化函数。其中,Serialize 函数将一个对象序列化成 byte array 对象,而 Deserialize 函数将 byte array 反序列化成原先对象。下面的代码片段实现了将传入参数序列化并存入合约的持久化存储中,同时也实现了从合约的持久化存储中取出数据并把它进行反序列化。
% O1 M+ x: ~1 Z5 U) Hfrom ontology.interop.System.Runtime import Notify, Serialize, Deserialize
& W2 b+ r( b; M! J0 s, _from ontology.interop.System.Storage import Put, Get, GetContext
8 C" V/ R( R; X: Mdef Main(operation, args):
: O* E( p7 [$ `8 H: d- k# f- k: f    if operation == 'serialize_to_bytearray':7 C) V, ?4 P9 q4 s3 h
        data = args[0]" k- p/ z! i6 k, ]( U! n9 E4 L
        return serialize_to_bytearray(data)
! a$ c9 j7 u7 N% Q    if operation == 'deserialize_from_bytearray':) p. |0 S3 w% d4 Q2 U
        key = args[0]9 |9 P* X9 L# ~1 U) n
        return deserialize_from_bytearray(key)8 n# Y6 s2 r* P% W9 K% [: J
    return False( \, H0 O3 ?8 L3 z
def serialize_to_bytearray(data):( d$ i2 K3 i! b& r0 O
    sc = GetContext()0 F# W% o/ ^, m5 Z! r; P+ h0 g
    key = "1"
9 R+ J& l+ S' s    byte_data = Serialize(data) # 序列化传入的参数2 V; C8 q. [. q% X- Y
    Put(sc, key, byte_data) # 将序列化后的数据存入区块链" K. w0 K8 F+ h
def deserialize_from_bytearray(key):
' `4 a% {' @$ c% ?* F7 y3 \3 Q    sc = GetContext()
4 A% n/ }$ r1 d* f+ e8 w  A    byte_data = Get(sc, key) # 按key从区块链中取出数据
! j( O) e$ C; O* {' h2 f    data = Deserialize(byte_data) # 将bytearray数据反序列化成原先类型数据
7 A' J! d" T/ d* n  M  D$ d    return data
/ d/ a  a) D' B2.5 Base58ToAddress & AdressToBase58
2 e% x. ?% S( A: S  E这是一对地址转换函数。其中,Base58ToAddress 函数将 base58 编码的地址转成 byte array 形式地址,而 AddressToBase58 则将 byte array 形式地址转成 base58 编码的地址。
  H% }7 m, x" f. `4 Hfrom ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58
7 L; L, N4 \1 G: X9 ?def demo():
5 y( _; S( a* z* M3 l; G3 k' L    base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn"
+ {/ O: R: x2 E& z2 g! W- w: t    addr=Base58ToAddress(base58_addr) # 将 base58 地址转成 bytearray形式地址8 F* h& ]0 s( X% \/ y
    Notify(addr)
" R* P% h) L0 D5 F: t; |( w# `1 r8 Y    base58_addr=AddressToBase58(addr) #将bytearray地址转为base58地址
# N/ c% _+ R$ Y* q" A$ z+ C    Notify(base58_addr)
8 U& _; Q/ i- ?5 e' l) l2 X6 Z2.6  CheckWitness
" m  A# {1 Q+ s  B, I7 P$ j; tCheckWitness(fromAcct) 函数有两个功能:
1 N+ x: L+ _$ l7 ]9 v% j4 Y  D验证当前的函数调用者是不是 fromAcct 。若是(即签名验证通过),则函数返回通过。' w1 j" `5 G- L4 }
检查当前函数调用者是不是一个合约。若是合约,且是从该合约发起去执行函数,则验证通过。即,验证 fromAcct 是不是 GetCallingScriptHash() 的返回值。其中,GetCallingScriptHash()  函数可以得到调用当前智能合约的合约哈希值。& H! j, s! A0 @9 W3 |( o
GetCallingScriptHash():9 @, `" a3 h& }, C3 d1 j
https://github.com/ontio/ontology-python-compiler/blob/master/ontology/interop/System/ExecutionEngine.py  Z6 A* i. U; x& t0 F
from ontology.interop.System.Runtime import CheckWitness
" b5 p7 S% r9 l9 pfrom ontology.interop.Ontology.Runtime import Base58ToAddress6 ?/ V7 [' u. x" }) }- u5 [0 v
def demo():
7 q7 P0 ]* O  s* x! D    addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z")- |2 P8 I1 L0 |5 i! w. D
    res=CheckWitness(addr) # 验证调用者地址是否为AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z
/ Q- U6 F/ `6 H/ I    return res
% V/ B* D0 j1 n5 Z* B03 总结- F$ M4 x4 a$ V1 X) e5 \1 A( ~
本次技术视点中我们介绍了本体区块链的 Runtime API,该类 API 在本体 Python 智能合约中用处十分巨大。在下一期技术视点中,我们将介绍 Native API,探讨如何在本体智能合约中进行转账等操作。本期讲述的所有语法部分我们提供了中文视频,小伙伴们可以观看学习。
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    0

  • 主题

    2