Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

wk国际特价机票
148 0 0
01 导语# G! Z8 R! x1 z- [6 |
上一期我们介绍了本体智能合约存储 API,相信很多小伙伴都明白了在本体上进行 Python 智能合约开发时如何调用相关 API 进行持久化存储。本期我们讨论如何使用 Runtime API(合约执行 API)。Runtime API 共有8个相关的 API,提供了合约执行时常用的接口,帮助开发者获取数据、转换数据以及验证数据。这8个 API 的简单描述如下:
7 {: R( p5 q" H9 U+ x; a7 }$ p8 t# T" F4 A
下面我们具体讲述一下这8个 API 的使用方法。在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。同样,在文章最后我们将给出这次讲解的所有源代码以及视频讲解。( Y  g# L0 Z2 z% \
02 Runtime API 使用方法, c  }7 C( u3 w% [% I
Runtime API 的引用分为两个路径,分别是 ontology.interop.System.Runtime 和 ontology.interop.Ontology.Runtime。其中,Ontology 路径下包含了新增的 API。下列语句引用了这几个 API。1 ?( n2 c9 n$ F$ g8 A7 u
from ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize8 u( U8 N: o" n% w. s7 e
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHas
1 {7 `9 v2 f& c! U" e3 [2.1  Notify API
, i3 u! U! C1 ]. p- g9 @Notify 函数将事件推送到全网。如下例子中函数 Notify 将会返回 “hello world” 十六进制字符串,并将其推送全网。8 r6 `* |3 L' Q6 L' n0 T# m
from ontology.interop.System.Runtime import Notify2 S0 d+ u9 H  z( z( n% O9 w- M; U
def demo():# {3 D' W% z) \  X' E( f
    Notify("hello world")
  [) c3 Z6 v6 u! v/ |7 s小伙伴可以在 Logs 中查看:' y; M9 e, `8 w3 Q7 }! F- ]7 K
, k( w5 D) q& r8 Q( N% }/ c) t0 U
2.2  GetTime API
# s4 c$ V* \% ^% jGetTime 函数返回当前时间戳,即返回调用该函数的 Unix 时间,其单位是秒。
( ?6 w7 b; T7 i" l) x& j6 `3 o+ }" Kfrom ontology.interop.System.Runtime import GetTime2 O, d! `& I$ U+ u6 H+ y. d
def demo():
1 U' g2 Z, Y' `- v! a4 ^3 r# L2 I% Q$ A    time=GetTime(); `& z5 ~' G# [; Y
    return time # 返回时间戳, 单位是秒
3 b3 H0 g  O: v0 i- K4 B2.3  GetCurrentBlockHash API
0 s$ v6 v9 g7 w. q* RGetCurrentBlockHash 函数返回当前区块的哈希值。, C% j/ X" S1 k& d( H$ Y1 i
from ontology.interop.Ontology.Runtime import GetCurrentBlockHash+ Z. N+ x# U7 b7 `
def demo():" \5 H/ K" t! K1 M: H! ]
    block_hash = GetCurrentBlockHash()+ E, Y! J9 G7 `  m4 u4 A
    return block_hash. G6 V7 w( n; r$ {2 x8 x8 Z
2.4  Serialize 和 Deserialize/ Z& P1 M- u) W2 k9 x! j9 j
这是一对序列化和反序列化函数。其中,Serialize 函数将一个对象序列化成 byte array 对象,而 Deserialize 函数将 byte array 反序列化成原先对象。下面的代码片段实现了将传入参数序列化并存入合约的持久化存储中,同时也实现了从合约的持久化存储中取出数据并把它进行反序列化。
5 A3 ?" f8 o; c  y2 ifrom ontology.interop.System.Runtime import Notify, Serialize, Deserialize
0 Q7 O' Q+ @3 v9 A2 B- L6 Wfrom ontology.interop.System.Storage import Put, Get, GetContext
1 w  \3 G2 h9 Gdef Main(operation, args):- S- D% p/ Z# F. K) i( D* w1 H
    if operation == 'serialize_to_bytearray':
4 K$ D/ P; Z+ \5 e2 O        data = args[0]
0 }! A& l+ G) f9 n        return serialize_to_bytearray(data)" D6 G7 E2 t( ^$ c- L
    if operation == 'deserialize_from_bytearray':
/ ~: J% s7 N, W9 r8 s7 X        key = args[0]" V3 s0 U3 R5 ~, u5 i+ x! S, h$ F
        return deserialize_from_bytearray(key)
& T5 ?# Q$ J, j( d    return False
7 S# D* K) N4 \2 t5 Y+ Tdef serialize_to_bytearray(data):
' Y8 z/ |# e* ?    sc = GetContext()5 V$ V4 ], s$ S/ h' k8 V6 Y& ]
    key = "1"0 d8 V2 x& Z( L/ {- m
    byte_data = Serialize(data) # 序列化传入的参数7 g$ l! W- h& N! r- m8 D  K
    Put(sc, key, byte_data) # 将序列化后的数据存入区块链3 U# r. ^( Y2 R
def deserialize_from_bytearray(key):, W( b# F3 l# t) M- q
    sc = GetContext()
( ^0 q. l: t5 b; F5 `' V    byte_data = Get(sc, key) # 按key从区块链中取出数据6 a( V! M1 ]4 O% w
    data = Deserialize(byte_data) # 将bytearray数据反序列化成原先类型数据
% u& J0 R) {1 w; U9 w) i' M    return data
5 c& a& M8 k4 p3 R2 d' X5 f+ ?2.5 Base58ToAddress & AdressToBase58. {% ]7 d$ J  U% F' v! D3 `
这是一对地址转换函数。其中,Base58ToAddress 函数将 base58 编码的地址转成 byte array 形式地址,而 AddressToBase58 则将 byte array 形式地址转成 base58 编码的地址。
- O; \0 {. j% l- Y  i6 _7 Nfrom ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58
0 U4 n4 a: e/ Q' odef demo():* @5 h1 R/ |! x0 M
    base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn"6 A5 v9 @9 k- R& t2 v8 c' \
    addr=Base58ToAddress(base58_addr) # 将 base58 地址转成 bytearray形式地址+ G4 i0 R6 Q* M- X, m
    Notify(addr)4 u. {) j: k8 u' |2 d: f2 t2 f
    base58_addr=AddressToBase58(addr) #将bytearray地址转为base58地址
6 G9 r( l2 R8 r1 Z    Notify(base58_addr)% D3 x, O7 y6 V. O. }+ M2 i
2.6  CheckWitness% G; {" m# c+ f  `2 O0 f0 ~
CheckWitness(fromAcct) 函数有两个功能:
. l8 C2 H) f( r/ R& B3 n验证当前的函数调用者是不是 fromAcct 。若是(即签名验证通过),则函数返回通过。
9 D* F7 m) [% G* f' N) E检查当前函数调用者是不是一个合约。若是合约,且是从该合约发起去执行函数,则验证通过。即,验证 fromAcct 是不是 GetCallingScriptHash() 的返回值。其中,GetCallingScriptHash()  函数可以得到调用当前智能合约的合约哈希值。" ^+ h5 V9 o. {3 `8 u) @
GetCallingScriptHash():
$ x# q8 [. K% }https://github.com/ontio/ontology-python-compiler/blob/master/ontology/interop/System/ExecutionEngine.py6 E$ u, \4 a; I) B1 n) o" A* z+ n
from ontology.interop.System.Runtime import CheckWitness. [3 k+ U/ R4 y+ t; D) r3 ^
from ontology.interop.Ontology.Runtime import Base58ToAddress
+ @7 b! d" Q$ k2 I: H$ e: ^3 ddef demo():* i: B6 m% J6 h6 N
    addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z")
# E3 v+ t- D1 p0 F. G6 e    res=CheckWitness(addr) # 验证调用者地址是否为AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z$ X9 A) H8 A# K8 v
    return res
! z% v; J6 U4 V! d9 k03 总结
: w. w6 `' C! t# q本次技术视点中我们介绍了本体区块链的 Runtime API,该类 API 在本体 Python 智能合约中用处十分巨大。在下一期技术视点中,我们将介绍 Native API,探讨如何在本体智能合约中进行转账等操作。本期讲述的所有语法部分我们提供了中文视频,小伙伴们可以观看学习。
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    0

  • 主题

    2