Mokhoa oa ho ngola konteraka e bohlale ho Python ho netweke ea Ontology. Karolo ea 3: Runtime API

Mokhoa oa ho ngola konteraka e bohlale ho Python ho netweke ea Ontology. Karolo ea 3: Runtime API

Ena ke karolo ea 3 letotong la lihlooho tsa thuto mabapi le ho theha likonteraka tse bohlale Python ho Ontology blockchain network. Lihloohong tse fetileng re ile ra tloaelana le tsona

  1. Blockchain & Block API
  2. Storage API.

Kaha joale u na le mohopolo oa ho letsetsa API e tsitsitseng ea polokelo ha u theha konteraka e bohlale u sebelisa Python ho netweke ea Ontology, ha re tsoeleng pele ho ithuta ho sebelisa. Runtime API (Contract Execution API). Runtime API e na le li-API tse 8 tse amanang tse fanang ka likhokahano tse tloaelehileng bakeng sa ho phethahatsa konteraka le ho thusa ba ntlafatsang ho fumana, ho fetola le ho netefatsa lintlha.

Ka tlase ke tlhaloso e khuts'oane ea data ea 8 API:

Mokhoa oa ho ngola konteraka e bohlale ho Python ho netweke ea Ontology. Karolo ea 3: Runtime API

Ha re shebeng ka botebo mokhoa oa ho sebelisa data ea 8 API. Pele ho mona, o ka theha konteraka e ncha ho sesebelisoa sa nts'etsopele ea konteraka e bohlale ea Ontology SmartX 'me u latele litaelo tse ka tlase.

Mokhoa oa ho sebelisa Runtime API

Ho na le mekhoa e 'meli ea ho reka kantle ho naha Runtime API: ontology.interop.System.Runtime и ontology.interop.Ontology.Nako ea ho sebetsa. Tsela ea Ontology e na le li-API tse sa tsoa eketsoa. Methalo e ka tlase e kenya data ea API.

from ontology.interop.System.Runtime import GetTime, CheckWitness, Log, Notify, Serialize, Deserialize
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHash

Notify API

Ts'ebetso ea Notify e hasanya ketsahalo ho pholletsa le netweke. Mohlala o ka tlase, mosebetsi oa Notify o tla khutlisa mohala oa hex "hello word" ebe o o phatlalatsa marang-rang.

from ontology.interop.System.Runtime import Notify
def demo():
    Notify("hello world")

U ka bona sena ho li-logs:

Mokhoa oa ho ngola konteraka e bohlale ho Python ho netweke ea Ontology. Karolo ea 3: Runtime API

GetTime API

Mosebetsi oa GetTime o khutlisa setempe sa nako sa hajoale, se khutlisetsang nako ea Unix eo tšebetso e neng e bitsoa ka eona. Tekanyo ea tekanyo ke ea bobeli.

from ontology.interop.System.Runtime import GetTime
def demo():
    time=GetTime()
    return time # return a uint num

GetCurrentBlockHash API

Mosebetsi oa GetCurrentBlockHash o khutlisa hash ea block ea hajoale.

from ontology.interop.Ontology.Runtime import GetCurrentBlockHash
def demo():
    block_hash = GetCurrentBlockHash()
    return block_hash

Senya le ho hlakola

Ena ke mesebetsi e 'meli ea serialization le deerialization. Sesebediswa sa Serialize se fetolela ntho hore e be ntho ya bytearray, mme tshebetso ya Deserialize e fetolela bytearray ho ntho ya pele. Mohlala oa khoutu o ka tlase o fetola liparamente tse kenang ebe o li boloka polokelong e phehellang ea konteraka. E boetse e khutlisa data ho tsoa polokelong e phehellang ea konteraka ebe e e fetola ntho ea mantlha.

from ontology.interop.System.Runtime import GetTime, CheckWitness, Log, Notify, Serialize, Deserialize
from ontology.interop.System.Storage import Put, Get, GetContext

def Main(operation, args):
    if operation == 'serialize_to_bytearray':
        data = args[0]
        return serialize_to_bytearray(data)
    if operation == 'deserialize_from_bytearray':
        key = args[0]
        return deserialize_from_bytearray(key)
    return False


def serialize_to_bytearray(data):
    sc = GetContext()
    key = "1"
    byte_data = Serialize(data)
    Put(sc, key, byte_data)


def deserialize_from_bytearray(key):
    sc = GetContext()
    byte_data = Get(sc, key)
    data = Deserialize(byte_data)
    return data

Base58ToAddress le AddressToBase58

Liaterese tsena tse peli lia sebetsa. Ts'ebetso ea Base58ToAddress e fetolela aterese e kentsoeng ea base58 ho aterese ea bytearray, mme AddressToBase58 e fetolela aterese ea bytearray ho aterese ea base58 e kentsoeng.

from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58
def demo():
    base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn"
    addr=Base58ToAddress(base58_addr)
    Log(addr)
    base58_addr=AddressToBase58(addr)
    Log(base58_addr)

Hlahloba Paki

Mosebetsi oa CheckWitness(fromAcct) o na le ts'ebetso e 'meli:

  • Netefatsa hore na moletsi oa tšebetso ea hajoale o tsoa hoAcct. Haeba ho joalo (ke hore, netefatso ea signature e fetile), ts'ebetso ea khutla.
  • Sheba hore na ntho e bitsang tšebetso ea hona joale ke konteraka. Haeba e le konteraka mme mosebetsi o phethiloe ho tloha konteraka, joale ho netefatsoa ho fetisitsoe. Ke hore, netefatsa hore na fromAcct ke boleng ba ho khutla ba GetCallingScriptHash(). Mosebetsi oa GetCallingScriptHash () o ka nka boleng ba konteraka ea hash ea konteraka e bohlale ea hona joale.

GetCallingScriptHash():

Ho feta ho Guthub

from ontology.interop.System.Runtime import CheckWitness
from ontology.interop.Ontology.Runtime import Base58ToAddress
def demo():
    addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z")
    res=CheckWitness(addr)
    return res

Lintlha tse ling li ka fumanoa ho Guthub. Sehloohong se latelang re tla kenyelletsa Native APIho ithuta ho fetisa thepa ka likonteraka tse bohlale tsa Ontology.

Sengoliloeng se fetoletsoe ke bahlophisi Hashrate&Shares haholo-holo bakeng sa Ontology Russia.

Na u moqapi? Kena le sechaba sa rona sa theknoloji ho Khahlano. Hape, sheba Setsi sa Moetsi Ontology, o ka fumana lisebelisoa tse ling, litokomane le tse ling tse ngata moo.

Bula mesebetsi bakeng sa bahlahisi. Qetella mosebetsi 'me u fumane moputso.

Etsa kopo bakeng sa lenaneo la talenta ea Ontology bakeng sa baithuti

Ontology

Webosaete ea Ontology - GitHub - Khahlano - Telegram ea Serussia - Twitter - Reddit

Source: www.habr.com

Eketsa ka tlhaloso