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

Mini Blockchain Project — အပိုင်း ၁: Block နဲ့ Chain အခြေခံဖွဲ့စည်းမှု

Blockchainသင်ခန်းစာ 32

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

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

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

ဒီ project မှာ Basic chapter မှာသင်ခဲ့တဲ့ block, hashing, immutability concept တွေကို တကယ်လက်တွေ့ code ရေးပြီး ချိတ်ဆက်ကြည့်မှာဖြစ်ပါတယ်။ Bitcoin, Ethereum လို real blockchain တွေမှာ block တစ်ခုချင်းစီရဲ့ hash ကို နောက် block ရဲ့ previousHash field ထဲမှာ ထည့်ထားပြီး chain အဖြစ် ချိတ်ဆက်ထားတာပါ။ ဒီ mechanism ကို နားလည်ဖို့ Node.js built-in crypto module နဲ့ SHA256 hash ကို တိုက်ရိုက်တွက်ချက်ပြီး ကိုယ်ပိုင် Block class ရေးကြည့်မှာဖြစ်ပါတယ်။ ဒီအပိုင်းမှာ Block class တည်ဆောက်ခြင်းနဲ့ Blockchain class ရဲ့ genesis block စတင်ခြင်းအထိပဲ ဖြစ်ပါတယ်၊ transaction နဲ့ mining ကို နောက်အပိုင်းမှာ ဆက်ထည့်ပါမယ်။

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

Project folder အသစ်တစ်ခုဖွင့်ပြီး blockchain.js ဖိုင်တစ်ခုဆောက်ပါ။ crypto module ကို built-in အနေနဲ့ require လုပ်ပါ (npm install မလိုပါ)။ Block class မှာ index, timestamp, data, previousHash, hash field တွေထည့်ပြီး calculateHash() method ထဲမှာ index+previousHash+timestamp+JSON.stringify(data) ကို SHA256 နဲ့ hash လုပ်ပါ။ Blockchain class မှာ chain array ကို createGenesisBlock() နဲ့ initialize လုပ်ပြီး getLatestBlock() method ကို ထည့်ထားပါ။ အဆုံးမှာ console.log(JSON.stringify(myChain, null, 2)) နဲ့ genesis block ကို print ကြည့်ပါ။

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

Part 1 toy blockchain က block serialization, cryptographic hash, previous-link နဲ့chain storage concepts ကိုလက်တွေ့သင်ရန်ဖြစ်ပြီး production consensus/security မဟုတ်ပါ။ Canonical serialization မတူလျှင် nodes အတူတူdata ကိုhash မတူနိုင်တာကြောင့် field order/encoding ကိုသတ်မှတ်ပါ။ Genesis block rules, deterministic validation နဲ့tampered chain tests ကိုအစကတည်းကရေးပါ။

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

Block/Blockchain classes ကိုcanonical serialization နဲ့ရေးပြီး valid chain, changed data, changed previous hash နဲ့duplicate genesis cases ကိုtest လုပ်ပါ။

Code နမူနာ

javascript
const crypto = require('crypto');

class Block {
  constructor(index, timestamp, data, previousHash = '') {
    this.index = index;
    this.timestamp = timestamp;
    this.data = data;
    this.previousHash = previousHash;
    this.hash = this.calculateHash();
  }

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

class Blockchain {
  constructor() {
    this.chain = [this.createGenesisBlock()];
  }

  createGenesisBlock() {
    return new Block(0, Date.now(), 'Genesis Block', '0');
  }

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

const myChain = new Blockchain();
console.log(JSON.stringify(myChain, null, 2));
You should see
node blockchain.js run လိုက်ရင် terminal ပေါ်မှာ chain array ထဲမှာ genesis block တစ်ခုနဲ့ hex string ပုံစံ hash value ကို JSON format နဲ့ print ထုတ်ပြပါလိမ့်မယ်။

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

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

Security သတိပေးချက်

JSON property order/timestamp မတည်ငြိမ်ဘဲhash လုပ်ခြင်းက run မတူတိုင်းblock hash ကွာစေနိုင်ပါတယ်။

NISTIR 8202 — Blockchain Technology Overview — NIST

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

  • JSON property order/timestamp မတည်ငြိမ်ဘဲhash လုပ်ခြင်းက run မတူတိုင်းblock hash ကွာစေနိုင်ပါတယ်။
  • Happy path တစ်ခုအောင်တာနဲ့ system secure သို့production-ready ဖြစ်ပြီဟုယူဆခြင်းက adversarial, key-management နဲ့economic risks များကိုလွတ်စေနိုင်ပါတယ်။

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

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

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: node blockchain.js run လိုက်ရင် terminal ပေါ်မှာ chain array ထဲမှာ genesis block တစ်ခုနဲ့ hex string ပုံစံ hash value ကို JSON format နဲ့ print ထုတ်ပြပါလိမ့်မယ်။

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

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

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

Mini Blockchain Project — အပိုင်း ၁: Block နဲ့ Chain အခြေခံဖွဲ့စည်းမှု | Thuta Learning