Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Layers of a blockchain

Layers of a blockchain split the system into stacked responsibilities (hardware, network, consensus, data/storage, execution, application) so engineers can design, optimise and test each layer independently.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two or three sentences: where an engineer uses this.

Blockchains are built in layers so engineers can design, optimise, test and secure each responsibility separately — for example improving network gossip without changing smart-contract logic. Understanding layers helps you reason about performance (throughput, latency), security (attack surface) and deployment (which layer to fork, and which tests to run).

Key ideas

Explain plainly from first principles; define terms before using them. Bold each term when defined.

  • Blockchain — a distributed, append‑only ledger made of a sequence of blocks (defined next). Assumes multiple independent nodes, persistent storage and cryptographic primitives (hashes, digital signatures).
  • Block — a container of one or more transactions plus a block header that includes metadata (previous block hash, timestamp, nonce (for PoW), merkle root). Blocks link by cryptographic hash to form the chain.
  • Transaction — an atomic change request to the ledger state (for cryptocurrencies: a transfer of value; for smart‑contract platforms: a call). Transactions are signed by the actor using asymmetric cryptography.
  • Mempool — the temporary pool of valid but unconfirmed transactions stored by nodes waiting to be included in a block.
  • Peer‑to‑peer (P2P) network — the overlay network that connects nodes; nodes exchange transactions and blocks via gossip protocols.
  • Consensus — the protocol that lets distributed nodes agree on a single sequence of blocks (examples: Proof of Work, Proof of Stake). Consensus ensures safety (no two honest nodes finalise conflicting histories) and liveness (new transactions eventually get included), under stated assumptions about faults and synchrony.
  • State — the data that represents the current results of all transactions (for UTXO systems like Bitcoin, the set of unspent outputs; for account models like Ethereum, account balances and contract storage).
  • Execution environment — the runtime that executes transactions and smart contracts (e.g., the Ethereum Virtual Machine, EVM). It enforces the rules of contract code and computes new state. It meters computation and storage with gas; per‑block gas limits determine how much work fits in a block and thus affect throughput.
  • Data / Storage layer — where block and state data are stored (local disk, databases). Includes mechanisms like pruning, snapshots, and Merkle trees for compact proofs.
  • Application layer — the user-facing programs and wallets that create transactions and read state via node APIs.
  • Incentive layer — the economic rules (fees, block rewards) that align participant behaviour with protocol goals.
  • Governance layer — off‑chain and on‑chain processes for protocol upgrades (forks, voting).

Typical layered stack engineers use (draw this as stacked boxes, top-to-bottom):

  1. Application layer (wallets, dApps)
  2. Contract / Smart contract layer (high-level code)
  3. Execution / VM layer (EVM, bytecode interpreter)
  4. Consensus layer (PoW, PoS, BFT)
  5. Networking layer (P2P gossip, peer discovery)
  6. Data / Storage layer (blocks, state, Merkle trees)
  7. Hardware / OS layer (CPU, memory, disk, network interfaces)

Exact statements and assumptions often asked in exams

  • Definition (Consensus safety): "If fewer than f nodes are Byzantine and the network satisfies the synchrony assumptions of the protocol, then no two honest nodes will decide conflicting blocks at the same height." State the protocol and its assumption (e.g., PBFT safety requires f < n/3; liveness requires partial/eventual synchrony).

  • Definition (PoW expected-work): In Bitcoin‑style PoW, the expected number of hash attempts to find a block that meets the target is difficulty × 2^32 (Bitcoin definition). Assumptions: uniform hash function output and independent trials.

  • Derivation (throughput relation): Transactions per second (TPS) follows directly from block composition: TPS = (transactions per block) / (average block time). If block size is the limiting factor, transactions per block ≈ block_size_bytes / average_tx_size_bytes. Assumptions: block full, network propagation not the bottleneck.

Diagrams to draw (describe exactly):

  • Draw a vertical stack of seven boxes labelled (top to bottom): Application, Smart Contracts, Execution/VM, Consensus, Networking, Data/Storage, Hardware/OS. Next to each box write one short bullet of responsibilities (e.g., Consensus: propose/validate blocks; Data: store block files and state DB).
  • Draw a small 4‑node P2P network: 4 circles labelled N1..N4; arrows for gossip; show a client sending a transaction to N1, N1 forwarding to N2/N3, miners assembling a block and broadcasting; label mempool and block propagation path.

Where to draw arrows and labels: use directed arrows for broadcasts, solid lines for peer connection, and a highlighted path for the block that becomes canonical.

Formulas

Each formula on its own line, then explanation.

TPS = (transactions_per_block) / (block_time)

  • transactions_per_block: average number of transactions in one block (unit: transactions). block_time: average time between blocks (unit: seconds). Applies when block assembly and block time determine throughput.

transactions_per_block ≈ block_size_bytes / avg_tx_size_bytes

  • block_size_bytes: bytes allocated for transactions in block. avg_tx_size_bytes: mean transaction size in bytes. Units: bytes. Assumes transactions fill the block and ignore header overhead.

expected_hashes_to_find_block = difficulty × 2^32

  • difficulty: Bitcoin difficulty (dimensionless). 2^32 = 4294967296 (unit: hashes per difficulty unit). Applies specifically to Bitcoin's difficulty definition.

expected_time_to_find_block = (difficulty × 2^32) / hashrate

  • hashrate: hashes per second (H/s). Result in seconds. Assumes miner hashes uniformly and independently.

TPS ≈ (block_size_bytes / avg_tx_size_bytes) / block_time

  • Combined form; useful when block size is known. For gas‑metered chains (e.g., Ethereum), TPS ≈ (gas_limit_per_block / avg_gas_per_tx) / block_time.

Note: For proof‑of‑stake or BFT systems no analogous simple hash formula exists; performance depends on protocol steps and network latency.

Worked examples

Example 1 — Compute TPS from block size (numerical): Given: block_size = 1,000,000 bytes (1 MB), average_tx_size = 500 bytes, block_time = 600 s (Bitcoin). Note: Bitcoin post‑SegWit measures block capacity in weight units; this example uses bytes for simplicity. Steps:

  1. transactions_per_block = block_size / avg_tx_size = 1,000,000 / 500 = 2000 transactions.
  2. TPS = transactions_per_block / block_time = 2000 / 600 = 3.333... transactions/sec. Final answer: 3.333 transactions/s.

Example 2 — Expected time for a miner to find a block (Bitcoin formula): Given: difficulty = 2.0 × 10^10 (20,000,000,000), hashrate = 100 TH/s = 1.0 × 10^14 H/s. Use 2^32 = 4,294,967,296.

  1. expected_hashes = difficulty × 2^32 = 2.0e10 × 4.294967296e9 = 8.589934592e19 hashes.
  2. expected_time_seconds = expected_hashes / hashrate = 8.589934592e19 / 1.0e14 = 8.589934592e5 s.
  3. Convert to days: 8.589934592e5 s / (86400 s/day) ≈ 9.94 days. Final answer: ≈ 9.94 days (expected time for that miner to find one block, ignoring pool dynamics).

Example 3 — Trace of a transaction through layers (algorithmic trace on small input): Network: nodes A, B, C, D. Client sends tx T to A.

  1. Application: Wallet constructs and signs T (signature attached).
  2. Networking: A fully validates T (syntax, signatures, basic policy), then places it into its mempool and gossips it to peers B and C.
  3. Mempool (Data layer): B and C validate signatures and add T to their mempools, then forward to D.
  4. Consensus (miner/node executing): Miner at C assembles a candidate block containing T, runs the consensus algorithm (PoW hashing) until a valid block is found.
  5. Block broadcast: C broadcasts the new block; A, B and D receive it and validate the header and transactions (including that T is included). After successful validation they mark the block as the new tip/canonical and append it to their local chain.
  6. Execution layer: Each node executes the transactions in the block to update state (balances, contract storage).
  7. Application: Wallet sees transaction included after required confirmations.

Show these steps on the diagram with arrows labelled at each transition.

Common mistakes

  • Using Bitcoin formulas (difficulty × 2^32) for non‑Bitcoin PoW systems without checking their difficulty definition.
  • Forgetting units when computing TPS (seconds vs milliseconds) or dropping header overhead when computing transactions per block.
  • Assuming block time reduction always increases TPS: shorter block time can raise orphan rate if network propagation is slow, reducing effective throughput.

For your exam

Kinds of questions this topic produces and what full‑marks answers include:

  • Definitions: define layers and terms (block, transaction, mempool, consensus). Full marks require the single‑sentence definition and one responsibility example.
  • Short notes: describe each layer and give responsibilities. Full marks require a labelled stack diagram and 1–2 concise bullets per layer.
  • Derivations: derive TPS formulas or expected time for PoW miners. Full marks require stating assumptions, giving formula derivation step by step, and final expression with units.
  • Numericals: compute TPS or expected mining time. Full marks require showing each calculation step, units, and the final bolded numerical answer.
  • Diagrams/trace: draw P2P propagation and label how a tx becomes included. Full marks require correctly labelled mempool, miner, block header fields and arrows showing flow.
  • Short essays: discuss how to improve throughput safely (layer‑by‑layer approaches: network improvements, block compression, sharding at consensus/execution). Full marks require trade‑offs and at least one example protocol name (refer to other topics by name, e.g., sharding, PBFT).

Quick check

  1. Name three responsibilities of the data/storage layer.
  2. State the Bitcoin formula for expected hashes to find a block and name its key assumption.
  3. If block_time halves but propagation delay stays the same, what exam concern increases?

Answers: 1) store blocks and state, provide pruning/snapshots, produce Merkle proofs; 2) expected_hashes = difficulty × 2^32; assumption: uniform, independent hash trials (Bitcoin difficulty definition); 3) orphan (orphaned/uncle) blocks increase and safety/consistency problems may rise.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?