summaryrefslogtreecommitdiff
path: root/bip-0068.mediawiki
diff options
context:
space:
mode:
authorMark Friedenbach <mark@friedenbach.org>2015-06-19 17:18:48 -0700
committerMark Friedenbach <mark@friedenbach.org>2015-06-21 20:12:58 -0700
commitd32087c2bf9a7adbfdf97f5f98a35417cdf48594 (patch)
treea6a55a0b20ae7a38ed40d314e739eb961c1f0c06 /bip-0068.mediawiki
parent8defdc87d18a8f36d426ed40145441d271f18ae4 (diff)
Add BIP 68: Consensus-enforced transaction replacement signalled via sequence numbers
Diffstat (limited to 'bip-0068.mediawiki')
-rw-r--r--bip-0068.mediawiki209
1 files changed, 209 insertions, 0 deletions
diff --git a/bip-0068.mediawiki b/bip-0068.mediawiki
new file mode 100644
index 0000000..8da92b8
--- /dev/null
+++ b/bip-0068.mediawiki
@@ -0,0 +1,209 @@
+<pre>
+ BIP: 68
+ Title: Consensus-enforced transaction replacement signalled via sequence numbers
+ Author: Mark Friedenbach <mark@friedenbach.org>
+ Status: Draft
+ Type: Standards Track
+ Created: 2015-05-28
+</pre>
+
+==Abstract==
+
+This BIP describes a modification to the consensus-enforced semantics of the sequence number field to enable a signed transaction input to remain invalid for a defined period of time after confirmation of its corresponding output, for the purpose of supporting consensus-enforced transaction replacement features.
+
+==Motivation==
+
+Bitcoin has sequence number fields for each input of a transaction. The original idea appears to have been that the highest sequence number should dominate and miners should prefer it over lower sequence numbers. This was never really implemented, and the half-implemented code seemed to be making this assumption that miners would honestly prefer the higher sequence numbers, even if the lower ones were much much more profitable. That turns out to be a dangerous assumption, and so most technical people have assumed that kind of sequence number mediated replacement was useless because there was no way to enforce "honest" behaviour, as even a few rational (profit maximizing) miners would break that completely. The change described by this BIP provides the missing piece that makes sequence numbers do something significant with respect to enforcing transaction replacement without assuming anything other than profit-maximizing behaviour on the part of miners.
+
+==Specification==
+
+The maximum sequence number can be included in any block, like normal. For transactions with an nVersion of 2 or greater, a sequence number of one less than the maximum can only be included in the next block after the input it is spending, rather than it being possible to be included in the same block. A sequence number one less than that can only be included two blocks later, and so on. Alternatively, a sequence number LOCKTIME_THRESHOLD (500,000,000) less than the maximum (0xffffffff - 500,000,000 = 0xe2329aff) can only be included in a block with an nTime timestamp at least one second greater than the median time stamp of the 11 blocks prior to the block containing the coin being spent. A sequence number one less than that can only be included two seconds later, and so on. This behaviour is only enforced if the most significant bit of the sequence number field is set.
+
+This is proposed to be accomplished by replacing IsFinalTx() and CheckFinalTx(), existing consensus code functions that return true if a transaction's lock-time constraints are satisfied and false otherwise, with LockTime() and CheckLockTime(), new functions that return a non-zero value if a transaction's lock-time or sequence number constraints are not satisfied and zero otherwise:
+
+<pre>
+ enum {
+ /* Interpret sequence numbers as relative lock-time constraints. */
+ LOCKTIME_VERIFY_SEQUENCE = (1 << 0),
+ };
+
+ const uint32_t SEQUENCE_THRESHOLD = (1 << 31);
+
+ int64_t LockTime(const CTransaction &tx,
+ int flags, const CCoinsView* pCoinsView,
+ int nBlockHeight, int64_t nBlockTime)
+ {
+ CCoins coins;
+ uint32_t nLockTime;
+
+ bool fEnforceBIP68 = tx.nVersion >= 2
+ && flags & LOCKTIME_VERIFY_SEQUENCE;
+
+ // Will be set to the equivalent height- and time-based nLockTime
+ // values that would be necessary to satisfy all relative lock-
+ // time constraints given our view of block chain history.
+ int nMinHeight = 0;
+ int64_t nMinTime = 0;
+ // Will remain equal to true if all inputs are finalized (MAX_INT).
+ bool fFinalized = true;
+
+ BOOST_FOREACH(const CTxIn& txin, tx.vin) {
+ // The relative lock-time is the inverted sequence number so
+ // as to preserve the semantics MAX_INT means an input is
+ // finalized (0 relative lock-time).
+ nLockTime = ~txin.nSequence;
+
+ // ~MAX_INT is zero/false, meaning the input is final.
+ if (!nLockTime)
+ continue;
+ else
+ fFinalized = false;
+
+ // Sequence numbers equal to or above the SEQUENCE_THRESHOLD
+ // are not treated as relative lock-times, nor are they given
+ // any consensus-enforced meaning at this point.
+ if (nLockTime >= SEQUENCE_THRESHOLD)
+ continue;
+
+ // Do not enforce sequence numbers as a relative lock time
+ // unless we have been instructed to.
+ if (!fEnforceBIP68)
+ continue;
+
+ // Skip this input if it is not in the UTXO set. This should
+ // only ever happen in non-consensus code.
+ if (!pCoinsView->GetCoins(txin.prevout.hash, coins))
+ continue;
+
+ if (nLockTime < LOCKTIME_THRESHOLD)
+ // We subtract 1 from relative lock-times because of lock
+ // time of 0 has the semantics of "same block," so a lock-
+ // time of 1 should mean "next block," but nLockTime has
+ // the semantics of "last invalid block height."
+ nMinHeight = std::max(nMinHeight, coins.nHeight + (int)nLockTime - 1);
+ else
+ // Time-based relative lock-times are measured from the
+ // smallest allowed timestamp of the block containing the
+ // txout being spent, which is the median time past of the
+ // block prior.
+ nMinTime = std::max(nMinTime,
+ pindexBestHeader->GetAncestor(coins.nHeight-1)->GetMedianTimePast()
+ - LOCKTIME_THRESHOLD + (int64_t)nLockTime);
+ }
+
+ // If sequence numbers are MAX_INT / zero relative lock-time, the
+ // transaction is considered final and nLockTime constraints are
+ // not enforced.
+ if (fFinalized)
+ return 0;
+
+ if ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD)
+ nMinHeight = std::max(nMinHeight, (int)tx.nLockTime);
+ else
+ nMinTime = std::max(nMinTime, (int64_t)tx.nLockTime);
+
+ if (nMinHeight >= nBlockHeight)
+ return nMinHeight;
+ if (nMinTime >= nBlockTime)
+ return nMinTime;
+
+ return 0;
+ }
+
+ int64_t CheckLockTime(const CTransaction &tx, int flags)
+ {
+ AssertLockHeld(cs_main);
+
+ // By convention a negative value for flags indicates that the
+ // current network-enforced consensus rules should be used.
+ flags = std::max(flags, 0);
+
+ // pcoinsTip contains the UTXO set for chainActive.Tip()
+ const CCoinsView *pCoinsView = pcoinsTip;
+
+ // CheckLockTime() uses chainActive.Height()+1 to evaluate
+ // nLockTime because when LockTime() is called within
+ // CBlock::AcceptBlock(), the height of the block *being*
+ // evaluated is what is used. Thus if we want to know if a
+ // transaction can be part of the *next* block, we need to call
+ // LockTime() with one more than chainActive.Height().
+ const int nBlockHeight = chainActive.Height() + 1;
+
+ // Timestamps on the other hand don't get any special treatment,
+ // because we can't know what timestamp the next block will have,
+ // and there aren't timestamp applications where it matters.
+ const int64_t nBlockTime = GetAdjustedTime();
+
+ return LockTime(tx, flags, pCoinsView, nBlockHeight, nBlockTime);
+ }
+</pre>
+
+Code conditional on the return value of IsFinalTx() / CheckLockTime() has to be updated as well, since the semantics of the return value has been inverted.
+
+==Rationale==
+
+Counting down from the maximum makes sense, since nothing can be higher than the maximum and no transaction can be in a block before its parent transactions. This means that a higher sequence number can always be included earlier than a lower one (even if the time the original coins being spent was unknown when the transaction was authored). Because of this, even rational miners should go along with the scheme: Take the higher sequence number and collect the fees, or skip over it in the hopes that no one else takes a higher number before the next available lower number becomes spendable. And, of course, users are not constrained to make their sequence numbers go down only one at a time. So it's "take the most updated version, now, or gamble that no one in the next dozen blocks takes the most updated and that you manage to include the next to most when it finally becomes mineable." This is similar to how lock-time works. In fact, this scheme is precisely a per-input relative lock-time.
+
+==Example: Bidirectional payment channel==
+
+A bidirectional payment channel can be established by two parties funding a single output in the following way: Alice funds a 1btc output which is the 2-of-2 multisig of Alice AND Bob, or Alice's key only after a sufficiently long timeout, e.g. 30 days or 4320 blocks. The channel-generating transaction is signed by Alice and broadcast to the network.
+
+Alice desires to send Bob a payment of 0.1btc. She does so by constructing a transaction spending the 1btc output and sending 0.1btc to Bob and 0.9btc back to herself. She provides her signature for the 2-of-2 multisig constraint, and sets a relative lock-time using the sequence number field such that the transaction will become valid 24-hours or 144 blocks before the refund timeout. Two more times Alice sends Bob a payment of 0.1btc, each time generating and signing her half of a transaction spending the 1btc output and sending 0.2btc, then 0.3btc to Bob with a relative lock-time of 29 days from creation of the channel.
+
+Bob now desires to send Alice a refund of 0.25btc. He does so by constructing a transaction spending the 1btc output and sending 0.95btc (= 0.7btc + 0.25btc) to Alice and 0.05btc to himself. Since Bob will still have in his logs the transaction giving him 0.7btc 29 days after the creation of the channel, Alice demands that this new transaction have a relative lock-time of 28 days so she has a full day to broadcast it before the next transaction matures.
+
+Alice and Bob continue to make payments to each other, decrementing the relative lock-time by one day each time the channel switches direction, until the present time is reached or either party desires to close out the channel. A close-out is performed by setting the relative lock-time to zero (nSequence = MAX_INT) and both parties signing.
+
+==Implementation==
+
+A reference implementation is provided in the following git repository:
+
+https://github.com/maaku/bitcoin/tree/sequencenumbers
+
+==Acknowledgements==
+
+Credit goes to Gregory Maxwell for providing a succinct and clear description of the behaviour of this change, which became the basis of this BIP text.
+
+==Deployment==
+
+We reuse the double-threshold switchover mechanism from BIPs 34 and 66, with the same thresholds, but for nVersion = 4. The new rules are in effect for every block (at height H) with nVersion = 4 and at least 750 out of 1000 blocks preceding it (with heights H-1000..H-1) also have nVersion = 4. Furthermore, when 950 out of the 1000 blocks preceding a block do have nVersion = 4, nVersion = 3 blocks become invalid, and all further blocks enforce the new rules.
+
+It is recommended that this soft-fork deployment trigger include other related proposals for improving Bitcoin's lock-time capabilities, such as [https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki BIP 65]: OP_CHECKLOCKTIMEVERIFY.
+
+==Compatibility==
+
+The only use of sequence numbers by the Bitcoin Core reference client software is to disable checking the nLockTime constraints in a transaction. The semantics of that application are preserved by this BIP without requiring any special-case handling.
+
+There may be other uses for the sequence number field that are not commonplace or yet to be discoverd. To allow for other uses of the sequence number field, it is only interpreted as a relative lock-time as described in this BIP if the most significant bit is set. This allows up to 31 bits of the sequence number field to be used for other purposes in applicaitons which don't simultaneously need a per-input relative lock-time.
+
+The most efficient way to calculate sequence number from relative lock-time is with bit-inversion:
+
+<pre>
+ // 0 <= nHeight < 500,000,000 blocks
+ nSequence = ~(nHeight);
+ nHeight = ~(nSequence);
+
+ // 1 <= nTime < 1,647,483,648 seconds (52.2 years)
+ nSequence = ~(nTime - 1 + LOCKTIME_THRESHOLD);
+ nTime = ~(nSequence) - LOCKTIME_THRESHOLD + 1;
+</pre>
+
+In languages which do not support bit inversion, the calculation can be accomplished with integer arithmetic instead:
+
+<pre>
+ nSequence = 0xffffffff - nHeight;
+ nHeight = 0xffffffff - nSequence;
+
+ nSequence = 0xffffffff - nTime + 1 - LOCKTIME_THRESHOLD;
+ nTime = 0xffffffff - nSequence + 1 - LOCKTIME_THRESHOLD;
+</pre>
+
+==References==
+
+Bitcoin mailing list discussion: https://www.mail-archive.com/bitcoin-development@lists.sourceforge.net/msg07864.html
+
+BIP 34: Block v2, Height in Coinbase, https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki
+
+BIP 65: OP_CHECKLOCKTIMEVERIFY, https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki
+
+BIP 66: Strict DER signatures, https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki