# What a Blockchain Is — Blockchain & Smart Contracts (Solidity)

Source: https://www.geekswithgeeks.com/en/solidity/b-blockchain

> Explain blocks, hashes, consensus and what blockchains are good and bad at.

## A shared, append-only ledger

A **blockchain** is a ledger replicated across many independent computers (**nodes**) that agree on its contents without a central authority. Transactions are grouped into **blocks**; each block contains the **cryptographic hash** of the previous block, so changing any historical transaction would change every later hash, making tampering evident. Nodes agree on which blocks are valid through a **consensus mechanism**. Bitcoin uses **proof of work**, where miners spend computation to propose blocks. Ethereum switched to **proof of stake** in **The Merge (September 2022)**: validators lock up ether as collateral, are chosen to propose and attest to blocks, and lose part of their stake (**slashing**) if they misbehave. Key properties are **transparency** (anyone can read the chain), **immutability** in practice, **censorship resistance** and **programmability**. Trade-offs: limited throughput, fees that rise with demand, public data (privacy needs extra techniques), irreversible mistakes and lost keys, and a domain full of scams and speculation. Use blockchains when multiple parties who do not fully trust each other need a shared, verifiable state; a normal database is better for most other applications.

## Blocks linked by hashes

Each block stores the previous block's hash, so altering history breaks the chain.

![A row of block boxes, each containing a small fingerprint icon connected by an arrow to the fingerprint inside the previous block.](assets/figures/solidity/section-1-map.svg) — Figure 1.1 — A hash-linked chain of blocks.

## Hash chaining in a few lines

A toy illustration in Python of why altering an old block is detectable.

```python
import hashlib, json

def block_hash(block: dict) -> str:
    return hashlib.sha256(json.dumps(block, sort_keys=True).encode()).hexdigest()

chain = []
prev = "0" * 64
for txs in (["asha->ravi 5"], ["ravi->meera 2"], ["meera->asha 1"]):
    block = {"prev": prev, "txs": txs}
    prev = block_hash(block)
    chain.append(block)

def is_valid(chain: list) -> bool:
    for earlier, later in zip(chain, chain[1:]):
        if later["prev"] != block_hash(earlier):
            return False
    return True

print(is_valid(chain))                    # True
chain[0]["txs"] = ["asha->ravi 500"]      # tamper with history
print(is_valid(chain))                    # False: block 1 no longer points to block 0's hash
```

## Ask "why a blockchain?"

If one organisation controls the data and users trust it, a regular database is faster, cheaper and private. Blockchains add value when several parties need a shared record that none of them controls alone.

**Quiz:** How did Ethereum reach consensus after The Merge in 2022?

- [x] Proof of stake with validators who stake ether
- [ ] Proof of work mining
- [ ] A central server
- [ ] Proof of authority by one company

*Answer:* Proof of stake with validators who stake ether. Ethereum moved from proof of work to proof of stake in September 2022.
