TeachMeBitcoin

Transaction Locktime (nLockTime): The Anchor Guide to Post-Dating the Ledger

From TeachMeBitcoin, the free encyclopedia Reading time: 4 min

Transaction Locktime (nLockTime): The Anchor Guide to Post-Dating the Ledger

IMPORTANT

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.

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).

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.


🛠️ Modern Use: Anti-Fee Sniping

Most modern wallets (like Bitcoin Core) set nLockTime to the Current Block Height by default.

  1. The Problem: Miners are incentivized to "re-mine" the most recent block if it contains high fees (this is called "Fee Sniping").

  2. The Fix: By setting nLockTime to the current height, the transaction cannot be included in any block older than the current one.

  3. 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.

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:

  1. Identify the physical location of nLockTime in a raw transaction.

  2. Explain the "500 Million Threshold" and how it toggles between blocks and time.

  3. Describe the mandatory relationship between nSequence and nLockTime.

  4. Understand why modern wallets use locktime to prevent "Fee Sniping."

  5. 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:

  1. The 500 Million Threshold: A technical deep dive into the date-limit math.

  2. Mempool Rejection Logic: How nodes filter non-final transactions.

  3. Time-Locked Escrow: Building a 2-of-2 multisig with a time-decay.

  4. 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.

☕ Help support TeachMeBitcoin

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:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!