Maitiro ekunyora chibvumirano chakangwara muPython pane Ontology network. Chikamu 1: Blockchain & Block API

Maitiro ekunyora chibvumirano chakangwara muPython pane Ontology network. Chikamu 1: Blockchain & Block API

Ichi ndicho chikamu chekutanga chenhevedzano yezvinyorwa zvedzidzo pakugadzira smart zvibvumirano muPython paOntology blockchain network uchishandisa smart contract development tool. SmartX.

Muchikamu chino, tichatanga kuzivana kwedu neOntology smart contract API. Iyo Ontology smart contract API yakakamurwa kuita 7 module:

  1. Blockchain & Block API,
  2. Runtime API
  3. Purogiramu inonzi Storage
  4. Native API
  5. Upgrade API,
  6. Execution Engine API uye
  7. Purogiramu inonzi Static & Dynamic Call.

Blockchain & Block API ndiyo yakakosha chikamu cheOntology smart contract system. Iyo Blockchain API inotsigira zvakakosha zve blockchain query mashandiro, senge kuwana hurefu hwazvino hwebhuroka, nepo Block API inotsigira basic block query mashandiro, senge kubvunza huwandu hwekutengesa kune yakapihwa block.

Ngatitangei!

Kutanga, gadzira kondirakiti itsva mukati SmartX, uye wotevera mirairo iri pasi apa.

1. Mashandisiro eBlockchain API

Smart contract function mareferensi akafanana nePython mareferensi. Unogona kupinda mabasa akakodzera sezvinodiwa. Semuenzaniso, chirevo chinotevera chinosuma GetHeight, basa rekuwana hurefu hwazvino hwebhuroko, uye GetHeader, basa rekutora zita re block.

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

GetHeight

GetHeight inoshandiswa kuwana yekupedzisira block sequence nhamba mu blockchain, sezvakaratidzwa mumuenzaniso pazasi. Mumuenzaniso wekupedzisira, kuitira nyore, isu tichasvetuka iyo Main basa, asi iwe unogona kuiwedzera kana zvichidikanwa.

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 inoshandiswa kuwana iyo block musoro; iyo paramende ndiyo serial nhamba yeblock mu blockchain. Muenzaniso:

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 inoshandiswa kudzoreredza kutengeserana uchishandisa hashi yekutengesa. The transaction hashi inotumirwa kune GetTransactionByHash sema parameter mu bytearray format. Kiyi yebasa iri kushandura iyo transaction hash mune hex fomati kune iyo transaction hash mune bytearray fomati. Iri idanho rinokosha. Zvikasadaro iwe waizowana kukanganisa kunoratidza kuti hapana block ine iyo block hash. Ngatitorei transaction hash mune hex fomati semuenzaniso kuishandura kuita bytearray fomati. Muenzaniso unotaridzika seizvi:

9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1

Kutanga kudzora hashi yekutengeserana:

c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279

Vagadziri vanogona kuita nhanho iyi vachishandisa iyo Hex Nhamba(diki endian)Nhamba yekushandura chishandiso yakapihwa neSmartX.

Wobva washandura mhedzisiro kuita bytearray fomati:

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

Izvi zvinogona kuitwa uchishandisa String Byte Array conversion tool yakapihwa neSmartX. Pakupedzisira, shandura iyo bytearray inoguma kuita tambo seizvi:

xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f

Pazasi pane muenzaniso weiyo GetTransactionByHash basa, inotora kutengeserana neiyo transaction hashi:

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 inoshandiswa kuwana hurefu hwekutengesa uchishandisa hashi yekutengesa. Ngatitorei hashi kubva pamuenzaniso uri pamusoro:

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

GetContract

Vagadziri vanogona kushandisa iyo GetContract basa kudzoreredza kondirakiti kuburikidza nekontrakiti hashi. Kondirakiti hashi yekushandura maitiro akafanana neye transaction hash conversion process yataurwa pamusoro apa.

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 inoshandiswa kuwana block. Pane nzira mbiri dzekuwana chaiyo block.

1. Tora bhuroko nekureba kwebhuroko:

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

2. Tora bhuroko ne block hash:

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

2. Mashandisiro eBlock API

Pane matatu anowanikwa mabasa muBlock API: GetTransactions, GetTransactionCountuye GetTransactionByIndex. Tichazvigadzirisa chimwe nechimwe.

GetTransactionCount

GetTransactionCount inoshandiswa kuwana nhamba yekutengeserana kune yakapihwa block.

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

Vagadziri vanogona kushandisa iyo GetTransactions basa kudzoreredza zvese zvekutengesa mune yakapihwa block.

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 inoshandiswa kudzoreredza imwe kutengeserana mune yakapihwa block.

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

Iyo yakazara dhairekitori inogona kuwanikwa pane yedu GitHub.

Afterword

Blockchain & Block API chikamu chakakosha chezvibvumirano zvakangwara nekuti unogona kuishandisa kubvunza blockchain data uye kuvhara data muzvibvumirano zvakangwara. Muzvinyorwa zvinotevera, tichakurukura mashandisiro asara APIs uye kunzwisisa mabatiro avanoita neOntology blockchain.

Chinyorwa chakaturikirwa nevapepeti veHashrate&Shares kunyanya kuOntologyRussia. kuchema

Iwe uri mugadziri? Joinha tekinoroji yedu munharaunda pa Kurwisana. Uyezve, tarisa Developer Center pawebhusaiti yedu, kwaunogona kuwana maturusi ekuvandudza, zvinyorwa, nezvimwe.

Ontology

Source: www.habr.com

Voeg