@@ -1858,12 +1858,65 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
1858
1858
// before the first had been spent. Since those coinbases are sufficiently buried its no longer possible to create further
1859
1859
// duplicate transactions descending from the known pairs either.
1860
1860
// If we're on the known chain at height greater than where BIP34 activated, we can save the db accesses needed for the BIP30 check.
1861
+
1862
+ // BIP34 requires that a block at height X (block X) has its coinbase
1863
+ // scriptSig start with a CScriptNum of X (indicated height X). The above
1864
+ // logic of no longer requiring BIP30 once BIP34 activates is flawed in the
1865
+ // case that there is a block X before the BIP34 height of 227,931 which has
1866
+ // an indicated height Y where Y is greater than X. The coinbase for block
1867
+ // X would also be a valid coinbase for block Y, which could be a BIP30
1868
+ // violation. An exhaustive search of all mainnet coinbases before the
1869
+ // BIP34 height which have an indicated height greater than the block height
1870
+ // reveals many occurrences. The 3 lowest indicated heights found are
1871
+ // 209,921, 490,897, and 1,983,702 and thus coinbases for blocks at these 3
1872
+ // heights would be the first opportunity for BIP30 to be violated.
1873
+
1874
+ // The search reveals a great many blocks which have an indicated height
1875
+ // greater than 1,983,702, so we simply remove the optimization to skip
1876
+ // BIP30 checking for blocks at height 1,983,702 or higher. Before we reach
1877
+ // that block in another 25 years or so, we should take advantage of a
1878
+ // future consensus change to do a new and improved version of BIP34 that
1879
+ // will actually prevent ever creating any duplicate coinbases in the
1880
+ // future.
1881
+ static constexpr int BIP34_IMPLIES_BIP30_LIMIT = 1983702 ;
1882
+
1883
+ // There is no potential to create a duplicate coinbase at block 209,921
1884
+ // because this is still before the BIP34 height and so explicit BIP30
1885
+ // checking is still active.
1886
+
1887
+ // The final case is block 176,684 which has an indicated height of
1888
+ // 490,897. Unfortunately, this issue was not discovered until about 2 weeks
1889
+ // before block 490,897 so there was not much opportunity to address this
1890
+ // case other than to carefully analyze it and determine it would not be a
1891
+ // problem. Block 490,897 was, in fact, mined with a different coinbase than
1892
+ // block 176,684, but it is important to note that even if it hadn't been or
1893
+ // is remined on an alternate fork with a duplicate coinbase, we would still
1894
+ // not run into a BIP30 violation. This is because the coinbase for 176,684
1895
+ // is spent in block 185,956 in transaction
1896
+ // d4f7fbbf92f4a3014a230b2dc70b8058d02eb36ac06b4a0736d9d60eaa9e8781. This
1897
+ // spending transaction can't be duplicated because it also spends coinbase
1898
+ // 0328dd85c331237f18e781d692c92de57649529bd5edf1d01036daea32ffde29. This
1899
+ // coinbase has an indicated height of over 4.2 billion, and wouldn't be
1900
+ // duplicatable until that height, and it's currently impossible to create a
1901
+ // chain that long. Nevertheless we may wish to consider a future soft fork
1902
+ // which retroactively prevents block 490,897 from creating a duplicate
1903
+ // coinbase. The two historical BIP30 violations often provide a confusing
1904
+ // edge case when manipulating the UTXO and it would be simpler not to have
1905
+ // another edge case to deal with.
1906
+
1907
+ // testnet3 has no blocks before the BIP34 height with indicated heights
1908
+ // post BIP34 before approximately height 486,000,000 and presumably will
1909
+ // be reset before it reaches block 1,983,702 and starts doing unnecessary
1910
+ // BIP30 checking again.
1861
1911
assert (pindex->pprev );
1862
1912
CBlockIndex *pindexBIP34height = pindex->pprev ->GetAncestor (chainparams.GetConsensus ().BIP34Height );
1863
1913
// Only continue to enforce if we're below BIP34 activation height or the block hash at that height doesn't correspond.
1864
1914
fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->GetBlockHash () == chainparams.GetConsensus ().BIP34Hash ));
1865
1915
1866
- if (fEnforceBIP30 ) {
1916
+ // TODO: Remove BIP30 checking from block height 1,983,702 on, once we have a
1917
+ // consensus change that ensures coinbases at those heights can not
1918
+ // duplicate earlier coinbases.
1919
+ if (fEnforceBIP30 || pindex->nHeight >= BIP34_IMPLIES_BIP30_LIMIT) {
1867
1920
for (const auto & tx : block.vtx ) {
1868
1921
for (size_t o = 0 ; o < tx->vout .size (); o++) {
1869
1922
if (view.HaveCoin (COutPoint (tx->GetHash (), o))) {
0 commit comments