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.
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.5-Minute Try-It
Push Task 3's difficulty up to 5 and spend 5 minutes watching how sharply the mining time spikes, paying attention to the exponential growth pattern.
A Quick Word of Caution
When doing Task 2, only change the data field — don't manually rewrite the hash field. If you edit the hash field too, you won't actually be testing the tamper detection logic.