Difficulty: Beginner
A blockchain is a distributed, immutable ledger that records transactions across a peer-to-peer network of computers. Unlike traditional databases controlled by a single entity, a blockchain distributes identical copies of the ledger to every participating node in the network. When a new transaction occurs, it is broadcast to all nodes, validated according to the network's consensus rules, and then permanently recorded in a new block that is cryptographically linked to the previous one.
The term "immutability" is central to understanding blockchain's value proposition. Once data is written into a block and that block is confirmed by the network, altering it becomes computationally infeasible. Each block contains a cryptographic hash of the previous block, creating a chain. If someone were to tamper with a past block, every subsequent block's hash would become invalid, and the rest of the network would immediately reject the fraudulent chain. This property makes blockchains exceptionally resistant to fraud and retroactive modification.
Decentralization is the architectural principle that distinguishes blockchain from conventional client-server systems. In a centralized system, a single authority controls the database - a bank, a government registry, or a cloud provider. In a decentralized blockchain, no single party has unilateral control. Decisions about the state of the ledger are made collectively through consensus mechanisms. This eliminates single points of failure and reduces the need for trust in any one intermediary.
The peer-to-peer (P2P) network layer is what makes decentralization possible. Nodes communicate directly with one another, sharing new transactions and blocks without routing through a central server. Each full node independently validates every transaction and block it receives, maintaining its own complete copy of the blockchain. This redundancy ensures that the network continues to function even if a significant number of nodes go offline.
Blockchain technology was first realized in 2008 with the publication of the Bitcoin whitepaper by the pseudonymous Satoshi Nakamoto. Since then, the technology has expanded far beyond cryptocurrency. Modern blockchains power decentralized finance (DeFi), supply chain tracking, digital identity, non-fungible tokens (NFTs), and decentralized autonomous organizations (DAOs). Understanding the foundational concepts of distributed ledgers, immutability, and decentralization is essential for anyone entering the blockchain space.
const crypto = require('crypto');
class Block {
constructor(index, timestamp, data, previousHash = '') {
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.previousHash = previousHash;
this.nonce = 0;
this.hash = this.calculateHash();
}
calculateHash() {
return crypto
.createHash('sha256')
.update(
this.index +
this.timestamp +
JSON.stringify(this.data) +
this.previousHash +
this.nonce
)
.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];
}
addBlock(data) {
const previousBlock = this.getLatestBlock();
const newBlock = new Block(
previousBlock.index + 1,
Date.now(),
data,
previousBlock.hash
);
this.chain.push(newBlock);
return newBlock;
}
isChainValid() {
for (let i = 1; i < this.chain.length; i++) {
const current = this.chain[i];
const previous = this.chain[i - 1];
if (current.hash !== current.calculateHash()) return false;
if (current.previousHash !== previous.hash) return false;
}
return true;
}
}
// Usage
const myChain = new Blockchain();
myChain.addBlock({ from: 'Alice', to: 'Bob', amount: 10 });
myChain.addBlock({ from: 'Bob', to: 'Charlie', amount: 5 });
console.log('Is chain valid?', myChain.isChainValid());
console.log('Chain:', JSON.stringify(myChain.chain, null, 2));
This example demonstrates the core structure of a blockchain. Each Block contains an index, timestamp, arbitrary data, the hash of the previous block, and its own hash computed from all these fields. The Blockchain class maintains an array of blocks starting with a genesis block. The isChainValid method shows how integrity is verified - if any block's data is tampered with, its hash will no longer match the stored value, and the chain breaks.
Distributed Ledger, Immutability, Decentralization, P2P Network