Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

wk国际特价机票
177 0 0
01 导语) v, y" Y9 S5 l  A* ]
上一期我们介绍了本体智能合约存储 API,相信很多小伙伴都明白了在本体上进行 Python 智能合约开发时如何调用相关 API 进行持久化存储。本期我们讨论如何使用 Runtime API(合约执行 API)。Runtime API 共有8个相关的 API,提供了合约执行时常用的接口,帮助开发者获取数据、转换数据以及验证数据。这8个 API 的简单描述如下:- }4 i5 Q# m+ e) I4 d. b

8 t% q  l( [- p8 y3 e下面我们具体讲述一下这8个 API 的使用方法。在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。同样,在文章最后我们将给出这次讲解的所有源代码以及视频讲解。! b4 v# x# d4 e/ M. x
02 Runtime API 使用方法
$ U7 q+ t5 f: ]* ]$ PRuntime API 的引用分为两个路径,分别是 ontology.interop.System.Runtime 和 ontology.interop.Ontology.Runtime。其中,Ontology 路径下包含了新增的 API。下列语句引用了这几个 API。
" Y( l) A2 f- {6 yfrom ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize
9 ?0 \2 V+ J4 ]from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHas1 k! P, x# G$ C9 Y. ^9 u
2.1  Notify API  s' a' Q7 D+ p1 `
Notify 函数将事件推送到全网。如下例子中函数 Notify 将会返回 “hello world” 十六进制字符串,并将其推送全网。+ N% j- W* U* V* R; O3 c
from ontology.interop.System.Runtime import Notify
" M, H: s! j+ o3 hdef demo():
& N1 r/ C7 X3 m. s& Q, V, k% y    Notify("hello world")3 Q& a  |7 A( ^* p
小伙伴可以在 Logs 中查看:
, ?- V* {- S0 ^5 |: v/ t, k& N, I- G/ }( D: g& k1 D  V
2.2  GetTime API
1 F. ?6 f+ w$ ~* G( xGetTime 函数返回当前时间戳,即返回调用该函数的 Unix 时间,其单位是秒。3 P! H# Z6 ?3 N, E, Z6 x1 d/ D
from ontology.interop.System.Runtime import GetTime
  e4 [5 ~/ G/ [def demo():
) b1 E* ?; v! I- K0 @4 [    time=GetTime()4 A3 a( d( W7 B3 p/ Q1 R: C) P2 Q4 W
    return time # 返回时间戳, 单位是秒
+ Q- u4 B. F3 S1 z  z2.3  GetCurrentBlockHash API- ~$ ^2 j: Q: h) a' ]3 s
GetCurrentBlockHash 函数返回当前区块的哈希值。
$ x+ V+ o1 v) O' d& lfrom ontology.interop.Ontology.Runtime import GetCurrentBlockHash
- Y8 Y5 f& Z8 l, x4 Ddef demo():
( E3 V2 l) D$ J: S& A/ q- R6 p    block_hash = GetCurrentBlockHash()
4 @; D# W4 \$ h    return block_hash  X+ S4 _, s  C
2.4  Serialize 和 Deserialize7 q( H, ~6 }; b2 q6 l; o% K
这是一对序列化和反序列化函数。其中,Serialize 函数将一个对象序列化成 byte array 对象,而 Deserialize 函数将 byte array 反序列化成原先对象。下面的代码片段实现了将传入参数序列化并存入合约的持久化存储中,同时也实现了从合约的持久化存储中取出数据并把它进行反序列化。3 d. d; f$ J) q0 E# O* C7 l
from ontology.interop.System.Runtime import Notify, Serialize, Deserialize5 u( F$ r* m& H* e( a* J
from ontology.interop.System.Storage import Put, Get, GetContext
' C  f+ Q# i0 ^def Main(operation, args):
+ g; I+ P3 V( f6 W' d7 p- N, N, H9 z    if operation == 'serialize_to_bytearray':! m* L0 A. h8 d: v1 I
        data = args[0]5 U! x. Z" G' N% D1 P% v
        return serialize_to_bytearray(data)
; L6 U- K9 b- S' `: V    if operation == 'deserialize_from_bytearray':
' g2 F! m; g' d        key = args[0]) P. `4 m! J9 R! Q5 V
        return deserialize_from_bytearray(key)
& S+ ?& n' f) _/ W% k: w! p    return False
! T3 F6 R& E( D3 Cdef serialize_to_bytearray(data):
& l( o% f% R1 K( V3 }    sc = GetContext()8 u4 L6 m3 u, V6 y
    key = "1"
! }; J8 i% f& x  |; p3 O    byte_data = Serialize(data) # 序列化传入的参数. A7 z4 M% o$ l9 H
    Put(sc, key, byte_data) # 将序列化后的数据存入区块链
4 L; ^8 r6 E3 [; V0 Z8 Q* ^def deserialize_from_bytearray(key):
' Y! L$ ^! R) u% ?( c! e    sc = GetContext()) N" [" c& s7 J
    byte_data = Get(sc, key) # 按key从区块链中取出数据
9 e& a  O! F( `/ B' \5 R    data = Deserialize(byte_data) # 将bytearray数据反序列化成原先类型数据
4 ^5 Y. M: ^2 s0 ~  K5 k0 H    return data! h8 D, i/ |5 S3 q  U! C
2.5 Base58ToAddress & AdressToBase58  l& j$ O# _; W. `1 m. l  Z
这是一对地址转换函数。其中,Base58ToAddress 函数将 base58 编码的地址转成 byte array 形式地址,而 AddressToBase58 则将 byte array 形式地址转成 base58 编码的地址。
4 A9 t! k. }# u1 Efrom ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58/ d1 F! T5 _4 h% e0 m4 f/ S
def demo():
; K. `6 U$ B9 k/ l7 f    base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn", ^6 I2 }) E6 E" Q8 Q! U% j
    addr=Base58ToAddress(base58_addr) # 将 base58 地址转成 bytearray形式地址! ?  p7 m( c, b: Q7 l' Y- X: e
    Notify(addr)
) d" T" E5 p& u4 u    base58_addr=AddressToBase58(addr) #将bytearray地址转为base58地址# A0 y* r( t$ d2 B* h
    Notify(base58_addr)6 y1 d+ C* x% p( X! {
2.6  CheckWitness4 [7 d+ H" z; {$ w' d1 u
CheckWitness(fromAcct) 函数有两个功能:( [* s- f6 `2 F4 B' h$ @! a0 s
验证当前的函数调用者是不是 fromAcct 。若是(即签名验证通过),则函数返回通过。
, C& j# j) {* P1 Y6 S0 m& }% N! ~检查当前函数调用者是不是一个合约。若是合约,且是从该合约发起去执行函数,则验证通过。即,验证 fromAcct 是不是 GetCallingScriptHash() 的返回值。其中,GetCallingScriptHash()  函数可以得到调用当前智能合约的合约哈希值。: D: K$ B. `" _5 h
GetCallingScriptHash():
8 c/ V% l5 k& Vhttps://github.com/ontio/ontology-python-compiler/blob/master/ontology/interop/System/ExecutionEngine.py% ~  C# \0 O% r0 X
from ontology.interop.System.Runtime import CheckWitness
: x: v& X. q0 K7 g2 _from ontology.interop.Ontology.Runtime import Base58ToAddress
% r0 l2 D6 f2 Q. K6 adef demo():- X( u. J1 R8 E& ~7 ~* h
    addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z")
+ [! M( M$ u! `2 D: |6 y" \    res=CheckWitness(addr) # 验证调用者地址是否为AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z
9 H+ ~, P' M" I0 N" I, [! e, k# e    return res
& p% I2 K* g  `/ X& Y5 g& x03 总结% }! C4 o- H: g9 v* _$ k
本次技术视点中我们介绍了本体区块链的 Runtime API,该类 API 在本体 Python 智能合约中用处十分巨大。在下一期技术视点中,我们将介绍 Native API,探讨如何在本体智能合约中进行转账等操作。本期讲述的所有语法部分我们提供了中文视频,小伙伴们可以观看学习。
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    0

  • 主题

    2