Let's think about this for a second
This lesson is a self-test practice set that combines the consensus, PoW/PoS, wallets, and keys concepts you learned in the Intermediate/Advanced chapters. It steps up from Exercise Set 1 with tasks like writing functions from scratch and checking security properties. There's no new teaching here — you'll be referencing the codebase you built in the Mini Blockchain Project and rewriting it yourself.
Exercises
Task 1 — Say you're given a chain array (mock data) with three blocks; write an isChainValid(chain) function from scratch that checks both the hash and the previousHash link. Task 2 — Tamper with only chain[1]'s data (leave the hash field alone) and check whether isChainValid() returns false. Task 3 — Modify the mineBlock(difficulty) function to accept difficulty as a parameter, and use console.time() to measure the difference in mining time between difficulty 2 and 4. Task 4 — Generate a key pair with crypto.generateKeyPairSync(), sign a message string with the private key, and verify it back with the public key.
Build a Complete Mental Model
Advanced exercises combine chain validation, transaction authorization, pool rules, and toy Proof of Work failure cases. Define validation order, state reconstruction, fork choice, and adversarial inputs before coding, then test normal, replay, tamper, overspend, invalid-work, and competing-chain cases. Label every result as an educational simulation rather than a production protocol guarantee.
Apply It Safely
For each challenge, provide a threat model, invariant list, tests, a rejected attack, and a report of remaining limitations.
Code Example
const crypto = require('crypto');
// Mock chain (Task 1-2 အတွက်)
const chain = [
{ index: 0, data: 'Genesis Block', previousHash: '0', hash: 'abc0...' },
{ index: 1, data: 'Tx: A->B 10', previousHash: 'abc0...', hash: 'abc1...' },
{ index: 2, data: 'Tx: B->C 5', previousHash: 'abc1...', hash: 'abc2...' },
];
function calculateHash(block) {
return crypto
.createHash('sha256')
.update(block.index + block.previousHash + block.data)
.digest('hex');
}
// Task 1: isChainValid() ကို scratch ကနေ ကိုယ်တိုင်ရေးပါ
function isChainValid(chain) {
// TODO: for loop နဲ့ hash + previousHash link နှစ်ခုလုံး စစ်ပါ
}
// Task 2: chain[1].data ကို tamper လုပ်ပြီး isChainValid() ပြန်ခေါ်ကြည့်ပါ
// Task 3: mineBlock(block, difficulty) function ကို difficulty parameter ပါအောင် ရေးပြီး
// console.time('mine-2') / console.timeEnd('mine-2') နဲ့ difficulty 2 vs 4 timing ယှဉ်ပါ
// Task 4: key pair generate လုပ်ပြီး message sign & verify practice
const { generateKeyPairSync, sign, verify } = crypto;
// TODO: generateKeyPairSync('ec', { namedCurve: 'secp256k1', ... }) နဲ့ sign/verify ကို ကိုယ်တိုင်ပြီးအောင်ရေးပါ
After Tasks 1-2, you'll see two booleans in the terminal — true before tampering, false after tampering. After Task 3, two mining times (ms) for different difficulty levels. After Task 4, a signature verify result of true.Try It Yourself
Rebuild the Blockchain Exercises — Set 2 (Chain Validation & Proof-of-Work) example on a local simulation or testnet without real funds, seeds, or private keys. Define two normal cases, one tampered or adversarial case, and one failure case, writing expected state and verification evidence before execution.
Security Warning
One valid happy-path chain does not establish that a validator handles adversarial input.