Pehea e kākau ai i kahi ʻaelike akamai ma Python ma ka pūnaewele Ontology. Mahele 1: Blockchain & Block API

Pehea e kākau ai i kahi ʻaelike akamai ma Python ma ka pūnaewele Ontology. Mahele 1: Blockchain & Block API

ʻO kēia ka ʻāpana mua o ke ʻano o nā kumu aʻo e pili ana i ka hana ʻana i nā ʻaelike akamai Python ma ka pūnaewele blockchain Ontology me ka hoʻohana ʻana i ka mea hana hoʻomohala akamai. SmartX.

Ma kēia ʻatikala, e hoʻomaka mākou e kamaʻilio me ka Ontology smart contract API. Ua māhele ʻia ka Ontology smart contract API i 7 modules:

  1. Blockchain & Block API,
  2. API no ka holo ana,
  3. API no ka mālama ʻana,
  4. API maoli,
  5. Hoʻonui API,
  6. API 'enekini ho'okō a
  7. API Kāhea Kūikawā a me Dynamic.

ʻO Blockchain & Block API ka mea nui o ka Ontology smart contract system. Kākoʻo ka Blockchain API i nā hana nīnau nīnau blockchain, e like me ka loaʻa ʻana o ke kiʻekiʻe o ka poloka i kēia manawa, ʻoiai ke kākoʻo nei ka Block API i nā hana nīnau nīnau poloka, e like me ka nīnau ʻana i ka helu o nā kālepa no kahi poloka i hāʻawi ʻia.

E hoʻomaka kākou!

ʻO ka mua, e hana i kahi ʻaelike hou ma SmartXa laila e hahai i nā kuhikuhi ma lalo nei.

1. Pehea e hoʻohana ai i ka Blockchain API

Ua like nā loulou i nā hana ʻaelike akamai me nā loulou Python. Hiki iā ʻoe ke komo i nā hana pili e like me ka mea e pono ai. No ka laʻana, hoʻolauna ka ʻōlelo ma lalo nei i kahi hana GetHeight e kiʻi i ke kiʻekiʻe o ka poloka o kēia manawa, a me kahi hana GetHeader e kiʻi i ke poʻo o ka poloka.

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

GetHeight

Hoʻohana ʻia ʻo GetHeight e kiʻi i ka helu kaʻina poloka hope loa ma ka blockchain, e like me ka hōʻike ʻana ma ka laʻana ma lalo nei. I ka laʻana hope, e haʻalele mākou i ka hana Main no ka maʻalahi, akā hiki iā ʻoe ke hoʻohui inā pono.

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

Hoʻohana ʻia ʻo GetHeader e kiʻi i ke poʻo poʻomanaʻo, ʻo ka ʻāpana ka helu serial o ka poloka ma ka blockchain. Laʻana:

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

Hoʻohana ʻia ʻo GetTransactionByHash e kiʻi i kahi kālepa ma o ka hash transaction. Hoʻouna ʻia ka hash kālepa i GetTransactionByHash e like me nā ʻāpana ma ke ʻano bytearray. ʻO ke kī o kēia hana ʻo ia ka hoʻololi ʻana i ka hash transaction ma ke ʻano hex i ka hash transaction in bytearray format. He ʻanuʻu koʻikoʻi kēia. A i ʻole, loaʻa iā ʻoe kahi hewa e hōʻike ana ʻaʻohe poloka me kēlā poloka hash. E lawe kākou i ka hash kālepa ma ke ʻano hex ma ke ʻano he laʻana e hoʻololi iā ia i ke ʻano bytearray. He laʻana e like me kēia:

9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1

ʻO ka mea mua, e hoʻohuli i ka hash transaction:

c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279

Hiki i nā mea hoʻomohala ke hoʻokō i kēia kaʻina me ka hoʻohana ʻana i ka Hex Number(little endian)Helu hoʻololi mea hana i hāʻawi ʻia e SmartX.

A laila e hoʻololi i ka hopena i ka format 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}

Hiki ke hana i kēia me ka hoʻohana ʻana i ka hāmeʻa hoʻololi String Byte Array i hāʻawi ʻia e SmartX. ʻO ka hope, hoʻololi i ka bytearray i loaʻa i kahi string like:

xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f

Eia kekahi laʻana o ka hana GetTransactionByHash, e lawe ana i kahi kālepa me ka hash o ka hana:

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

GetTransactionHeight

Hoʻohana ʻia ʻo GetTransactionHeight e kiʻi i ke kiʻekiʻe o ke kālepa ma o ka hash transaction. E lawe kākou i ka hash mai ka laʻana ma luna:

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

GetContract

Hiki i nā mea hoʻomohala ke hoʻohana i ka hana GetContract e kiʻi i kahi ʻaelike ma o ka hash o ka ʻaelike. ʻO ke kaʻina hana hoʻololi hash aelike e like me ke kaʻina hana hoʻololi hash i ʻōlelo ʻia ma luna.

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

GetBlock

Hoʻohana ʻia ʻo GetBlock e kiʻi i kahi poloka. ʻElua ala e loaʻa ai kahi poloka kikoʻī.

1. E kiʻi i ka ʻāpana ma ke kiʻekiʻe o ka poloka:

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

2. E kiʻi i kekahi poloka ma kēlā me kēia poloka hash:

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

2. Pehea e hoʻohana ai i Block API

Aia ʻekolu mau hana i loaʻa i ka Block API: GetTransactions, GetTransactionCounta GetTransactionByIndex. E wāwahi mākou iā lākou i kēlā me kēia.

GetTransactionCount

Hoʻohana ʻia ʻo GetTransactionCount e kiʻi i ka helu o nā kālepa no kahi poloka i hāʻawi ʻia.

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

Hiki i nā mea hoʻomohala ke hoʻohana i ka hana GetTransactions no ka loaʻa ʻana o nā hana āpau ma kahi poloka i hāʻawi ʻia.

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

Hoʻohana ʻia ʻo GetTransactionByIndex e kiʻi i kahi kālepa kikoʻī ma kahi poloka i hāʻawi ʻia.

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

Hiki ke loaʻa kahi alakaʻi piha ma kā mākou GitHub.

Ma hope o ka'ōlelo

ʻO ka Blockchain & Block API kahi ʻāpana koʻikoʻi o nā ʻaelike akamai no ka mea hiki iā ʻoe ke hoʻohana iā lākou no ke noi ʻana i ka ʻikepili blockchain a hoʻopaʻa i ka ʻikepili i nā ʻaelike akamai. Ma nā ʻatikala aʻe, e kūkākūkā mākou pehea e hoʻohana ai i ke koena o nā API a ʻike pehea lākou e launa pū ai me ka Ontology blockchain.

Ua unuhi ʻia ka ʻatikala e nā mea hoʻoponopono o Hashrate&Shares kūikawā no OntologyRussia. uwe

He mea hoʻomohala ʻoe? E hui pū me kā mākou kaiaulu ʻenehana ma Ka wehewehe. Eia kekahi, e nānā i Keena hoʻomohala ma kā mākou pūnaewele, kahi e ʻike ai ʻoe i nā mea hana hoʻomohala, palapala, a me nā mea hou aku.

Ontology

Source: www.habr.com

Pākuʻi i ka manaʻo hoʻopuka