Joseph Carothers
Blockchain Demo

Blockchain Demo

Thu Nov 18 2021

Live Demo

Blockchain Demo

Welcome to my Blockchain Demo project! This educational tool provides a hands-on way to learn the core concepts behind blockchain technologies. Whether you’re new to the blockchain world or brushing up on your knowledge, this demo walks you through the foundational ideas: hash functions, blocks, and distributed ledgers.

I've built this site using ReactJS and created a pseudo-blockchain engine to drive the interactive elements. You can see and interact with blockchain mechanics in real-time, such as mining blocks, validating hashes, and adding new blocks.

Inspired by the great work and videos done by Anders Brownworth

How it Works

At its core, this project implements a simplified version of a blockchain, demonstrating basic methods you'd find in real-world blockchains: validating hashes, mining blocks, and adding new ones to the chain. The goal is to make complex blockchain ideas more tangible and easy to understand.

Here’s a quick breakdown of the blockchain class:

Key Features of the Blockchain Class

Below is the source code that powers the blockchain functionality:

export class Blockchain {
  constructor() {
    this.chain = [this.initGenesisBlock()];
    this.difficulty = 4;
  }

  initGenesisBlock() {
    let prevHash = "0000000000000000000000000000000000000000000000000000000000000000";
    let data = "Initial Block in the Chain";
    let firstBlock = new Block(
      prevHash,
      0,
      data,
      "0000f727854b50bb95c054b39c1fe5c92e5ebcfa4bcb5dc279f56aa96a365e5a"
    );
    firstBlock.nonce = 12424;
    return firstBlock;
  }

  calculateHash(block, testNonce) {
    return SHA256(
      testNonce + block.data + block.index + block.prevHash
    ).toString();
  }

  validateHash = (block) => {
    return (
      this.calculateHash(block, block.nonce).substring(0, this.difficulty) !==
      Array(this.difficulty + 1).join("0")
    );
  };

  updateChain(block) {
    // Updates hash and validates new blocks in the chain
    for (let i = block.index + 1; i < this.chain.length; i++) {
      this.chain[i].prevHash = this.chain[i - 1].hash;
      this.chain[i].hash = this.calculateHash(this.chain[i], this.chain[i].nonce);
      this.chain[i].error = this.validateHash(this.chain);
    }
  }

  latestBlock() {
    return this.chain[this.chain.length - 1];
  }

  mineBlock(block, isChain) {
    if (this.validateHash(block, block.nonce)) {
      let testNonce = 0;
      while (
        this.calculateHash(block, testNonce).substring(0, this.difficulty) !==
        Array(this.difficulty + 1).join("0")
      ) {
        testNonce++;
      }
      this.chain[block.index].nonce = testNonce;
      this.chain[block.index].hash = this.calculateHash(block, testNonce);
      this.chain[block.index].error = this.validateHash(block);

      if (isChain) {
        this.updateChain(block);
      }
      return this.chain;
    } else {
      block.error = false;
      return​⬤
    }