如何在本體網路上用Python編寫智能合約。 第 3 部分:運行時 API

如何在本體網路上用Python編寫智能合約。 第 3 部分:運行時 API

這是關於在本體區塊鏈網路上使用 Python 創建智能合約的系列教育文章的第三部分。 在之前的文章中我們認識了

  1. 區塊鏈和區塊 API
  2. 儲存API.

現在您已經了解了在本體網路上使用Python開發智能合約時如何呼叫合適的持久儲存API,讓我們繼續學習如何使用 運行時API (合約執行API)。 Runtime API 有 8 個相關 API,為合約執行提供通用接口,幫助開發人員檢索、轉換和驗證資料。

以下簡單介紹一下8個API資料:

如何在本體網路上用Python編寫智能合約。 第 3 部分:運行時 API

下面我們詳細看看8個API資料如何使用。 在此之前,您可以在本體智能合約開發工具中建立新合約 智能X 並按照以下說明進行操作。

如何使用運行時 API

有兩種導入方式 運行時API:ontology.interop.System.Runtime и 本體.互通.本體.運作時。 Ontology路徑包含新新增的API。 下面的行導入 API 資料。

from ontology.interop.System.Runtime import GetTime, CheckWitness, Log, Notify, Serialize, Deserialize
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHash

通知API

通知功能在整個網路中廣播該事件。 在下面的範例中,Notify 函數將傳回十六進位字串「hello word」並在整個網路中廣播它。

from ontology.interop.System.Runtime import Notify
def demo():
    Notify("hello world")

您可以在日誌中看到這一點:

如何在本體網路上用Python編寫智能合約。 第 3 部分:運行時 API

取得時間API

GetTime 函數傳回目前時間戳,即傳回呼叫函數的 Unix 時間。 計量單位是秒。

from ontology.interop.System.Runtime import GetTime
def demo():
    time=GetTime()
    return time # return a uint num

獲取當前區塊哈希 API

GetCurrentBlockHash 函數傳回目前區塊的雜湊值。

from ontology.interop.Ontology.Runtime import GetCurrentBlockHash
def demo():
    block_hash = GetCurrentBlockHash()
    return block_hash

序列化和反序列化

這是一對序列化和反序列化函數。 Serialize 函數將物件轉換為 bytearray 對象,Deserialize 函數將 bytearray 轉換為原始物件。 下面的程式碼範例轉換傳入的參數並將它們儲存在合約的持久性儲存中。 它還從合約的持久性儲存中檢索資料並將其轉換為原始物件。

from ontology.interop.System.Runtime import GetTime, CheckWitness, Log, 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)
    data = Deserialize(byte_data)
    return data

Base58ToAddress 和 AddressToBase58

這對地址轉換函數。 Base58ToAddress函數將base58編碼的位址轉換為bytearray位址,AddressToBase58將bytearray位址轉換為base58編碼的位址。

from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58
def demo():
    base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn"
    addr=Base58ToAddress(base58_addr)
    Log(addr)
    base58_addr=AddressToBase58(addr)
    Log(base58_addr)

檢查證人

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)
    return res

更多資訊請參見 古圖布。 在下一篇文章中我們將介紹 原生API了解如何在本體智能合約中轉移資產。

文章由編輯翻譯 算力&份額 特別是對於OntologyRussia。

你是開發者嗎? 加入我們的技術社區 不和. 另外,看看 開發者中心 Ontology,您可以在那裡找到更多工具、文件等等。

為開發人員開放任務。 完成任務並獲得獎勵。

申請 本體人才計畫學生

本體論

本體網站 - GitHub上 - 不和 - 電報俄語 - Twitter - 書籤交易

來源: www.habr.com

添加評論