Yuav ua li cas sau daim ntawv cog lus ntse hauv Python ntawm Ontology network. Part 1: Blockchain & Block API

Yuav ua li cas sau daim ntawv cog lus ntse hauv Python ntawm Ontology network. Part 1: Blockchain & Block API

Qhov no yog thawj feem ntawm cov kev qhia ntawm kev tsim Python ntse daim ntawv cog lus ntawm Ontology blockchain network siv cov cuab yeej kev cog lus ntse. SmartX.

Hauv tsab xov xwm no, peb yuav pib peb cov kev paub nrog Ontology ntse daim ntawv cog lus API. Ontology ntse daim ntawv cog lus API muab faib ua 7 modules:

  1. Blockchain & Block API,
  2. runtime API,
  3. API cia khoom,
  4. Native API
  5. Txhim kho API,
  6. Execution Engine API thiab
  7. Static & Dynamic Hu API.

Blockchain & Block API yog qhov tseem ceeb ntawm Ontology ntse daim ntawv cog lus. Blockchain API txhawb nqa kev ua haujlwm ntawm blockchain cov lus nug, xws li tau txais qhov siab tam sim no, thaum Block API txhawb nqa cov lus nug yooj yim, xws li nug tus lej ntawm kev lag luam rau ib qho thaiv.

Cia peb pib!

Ua ntej, tsim ib daim ntawv cog lus tshiab hauv SmartXthiab tom qab ntawd ua raws li cov lus qhia hauv qab no.

1. Yuav siv Blockchain API li cas

Txuas mus rau cov ntawv cog lus ntse ua haujlwm zoo ib yam rau Python txuas. Koj tuaj yeem nkag mus rau cov haujlwm sib xws raws li xav tau. Piv txwv li, nqe lus hauv qab no qhia txog GetHeight muaj nuj nqi kom tau txais qhov siab tam sim no, thiab GetHeader muaj nuj nqi kom tau txais lub block header.

from ontology.interop.System.Blockchain import GetHeight, GetHeader

GetHeight

GetHeight yog siv kom tau txais tus lej ntu ntu kawg hauv blockchain, raws li qhia hauv qhov piv txwv hauv qab no. Hauv qhov piv txwv kawg, peb yuav tshem tawm Cov Haujlwm Tseem Ceeb kom yooj yim, tab sis koj tuaj yeem ntxiv nws yog tias tsim nyog.

from ontology.interop.System.Runtime import Notify
from ontology.interop.System.Blockchain import GetHeight
def Main(operation):
    if operation == 'demo':
        return demo()
    return False

def demo():
    height=GetHeight()
    Notify(height) # print height
    return height #return height after running the function

GetHeader

GetHeader yog siv kom tau lub block header, parameter yog tus naj npawb ntawm cov block hauv blockchain. Piv txwv:

from ontology.interop.System.Runtime import Notify
from ontology.interop.System.Blockchain import GetHeader
def demo():
    block_height=10
    header=GetHeader(block_height) 
    Notify(header)
return header

GetTransactionByHash

GetTransactionByHash yog siv kom tau txais kev sib pauv ntawm kev sib pauv hash. Kev hloov pauv hash raug xa mus rau GetTransactionByHash raws li tsis muaj nyob rau hauv bytearray hom. Tus yuam sij rau txoj haujlwm no yog hloov pauv cov kev sib pauv hash hauv hom hex rau kev sib pauv hash hauv hom ntawv bytearray. Qhov no yog ib kauj ruam tseem ceeb. Txwv tsis pub, koj yuav tau txais qhov yuam kev uas qhia tias tsis muaj qhov thaiv nrog qhov thaiv qhov thaiv ntawd. Cia peb coj cov kev sib pauv hash hauv hex hom ua piv txwv los hloov nws mus rau hom ntawv bytearray. Ib qho piv txwv zoo li no:

9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1

Ua ntej, thim rov qab qhov kev sib pauv hash:

c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279

Cov neeg tsim khoom tuaj yeem ua tiav cov kauj ruam no siv tus lej Hex (me ntsis endian) Tus lej hloov pauv uas muab los ntawm SmartX.

Tom qab ntawd hloov qhov tshwm sim rau bytearray hom:

{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}

Qhov no tuaj yeem ua tiav siv txoj hlua Byte Array hloov dua siab tshiab muab los ntawm SmartX. Thaum kawg, hloov cov txiaj ntsig bytearray mus rau ib txoj hlua zoo sib xws:

xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f

Cov hauv qab no yog ib qho piv txwv ntawm GetTransactionByHash muaj nuj nqi, uas yuav siv sij hawm ib qho kev sib pauv siv lub hash ntawm kev sib pauv:

from ontology.interop.System.Blockchain import GetTransactionByHash
def demo():
    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"    
    tx_hash=bytearray(b"xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f")
    tx=GetTransactionByHash(tx_hash)
    return tx

GetTransactionHeight

GetTransactionHeight yog siv kom tau txais qhov kev sib pauv siab ntawm kev sib pauv hash. Cia peb coj tus hash los ntawm qhov piv txwv saum toj no:

from ontology.interop.System.Blockchain import  GetTransactionHeight
def demo():
    #   tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"    
    tx_hash=bytearray(b"xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f")
    height=GetTransactionHeight(tx_hash)
    return height

GetContract

Cov neeg tsim khoom tuaj yeem siv GetContract muaj nuj nqi kom tau txais daim ntawv cog lus los ntawm hash ntawm daim ntawv cog lus. Daim ntawv cog lus hash conversion txheej txheem yog tib yam li kev sib pauv hash conversion txheej txheem hais saum toj no.

from ontology.interop.System.Blockchain import GetContract
def demo():
    # contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"    
    contract_hash=bytearray(b"xfax98x96x21x32x32xbbxf0x9fx23x91xfaxefx95x9bxffxa5x75x1axd8")
    contract=GetContract(contract_hash)
    return contract

GetBlock

GetBlock yog siv kom tau ib qho thaiv. Muaj ob txoj hauv kev kom tau txais ib qho thaiv tshwj xeeb.

1. Tau thaiv los ntawm thaiv qhov siab:

from ontology.interop.System.Blockchain import GetBlock
def demo():
    block=GetBlock(1408)
    return block

2. Tau txais ib qho thaiv los ntawm block hash:

from ontology.interop.System.Blockchain import GetBlock
def demo():    
    block_hash=bytearray(b'x16xe0xc5x40x82x79x77x30x44xeax66xc8xc4x5dx17xf7x17x73x92x33x6dx54xe3x48x46x0bxc3x2fxe2x15x03xe4')
    block=GetBlock(block_hash)

2. Yuav siv Block API li cas

Muaj peb txoj haujlwm muaj nyob hauv Block API: GetTransactions, GetTransactionCountthiab GetTransactionByIndex. Peb yuav rhuav tshem lawv ib leeg.

GetTransactionCount

GetTransactionCount yog siv kom tau txais tus lej ntawm kev hloov pauv rau qhov muab thaiv.

from ontology.interop.System.Blockchain import GetBlock
from ontology.interop.System.Block import GetTransactionCount
def demo():
    block=GetBlock(1408)
    count=GetTransactionCount(block)
    return count

GetTransactions

Cov neeg tsim khoom tuaj yeem siv GetTransactions muaj nuj nqi kom tau txais tag nrho cov kev lag luam hauv ib qho thaiv.

from ontology.interop.System.Blockchain import GetBlock
from ontology.interop.System.Block import GetTransactions 
def demo():
    block=GetBlock(1408)
    txs=GetTransactions(block)
    return txs

GetTransactionByIndex

GetTransactionByIndex yog siv kom tau txais ib qho kev lag luam tshwj xeeb hauv ib qho thaiv.

from ontology.interop.System.Blockchain import GetBlock
from ontology.interop.System.Block import GetTransactionByIndex
def demo():
    block=GetBlock(1408)
    tx=GetTransactionByIndex(block,0) # index starts from 0.
    return tx

Daim ntawv qhia ua tiav tuaj yeem pom ntawm peb GitHub.

Tom qab ntawd

Blockchain & Block API yog ib qho tseem ceeb ntawm cov ntawv cog lus ntse raws li koj tuaj yeem siv lawv los thov cov ntaub ntawv blockchain thiab thaiv cov ntaub ntawv hauv cov ntawv cog lus ntse. Hauv cov ntawv yav tom ntej, peb yuav tham txog yuav ua li cas siv lwm tus APIs thiab nrhiav seb lawv cuam tshuam li cas nrog Ontology blockchain.

Tsab ntawv tau muab txhais los ntawm cov neeg kho ntawm Hashrate & Shares tshwj xeeb tshaj yog rau OntologyRussia. quaj

Koj puas yog tus tsim tawm? Koom nrog peb tech zej zog ntawm Discord. Tsis tas li, ua tib zoo saib Developer Center ntawm peb lub vev xaib, qhov twg koj tuaj yeem nrhiav cov cuab yeej tsim tawm, cov ntaub ntawv, thiab lwm yam.

Ontology

Tau qhov twg los: www.hab.com

Ntxiv ib saib