วิธีเขียนสัญญาอัจฉริยะใน Python บนเครือข่าย Ontology ส่วนที่ 1: Blockchain & Block API

วิธีเขียนสัญญาอัจฉริยะใน Python บนเครือข่าย Ontology ส่วนที่ 1: Blockchain & Block API

นี่เป็นส่วนแรกของชุดบทช่วยสอนเกี่ยวกับการสร้างสัญญาอัจฉริยะ Python บนเครือข่ายบล็อกเชน Ontology โดยใช้เครื่องมือการพัฒนาสัญญาอัจฉริยะ SmartX.

ในบทความนี้ เราจะเริ่มทำความรู้จักกับ API สัญญาอัจฉริยะของ Ontology API สัญญาอัจฉริยะของ Ontology แบ่งออกเป็น 7 โมดูล:

  1. บล็อกเชนและบล็อก API
  2. รันไทม์ API,
  3. API การจัดเก็บข้อมูล,
  4. API ดั้งเดิม
  5. อัปเกรด API
  6. Execution Engine API และ
  7. API การโทรแบบคงที่และแบบไดนามิก

Blockchain & Block API เป็นส่วนหลักของระบบสัญญาอัจฉริยะ Ontology Blockchain API รองรับการดำเนินการสืบค้นบล็อกพื้นฐาน เช่น รับความสูงของบล็อกปัจจุบัน ในขณะที่ Block API รองรับการดำเนินการสืบค้นบล็อกพื้นฐาน เช่น การสืบค้นจำนวนธุรกรรมสำหรับบล็อกที่กำหนด

มาเริ่มกันเลย!

ขั้นแรกให้สร้างสัญญาใหม่ใน SmartXจากนั้นทำตามคำแนะนำด้านล่าง

1. วิธีการใช้งาน Blockchain API

ลิงก์ไปยังฟังก์ชันสัญญาอัจฉริยะจะเหมือนกับลิงก์ Python คุณสามารถป้อนฟังก์ชันที่เกี่ยวข้องได้ตามต้องการ ตัวอย่างเช่น คำสั่งต่อไปนี้จะแนะนำฟังก์ชัน 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

รับธุรกรรมโดยHash

GetTransactionByHash ใช้เพื่อรับธุรกรรมผ่านแฮชของธุรกรรม แฮชธุรกรรมถูกส่งไปยัง รับธุรกรรมโดยHash เป็นพารามิเตอร์ในรูปแบบ bytearray กุญแจสำคัญของฟังก์ชันนี้คือการแปลงแฮชของธุรกรรมในรูปแบบเลขฐานสิบหกให้เป็นแฮชของธุรกรรมในรูปแบบ bytearray นี่เป็นขั้นตอนสำคัญ มิฉะนั้น คุณจะได้รับข้อผิดพลาดที่ระบุว่าไม่มีบล็อกที่มีแฮชของบล็อกนั้น ลองใช้แฮชของธุรกรรมในรูปแบบ hex เป็นตัวอย่างเพื่อแปลงเป็นรูปแบบ bytearray ตัวอย่างมีลักษณะดังนี้:

9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1

ขั้นแรก ให้ย้อนกลับแฮชของธุรกรรม:

c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279

นักพัฒนาสามารถทำขั้นตอนนี้ให้สำเร็จได้โดยใช้เครื่องมือแปลงเลขฐานสิบหก (ตัวเอนตัวน้อย) ที่ SmartX มอบให้

จากนั้นแปลงผลลัพธ์เป็นรูปแบบ 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}

ซึ่งสามารถทำได้โดยใช้เครื่องมือการแปลง String Byte Array ที่ SmartX มอบให้ สุดท้าย แปลง bytearray ที่เป็นผลลัพธ์ให้เป็นสตริงที่คล้ายกัน:

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

รับTransactionHeight

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. วิธีการใช้งาน Block API

Block API มีฟังก์ชันให้เลือกสามฟังก์ชัน: รับธุรกรรม, รับTransactionCountและ รับTransactionByIndex. เราจะแยกพวกมันออกทีละอัน

รับTransactionCount

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

รับTransactionByIndex

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

คุณสามารถดูคำแนะนำฉบับสมบูรณ์ได้จากเรา GitHub.

เล่ม

Blockchain & Block API เป็นส่วนสำคัญของสัญญาอัจฉริยะ เนื่องจากคุณสามารถใช้เพื่อขอข้อมูลบล็อคเชนและบล็อกข้อมูลในสัญญาอัจฉริยะได้ ในบทความต่อไปนี้ เราจะพูดถึงวิธีใช้ API ที่เหลือ และดูว่า API เหล่านี้โต้ตอบกับบล็อกเชน Ontology อย่างไร

บทความนี้แปลโดยบรรณาธิการของ Hashrate&Shares สำหรับ OntologyRussia โดยเฉพาะ คลิก

คุณเป็นนักพัฒนาหรือไม่? เข้าร่วมชุมชนเทคโนโลยีของเราที่ ไม่ลงรอยกัน. นอกจากนี้ ลองดูที่ ศูนย์นักพัฒนา บนเว็บไซต์ของเรา ซึ่งคุณสามารถค้นหาเครื่องมือสำหรับนักพัฒนา เอกสารประกอบ และอื่นๆ อีกมากมาย

อภิปรัชญา

ที่มา: will.com

เพิ่มความคิดเห็น