You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: decoding/compact-size.mdx
+159-1Lines changed: 159 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -14,4 +14,162 @@ images:
14
14
parent: "technical-foundation"
15
15
---
16
16
17
-
(coming soon)
17
+
Compact Size (also known as CompactSize or var_int) is a variable-length integer encoding used in Bitcoin to efficiently represent numbers while minimizing space usage. It's primarily used in transaction data and network messages to indicate:
18
+
19
+
- Number of inputs/outputs in a transaction
20
+
- Script sizes
21
+
- Number of witness elements
22
+
- Length of upcoming data fields
23
+
24
+
## Structure
25
+
26
+
The encoding uses a prefix byte to determine how to read the subsequent bytes:
- Most transactions use single-byte encoding (≤ 252)
170
+
- The FF prefix (8-byte numbers) is rarely used in practice
171
+
- All multi-byte numbers must be encoded in little-endian format
172
+
- This encoding has been part of Bitcoin since its first release
173
+
</ExpandableAlert>
174
+
175
+
This variable-length integer encoding plays a crucial role in keeping transaction and block data compact while maintaining flexibility for larger values when needed.
Copy file name to clipboardExpand all lines: decoding/endianness.mdx
+58-1Lines changed: 58 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -75,7 +75,7 @@ Big-endian is considered more "human-readable" because the data is stored in the
75
75
76
76
Little-endian stores the **least significant byte** first. This might feel counterintuitive to humans but is more efficient for modern processors.
77
77
78
-
Using the same number **12345678** (`0x00BC614E`), here’s how it looks in **little-endian**:
78
+
Using the same number **12345678** (`0x00BC614E`), here's how it looks in **little-endian**:
79
79
80
80
<CodeSnippet
81
81
language="Hex"
@@ -146,3 +146,60 @@ Most modern CPUs are little-endian and the network protocols typically use big-e
146
146
This duality requires developers to frequently convert between the two formats when working with Bitcoin data.
147
147
148
148
---
149
+
150
+
## 4. Working with Endianness in Code
151
+
152
+
When working with Bitcoin data, you'll frequently need to convert between little-endian and big-endian formats. Here are some common Python functions for handling endianness:
153
+
154
+
### Little-Endian to Integer
155
+
156
+
<CodeSnippet
157
+
language="python"
158
+
code={`def little_endian_to_int(b):
159
+
'''Convert little-endian bytes to integer'''
160
+
return int.from_bytes(b, 'little')
161
+
162
+
# Example usage
163
+
bytes_data = bytes.fromhex('4E61BC00') # 12345678 in little-endian
164
+
number = little_endian_to_int(bytes_data)
165
+
print(number) # Output: 12345678`}
166
+
/>
167
+
168
+
### Integer to Little-Endian
169
+
170
+
<CodeSnippet
171
+
language="python"
172
+
code={`def int_to_little_endian(n, length):
173
+
'''Convert integer to little-endian bytes of specified length'''
174
+
return n.to_bytes(length, 'little')
175
+
176
+
# Example usage
177
+
number = 12345678
178
+
bytes_data = int_to_little_endian(number, 4)
179
+
print(bytes_data.hex()) # Output: 4e61bc00`}
180
+
/>
181
+
182
+
### Common Gotchas
183
+
184
+
1.**Byte Order Confusion**: When reading transaction IDs or hashes:
185
+
186
+
<CodeSnippet
187
+
language="python"
188
+
code={`# INCORRECT - reading txid directly from hex
When miners select transactions for inclusion in a block, they don't just look at the raw fee amount, they evaluate transactions based on their _fee rate_.
Even though Transaction A pays a higher absolute fee, Transaction B is more profitable for miners because it provides more fees per unit of block space.
0 commit comments