本體技術視點 | Python智慧合約執行API知多少?

買賣虛擬貨幣
01 導語

上一期我們介紹了本體智慧合約儲存 API,相信很多小夥伴都明白了在本體上進行 Python 智慧合約開發時如何呼叫相關 API 進行持久化儲存。本期我們討論如何使用 Runtime API(合約執行 API)。Runtime API 共有8個相關的 API,提供了合約執行時常用的介面,幫助開發者獲取資料、轉換資料以及驗證資料。這8個 API 的簡單描述如下:

下面我們具體講述一下這8個 API 的使用方法。在這之前,小夥伴們可以在本體智慧合約開發工具 SmartX 中新建一個合約,跟著我們進行操作。同樣,在文章最後我們將給出這次講解的所有原始碼以及影片講解。 

2. Runtime API 使用方法

Runtime API 的引用分為兩個路徑,分別是 ontology.interop.System.Runtime 和 ontology.interop.Ontology.Runtime。其中,Ontology 路徑下包含了新增的 API。下列語句引用了這幾個 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 函式將事件推送到全網。如下例子中函式 Notify 將會返回 “hello world” 十六進位制字串,並將其推送全網。

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

這是一對序列化和反序列化函式。其中,Serialize 函式將一個物件序列化成 byte array 物件,而 Deserialize 函式將 byte array 反序列化成原先物件。下面的程式碼片段實現了將傳入引數序列化並存入合約的持久化儲存中,同時也實現了從合約的持久化儲存中取出資料並把它進行反序列化。

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) # 將序列化後的資料存入區塊鏈

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():

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) # 驗證呼叫者地址是否為AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z
    return res

3. 總結

本次技術視點中我們介紹了本體區塊鏈的 Runtime API,該類 API 在本體 Python 智慧合約中用處十分巨大。在下一期技術視點中,我們將介紹 Native API,探討如何在本體智慧合約中進行轉賬等操作。

免責聲明:

  1. 本文版權歸原作者所有,僅代表作者本人觀點,不代表鏈報觀點或立場。
  2. 如發現文章、圖片等侵權行爲,侵權責任將由作者本人承擔。
  3. 鏈報僅提供相關項目信息,不構成任何投資建議

推荐阅读

;