'Python智能合約執行API知多少?'

Python 區塊鏈 UNIX 巴比特 2019-09-14
"

01 導語

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

"

01 導語

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

Python智能合約執行API知多少?"

01 導語

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

Python智能合約執行API知多少?Python智能合約執行API知多少?

圖 | 網絡

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

02 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, GetCurrentBlockHas

2.1 Notify API

Notify 函數將事件推送到全網。如下例子中函數 Notify 將會返回 “hello world” 十六進制字符串,並將其推送全網。

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

小夥伴可以在 Logs 中查看:

"

01 導語

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

Python智能合約執行API知多少?Python智能合約執行API知多少?

圖 | 網絡

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

02 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, GetCurrentBlockHas

2.1 Notify API

Notify 函數將事件推送到全網。如下例子中函數 Notify 將會返回 “hello world” 十六進制字符串,並將其推送全網。

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

小夥伴可以在 Logs 中查看:

Python智能合約執行API知多少?

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, Deserializefrom ontology.interop.System.Storage import Put, Get, GetContextdef 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 Falsedef 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

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)

這是一對地址轉換函數。其中,Base58ToAddress 函數將 base58 編碼的地址轉成 byte array 形式地址,而 AddressToBase58 則將 byte array 形式地址轉成 base58 編碼的地址。

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

03 總結

本次技術視點中我們介紹了本體區塊鏈的 Runtime API,該類 API 在本體 Python 智能合約中用處十分巨大。在下一期技術視點中,我們將介紹 Native API,探討如何在本體智能合約中進行轉賬等操作。本期講述的所有語法部分我們提供了中文視頻,小夥伴們可以觀看學習。

"

相關推薦

推薦中...