Yuav ua li cas sau daim ntawv cog lus ntse hauv Python ntawm Ontology network. Ntu 3: Runtime API

Yuav ua li cas sau daim ntawv cog lus ntse hauv Python ntawm Ontology network. Ntu 3: Runtime API

Qhov no yog qhov thib 3 hauv cov lus qhia txog kev tsim cov ntawv cog lus ntse hauv Python ntawm Ontology blockchain network. Hauv tsab xov xwm dhau los peb tau paub nrog

  1. Blockchain & Block API
  2. Storage API.

Tam sim no koj muaj lub tswv yim ntawm yuav ua li cas hu rau qhov tsim nyog ruaj khov API thaum tsim daim ntawv cog lus ntse siv Python ntawm Ontology network, cia peb txav mus rau kev kawm paub siv. Runtime API (Contract Execution API). Lub Runtime API muaj 8 qhov cuam tshuam APIs uas muab cov kev sib txuas sib txuas rau kev ua tiav daim ntawv cog lus thiab pab cov neeg tsim khoom khaws, hloov pauv, thiab siv tau cov ntaub ntawv.

Hauv qab no yog cov lus piav qhia luv luv ntawm 8 API cov ntaub ntawv:

Yuav ua li cas sau daim ntawv cog lus ntse hauv Python ntawm Ontology network. Ntu 3: Runtime API

Cia peb ua tib zoo saib yuav ua li cas siv 8 API cov ntaub ntawv. Ua ntej no, koj tuaj yeem tsim daim ntawv cog lus tshiab hauv Ontology ntse kev cog lus cov cuab yeej SmartX thiab ua raws li cov lus qhia hauv qab no.

Yuav siv Runtime API li cas

Muaj ob txoj hauv kev los import Runtime API: ontology.interop.System.Runtime и ontology.interop.Ontology.Runtime. Txoj kev Ontology muaj cov APIs tshiab tshiab. Cov kab hauv qab no import cov ntaub ntawv API.

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

Ceeb toom API

Kev ceeb toom muaj nuj nqi tshaj tawm cov xwm txheej thoob plaws hauv lub network. Hauv qhov piv txwv hauv qab no, Kev Ceeb Toom muaj nuj nqi yuav rov qab cov hlua hex "nyob zoo lo lus" thiab tshaj tawm nws thoob plaws hauv lub network.

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

Koj tuaj yeem pom qhov no hauv cov log:

Yuav ua li cas sau daim ntawv cog lus ntse hauv Python ntawm Ontology network. Ntu 3: Runtime API

GetTime API

Lub GetTime muaj nuj nqi xa rov qab lub sijhawm tam sim no, uas xa rov qab Unix lub sijhawm uas lub luag haujlwm raug hu. Chav ntsuas ntsuas yog thib ob.

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

GetCurrentBlockHash API

Lub GetCurrentBlockHash muaj nuj nqi rov qab cov hash ntawm tam sim no thaiv.

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

Serialize thiab Deserialize

Qhov no yog ib khub ntawm serialization thiab deserialization functions. Serialize muaj nuj nqi hloov ib yam khoom mus rau ib yam khoom bytearray, thiab Deserialize muaj nuj nqi converts ib tug bytearray rau cov khoom qub. Cov qauv hauv qab no hloov pauv cov khoom tsis tuaj thiab khaws cia hauv daim ntawv cog lus tsis tu ncua. Nws tseem khaws cov ntaub ntawv los ntawm daim ntawv cog lus tsis tu ncua cia thiab hloov mus rau hauv cov khoom qub.

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 thiab Chaw Nyob ToBase58

Qhov khub ntawm qhov chaw nyob txhais lus ua haujlwm. Lub Base58ToAddress muaj nuj nqi hloov qhov chaw nyob hauv base58 mus rau qhov chaw nyob bytearray, thiab AddressToBase58 hloov qhov chaw nyob bytearray mus rau qhov chaw nyob base58 encoded.

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)

Xyuas Timkhawv

Lub CheckWitness (los ntawm Acct) muaj ob txoj haujlwm:

  • Xyuas seb qhov haujlwm tam sim no tus hu yog los ntawmAcct. Yog tias muaj (uas yog, kos npe pov thawj dhau), cov haujlwm rov qab los.
  • Xyuas seb cov khoom hu ua haujlwm tam sim no puas yog daim ntawv cog lus. Yog tias nws yog daim ntawv cog lus thiab kev ua haujlwm tau ua tiav los ntawm daim ntawv cog lus, ces qhov kev pov thawj dhau lawm. Ntawd yog, txheeb xyuas yog tias los ntawmAcct yog tus nqi rov qab ntawm GetCallingScriptHash(). GetCallingScriptHash() muaj nuj nqi tuaj yeem nqa daim ntawv cog lus hash tus nqi ntawm daim ntawv cog lus ntse tam sim no.

GetCallingScriptHash():

Nyeem ntxiv rau 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

Xav paub ntau ntxiv tuaj yeem nrhiav tau ntawm Guthub. Hauv tsab xov xwm tom ntej no peb yuav qhia Haiv neeg APIkawm paub hloov cov cuab tam hauv Ontology ntse daim ntawv cog lus.

Tsab ntawv tau muab txhais los ntawm cov neeg kho Hashrate & Shares tshwj xeeb tshaj yog rau OntologyRussia.

Koj puas yog tus tsim tawm? Koom nrog peb tech zej zog ntawm Discord. Tsis tas li, ua tib zoo saib Developer Center Ontology, koj tuaj yeem pom ntau yam cuab yeej, cov ntaub ntawv thiab ntau ntxiv nyob ntawd.

Qhib cov haujlwm rau cov neeg tsim khoom. Ua kom tiav txoj haujlwm thiab tau txais nqi zog.

Thov rau Ontology txuj ci program rau cov tub ntxhais kawm

Ontology

Ontology lub website - GitHub - Discord - Telegram Lavxias - Twitter - Reddit

Tau qhov twg los: www.hab.com

Ntxiv ib saib