如何正确的使用智能合约执行Runtime API

网友投稿 306 2022-10-23

如何正确的使用智能合约执行Runtime API

01 导语

下面我们具体讲述一下这8个 API 的使用方法。在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。同样,在文章最后我们将给出这次讲解的所有源代码以及视频讲解。

from ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize

2.1 Notify API

from ontology.interop.System.Runtime import Notify

def demo():

Notify(“hello world”)

小伙伴可以在 Logs 中查看:

2.2 GetTime API

GetTime 函数返回当前时间戳,即返回调用该函数的 Unix 时间,其单位是秒。

from ontology.interop.System.Runtime import GetTime

def demo():

time=GetTime()

return time # 返回时间戳, 单位是秒

2.3 GetCurrentBlockHash API

GetCurrentBlockHash 函数返回当前区块的哈希值。

from ontology.interop.Ontology.Runtime import GetCurrentBlockHash

def demo():

block_hash = GetCurrentBlockHash()

return block_hash

2.4 Serialize 和 Deserialize

from ontology.interop.System.Runtime import Notify, Serialize, Deserialize

from ontology.interop.System.Storage import Put, Get, GetContext

def Main(operation, args):

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) # 按key从区块链中取出数据

data = Deserialize(byte_data) # 将bytearray数据反序列化成原先类型数据

return data

2.5 Base58ToAddress & AdressToBase58

这是一对地址转换函数。其中,Base58ToAddress 函数将 base58 编码的地址转成 byte array 形式地址,而 AddressToBase58 则将 byte array 形式地址转成 base58 编码的地址。

from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58

def demo():

base58_addr=“AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn”

addr=Base58ToAddress(base58_addr) # 将 base58 地址转成 bytearray形式地址

Notify(addr)

base58_addr=AddressToBase58(addr) #将bytearray地址转为base58地址

Notify(base58_addr)

2.6 CheckWitness

CheckWitness(fromAcct) 函数有两个功能:

验证当前的函数调用者是不是 fromAcct 。若是(即签名验证通过),则函数返回通过。

检查当前函数调用者是不是一个合约。若是合约,且是从该合约发起去执行函数,则验证通过。即,验证 fromAcct 是不是 GetCallingScriptHash() 的返回值。其中,GetCallingScriptHash() 函数可以得到调用当前智能合约的合约哈希值。

GetCallingScriptHash():

from ontology.interop.System.Runtime import CheckWitness

from ontology.interop.Ontology.Runtime import Base58ToAddress

def demo():

addr=Base58ToAddress(“AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z”)

res=CheckWitness(addr) # 验证调用者地址是否为AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z

return res

3. 总结

本次技术视点中我们介绍了本体区块链的 Runtime API,该类 API 在本体 Python 智能合约中用处十分巨大。在下一期技术视点中,我们将介绍 Native API,探讨如何在本体智能合约中进行转账等操作。 来源: 本体研究院

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Rancher 和知乎超大规模多集群管理联合实践
下一篇:一篇文章超详细的介绍Java继承
相关文章

 发表评论

暂时没有评论,来抢沙发吧~