Skip to content

Commit 171c392

Browse files
committed
Merge remote-tracking branch 'refs/remotes/bitcoin/master'
2 parents 345f1c4 + f01ec3f commit 171c392

12 files changed

+690
-346
lines changed

README.mediawiki

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ Those proposing changes should consider that ultimately consent may rest with th
252252
| OP_CHECKLOCKTIMEVERIFY
253253
| Peter Todd
254254
| Standard
255-
| Draft
255+
| Accepted
256256
|-
257257
| [[bip-0066.mediawiki|66]]
258258
| Strict DER signatures
@@ -302,6 +302,12 @@ Those proposing changes should consider that ultimately consent may rest with th
302302
| Standard
303303
| Draft
304304
|-
305+
| [[bip-0083.mediawiki|83]]
306+
| Dynamic Hierarchical Deterministic Key Trees
307+
| Eric Lombrozo
308+
| Standard
309+
| Draft
310+
|-
305311
| [[bip-0099.mediawiki|99]]
306312
| Motivation and deployment of consensus rule changes ([soft/hard]forks)
307313
| Jorge Timón
@@ -386,6 +392,12 @@ Those proposing changes should consider that ultimately consent may rest with th
386392
| Informational
387393
| Draft
388394
|-
395+
| [[bip-0124.mediawiki|124]]
396+
| Hierarchical Deterministic Script Templates
397+
| Eric Lombrozo, William Swanson
398+
| Informational
399+
| Draft
400+
|-
389401
| [[bip-0125.mediawiki|125]]
390402
| Opt-in Full Replace-by-Fee Signaling
391403
| David Harding, Peter Todd
@@ -398,6 +410,12 @@ Those proposing changes should consider that ultimately consent may rest with th
398410
| Standard
399411
| Draft
400412
|-
413+
| [[bip-0131.mediawiki|131]]
414+
| "Coalescing Transaction" Specification (wildcard inputs)
415+
| Chris Priest
416+
| Standard
417+
| Draft
418+
|-
401419
| [[bip-0140.mediawiki|140]]
402420
| Normalized TXID
403421
| Christian Decker
@@ -411,13 +429,13 @@ Those proposing changes should consider that ultimately consent may rest with th
411429
| Draft
412430
|-
413431
| [[bip-0142.mediawiki|142]]
414-
| Address Formats for Witness Program
432+
| Address Format for Segregated Witness
415433
| Johnson Lau
416434
| Standard
417435
| Draft
418436
|-
419437
| [[bip-0143.mediawiki|143]]
420-
| Transaction signature verification for version 0 and version 1 witness program
438+
| Transaction Signature Verification for Version 0 Witness Program
421439
| Johnson Lau, Pieter Wuille
422440
| Standard
423441
| Draft

bip-0032.mediawiki

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ PyCoin (https://github.com/richardkiss/pycoin) is a suite of utilities for deali
259259

260260
A Java implementation is available at https://github.com/bitsofproof/supernode/blob/1.1/api/src/main/java/com/bitsofproof/supernode/api/ExtendedKey.java
261261

262-
A C++ implementation is available at https://github.com/CodeShark/CoinClasses/tree/master/tests/hdwallets
262+
A C++ implementation is available at https://github.com/ciphrex/mSIGNA/blob/master/deps/CoinCore/src/hdkeys.h
263263

264264
An Objective-C implementation is available at https://github.com/oleganza/CoreBitcoin/blob/master/CoreBitcoin/BTCKeychain.h
265265

@@ -281,4 +281,5 @@ A Haskell implementation is available at https://github.com/haskoin/haskoin toge
281281

282282
* Gregory Maxwell for the original idea of type-2 deterministic wallets, and many discussions about it.
283283
* Alan Reiner for the implementation of this scheme in Armory, and the suggestions that followed from that.
284+
* Eric Lombrozo for reviewing and revising this BIP.
284285
* Mike Caldwell for the version bytes to obtain human-recognizable Base58 strings.

bip-0068.mediawiki

Lines changed: 143 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -49,139 +49,160 @@ The block produced time is equal to the median-time-past of its previous block.
4949

5050
When the relative lock-time is block-based, it is interpreted as a minimum block-height constraint over the input's age. A relative block-based lock-time of zero indicates an input which can be included in any block. More generally, a relative block lock-time n can be included n blocks after the mining date of the output it is spending, or any block thereafter.
5151

52-
This is proposed to be accomplished by replacing IsFinalTx() and CheckFinalTx(), existing consensus and non-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:
52+
==Implementation==
53+
54+
A reference implementation is provided by the following pull request
55+
56+
https://github.com/bitcoin/bitcoin/pull/7184
5357

5458
<pre>
55-
enum {
56-
/* Interpret sequence numbers as relative lock-time constraints. */
57-
LOCKTIME_VERIFY_SEQUENCE = (1 << 0),
58-
};
59-
60-
/* Setting nSequence to this value for every input in a transaction
61-
* disables nLockTime. */
62-
static const uint32_t SEQUENCE_FINAL = 0xffffffff;
63-
64-
/* If this flag set, CTxIn::nSequence is NOT interpreted as a
65-
* relative lock-time. Setting the most significant bit of a
66-
* sequence number disabled relative lock-time. */
67-
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1 << 31);
59+
enum {
60+
/* Interpret sequence numbers as relative lock-time constraints. */
61+
LOCKTIME_VERIFY_SEQUENCE = (1 << 0),
62+
};
6863

69-
/* If CTxIn::nSequence encodes a relative lock-time and this flag
70-
* is set, the relative lock-time has units of 512 seconds,
71-
* otherwise it specifies blocks with a granularity of 1. */
72-
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
64+
/* Setting nSequence to this value for every input in a transaction
65+
* disables nLockTime. */
66+
static const uint32_t SEQUENCE_FINAL = 0xffffffff;
7367

74-
/* If CTxIn::nSequence encodes a relative lock-time, this mask is
75-
* applied to extract that lock-time from the sequence field. */
76-
static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
68+
/* If this flag set, CTxIn::nSequence is NOT interpreted as a
69+
* relative lock-time. Setting the most significant bit of a
70+
* sequence number disabled relative lock-time. */
71+
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1 << 31);
7772

78-
/* In order to use the same number of bits to encode roughly the
79-
* same wall-clock duration, and because blocks are naturally
80-
* limited to occur every 600s on average, the minimum granularity
81-
* for time-based relative lock-time is fixed at 512 seconds.
82-
* Converting from CTxIn::nSequence to seconds is performed by
83-
* multiplying by 512 = 2^9, or equivalently shifting up by
84-
* 9 bits. */
85-
static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
73+
/* If CTxIn::nSequence encodes a relative lock-time and this flag
74+
* is set, the relative lock-time has units of 512 seconds,
75+
* otherwise it specifies blocks with a granularity of 1. */
76+
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
8677

87-
int64_t LockTime(const CTransaction &tx, int flags, const std::vector<int>* prevHeights, const CBlockIndex& block)
88-
{
89-
assert(prevHeights == NULL || prevHeights->size() == tx.vin.size());
90-
int64_t nBlockTime = (flags & LOCKTIME_MEDIAN_TIME_PAST)
91-
? block.GetAncestor(std::max(block.nHeight-1, 0))->GetMedianTimePast()
92-
: block.GetBlockTime();
93-
94-
bool fEnforceBIP68 = static_cast<uint32_t>(tx.nVersion) >= 2
95-
&& flags & LOCKTIME_VERIFY_SEQUENCE;
96-
97-
// Will be set to the equivalent height- and time-based nLockTime
98-
// values that would be necessary to satisfy all relative lock-
99-
// time constraints given our view of block chain history.
100-
int nMinHeight = 0;
101-
int64_t nMinTime = 0;
102-
// Will remain equal to true if all inputs are finalized
103-
// (CTxIn::SEQUENCE_FINAL).
104-
bool fFinalized = true;
105-
for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
106-
const CTxIn& txin = tx.vin[txinIndex];
107-
// Set a flag if we witness an input that isn't finalized.
108-
if (txin.nSequence == CTxIn::SEQUENCE_FINAL)
109-
continue;
110-
else
111-
fFinalized = false;
78+
/* If CTxIn::nSequence encodes a relative lock-time, this mask is
79+
* applied to extract that lock-time from the sequence field. */
80+
static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
11281

113-
// Do not enforce sequence numbers as a relative lock time
114-
// unless we have been instructed to, and a view has been
115-
// provided.
116-
if (!fEnforceBIP68)
117-
continue;
82+
/* In order to use the same number of bits to encode roughly the
83+
* same wall-clock duration, and because blocks are naturally
84+
* limited to occur every 600s on average, the minimum granularity
85+
* for time-based relative lock-time is fixed at 512 seconds.
86+
* Converting from CTxIn::nSequence to seconds is performed by
87+
* multiplying by 512 = 2^9, or equivalently shifting up by
88+
* 9 bits. */
89+
static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
11890

119-
// Sequence numbers with the most significant bit set are not
120-
// treated as relative lock-times, nor are they given any
121-
// consensus-enforced meaning at this point.
122-
if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG)
123-
continue;
124-
125-
if (prevHeights == NULL)
126-
continue;
127-
128-
int nCoinHeight = (*prevHeights)[txinIndex];
129-
130-
if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) {
131-
132-
int64_t nCoinTime = block.GetAncestor(std::max(nCoinHeight-1, 0))->GetMedianTimePast();
133-
134-
// Time-based relative lock-times are measured from the
135-
// smallest allowed timestamp of the block containing the
136-
// txout being spent, which is the median time past of the
137-
// block prior.
138-
nMinTime = std::max(nMinTime, nCoinTime + (int64_t)((txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) << CTxIn::SEQUENCE_LOCKTIME_GRANULARITY) - 1);
139-
} else {
140-
// We subtract 1 from relative lock-times because a lock-
141-
// time of 0 has the semantics of "same block," so a lock-
142-
// time of 1 should mean "next block," but nLockTime has
143-
// the semantics of "last invalid block height."
144-
nMinHeight = std::max(nMinHeight, nCoinHeight + (int)(txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) - 1);
145-
}
91+
/**
92+
* Calculates the block height and time which the transaction must be later than
93+
* in order to be considered final in the context of BIP 68. It also removes
94+
* from the vector of input heights any entries which did not correspond to sequence
95+
* locked inputs as they do not affect the calculation.
96+
*/
97+
static std::pair<int, int64_t> CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector<int>* prevHeights, const CBlockIndex& block)
98+
{
99+
assert(prevHeights->size() == tx.vin.size());
100+
101+
// Will be set to the equivalent height- and time-based nLockTime
102+
// values that would be necessary to satisfy all relative lock-
103+
// time constraints given our view of block chain history.
104+
// The semantics of nLockTime are the last invalid height/time, so
105+
// use -1 to have the effect of any height or time being valid.
106+
int nMinHeight = -1;
107+
int64_t nMinTime = -1;
108+
109+
// tx.nVersion is signed integer so requires cast to unsigned otherwise
110+
// we would be doing a signed comparison and half the range of nVersion
111+
// wouldn't support BIP 68.
112+
bool fEnforceBIP68 = static_cast<uint32_t>(tx.nVersion) >= 2
113+
&& flags & LOCKTIME_VERIFY_SEQUENCE;
114+
115+
// Do not enforce sequence numbers as a relative lock time
116+
// unless we have been instructed to
117+
if (!fEnforceBIP68) {
118+
return std::make_pair(nMinHeight, nMinTime);
119+
}
120+
121+
for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
122+
const CTxIn& txin = tx.vin[txinIndex];
123+
124+
// Sequence numbers with the most significant bit set are not
125+
// treated as relative lock-times, nor are they given any
126+
// consensus-enforced meaning at this point.
127+
if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG) {
128+
// The height of this input is not relevant for sequence locks
129+
(*prevHeights)[txinIndex] = 0;
130+
continue;
146131
}
147-
148-
// If all sequence numbers are CTxIn::SEQUENCE_FINAL, the
149-
// transaction is considered final and nLockTime constraints
150-
// are not enforced.
151-
if (fFinalized)
152-
return 0;
153-
154-
if ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD)
155-
nMinHeight = std::max(nMinHeight, (int)tx.nLockTime);
156-
else
157-
nMinTime = std::max(nMinTime, (int64_t)tx.nLockTime);
158-
159-
if (nMinHeight >= block.nHeight)
160-
return nMinHeight;
161-
162-
if (nMinTime >= nBlockTime)
163-
return nMinTime;
164-
165-
return 0;
166-
}</pre>
167-
168-
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.
169-
170-
==Example: Bidirectional payment channel==
171132
172-
A bidirectional payment channel can be established by two parties funding a single output in the following way: Alice funds a 1 BTC 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.
173-
174-
Alice desires to send Bob a payment of 0.1 BTC. She does so by constructing a transaction spending the 1 BTC output and sending 0.1 BTC to Bob and 0.9 BTC 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.1 BTC, each time generating and signing her half of a transaction spending the 1btc output and sending 0.2 BTC, then 0.3 BTC to Bob with a relative lock-time of 29 days from creation of the channel.
175-
176-
Bob now desires to send Alice a refund of 0.25 BTC. He does so by constructing a transaction spending the 1btc output and sending 0.95 BTC (= 0.7 BTC + 0.25 BTC) to Alice and 0.05 BTC to himself. Since Bob will still have in his logs the transaction giving him 0.7 BTC 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.
177-
178-
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 finalizing the input (nSequence = MAX_INT) and both parties signing.
179-
180-
==Implementation==
181-
182-
A reference implementation is provided by the following pull request
133+
int nCoinHeight = (*prevHeights)[txinIndex];
134+
135+
if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) {
136+
int64_t nCoinTime = block.GetAncestor(std::max(nCoinHeight-1, 0))->GetMedianTimePast();
137+
138+
// Time-based relative lock-times are measured from the
139+
// smallest allowed timestamp of the block containing the
140+
// txout being spent, which is the median time past of the
141+
// block prior.
142+
nMinTime = std::max(nMinTime, nCoinTime + (int64_t)((txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) << CTxIn::SEQUENCE_LOCKTIME_GRANULARITY) - 1);
143+
} else {
144+
// We subtract 1 from relative lock-times because a lock-
145+
// time of 0 has the semantics of "same block," so a lock-
146+
// time of 1 should mean "next block," but nLockTime has
147+
// the semantics of "last invalid block height."
148+
nMinHeight = std::max(nMinHeight, nCoinHeight + (int)(txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) - 1);
149+
}
150+
}
151+
152+
return std::make_pair(nMinHeight, nMinTime);
153+
}
154+
155+
static bool EvaluateSequenceLocks(const CBlockIndex& block, std::pair<int, int64_t> lockPair)
156+
{
157+
int64_t nBlockTime = block.pprev ? block.pprev->GetMedianTimePast() : 0;
158+
if (lockPair.first >= block.nHeight || lockPair.second >= nBlockTime)
159+
return false;
160+
161+
return true;
162+
}
163+
164+
bool SequenceLocks(const CTransaction &tx, int flags, std::vector<int>* prevHeights, const CBlockIndex& block)
165+
{
166+
return EvaluateSequenceLocks(block, CalculateSequenceLocks(tx, flags, prevHeights, block));
167+
}
168+
169+
bool CheckSequenceLocks(const CTransaction &tx, int flags)
170+
{
171+
AssertLockHeld(cs_main);
172+
173+
CBlockIndex* tip = chainActive.Tip();
174+
CBlockIndex index;
175+
index.pprev = tip;
176+
// CheckSequenceLocks() uses chainActive.Height()+1 to evaluate
177+
// height based locks because when SequenceLocks() is called within
178+
// CBlock::AcceptBlock(), the height of the block *being*
179+
// evaluated is what is used. Thus if we want to know if a
180+
// transaction can be part of the *next* block, we need to call
181+
// SequenceLocks() with one more than chainActive.Height().
182+
index.nHeight = tip->nHeight + 1;
183+
184+
// pcoinsTip contains the UTXO set for chainActive.Tip()
185+
CCoinsViewMemPool viewMemPool(pcoinsTip, mempool);
186+
std::vector<int> prevheights;
187+
prevheights.resize(tx.vin.size());
188+
for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
189+
const CTxIn& txin = tx.vin[txinIndex];
190+
CCoins coins;
191+
if (!viewMemPool.GetCoins(txin.prevout.hash, coins)) {
192+
return error("%s: Missing input", __func__);
193+
}
194+
if (coins.nHeight == MEMPOOL_HEIGHT) {
195+
// Assume all mempool transaction confirm in the next block
196+
prevheights[txinIndex] = tip->nHeight + 1;
197+
} else {
198+
prevheights[txinIndex] = coins.nHeight;
199+
}
200+
}
183201
184-
https://github.com/bitcoin/bitcoin/pull/6312
202+
std::pair<int, int64_t> lockPair = CalculateSequenceLocks(tx, flags, &prevheights, index);
203+
return EvaluateSequenceLocks(index, lockPair);
204+
}
205+
</pre>
185206

186207
==Acknowledgments==
187208

0 commit comments

Comments
 (0)