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