TechLead
Lesson 1 of 20
5 min read
Web3 & Blockchain

Blockchain Fundamentals

Understand the core concepts of blockchain technology including distributed ledgers, consensus mechanisms, and cryptographic hashing

What is a Blockchain?

A blockchain is a distributed, immutable digital ledger that records transactions across a network of computers. Each block contains a set of transactions, a timestamp, and a cryptographic hash of the previous block, forming a tamper-proof chain. No single entity controls the network — instead, consensus among participants validates new entries.

Key Properties of Blockchain

  • Decentralization: No single point of control or failure — the network is maintained by thousands of nodes worldwide
  • Immutability: Once a block is confirmed and added to the chain, altering it requires re-computing every subsequent block, which is computationally infeasible
  • Transparency: All transactions are publicly visible on the ledger, enabling auditability and trust without intermediaries
  • Security: Cryptographic hashing (SHA-256, Keccak-256) and digital signatures ensure data integrity and authenticity
  • Consensus: Mechanisms like Proof of Work or Proof of Stake ensure all participants agree on the state of the ledger

How Blocks Are Structured

Every block in a blockchain contains a header and a body. The header stores metadata — including the hash of the previous block, a Merkle root of all transactions, a nonce (for PoW chains), and a timestamp. The body contains the actual transaction data. This structure ensures that changing any single transaction would alter the Merkle root and break the chain of hashes.

// Simplified block structure
interface Block {
  index: number;
  timestamp: number;
  previousHash: string;
  hash: string;
  nonce: number;
  transactions: Transaction[];
  merkleRoot: string;
}

interface Transaction {
  from: string;
  to: string;
  value: number;
  data: string;
  signature: string;
}

// Hashing a block
import { createHash } from 'crypto';

function calculateHash(block: Omit<Block, 'hash'>): string {
  const data = JSON.stringify({
    index: block.index,
    timestamp: block.timestamp,
    previousHash: block.previousHash,
    nonce: block.nonce,
    merkleRoot: block.merkleRoot,
  });
  return createHash('sha256').update(data).digest('hex');
}

// Simple proof-of-work mining
function mineBlock(block: Omit<Block, 'hash'>, difficulty: number): Block {
  let nonce = 0;
  let hash = '';
  const prefix = '0'.repeat(difficulty);

  while (!hash.startsWith(prefix)) {
    nonce++;
    hash = calculateHash({ ...block, nonce });
  }

  return { ...block, nonce, hash };
}

Consensus Mechanisms

Consensus mechanisms are the protocols that ensure all nodes in a distributed network agree on the current state of the blockchain. Different blockchains use different mechanisms, each with trade-offs between security, speed, decentralization, and energy consumption.

Consensus Comparison

Mechanism Security Speed Energy
Proof of Work (PoW)Very HighSlow (10+ min)Very High
Proof of Stake (PoS)HighFast (12 sec)Low
Delegated PoS (DPoS)ModerateVery FastVery Low
Practical BFT (PBFT)HighFastLow

Cryptographic Hashing

Hash functions are the backbone of blockchain security. A cryptographic hash function takes an input of any size and produces a fixed-size output (digest) with these properties: it is deterministic (same input always yields the same output), pre-image resistant (you cannot reverse-engineer the input from the output), and collision resistant (it is practically impossible to find two different inputs that produce the same hash).

import { createHash } from 'crypto';

// SHA-256 produces a 64-character hex string (256 bits)
const hash1 = createHash('sha256').update('Hello Blockchain').digest('hex');
console.log(hash1);
// "7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069"

// Even a tiny change completely alters the hash (avalanche effect)
const hash2 = createHash('sha256').update('Hello Blockchaim').digest('hex');
console.log(hash2);
// "e4d7f1b4ed2e42d15898f4b27b019da36cf7c4be0b72..."

// Merkle tree for efficient transaction verification
function buildMerkleRoot(txHashes: string[]): string {
  if (txHashes.length === 0) return '';
  if (txHashes.length === 1) return txHashes[0];

  const nextLevel: string[] = [];
  for (let i = 0; i < txHashes.length; i += 2) {
    const left = txHashes[i];
    const right = txHashes[i + 1] || left; // duplicate last if odd
    const combined = createHash('sha256')
      .update(left + right)
      .digest('hex');
    nextLevel.push(combined);
  }
  return buildMerkleRoot(nextLevel);
}

Public and Private Keys

Blockchain uses asymmetric cryptography (public-key cryptography) for identity and authorization. Each user has a key pair: a private key that must be kept secret and a public key that can be shared freely. The public key (or a hash of it) serves as the user's address on the blockchain, while the private key is used to sign transactions, proving ownership without revealing the key itself.

Key Concepts to Remember

  • Private Key: A 256-bit random number that must never be shared. It signs transactions and proves ownership of funds.
  • Public Key: Derived from the private key using elliptic curve multiplication (secp256k1). It can be shared publicly.
  • Address: A shortened, checksummed version of the public key (e.g., Ethereum addresses are the last 20 bytes of the Keccak-256 hash of the public key).
  • Digital Signature: Created by signing a message hash with your private key. Anyone with your public key can verify the signature without learning your private key.

Types of Blockchains

Not all blockchains are the same. They can be categorized based on who can participate and who can validate transactions.

  • Public blockchains (Bitcoin, Ethereum) — open to anyone; anyone can read, write, and validate. Maximum decentralization and transparency.
  • Private blockchains (Hyperledger Fabric) — access is restricted to authorized participants. Used in enterprise settings for supply chain, finance, etc.
  • Consortium blockchains (R3 Corda) — a hybrid where a group of organizations jointly manages the network. Common in banking and cross-industry collaborations.

Real-World Applications

Blockchain technology extends far beyond cryptocurrency. It powers decentralized finance (DeFi), supply chain tracking, digital identity, voting systems, NFTs, gaming economies, and more. Understanding the fundamentals covered here — hashing, consensus, key pairs, and block structure — is essential before diving into smart contracts and dApp development in the following lessons.

Continue Learning