2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								<pre>
							 
						 
					
						
							
								
									
										
										
										
											2015-08-13 08:14:38 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  BIP: 112
							 
						 
					
						
							
								
									
										
										
										
											2016-11-30 09:45:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Layer: Consensus (soft fork)
							 
						 
					
						
							
								
									
										
										
										
											2015-08-12 18:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Title: CHECKSEQUENCEVERIFY
							 
						 
					
						
							
								
									
										
										
										
											2016-02-03 07:02:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Author: BtcDrak <btcdrak@gmail.com>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          Mark Friedenbach <mark@friedenbach.org>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          Eric Lombrozo <elombrozo@gmail.com>
							 
						 
					
						
							
								
									
										
										
										
											2016-11-30 09:47:31 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Comments-Summary: No comments yet.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Comments-URI: https://github.com/bitcoin/bips/wiki/Comments:BIP-0112
							 
						 
					
						
							
								
									
										
										
										
											2016-07-18 23:37:42 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Status: Final
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  Type: Standards Track
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Created: 2015-08-10
							 
						 
					
						
							
								
									
										
										
										
											2016-11-30 09:47:31 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  License: PD
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								</pre>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								==Abstract==
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-12 18:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This BIP describes a new opcode (CHECKSEQUENCEVERIFY) for the Bitcoin
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								scripting system that in combination with BIP 68 allows execution
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								pathways of a script to be restricted based on the age of the output
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								being spent.
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								==Summary==
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								CHECKSEQUENCEVERIFY redefines the existing NOP3 opcode.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-24 13:08:54 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								When executed, if any of the following conditions are true, the script interpreter will terminate with an error:
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-01 01:47:16 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								* the stack is empty; or
							 
						 
					
						
							
								
									
										
										
										
											2015-11-24 13:08:54 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								* the top item on the stack is less than 0; or
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								* the top item on the stack has the disable flag (1 << 31) unset; and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								** the transaction version is less than 2; or
							 
						 
					
						
							
								
									
										
										
										
											2015-12-01 01:47:16 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								** the transaction input sequence number disable flag (1 << 31) is set; or
							 
						 
					
						
							
								
									
										
										
										
											2015-11-24 13:08:54 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								** the relative lock-time type is not the same; or
							 
						 
					
						
							
								
									
										
										
										
											2018-05-16 19:23:22 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								** the top stack item is greater than the transaction input sequence (when masked according to the BIP68);
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-24 13:08:54 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Otherwise, script execution will continue as if a NOP had been executed.
							 
						 
					
						
							
								
									
										
										
										
											2015-08-12 18:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-08-03 13:55:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								BIP 68 prevents a non-final transaction from being selected for inclusion in a block until the corresponding input has reached the specified age, as measured in block-height or block-time. By comparing the argument to CHECKSEQUENCEVERIFY against the nSequence field, we indirectly verify a desired minimum age of
							 
						 
					
						
							
								
									
										
										
										
											2015-11-23 20:21:32 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								the output being spent; until that relative age has been reached any script execution pathway including the CHECKSEQUENCEVERIFY will fail to validate, causing the transaction not to be selected for inclusion in a block.
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								==Motivation==
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-12 18:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								BIP 68 repurposes the transaction nSequence field meaning by giving
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								sequence numbers new consensus-enforced semantics as a relative
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								lock-time. However, there is no way to build Bitcoin scripts to make
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								decisions based on this field.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								By making the nSequence field accessible to script, it becomes
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								possible to construct code pathways that only become accessible some
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								minimum time after proof-of-publication. This enables a wide variety
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								of applications in phased protocols such as escrow, payment channels,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								or bidirectional pegs.
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-03 23:24:17 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 19:23:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								===Contracts With Expiration Deadlines===
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 03:51:17 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 19:23:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								====Escrow with Timeout====
							 
						 
					
						
							
								
									
										
										
										
											2015-10-03 23:24:17 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								An escrow that times out automatically 30 days after being funded can be
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								established in the following way. Alice, Bob and Escrow create a 2-of-3
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								address with the following redeemscript.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    IF
							 
						 
					
						
							
								
									
										
										
										
											2016-07-27 17:11:09 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        2 <Alice's pubkey> <Bob's pubkey> <Escrow's pubkey> 3 CHECKMULTISIG
							 
						 
					
						
							
								
									
										
										
										
											2015-10-03 23:24:17 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    ELSE
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        "30d" CHECKSEQUENCEVERIFY DROP
							 
						 
					
						
							
								
									
										
										
										
											2016-07-27 17:11:09 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        <Alice's pubkey> CHECKSIG
							 
						 
					
						
							
								
									
										
										
										
											2015-10-03 23:24:17 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    ENDIF
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-07-25 18:35:39 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								At any time funds can be spent using signatures from any two of Alice,
							 
						 
					
						
							
								
									
										
										
										
											2015-10-03 23:24:17 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Bob or the Escrow.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								After 30 days Alice can sign alone.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The clock does not start ticking until the payment to the escrow address
							 
						 
					
						
							
								
									
										
										
										
											2024-07-25 18:35:39 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								confirms.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-03 23:24:17 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 19:23:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								===Retroactive Invalidation===
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 03:51:17 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In many instances, we would like to create contracts that can be revoked in case
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								of some future event. However, given the immutable nature of the blockchain, it
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								is practically impossible to retroactively invalidate a previous commitment that
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								has already confirmed. The only mechanism we really have for retroactive
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								invalidation is blockchain reorganization which, for fundamental security
							 
						 
					
						
							
								
									
										
										
										
											2015-11-17 07:37:03 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								reasons, is designed to be very hard and very expensive to do.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 03:51:17 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-17 07:37:03 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Despite this limitation, we do have a way to provide something functionally similar to retroactive invalidation while preserving irreversibility of past commitments
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 03:51:17 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								using CHECKSEQUENCEVERIFY. By constructing scripts with multiple branches of
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								execution where one or more of the branches are delayed we provide
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								a time window in which someone can supply an invalidation condition that allows the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								output to be spent, effectively invalidating the would-be delayed branch and potentially discouraging
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								another party from broadcasting the transaction in the first place. If the invalidation
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								condition does not occur before the timeout, the delayed branch becomes spendable,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								honoring the original contract.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 15:21:03 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Some more specific applications of this idea:
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 03:51:17 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 19:23:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								====Hash Time-Locked Contracts====
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 03:51:17 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-23 20:21:32 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Hash Time-Locked Contracts (HTLCs) provide a general mechanism for off-chain contract negotiation. An execution pathway can be made to require knowledge of a secret (a hash preimage) that can be presented within an invalidation time window. By sharing the secret it is possible to guarantee to the counterparty that the transaction will never be broadcast since this would allow the counterparty to claim the output immediately while one would have to wait for the time window to pass. If the secret has not been shared, the counterparty will be unable to use the instant pathway and the delayed pathway must be used instead.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 07:55:20 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 19:23:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								====Bidirectional Payment Channels====
							 
						 
					
						
							
								
									
										
										
										
											2015-10-03 23:24:17 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Scriptable relative locktime provides a predictable amount of time to respond in
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the event a counterparty broadcasts a revoked transaction: Absolute locktime
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								necessitates closing the channel and reopen it when getting close to the timeout,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								whereas with relative locktime, the clock starts ticking the moment the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								transactions confirms in a block. It also provides a means to know exactly how
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								long to wait (in number of blocks) before funds can be pulled out of the channel
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								in the event of a noncooperative counterparty.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 19:23:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								====Lightning Network====
							 
						 
					
						
							
								
									
										
										
										
											2015-10-03 23:24:17 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 07:55:20 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The lightning network extends the bidirectional payment channel idea to allow for payments to be routed over multiple bidirectional payment channel hops.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-03 23:24:17 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 15:21:03 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								These channels are based on an anchor transaction that requires a 2-of-2
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								multisig from Alice and Bob, and a series of revocable commitment
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								transactions that spend the anchor transaction.  The commitment
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								transaction splits the funds from the anchor between Alice and Bob and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the latest commitment transaction may be published by either party at
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								any time, finalising the channel.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Ideally then, a revoked commitment transaction would never be able to
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								be successfully spent; and the latest commitment transaction would be
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								able to be spent very quickly.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								To allow a commitment transaction to be effectively revoked, Alice
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								and Bob have slightly different versions of the latest commitment
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								transaction. In Alice's version, any outputs in the commitment
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								transaction that pay Alice also include a forced delay, and an
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								alternative branch that allows Bob to spend the output if he knows that
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								transaction's revocation code. In Bob's version, payments to Bob are
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								similarly encumbered. When Alice and Bob negotiate new balances and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								new commitment transactions, they also reveal the old revocation code,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								thus committing to not relaying the old transaction.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								A simple output, paying to Alice might then look like:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    HASH160 <revokehash> EQUAL
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    IF
							 
						 
					
						
							
								
									
										
										
										
											2016-06-24 01:59:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        <Bob's pubkey>
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    ELSE
							 
						 
					
						
							
								
									
										
										
										
											2015-11-25 18:48:39 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        "24h" CHECKSEQUENCEVERIFY DROP
							 
						 
					
						
							
								
									
										
										
										
											2016-06-24 01:59:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        <Alice's pubkey>
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    ENDIF
							 
						 
					
						
							
								
									
										
										
										
											2015-11-25 18:48:39 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    CHECKSIG
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 15:21:03 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								This allows Alice to publish the latest commitment transaction at any
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								time and spend the funds after 24 hours, but also ensures that if Alice
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								relays a revoked transaction, that Bob has 24 hours to claim the funds.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								With CHECKLOCKTIMEVERIFY, this would look like:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    HASH160 <revokehash> EQUAL
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    IF
							 
						 
					
						
							
								
									
										
										
										
											2016-06-24 01:59:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        <Bob's pubkey>
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    ELSE
							 
						 
					
						
							
								
									
										
										
										
											2015-11-25 18:48:39 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        "2015/12/15" CHECKLOCKTIMEVERIFY DROP
							 
						 
					
						
							
								
									
										
										
										
											2016-06-24 01:59:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        <Alice's pubkey>
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    ENDIF
							 
						 
					
						
							
								
									
										
										
										
											2015-11-25 18:48:39 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    CHECKSIG
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 15:21:03 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								This form of transaction would mean that if the anchor is unspent on
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								2015/12/16, Alice can use this commitment even if it has been revoked,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								simply by spending it immediately, giving no time for Bob to claim it.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-23 20:21:32 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This means that the channel has a deadline that cannot be pushed
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 15:21:03 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								back without hitting the blockchain; and also that funds may not be
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								available until the deadline is hit. CHECKSEQUENCEVERIFY allows you
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								to avoid making such a tradeoff.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 15:21:03 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Hashed Time-Lock Contracts (HTLCs) make this slightly more complicated,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								since in principle they may pay either Alice or Bob, depending on whether
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Alice discovers a secret R, or a timeout is reached, but the same principle
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								applies -- the branch paying Alice in Alice's commitment transaction gets a
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								delay, and the entire output can be claimed by the other party if the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								revocation secret is known. With CHECKSEQUENCEVERIFY, a HTLC payable to
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Alice might look like the following in Alice's commitment transaction:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-25 18:48:39 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    HASH160 DUP <R-HASH> EQUAL
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    IF
							 
						 
					
						
							
								
									
										
										
										
											2015-11-25 18:48:39 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        "24h" CHECKSEQUENCEVERIFY
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        2DROP
							 
						 
					
						
							
								
									
										
										
										
											2016-06-24 01:59:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        <Alice's pubkey>
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    ELSE
							 
						 
					
						
							
								
									
										
										
										
											2015-11-25 18:48:39 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        <Commit-Revocation-Hash> EQUAL
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        NOTIF
							 
						 
					
						
							
								
									
										
										
										
											2016-04-30 22:19:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            "2015/10/20 10:33" CHECKLOCKTIMEVERIFY DROP
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        ENDIF
							 
						 
					
						
							
								
									
										
										
										
											2016-06-24 01:59:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        <Bob's pubkey>
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    ENDIF
							 
						 
					
						
							
								
									
										
										
										
											2015-11-25 18:48:39 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    CHECKSIG
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 15:21:03 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								and correspondingly in Bob's commitment transaction:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-25 18:48:39 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								   HASH160 DUP <R-HASH> EQUAL
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								   SWAP <Commit-Revocation-Hash> EQUAL ADD
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								   IF
							 
						 
					
						
							
								
									
										
										
										
											2016-06-24 01:59:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								       <Alice's pubkey>
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								   ELSE
							 
						 
					
						
							
								
									
										
										
										
											2015-11-25 18:48:39 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								       "2015/10/20 10:33" CHECKLOCKTIMEVERIFY
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								       "24h" CHECKSEQUENCEVERIFY
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								       2DROP
							 
						 
					
						
							
								
									
										
										
										
											2016-06-24 01:59:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								       <Bob's pubkey>
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								   ENDIF
							 
						 
					
						
							
								
									
										
										
										
											2015-11-25 18:48:39 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								   CHECKSIG
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 15:21:03 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Note that both CHECKSEQUENCEVERIFY and CHECKLOCKTIMEVERIFY are used in the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								final branch of above to ensure Bob cannot spend the output until after both
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the timeout is complete and Alice has had time to reveal the revocation
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								secret.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-04-28 17:19:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								See the [https://github.com/ElementsProject/lightning/blob/master/doc/miscellaneous/deployable-lightning.pdf Deployable Lightning] paper.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 10:42:51 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 19:23:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								====2-Way Pegged Sidechains====
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 10:42:51 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:37:54 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The 2-way pegged sidechain requires a new REORGPROOFVERIFY opcode, the semantics of which are outside the scope of this BIP. CHECKSEQUENCEVERIFY is used to make sure that sufficient time has passed since the return peg was posted to publish a reorg proof:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    IF
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        lockTxHeight <lockTxHash> nlocktxOut [<workAmount>] reorgBounty Hash160(<...>) <genesisHash> REORGPROOFVERIFY
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    ELSE
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        withdrawLockTime CHECKSEQUENCEVERIFY DROP HASH160 p2shWithdrawDest EQUAL
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    ENDIF
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 10:42:51 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								==Specification==
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-07-25 18:35:39 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Refer to the reference implementation, reproduced below, for the precise
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								semantics and detailed rationale for those semantics.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-02-13 12:14:03 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								<pre>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/* Below flags apply in the context of BIP 68 */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/* If this flag set, CTxIn::nSequence is NOT interpreted as a
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 * relative lock-time. */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1 << 31);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/* If CTxIn::nSequence encodes a relative lock-time and this flag
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 * is set, the relative lock-time has units of 512 seconds,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 * otherwise it specifies blocks with a granularity of 1. */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/* If CTxIn::nSequence encodes a relative lock-time, this mask is
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 * applied to extract that lock-time from the sequence field. */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
							 
						 
					
						
							
								
									
										
										
										
											2024-07-25 18:35:39 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-02-13 12:14:03 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								case OP_NOP3:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (!(flags & SCRIPT_VERIFY_CHECKSEQUENCEVERIFY)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // not enabled; treat as a NOP3
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return set_error(serror, SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS);
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        break;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-02-13 12:14:03 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    if (stack.size() < 1)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								       return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Note that elsewhere numeric opcodes are limited to
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // operands in the range -2**31+1 to 2**31-1, however it is
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // legal for opcodes to produce results exceeding that
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // range. This limitation is implemented by CScriptNum's
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // default 4-byte limit.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    //
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Thus as a special case we tell CScriptNum to accept up
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // to 5-byte bignums, which are good until 2**39-1, well
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // beyond the 2**32-1 limit of the nSequence field itself.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const CScriptNum nSequence(stacktop(-1), fRequireMinimal, 5);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // In the rare event that the argument may be < 0 due to
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // some arithmetic being done first, you can always use
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 0 MAX CHECKSEQUENCEVERIFY.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (nSequence < 0)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return set_error(serror, SCRIPT_ERR_NEGATIVE_LOCKTIME);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // To provide for future soft-fork extensibility, if the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // operand has the disabled lock-time flag set,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // CHECKSEQUENCEVERIFY behaves as a NOP.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if ((nSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG) != 0)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        break;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Compare the specified sequence number with the input.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (!checker.CheckSequence(nSequence))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return set_error(serror, SCRIPT_ERR_UNSATISFIED_LOCKTIME);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    break;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2024-07-25 18:35:39 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-02-13 12:14:03 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								bool TransactionSignatureChecker::CheckSequence(const CScriptNum& nSequence) const
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Relative lock times are supported by comparing the passed
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // in operand to the sequence number of the input.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const int64_t txToSequence = (int64_t)txTo->vin[nIn].nSequence;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Fail if the transaction's version number is not set high
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // enough to trigger BIP 68 rules.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (static_cast<uint32_t>(txTo->nVersion) < 2)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Sequence numbers with their most significant bit set are not
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // consensus constrained. Testing that the transaction's sequence
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // number do not have this bit set prevents using this property
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // to get around a CHECKSEQUENCEVERIFY check.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (txToSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Mask off any bits that do not have consensus-enforced meaning
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // before doing the integer comparisons
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const uint32_t nLockTimeMask = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | CTxIn::SEQUENCE_LOCKTIME_MASK;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const int64_t txToSequenceMasked = txToSequence & nLockTimeMask;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const CScriptNum nSequenceMasked = nSequence & nLockTimeMask;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // There are two kinds of nSequence: lock-by-blockheight
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // and lock-by-blocktime, distinguished by whether
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // nSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    //
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // We want to compare apples to apples, so fail the script
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // unless the type of nSequenceMasked being tested is the same as
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // the nSequenceMasked in the transaction.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (!(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        (txToSequenceMasked <  CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG && nSequenceMasked <  CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) ||
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        (txToSequenceMasked >= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG && nSequenceMasked >= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    ))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Now that we know we're comparing apples-to-apples, the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // comparison is a simple numeric one.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (nSequenceMasked > txToSequenceMasked)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								</pre>
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								==Reference Implementation==
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 16:39:01 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								A reference implementation is provided by the following pull request:
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-02-12 19:01:52 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								https://github.com/bitcoin/bitcoin/pull/7524
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								==Deployment==
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-03-17 20:14:31 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This BIP is to be deployed by "versionbits" BIP9 using bit 0.
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-03-17 20:14:31 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For Bitcoin '''mainnet''', the BIP9 '''starttime''' will be midnight 1st May 2016 UTC (Epoch timestamp 1462060800) and BIP9 '''timeout''' will be midnight 1st May 2017 UTC (Epoch timestamp 1493596800).
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-03-17 20:14:31 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For Bitcoin '''testnet''', the BIP9 '''starttime''' will be midnight 1st March 2016 UTC (Epoch timestamp 1456790400) and BIP9 '''timeout''' will be midnight 1st May 2017 UTC (Epoch timestamp 1493596800).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								This BIP must be deployed simultaneously with BIP68 and BIP113 using the same deployment mechanism.
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-12 18:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								==Credits==
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-12 18:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Mark Friedenbach invented the application of sequence numbers to
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								achieve relative lock-time, and wrote the reference implementation of
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								CHECKSEQUENCEVERIFY.
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-12 18:14:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The reference implementation and this BIP was based heavily on work
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								done by Peter Todd for the closely related BIP 65.
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-13 08:14:38 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								BtcDrak authored this BIP document.
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-23 20:21:32 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Thanks to Eric Lombrozo and Anthony Towns for contributing example use cases.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 10:42:51 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								==References==
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-03-17 20:14:31 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[https://github.com/bitcoin/bips/blob/master/bip-0009.mediawiki BIP 9] Versionbits
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-24 13:08:54 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki BIP 68] Relative lock-time through consensus-enforced sequence numbers
							 
						 
					
						
							
								
									
										
										
										
											2015-10-03 23:14:20 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki BIP 65] OP_CHECKLOCKTIMEVERIFY
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-03 23:14:20 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[https://github.com/bitcoin/bips/blob/master/bip-0113.mediawiki BIP 113] Median past block time for time-lock constraints
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-03 23:14:20 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[http://lists.linuxfoundation.org/pipermail/lightning-dev/2015-July/000021.html HTLCs using OP_CHECKSEQUENCEVERIFY/OP_LOCKTIMEVERIFY and revocation hashes]
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-03 23:14:20 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[http://lightning.network/lightning-network-paper.pdf Lightning Network]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-04-28 17:19:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[https://github.com/ElementsProject/lightning/blob/master/doc/miscellaneous/deployable-lightning.pdf Deployable Lightning]
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 15:21:03 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-03 23:14:20 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[http://diyhpl.us/diyhpluswiki/transcripts/sf-bitcoin-meetup/2015-02-23-scaling-bitcoin-to-billions-of-transactions-per-day/ Scaling Bitcoin to Billions of Transactions Per Day]
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-13 08:14:38 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[http://lists.linuxfoundation.org/pipermail/bitcoin-dev/2015-August/010396.html Softfork deployment considerations]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-01-24 13:24:09 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[https://web.archive.org/web/20210925124425/https://gist.github.com/sipa/bf69659f43e763540550 Version bits]
							 
						 
					
						
							
								
									
										
										
										
											2015-08-13 08:14:38 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-03 23:14:20 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2013-April/002433.html Jeremy Spilman Micropayment Channels]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 00:49:09 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								==Copyright==
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								This document is placed in the public domain.
							 
						 
					
						
							
								
									
										
										
										
											2015-09-26 09:54:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-23 20:21:32 +00:00