本體技術視點 | Python智慧合約開發學起來!
By 本體研究院·
1. 前言在之前的技術視點文章中,我們介紹了目前本體主網支援的智慧合約體系以及相應的智慧合約開發工具 SmartX。很多小夥伴都想上手練一練。在本期的本體技術視點中,我們將正式開始講述智慧合約語法部分。本體的智慧合約 API 分為7個模組,分別是 Blockchain & Block API、Runtime API、Storage API、Native API、Upgrade API、Execution Engine API 以及 Static & Dynamic Call API。本期我們將介紹 Blockchain & Block API,這是本體智慧合約體系中最基礎的部分。其中,Blockchain API 支援基本的區塊鏈查詢操作,如獲取當前塊高等;Block API 支援基本的區塊查詢操作,如查詢指定區塊交易數等。同時,文末將提供影片講解。在這之前,小夥伴們可以在本體智慧合約開發工具 SmartX 中新建一個合約,跟著我們進行操作。2. Blockchain API 使用方法智慧合約函式的引用與 Python 的引用如出一轍。開發者可以根據需要引入相應的函式。例如,下面語句引入了獲取當前最新塊高函式 GetHeight 和獲取區塊頭函式 GetHeader。from ontology.interop.System.Blockchain import GetHeight, GetHeader2.1 GetHeight開發者可以使用 GetHeight 來獲取當前最新塊高,具體例子如下。在後面的例子中,為了節省空間,我們將省略 Main 函式,小夥伴在練習的時候可以根據需要加入。from ontology.interop.System.Runtime import Notifyfrom ontology.interop.System.Blockchain import GetHeightdef Main(operation): if operation == 'demo': return demo() return Falsedef demo(): height=GetHeight() Notify(height) # 列印height return height #在函式執行結束後返回height2.2 GetHeader開發者可以使用 GetHeader 來獲取區塊頭,引數是某個塊的塊高。具體例子如下:from ontology.interop.System.Runtime import Notifyfrom ontology.interop.System.Blockchain import GetHeaderdef demo(): block_height=10 header=GetHeader(block_height) Notify(header)return header2.3 GetTransactionByHash開發者可以使用 GetTransactionByHash 函式透過交易雜湊獲取交易。交易雜湊以 bytearray 的格式,作為引數傳入 GetTransactionByHash。這個函式的關鍵在於如何轉換將十六進位制格式的交易雜湊轉變為 bytearray 格式的交易雜湊。我們以16進位制格式的交易雜湊為例,實現將十六進位制格式的交易雜湊轉變為 bytearray 格式的交易雜湊。示例雜湊如下:9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1首先,將該交易雜湊反序得到:c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279開發者可以透過 SmartX 提供的轉換工具 Hex Number(little endian) <--> Number 實現這一步。然後,將其轉成 bytearray 格式:{0xc1,0x89,0x0c,0x4d,0x73,0x06,0x26,0xdf,0xaa,0x94,0x49,0x41,0x9d,0x66,0x25,0x05,0xea,0xb3,0xbd,0xa2,0xe1,0xf0,0x1f,0x89,0x46,0x3c,0xc1,0xa4,0xa3,0x0a,0x27,0x9f}開發者可以透過 SmartX 提供的轉換工具 String <--> Byte Array 實現這一步。最後,將得到的 bytearray 轉換成相應的字串:\xc1\x89\x0c\x4d\x73\x06\x26\xdf\xaa\x94\x49\x41\x9d\x66\x25\x05\xea\xb3\xbd\xa2\xe1\xf0\x1f\x89\x46\x3c\xc1\xa4\xa3\x0a\x27\x9fGetTransactionByHash 函式透過交易雜湊獲取交易的例子如下:from ontology.interop.System.Blockchain import GetTransactionByHashdef demo(): # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1" tx_hash=bytearray(b"\xc1\x89\x0c\x4d\x73\x06\x26\xdf\xaa\x94\x49\x41\x9d\x66\x25\x05\xea\xb3\xbd\xa2\xe1\xf0\x1f\x89\x46\x3c\xc1\xa4\xa3\x0a\x27\x9f") tx=GetTransactionByHash(tx_hash) return tx2.4 GetTransactionHeight開發者可以使用 GetTransactionHeight 函式透過交易雜湊獲取交易高度。我們還是以上個例子中的雜湊為例:from ontology.interop.System.Blockchain import GetTransactionHeightdef demo(): # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1" tx_hash=bytearray(b"\xc1\x89\x0c\x4d\x73\x06\x26\xdf\xaa\x94\x49\x41\x9d\x66\x25\x05\xea\xb3\xbd\xa2\xe1\xf0\x1f\x89\x46\x3c\xc1\xa4\xa3\x0a\x27\x9f") height=GetTransactionHeight(tx_hash) return height2.5 GetContract開發者可以使用 GetContract 函式透過合約雜湊獲取合約。其中,合約雜湊的轉換過程與上面講到的交易雜湊轉換過程一致。from ontology.interop.System.Blockchain import GetContractdef demo(): # contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa" contract_hash=bytearray(b"\xfa\x98\x96\x21\x32\x32\xbb\xf0\x9f\x23\x91\xfa\xef\x95\x9b\xff\xa5\x75\x1a\xd8") contract=GetContract(contract_hash) return contract2.6 GetBlock開發者可以使用 GetBlock 函式獲取區塊。有兩種方法可以獲取指定區塊:1. 透過塊高獲取區塊:from ontology.interop.System.Blockchain import GetBlockdef demo(): block=GetBlock(1408) return block2. 透過區塊雜湊獲取區塊:from ontology.interop.System.Blockchain import GetBlockdef demo(): block_hash=bytearray(b'\x16\xe0\xc5\x40\x82\x79\x77\x30\x44\xea\x66\xc8\xc4\x5d\x17\xf7\x17\x73\x92\x33\x6d\x54\xe3\x48\x46\x0b\xc3\x2f\xe2\x15\x03\xe4') block=GetBlock(block_hash)3. Block API 使用方法Block API 中可供引用的函式有三個,它們分別是 GetTransactions、GetTransactionCount 和 GetTransactionByIndex。我們依次介紹下這三個函式。3.1 GetTransactionCount開發者可以使用 GetTransactionCount 函式獲取指定區塊的交易數量。from ontology.interop.System.Blockchain import GetBlockfrom ontology.interop.System.Block import GetTransactionCountdef demo(): block=GetBlock(1408) count=GetTransactionCount(block) return count3.2 GetTransactions開發者可以使用 GetTransactions 函式獲取獲取指定區塊的所有交易。from ontology.interop.System.Blockchain import GetBlockfrom ontology.interop.System.Block import GetTransactions def demo(): block=GetBlock(1408) txs=GetTransactions(block) return txs3.3 GetTransactionByIndex開發者可以使用 GetTransactionByIndex 函式獲取指定區塊的指定交易。from ontology.interop.System.Blockchain import GetBlockfrom ontology.interop.System.Block import GetTransactionByIndexdef demo(): block=GetBlock(1408) tx=GetTransactionByIndex(block,0) # index starts from 0. return tx04 後記Blockchain & Block API 在智慧合約中起到查詢區塊鏈資料和區塊資料的作用,是智慧合約最不可缺少的一部分。在後面的技術視點中,我們將討論如何使用其它 API,探討它們和本體區塊鏈的互動。本期講述的所有語法部分我們提供了中文影片,小夥伴們可以觀看和學習。#本體#Python#智慧合約
免責聲明:
- 本文版權歸原作者所有,僅代表作者本人觀點,不代表鏈報觀點或立場。
- 如發現文章、圖片等侵權行爲,侵權責任將由作者本人承擔。
- 鏈報僅提供相關項目信息,不構成任何投資建議。
推荐阅读