Transaction Locktime (nLockTime): The Anchor Guide to Post-Dating the Ledger
Transaction Locktime (nLockTime): The Anchor Guide to Post-Dating the Ledger
Executive Summary: nLockTime is a 4-byte (32-bit) field located at the absolute end of every Bitcoin transaction. It defines the earliest point in time—either a specific Block Height or a Unix Timestamp—at which a transaction is eligible to be included in a block. A transaction with a future locktime is considered "Non-Final" and is rejected by the network until the specified target is reached. This field is the foundation of Bitcoin's "Post-Dated Check" system.
🔍 Why This Module Matters
In a traditional bank, you can write a check today but date it for next month. Bitcoin provides this same capability through nLockTime. This field allows for the creation of complex financial contracts, escrow agreements, and "Anti-Fee Sniping" protections. This module will deconstruct the "Absolute Timelock" mechanism, explain how the network differentiates between blocks and seconds, and show why this 4-byte field is the ultimate gatekeeper of transaction finality.
🏛️ The Dual-Mode Logic: Blocks vs. Time
The nLockTime field interprets its 32 bits differently depending on the value of the number.
1. Block Height Mode (Values < 500,000,000)
If the value is less than 500 million, Bitcoin treats it as a Block Height.
- Example: A locktime of
850,000means the transaction cannot be mined until the blockchain reaches height 850,000.
2. Unix Timestamp Mode (Values >= 500,000,000)
If the value is 500 million or greater, Bitcoin treats it as a Unix Timestamp (seconds since Jan 1, 1970).
- Example: A locktime of
1,715,000,000corresponds to a specific date and time in the year 2024.
graph TD
A[nLockTime Value] --> B{Value < 500M?}
B -- YES --> C[Lock until BLOCK HEIGHT]
B -- NO --> D[Lock until UNIX TIMESTAMP]
style C fill:#9f9,stroke:#333,stroke-width:2px
style D fill:#f96,stroke:#333,stroke-width:2px
⚙️ The nSequence Connection: The "Master Switch"
A common mistake for beginners is forgetting that nLockTime depends on the nSequence field of the transaction's inputs.
-
The Rule: If ALL inputs in a transaction have an
nSequencevalue of0xFFFFFFFF, thenLockTimefield is Ignored. The transaction is final immediately. -
The Requirement: To make a transaction respect its locktime, at least one input must have a sequence value less than
0xFFFFFFFF(usually0xFFFFFFFE).
🛠️ Modern Use: Anti-Fee Sniping
Most modern wallets (like Bitcoin Core) set nLockTime to the Current Block Height by default.
-
The Problem: Miners are incentivized to "re-mine" the most recent block if it contains high fees (this is called "Fee Sniping").
-
The Fix: By setting
nLockTimeto the current height, the transaction cannot be included in any block older than the current one. -
The Result: This discourages miners from attempting to rewrite history, as the high-fee transactions they want to steal wouldn't be valid in their secret, shorter chain.
⚖️ nLockTime vs. OP_CLTV
It is important to distinguish between the transaction-level lock and the script-level lock.
-
nLockTime: A property of the Transaction. It determines if the transaction as a whole is valid yet.
-
OP_CHECKLOCKTIMEVERIFY (CLTV): A property of the Output Script. It allows a sender to create a "Frozen Output" that cannot be spent by any transaction until a certain time has passed.
| Feature | nLockTime | OP_CLTV |
|---|---|---|
| Location | Final 4 bytes of TX | Inside ScriptPubKey |
| Impact | Entire Transaction | Specific UTXO |
| Logic | "Can I mine this yet?" | "Can I spend this coin yet?" |
🎯 Learning Objectives for this Module
By the end of this module, you will be able to:
-
Identify the physical location of
nLockTimein a raw transaction. -
Explain the "500 Million Threshold" and how it toggles between blocks and time.
-
Describe the mandatory relationship between
nSequenceandnLockTime. -
Understand why modern wallets use locktime to prevent "Fee Sniping."
-
Contrast transaction-level locktime with script-level CLTV.
🗺️ Module Roadmap: What's Next?
Now that we've seen the "Timer" on a transaction, we will explore its math:
-
The 500 Million Threshold: A technical deep dive into the date-limit math.
-
Mempool Rejection Logic: How nodes filter non-final transactions.
-
Time-Locked Escrow: Building a 2-of-2 multisig with a time-decay.
-
Python Locktime Decoder: Writing a script to convert 4-byte LE hex into a readable date.
🎓 Summary
nLockTime is the "Clock" of the Bitcoin ledger. It provides the protocol with a sense of time, allowing for financial instruments that are anchored to the future. By mastering the nuances of the locktime field, you are understanding the fundamental temporal constraints that make Bitcoin a programmable, multi-generational store of value.
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: