Bitcoin allows the ability to have it's transactions encumbered by explicit absolute or relative timelocks, through transaction-level or script-level locks by specifying either block heights or timestamps. These locks control the validity of an entire transaction or a specific transaction input. The transaction-level locks are determined by the values in the locktime field (nLockTime
) of a transaction and the sequence field of each input in a transaction (nSequence
). This functionality is in addition to their existing uses to help prevent fee sniping for example in the case of nLockTime
, and signaling opt-in RBF or ignoring the nLocktime
in the case of nSequence
.
Note:
nSequence
was originally envisioned to be used for ordering unconfirmed transactions in the mempool, however, it was given a new lease on life in BIP 68 to supplement its existing use case(s) to include timelocks.
For more granular control over a transaction's outputs, BIP 65 and BIP 112 were introduced to allow embedding the timelock in a transaction's script, these script operator codes are OP_CheckLockTimeVerify
(OP_CLTV
) and OP_CheckSequenceVerify
(OP_CSV
) respectively. Both allow individual transaction outputs and not entire transactions to be restricted by either an absolute or relative timelock or both. They force the nLockTime
or nSequence
values in a transaction spending OP_CLTV
and or OP_CSV
locked outputs to be set to a value greater than or equal to the timelock of their operator arguments.
The key difference between OP_CLTV
and OP_CSV
has been nicely summed up by Nicolas Dorier on Reddit:
With CheckSequenceVerify you can say: I want this output to be spendable when it has X confirmation. (relative to time of mining)
With CheckLocktimeVerify you can say: I want this output to be spendable at height X. (absolute time)
It is also possible to lock a particular output with both an absolute and relative timelock, in which case the following could be an example scenario:
I want this output to be spendable after time X and after Y confirmations.
Timelock wiki
Bitcoin Time Locks article
Bitcoin Timelocks in a nutshell article
What does the sequence in a transaction input mean?
stackexchange
CLTV vs NLockTime stackexchange
nSequence/nLocktime vs CHECKSEQUENCEVERIFY vs CHECKLOCKTIMEVERIFY reddit
[1] The terms "transaction-level" and "script-level" refer to where the encumbrance rules are specified or found in a transaction's structure; "transaction-level" refers to the fields in a transaction, while "script-level" refers to instructions set in the transaction's locking script (i.e. ScriptPubKey).
[2] The n
in nSequence
and nLockTime
stands for 'number'.
"The concept of prefixing variable names with information about their type is called Hungarian notation. It was common in Windows programming in the '90s, and now is generally considered a bad idea." - Pieter Wuille
MIT © Zero-1729