Thuta Learning
Blockchain
ProjectsSecurityAdvanced

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

BlockchainLesson 33

What you'll walk away with

  • Explain the protocol model, trust assumptions, and failure modes of Mini Blockchain Project — Part 2: Adding a Transaction Pool & Mining
  • Test normal, tampered, and failure cases in a local simulation or testnet
  • Apply safe practices and explain limitations without real keys or funds

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.

Build a Complete Mental Model

Part 2 adds a transaction pool, validation, block selection, and toy proof of work to model pending-to-included lifecycle. Validate schema, amount, authorization, replay, and double-spend policy before accepting transactions; the mining puzzle is only an educational approximation of Sybil cost. Test difficulty bounds, deterministic rewards, pool cleanup, and failed candidates.

Apply It Safely

Mine a pool containing invalid and duplicate transactions, verifying valid-only inclusion, nonce and difficulty checks, and remaining-pool behavior.

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.

Try It Yourself

Rebuild the Mini Blockchain Project — Part 2: Adding a Transaction Pool & Mining 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

A hash-prefix puzzle alone is not network-safe Proof of Work consensus.

NISTIR 8202 — Blockchain Technology Overview — NIST

Easy traps

  • A hash-prefix puzzle alone is not network-safe Proof of Work consensus.
  • One successful happy path does not establish security or production readiness; adversarial, key-management, and economic risks need explicit analysis.

Hands-on Exercise

Rebuild the Mini Blockchain Project — Part 2: Adding a Transaction Pool & Mining 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.

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.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

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