Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

wk国际特价机票
176 0 0
01 导语
0 j2 `7 `$ X; q3 k8 W4 b7 {上一期我们介绍了本体智能合约存储 API,相信很多小伙伴都明白了在本体上进行 Python 智能合约开发时如何调用相关 API 进行持久化存储。本期我们讨论如何使用 Runtime API(合约执行 API)。Runtime API 共有8个相关的 API,提供了合约执行时常用的接口,帮助开发者获取数据、转换数据以及验证数据。这8个 API 的简单描述如下:
+ C4 Y% O' J2 W  [+ b
, \; d. F3 S( ]* U: w下面我们具体讲述一下这8个 API 的使用方法。在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。同样,在文章最后我们将给出这次讲解的所有源代码以及视频讲解。- w: p' I$ K; T! {( Z3 v1 M
02 Runtime API 使用方法
! y: M5 T0 p6 Q! {, G) RRuntime API 的引用分为两个路径,分别是 ontology.interop.System.Runtime 和 ontology.interop.Ontology.Runtime。其中,Ontology 路径下包含了新增的 API。下列语句引用了这几个 API。/ L% ]1 m4 p$ M) e* V1 r" x9 D
from ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize
% @4 C1 J: t5 o' ?0 efrom ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHas
8 F6 W7 u' s/ f) |: d  [2.1  Notify API# I4 H, b3 B$ \: \2 n5 {, G
Notify 函数将事件推送到全网。如下例子中函数 Notify 将会返回 “hello world” 十六进制字符串,并将其推送全网。
4 o. {. x9 h. |+ M4 ofrom ontology.interop.System.Runtime import Notify1 k. m$ C( Q1 F1 ?, c' Z! @1 P
def demo():
9 I3 [) y1 M! P0 c& C    Notify("hello world")- Z+ g! r1 V" P9 q+ X
小伙伴可以在 Logs 中查看:
8 ~0 `4 J! g& Z" t4 F, T- y# o0 E" X; U$ G4 s0 d5 M
2.2  GetTime API
  ?) ?$ k# S1 w- v! V  LGetTime 函数返回当前时间戳,即返回调用该函数的 Unix 时间,其单位是秒。5 \( G/ C1 J# B% e( F. m
from ontology.interop.System.Runtime import GetTime- @( Z0 u; }# l  q% Z7 R. ?
def demo():
0 {8 P5 d6 U" x. O3 X% V    time=GetTime()
5 t% M# n3 ~. W. i    return time # 返回时间戳, 单位是秒
9 O- W3 {' X* _2.3  GetCurrentBlockHash API
, Z. k2 f+ B9 h" E! g8 @! pGetCurrentBlockHash 函数返回当前区块的哈希值。
- e  P0 f$ d$ W( \- l2 gfrom ontology.interop.Ontology.Runtime import GetCurrentBlockHash! _% }4 t6 B1 T
def demo():
4 _7 E* I! }* F. q5 g2 `    block_hash = GetCurrentBlockHash()/ M/ n" h9 S( I2 {4 u4 G
    return block_hash
! w3 c' a0 O$ D$ U5 Q0 q& L2.4  Serialize 和 Deserialize
. V* y: M5 _2 J- v这是一对序列化和反序列化函数。其中,Serialize 函数将一个对象序列化成 byte array 对象,而 Deserialize 函数将 byte array 反序列化成原先对象。下面的代码片段实现了将传入参数序列化并存入合约的持久化存储中,同时也实现了从合约的持久化存储中取出数据并把它进行反序列化。
" W# _/ ?+ e5 e8 Ifrom ontology.interop.System.Runtime import Notify, Serialize, Deserialize
  _2 b" y# \1 s/ c$ I  Rfrom ontology.interop.System.Storage import Put, Get, GetContext- A0 z: P) V# w8 y" G. w  ^7 Y5 o
def Main(operation, args):$ ]& S) _1 X4 P- R; @
    if operation == 'serialize_to_bytearray':( F( q. k* b# U/ D' W$ j% a
        data = args[0]
4 D- T- K8 N. O" @5 Z        return serialize_to_bytearray(data)
- E1 H: Y$ m! q    if operation == 'deserialize_from_bytearray':6 J- G" w% ^* _2 M. l
        key = args[0]
; C7 B% I+ d% ^1 [( n; ^8 z) s/ d& e/ e        return deserialize_from_bytearray(key)( F0 q( @# ]* I3 C# ^1 p, ^
    return False
& D4 v8 b  o6 [# q( b3 G* Vdef serialize_to_bytearray(data):. u4 Q  P2 C8 [' L4 m( v, {4 P
    sc = GetContext()
; ?' r5 p: D6 s# ^2 a    key = "1"
3 O7 g% X1 }. {% o" |6 v0 \    byte_data = Serialize(data) # 序列化传入的参数
. X5 T, l5 Z; x8 i    Put(sc, key, byte_data) # 将序列化后的数据存入区块链; x/ G& A$ Y! m' U( _; H/ F% b
def deserialize_from_bytearray(key):
0 Z1 }% A. q9 T. B; V) L    sc = GetContext()% W/ C/ n' q* a- h+ T8 f8 g
    byte_data = Get(sc, key) # 按key从区块链中取出数据
  J; x$ U9 T0 P! v% H; X) w    data = Deserialize(byte_data) # 将bytearray数据反序列化成原先类型数据
6 j& K/ {. H9 w# g1 _    return data% w* [9 Q1 R/ {8 ^( _) t' W5 P. n
2.5 Base58ToAddress & AdressToBase58
* p- }# V9 r- r2 [这是一对地址转换函数。其中,Base58ToAddress 函数将 base58 编码的地址转成 byte array 形式地址,而 AddressToBase58 则将 byte array 形式地址转成 base58 编码的地址。; ~4 v& Q, i( I$ B
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase585 \2 V" {* Z& I/ ?1 ~% @6 z% H
def demo():2 n) V# s4 o  b
    base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn"
& _: |  J0 D+ S: |7 q    addr=Base58ToAddress(base58_addr) # 将 base58 地址转成 bytearray形式地址( i% |! H) `3 u- j
    Notify(addr)
: G" g7 A) t$ m5 N% N+ r7 S6 f* n& L    base58_addr=AddressToBase58(addr) #将bytearray地址转为base58地址4 V% j/ V: I3 T! W  q
    Notify(base58_addr); f/ I; a0 r7 e/ q
2.6  CheckWitness
( r4 S6 D$ I( a! o! t3 oCheckWitness(fromAcct) 函数有两个功能:& ~' w/ o' w9 Q" g8 u' k
验证当前的函数调用者是不是 fromAcct 。若是(即签名验证通过),则函数返回通过。
0 M8 X( T7 b* Y1 }检查当前函数调用者是不是一个合约。若是合约,且是从该合约发起去执行函数,则验证通过。即,验证 fromAcct 是不是 GetCallingScriptHash() 的返回值。其中,GetCallingScriptHash()  函数可以得到调用当前智能合约的合约哈希值。
% W' ?. x4 T  p9 @GetCallingScriptHash():
5 k5 ]# L& K, W$ dhttps://github.com/ontio/ontology-python-compiler/blob/master/ontology/interop/System/ExecutionEngine.py
1 P$ L% {6 N* F. Gfrom ontology.interop.System.Runtime import CheckWitness8 X2 s* p; L  i2 s0 k+ y8 C
from ontology.interop.Ontology.Runtime import Base58ToAddress
. ]1 h2 Z& N+ wdef demo():" ]9 D. c2 x6 y, Z* {; K9 P
    addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z")
3 ]" D. L7 U9 a# J    res=CheckWitness(addr) # 验证调用者地址是否为AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z
$ ]$ Q. Z( ]5 h( k) q& F    return res
! W: p  x% s/ ?0 e% w" i- A03 总结
" S  X$ \- i; J  s/ f本次技术视点中我们介绍了本体区块链的 Runtime API,该类 API 在本体 Python 智能合约中用处十分巨大。在下一期技术视点中,我们将介绍 Native API,探讨如何在本体智能合约中进行转账等操作。本期讲述的所有语法部分我们提供了中文视频,小伙伴们可以观看学习。
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    0

  • 主题

    2