如何正確的使用智能合約執(zhí)行Runtime API
01 導(dǎo)語(yǔ)
本期我們討論如何使用 Runtime API(合約執(zhí)行 API)。RunTIme API 共有8個(gè)相關(guān)的 API,提供了合約執(zhí)行時(shí)常用的接口,幫助開發(fā)者獲取數(shù)據(jù)、轉(zhuǎn)換數(shù)據(jù)以及驗(yàn)證數(shù)據(jù)。這8個(gè) API 的簡(jiǎn)單描述如下:
下面我們具體講述一下這8個(gè) API 的使用方法。在這之前,小伙伴們可以在本體智能合約開發(fā)工具 SmartX 中新建一個(gè)合約,跟著我們進(jìn)行操作。同樣,在文章最后我們將給出這次講解的所有源代碼以及視頻講解。
2. RunTIme API 使用方法
RunTIme API 的引用分為兩個(gè)路徑,分別是 ontology.interop.System.RunTIme 和 ontology.interop.Ontology.Runtime。其中,Ontology 路徑下包含了新增的 API。下列語(yǔ)句引用了這幾個(gè) API。
from ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHash
2.1 Notify API
Notify 函數(shù)將事件推送到全網(wǎng)。如下例子中函數(shù) Notify 將會(huì)返回 “hello world” 十六進(jìn)制字符串,并將其推送全網(wǎng)。
from ontology.interop.System.Runtime import Notify
def demo():
Notify(“hello world”)
小伙伴可以在 Logs 中查看:
2.2 GetTime API
GetTime 函數(shù)返回當(dāng)前時(shí)間戳,即返回調(diào)用該函數(shù)的 Unix 時(shí)間,其單位是秒。
from ontology.interop.System.Runtime import GetTime
def demo():
time=GetTime()
return time # 返回時(shí)間戳, 單位是秒
2.3 GetCurrentBlockHash API
GetCurrentBlockHash 函數(shù)返回當(dāng)前區(qū)塊的哈希值。
from ontology.interop.Ontology.Runtime import GetCurrentBlockHash
def demo():
block_hash = GetCurrentBlockHash()
return block_hash
2.4 Serialize 和 Deserialize
這是一對(duì)序列化和反序列化函數(shù)。其中,Serialize 函數(shù)將一個(gè)對(duì)象序列化成 byte array 對(duì)象,而 Deserialize 函數(shù)將 byte array 反序列化成原先對(duì)象。下面的代碼片段實(shí)現(xiàn)了將傳入?yún)?shù)序列化并存入合約的持久化存儲(chǔ)中,同時(shí)也實(shí)現(xiàn)了從合約的持久化存儲(chǔ)中取出數(shù)據(jù)并把它進(jìn)行反序列化。
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) # 序列化傳入的參數(shù)
Put(sc, key, byte_data) # 將序列化后的數(shù)據(jù)存入?yún)^(qū)塊鏈
def deserialize_from_bytearray(key):
sc = GetContext()
byte_data = Get(sc, key) # 按key從區(qū)塊鏈中取出數(shù)據(jù)
data = Deserialize(byte_data) # 將bytearray數(shù)據(jù)反序列化成原先類型數(shù)據(jù)
return data
2.5 Base58ToAddress & AdressToBase58
這是一對(duì)地址轉(zhuǎn)換函數(shù)。其中,Base58ToAddress 函數(shù)將 base58 編碼的地址轉(zhuǎn)成 byte array 形式地址,而 AddressToBase58 則將 byte array 形式地址轉(zhuǎn)成 base58 編碼的地址。
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58
def demo():
base58_addr=“AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn”
addr=Base58ToAddress(base58_addr) # 將 base58 地址轉(zhuǎn)成 bytearray形式地址
Notify(addr)
base58_addr=AddressToBase58(addr) #將bytearray地址轉(zhuǎn)為base58地址
Notify(base58_addr)
2.6 CheckWitness
CheckWitness(fromAcct) 函數(shù)有兩個(gè)功能:
驗(yàn)證當(dāng)前的函數(shù)調(diào)用者是不是 fromAcct 。若是(即簽名驗(yàn)證通過(guò)),則函數(shù)返回通過(guò)。
檢查當(dāng)前函數(shù)調(diào)用者是不是一個(gè)合約。若是合約,且是從該合約發(fā)起去執(zhí)行函數(shù),則驗(yàn)證通過(guò)。即,驗(yàn)證 fromAcct 是不是 GetCallingScriptHash() 的返回值。其中,GetCallingScriptHash() 函數(shù)可以得到調(diào)用當(dāng)前智能合約的合約哈希值。
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”)
res=CheckWitness(addr) # 驗(yàn)證調(diào)用者地址是否為AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z
return res
3. 總結(jié)
本次技術(shù)視點(diǎn)中我們介紹了本體區(qū)塊鏈的 Runtime API,該類 API 在本體 Python 智能合約中用處十分巨大。在下一期技術(shù)視點(diǎn)中,我們將介紹 Native API,探討如何在本體智能合約中進(jìn)行轉(zhuǎn)賬等操作。
來(lái)源: 本體研究院?