Momwe mungalembe mgwirizano wanzeru ku Python pa netiweki ya Ontology. Gawo 1: Blockchain & Block API

Momwe mungalembe mgwirizano wanzeru ku Python pa netiweki ya Ontology. Gawo 1: Blockchain & Block API

Ili ndi gawo loyamba la mndandanda wankhani zophunzitsa zopanga makontrakitala anzeru ku Python pa netiweki ya Ontology blockchain pogwiritsa ntchito chida chokulitsa mgwirizano wanzeru. SmartX.

M'nkhaniyi, tiyamba kudziwana ndi Ontology smart contract API. The Ontology smart contract API yagawidwa m'ma module 7:

  1. Blockchain & Block API,
  2. Runtime API
  3. API yosungirako
  4. Native API
  5. Upgrade API,
  6. Execution Engine API ndi
  7. Static & Dynamic Call API.

Blockchain & Block API ndiye gawo lalikulu la Ontology smart contract system. Blockchain API imathandizira ntchito zoyambira zamafunso za blockchain, monga kupeza kutalika kwa chipika, pomwe Block API imathandizira ntchito zamafunso a blockchain, monga kufunsira kuchuluka kwa zomwe zachitika pa block yomwe wapatsidwa.

Tiyeni tiyambe!

Kuti muyambe, pangani mgwirizano watsopano mu SmartX, ndiyeno tsatirani malangizo ali m’munsiwa.

1. Momwe mungagwiritsire ntchito Blockchain API

Zolemba za Smart contract ntchito ndizofanana ndi zolemba za Python. Mutha kuyika magwiridwe antchito ngati pakufunika. Mwachitsanzo, mawu otsatirawa amayambitsa GetHeight, ntchito yopezera kutalika kwa chipika, ndi GetHeader, ntchito yopezera mutu wa block.

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

GetHeight

GetHeight imagwiritsidwa ntchito kuti mupeze nambala yomaliza ya blockchain mu blockchain, monga tawonera mu chitsanzo pansipa. Mu chitsanzo chomaliza, kuti zitheke, tidzalumpha Ntchito Yaikulu, koma mukhoza kuwonjezera ngati kuli kofunikira.

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 imagwiritsidwa ntchito kupeza mutu wa block; parameter ndiye nambala ya blockchain mu blockchain. Chitsanzo:

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 imagwiritsidwa ntchito kubweza zomwe zachitika pogwiritsa ntchito hashi. Hashi yobwereketsa imatumizidwa ku GetTransactionByHash monga magawo mu mawonekedwe a bytearray. Chinsinsi cha ntchitoyi ndikutembenuza ma hashi mumtundu wa hex kukhala mawonekedwe a bytearray. Iyi ndi sitepe yofunika. Apo ayi mudzapeza cholakwika chosonyeza kuti palibe chipika chokhala ndi block hash. Tiyeni titenge ma hashi mumtundu wa hex ngati chitsanzo kuti tisinthe kukhala mtundu wa bytearray. Chitsanzo chikuwoneka motere:

9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1

Choyamba sinthani hashi yamalonda:

c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279

Madivelopa atha kuchita izi pogwiritsa ntchito Hex Number(pang'ono endian)Nambala chida chosinthira choperekedwa ndi SmartX.

Kenako sinthani zotsatirazo kukhala mtundu wa 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}

Izi zitha kuchitika pogwiritsa ntchito chida chosinthira cha String Byte Array choperekedwa ndi SmartX. Pomaliza, sinthani mzere wotsatira kukhala chingwe monga chonchi:

xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f

Pansipa pali chitsanzo cha ntchito ya GetTransactionByHash, yomwe imatenga ndalama ndi hashi yogulitsa:

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 imagwiritsidwa ntchito kupeza kutalika kwa msikawo pogwiritsa ntchito hashi yobwereketsa. Tiyeni titenge hashi kuchokera ku chitsanzo pamwambapa:

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

GetContract

Madivelopa atha kugwiritsa ntchito GetContract kuti atengenso mgwirizano kudzera pa hashi ya mgwirizano. Njira yosinthira hashi ya mgwirizano ndi yofanana ndi njira yosinthira hashi yomwe tatchula pamwambapa.

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 imagwiritsidwa ntchito kupeza chipika. Pali njira ziwiri zopezera chipika chapadera.

1. Pezani chipika potengera kutalika kwa midadada:

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

2. Pezani chipika ndi block hash:

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

2. Momwe mungagwiritsire ntchito Block API

Pali ntchito zitatu zomwe zikupezeka mu Block API: GetTransactions, GetTransactionCountndi GetTransactionByIndex. Tidzawakonza mmodzimmodzi.

GetTransactionCount

GetTransactionCount imagwiritsidwa ntchito kupeza kuchuluka kwazomwe zachitika pa block yomwe wapatsidwa.

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

Madivelopa atha kugwiritsa ntchito GetTransactions ntchito kuti atengenso zochitika zonse mu block yoperekedwa.

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 imagwiritsidwa ntchito kubweza zomwe zachitika mu block yomwe wapatsidwa.

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

Kalozera wathunthu angapezeke wathu GitHub.

Pambuyo pake

Blockchain & Block API ndi gawo lofunikira kwambiri pamakontrakitala anzeru chifukwa mutha kuyigwiritsa ntchito kufunsira deta ya blockchain ndikutsekereza deta mumakontrakitala anzeru. M'nkhani zamtsogolo, tikambirana momwe tingagwiritsire ntchito ma API otsala ndikumvetsetsa momwe amalumikizirana ndi blockchain ya Ontology.

Nkhaniyi idamasuliridwa ndi akonzi a Hashrate&Shares makamaka a OntologyRussia. kulira

Kodi ndinu wopanga mapulogalamu? Lowani nawo gulu lathu laukadaulo ku Kusamvana. Komanso, yang'anani Developer Center patsamba lathu, komwe mungapeze zida zamapulogalamu, zolemba, ndi zina zambiri.

Ontology

Source: www.habr.com

Kuwonjezera ndemanga