Bitcoin cannot do anything later. There is no scheduler, no cron, no callback, and no way for a script to wake up. Every state change in the system happens because somebody broadcast a transaction and a miner included it.
So a timelock does the only thing available: it makes a transaction invalid until a condition on the chain is met. Nothing happens at the deadline. What changes is that a transaction which every node would have rejected a moment earlier becomes acceptable, and whoever holds it can now broadcast it. The delay is enforced by refusal rather than by action, and that inversion is the key to reading any contract built on it.
What does a timelock actually lock?
Either the whole transaction or one input, and the distinction decides everything that follows.
There are two axes. A lock is absolute — valid after a stated height or time — or relative, valid only after a stated interval has passed since the coin being spent was confirmed. And a lock is enforced either by a field in the transaction, which anybody constructing it may choose, or by an opcode in the output’s script, which the spender has no choice about.
Crossing the axes gives the four mechanisms:
field in the tx opcode in the script
absolute nLockTime OP_CHECKLOCKTIMEVERIFY
relative nSequence (BIP 68) OP_CHECKSEQUENCEVERIFY
The field forms are a sender’s convenience. The opcode forms are a constraint the coin carries: whoever wrote the script decided, at the time the coins were received, that no spend would be possible before the delay. That is the version that makes contracts possible.
How does an absolute timelock work?
Through a single 32-bit field, interpreted two ways, with one condition attached.
Every transaction has an nLockTime. If its value is below 500,000,000 it
is read as a block height; at or above that, as a Unix timestamp — the
threshold is a constant in Bitcoin Core, chosen because no plausible block
height would collide with a plausible epoch time. A transaction with
nLockTime set to height N cannot be included in a block at height N
or earlier, so the first block that can carry it is N+1.
The condition is easy to miss and has cost people real money: nLockTime
is only enforced if at least one input has a sequence number other than
0xffffffff. That value is named SEQUENCE_FINAL in the source, and it
means exactly what it says — this input is final, so do not bother checking
the lock. A transaction with a carefully chosen lock time and every
sequence left at its default is spendable immediately.
For the timestamp interpretation, the comparison is not against the block’s own timestamp. BIP 113 moved it to the median of the previous eleven blocks’ timestamps, because a miner controls its own header’s clock within loose bounds and could otherwise pull a time-based lock forward. The median is not gameable by a single miner, at the cost of running roughly an hour behind the wall clock.
Most wallets set a non-trivial nLockTime on ordinary payments anyway, for
an unrelated reason: a transaction locked to the current height cannot be
included in a reorganised block at a lower height, which removes the
incentive for a miner to re-mine the previous block to capture its fees.
That is anti-fee-sniping, and it is why an ordinary wallet transaction has
a lock time that looks meaningful and is not.
Why does a script need CHECKLOCKTIMEVERIFY at all?
Because a field the spender chooses constrains nobody.
nLockTime is set by whoever builds the transaction. If an output is to be
unspendable until 2030, the restriction cannot live in a field the spender
fills in. It has to live in the output’s script, which the spender must
satisfy.
OP_CHECKLOCKTIMEVERIFY, added by BIP 65 and active since block 388,381,
does this indirectly and rather elegantly. The opcode does not know the
current height. It compares the value on the stack against the spending
transaction’s own nLockTime, fails unless the lock time is at least as
large and of the same type, and fails if that input’s sequence is final.
Combined with the existing consensus rule that a transaction cannot be
mined before its lock time, the result is that the spender is forced to
declare a lock time no earlier than the script demands, and the network
then refuses the transaction until that point.
So the opcode borrows the enforcement it needs from a rule that already existed. The same trick explains why the two forms of absolute lock are linked rather than independent: one is the sender’s promise, the other makes that promise mandatory.
What makes a relative timelock different?
It counts from the coin’s confirmation rather than from a fixed point, which is what makes a protocol out of a delay.
BIP 68 reinterpreted the sequence number of each input, for transactions of
version 2 or higher. When bit 31 is clear, the low sixteen bits are a
relative lock: in blocks by default, or in units of 512 seconds if bit 22
is set. Sixteen bits caps the range at 65,535 blocks — around 455 days — or
65,535 units of 512 seconds, around 388 days. OP_CHECKSEQUENCEVERIFY
(BIP 112) is the script-enforced counterpart, working the same indirect way
CLTV does, against the input’s sequence rather than the transaction’s lock
time. Both activated together with BIP 113 at block 419,328.
The granularity is worth noticing. Time-based relative locks move in roughly eight-and-a-half-minute steps because the encoding had sixteen bits to spend and needed to cover a year; there is no finer option. Block-based locks are exact in blocks and approximate in time, which is usually the better choice precisely because an attacker cannot make blocks arrive faster.
What makes the relative form powerful is that the clock starts when the coin lands. A transaction can be signed today, spending an output that does not exist yet, with the guarantee that it will be unspendable for a day after that output confirms. That is a signed statement about the future with no trusted party keeping it.
Where do relative timelocks earn their keep?
In every construction where one party needs time to respond to another.
Lightning is the clearest case. A channel’s current state is kept enforceable by a pair of asymmetric transactions, and the party who publishes one must wait out a relative delay before claiming its own output. That delay is the window in which the counterparty, if the published state was an old one, can take the funds instead using a revocation key. The security of the channel is literally the length of that delay against the counterparty’s ability to get a transaction confirmed inside it — which is why Lightning’s failure modes are all, in the end, about fees and about being online.
The same shape appears elsewhere. A vault spends to an intermediate output with a delay before the coins become freely spendable, giving the owner a window to intervene with a recovery path if the first spend was not theirs. An inheritance arrangement gives a beneficiary a key that only works after a long absolute lock, with the owner periodically moving the coins to reset it — a deadman switch with no third party. Atomic swaps use matched timelocks so that a party who stops cooperating loses nothing but time.
In each case the delay is not a feature of the application. It is the only primitive available for “and if you do not respond, this happens instead.”
What goes wrong in practice?
Three failures recur, and two of them have no fix inside the protocol.
The fee you signed is the fee you get. A presigned timelocked transaction carries a fee chosen when it was signed, possibly years before it is broadcast. If fee rates have risen since, it may be unminable at the moment it matters most. This is the single hardest problem in vault and channel design, and the mitigations — fee-bumping outputs, anchor outputs, package relay — are all ways of attaching fresh money to a stale transaction rather than ways of fixing the stale transaction.
Relative locks reset. The clock starts at the confirmation of the output being spent. Move coins for any reason and every relative delay begins again, which is correct and routinely surprising. A wallet that consolidates inputs can quietly restart a delay someone was depending on.
A long absolute lock is a bet on key survival. A coin locked until 2035 is not safer for being locked; it is unreachable by its owner as well as by a thief, for a decade in which the key can be lost, the backup can rot, the software that understands the script can disappear, and the person who remembers the arrangement can die. Timelocks concentrate risk in the things a wallet cannot protect, and they remove exactly the option — move the coins now — that most other mistakes leave open.
Check it yourself
Create and inspect a timelocked transaction without broadcasting it. The decoded output shows both the lock time and the sequence numbers that decide whether it is enforced:
bitcoin-cli decoderawtransaction <hex> | grep -E 'locktime|sequence'
To see the anti-fee-sniping behaviour, decode any recent transaction from
your own wallet and compare its locktime to the current height:
bitcoin-cli getblockcount
They will usually be within a block or two of each other, which tells you the lock time is there for reorg protection rather than for any delay the sender intended.
What remains uncertain
Whether presigned transactions can ever be fee-robust. Every current answer attaches a second input or output that can be bumped later, which costs block space, complicates the script, and assumes the bumping party is present. Package relay improves the mechanics without changing the premise that someone must show up with money. A design where a years-old signed transaction is reliably minable on demand does not exist.
How long the long locks will hold up socially. Multi-decade locks are being written now, by people using software that will not be maintained that long, in script forms a future wallet may not recognise. Nothing in the protocol will forget them. Whether the surrounding tooling remembers how to spend them is an open question that only time answers, and the people who will find out are not the ones who set them.
Whether the 512-second granularity will ever matter. It was a reasonable encoding choice in 2016 and it quietly caps what time-based relative locks can express. Nobody has proposed changing it, partly because block-based locks are the better tool in almost every case where the difference would show.
Sources
- PrimaryBIP 65 — OP_CHECKLOCKTIMEVERIFY ↗
- PrimaryBIP 68 — Relative lock-time using consensus-enforced sequence numbers ↗
- PrimaryBIP 112 — CHECKSEQUENCEVERIFY ↗
- PrimaryBIP 113 — Median time-past as endpoint for lock-time calculations ↗
- First-partyBitcoin Core — src/primitives/transaction.h ↗
- First-partyBitcoin Core — src/consensus/tx_verify.cpp ↗