Python智能合约执行API知多少?
wk国际特价机票
发表于 2023-1-9 11:30:42
149
0
0
上一期我们介绍了本体智能合约存储 API,相信很多小伙伴都明白了在本体上进行 Python 智能合约开发时如何调用相关 API 进行持久化存储。本期我们讨论如何使用 Runtime API(合约执行 API)。Runtime API 共有8个相关的 API,提供了合约执行时常用的接口,帮助开发者获取数据、转换数据以及验证数据。这8个 API 的简单描述如下:5 J9 e9 X' _- a/ ^* O# J5 E7 s
3 k7 V+ v( W) A6 ~5 j! ]5 b' E
下面我们具体讲述一下这8个 API 的使用方法。在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。同样,在文章最后我们将给出这次讲解的所有源代码以及视频讲解。4 q/ M7 G, _8 Y- o
02 Runtime API 使用方法
Runtime API 的引用分为两个路径,分别是 ontology.interop.System.Runtime 和 ontology.interop.Ontology.Runtime。其中,Ontology 路径下包含了新增的 API。下列语句引用了这几个 API。
from ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize; h R$ g! G/ Q8 h& _! o
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHas
2.1 Notify API2 l l; i; A. m$ y
Notify 函数将事件推送到全网。如下例子中函数 Notify 将会返回 “hello world” 十六进制字符串,并将其推送全网。
from ontology.interop.System.Runtime import Notify
def demo():, n% X5 m5 L! T, S! n4 q" W# {
Notify("hello world")& G6 R, k* A L
小伙伴可以在 Logs 中查看:6 a* e% n8 c/ m& G
2.2 GetTime API
GetTime 函数返回当前时间戳,即返回调用该函数的 Unix 时间,其单位是秒。% g7 k" i1 `4 r$ D' n
from ontology.interop.System.Runtime import GetTime& U9 b& H; T# J0 X1 J
def demo():- K- i6 Q# P: X- S% f
time=GetTime()( D- ]. b d: v/ z. i: e. _. J
return time # 返回时间戳, 单位是秒9 P$ H; K6 ^! M' }0 w1 c
2.3 GetCurrentBlockHash API
GetCurrentBlockHash 函数返回当前区块的哈希值。( E1 m, u- G% A# T% K. ~
from ontology.interop.Ontology.Runtime import GetCurrentBlockHash7 O0 i2 n' W- a& Z0 V+ }4 H
def demo():
block_hash = GetCurrentBlockHash()2 \( V- H, P+ f$ V7 t( G' }, B
return block_hash
2.4 Serialize 和 Deserialize( d' b' ^& F4 G) ]% c
这是一对序列化和反序列化函数。其中,Serialize 函数将一个对象序列化成 byte array 对象,而 Deserialize 函数将 byte array 反序列化成原先对象。下面的代码片段实现了将传入参数序列化并存入合约的持久化存储中,同时也实现了从合约的持久化存储中取出数据并把它进行反序列化。
from ontology.interop.System.Runtime import Notify, Serialize, Deserialize& _* L& N# t) Z+ u3 z
from ontology.interop.System.Storage import Put, Get, GetContext
def Main(operation, args):* T3 m9 m( C! o+ M# z1 t
if operation == 'serialize_to_bytearray':
data = args[0]
return serialize_to_bytearray(data)
if operation == 'deserialize_from_bytearray':2 Y, |' L: S3 h0 t3 E
key = args[0]
return deserialize_from_bytearray(key)
return False
def serialize_to_bytearray(data):' R- G4 b9 I; r6 Z# a2 y2 w {8 f
sc = GetContext()
key = "1"
byte_data = Serialize(data) # 序列化传入的参数/ i5 q4 X/ I, g) ^# X
Put(sc, key, byte_data) # 将序列化后的数据存入区块链
def deserialize_from_bytearray(key):# l4 E2 x" y- ?% Z% r/ {+ L
sc = GetContext()
byte_data = Get(sc, key) # 按key从区块链中取出数据- Z+ f) h3 C7 n: A5 h" E9 N f+ `0 f
data = Deserialize(byte_data) # 将bytearray数据反序列化成原先类型数据
return data+ W# d& r6 d& C$ c3 u6 ^, p4 F
2.5 Base58ToAddress & AdressToBase584 j, k) _! s" b* o0 g
这是一对地址转换函数。其中,Base58ToAddress 函数将 base58 编码的地址转成 byte array 形式地址,而 AddressToBase58 则将 byte array 形式地址转成 base58 编码的地址。' Y+ m9 {# G- t/ i$ [% ?+ I
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58
def demo():
base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn"* [0 S S* \! [, d3 B @4 `
addr=Base58ToAddress(base58_addr) # 将 base58 地址转成 bytearray形式地址5 L9 m! z7 w+ c/ j- S6 @4 m" h
Notify(addr)
base58_addr=AddressToBase58(addr) #将bytearray地址转为base58地址7 ]' b) `9 Z( z" B, L% O; o% o) x
Notify(base58_addr)( A9 }8 p9 n, f# d# j
2.6 CheckWitness) M, D2 u6 }* }: I0 C3 R2 Y5 }
CheckWitness(fromAcct) 函数有两个功能:) }4 c$ A& r5 `- I
验证当前的函数调用者是不是 fromAcct 。若是(即签名验证通过),则函数返回通过。* @! ~& s# l& {0 g' s
检查当前函数调用者是不是一个合约。若是合约,且是从该合约发起去执行函数,则验证通过。即,验证 fromAcct 是不是 GetCallingScriptHash() 的返回值。其中,GetCallingScriptHash() 函数可以得到调用当前智能合约的合约哈希值。2 t" z& W v% H8 a
GetCallingScriptHash():
https://github.com/ontio/ontology-python-compiler/blob/master/ontology/interop/System/ExecutionEngine.py
from ontology.interop.System.Runtime import CheckWitness
from ontology.interop.Ontology.Runtime import Base58ToAddress
def demo():
addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z")4 T' d% H4 o: @' l, [" B
res=CheckWitness(addr) # 验证调用者地址是否为AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z
return res8 G, N2 Z# \) |5 K7 D
03 总结
本次技术视点中我们介绍了本体区块链的 Runtime API,该类 API 在本体 Python 智能合约中用处十分巨大。在下一期技术视点中,我们将介绍 Native API,探讨如何在本体智能合约中进行转账等操作。本期讲述的所有语法部分我们提供了中文视频,小伙伴们可以观看学习。
成为第一个吐槽的人