Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

wk国际特价机票
121 0 0
01 导语
9 }3 V4 C7 I5 O: L! o  O上一期我们介绍了本体智能合约存储 API,相信很多小伙伴都明白了在本体上进行 Python 智能合约开发时如何调用相关 API 进行持久化存储。本期我们讨论如何使用 Runtime API(合约执行 API)。Runtime API 共有8个相关的 API,提供了合约执行时常用的接口,帮助开发者获取数据、转换数据以及验证数据。这8个 API 的简单描述如下:
- U. x- t% i8 g. T6 U4 |
. k( B. G3 k. ~2 S' Y下面我们具体讲述一下这8个 API 的使用方法。在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。同样,在文章最后我们将给出这次讲解的所有源代码以及视频讲解。& V$ ?! p3 a# v& v, `6 ]& {  i& d
02 Runtime API 使用方法
/ f  ~* U, j* j9 D3 ?- F) w9 WRuntime API 的引用分为两个路径,分别是 ontology.interop.System.Runtime 和 ontology.interop.Ontology.Runtime。其中,Ontology 路径下包含了新增的 API。下列语句引用了这几个 API。
. T/ ^+ J& g7 e+ w1 ?from ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize
  ^" [% N1 h+ `" z; ]from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHas9 u% _* V. p" t! E
2.1  Notify API3 w; w" s6 Q) U8 }
Notify 函数将事件推送到全网。如下例子中函数 Notify 将会返回 “hello world” 十六进制字符串,并将其推送全网。
1 T& K* U' u4 S5 h8 f# ~from ontology.interop.System.Runtime import Notify
' J% h" B" n  Z* M3 q& d1 ?2 V" U3 ~# H+ Kdef demo():
' B# A1 u' l% N8 n5 J5 L. h    Notify("hello world")
0 y  f# R( c  {7 g小伙伴可以在 Logs 中查看:/ j7 l6 K9 d0 c6 y% ?# X6 _
1 O# |" g3 z% _: a7 K5 Y
2.2  GetTime API
9 @/ c* q. M  ~: jGetTime 函数返回当前时间戳,即返回调用该函数的 Unix 时间,其单位是秒。* ~4 n& c2 C* y8 S3 g
from ontology.interop.System.Runtime import GetTime2 L0 l, P7 E) U" M9 {/ u
def demo():
: F2 }9 x- d% V3 y" ^% k+ y    time=GetTime()7 f8 B# V0 F  H1 ]5 T1 h: o9 h
    return time # 返回时间戳, 单位是秒
$ `! ^. `* h3 n0 B; z/ j. x2.3  GetCurrentBlockHash API" ^0 z# d9 ~, y, \% h+ B
GetCurrentBlockHash 函数返回当前区块的哈希值。
' n# S1 Z: x  Q+ {, d+ Q8 [1 U, Afrom ontology.interop.Ontology.Runtime import GetCurrentBlockHash
1 F2 @5 C" n  ^4 K; ~5 Ndef demo():5 M! g3 B- h: Y
    block_hash = GetCurrentBlockHash()
/ f. F# X  y3 E) h5 H    return block_hash
" Y/ F; H3 }8 e/ }- [2.4  Serialize 和 Deserialize
2 }3 ^4 E: }( G: \% G这是一对序列化和反序列化函数。其中,Serialize 函数将一个对象序列化成 byte array 对象,而 Deserialize 函数将 byte array 反序列化成原先对象。下面的代码片段实现了将传入参数序列化并存入合约的持久化存储中,同时也实现了从合约的持久化存储中取出数据并把它进行反序列化。9 W0 b/ Q3 B; z
from ontology.interop.System.Runtime import Notify, Serialize, Deserialize, D3 _+ e. B: K2 N$ @5 ?- R+ E+ s
from ontology.interop.System.Storage import Put, Get, GetContext, l3 Q+ B% u1 u6 \
def Main(operation, args):( m( [: o- T; o" K$ a) ~: q; V( l
    if operation == 'serialize_to_bytearray':
" E" ?) V$ j* S! ]5 q; w! d        data = args[0]
6 x3 W3 k/ ^- ^        return serialize_to_bytearray(data)
! H4 T' D9 x+ l    if operation == 'deserialize_from_bytearray':) r" c, \- c8 {1 ]) K4 X4 G2 Q
        key = args[0]$ q& G6 b( R& u1 Y% X: X
        return deserialize_from_bytearray(key)
, Q# C( ^7 x7 }. h3 {" g' }    return False: P) P" d6 a- Z7 e
def serialize_to_bytearray(data):
/ r( `! e4 f" g3 i9 n9 A8 q1 z    sc = GetContext()
8 q+ e6 @5 b: k! y2 n6 k0 ?* p    key = "1"5 Q+ F2 N0 G+ j; N; }5 {, _. W& f
    byte_data = Serialize(data) # 序列化传入的参数
$ E# d2 r: {: c7 _* ~7 h4 d    Put(sc, key, byte_data) # 将序列化后的数据存入区块链- ]1 i% d3 _3 X/ e! w0 ?
def deserialize_from_bytearray(key):% ^' y% Z1 d2 ^( R1 c
    sc = GetContext()4 L2 Y) P0 s) c9 X
    byte_data = Get(sc, key) # 按key从区块链中取出数据( T! k7 R2 N& H2 F2 b
    data = Deserialize(byte_data) # 将bytearray数据反序列化成原先类型数据- n6 {$ V8 m2 i5 _1 n
    return data
8 G4 q6 h. U' A2.5 Base58ToAddress & AdressToBase58
% ?1 L5 \2 x, |这是一对地址转换函数。其中,Base58ToAddress 函数将 base58 编码的地址转成 byte array 形式地址,而 AddressToBase58 则将 byte array 形式地址转成 base58 编码的地址。
/ @* v( F; x* Y& [from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58
7 T( g% c, [1 c# k& r' _6 z9 `def demo():
1 S: q* d: }# G- |. Z* U    base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn"0 R+ {: K# z0 d: ^1 C
    addr=Base58ToAddress(base58_addr) # 将 base58 地址转成 bytearray形式地址$ X/ d3 X, a/ X4 R
    Notify(addr)3 T# E& h$ J) h- O! c8 T
    base58_addr=AddressToBase58(addr) #将bytearray地址转为base58地址
- t3 }* K8 m9 O% k3 P+ n& B    Notify(base58_addr)
8 r: L, D+ k6 B* M2.6  CheckWitness. v0 d1 i3 u* R% S( b2 c% t0 f
CheckWitness(fromAcct) 函数有两个功能:- X0 g& e7 B2 x: m" m) p6 D; W8 B* q
验证当前的函数调用者是不是 fromAcct 。若是(即签名验证通过),则函数返回通过。
) F8 {$ D4 }* _8 W6 ]3 ~; R检查当前函数调用者是不是一个合约。若是合约,且是从该合约发起去执行函数,则验证通过。即,验证 fromAcct 是不是 GetCallingScriptHash() 的返回值。其中,GetCallingScriptHash()  函数可以得到调用当前智能合约的合约哈希值。
/ e4 W% S) ]1 j9 S1 pGetCallingScriptHash():: d" C. [3 @$ f
https://github.com/ontio/ontology-python-compiler/blob/master/ontology/interop/System/ExecutionEngine.py* q' P* ?: l1 b  ?( o
from ontology.interop.System.Runtime import CheckWitness& X. _& a: @3 T; a4 D+ Z  g- r
from ontology.interop.Ontology.Runtime import Base58ToAddress
1 D9 h6 ^! l' J- c% idef demo():" ~" K0 X1 `1 b! p
    addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z")+ n, U7 B3 ~) _2 R2 l5 Z4 x
    res=CheckWitness(addr) # 验证调用者地址是否为AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z, X/ F2 G5 _# ^& D; ^( j
    return res
1 `0 f/ q, c" {! b03 总结9 _% }8 s  w( s7 Q; U% P9 q
本次技术视点中我们介绍了本体区块链的 Runtime API,该类 API 在本体 Python 智能合约中用处十分巨大。在下一期技术视点中,我们将介绍 Native API,探讨如何在本体智能合约中进行转账等操作。本期讲述的所有语法部分我们提供了中文视频,小伙伴们可以观看学习。
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    0

  • 主题

    2