Skip to content

Commit 72b221f

Browse files
authored
Revert "fix BIP68 granularity and mask (#1641)" (#1647)
This reverts commit d3829e5. Reverting and postponing the fix till later. Probably should write a DIP and explore different options first.
1 parent b41f8d3 commit 72b221f

File tree

5 files changed

+18
-28
lines changed

5 files changed

+18
-28
lines changed

qa/rpc-tests/bip68-sequence.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
SEQUENCE_LOCKTIME_DISABLE_FLAG = (1<<31)
1717
SEQUENCE_LOCKTIME_TYPE_FLAG = (1<<22) # this means use time (0 means height)
18-
SEQUENCE_LOCKTIME_GRANULARITY = 7 # this is a bit-shift
19-
SEQUENCE_LOCKTIME_MASK = 0x0003ffff
18+
SEQUENCE_LOCKTIME_GRANULARITY = 9 # this is a bit-shift
19+
SEQUENCE_LOCKTIME_MASK = 0x0000ffff
2020

2121
# RPC error for non-BIP68 final transactions
2222
NOT_FINAL_ERROR = "64: non-BIP68-final"

src/primitives/transaction.h

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,22 @@ class CTxIn
7373
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1 << 31);
7474

7575
/* If CTxIn::nSequence encodes a relative lock-time and this flag
76-
* is set, the relative lock-time has units of 128 seconds,
76+
* is set, the relative lock-time has units of 512 seconds,
7777
* otherwise it specifies blocks with a granularity of 1. */
7878
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
7979

8080
/* If CTxIn::nSequence encodes a relative lock-time, this mask is
8181
* applied to extract that lock-time from the sequence field. */
82-
static const uint32_t LEGACY_SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
83-
static const uint32_t DIP0001_SEQUENCE_LOCKTIME_MASK = 0x0003ffff;
84-
static const uint32_t GetSequenceLocktimeMask(bool fDIP0001Active /*= false */)
85-
{
86-
return fDIP0001Active ? DIP0001_SEQUENCE_LOCKTIME_MASK : LEGACY_SEQUENCE_LOCKTIME_MASK;
87-
}
82+
static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
8883

8984
/* In order to use the same number of bits to encode roughly the
9085
* same wall-clock duration, and because blocks are naturally
91-
* limited to occur every 150s on average, the minimum granularity
92-
* for time-based relative lock-time is fixed at 128 seconds.
86+
* limited to occur every 600s on average, the minimum granularity
87+
* for time-based relative lock-time is fixed at 512 seconds.
9388
* Converting from CTxIn::nSequence to seconds is performed by
94-
* multiplying by 128 = 2^7, or equivalently shifting up by
95-
* 7 bits. */
96-
static const int LEGACY_SEQUENCE_LOCKTIME_GRANULARITY = 9;
97-
static const int DIP0001_SEQUENCE_LOCKTIME_GRANULARITY = 7;
98-
static const int GetSequenceLocktimeGranularity(bool fDIP0001Active /*= false */)
99-
{
100-
return fDIP0001Active ? DIP0001_SEQUENCE_LOCKTIME_GRANULARITY : LEGACY_SEQUENCE_LOCKTIME_GRANULARITY;
101-
}
89+
* multiplying by 512 = 2^9, or equivalently shifting up by
90+
* 9 bits. */
91+
static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
10292

10393
CTxIn()
10494
{

src/script/interpreter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ bool TransactionSignatureChecker::CheckSequence(const CScriptNum& nSequence) con
12131213

12141214
// Mask off any bits that do not have consensus-enforced meaning
12151215
// before doing the integer comparisons
1216-
const uint32_t nLockTimeMask = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | CTxIn::LEGACY_SEQUENCE_LOCKTIME_MASK;
1216+
const uint32_t nLockTimeMask = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | CTxIn::SEQUENCE_LOCKTIME_MASK;
12171217
const int64_t txToSequenceMasked = txToSequence & nLockTimeMask;
12181218
const CScriptNum nSequenceMasked = nSequence & nLockTimeMask;
12191219

src/test/miner_tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,18 +311,18 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
311311

312312
// relative time locked
313313
tx.vin[0].prevout.hash = txFirst[1]->GetHash();
314-
tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | (((chainActive.Tip()->GetMedianTimePast()+1-chainActive[1]->GetMedianTimePast()) >> CTxIn::GetSequenceLocktimeGranularity(fDIP0001ActiveAtTip)) + 1); // txFirst[1] is the 3rd block
314+
tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | (((chainActive.Tip()->GetMedianTimePast()+1-chainActive[1]->GetMedianTimePast()) >> CTxIn::SEQUENCE_LOCKTIME_GRANULARITY) + 1); // txFirst[1] is the 3rd block
315315
prevheights[0] = baseheight + 2;
316316
hash = tx.GetHash();
317317
mempool.addUnchecked(hash, entry.Time(GetTime()).FromTx(tx));
318318
BOOST_CHECK(CheckFinalTx(tx, flags)); // Locktime passes
319319
BOOST_CHECK(!TestSequenceLocks(tx, flags)); // Sequence locks fail
320320

321321
for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
322-
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime += fDIP0001ActiveAtTip ? 128 : 512; //Trick the MedianTimePast
323-
BOOST_CHECK(SequenceLocks(tx, flags, &prevheights, CreateBlockIndex(chainActive.Tip()->nHeight + 1))); // Sequence locks pass 128/512 seconds later
322+
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime += 512; //Trick the MedianTimePast
323+
BOOST_CHECK(SequenceLocks(tx, flags, &prevheights, CreateBlockIndex(chainActive.Tip()->nHeight + 1))); // Sequence locks pass 512 seconds later
324324
for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
325-
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime -= fDIP0001ActiveAtTip ? 128 : 512; //undo tricked MTP
325+
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime -= 512; //undo tricked MTP
326326

327327
// absolute height locked
328328
tx.vin[0].prevout.hash = txFirst[2]->GetHash();
@@ -368,9 +368,9 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
368368
// For now these will still generate a valid template until BIP68 soft fork
369369
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 3);
370370
delete pblocktemplate;
371-
// However if we advance height by 1 and time by 128/512, all of them should be mined
371+
// However if we advance height by 1 and time by 512, all of them should be mined
372372
for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
373-
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime += fDIP0001ActiveAtTip ? 128 : 512; //Trick the MedianTimePast
373+
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime += 512; //Trick the MedianTimePast
374374
chainActive.Tip()->nHeight++;
375375
SetMockTime(chainActive.Tip()->GetMedianTimePast() + 1);
376376

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,9 @@ static std::pair<int, int64_t> CalculateSequenceLocks(const CTransaction &tx, in
301301
// smallest allowed timestamp of the block containing the
302302
// txout being spent, which is the median time past of the
303303
// block prior.
304-
nMinTime = std::max(nMinTime, nCoinTime + (int64_t)((txin.nSequence & CTxIn::GetSequenceLocktimeMask(fDIP0001ActiveAtTip)) << CTxIn::GetSequenceLocktimeGranularity(fDIP0001ActiveAtTip)) - 1);
304+
nMinTime = std::max(nMinTime, nCoinTime + (int64_t)((txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) << CTxIn::SEQUENCE_LOCKTIME_GRANULARITY) - 1);
305305
} else {
306-
nMinHeight = std::max(nMinHeight, nCoinHeight + (int)(txin.nSequence & CTxIn::GetSequenceLocktimeMask(fDIP0001ActiveAtTip)) - 1);
306+
nMinHeight = std::max(nMinHeight, nCoinHeight + (int)(txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) - 1);
307307
}
308308
}
309309

0 commit comments

Comments
 (0)