Thuta Learning
Blockchain
ProjectsSecurityအဆင့်မြင့်

Mini Blockchain Project — အပိုင်း ၂: Transaction Pool နဲ့ Mining ထည့်သွင်းခြင်း

Blockchainသင်ခန်းစာ 33

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Mini Blockchain Project — အပိုင်း ၂: Transaction Pool နဲ့ Mining ထည့်သွင်းခြင်း ရဲ့ protocol model, trust assumptions နဲ့failure modes ကိုရှင်းပြနိုင်ရန်
  • Local simulation/testnet တွင် normal, tampered နဲ့failure cases ကိုစမ်းနိုင်ရန်
  • Real keys/funds မသုံးဘဲ security limitations နဲ့safe practice ကိုအသုံးချနိုင်ရန်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

Intermediate chapter မှာ သင်ခဲ့တဲ့ transactions, consensus, pow-pos concept တွေကို ဒီအပိုင်းမှာ code ထဲ တကယ်လုပ်ဆောင်ကြည့်မှာဖြစ်ပါတယ်။ Real network တစ်ခုမှာ user တွေက transaction ကို broadcast လုပ်တယ်၊ miner တွေက ဒီ transaction တွေကို block ထဲစုပြီး Proof-of-Work puzzle ဖြေရှင်းပြီးမှ chain ထဲ confirm ထည့်ပေးတာပါ။ ဒီအပိုင်းမှာ pendingTransactions array တစ်ခု ဖန်တီးပြီး transaction တွေကို ခဏသိမ်းထားမယ်၊ mining function ထဲမှာတော့ hash ရဲ့ ရှေ့ဆုံးမှာ zero အလုံးရေအတိအကျပါအောင် nonce ကို တိုးပြီး loop ပတ်ရှာမှာ ဖြစ်ပါတယ်။ ဒါက PoW ရဲ့ core idea ဖြစ်တဲ့ compute-heavy puzzle ကို ရိုးရှင်းစွာ ပုံဖော်ပြထားတာပါ။

လက်တွေ့ ဆောက်ကြည့်မယ်

Transaction class ကို fromAddress, toAddress, amount field သုံးခုနဲ့ ရေးပါ။ Blockchain class ထဲမှာ pendingTransactions = [] property တစ်ခုနဲ့ difficulty = 2 property တစ်ခု ထည့်ပါ။ Block class ထဲမှာ nonce field တစ်ခုထပ်ထည့်ပြီး mineBlock(difficulty) method ရေးပါ — while loop နဲ့ hash.substring(0, difficulty) ဟာ '0'.repeat(difficulty) ဖြစ်တဲ့အထိ nonce++ ပြီး hash ပြန်တွက်ပါ။ Blockchain class ထဲမှာ minePendingTransactions(miningRewardAddress) method ရေးပါ — pendingTransactions တွေကို data အဖြစ်ယူပြီး new Block ဖန်တီး၊ mineBlock() ခေါ်၊ chain.push()၊ ပြီးရင် mining reward transaction တစ်ခုကို ထပ်ထည့်ပြီး pendingTransactions ကို reset ပါ။

ပိုပြီးနားလည်ထားရမယ့် အချက်

Part 2 မှာ transaction pool, validation, block selection နဲ့toy proof-of-work ကိုထည့်ပြီး pending → included lifecycle ပြပါတယ်။ Pool ထဲမဝင်မီ transaction schema, amount, sender authorization/replay/double-spend policy ကိုစစ်ရပြီး mining puzzle ကeducational Sybil-cost approximation သာဖြစ်ပါတယ်။ Difficulty bounds, deterministic reward rules, pool cleanup နဲ့failed candidate handling ကိုtests လုပ်ပါ။

လက်တွေ့မှာ ဘယ်လိုအသုံးချမလဲ

Invalid/duplicate transactions ရောထားသော pool ကိုmine လုပ်ပြီး valid-only inclusion, nonce/difficulty validation နဲ့remaining pool behavior စမ်းပါ။

Code နမူနာ

javascript
const crypto = require('crypto');

class Transaction {
  constructor(fromAddress, toAddress, amount) {
    this.fromAddress = fromAddress;
    this.toAddress = toAddress;
    this.amount = amount;
  }
}

class Block {
  constructor(timestamp, transactions, previousHash = '') {
    this.timestamp = timestamp;
    this.transactions = transactions;
    this.previousHash = previousHash;
    this.nonce = 0;
    this.hash = this.calculateHash();
  }

  calculateHash() {
    return crypto
      .createHash('sha256')
      .update(this.previousHash + this.timestamp + JSON.stringify(this.transactions) + this.nonce)
      .digest('hex');
  }

  mineBlock(difficulty) {
    const target = Array(difficulty + 1).join('0');
    while (this.hash.substring(0, difficulty) !== target) {
      this.nonce++;
      this.hash = this.calculateHash();
    }
    console.log(`Block mined: ${this.hash} (nonce: ${this.nonce})`);
  }
}

class Blockchain {
  constructor() {
    this.chain = [new Block(Date.now(), 'Genesis Block', '0')];
    this.difficulty = 2;
    this.pendingTransactions = [];
  }

  getLatestBlock() {
    return this.chain[this.chain.length - 1];
  }

  minePendingTransactions(miningRewardAddress) {
    const block = new Block(Date.now(), this.pendingTransactions, this.getLatestBlock().hash);
    block.mineBlock(this.difficulty);
    this.chain.push(block);
    this.pendingTransactions = [new Transaction(null, miningRewardAddress, 1)];
  }

  createTransaction(transaction) {
    this.pendingTransactions.push(transaction);
  }
}

const myChain = new Blockchain();
myChain.createTransaction(new Transaction('wallet-A', 'wallet-B', 50));
myChain.minePendingTransactions('miner-address');
You should see
run လိုက်ရင် mined block ရဲ့ hash က difficulty နဲ့ညီတဲ့ '00' ကနေစပြီး terminal ပေါ်မှာ ပေါ်လာပြီး၊ ဘယ်နှစ်ကြိမ် nonce loop ပတ်ခဲ့လဲဆိုတာကိုပါ တွေ့ရပါလိမ့်မယ်။

ကိုယ်တိုင်စမ်းကြည့်ရန်

Mini Blockchain Project — အပိုင်း ၂: Transaction Pool နဲ့ Mining ထည့်သွင်းခြင်း နမူနာကို real funds သို့real seed/private key မသုံးဘဲ local simulation သို့testnet မှာပြန်တည်ဆောက်ပါ။ Normal case နှစ်ခု၊ tampered/adversarial case တစ်ခုနှင့် failure case တစ်ခုအတွက် expected state နဲ့verification evidence ကိုမ run မီရေးထားပါ။

Security သတိပေးချက်

Hash prefix puzzle တစ်ခုထည့်လိုက်တာနဲ့ network-safe Proof of Work consensus ဖြစ်ပြီလို့မယူဆပါနှင့်။

NISTIR 8202 — Blockchain Technology Overview — NIST

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Hash prefix puzzle တစ်ခုထည့်လိုက်တာနဲ့ network-safe Proof of Work consensus ဖြစ်ပြီလို့မယူဆပါနှင့်။
  • Happy path တစ်ခုအောင်တာနဲ့ system secure သို့production-ready ဖြစ်ပြီဟုယူဆခြင်းက adversarial, key-management နဲ့economic risks များကိုလွတ်စေနိုင်ပါတယ်။

လက်တွေ့လေ့ကျင့်ခန်း

Mini Blockchain Project — အပိုင်း ၂: Transaction Pool နဲ့ Mining ထည့်သွင်းခြင်း နမူနာကို real funds သို့real seed/private key မသုံးဘဲ local simulation သို့testnet မှာပြန်တည်ဆောက်ပါ။ Normal case နှစ်ခု၊ tampered/adversarial case တစ်ခုနှင့် failure case တစ်ခုအတွက် expected state နဲ့verification evidence ကိုမ run မီရေးထားပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: run လိုက်ရင် mined block ရဲ့ hash က difficulty နဲ့ညီတဲ့ '00' ကနေစပြီး terminal ပေါ်မှာ ပေါ်လာပြီး၊ ဘယ်နှစ်ကြိမ် nonce loop ပတ်ခဲ့လဲဆိုတာကိုပါ တွေ့ရပါလိမ့်မယ်။

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Mini Blockchain Project — အပိုင်း ၂: Transaction Pool နဲ့ Mining ထည့်သွင်းခြင်း | Thuta Learning