Skip to content

Commit e421753

Browse files
committed
merge bitcoin#24732: Remove buggy and confusing IncrementExtraNonce
`feature_blockfilterindex_prune.py` changes to `feature_index_prune.py` in bitcoin#21726 (backported as baf6e26, dash#6327), so changes are made there instead.
1 parent 51a68b0 commit e421753

File tree

8 files changed

+33
-59
lines changed

8 files changed

+33
-59
lines changed

src/node/miner.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -584,21 +584,3 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
584584
nDescendantsUpdated += UpdatePackagesForAdded(mempool, ancestors, mapModifiedTx);
585585
}
586586
}
587-
588-
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
589-
{
590-
// Update nExtraNonce
591-
static uint256 hashPrevBlock;
592-
if (hashPrevBlock != pblock->hashPrevBlock) {
593-
nExtraNonce = 0;
594-
hashPrevBlock = pblock->hashPrevBlock;
595-
}
596-
++nExtraNonce;
597-
unsigned int nHeight = pindexPrev->nHeight + 1; // Height first in coinbase required for block.version=2
598-
CMutableTransaction txCoinbase(*pblock->vtx[0]);
599-
txCoinbase.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce));
600-
assert(txCoinbase.vin[0].scriptSig.size() <= 100);
601-
602-
pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
603-
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
604-
}

src/node/miner.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,6 @@ class BlockAssembler
226226
void SortForBlock(const CTxMemPool::setEntries& package, std::vector<CTxMemPool::txiter>& sortedEntries);
227227
};
228228

229-
/** Modify the extranonce in a block */
230-
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
231229
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
232230

233231
#endif // BITCOIN_NODE_MINER_H

src/rpc/mining.cpp

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <chainparams.h>
99
#include <consensus/amount.h>
1010
#include <consensus/consensus.h>
11+
#include <consensus/merkle.h>
1112
#include <consensus/params.h>
1213
#include <consensus/validation.h>
1314
#include <core_io.h>
@@ -121,14 +122,10 @@ static RPCHelpMan getnetworkhashps()
121122
}
122123

123124
#if ENABLE_MINER
124-
static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t& max_tries, unsigned int& extra_nonce, uint256& block_hash)
125+
static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t& max_tries, uint256& block_hash)
125126
{
126127
block_hash.SetNull();
127-
128-
{
129-
LOCK(cs_main);
130-
IncrementExtraNonce(&block, chainman.ActiveChain().Tip(), extra_nonce);
131-
}
128+
block.hashMerkleRoot = BlockMerkleRoot(block);
132129

133130
CChainParams chainparams(Params());
134131

@@ -157,30 +154,20 @@ static UniValue generateBlocks(ChainstateManager& chainman, const NodeContext& n
157154
{
158155
EnsureLLMQContext(node);
159156

160-
int nHeightEnd = 0;
161-
int nHeight = 0;
162-
163-
{ // Don't keep cs_main locked
164-
LOCK(cs_main);
165-
nHeight = chainman.ActiveChain().Height();
166-
nHeightEnd = nHeight+nGenerate;
167-
}
168-
unsigned int nExtraNonce = 0;
169157
UniValue blockHashes(UniValue::VARR);
170-
while (nHeight < nHeightEnd && !ShutdownRequested())
171-
{
158+
while (nGenerate > 0 && !ShutdownRequested()) {
172159
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(chainman.ActiveChainstate(), node, &mempool, Params()).CreateNewBlock(coinbase_script));
173160
if (!pblocktemplate.get())
174161
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
175162
CBlock *pblock = &pblocktemplate->block;
176163

177164
uint256 block_hash;
178-
if (!GenerateBlock(chainman, *pblock, nMaxTries, nExtraNonce, block_hash)) {
165+
if (!GenerateBlock(chainman, *pblock, nMaxTries, block_hash)) {
179166
break;
180167
}
181168

182169
if (!block_hash.IsNull()) {
183-
++nHeight;
170+
--nGenerate;
184171
blockHashes.push_back(block_hash.GetHex());
185172
}
186173
}
@@ -400,9 +387,8 @@ static RPCHelpMan generateblock()
400387

401388
uint256 block_hash;
402389
uint64_t max_tries{DEFAULT_MAX_TRIES};
403-
unsigned int extra_nonce{0};
404390

405-
if (!GenerateBlock(chainman, block, max_tries, extra_nonce, block_hash) || block_hash.IsNull()) {
391+
if (!GenerateBlock(chainman, block, max_tries, block_hash) || block_hash.IsNull()) {
406392
throw JSONRPCError(RPC_MISC_ERROR, "Failed to make block.");
407393
}
408394

src/test/blockfilter_index_tests.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <blockfilter.h>
66
#include <chainparams.h>
7+
#include <consensus/merkle.h>
78
#include <consensus/validation.h>
89
#include <index/blockfilterindex.h>
910
#include <node/miner.h>
@@ -73,9 +74,12 @@ CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev,
7374
for (const CMutableTransaction& tx : txns) {
7475
block.vtx.push_back(MakeTransactionRef(tx));
7576
}
76-
// IncrementExtraNonce creates a valid coinbase and merkleRoot
77-
unsigned int extraNonce = 0;
78-
IncrementExtraNonce(&block, prev, extraNonce);
77+
{
78+
CMutableTransaction tx_coinbase{*block.vtx.at(0)};
79+
tx_coinbase.vin.at(0).scriptSig = CScript{} << prev->nHeight + 1;
80+
block.vtx.at(0) = MakeTransactionRef(std::move(tx_coinbase));
81+
block.hashMerkleRoot = BlockMerkleRoot(block);
82+
}
7983

8084
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
8185

src/test/util/setup_common.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <banman.h>
99
#include <chainparams.h>
1010
#include <consensus/consensus.h>
11+
#include <consensus/merkle.h>
1112
#include <consensus/params.h>
1213
#include <consensus/validation.h>
1314
#include <deploymentstatus.h>
@@ -502,11 +503,14 @@ CBlock TestChainSetup::CreateBlock(
502503
block.vtx[0] = MakeTransactionRef(tmpTx);
503504
}
504505

505-
// IncrementExtraNonce creates a valid coinbase and merkleRoot
506+
// Create a valid coinbase and merkleRoot
506507
{
507-
LOCK(cs_main);
508-
unsigned int extraNonce = 0;
509-
IncrementExtraNonce(&block, chainstate.m_chain.Tip(), extraNonce);
508+
LOCK(::cs_main);
509+
block.hashPrevBlock = chainstate.m_chain.Tip()->GetBlockHash();
510+
CMutableTransaction tx_coinbase{*block.vtx[0]};
511+
tx_coinbase.vin[0].scriptSig = CScript{} << (chainstate.m_chain.Height() + 1) << CScriptNum{1};
512+
block.vtx[0] = MakeTransactionRef(std::move(tx_coinbase));
513+
block.hashMerkleRoot = BlockMerkleRoot(block);
510514
}
511515

512516
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;

test/functional/feature_index_prune.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def run_test(self):
7878
pruneheight_new = node.pruneblockchain(400)
7979
# the prune heights used here and below are magic numbers that are determined by the
8080
# thresholds at which block files wrap, so they depend on disk serialization and default block file size.
81-
assert_equal(pruneheight_new, 366)
81+
assert_equal(pruneheight_new, 368)
8282

8383
self.log.info("check if we can access the tips blockfilter and coinstats when we have pruned some blocks")
8484
tip = self.nodes[0].getbestblockhash()
@@ -95,8 +95,8 @@ def run_test(self):
9595
assert(node.gettxoutsetinfo(hash_type="muhash", hash_or_height=height_hash)['muhash'])
9696

9797
# mine and sync index up to a height that will later be the pruneheight
98-
self.generate(self.nodes[0], 298)
99-
self.sync_index(height=998)
98+
self.generate(self.nodes[0], 51)
99+
self.sync_index(height=751)
100100

101101
self.restart_without_indices()
102102

@@ -108,12 +108,12 @@ def run_test(self):
108108
msg = "Querying specific block heights requires coinstatsindex"
109109
assert_raises_rpc_error(-8, msg, node.gettxoutsetinfo, "muhash", height_hash)
110110

111-
self.mine_batches(502)
111+
self.mine_batches(749)
112112

113113
self.log.info("prune exactly up to the indices best blocks while the indices are disabled")
114114
for i in range(3):
115115
pruneheight_2 = self.nodes[i].pruneblockchain(1000)
116-
assert_equal(pruneheight_2, 732)
116+
assert_equal(pruneheight_2, 736)
117117
# Restart the nodes again with the indices activated
118118
self.restart_node(i, extra_args=self.extra_args[i], expected_stderr=EXPECTED_STDERR_NO_GOV_PRUNE)
119119

@@ -148,7 +148,7 @@ def run_test(self):
148148
for node in self.nodes[:2]:
149149
with node.assert_debug_log(['limited pruning to height 2489']):
150150
pruneheight_new = node.pruneblockchain(2500)
151-
assert_equal(pruneheight_new, 2196)
151+
assert_equal(pruneheight_new, 2208)
152152

153153
self.log.info("ensure that prune locks don't prevent indices from failing in a reorg scenario")
154154
with self.nodes[0].assert_debug_log(['basic block filter index prune lock moved back to 2480']):

test/functional/feature_utxo_set_hash.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ def test_muhash_implementation(self):
6767
assert_equal(finalized[::-1].hex(), node_muhash)
6868

6969
self.log.info("Test deterministic UTXO set hash results")
70-
assert_equal(node.gettxoutsetinfo()['hash_serialized_2'], "91ceba6561281d14db95afb48894fa6f764daa9f2190d05cb397e759634e10bf")
71-
assert_equal(node.gettxoutsetinfo("muhash")['muhash'], "c75ac7fcf057ef30cb5880fc800a99d3908fd0eb8593647512823ada6e359ef5")
70+
assert_equal(node.gettxoutsetinfo()['hash_serialized_2'], "1d640be368b1d811619bc27bc435db672e3ce17f524c0f78def9f7398aef9eac")
71+
assert_equal(node.gettxoutsetinfo("muhash")['muhash'], "c2ca3b5233cf7f4f9ba63ecd3166abf9a6b8c76fe050cf7d51acaeca0d52e78e")
7272

7373
def run_test(self):
7474
self.test_muhash_implementation()

test/functional/rpc_dumptxoutset.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ def run_test(self):
3737
# Blockhash should be deterministic based on mocked time.
3838
assert_equal(
3939
out['base_hash'],
40-
'0266fa3cd96867c40f2f3056da3b66daf8fb4c83ec4f3f5344fd3fe9755794b5')
40+
'38619d68335f21ded08d6e1f4ef9572eaeb0bb04b9070b3e6be33c7c908564b4')
4141

4242
with open(str(expected_path), 'rb') as f:
4343
digest = hashlib.sha256(f.read()).hexdigest()
4444
# UTXO snapshot hash should be deterministic based on mocked time.
4545
assert_equal(
46-
digest, '32f1d4b7f643c97e88c540f431e8277fdd9332c3dea260b046c93787745e35b0')
46+
digest, '4a34cf865938252bf0bef702989955824d886f595afab596b1edac4dd31cd89f')
4747

4848
assert_equal(
49-
out['txoutset_hash'], '970c1bc05f0d0920cc1eae5854860f90f85453ec815e710f40135ec149345ade')
49+
out['txoutset_hash'], 'b2d7429106c96f5ab831843d5c96ba131ca8793111d0a0e30e7d7d8b4841e6cc')
5050
assert_equal(out['nchaintx'], 101)
5151

5252
# Specifying a path to an existing file will fail.

0 commit comments

Comments
 (0)