Thuta Learning
ProjectsSecurityadvanced

Mini Blockchain Project — Part 2: Adding a Transaction Pool & Mining

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Apply Mini Blockchain Project — Part 2: Adding a Transaction Pool & Mining in a hands-on project
  • Write and run your own code
  • Build a complete project step by step

Let's think about this for a second

In this part, we'll actually implement in code the transactions, consensus, and PoW/PoS concepts you learned in the Intermediate chapter. On a real network, users broadcast transactions, and miners gather those transactions into a block, solve a Proof-of-Work puzzle, and only then confirm it onto the chain. Here, we'll create a pendingTransactions array to temporarily hold transactions, and in the mining function we'll increment a nonce and loop until the hash starts with the exact number of zeros required. This is a simplified illustration of PoW's core idea: a compute-heavy puzzle.

Let's build it

Write a Transaction class with three fields: fromAddress, toAddress, and amount. In the Blockchain class, add a pendingTransactions = [] property and a difficulty = 2 property. In the Block class, add a nonce field and write a mineBlock(difficulty) method — using a while loop, keep incrementing nonce and recalculating the hash until hash.substring(0, difficulty) equals '0'.repeat(difficulty). In the Blockchain class, write a minePendingTransactions(miningRewardAddress) method — take the pendingTransactions as data, create a new Block, call mineBlock(), push it onto chain, then add a mining reward transaction and reset pendingTransactions.

Code Example

javascript
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');
You should see
After running it, you'll see the mined block's hash starting with as many '00' zeros as the difficulty requires printed to the terminal, along with how many times the nonce loop had to run.

5-Minute Try-It

Raise difficulty from 2 to 4 and use console.time()/console.timeEnd() to measure, within 5 minutes, how much the mining time changes.

A Quick Word of Caution

Real-world PoW mining (like Bitcoin) burns huge amounts of electricity and runs at a global network scale — you can't actually use this local demo to mine crypto of your own. It's built purely to teach the concept.

Easy traps

  • Forgetting to increment nonce++ inside the mineBlock() loop, ending up with an infinite loop
  • Setting difficulty as high as 5-6, which makes mining take forever on your local machine (for a learning simulation, 2-3 is plenty)

Now Try It Yourself

Raise difficulty from 2 to 4 and use console.time()/console.timeEnd() to measure, within 5 minutes, how much the mining time changes.

You'll know it worked when: After running it, you'll see the mined block's hash starting with as many '00' zeros as the difficulty requires printed to the terminal, along with how many times the nonce loop had to run.

Mini Blockchain Project — Part 2: Adding a Transaction Pool & Mining | Thuta Learning