Timelock: Difference between revisions

From Bitcoin Wiki
Jump to navigation Jump to search
Belcher (talk | contribs)
m added to catagory technical
Belcher (talk | contribs)
added warning about not locking up coins too far in the future, added note about time locking investment bitcoins, added link to reddit discussion of the uses of CSV
Line 1: Line 1:
A '''Timelock''' is a type of [[encumbrance]] that restricts the spending of some bitcoins until a specified future time or block height.  Timelocks feature prominently in many Bitcoin [[Contracts|contracts]], including [[payment channels]] and [[Hashed Timelock Contracts|hashed timelock contracts]]. It is also used to make [[fee sniping]] less profitable.
A '''Timelock''' is a type of smart contract primitive that restricts the spending of some bitcoins until a specified future time or block height.  Timelocks feature prominently in many Bitcoin [[Contracts|smart contracts]], including [[payment channels]] and [[Hashed Timelock Contracts|hashed timelock contracts]]. It can also be used to lock-up bitcoins held as an investment for a period of months or years. Time lock is also used to make [[fee sniping]] less profitable.


== nLockTime ==
== Primitives ==
 
=== nLockTime ===


''Main article: [[nLockTime]]''
''Main article: [[nLockTime]]''
Line 9: Line 11:
Although every transaction contains the nLockTime field, every wallet up until recently set nLockTime to <code>0</code>, meaning the transaction was valid in any block.  Starting with [[Bitcoin Core]] 0.11.0, every normal transaction automatically generated by began including an nLockTime set to a recent block height as a way to make hypothesized [[fee sniping]] less profitable<ref name="anti_fee_sniping"/>; other wallets are recommended to do the same.
Although every transaction contains the nLockTime field, every wallet up until recently set nLockTime to <code>0</code>, meaning the transaction was valid in any block.  Starting with [[Bitcoin Core]] 0.11.0, every normal transaction automatically generated by began including an nLockTime set to a recent block height as a way to make hypothesized [[fee sniping]] less profitable<ref name="anti_fee_sniping"/>; other wallets are recommended to do the same.


== CheckLockTimeVerify ==
=== CheckLockTimeVerify ===


In late 2015, the BIP65 soft fork<ref name="bip65"/> redefined the NOP2 opcode as the [[CheckLockTimeVerify]] (CLTV) opcode, allowing transaction outputs (rather than whole transactions) to be encumbered by a timelock.  When the CLTV opcode is called, it will cause the script to fail unless the nLockTime on the transaction is equal to or greater than the time parameter provided to the CLTV opcode.  Since a transaction may only be included in a valid block if its nLockTime is in the past, this ensures the CLTV-based timelock has expired before the transaction may be included in a valid block.
In late 2015, the BIP65 soft fork<ref name="bip65"/> redefined the NOP2 opcode as the [[CheckLockTimeVerify]] (CLTV) opcode, allowing transaction outputs (rather than whole transactions) to be encumbered by a timelock.  When the CLTV opcode is called, it will cause the script to fail unless the nLockTime on the transaction is equal to or greater than the time parameter provided to the CLTV opcode.  Since a transaction may only be included in a valid block if its nLockTime is in the past, this ensures the CLTV-based timelock has expired before the transaction may be included in a valid block.
Line 15: Line 17:
CLTV is currently used in [[CLTV-style payment channels]].
CLTV is currently used in [[CLTV-style payment channels]].


== Relative locktime ==
=== Relative locktime ===


In mid-2016, the BIP68/112/113 soft fork gave consensus-enforced meaning to some values in the [[nSequence]] field<ref name="bip68"/> that is a part of every transaction input, creating a "relative locktime"{{citation needed}}.  This allowed an input to specify the earliest time it can be added to a block based on how long ago the output being spent by that input was included in a block on the block chain.
In mid-2016, the BIP68/112/113 soft fork gave consensus-enforced meaning to some values in the [[nSequence]] field<ref name="bip68"/> that is a part of every transaction input, creating a "relative locktime"{{citation needed}}.  This allowed an input to specify the earliest time it can be added to a block based on how long ago the output being spent by that input was included in a block on the block chain.


== CheckSequenceVerify ==
=== CheckSequenceVerify ===


Also part of the BIP68/112/113 soft fork was the [[CheckSequenceVerify]] opcode<ref name="bip112"/>, which provides for relative locktime the same feature CLTV provides for absolute locktime.  When the CSV opcode is called, it will cause the script to fail unless the nSequence on the transaction indicates an equal or greater amount of relative locktime has passed than the parameter provided to the CSV opcode.  Since an input may only be included in a valid block if its relative locktime is expired, this ensures the CSV-based timelock has expired before the transaction may be included in a valid block.
Also part of the BIP68/112/113 soft fork was the [[CheckSequenceVerify]] opcode<ref name="bip112"/>, which provides for relative locktime the same feature CLTV provides for absolute locktime.  When the CSV opcode is called, it will cause the script to fail unless the nSequence on the transaction indicates an equal or greater amount of relative locktime has passed than the parameter provided to the CSV opcode.  Since an input may only be included in a valid block if its relative locktime is expired, this ensures the CSV-based timelock has expired before the transaction may be included in a valid block.


CSV is used by [[Lightning Network]] transactions.
CSV is used by [[Lightning Network]] transactions.
== Far-future locks ==
It is not advised to lock up bitcoins into the far future because it takes on risk of the bitcoin network changing. For example, if there were an ECDSA or RIPEMD160 algorithm break that made any coins spendable with a few months of VPU time, the network might need to to prohibit moving old unspent coins after some transition, but long locktimed coins could not make such a transition.
OP_CheckSequenceVerify allows locking for at most 65535 blocks (about 455 days) or for at most 65535*512 seconds (about 388 days). OP_CheckLockTimeVerify could be used to lock up coins for several centuries.
== See Also ==
* [https://coinb.in/#newTimeLocked coinb.in's time-locked page] javascript page for creating time-locked bitcoin wallets.
* [https://www.reddit.com/r/Bitcoin/comments/4p4klg/bitcoin_core_project_the_csv_soft_fork_has/d4i01he/ Reddit discussion of the possible uses OP_CHECKSEQUENCEVERIFY]


== References ==
== References ==

Revision as of 17:30, 20 August 2017

A Timelock is a type of smart contract primitive that restricts the spending of some bitcoins until a specified future time or block height. Timelocks feature prominently in many Bitcoin smart contracts, including payment channels and hashed timelock contracts. It can also be used to lock-up bitcoins held as an investment for a period of months or years. Time lock is also used to make fee sniping less profitable.

Primitives

nLockTime

Main article: nLockTime

A part of the original Bitcoin implementation[citation needed], nLockTime is a field that specifies the earliest time a transaction may be added to a valid block. A later Bitcoin soft fork[citation needed] allowed nLockTime to alternatively specify the lowest block height a transaction may be added to a valid block.

Although every transaction contains the nLockTime field, every wallet up until recently set nLockTime to 0, meaning the transaction was valid in any block. Starting with Bitcoin Core 0.11.0, every normal transaction automatically generated by began including an nLockTime set to a recent block height as a way to make hypothesized fee sniping less profitable[1]; other wallets are recommended to do the same.

CheckLockTimeVerify

In late 2015, the BIP65 soft fork[2] redefined the NOP2 opcode as the CheckLockTimeVerify (CLTV) opcode, allowing transaction outputs (rather than whole transactions) to be encumbered by a timelock. When the CLTV opcode is called, it will cause the script to fail unless the nLockTime on the transaction is equal to or greater than the time parameter provided to the CLTV opcode. Since a transaction may only be included in a valid block if its nLockTime is in the past, this ensures the CLTV-based timelock has expired before the transaction may be included in a valid block.

CLTV is currently used in CLTV-style payment channels.

Relative locktime

In mid-2016, the BIP68/112/113 soft fork gave consensus-enforced meaning to some values in the nSequence field[3] that is a part of every transaction input, creating a "relative locktime"[citation needed]. This allowed an input to specify the earliest time it can be added to a block based on how long ago the output being spent by that input was included in a block on the block chain.

CheckSequenceVerify

Also part of the BIP68/112/113 soft fork was the CheckSequenceVerify opcode[4], which provides for relative locktime the same feature CLTV provides for absolute locktime. When the CSV opcode is called, it will cause the script to fail unless the nSequence on the transaction indicates an equal or greater amount of relative locktime has passed than the parameter provided to the CSV opcode. Since an input may only be included in a valid block if its relative locktime is expired, this ensures the CSV-based timelock has expired before the transaction may be included in a valid block.

CSV is used by Lightning Network transactions.

Far-future locks

It is not advised to lock up bitcoins into the far future because it takes on risk of the bitcoin network changing. For example, if there were an ECDSA or RIPEMD160 algorithm break that made any coins spendable with a few months of VPU time, the network might need to to prohibit moving old unspent coins after some transition, but long locktimed coins could not make such a transition.

OP_CheckSequenceVerify allows locking for at most 65535 blocks (about 455 days) or for at most 65535*512 seconds (about 388 days). OP_CheckLockTimeVerify could be used to lock up coins for several centuries.

See Also

References

  1. Bitcoin Core 0.11.0 release notes
    Bitcoin.org
    Retrieved 2016-11-06
  2. BIP65: OP_CHECKLOCKTIMEVERIFY
    Peter Todd
    Retrieved 2016-04-12
  3. BIP68: Relative lock-time using consensus-enforced sequence numbers
    Mark Friedenbach, BtcDrak, Nicolas Dorier, and kinoshitajona
    Retrieved 2016-04-12
  4. BIP112: CHECKSEQUENCEVERIFY
    BtcDrak, Mark Friedenbach, Eric Lombrozo
    Retrieved 2016-04-12