Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

wk国际特价机票
150 0 0
01 导语
0 n$ T" Z6 F; ]  J+ h0 x上一期我们介绍了本体智能合约存储 API,相信很多小伙伴都明白了在本体上进行 Python 智能合约开发时如何调用相关 API 进行持久化存储。本期我们讨论如何使用 Runtime API(合约执行 API)。Runtime API 共有8个相关的 API,提供了合约执行时常用的接口,帮助开发者获取数据、转换数据以及验证数据。这8个 API 的简单描述如下:  B  u  m( x/ H

+ E& a  s. u  b9 }下面我们具体讲述一下这8个 API 的使用方法。在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。同样,在文章最后我们将给出这次讲解的所有源代码以及视频讲解。, L. }* o2 [- P4 H
02 Runtime API 使用方法
* C  F( u2 v; A6 RRuntime API 的引用分为两个路径,分别是 ontology.interop.System.Runtime 和 ontology.interop.Ontology.Runtime。其中,Ontology 路径下包含了新增的 API。下列语句引用了这几个 API。; j5 |3 j3 z6 r
from ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize0 Z7 T; e0 ]8 }& Q/ d) v' K
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHas/ Z9 C8 G4 v$ F( \" V" {$ m' q+ J
2.1  Notify API0 g# g7 v( U3 P
Notify 函数将事件推送到全网。如下例子中函数 Notify 将会返回 “hello world” 十六进制字符串,并将其推送全网。; ?) B) R+ Q" f* V) ~
from ontology.interop.System.Runtime import Notify
0 r; T$ R5 b# \* f" xdef demo():) O( L, [# V9 M7 U
    Notify("hello world")
6 W. n- O- j# A" |6 U4 u: S小伙伴可以在 Logs 中查看:
& \9 b% K' K- S  i* e: R3 f/ l& d  y2 {! q  z' b2 R& U
2.2  GetTime API
( K0 N8 s! r2 e4 w0 w' ~5 P/ N# @* DGetTime 函数返回当前时间戳,即返回调用该函数的 Unix 时间,其单位是秒。9 R4 l' W/ ]5 z9 u% `& J
from ontology.interop.System.Runtime import GetTime
+ W5 ^# S. `$ s" f% q2 Rdef demo():
* ?, _/ O: N. y3 d    time=GetTime()
: |) \6 f1 n; G" r: W, t    return time # 返回时间戳, 单位是秒- n2 q2 f( B) V) s
2.3  GetCurrentBlockHash API4 N, r% G' U# L4 h) V$ X
GetCurrentBlockHash 函数返回当前区块的哈希值。7 A/ f+ [. Q7 t1 H
from ontology.interop.Ontology.Runtime import GetCurrentBlockHash  T5 [7 g1 B6 g
def demo():
, [6 A4 }% Z- x4 R. P    block_hash = GetCurrentBlockHash()
1 t  G2 b% i/ ], M    return block_hash) d& s% e! |# k9 W1 ?8 j; s  m
2.4  Serialize 和 Deserialize& w+ _- q, `) m  s2 u5 R5 e
这是一对序列化和反序列化函数。其中,Serialize 函数将一个对象序列化成 byte array 对象,而 Deserialize 函数将 byte array 反序列化成原先对象。下面的代码片段实现了将传入参数序列化并存入合约的持久化存储中,同时也实现了从合约的持久化存储中取出数据并把它进行反序列化。* D. H% q/ D; }* a
from ontology.interop.System.Runtime import Notify, Serialize, Deserialize8 W+ l" A+ u/ ^# g
from ontology.interop.System.Storage import Put, Get, GetContext
7 O( h! W5 Z- Kdef Main(operation, args):( ]" ^0 g0 |, C: }
    if operation == 'serialize_to_bytearray':; _! y( b0 ~; O5 o: U2 ~
        data = args[0]
2 C  ~, ~3 z8 @( L$ N/ F        return serialize_to_bytearray(data)
* {! _4 L9 f8 t  X5 A* z5 G    if operation == 'deserialize_from_bytearray':
( X. B/ [3 r0 E1 W        key = args[0]" _( n, ~. f. T1 U; U
        return deserialize_from_bytearray(key)) I) w  A! `' `; D3 d8 E5 i
    return False
4 j8 k# H$ ?& x2 X, N' @3 ^def serialize_to_bytearray(data):" O) O& G4 `  g% N/ C3 ~
    sc = GetContext(); m# n* c. a6 T* N
    key = "1"
9 Z( ^. r8 I1 ]+ }; O3 s% s    byte_data = Serialize(data) # 序列化传入的参数2 q4 H& k) ?2 q4 H
    Put(sc, key, byte_data) # 将序列化后的数据存入区块链4 C. h5 d5 C& j: n. y. O  r$ w' _/ |
def deserialize_from_bytearray(key):
/ _! s! O% O: }6 k' C    sc = GetContext(); w+ x. p  [2 T; M8 U0 h
    byte_data = Get(sc, key) # 按key从区块链中取出数据
: B1 S0 Z8 R( q8 B1 o    data = Deserialize(byte_data) # 将bytearray数据反序列化成原先类型数据
1 P+ t* O# u1 C1 Q7 E    return data
+ W+ |% e. _. z/ z& W8 N2.5 Base58ToAddress & AdressToBase58
; J) W9 r. O( A0 @这是一对地址转换函数。其中,Base58ToAddress 函数将 base58 编码的地址转成 byte array 形式地址,而 AddressToBase58 则将 byte array 形式地址转成 base58 编码的地址。0 f+ Q' q: I( [9 S
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase581 A5 k# T7 M5 c' I2 ^/ A
def demo():
: v! @: F2 n. M( i    base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn"
6 W: _, y0 J) M+ ^    addr=Base58ToAddress(base58_addr) # 将 base58 地址转成 bytearray形式地址
0 w! @: `  a8 G! q# s6 \: e7 x    Notify(addr). H. W0 ]8 z! W; s6 k2 M- {5 Q
    base58_addr=AddressToBase58(addr) #将bytearray地址转为base58地址
7 T# A* t: w0 H    Notify(base58_addr)
( I) I7 |# r% A# L$ Y2 f8 G1 K, y2.6  CheckWitness* F1 I. [% }! s$ Q$ y0 i* a
CheckWitness(fromAcct) 函数有两个功能:( W+ k; y1 U5 K; s7 f# M
验证当前的函数调用者是不是 fromAcct 。若是(即签名验证通过),则函数返回通过。
! L2 e  |# D$ {) a$ A检查当前函数调用者是不是一个合约。若是合约,且是从该合约发起去执行函数,则验证通过。即,验证 fromAcct 是不是 GetCallingScriptHash() 的返回值。其中,GetCallingScriptHash()  函数可以得到调用当前智能合约的合约哈希值。+ w' {3 X& [8 v- ?' Z( u
GetCallingScriptHash():
0 B+ {' R8 b1 G4 ~/ yhttps://github.com/ontio/ontology-python-compiler/blob/master/ontology/interop/System/ExecutionEngine.py
/ i; @2 C6 ~+ ufrom ontology.interop.System.Runtime import CheckWitness
2 t- C% j) V0 y2 O% {9 x: Nfrom ontology.interop.Ontology.Runtime import Base58ToAddress
- S! l' _3 F6 l) w6 P& A% ydef demo():
- T$ g1 C, T9 I0 H/ n    addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z"), N1 ?( ^- k9 V6 |2 z4 H6 w- E$ B$ u
    res=CheckWitness(addr) # 验证调用者地址是否为AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z
0 w, n4 v8 U0 X5 d    return res
, @6 P, z5 C2 l: T" d1 \- o03 总结- C/ |' M3 w' t) N
本次技术视点中我们介绍了本体区块链的 Runtime API,该类 API 在本体 Python 智能合约中用处十分巨大。在下一期技术视点中,我们将介绍 Native API,探讨如何在本体智能合约中进行转账等操作。本期讲述的所有语法部分我们提供了中文视频,小伙伴们可以观看学习。
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    0

  • 主题

    2