liveBook · Manning

Blockchain

As explained in The Joy of JavaScript

A blockchain is a distributed append-only ledger composed of chronologically ordered blocks linked by cryptographic hashes.

A blockchain is an append-only sequence of blocks. Each block holds data in chronological order and records the previous block’s hash. That hash contributes to the current block’s hash, linking the records cryptographically rather than through object references. Once a block is part of the chain, its contents are meant to remain unchanged; new data goes into new blocks. section 1.6

A small transaction ledger separates three jobs:

  • A transaction records a transfer; pending transactions can be collected into a block. A wallet identifies a participant. section 1.6 section 3.5
  • A block stores transactions, its index, timestamp, previous hash, and its own calculated hash. Recalculating a changed block’s hash can expose tampering. section 3.5
  • The chain stores blocks, starts with a genesis block, and offers operations such as height, hash lookup, and push. section 3.5
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.

Follow each stored previous hash into the next hash calculation. Changing an earlier block breaks the hash relationship that later blocks depend on. section 1.6 section 3.5

Adding a block

A JavaScript model can keep blocks in a private Map keyed by hash. Its constructor inserts a genesis block; push(newBlock) inserts another, while height() reports the Map’s size. The Block model calculates its hash from selected fields, including previousHash and data. This is a teaching skeleton, not a production ledger: its separate HasHash example uses a simple numeric calculation rather than a cryptographically secure hash. section 3.5

Mining before insertion

A demonstration proof-of-work loop increments a block’s nonce and recalculates its hash until its string representation begins with the required zero prefix. A mining function then pushes the resulting block into the ledger. This distinguishes preparing a candidate block from extending the chain; it does not turn the small model into a production-ready protocol. section 7.4.2 section 1.6

The chain can also serve as a JavaScript integration point. A generator can repeatedly create blocks using the current height, top hash, and pending transactions; 20 additions produce height 21 when the genesis block is counted. Alternatively, a Proxy can intercept push so an observable replays existing blocks and emits new ones to subscribers. These extensions support simulations or reactive consumers without making either feature part of the hash linkage itself. section 9.2.2 section 9.4.8

How The Joy of JavaScript covers it

How deep it goes
Defines block linkage and sketches storage, hashing, mining, generation, and streaming, while stopping short of a production-ready protocol.
Examples
Illustrates a wallet payment, JavaScript Block and Blockchain classes, a nonce-search loop, and a chain reaching height 21.
What it assumes
Assumes some JavaScript background but introduces the blockchain domain without requiring prior blockchain knowledge.
What you can do after
Build it from scratch as a small JavaScript ledger model and experiment with mining or observing newly pushed blocks.
How it connects
Connects blocks and transactions to cryptographic hashes, proof of work, generators, and observables.

Also explained in

Compare how 2 books explain blockchain →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Joy of JavaScript. The section links point to the parts of the book it draws on.