Ibhalwa kanjani inkontileka ehlakaniphile kuPython kunethiwekhi ye-Ontology. Ingxenye 1: Blockchain & Block API

Ibhalwa kanjani inkontileka ehlakaniphile kuPython kunethiwekhi ye-Ontology. Ingxenye 1: Blockchain & Block API

Lena ingxenye yokuqala ochungechungeni lwezifundo zokudala izinkontileka ezihlakaniphile ze-Python kunethiwekhi ye-Ontology blockchain kusetshenziswa ithuluzi lokuthuthukisa izinkontileka ezihlakaniphile. I-SmartX.

Kulesi sihloko, sizoqala ukwazana kwethu ne-Ontology smart contract API. I-Ontology smart contract API ihlukaniswe ngamamojula ayi-7:

  1. I-Blockchain & Block API,
  2. i-runtime API,
  3. isitoreji API,
  4. i-API yomdabu,
  5. Thuthukisa i-API,
  6. I-Execution Engine API kanye
  7. I-Static & Dynamic Call API.

I-Blockchain & Block API iyingxenye eyinhloko yohlelo lwenkontileka ehlakaniphile ye-Ontology. I-Blockchain API isekela imisebenzi eyisisekelo yombuzo we-blockchain, njengokuthola ukuphakama kwebhulokhi yamanje, kuyilapho i-Block API isekela imisebenzi eyisisekelo yombuzo webhulokhi, njengokubuza inombolo yokuthenga yebhulokhi enikeziwe.

Ake siqale!

Okokuqala, dala inkontileka entsha I-SmartXbese ulandela imiyalelo engezansi.

1. Indlela yokusebenzisa i-Blockchain API

Izixhumanisi zemisebenzi yenkontileka ehlakaniphile ziyefana nezixhumanisi zePython. Ungafaka imisebenzi ehambisanayo njengoba kudingeka. Isibonelo, isitatimende esilandelayo sethula umsebenzi we-GetHeight ukuze uthole ubude bebhulokhi yamanje, nomsebenzi we-GetHeader ukuze uthole unhlokweni webhulokhi.

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

I-GetHeight

I-GetHeight isetshenziselwa ukuthola inombolo yokugcina yokulandelana kwebhulokhi ku-blockchain, njengoba kukhonjisiwe esibonelweni esingezansi. Esibonelweni sokugcina, sizoshiya umsebenzi oyinhloko ukuze kube lula, kodwa ungawengeza uma kunesidingo.

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

I-GetHeader isetshenziselwa ukuthola unhlokweni webhulokhi, ipharamitha iyinombolo ye-serial yebhulokhi ku-blockchain. Isibonelo:

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

I-GetTransactionByHash isetshenziselwa ukuthola umsebenzi nge-hashi yokwenziwe. I-hash yomsebenzi ithunyelwa ku GetTransactionByHash njengamapharamitha ngefomethi ye-bytearray. Ukhiye walo msebenzi uwukuguqula i-hashi yokwenziwe ngefomethi ye-hex ibe i-hashi yomsebenzi ngefomethi ye-bytearray. Lesi isinyathelo esibalulekile. Uma kungenjalo, uzothola iphutha elibonisa ukuthi asikho ibhulokhi enaleyo block hash. Ake sithathe i-hashi yokwenziwe ngefomethi ye-hex njengesibonelo ukuyiguqulela kufomethi ye-bytearray. Isibonelo sibukeka kanje:

9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1

Okokuqala, hlehlisa i-hash yomsebenzi:

c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279

Onjiniyela bangafeza lesi sinyathelo besebenzisa i-Hex Number(i-endian encane)ithuluzi lokuguqula inombolo elinikezwe i-SmartX.

Bese uguqula umphumela ube yifomethi ye-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}

Lokhu kungenziwa kusetshenziswa ithuluzi lokuguqula i-String Byte Array elinikezwe i-SmartX. Okokugcina, guqula i-bytearray ewumphumela ube iyunithi yezinhlamvu efanayo:

xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f

Okulandelayo isibonelo somsebenzi we-GetTransactionByHash othatha umsebenzi usebenzisa i-hash yomsebenzi:

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

I-GetTransactionHeight

I-GetTransactionHeight isetshenziselwa ukuthola ubude bokwenziwe nge-hashi yokwenziwe. Ake sithathe i-hashi esibonelweni esingenhla:

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

TholaInkontileka

Onjiniyela bangasebenzisa umsebenzi we-GetContract ukuze bathole inkontileka nge-hash yenkontileka. Inqubo yokuguqula i-hashi yenkontileka iyafana nenqubo yokuguqulwa kwe-hashi yokwenziwayo okukhulunywe ngenhla.

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

I-GetBlock

I-GetBlock isetshenziselwa ukuthola ibhulokhi. Kunezindlela ezimbili zokuthola ibhulokhi ethile.

1. Thola ibhulokhi ngobude bebhulokhi:

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

2. Thola i-block by block hash:

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

2. Isetshenziswa kanjani i-Block API

Kunemisebenzi emithathu etholakalayo ku-Block API: Thola Ukuthengiselana, GetTransactionCountfuthi GetTransactionByIndex. Sizowahlephula ngamunye ngamunye.

GetTransactionCount

I-GetTransactionCount isetshenziselwa ukuthola inani lemisebenzi yebhulokhi enikeziwe.

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

Thola Ukuthengiselana

Onjiniyela bangasebenzisa umsebenzi we-GetTransactions ukuze bathole konke ukuthengiselana endaweni ethile.

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

I-GetTransactionByIndex isetshenziselwa ukuthola ukuthengiselana okuthile endaweni ethile.

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

Umhlahlandlela ophelele ungatholakala kwethi GitHub.

I-Afterword

I-Blockchain & Block API iyingxenye ebalulekile yezinkontileka ezihlakaniphile njengoba ungazisebenzisa ukucela idatha ye-blockchain futhi uvimbe idatha ezinkontilekeni ezihlakaniphile. Ezihlokweni ezilandelayo, sizoxoxa ngendlela yokusebenzisa amanye ama-API futhi sithole ukuthi asebenzisana kanjani ne-Ontology blockchain.

Isihloko sahunyushwa abahleli be-Hashrate&Shares ikakhulukazi i-OntologyRussia. khala

Ingabe ungunjiniyela? Joyina umphakathi wethu wezobuchwepheshe ku I-Discord. Futhi, bheka Isikhungo Sonjiniyela kuwebhusayithi yethu, lapho ungathola khona amathuluzi onjiniyela, imibhalo, nokuningi.

I-Ontology

Source: www.habr.com

Engeza amazwana