MTP (Median Time Past)
Bitcoin uses the median of the last 11 block timestamps as the chain's own clock. Here's why - and how it stops miners from gaming time-locked transactions.
Median Time Past (MTP) is the median of the previous 11 block headers' timestamps. It's used as the chain's own clock for time-based consensus rules in Bitcoin, replacing the more easily-manipulated per-block timestamp.
The problem MTP solves: miners have limited but real discretion over block timestamps. The protocol allows a block's timestamp to run up to two hours ahead of a node's clock, and behind real time by as much as the median of the previous eleven blocks allows, usually about an hour. A single miner could push their timestamp forward to manipulate time-based logic - like making time-locked outputs spendable earlier than the depositor intended.
How MTP fixes it: by taking the median of the previous 11 blocks' timestamps, the network gets a value that:
- Resists single-miner manipulation. One miner setting a wildly skewed timestamp doesn't move the median significantly.
- Stays close to real time. The median of 11 honest miners' timestamps approximates real time well, within a small bias.
- Increases monotonically. As new blocks arrive, MTP never moves backward (it can stay the same from one block to the next), providing a stable monotonic clock.
Where MTP is used:
- Every block's own timestamp. A block must be stamped later than the MTP of the eleven blocks before it, or nodes reject it as
time-too-old. This floor is in Satoshi's 2009 code; see block timestamp rules. - BIP-113 locktimes. Time-based
nLockTimeand CLTV evaluations use MTP as the reference, not the current block's timestamp. - BIP-68 relative locktimes and CSV.
For most users, MTP is invisible. For protocol designers building time-locked constructions, it's the precise notion of "time" Bitcoin actually uses internally. Treating block timestamps as raw real-time gives wrong answers; treating MTP as real-time gives essentially correct ones.
The 11-block window is a sweet spot: large enough to resist manipulation, small enough to stay close to real time. Average lag from real time is around 60 minutes (half the window). A miner with most of the hash rate can do what a single miner cannot and hold the median back for weeks, which is the attack The Time Warp walks through.
Key takeaways
- Prevents outlier timestamps from messing with time-based rules
- Ensures consistent locktime functionality and consensus enforcement
- A rolling median of 11 blocks for robust approximation of real time
Related chapters
On the mailing list
The topic timeline: Time warp, timestamps and the consensus cleanup (59 threads).
Corrected
-
Was: The entry cited no sources, left out the median's first job (every block must be stamped later than it), said a timestamp may be off by a couple of hours either way, and said the median can only move forward.
Now: Four sources added (chain.h, validation.cpp, BIP-113, Wuille 2011). The time-too-old floor is named first. A timestamp may run two hours ahead of a node's clock and behind real time by as much as the median allows, usually about an hour. The median never moves backward and can stay the same from one block to the next. Details
Sources (4)
- Bitcoin Core - src/chain.h: GetMedianTimePast sorts the timestamps of the last nMedianTimeSpan = 11 blocks and returns the middle one
- Bitcoin Core - src/validation.cpp: ContextualCheckBlockHeader rejects a block stamped at or below the previous block's median time past (time-too-old)
- BIP-113 - Median time-past as endpoint for lock-time calculations: time-based lock times compare against the median of the past 11 blocks, which advances monotonically
- Pieter Wuille - bitcoin-dev, Difficulty adjustment / time issues (November 7, 2011): a block's timestamp must lie between the median of the previous eleven, roughly five blocks in the past, and two hours in the future
Related terms (3)
Published . Revised . One correction logged for this page.
Edit this page on GitHub