Bitcoin Architecture
Bitcoin block structure, UTXO model, Bitcoin Script, transaction format, SegWit, Taproot, and BIPs.
Overview
Bitcoin's architecture is the foundation of all cryptocurrencies. Understanding its internals is essential for blockchain development.
Bitcoin Block Structure
UTXO Model
The Unspent Transaction Output (UTXO) model is Bitcoin's way of tracking ownership.
UTXO Properties
| Property | Description | Benefit |
|---|---|---|
| Independence | Each UTXO is discrete | Parallel processing |
| Fungibility | Units are interchangeable | Privacy |
| Divisibility | Can be split | Flexible payments |
| Verifiability | Cryptographic proof | Trustless validation |
Transaction Format
{
"version": 2,
"vin": [
{
"txid": "prev_tx_hash",
"vout": 0,
"scriptSig": "signature pub_key",
"sequence": 0xffffffff
}
],
"vout": [
{
"value": 0.01,
"n": 0,
"scriptPubKey": "OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG"
}
],
"locktime": 0
}
Bitcoin Script
Bitcoin Script is a stack-based, non-Turing-complete language.
Script Execution Stack
Common Script Types
| Script Type | Pattern | Use Case |
|---|---|---|
| P2PKH | OP_DUP OP_HASH160 {hash} OP_EQUALVERIFY OP_CHECKSIG | Legacy addresses (1...) |
| P2SH | OP_HASH160 {hash} OP_EQUAL | Multisig, complex scripts |
| P2WPKH | OP_0 {20-byte-hash} | Native SegWit (bc1q...) |
| P2TR | OP_1 {32-byte-key} | Taproot (bc1p...) |
| P2MS | m {pubkeys} n OP_CHECKMULTISIG | Bare multisig (deprecated) |
SegWit (Segregated Witness)
SegWit separated signature data from transaction data, enabling:
- Block weight reduction: Signatures moved to witness data
- Lightning Network: Enabled through SegWit addresses
- Malleability fix: Transaction IDs no longer change with signature changes
SegWit Transaction Structure
| Field | Legacy | SegWit | Change |
|---|---|---|---|
| Version | 4 bytes | 4 bytes | Same |
| Marker | - | 0x00 | New |
| Flag | - | 0x01 | New |
| Input | VarInt | VarInt | Same |
| Output | VarInt | VarInt | Same |
| Witness | - | Witness data | Moved |
| Locktime | 4 bytes | 4 bytes | Same |
Weight vs Size
Weight = Base Size à 3 + Total Size
Virtual Bytes (vB) = Weight / 4
Base size: 1 byte per input/output overhead Total size: includes witness data
Taproot (BIP-340/341/342)
Taproot introduced Schnorr signatures and Merkle Abstract Syntax Trees (MAST).
Taproot Key Path vs Script Path
Schnorr vs ECDSA
| Feature | ECDSA (secp256k1) | Schnorr (BIP-340) |
|---|---|---|
| Signature Size | ~71-72 bytes | 64 bytes |
| Batch Verification | No | Yes (up to 10x faster) |
| Multi-signature | Individual sigs | Key aggregation (MuSig) |
| Malleability | Potential | Non-malleable |
| Privacy | Multisig visible | Indistinguishable |
Key Size Calculations
Transaction Size
For P2WPKH:
Fee Estimation
Example:
- 1 P2WPKH input + 2 outputs = ~141 vB
- At 10 sat/vB: Fee = 141 Ã 10 = 1,410 satoshis
Bitcoin Improvement Proposals (BIPs)
| BIP | Name | Year | Impact |
|---|---|---|---|
| BIP-32 | HD Wallets | 2012 | Deterministic key generation |
| BIP-39 | Mnemonic Seeds | 2013 | Human-readable seed phrases |
| BIP-44 | Multi-account | 2014 | Hierarchical wallet structure |
| BIP-141 | SegWit | 2015 | Block weight, witness data |
| BIP-148 | UASF | 2017 | User-activated soft fork |
| BIP-340 | Schnorr | 2020 | New signature scheme |
| BIP-341 | Taproot | 2020 | MAST, key/spend paths |
| BIP-342 | Tapscript | 2020 | Script upgrades for Taproot |
Real-World Examples
Bitcoin Transaction Fees Over Time
| Period | Avg Fee (USD) | Avg Fee (sat/vB) | Block Size |
|---|---|---|---|
| 2017 Peak | $55 | 400+ | ~1 MB |
| 2019 Low | $0.50 | 10-20 | ~1 MB |
| 2021 Peak | $60 | 200+ | ~2 MB |
| 2023 Average | $2-5 | 15-30 | ~1.5 MB |
| Ordinals 2023 | $10-50 | 50-200 | ~3-4 MB |
Lightning Network Statistics (2024)
| Metric | Value |
|---|---|
| Channel Capacity | ~5,400 BTC |
| Nodes | ~16,000 |
| Channels | ~75,000 |
| Avg Tx Fee | 0.01-0.1% |
Trade-offs
Legacy vs SegWit vs Taproot
| Aspect | Legacy | SegWit | Taproot |
|---|---|---|---|
| Compatibility | All wallets | Most wallets | Newer wallets |
| Fee Savings | Baseline | 25-40% | 40-60% |
| Privacy | Basic | Basic | Enhanced |
| Smart Contracts | Limited | P2SH | Complex scripts |
| Adoption | 100% | ~70% | ~30% |
UTXO vs Account Model
| Feature | UTXO (Bitcoin) | Account (Ethereum) |
|---|---|---|
| State Tracking | Set of unspent outputs | Account balances |
| Privacy | Better (unlinkable) | Worse (pseudonymous) |
| Parallelism | High | Low |
| Smart Contracts | Limited | Full Turing-complete |
| Atomicity | Built-in | Requires careful coding |
Interview Questions
-
How does the UTXO model ensure transaction atomicity?
- Answer: All inputs must be consumed; if valid, outputs are created atomically. Either the entire transaction succeeds or fails completely.
-
What problem does Taproot solve that SegWit didn't?
- Answer: Taproot adds privacy for complex scripts by allowing them to look like simple payments, and enables key aggregation for multisig via Schnorr signatures.
-
Why is Bitcoin Script non-Turing-complete, and is this a limitation?
- Answer: It lacks loops and has limited opcodes to prevent halting problems and reduce attack surface. This is intentional for security, not a limitation for Bitcoin's use case.
Quiz Questions
-
What is the maximum block weight in SegWit?
- A) 1 MB
- B) 2 MB
- C) 4 MB â
- D) 8 MB
-
Which BIP introduced HD wallet key derivation?
- A) BIP-39
- B) BIP-32 â
- C) BIP-44
- D) BIP-141
-
In a P2PKH transaction, what does OP_CHECKSIG verify?
- A) Transaction amount
- B) Block hash
- C) Signature against public key â
- D) UTXO existence
Practice
- Build a raw Bitcoin transaction manually (testnet)
- Implement a UTXO set tracker in Python
- Write a Bitcoin Script parser for common patterns
- Compare transaction sizes between P2PKH, P2SH, P2WPKH, and P2TR