iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Blockchain Basics

What a blockchain actually is: an append-only public log replicated across nodes, with cryptographic guarantees on order and authorship.

Web3 — blockchain primer

EXAMPLE
# ===== The minimal idea =====
# - Blocks contain transactions
# - Each block references the hash of the previous block
# - Nodes agree on the next block via a consensus algorithm
# - Once a block is widely confirmed, rewriting it costs more than the reward
# Result: a tamper-resistant, replicated, append-only ledger

# ===== Hash chain =====
block_n.hash = H(block_n.header || block_n.transactions)
block_n.header.prev = block_{n-1}.hash
# Change anything in any past block -> all subsequent hashes change.
# Nodes detect this and reject the rewrite.

# ===== Consensus =====
# Proof of Work (Bitcoin): miners burn energy to find a low hash; longest chain wins
# Proof of Stake (Ethereum since The Merge): validators stake tokens; misbehave -> slashed
# Other: BFT-style (Tendermint), DAG-based (Solana 'PoH'), federated (Stellar)

# ===== Accounts vs UTXO =====
# Bitcoin-style UTXO: transactions consume previous outputs, produce new outputs
# Ethereum-style accounts: each address has a balance; transactions move balance
# Smart-contract chains generally use the account model.

# ===== Smart contracts =====
# Programs stored AT an address on the chain.
# Anyone can read them; anyone can call them by sending a transaction.
# Execution charges gas (computation + storage + bandwidth).
# Bugs are public + often immutable.

# ===== Public + permissionless =====
# - Anyone can read the chain (every transaction since genesis)
# - Anyone can publish a transaction (paying gas)
# - Anyone can run a node + verify the chain
# Private / consortium chains exist (Hyperledger, R3 Corda) but trade decentralisation
# for permissioning.

# ===== Wallets + keys =====
# A wallet is a key pair (private + public).
# Sign a transaction with the private key -> any node can verify ownership.
# Lose the key -> lose the funds. No password reset.

# ===== Trade-offs vs traditional databases =====
# + No central operator; censorship-resistant
# + Public verifiability
# + Programmable money (smart contracts)
# - Higher latency + cost than a database
# - Storage is expensive on-chain
# - Mistakes are often irreversible

# ===== Layer 1 vs Layer 2 =====
# L1: the base chain (Ethereum mainnet, Bitcoin, Solana)
# L2: scales the L1 by processing transactions off-chain and posting summaries back
#     (Optimistic rollups: Arbitrum, Optimism; ZK rollups: zkSync, Starknet, Scroll)
# L2 is where most user activity sits in 2026: cheaper + faster + secured by L1.

# ===== Reading the chain =====
import { createPublicClient, http } from 'viem';
import { mainnet } from 'viem/chains';
const client = createPublicClient({ chain: mainnet, transport: http() });
const block = await client.getBlock();
const balance = await client.getBalance({ address: '0x...' });

# ===== Patterns to internalise =====
# - 'Trustless' means 'trust the protocol, not a party'; bugs in the protocol break that
# - Storage is expensive; minimise on-chain state
# - L2s for most user-facing apps; L1 for settlement
# - Public chains leak everything; design with privacy in mind (zk for sensitive data)

# ===== Pitfalls =====
# - Treating a blockchain as a database (storage cost + latency)
# - Confusing 'decentralised' with 'distributed'
# - Storing PII on-chain (cannot delete; GDPR conflicts)
# - Treating mainnet as a testbed (real money)

Why it matters

A blockchain is an append-only log that many parties replicate and agree on without a central operator. Hash chains for tamper detection, consensus for ordering, smart contracts for programmable behaviour, and Layer 2 for scale. Pick on-chain only when you actually need the trust model — otherwise a database is cheaper, faster, and more flexible.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
// A blockchain is a replicated, append-only log of transactions.
// Consensus (PoS, PoW) keeps participants honest.
// Smart contracts are programs stored on the chain and run by every node.
Try it Yourself »

Discussion

Loading…