本体技术视点 | Python智能合约执行API知多少?

2019-09-10 19:35 栏目:经验之谈 来源: 查看()
01铅

在上一期中,我们介绍了Ontology Smart Contract Storage API。我相信我的很多朋友都知道在本体上开发Python智能合约时如何调用相关API来持久存储。在本期中,我们将讨论如何使用Runtime API(Contract Execution API)。 Runtime API有八个相关的API,为合同执行提供通用接口,帮助开发人员获取数据,转换数据和验证数据。这8个API的简要说明如下:

本体技术视点 | Python智能合约执行API知多少?

让我们仔细看看如何使用这8个API。在此之前,小型合作伙伴可以在本体智能合约开发工具SmartX中创建新合同,关注我们。同样,在本文的最后,我们将为您提供本教程的所有源代码和视频说明。 2.运行时API使用情况 对Runtime API的引用分为两个路径:ontology.interop.System.Runtime和ontology.interop.Ontology.Runtime。其中,Ontology路径包含一个新的API。以下语句引用了这些API。 从ontology.interop.system.runtime导入gettime,checkwitness,notify,serialize,反序列化 从ontology.interop.ontology.runtime导入base58toaddress、addresstobase58、getcurrentblockhash 2.1通知API notify函数将事件推送到整个网络。在下面的示例中,函数notify将返回一个“hello world”十六进制字符串并将其推送到整个网络。 从ontology.interop.system.runtime导入通知 def demo(): 通知('你好世界') 合作伙伴可以在日志中查看:

本体技术视点 | Python智能合约执行API知多少?

2.2获取时间API gettime函数返回当前时间戳,该时间戳以秒为单位返回调用该函数的unix时间。 从ontology.interop.system.runtime导入gettime def demo(): 时间=gettime() 返回时间返回时间戳(秒) 2.3 GetCurrentBlockHash API getcurrentblockhash函数返回当前块的哈希值。 从ontology.interop.ontology.runtime导入GetCurrentBlockHash def demo(): 块哈希=GetCurrentBlockHash() 返回块哈希 2.4序列化和反序列化 这是一对序列化和反序列化函数。 Serialize函数将对象序列化为字节数组对象,Deserialize函数将字节数组反序列化为原始对象。以下代码段实现了传入参数的序列化,并将它们存储在合同的持久存储中。它还实现从合约的持久存储中获取数据并对其进行反序列化。 从ontology.interop.System.Runtime导入Notify,Serialize,Deserialize 从ontology.interop.System.Storage导入Put,Get,GetContext Def Main(操作,args): 如果operation=='serialize_to_bytearray': 数据=args [0] 返回serialize_to_bytearray(数据) 如果operation=='deserialize_from_bytearray': Key=args [0] 返回deserialize_from_bytearray(键) 返回错误 Def serialize_to_bytearray(数据): Sc=GetContext() 键='1' Byte_data=Serialize(data)#Serialize传入参数 Put(sc,key,byte_data)#在块链中存储序列化数据 Def deserialize_from_bytearray(key): SC=GetContext() Byte_data=Get(sc,key)#按键从块链中提取数据 Data=Deserialize(byte_data)#将bytearray数据反序列化为原始数据类型 返回数据 2.5 Base58ToAddress& AdressToBase58 这是一对地址转换功能。其中,Base58ToAddress函数将base58编码的地址转换为字节数组,而AddressToBase58将字节数组转换为base58。 从本体论。互操作。本体论。运行时导入Base58ToAddress,AddressToBase58 Def demo(): Base58_addr='AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn' Addr=Base58ToAddress(base58_addr)#将base58地址转换为bytearray地址 通知(地址) Base58_addr=AddressToBase58(addr)#将bytearray地址转换为base58地址 通知(基础58_addr) 2.6检查证人 CheckWitness(fromAcct)函数有两个函数: 验证当前函数调用者是否不是来自Act。如果是(即通过签名验证),则函数返回。 检查当前函数调用者是否是合同。如果是合同,并且从合同执行该功能,则通过验证。也就是说,验证fromAcct是GetCallingScriptHash()的返回值。其中,GetCallingScriptHash()函数可以获取当前智能合约的合约哈希值。 GetCallingScriptHash(): https://github.com/ontio/ontology-python-compiler/blob/master/ontology/interop/System/ExecutionEngine.py 从ontology.interop.System.Runtime导入CheckWitness 来自ontology.interop.Ontology.Runtime导入Base58ToAddress Def demo(): ADDR=Base58ToAddress( 'AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z') Res=CheckWitness(addr)#验证调用者地址是否为AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z 返回资源 3.总结 在这个技术观点中,我们引入了本体区块链的Runtime API,这在本体Python智能合约中非常有用。在下一个技术观点中,我们将介绍Native API,以探索如何在本体智能合约中转移资金。

微信二维码
售前客服二维码

文章均源于网络收集编辑侵删

提示:仅接受技术开发咨询!

郑重申明:资讯文章为网络收集整理,官方公告以外的资讯内容与本站无关!
NFT开发,NFT交易所开发,DAPP开发 Keywords: NFT开发 NFT交易所开发 DAPP开发