NSequence

From BitcoinWiki
This is the approved revision of this page, as well as being the most recent.
Jump to: navigation, search
nSequence – sequence number

nSequence is the transaction-level relative time lock, which was in Bitcoin always and was designed by Satoshi Nakamoto. Changing sequence number of operation was never used for years – it’s doesn’t make sense in a long run. That’s why it was disabled. But when transactions become slow and mining fees become extremely high, the situation was changed.

What is Sequence number (nSequence)?[edit]

When you want to make trasaction in Bitcoin it could be that there are a lot of people who wants to do the same. Usually you are not checking a mempool – you just click «send BTC». And if mempool is busy you have to wait. Miners can confirm it later, which is not suitable for you. Or they can reject your payment, excluding it from mempool. But there was third option – do the same but with higher fee. Such transactions was named as replace-by-fee (RBF). And that’s where nSequence is used. You can change it to define sequence number of your operation and make it older.

BIP-68[edit]

nSequence and RBF was first described in BIP-0125 and implemented at BIP-68. BIP means Bitcoin Improvement Proposals. Also changes were included in Bitcoin Core 0.12+ software. And this filed become active again. Now, usualy most of programs fulfill nSequence as maximum – (0xffffffff). So you have to change it by your own. At image below you can see how it looks like in Electrum Wallet – it is a check point «Enable Replace-By-Fee». 0*vTdHldiBeavNmY3U.png

nSequence examples[edit]

There are the example of sequence numbers in Bitcoin.

# Most of the transaction is omitted. Using decimal for human readability.
# Using hex for sequence numbers due to the presence of flags.

# This transaction is locked for 4096 second. Just over 1 hour.
tx_1:  
 input_1:
   sequence_no: 0x00400008
   # Disable flag is not set, type flag is set. Input locked for 8 * 512 seconds.

# This transaction is not nSequence locked, but may be nLocktime locked, and allows RBF.
tx_2:  
 input_1:
   sequence_no: 0xFEDC3210
   # Disable flag is set. nSequence locking disabled.

# This transaction is invalid until 16 blocks have elapsed since input_1's prevout confirms.
tx_3:  
 input_1:
   sequence_no: 0x00000010  
   # Disable flag is not set, type flag not set. This input locked for 16 blocks.
 input_2:
   sequence_no: 0xFFFFFFFF  
   # Disable flag is set.

# This transaction is not time locked, but has opted to allow Replace-By-Fee.
tx_4:  
 lock_time: 0
 input_1:
   sequence_no: 0xFFFFFFFE  
   # nSequence is disabled, nLocktime is enabled, RBF is not signaled.
 input_2:
   sequence_no: 0xFFFFFFFD  
   # nSequence is disabled, nLocktime is enabled, RBF is signaled.

# This transaction is not valid until block 506221
# It is also not valid until 87040 seconds have passed since the confirmation of input_1's previous output
tx_5:  
 lock_time: 506221
 input_1:
   sequence_no: 0x004000AA

Sources[edit]

https://books.google.ru/books?id=MpwnDwAAQBAJ&pg=PA162&lpg=PA162&dq=nsequence+bitcoin&source=bl&ots=wQ5rzfz1y7&sig=tQovPkQk2kdSNbBdnycMcQ2-YOg&hl=ru&sa=X&ved=0ahUKEwi3l7_tgNHbAhXHulMKHYv1D-gQ6AEIbTAG#v=onepage&q=nsequence%20bitcoin&f=false
https://prestwi.ch/bitcoin-time-locks/
https://medium.com/@overtorment/bitcoin-replace-by-fee-guide-e10032f9a93f
https://en.bitcoin.it/wiki/Payment_channels
https://bitcoin.org/en/glossary/sequence-number

See Also on BitcoinWiki[edit]