Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

wk国际特价机票
89 0 0
01 导语
9 o) N% B& Z7 A( V2 H; t5 H上一期我们介绍了本体智能合约存储 API,相信很多小伙伴都明白了在本体上进行 Python 智能合约开发时如何调用相关 API 进行持久化存储。本期我们讨论如何使用 Runtime API(合约执行 API)。Runtime API 共有8个相关的 API,提供了合约执行时常用的接口,帮助开发者获取数据、转换数据以及验证数据。这8个 API 的简单描述如下:
2 Q2 s' T& @2 C5 A0 T
7 w# b1 Z9 b* C8 R7 Z6 L下面我们具体讲述一下这8个 API 的使用方法。在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。同样,在文章最后我们将给出这次讲解的所有源代码以及视频讲解。9 T+ ?1 g2 f1 R% D
02 Runtime API 使用方法
# ?+ X8 ?( d# tRuntime API 的引用分为两个路径,分别是 ontology.interop.System.Runtime 和 ontology.interop.Ontology.Runtime。其中,Ontology 路径下包含了新增的 API。下列语句引用了这几个 API。* @- j6 Z  A* |5 ^/ i* e/ z
from ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize
' Y5 W( N0 ?) o$ N+ h: w1 T; Q- `8 Afrom ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHas; Z. }$ c$ p; u4 k* d1 r
2.1  Notify API, C6 t6 E9 p1 a3 C2 k
Notify 函数将事件推送到全网。如下例子中函数 Notify 将会返回 “hello world” 十六进制字符串,并将其推送全网。( y" ?( E8 h/ [7 V1 H
from ontology.interop.System.Runtime import Notify7 q% Z& M! A  F/ J
def demo():
7 C) w" `( u6 i$ W! F* G5 \1 D8 X" U    Notify("hello world")
0 K( q# B7 U. `小伙伴可以在 Logs 中查看:4 ?% H: R5 I+ N" I4 U# m9 p  i3 D

. y- [; n3 m% B3 u2.2  GetTime API
# A1 F+ V0 |2 }) qGetTime 函数返回当前时间戳,即返回调用该函数的 Unix 时间,其单位是秒。  B5 c4 W/ _0 D# I
from ontology.interop.System.Runtime import GetTime
; V( _/ W6 @: u* [0 Idef demo():2 W; h& @( `% ^1 N1 b2 A
    time=GetTime()
) d( A! S( B0 K$ y, e    return time # 返回时间戳, 单位是秒
- J# h  q0 h8 M, z# S' {( U! T/ ^" g. y9 N2.3  GetCurrentBlockHash API$ o2 ]7 }  a- n8 X. L( F" k3 s0 s9 T
GetCurrentBlockHash 函数返回当前区块的哈希值。( Z) r. J8 @, y) F+ q8 a* O# d
from ontology.interop.Ontology.Runtime import GetCurrentBlockHash
' N% r) K$ |* ~. {def demo():3 O6 j8 v  k" W9 L5 H) H3 X
    block_hash = GetCurrentBlockHash()1 N! W/ X3 p5 _+ R
    return block_hash( V' j' k6 D: D& V# m$ h/ u! j
2.4  Serialize 和 Deserialize' T; D2 g% E% \* e
这是一对序列化和反序列化函数。其中,Serialize 函数将一个对象序列化成 byte array 对象,而 Deserialize 函数将 byte array 反序列化成原先对象。下面的代码片段实现了将传入参数序列化并存入合约的持久化存储中,同时也实现了从合约的持久化存储中取出数据并把它进行反序列化。
2 L; U7 U) ]) f3 _! P% @6 Ffrom ontology.interop.System.Runtime import Notify, Serialize, Deserialize6 o2 X  k& a5 f9 J
from ontology.interop.System.Storage import Put, Get, GetContext
9 f' f& ^0 M/ z! I" K9 ]5 m8 n% b  w$ fdef Main(operation, args):
# w  t& i/ n8 J1 `9 |! G- u9 i    if operation == 'serialize_to_bytearray':
7 P7 `9 A/ W0 F  y        data = args[0]& ], L; J3 T* o3 T9 y# [$ L- p( k
        return serialize_to_bytearray(data)
0 b" ]; R. w' ^2 }3 x1 [- J+ f$ q    if operation == 'deserialize_from_bytearray':6 |& q& N9 k! u
        key = args[0]. r1 W5 Q. H# D( `
        return deserialize_from_bytearray(key)% z$ `' l# Z, W. ]& A9 }: C
    return False  T4 e& W  I* z- M5 v# R+ ^
def serialize_to_bytearray(data):
% v/ `& s2 Z& M  t! k- |$ J. _    sc = GetContext()5 x# q' s. W6 m1 R  m4 E0 y' ?9 y+ s3 J. `
    key = "1"/ q1 [+ |0 o# o
    byte_data = Serialize(data) # 序列化传入的参数
! P& |8 W' R$ D9 k5 \8 S8 Z& U( X, e    Put(sc, key, byte_data) # 将序列化后的数据存入区块链/ e* O. j' f* c
def deserialize_from_bytearray(key):* z* x$ P/ d- d4 e6 B4 _4 I( |( U. b$ H
    sc = GetContext()
; v: S4 C6 s8 K2 i7 V, x' V. V    byte_data = Get(sc, key) # 按key从区块链中取出数据
7 T- f' o1 F$ Q0 w    data = Deserialize(byte_data) # 将bytearray数据反序列化成原先类型数据( [  T" V5 v9 r. j% [5 F' ?% \" C
    return data
) U% ~6 ]) C  Z, J2.5 Base58ToAddress & AdressToBase583 U9 @  v4 G) P3 C) }+ i4 L  ?' d
这是一对地址转换函数。其中,Base58ToAddress 函数将 base58 编码的地址转成 byte array 形式地址,而 AddressToBase58 则将 byte array 形式地址转成 base58 编码的地址。
* T" ^, a4 t! X4 Z. ^, l/ pfrom ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58
: h# w( p# C; y  y9 E0 L2 fdef demo():; X( @. f9 @- u' O5 D* {# T
    base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn"
" t! @5 y1 D1 F7 \7 W# M/ Z& N; x    addr=Base58ToAddress(base58_addr) # 将 base58 地址转成 bytearray形式地址! h: ?% G- f8 G, Q/ g% A0 E4 X
    Notify(addr)
$ z! [# m  h: r3 I/ v( }' a+ {5 V    base58_addr=AddressToBase58(addr) #将bytearray地址转为base58地址
7 d0 z# i* }$ v( F+ E* l* L% |6 F" A    Notify(base58_addr)
% k3 C. u- G/ n+ ^0 I& @8 [2.6  CheckWitness
: r8 V7 }; Y2 u0 ~, M9 _CheckWitness(fromAcct) 函数有两个功能:
8 ^& C+ n6 W# N  V% R' y验证当前的函数调用者是不是 fromAcct 。若是(即签名验证通过),则函数返回通过。
+ q# A3 p  z: @检查当前函数调用者是不是一个合约。若是合约,且是从该合约发起去执行函数,则验证通过。即,验证 fromAcct 是不是 GetCallingScriptHash() 的返回值。其中,GetCallingScriptHash()  函数可以得到调用当前智能合约的合约哈希值。
- `* N( U' X# p  ?$ L- RGetCallingScriptHash():
/ I, h  I. l+ L# R! Whttps://github.com/ontio/ontology-python-compiler/blob/master/ontology/interop/System/ExecutionEngine.py1 B1 A# A# h, q: k" h
from ontology.interop.System.Runtime import CheckWitness
' {& a! K3 Z! C4 F/ @2 l+ a" Xfrom ontology.interop.Ontology.Runtime import Base58ToAddress, I5 h% D0 ~$ X/ [" P
def demo():$ y0 P; H6 [( B7 U8 o. D
    addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z")! P0 b& x7 T: U# M" I* H
    res=CheckWitness(addr) # 验证调用者地址是否为AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z% ]1 `9 \& P- i: ?
    return res! c" T6 Z. ~. n$ r- `
03 总结
5 p3 ~. g1 ^# l* b$ C) A. X本次技术视点中我们介绍了本体区块链的 Runtime API,该类 API 在本体 Python 智能合约中用处十分巨大。在下一期技术视点中,我们将介绍 Native API,探讨如何在本体智能合约中进行转账等操作。本期讲述的所有语法部分我们提供了中文视频,小伙伴们可以观看学习。
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    0

  • 主题

    2