Thuta Learning
Blockchain
ProjectsSecurityAdvanced

Mini Blockchain Project — Part 1: Basic Block & Chain Structure

BlockchainLesson 32

What you'll walk away with

  • Explain the protocol model, trust assumptions, and failure modes of Mini Blockchain Project — Part 1: Basic Block & Chain Structure
  • 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 project we'll take the block, hashing, and immutability concepts you learned in the Basic chapter and actually wire them together in real code. In real blockchains like Bitcoin and Ethereum, each block's hash gets stored in the next block's previousHash field, linking them into a chain. To really understand this mechanism, we'll compute a SHA256 hash directly with Node.js's built-in crypto module and write our own Block class. This part covers building the Block class and getting the Blockchain class's genesis block started — transactions and mining come in the next part.

Let's build it

Open a new project folder and create a blockchain.js file. Require the crypto module as a built-in (no npm install needed). Give the Block class an index, timestamp, data, previousHash, and hash field, and inside the calculateHash() method, hash index+previousHash+timestamp+JSON.stringify(data) with SHA256. In the Blockchain class, initialize the chain array with createGenesisBlock() and add a getLatestBlock() method. Finally, print the genesis block with console.log(JSON.stringify(myChain, null, 2)).

Build a Complete Mental Model

Part 1's toy blockchain teaches block serialization, cryptographic hashes, previous links, and chain storage; it is not production consensus or security. Define canonical field order and encoding so identical data hashes identically across nodes. Establish genesis rules, deterministic validation, and tampered-chain tests from the start.

Apply It Safely

Implement Block and Blockchain with canonical serialization and test valid chains, changed data, changed previous hashes, and duplicate-genesis cases.

Code Example

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
Running node blockchain.js will print the chain array to the terminal, showing one genesis block with its hash value in hex string format, formatted as JSON.

Try It Yourself

Rebuild the Mini Blockchain Project — Part 1: Basic Block & Chain Structure 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

Hashing nondeterministic JSON or timestamps can produce different block hashes across runs.

NISTIR 8202 — Blockchain Technology Overview — NIST

Easy traps

  • Hashing nondeterministic JSON or timestamps can produce different block hashes across runs.
  • 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 1: Basic Block & Chain Structure 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: Running node blockchain.js will print the chain array to the terminal, showing one genesis block with its hash value in hex string format, formatted as JSON.

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 1: Basic Block & Chain Structure | Thuta Learning