
引言
之前,我在
第1部分:区块链和区块API和
第2部分:存储API现在,当您有了关于在本体论上开发Python智能合约时如何调用相关API进行持久存储的想法时,让我们继续
运行时API (合约执行API)。 运行时API具有8个相关的API,这些API提供了用于合同执行的通用接口,并帮助开发人员获取,转换和验证数据。 以下是这8种API的简要说明:

让我们仔细看看如何使用这8个API。 在此之前,您可以在本体智能合约开发工具
SmartX中创建一个新合约,并按照以下说明进行操作。 像往常一样,在文章末尾,我将提供到源代码的链接。
如何使用运行时API
有2条导入运行时API的路径,
ontology.interop.System.Runtime和
ontology.interop.Ontology.Runtime 。 本体路径包含新添加的API。 以下几行将导入这些API。
from ontology.interop.System.Runtime import GetTime, CheckWitness, Log, Notify, Serialize, Deserialize from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHash
通知API
通知功能将事件广播到整个网络。 在以下示例中,
Notify函数将返回“ hello world”十六进制字符串,并将其广播到整个网络。
from ontology.interop.System.Runtime import Notify def demo(): Notify("hello world")
您可以在
日志中查看它:

GetTime API
GetTime函数返回当前时间戳,该时间戳返回调用该函数的Unix时间。 单位是第二。
from ontology.interop.System.Runtime import GetTime def demo(): time=GetTime() return time
GetCurrentBlockHash API
GetCurrentBlockHash函数返回当前块的哈希。
from ontology.interop.Ontology.Runtime import GetCurrentBlockHash def demo(): block_hash = GetCurrentBlockHash() return block_hash
序列化和反序列化
这是一对序列化和反序列化功能。
Serialize函数将一个对象
序列化为字节数组对象,而
Deserialize函数将字节数组反序列化为原始对象。 以下代码示例实现了传入参数的序列化,并将其存储在协定的持久性存储中。 它还从合同的持久性存储中提取数据并反序列化。
from ontology.interop.System.Runtime import GetTime, CheckWitness, Log, Notify, Serialize, Deserialize from ontology.interop.System.Storage import Put, Get, GetContext def Main(operation, args): if operation == 'serialize_to_bytearray': data = args[0] return serialize_to_bytearray(data) if operation == 'deserialize_from_bytearray': key = args[0] return deserialize_from_bytearray(key) return False def serialize_to_bytearray(data): sc = GetContext() key = "1" byte_data = Serialize(data) Put(sc, key, byte_data) def deserialize_from_bytearray(key): sc = GetContext() byte_data = Get(sc, key) data = Deserialize(byte_data) return data
Base58ToAddress和AddressToBase58
这是一对地址转换功能。
Base58ToAddress函数将base58编码的地址转换为字节数组形式的地址,AddressToBase58将字节阵列的地址转换为base58编码地址。
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58 def demo(): base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn" addr=Base58ToAddress(base58_addr) Log(addr) base58_addr=AddressToBase58(addr) Log(base58_addr)
证人
CheckWitness(fromAcct)功能具有两个功能:
- 验证当前函数调用者是否是fromAcct。 如果是(即通过了签名验证),则函数返回。
- 检查当前函数调用者是否为合同。 如果它是合同,并且从合同执行了该功能,那么将通过验证。 即,验证fromAcct是否为GetCallingScriptHash()的返回值。 GetCallingScriptHash()函数可以获取当前智能合约的合约哈希值。
GetCallingScriptHash():
本体Python编译器
from ontology.interop.System.Runtime import CheckWitness from ontology.interop.Ontology.Runtime import Base58ToAddress def demo(): addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z") res=CheckWitness(addr) return res
在
此处找到完整的教程。
总结
在本文中,我介绍了Ontology区块链的
Runtime API ,这在Ontology Python智能合约中非常重要。 在下一篇文章中,我们将查看
Native API,以探索如何在Ontology智能合约中转移资产。
这是先前在Ontology Medium博客上发布的官方教程
您是开发人员吗? 确保您已加入
Discord上的我们的技术社区。 另外,请访问我们网站上的
开发人员中心,在那里您可以找到开发人员工具,文档等。
在其他地方找到本体
本体网站GitHub /
不和谐电报
英语 /
俄语Twitter /
Reddit