locktime

Locktime is a field added to the end the Bitcoin script that postdates the earliest time or block that a transaction can validly be added to the blockchain. The program can set locktime to an actual time or to the block height.

Adding a locktime to the script means that the transaction will only be confirmed and the new block added once the requisite time has elapsed or the block height has been met. Put another way, when a this function is added to the script, the transaction is not valid until the set time or height. You can think of locktime being like a post-dated cheque; the cheque is not processed until the named date.

The network’s nodes validate each transaction and block received. Transactions with locktimes are therefore rejected until the specific time or height set by the script is reached. It is also the last field in the script, which you can see below.

Locktime in the Script

Locktime is the transaction-level, absolute time lock and is the only time lock that was part of the creator Satoshi Nakamoto’s original program. Most transactions do not use a set locktime. So in most cases, the transaction’s locktime is set to 0x00000000.

Below is a Bitcoin transaction. Note that the last list of 00000000 is the locktime. Note also that if the locktime was disabled, the default remains which looks like this: 0xffffffff

Transaction Example:

0100000001(Version) (INPUT)7967a5185e907a25225574544c31f7b059c1a191d65b53dcc1554d339c4f9efc (TXID) 01000000 (VOUT) 6a (scriptSig Size) 47304402206a2eb16b7b92051d0fa 38c133e67684ed064effada1d7f925c842da401d4f2270220 1f196b10e6e4b4a9fff948e5c5d71ec5da53e90529c8dbd122bff2b1d21dc8a90121039b7bcd0824b9a9164f7ba098408e63e5b7e3cf90835cceb19868f54f8961a825 (Input cont.) ffffffff (Sequence) 01(Output Count) 4baf210000000000 (Value) 19 (OUTPUT/scriptPubKey Size) 76a914db4d1141d0048b1ed 15839d0b7a4c488cd368b0e88ac (scriptPubKey) 00000000(LOCKTIME!!!!)

Time Reading Example:

0x06241559: This time is set to Fri, 12 May 2017 02:55:02 in Unix Time, and so can only be relayed to the network after the median time of the last 11 blocks.

Why Use This Function?

Locktimes are useful in the event that the signer(s) decides to change their mind. In the case that a signer does not agree to the transaction, they can instead add a new non-locktime transaction. The new transaction will use one of the same outputs from the inputs. The input is derived from what was originally an input from the locktimed transaction. These transactions are invalid until the time set in the script, so long as the new transaction is added to the blockchain before the time lock expires.

This is an important caveat to remember with locktimes. Locktimes are related to block time, which is the average time it takes to generate an extra block in the blockchain. Block time, however, can be up to two hours ahead of real time. Moreover, blocks are not created at specific intervals. This means that there is no precise measurement to know exactly when a block is added. It is therefore important that if the signatory wants to invalidate the transaction they do so as much in advance of the locktime as possible. Because of the way time operates in the Bitcoin blockchain, it is very possible that a node will approve the transaction two hours earlier than the time written into the script even if it has a time set.

Unix Time

There are two ways to set a locktime, using block height or a specific time. Bitcoin’s also uses Unix Time. Unix Time is a measurement of time used to describe a point in time. The clock began at 00:00:00 Thursday, 1 January 1970, a Coordinated Universal Time. Every day is measured in seconds and is counted as if the day contains exactly 86400 seconds.

Time and Block Height

Locktime can be set to make sure that a transaction is locked until a specific block height or a point in time. So the locktime has a dual-function by setting either a block height or time.

In the script they will look like this:

  • Unlocked at block height: < 500000000
    • Essentially this means that the locktime needs to be set to anything above the current block height
  • Unlocked at specific time: >= 500000000
    • As mentioned, this is measured in Unix Time. This can be more or less specific as well, but to if there is a locktime it must be set later than the current Unix Time.
  • No specific locktime field: 0x00000000 (or anything below the current block height or Unix time).

Variations of Bitcoin Core

The ability to disable locktime was not an original feature. However, setting all sequence numbers to the default 0xffffffff can still disable the time lock. Therefore, locktime needs at least one input to have a sequence number below the maximum. That is higher than the blocks that are already part of the blockchain. Setting a sequence number to zero is sufficient to enable locktime. It is also the only part of the script that uses sequenced numbers.

Take the Time

Bitcoin transactions are a composite data structure which includes inputs and outputs. The entire script follows a very basic, but effective, script that processes one operation at a time. All adding a locktime does is specify at what point in the future the transaction can be confirmed. Locktime appears at the end of the sequence. It is set using block height or Unix time.

The times are expressed as an unsigned 32-bit integer. If locktime is 0, it is overlooked and inconsequential. However, if the number is 500,000,000 or above, then it is treated as a Unix timestamp. There are no real limits to how long you can lock a transaction for. So be careful you don’t lock if for 200 years accidentally! Both the nLocktime and input sequence numbers were originally designed to create a simple update mechanism for the transaction.

Locktime is an optional function that can be added to the script. If it is not explicitly added, then the transaction will be confirmed by a node as soon as the network receives it. Until the programmed locktime the transaction remains unconfirmed and therefore is not valid until the time has elapsed.

trade predictions