BIP154 & CompactSize History
BIP154 & CompactSize History
The term "VarInt" is actually used to describe two different things in the Bitcoin codebase. This ambiguity has led to significant confusion for developers.
1. CompactSize (The P2P Format)
What we have been describing—the FD, FE, FF markers—is technically called CompactSize in the Bitcoin Core source code.
- This is the format used for everything in the Transaction and Block messages sent over the network.
2. VarInt (The Database Format)
There is another format, properly called VarInt (or Base-128 VarInt), used inside the local database (LevelDB) on your hard drive.
-
This format uses the MSB (Most Significant Bit) as a continuation flag.
-
It is even more space-efficient than CompactSize but is NOT used in the actual Bitcoin protocol messages.
3. BIP154: Rate Limiting
BIP154 discusses the impact of VarInts on network peer management.
-
Because VarInts are used to count inputs and outputs, nodes use them to quickly estimate the "Work" required to validate a transaction.
-
A peer that sends many transactions with massive VarInt counts might be identified as a "spammer" and disconnected.
4. The "Variable-Length" Philosophy
Satoshi's use of variable-length structures was a radical departure from the fixed-length record formats used in traditional finance (like ISO 8583).
- By making the protocol dynamic, Satoshi ensured that Bitcoin could adapt to future needs (like the massive scripts required for Taproot) without needing to change the underlying serialization logic.
5. Developer Checklist
When someone says "VarInt," you should ask:
-
"Do you mean the CompactSize used in transactions?" (Usually Yes).
-
"Do you mean the Base-128 VarInt used in LevelDB?" (Only if you are writing database code).
| Context | Name in Code | Marker Type |
|---|---|---|
| P2P / Blocks | CompactSize |
Prefix Byte (FD, FE, FF) |
| Local Database | VarInt |
MSB Bit (Base-128) |
In the final section, we will build a Python VarInt Auditor.
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: