Blockchain

Overview

A blockchain is a specialized type of database that organizes data into a series of records known as blocks. These blocks are stored in chronological order and are cryptographically linked, forming a tamperproof chain. Each block contains a hash that is dependent on the hash of the previous block, ensuring the integrity and immutability of the entire chain. This structure is widely used in various applications, including financial software and secure document storage.

Figure 1.13 A simple representation of a blockchain, in which each block stores the previous block’s hash. This hash is used to compute the current block’s own hash, effectively connecting all these blocks in a chain. Figure 1.13 A simple representation of a blockchain, in which each block stores the previous block’s hash. This hash is used to compute the current block’s own hash, effectively connecting all these blocks in a chain.

Blockchain Class Definition

In the context of programming, a blockchain can be represented as a class that manages a collection of blocks, each storing a set of transactions. The Blockchain class is designed to handle the addition of new blocks and the calculation of hashes using the previous block's hash. This class is extended with mixins to enhance its functionality, such as validation.

Listing: Blockchain Definition with Mixins

class Blockchain {
     
  #blocks = new Map();
 
  constructor(genesis = createGenesisBlock()) { 
     this.#blocks.set(genesis.hash, genesis);
  }
  
  height() {
     return this.#blocks.size;
  }
 
  lookup(hash) {
    const h = hash;
    if (this.#blocks.has(h)) {
      return this.#blocks.get(h);
    }
    throw new Error(`Block with hash ${h} not found!`);
  }
 
  push(newBlock) {     
     this.#blocks.set(newBlock.hash, newBlock);
     return newBlock;
     }  
}
 
function createGenesisBlock(previousHash = '0'.repeat(64)) {
  //...
}
 
Object.assign(Blockchain.prototype, HasValidation());       #1
#1 As with Transaction, extends blockchain with validation functionality. (Full implementation of validation logic is covered in chapter 5.)

Main Objects in a Blockchain Application

In a simple blockchain application, several key objects interact to form the system. These include blocks, transactions, and other domain-specific entities. The following figure illustrates the main objects involved in such an application.

Figure 3.10 The main objects at play in our simple blockchain application. I have not shown Money, a value object that describes an amount and currency. Figure 3.10 The main objects at play in our simple blockchain application. I have not shown Money, a value object that describes an amount and currency.

Code Availability

The code for the blockchain application discussed here is available on GitHub. The repository includes the blockchain application and all code listings as unit tests, along with Babel configuration files for transpiling nonstandard proposals. You can access it at https://github.com/JoyOfJavaScript/joj.

FAQ (Frequently asked questions)

Where can I find the code for the blockchain application discussed in 'The Joy of JavaScript'?

How is the Blockchain class defined in 'The Joy of JavaScript'?