ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
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 နမူနာ
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');
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 ဖြစ်ပြီလို့မယူဆပါနှင့်။