VarInt: The Anchor Guide to Variable-Length Integers
VarInt: The Anchor Guide to Variable-Length Integers
Executive Summary: A VarInt (Variable-Length Integer), technically known as CompactSize, is a serialization format used by Bitcoin to store integers with maximum space efficiency. Because most "counts" in Bitcoin (like the number of inputs or script lengths) are small numbers, VarInt allows them to be stored in just a single byte. For larger numbers, it uses a "Sentinel Byte" system to signal that more bytes follow. This optimization is the primary reason the Bitcoin blockchain remains manageable in size.
🔍 Why This Module Matters
Every byte on the Bitcoin blockchain costs money (fees) and storage (node hardware). If Bitcoin used a standard 8-byte integer for every "count" field, the blockchain would be nearly double its current size today. VarInt is the "Compression" engine of the protocol. This module will deconstruct the "Sentinel Byte" logic, explain the four tiers of integer magnitude, and show you how to spot a VarInt inside a raw hex transaction dump.
🏛️ The Sentinel Logic: Scaling on Demand
The brilliance of VarInt is that the First Byte acts as a traffic controller.
Tier 1: The "Lite" Mode (Value < 253)
-
The Rule: If the first byte is any value between
0x00and0xFC(0 to 252). -
The Result: The byte is the value. No further reading is required.
-
Use Case: 99% of transaction input/output counts.
Tier 2: The "Extended" Modes (Sentinels)
If the first byte is 0xFD, 0xFE, or 0xFF, it is a Sentinel. It means: "Stop! Don't use my value. Look at the bytes that follow me."
| Sentinel | Followed By | Max Value | Typical Use Case |
|---|---|---|---|
0xFD |
2 Bytes | 65,535 | Long Output Scripts (Multisig). |
0xFE |
4 Bytes | 4,294,967,295 | Large Mempool counts. |
0xFF |
8 Bytes | $1.8 \times 10^{19}$ | Theoretical max (Total Satoshis). |
⚙️ Visualizing VarInt in Raw Hex
Let's look at how the number 500 is stored in a Bitcoin transaction.
-
500 is greater than 252, so we need a sentinel.
-
500 fits in 2 bytes, so we use the
0xFDsentinel. -
The Hex:
FD F4 01(Little-Endian representation of 500).
graph TD
A[Read First Byte] --> B{Byte < 0xFD?}
B -- YES --> C[Value = Byte]
B -- NO --> D{Is it 0xFD?}
D -- YES --> E[Read next 2 bytes]
D -- NO --> F{Is it 0xFE?}
F -- YES --> G[Read next 4 bytes]
F -- NO --> H[Read next 8 bytes]
style C fill:#9f9,stroke:#333,stroke-width:2px
style E fill:#f96,stroke:#333,stroke-width:2px
🛠️ Where are VarInts Used?
You will encounter VarInts every time you parse a transaction:
-
TX_IN Count: The number of inputs in the transaction.
-
TX_OUT Count: The number of outputs.
-
Script Length: The number of bytes in the
scriptSigorscriptPubKey. -
Message Size: The length of P2P network messages.
Note: VarInt is not used for the block size itself (which is a fixed 4-byte uint32) or for the transaction version. It is only used for "Variable-Length" fields.
🛡️ VarInt vs. Fixed-Length: The Performance Trade-off
| Feature | Fixed-Length (uint32) | VarInt (CompactSize) |
|---|---|---|
| Size | Always 4 Bytes | 1, 3, 5, or 9 Bytes |
| Complexity | Low (Easy to parse) | High (Conditional logic) |
| Efficiency | Poor (Wastes space) | Excellent (Saves space) |
| Protocol | Block Headers, Version | Transactions, P2P Traffic |
🎯 Learning Objectives for this Module
By the end of this module, you will be able to:
-
Define a VarInt and its primary purpose in the Bitcoin protocol.
-
Identify the four sentinel bytes and the magnitude of data they represent.
-
Explain why the first byte is the "Key" to decoding a VarInt.
-
Describe the cumulative impact of VarInt on blockchain storage size.
-
Differentiate between fields that use VarInt and those that use fixed-length integers.
🗺️ Module Roadmap: What's Next?
Now that we've seen the "Space-Saving" logic, we will look at the implementation:
-
VarInt Logic: A deep dive into the
CompactSizespecification. -
Fixed vs. VarInt: Why some fields were chosen to be variable.
-
VarInt History: How the format evolved from Satoshi's original code.
-
Python VarInt Auditor: Writing a script to read and write VarInt bytes.
🎓 Summary
VarInt is the "Micro-Optimizer" of the Bitcoin network. It is a simple yet powerful example of how every bit and byte of the protocol is tuned for long-term sustainability. By mastering the logic of variable-length integers, you are understanding the engineering discipline required to build a global financial system that can survive for centuries on limited resources.
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: