Thuta Learning
ရှာဖွေရန်
ProjectsSecurityadvanced

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

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

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

  • Mini Blockchain Project — အပိုင်း ၂: Transaction Pool နဲ့ Mining ထည့်သွင်းခြင်း ကို လက်တွေ့ project ထဲမှာ အသုံးချတတ်မယ်
  • ကိုယ်တိုင် code ရေးပြီး run ကြည့်တတ်မယ်
  • Project တစ်ခုလုံးကို အဆင့်လိုက် ဆောက်တတ်မယ်

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

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 ပါ။

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 ပတ်ခဲ့လဲဆိုတာကိုပါ တွေ့ရပါလိမ့်မယ်။

၅ မိနစ် စမ်းကြည့်

difficulty ကို 2 ကနေ 4 အထိတိုးပြီး console.time()/console.timeEnd() နဲ့ mining time ဘယ်လောက်ကွာသွားလဲ 5 မိနစ်အတွင်း တိုင်းတာကြည့်ပါ။

သတိလေးတစ်ချက်

Real-world PoW mining (Bitcoin) က electricity အများကြီးသုံးပြီး global network level မှာ run နေတာမို့၊ ဒီ local demo ကို ကိုယ်ပိုင် crypto mine ဖို့ တကယ်သုံးလို့မရပါဘူး — concept သင်ဖို့ပဲ ရည်ရွယ်ပါတယ်။

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

  • mineBlock() loop ထဲမှာ nonce++ ထည့်ဖို့မေ့ပြီး infinite loop ဖြစ်နေအောင် ရေးမိတတ်ခြင်း
  • difficulty ကို 5-6 လောက်အထိ သတ်မှတ်လိုက်ပြီး local machine ပေါ်မှာ mining time အရမ်းကြာသွားအောင် ဖြစ်စေခြင်း (learning simulation အတွက် 2-3 လောက်ပဲ လုံလောက်ပါတယ်)

အခု ကိုယ်တိုင် စမ်းကြည့်

difficulty ကို 2 ကနေ 4 အထိတိုးပြီး console.time()/console.timeEnd() နဲ့ mining time ဘယ်လောက်ကွာသွားလဲ 5 မိနစ်အတွင်း တိုင်းတာကြည့်ပါ။

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

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