លំហសិក្សាកម្ពុជា លំហសិក្សាកម្ពុជា V1.0
ចូល ចុះឈ្មោះ

មេរៀនទី ៦៖ ការបង្កើត Blockchain សាមញ្ញក្នុង Python Console

មូលសង្ខេប / Abstract

មេរៀនទី ៦ នេះផ្ដោតលើការបង្កើត Blockchain សាមញ្ញក្នុង Python Console ដោយយកចំណេះដឹងពីមេរៀនមុនៗមកភ្ជាប់ជាប្រព័ន្ធតែមួយ។ នៅមេរៀនទី ៥ និស្សិតបានរៀនពី Transaction Structure និង Block Structure។ ក្នុងមេរៀននេះ និស្សិតនឹងរៀនបង្កើត Genesis Block, បន្ថែម Block ថ្មី, ភ្ជាប់ Block ដោយប្រើ previous_hash, បង្កើត current hash, រក្សាទុក Blocks ក្នុង Python List និងបង្ហាញ Blockchain នៅក្នុង Console។ Blockchain ត្រូវបានពន្យល់ដោយ NIST ថាជា Ledger ដែលមាន Blocks ភ្ជាប់គ្នាតាម Cryptographic Hashes និងអាចជួយឱ្យការកែប្រែទិន្នន័យត្រូវបានរកឃើញ [1]។ ក្នុងមេរៀននេះ និស្សិតនឹងប្រើ Python modules ដូចជា hashlib, json និង datetime ដើម្បីបង្កើត Hash និង Timestamp សម្រាប់ Block។ Python hashlib ផ្ដល់ interface សម្រាប់ secure hashes ដូចជា SHA-256 [2] ហើយ json.dumps(..., sort_keys=True) ជួយ serialize dictionary ដោយមានលំដាប់ key ថេរ មុនធ្វើ hashing [3]។ មេរៀននេះជាជំហានសំខាន់មុនទៅកាន់មេរៀនទី ៧ ដែលនឹងបន្តទៅ CSV-Based Blockchain Storage។

ពាក្យគន្លឹះ

១. ព័ត៌មានទូទៅអំពីមេរៀន

ចំណុចព័ត៌មាន
មុខវិជ្ជាBlockchain Technology Using Python Console and CSV
មេរៀនមេរៀនទី ៦
ចំណងជើងBuilding a Simple Blockchain in Python Console
ប្រធានបទសំខាន់Genesis Block, Add Block, Previous Hash Linking, View Blockchain
កម្រិតនិស្សិតBeginner to Intermediate
ភាសាសរសេរកម្មវិធីPython
ប្រភេទកម្មវិធីConsole Application
Python Moduleshashlib, json, datetime
លទ្ធផលរំពឹងទុកនិស្សិតអាចបង្កើត Blockchain សាមញ្ញដែលមាន Blocks ជាច្រើន និងភ្ជាប់គ្នាតាម Previous Hash
ទិសដៅ ProjectConsole-Based Blockchain Student Certificate Verification System

២. គោលបំណងសិក្សា

បន្ទាប់ពីបញ្ចប់មេរៀននេះ និស្សិតគួរអាច៖

  1. ពន្យល់បានថា Blockchain List គឺជាអ្វី។
  2. ពន្យល់បានថា Genesis Block មានន័យដូចម្តេច។
  3. បង្កើត Genesis Block ដោយប្រើ Python Dictionary។
  4. បង្កើត Function generate_hash(data) សម្រាប់ Block Hash។
  5. បង្កើត Function create_genesis_block().
  6. បង្កើត Function create_block(index, transactions, previous_hash).
  7. បន្ថែម Blocks ថ្មីទៅក្នុង Blockchain List។
  8. ភ្ជាប់ Block ថ្មីទៅ Block មុនដោយប្រើ previous_hash.
  9. បង្ហាញ Blockchain ទាំងមូលនៅក្នុង Console។
  10. ពន្យល់បានថា ការភ្ជាប់ Blocks ជួយ Data Integrity និង Tamper Detection យ៉ាងដូចម្តេច។

៣. ទំនាក់ទំនងមេរៀនទី៦ជាមួយ Final Project

នៅមេរៀនទី ៥ និស្សិតបានរៀនថា Block មួយមាន Fields សំខាន់ៗ៖

index, timestamp, transactions, previous_hash, nonce, hash

ក្នុងមេរៀនទី ៦ នេះ យើងនឹងយក Block Structure នោះមកបង្កើតជា Blockchain ពិតប្រាកដក្នុង Python Console។ Blockchain របស់យើងនឹងត្រូវរក្សាទុកជា List មួយ៖

blockchain = []

List នេះនឹងរក្សាទុក Blocks ជាច្រើនតាមលំដាប់៖

Genesis Block → Block 1 → Block 2 → Block 3 → ...

Flow របស់មេរៀននេះគឺ៖

Create Genesis Block
↓
Prepare Certificate Transactions
↓
Create New Block
↓
Use Previous Block Hash
↓
Add New Block to Blockchain List
↓
View Blockchain in Console

Figure 1. From Block Structure to Blockchain List

រូបភាពនេះគួរបង្ហាញថា Block Structure ដែលបានរៀននៅមេរៀនទី ៥ ត្រូវបានដាក់ចូលទៅក្នុង Python List មួយដែលហៅថា blockchain។ List នេះរក្សាទុក Genesis Block និង Blocks ថ្មីៗតាមលំដាប់។ វាជួយឱ្យនិស្សិតយល់ថា Blockchain ក្នុងកម្រិត Beginner អាចចាប់ផ្ដើមពី List of Blocks មួយបាន។


៤. តើ Blockchain List គឺជាអ្វី?

ក្នុង Python យើងអាចចាប់ផ្ដើម Blockchain ដោយប្រើ List។ List នេះនឹងរក្សាទុក Block Dictionaries ម្តងមួយៗ។ ឧទាហរណ៍៖

blockchain = []

នៅពេលបង្កើត Genesis Block រួច យើងអាចបន្ថែមវាចូលទៅក្នុង List៖

blockchain.append(genesis_block)

បន្ទាប់មក Block ថ្មីៗក៏អាចបន្ថែមតាមរបៀបដូចគ្នា៖

blockchain.append(new_block)

ហេតុអ្វីប្រើ List?

មូលហេតុការពន្យល់
ងាយស្រួលសម្រាប់ BeginnerList ងាយយល់ និងងាយប្រើ
រក្សាទុក Blocks តាមលំដាប់Blockchain ត្រូវការលំដាប់ Block
អាចយក Block ចុងក្រោយបានងាយប្រើ blockchain[-1]
អាច Loop មើល Block ទាំងអស់ប្រើ for block in blockchain
អាចបន្តទៅ CSV Storageនៅមេរៀនទី៧ អាច Save List of Blocks ទៅ CSV

៥. Genesis Block

៥.១ តើ Genesis Block គឺជាអ្វី?

Genesis Block គឺជា Block ដំបូងបំផុតក្នុង Blockchain។ វាមានសារៈសំខាន់ ព្រោះ Blockchain ត្រូវការចំណុចចាប់ផ្ដើម។ Block ថ្មីៗទាំងអស់នឹងភ្ជាប់ទៅ Block មុន តែ Genesis Block គ្មាន Block មុនទេ។

ដូច្នេះ previous_hash របស់ Genesis Block ត្រូវកំណត់ជា៖

"previous_hash": "0"

NIST ពន្យល់ថា Blocks ត្រូវបានភ្ជាប់តាម cryptographic hashes ដែលជួយបង្កើត chain structure និង tamper-evident records [1]។ Genesis Block គឺជាចំណុចចាប់ផ្ដើមនៃ chain structure នោះ។

៥.២ Genesis Block Structure

genesis_block = {
    "index": 0,
    "timestamp": "2026-08-25 23:00:00",
    "transactions": [],
    "previous_hash": "0",
    "nonce": 0,
    "hash": ""
}

Figure 2. Genesis Block as the First Block

រូបភាពនេះគួរបង្ហាញ Genesis Block ជា Block ដំបូងក្នុង Blockchain ដែលមាន index = 0 និង previous_hash = 0។ វាជួយឱ្យនិស្សិតយល់ថា Blockchain ត្រូវការចំណុចចាប់ផ្ដើមមួយ មុនពេលបន្ថែម Blocks ថ្មីៗ។


៦. Generate Hash Function

ដើម្បីបង្កើត Block Hash យើងប្រើ Function មួយឈ្មោះ generate_hash(data)។ Function នេះត្រូវបម្លែង Dictionary ទៅជា JSON String ជាមុន ដោយប្រើ json.dumps(data, sort_keys=True) ហើយបន្ទាប់មកប្រើ SHA-256។

import hashlib
import json

def generate_hash(data):
    data_string = json.dumps(data, sort_keys=True)
    return hashlib.sha256(data_string.encode("utf-8")).hexdigest()

ការពន្យល់

  • json.dumps(data, sort_keys=True) បម្លែង Dictionary ទៅ JSON string និងតម្រៀប keys ឱ្យមានលំដាប់ថេរ។
  • encode("utf-8") បម្លែង string ទៅ bytes។
  • hashlib.sha256(...) បង្កើត SHA-256 hash object។
  • .hexdigest() បង្ហាញ hash ជា hexadecimal string។

Python hashlib documentation បង្ហាញថា sha256() គឺជាមុខងារមួយក្នុង secure hash algorithms [2]។ Python json documentation បញ្ជាក់ថា sort_keys=True នឹងធ្វើឱ្យ dictionary output ត្រូវបាន sort តាម keys [3]។


៧. Practical Lab 1: Create Genesis Block

គោលបំណង

បង្កើត Genesis Block ក្នុង Python Console។

Source Code

from datetime import datetime
import hashlib
import json

def generate_hash(data):
    data_string = json.dumps(data, sort_keys=True)
    return hashlib.sha256(data_string.encode("utf-8")).hexdigest()

def create_genesis_block():
    block = {
        "index": 0,
        "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        "transactions": [],
        "previous_hash": "0",
        "nonce": 0
    }

    block["hash"] = generate_hash(block)
    return block

genesis_block = create_genesis_block()

print("=== Genesis Block ===")
print(genesis_block)

ការពន្យល់

  • create_genesis_block() បង្កើត Block ដំបូង។
  • index = 0 បង្ហាញថា Block នេះជាដំបូង។
  • transactions = [] មានន័យថា Genesis Block មិនទាន់មាន Certificate Transactions។
  • previous_hash = "0" ព្រោះមិនមាន Block មុន។
  • block["hash"] = generate_hash(block) បង្កើត Hash សម្រាប់ Genesis Block។

Figure 3. Creating Genesis Block in Python

រូបភាពនេះគួរបង្ហាញ Python Console ដែលបង្កើត Genesis Block ហើយបង្ហាញ Fields ដូចជា Index, Timestamp, Previous Hash និង Hash។ វាជួយឱ្យនិស្សិតឃើញថា Genesis Block អាចបង្កើតបានដោយ Python Dictionary និង Hash Function។


៨. Create New Block Function

បន្ទាប់ពីមាន Genesis Block យើងអាចបង្កើត Block ថ្មី។ Block ថ្មីត្រូវការទិន្នន័យ ៣ សំខាន់៖

  1. index
  2. transactions
  3. previous_hash

Source Code

def create_block(index, transactions, previous_hash):
    block = {
        "index": index,
        "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        "transactions": transactions,
        "previous_hash": previous_hash,
        "nonce": 0
    }

    block["hash"] = generate_hash(block)
    return block

ការពន្យល់

Function នេះអាចប្រើសម្រាប់បង្កើត Block ថ្មីៗជាច្រើន។ វាប្រើ previous_hash ដើម្បីភ្ជាប់ទៅ Block មុន។ Hash របស់ Block ថ្មីត្រូវបានគណនាដោយប្រើ Fields ទាំងអស់ក្នុង Block។


៩. Add Block to Blockchain

៩.១ Concept

នៅពេល Blockchain មាន Genesis Block រួច យើងអាចបន្ថែម Block ថ្មី។ ដើម្បីភ្ជាប់ Block ថ្មីទៅ Block មុន យើងត្រូវយក Hash របស់ Block ចុងក្រោយ៖

previous_block = blockchain[-1]
previous_hash = previous_block["hash"]

បន្ទាប់មកបង្កើត Block ថ្មី ហើយបន្ថែមទៅ List៖

new_block = create_block(len(blockchain), transactions, previous_hash)
blockchain.append(new_block)

Figure 4. Adding a New Block Using Previous Hash

រូបភាពនេះគួរបង្ហាញថា Block ថ្មីយក Hash របស់ Block ចុងក្រោយមកដាក់ជា previous_hash។ វាជួយឱ្យនិស្សិតយល់ពីរបៀបដែល Blockchain ភ្ជាប់ Blocks ជាសង្វាក់។


១០. Practical Lab 2: Create Blockchain with Two Blocks

គោលបំណង

បង្កើត Blockchain ដែលមាន Genesis Block និង Block ថ្មីមួយ។

Source Code

from datetime import datetime
import hashlib
import json

blockchain = []

def generate_hash(data):
    data_string = json.dumps(data, sort_keys=True)
    return hashlib.sha256(data_string.encode("utf-8")).hexdigest()

def create_genesis_block():
    block = {
        "index": 0,
        "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        "transactions": [],
        "previous_hash": "0",
        "nonce": 0
    }

    block["hash"] = generate_hash(block)
    return block

def create_block(index, transactions, previous_hash):
    block = {
        "index": index,
        "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        "transactions": transactions,
        "previous_hash": previous_hash,
        "nonce": 0
    }

    block["hash"] = generate_hash(block)
    return block

blockchain.append(create_genesis_block())

transactions = [
    {
        "transaction_id": "TX-0001",
        "certificate_id": "CERT-FDE-001",
        "student_name": "Sok Dara",
        "program": "Digital Economy",
        "certificate_title": "Blockchain Technology Completion",
        "issue_date": "2026-08-25",
        "status": "Valid"
    }
]

previous_hash = blockchain[-1]["hash"]
new_block = create_block(len(blockchain), transactions, previous_hash)
blockchain.append(new_block)

print("=== Blockchain ===")
for block in blockchain:
    print(block)

ការពន្យល់

  • blockchain = [] បង្កើត List ទទេ។
  • blockchain.append(create_genesis_block()) បន្ថែម Genesis Block។
  • transactions គឺជា List of Certificate Transactions។
  • previous_hash = blockchain[-1]["hash"] យក Hash របស់ Block ចុងក្រោយ។
  • create_block(...) បង្កើត Block ថ្មី។
  • blockchain.append(new_block) បន្ថែម Block ថ្មីទៅ Blockchain។

១១. View Blockchain Function

នៅពេល Blockchain មាន Blocks ច្រើន យើងត្រូវមាន Function សម្រាប់បង្ហាញ Blockchain ឱ្យអានងាយ។

def view_blockchain():
    print("\n=== Blockchain Records ===")

    for block in blockchain:
        print("\n-----------------------------")
        print("Block Index:", block["index"])
        print("Timestamp:", block["timestamp"])
        print("Previous Hash:", block["previous_hash"])
        print("Current Hash:", block["hash"])
        print("Nonce:", block["nonce"])
        print("Transactions:")

        for tx in block["transactions"]:
            print("  - Transaction ID:", tx["transaction_id"])
            print("    Certificate ID:", tx["certificate_id"])
            print("    Student Name:", tx["student_name"])
            print("    Status:", tx["status"])

Figure 5. Viewing Blockchain Records in Console

រូបភាពនេះគួរបង្ហាញ Console Output ដែលបង្ហាញ Block Index, Timestamp, Previous Hash, Current Hash និង Transactions។ វាជួយឱ្យនិស្សិតយល់ថា Blockchain អាចត្រូវបានបង្ហាញជា Text-Based Records ក្នុង Console។


១២. Practical Lab 3: Blockchain with Multiple Blocks

គោលបំណង

បង្កើត Blockchain ដែលមាន Genesis Block និង Blocks ថ្មីចំនួន ៣។

Source Code

from datetime import datetime
import hashlib
import json

blockchain = []

def generate_hash(data):
    data_string = json.dumps(data, sort_keys=True)
    return hashlib.sha256(data_string.encode("utf-8")).hexdigest()

def create_genesis_block():
    block = {
        "index": 0,
        "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        "transactions": [],
        "previous_hash": "0",
        "nonce": 0
    }

    block["hash"] = generate_hash(block)
    return block

def create_block(index, transactions, previous_hash):
    block = {
        "index": index,
        "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        "transactions": transactions,
        "previous_hash": previous_hash,
        "nonce": 0
    }

    block["hash"] = generate_hash(block)
    return block

def add_block(transactions):
    previous_block = blockchain[-1]
    new_block = create_block(len(blockchain), transactions, previous_block["hash"])
    blockchain.append(new_block)

def view_blockchain():
    print("\n=== Blockchain Records ===")

    for block in blockchain:
        print("\n-----------------------------")
        print("Block Index:", block["index"])
        print("Timestamp:", block["timestamp"])
        print("Previous Hash:", block["previous_hash"])
        print("Current Hash:", block["hash"])
        print("Nonce:", block["nonce"])
        print("Transactions:", block["transactions"])

blockchain.append(create_genesis_block())

add_block([
    {
        "transaction_id": "TX-0001",
        "certificate_id": "CERT-FDE-001",
        "student_name": "Sok Dara",
        "program": "Digital Economy",
        "certificate_title": "Blockchain Technology Completion",
        "issue_date": "2026-08-25",
        "status": "Valid"
    }
])

add_block([
    {
        "transaction_id": "TX-0002",
        "certificate_id": "CERT-FDE-002",
        "student_name": "Chan Vicheka",
        "program": "Information Technology",
        "certificate_title": "Python Programming Completion",
        "issue_date": "2026-08-26",
        "status": "Valid"
    }
])

add_block([
    {
        "transaction_id": "TX-0003",
        "certificate_id": "CERT-FDE-003",
        "student_name": "Kim Sopheak",
        "program": "Data Science",
        "certificate_title": "Data Science Basics Completion",
        "issue_date": "2026-08-27",
        "status": "Revoked"
    }
])

view_blockchain()

១៣. Basic Chain Check

មុននឹងទៅ Validation ពេញលេញនៅមេរៀនក្រោយៗ យើងអាចពិនិត្យ Chain សាមញ្ញដោយមើលថា៖

current block previous_hash == previous block hash

Source Code

def check_chain_links():
    for i in range(1, len(blockchain)):
        current_block = blockchain[i]
        previous_block = blockchain[i - 1]

        if current_block["previous_hash"] == previous_block["hash"]:
            print(f"Block {i} link is valid.")
        else:
            print(f"Block {i} link is invalid.")

ការពន្យល់

Function នេះពិនិត្យពី Block 1 ដល់ Block ចុងក្រោយ។ វាមិនពិនិត្យ Hash ទាំងមូលនៅឡើយទេ ប៉ុន្តែវាជួយឱ្យនិស្សិតយល់ពី Previous Hash Linking ជាមុន។

Figure 6. Checking Previous Hash Links

រូបភាពនេះគួរបង្ហាញការប្រៀបធៀប current_block["previous_hash"] ជាមួយ previous_block["hash"]។ ប្រសិនបើតម្លៃទាំងពីរដូចគ្នា Link គឺ Valid។ ប្រសិនបើមិនដូចគ្នា Link គឺ Invalid។


១៤. Complete Console Blockchain Demo

Source Code

from datetime import datetime
import hashlib
import json

blockchain = []

def generate_hash(data):
    data_string = json.dumps(data, sort_keys=True)
    return hashlib.sha256(data_string.encode("utf-8")).hexdigest()

def create_genesis_block():
    block = {
        "index": 0,
        "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        "transactions": [],
        "previous_hash": "0",
        "nonce": 0
    }

    block["hash"] = generate_hash(block)
    return block

def create_block(index, transactions, previous_hash):
    block = {
        "index": index,
        "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        "transactions": transactions,
        "previous_hash": previous_hash,
        "nonce": 0
    }

    block["hash"] = generate_hash(block)
    return block

def add_block(transactions):
    previous_block = blockchain[-1]
    new_block = create_block(len(blockchain), transactions, previous_block["hash"])
    blockchain.append(new_block)
    print("New block added successfully.")

def view_blockchain():
    print("\n=== Blockchain Records ===")

    for block in blockchain:
        print("\n-----------------------------")
        print("Block Index:", block["index"])
        print("Timestamp:", block["timestamp"])
        print("Previous Hash:", block["previous_hash"])
        print("Current Hash:", block["hash"])
        print("Nonce:", block["nonce"])
        print("Transactions:", block["transactions"])

def check_chain_links():
    print("\n=== Chain Link Check ===")

    for i in range(1, len(blockchain)):
        current_block = blockchain[i]
        previous_block = blockchain[i - 1]

        if current_block["previous_hash"] == previous_block["hash"]:
            print(f"Block {i} link is valid.")
        else:
            print(f"Block {i} link is invalid.")

blockchain.append(create_genesis_block())

add_block([
    {
        "transaction_id": "TX-0001",
        "certificate_id": "CERT-FDE-001",
        "student_name": "Sok Dara",
        "program": "Digital Economy",
        "certificate_title": "Blockchain Technology Completion",
        "issue_date": "2026-08-25",
        "status": "Valid"
    }
])

add_block([
    {
        "transaction_id": "TX-0002",
        "certificate_id": "CERT-FDE-002",
        "student_name": "Chan Vicheka",
        "program": "Information Technology",
        "certificate_title": "Python Programming Completion",
        "issue_date": "2026-08-26",
        "status": "Valid"
    }
])

view_blockchain()
check_chain_links()

Figure 7. Complete Simple Blockchain in Python Console

រូបភាពនេះគួរបង្ហាញ Console Application ដែលបង្កើត Genesis Block, បន្ថែម Blocks ថ្មី, បង្ហាញ Blockchain និងពិនិត្យ Chain Links។ វាជួយបង្ហាញថា Blockchain សាមញ្ញអាចបង្កើតបានដោយប្រើ Python List, Dictionary និង Hash Function។


១៥. ការពិភាក្សាក្នុងថ្នាក់

គ្រូអាចប្រើសំណួរខាងក្រោម៖

  1. ហេតុអ្វី Blockchain ត្រូវមាន Genesis Block?
  2. តើ previous_hash ក្នុង Block ថ្មីយកតម្លៃពីណា?
  3. ហេតុអ្វីយើងប្រើ blockchain[-1]?
  4. តើ Hash របស់ Block មានតួនាទីអ្វី?
  5. តើអ្វីកើតឡើង បើកែ Data ក្នុង Block មុន?
  6. តើ Blockchain List ខុសពី List of Transactions យ៉ាងដូចម្តេច?
  7. តើមេរៀននេះត្រៀមយើងទៅរក CSV-Based Blockchain Storage យ៉ាងដូចម្តេច?

១៦. Reflection Exercise

សរសេរចម្លើយខ្លី ៥–៧ បន្ទាត់៖

ហេតុអ្វី Genesis Block និង Previous Hash មានសារៈសំខាន់ក្នុងការបង្កើត Blockchain?

ចម្លើយគួររួមបញ្ចូលគំនិត៖

  • Genesis Block ជាចំណុចចាប់ផ្ដើម។
  • Previous Hash ភ្ជាប់ Block ថ្មីទៅ Block មុន។
  • Chain Structure កើតឡើងពីការភ្ជាប់ Hash។
  • ប្រសិនបើ Block មុនត្រូវបានកែ Previous Hash Link នឹងមិនត្រឹមត្រូវ។
  • វាជួយបង្កើត Tamper-Evident Records។

១៧. លំហាត់អនុវត្ត

លំហាត់ទី១៖ Create Genesis Block

សរសេរ Function create_genesis_block() ដែលបង្កើត Block ដំបូងដែលមាន៖

index = 0
transactions = []
previous_hash = "0"
nonce = 0
hash = generated hash

លំហាត់ទី២៖ Create New Block

សរសេរ Function create_block(index, transactions, previous_hash) ដើម្បីបង្កើត Block ថ្មី។


លំហាត់ទី៣៖ Add Blocks to Blockchain

បង្កើត blockchain = [] ហើយបន្ថែម Genesis Block និង Blocks ថ្មីចំនួន ៣។


លំហាត់ទី៤៖ View Blockchain

សរសេរ Function view_blockchain() ដើម្បីបង្ហាញ Blocks ទាំងអស់ក្នុង Console។


លំហាត់ទី៥៖ Check Previous Hash Links

សរសេរ Function check_chain_links() ដើម្បីពិនិត្យថា previous_hash របស់ Block បច្ចុប្បន្នត្រូវគ្នាជាមួយ hash របស់ Block មុនឬអត់។


លំហាត់ទី៦៖ Add Multiple Transactions in One Block

បង្កើត Block មួយដែលមាន Certificate Transactions យ៉ាងតិច ២។


១៨. កិច្ចការផ្ទះ

បង្កើតកម្មវិធី Python Console មួយឈ្មោះ៖

simple_blockchain_demo.py

កម្មវិធីត្រូវមាន៖

  1. generate_hash(data)
  2. create_genesis_block()
  3. create_block(index, transactions, previous_hash)
  4. add_block(transactions)
  5. view_blockchain()
  6. check_chain_links()
  7. Genesis Block មួយ
  8. Blocks ថ្មីយ៉ាងតិច ៣
  9. Certificate Transactions យ៉ាងតិច ៥
  10. Comment ពន្យល់កូដសំខាន់ៗ
  11. Console Output ដែលបង្ហាញ Blockchain និង Chain Link Status

១៩. លទ្ធផលដែលនិស្សិតគួរទទួលបាន

បន្ទាប់ពីបញ្ចប់មេរៀនទី ៦ និស្សិតគួរមាន៖

  1. ការយល់ដឹងពី Blockchain List។
  2. ការយល់ពី Genesis Block។
  3. ការយល់ពីការបន្ថែម Block ថ្មី។
  4. ការយល់ពី Previous Hash Linking។
  5. ការបង្កើត Blockchain សាមញ្ញក្នុង Python Console។
  6. ការបង្ហាញ Blockchain Records ក្នុង Console។
  7. ការពិនិត្យ Chain Links ជាមូលដ្ឋាន។
  8. មូលដ្ឋានសម្រាប់បន្តទៅមេរៀនទី៧៖ CSV-Based Blockchain Storage។

២០. Notes

មេរៀននេះ គឺជាជំហានដំបូងដែលនិស្សិតបានបង្កើត Blockchain ពិតប្រាកដក្នុង Python Console។ ទោះបីវាមិនទាន់ជា Distributed Blockchain ឬ Public Blockchain ក៏ដោយ វាមានគំនិតស្នូលដូចជា Genesis Block, Block Hash, Previous Hash និង Chain Linking។

Live Demo ដោយបោះពុម្ព Hash របស់ Block 0 ហើយបង្ហាញថា Block 1 យក Hash នោះទៅដាក់ជា previous_hash។ វាជាវិធីងាយបំផុតឱ្យនិស្សិតមើលឃើញ Blockchain Linking ជាក់ស្តែង។


២១. ការណែនាំរូបភាពក្នុងមេរៀនទី ៦

Figureកន្លែងបញ្ចូលគោលបំណង
Figure 1ផ្នែក ៣បង្ហាញពី Block Structure ទៅ Blockchain List
Figure 2ផ្នែក ៥.២បង្ហាញ Genesis Block ជា Block ដំបូង
Figure 3ផ្នែក ៧បង្ហាញការបង្កើត Genesis Block ក្នុង Python
Figure 4ផ្នែក ៩.១បង្ហាញការបន្ថែម Block ថ្មីដោយប្រើ Previous Hash
Figure 5ផ្នែក ១១បង្ហាញការមើល Blockchain Records ក្នុង Console
Figure 6ផ្នែក ១៣បង្ហាញការពិនិត្យ Previous Hash Links
Figure 7ផ្នែក ១៤បង្ហាញ Complete Simple Blockchain in Console

២២. IEEE-Style References

[1] D. Yaga, P. Mell, N. Roby, and K. Scarfone, “Blockchain Technology Overview,” National Institute of Standards and Technology, NISTIR 8202, 2018. doi: 10.6028/NIST.IR.8202.

[2] Python Software Foundation, “hashlib — Secure hashes and message digests,” Python 3 Documentation, 2026.

[3] Python Software Foundation, “json — JSON encoder and decoder,” Python 3 Documentation, 2026.

ចែករំលែក
រក្សាទុក
ការផ្សព្វផ្សាយ
ការផ្សព្វផ្សាយ
មតិយោបល់ 0
ចូលគណនី ដើម្បីបញ្ចេញមតិ