Mokhoa oa ho ngola konteraka e bohlale ho Python ho netweke ea Ontology. Karolo ea 1: Blockchain & Block API

Mokhoa oa ho ngola konteraka e bohlale ho Python ho netweke ea Ontology. Karolo ea 1: Blockchain & Block API

Ena ke karolo ea pele letotong la lithuto tse mabapi le ho theha likonteraka tse bohlale tsa Python ho Ontology blockchain netweke ho sebelisa sesebelisoa se bohlale sa nts'etsopele ea konteraka. SmartX.

Sengoliloeng sena, re tla qala ho tloaelana le API ea konteraka e bohlale ea Ontology. Ontology smart contract API e arotsoe ka li-module tse 7:

  1. Blockchain & Block API,
  2. Rutime API,
  3. polokelo API,
  4. native API,
  5. Ntlafatsa API,
  6. Execution Engine API le
  7. Static & Dynamic Call API.

Blockchain & Block API ke karolo e ka sehloohong ea tsamaiso ea konteraka e bohlale ea Ontology. Blockchain API e ts'ehetsa ts'ebetso ea lipotso tsa mantlha tsa blockchain, joalo ka ho fumana bophahamo ba hona joale ba block, athe Block API e ts'ehetsa ts'ebetso ea mantlha ea li-block, joalo ka ho botsa palo ea litšebelisano bakeng sa block e fanoeng.

A re qaleng!

Taba ea pele, theha konteraka e ncha ka SmartXebe u latela litaelo tse ka tlase.

1. Mokhoa oa ho sebelisa Blockchain API

Lihokelo tsa mesebetsi ea konteraka e bohlale li ts'oana le lihokelo tsa Python. O ka kenya ditshebetso tse tsamaellanang ha ho hlokahala. Mohlala, polelo e latelang e hlahisa mosebetsi oa GetHeight ho fumana bophahamo ba block ba hajoale, le ts'ebetso ea GetHeader ho fumana hlooho ea block.

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

GetHeight

GetHeight e sebelisoa ho fumana nomoro ea ho qetela ea tatellano ea li-block ho blockchain, joalo ka ha ho bonts'itsoe mohlaleng o ka tlase. Mohlala oa ho qetela, re tla tlohela mosebetsi oa Main bakeng sa boiketlo, empa u ka e eketsa ha ho hlokahala.

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 e sebelisoa ho fumana sehlooho sa block, paramente ke nomoro ea serial ea block ho blockchain. Mohlala:

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

FumanaTransactionByHash

GetTransactionByHash e sebelisetsoa ho fumana transaction ka hash ea transaction. The transaction hash e romelloa ho FumanaTransactionByHash joalo ka liparamente ka sebopeho sa bytearray. Senotlolo sa ts'ebetso ena ke ho fetolela hash ea transaction ka sebopeho sa hex ho hash ea transaction ka sebopeho sa bytearray. Ena ke mohato oa bohlokoa. Ho seng joalo, u tla fumana phoso e bontšang hore ha ho na block e nang le block hash. Ha re nke hash ea transaction ka sebopeho sa hex e le mohlala oa ho e fetolela ho sebopeho sa bytearray. Mohlala o shebahala tjena:

9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1

Taba ea pele, fetola hash ea transaction:

c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279

Bahlahisi ba ka fihlela mohato ona ba sebelisa Hex Number(endian e nyane) sesebelisoa sa ho fetolela Numoro se fanoeng ke SmartX.

Ebe u fetolela sephetho ho sebopeho sa 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}

Sena se ka etsoa ho sebelisa sesebelisoa sa phetoho sa String Byte Array se fanoeng ke SmartX. Qetellong, fetolela bytearray e hlahisoang ho khoele e ts'oanang:

xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f

Se latelang ke mohlala oa ts'ebetso ea GetTransactionByHash, e nkang transaction ho sebelisa hash ea transaction:

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

FumanaTransactionHeight

GetTransactionHeight e sebelisoa ho fumana bophahamo ba transaction ka hash ea transaction. Ha re nke hash mohlaleng o ka holimo:

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

GetContract

Bahlahisi ba ka sebelisa mosebetsi oa GetContract ho fumana konteraka ka hash ea konteraka. Ts'ebetso ea phetoho ea hash ea konteraka e ts'oana le ts'ebetso ea phetoho ea hash e boletsoeng ka holimo.

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 e sebelisoa ho fumana block. Ho na le mekhoa e 'meli ea ho fumana block e khethehileng.

1. Fumana boloko ka bophahamo ba boloko:

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

2. Fumana boloko ka block hash:

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

2. Mokhoa oa ho sebelisa Block API

Ho na le mesebetsi e meraro e fumanehang ho Block API: Fumana Transactions, GetTransactionCountle FumanaTransactionByIndex. Re tla li pshatla ka bonngoe.

GetTransactionCount

GetTransactionCount e sebelisoa ho fumana palo ea litšebelisano bakeng sa block e fanoeng.

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

Fumana Transactions

Bahlahisi ba ka sebelisa ts'ebetso ea GetTransactions ho fumana litšebelisano tsohle sebakeng se fanoeng.

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

FumanaTransactionByIndex

GetTransactionByIndex e sebelisetsoa ho fumana transaction e itseng sebakeng se fanoeng.

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

Tataiso e felletseng e ka fumanoa ho rona GitHub.

Afterword

Blockchain & Block API ke karolo ea bohlokoa ea likonteraka tse bohlale kaha u ka li sebelisa ho kopa data ea blockchain le ho thibela data ho likonteraka tse bohlale. Lihloohong tse latelang, re tla tšohla mokhoa oa ho sebelisa li-API tse setseng le ho fumana hore na li sebelisana joang le Ontology blockchain.

Sengoliloeng se fetoletsoe ke bahlophisi ba Hashrate&Shares haholo-holo bakeng sa OntologyRussia. lla

Na u moqapi? Kena le sechaba sa rona sa theknoloji ho Khahlano. Hape, sheba Setsi sa Moetsi webosaeteng ea rona, moo o ka fumanang lisebelisoa tsa nts'etsopele, litokomane le tse ling.

Ontology

Source: www.habr.com

Eketsa ka tlhaloso