ఒంటాలజీ నెట్‌వర్క్‌లో పైథాన్‌లో స్మార్ట్ ఒప్పందాన్ని ఎలా వ్రాయాలి. పార్ట్ 1: బ్లాక్‌చెయిన్ & బ్లాక్ API

ఒంటాలజీ నెట్‌వర్క్‌లో పైథాన్‌లో స్మార్ట్ ఒప్పందాన్ని ఎలా వ్రాయాలి. పార్ట్ 1: బ్లాక్‌చెయిన్ & బ్లాక్ API

స్మార్ట్ కాంట్రాక్ట్ డెవలప్‌మెంట్ టూల్‌ని ఉపయోగించి ఒంటాలజీ బ్లాక్‌చెయిన్ నెట్‌వర్క్‌లో పైథాన్ స్మార్ట్ కాంట్రాక్ట్‌లను రూపొందించడంపై ట్యుటోరియల్‌ల శ్రేణిలో ఇది మొదటి భాగం. SmartX.

ఈ కథనంలో, మేము ఒంటాలజీ స్మార్ట్ కాంట్రాక్ట్ APIతో మా పరిచయాన్ని ప్రారంభిస్తాము. ఒంటాలజీ స్మార్ట్ కాంట్రాక్ట్ API 7 మాడ్యూల్‌లుగా విభజించబడింది:

  1. బ్లాక్‌చెయిన్ & బ్లాక్ API,
  2. రన్‌టైమ్ API,
  3. నిల్వ API,
  4. స్థానిక API,
  5. APIని అప్‌గ్రేడ్ చేయండి,
  6. ఎగ్జిక్యూషన్ ఇంజిన్ API మరియు
  7. స్టాటిక్ & డైనమిక్ కాల్ API.

బ్లాక్‌చెయిన్ & బ్లాక్ API అనేది ఒంటాలజీ స్మార్ట్ కాంట్రాక్ట్ సిస్టమ్‌లో ప్రధాన భాగం. Blockchain API ప్రస్తుత బ్లాక్ ఎత్తును పొందడం వంటి ప్రాథమిక బ్లాక్‌చెయిన్ ప్రశ్న కార్యకలాపాలకు మద్దతు ఇస్తుంది, అయితే Block API ఇచ్చిన బ్లాక్ కోసం లావాదేవీల సంఖ్యను ప్రశ్నించడం వంటి ప్రాథమిక బ్లాక్ ప్రశ్న కార్యకలాపాలకు మద్దతు ఇస్తుంది.

ప్రారంభిద్దాం!

మొదట, కొత్త ఒప్పందాన్ని సృష్టించండి SmartXఆపై క్రింది సూచనలను అనుసరించండి.

1. Blockchain APIని ఎలా ఉపయోగించాలి

స్మార్ట్ కాంట్రాక్ట్ ఫంక్షన్‌లకు లింక్‌లు పైథాన్ లింక్‌లకు సమానంగా ఉంటాయి. మీరు అవసరమైన విధంగా సంబంధిత ఫంక్షన్లను నమోదు చేయవచ్చు. ఉదాహరణకు, కింది స్టేట్‌మెంట్ ప్రస్తుత బ్లాక్ ఎత్తును పొందడానికి GetHeight ఫంక్షన్‌ను మరియు బ్లాక్ హెడర్‌ను పొందడానికి GetHeader ఫంక్షన్‌ను పరిచయం చేస్తుంది.

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

గెట్‌హైట్

దిగువ ఉదాహరణలో చూపిన విధంగా బ్లాక్‌చెయిన్‌లో చివరి బ్లాక్ సీక్వెన్స్ నంబర్‌ను పొందడానికి GetHeight ఉపయోగించబడుతుంది. చివరి ఉదాహరణలో, మేము సౌలభ్యం కోసం ప్రధాన విధిని విస్మరిస్తాము, అయితే అవసరమైతే మీరు దానిని జోడించవచ్చు.

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 ఉపయోగించబడుతుంది, పరామితి అనేది బ్లాక్‌చెయిన్‌లోని బ్లాక్ యొక్క క్రమ సంఖ్య. ఉదాహరణ:

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 ఉపయోగించబడుతుంది. లావాదేవీకి హాష్ పంపబడింది GetTransactionByHash bytearray ఆకృతిలో పారామీటర్‌లుగా. ఈ ఫంక్షన్‌కు కీలకం ఏమిటంటే, లావాదేవీ హ్యాష్‌ను హెక్స్ ఫార్మాట్‌లో లావాదేవీ హ్యాష్‌గా బైటియర్రే ఫార్మాట్‌లో మార్చడం. ఇది ఒక ముఖ్యమైన దశ. లేకపోతే, ఆ బ్లాక్ హాష్‌తో బ్లాక్ లేదని సూచించే ఎర్రర్ మీకు వస్తుంది. దీన్ని bytearray ఫార్మాట్‌కి మార్చడానికి ఉదాహరణగా హెక్స్ ఫార్మాట్‌లో లావాదేవీ హాష్‌ని తీసుకుందాం. ఒక ఉదాహరణ ఇలా కనిపిస్తుంది:

9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1

ముందుగా, లావాదేవీ హాష్‌ను రివర్స్ చేయండి:

c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279

స్మార్ట్‌ఎక్స్ అందించిన హెక్స్ నంబర్ (లిటిల్ ఎండియన్) నంబర్ కన్వర్షన్ సాధనాన్ని ఉపయోగించి డెవలపర్‌లు ఈ దశను సాధించగలరు.

ఆపై ఫలితాన్ని 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}

SmartX అందించిన స్ట్రింగ్ బైట్ అర్రే మార్పిడి సాధనాన్ని ఉపయోగించి ఇది చేయవచ్చు. చివరగా, ఫలిత బైటర్‌రేని ఇదే స్ట్రింగ్‌కి మార్చండి:

xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f

లావాదేవీ యొక్క హాష్‌ని ఉపయోగించి లావాదేవీని తీసుకునే GetTransactionByHash ఫంక్షన్‌కి క్రింది ఉదాహరణ:

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

లావాదేవీ ఎత్తు పొందండి

లావాదేవీ హ్యాష్ ద్వారా లావాదేవీ ఎత్తును పొందడానికి GetTransactionHeight ఉపయోగించబడుతుంది. పై ఉదాహరణ నుండి హాష్‌ని తీసుకుందాం:

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

కాంట్రాక్ట్ పొందండి

డెవలపర్‌లు ఒప్పందం యొక్క హాష్ ద్వారా ఒప్పందాన్ని పొందడానికి GetContract ఫంక్షన్‌ని ఉపయోగించవచ్చు. కాంట్రాక్ట్ హాష్ మార్పిడి ప్రక్రియ పైన పేర్కొన్న లావాదేవీ హాష్ మార్పిడి ప్రక్రియ వలె ఉంటుంది.

from ontology.interop.System.Blockchain import GetContract
def demo():
    # contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"    
    contract_hash=bytearray(b"xfax98x96x21x32x32xbbxf0x9fx23x91xfaxefx95x9bxffxa5x75x1axd8")
    contract=GetContract(contract_hash)
    return contract

గెట్‌బ్లాక్

GetBlock బ్లాక్‌ని పొందడానికి ఉపయోగించబడుతుంది. నిర్దిష్ట బ్లాక్ పొందడానికి రెండు మార్గాలు ఉన్నాయి.

1. బ్లాక్ ఎత్తు ద్వారా బ్లాక్ పొందండి:

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

2. బ్లాక్ హాష్ ద్వారా బ్లాక్ పొందండి:

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

2. బ్లాక్ APIని ఎలా ఉపయోగించాలి

బ్లాక్ APIలో మూడు అందుబాటులో ఉన్న ఫంక్షన్‌లు ఉన్నాయి: లావాదేవీలను పొందండి, GetTransactioncountమరియు GetTransactionByIndex. మేము వాటిని ఒక్కొక్కటిగా విచ్ఛిన్నం చేస్తాము.

GetTransactioncount

ఇచ్చిన బ్లాక్ కోసం లావాదేవీల సంఖ్యను పొందడానికి GetTransactionCount ఉపయోగించబడుతుంది.

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 ఫంక్షన్‌ని ఉపయోగించవచ్చు.

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 ఉపయోగించబడుతుంది.

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

పూర్తి గైడ్ మాలో చూడవచ్చు గ్యాలరీలు.

తరువాతి మాట

Blockchain & Block API అనేది స్మార్ట్ కాంట్రాక్ట్‌లలో ఒక అనివార్యమైన భాగం, ఎందుకంటే మీరు బ్లాక్‌చెయిన్ డేటాను అభ్యర్థించడానికి మరియు స్మార్ట్ కాంట్రాక్ట్‌లలో డేటాను బ్లాక్ చేయడానికి వాటిని ఉపయోగించవచ్చు. కింది కథనాలలో, మేము మిగిలిన APIలను ఎలా ఉపయోగించాలో చర్చిస్తాము మరియు అవి ఒంటాలజీ బ్లాక్‌చెయిన్‌తో ఎలా సంకర్షణ చెందుతాయి.

ముఖ్యంగా ఒంటాలజీ రష్యా కోసం హష్రేట్&షేర్స్ సంపాదకులు ఈ కథనాన్ని అనువదించారు. ఏడుస్తారు

మీరు డెవలపర్‌లా? మా సాంకేతిక సంఘంలో చేరండి అసమ్మతి. అలాగే, పరిశీలించండి డెవలపర్ సెంటర్ మా వెబ్‌సైట్‌లో, మీరు డెవలపర్ సాధనాలు, డాక్యుమెంటేషన్ మరియు మరిన్నింటిని కనుగొనవచ్చు.

అస్తిత్వం

మూలం: www.habr.com

ఒక వ్యాఖ్యను జోడించండి