01 导语
图 | 网络
02 Runtime API 使用方法
from ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHas
from ontology.interop.System.Runtime import Notify
def demo():
Notify("hello world")
小伙伴可以在 Logs 中查看:
def deserialize_from_bytearray(key):
sc = GetContext()
byte_data = Get(sc, key) # 按key从区块链中取出数据
data = Deserialize(byte_data) # 将bytearray数据反序列化成原先类型数据
return data https://github.com/ontio/ontology-python-compiler/blob/master/ontology/interop/System/ExecutionEngine.py
from ontology.interop.System.Runtime import GetTime
def demo():
time=GetTime()
return time # 返回时间戳, 单位是秒
from ontology.interop.Ontology.Runtime import GetCurrentBlockHash
def demo():
block_hash = GetCurrentBlockHash()
return block_hash
from ontology.interop.System.Runtime import 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) # 将序列化后的数据存入区块链
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)
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
03 总结