Skip to content

Commit 79bf1a0

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#24732: Remove buggy and confusing IncrementExtraNonce
cccc4e8 Remove nHeightEnd and nHeight in generateBlocks helper (MarcoFalke) fa38b1c Remove buggy and confusing IncrementExtraNonce (MarcoFalke) Pull request description: IncrementExtraNonce has many issues: * It is test-only code, but part of bitcoind * It is using the block height of the tip, as opposed to the block's previous block as reference for the new height. See bitcoin/bitcoin#24730 (comment) * It has no use case in regtest testing. With a low difficulty the extra nonce won't be incremented. With a high difficulty the test-only functions are clumsy to handle anyway. For example, the generate* RPCs will return an empty array once they reached `maxtries`, as opposed to an error. Also the calls can't be aborted early unless the node shuts down completely. So I think it is fine to just remove the extra nonce functionality and leave it to the outside to implement, if needed. For example, a wrapper script can call the `generate*` RPCs once every second, to use the timestamp as extra nonce. ACKs for top commit: ajtowns: ACK cccc4e8 Tree-SHA512: d8a3989ad280ebd4b1b574159b3a396b8a42134347e6be3c88445162d86624d221c416456f45ae75aea62ed8c8a1a9bb3a2532924abca2ef7a879cb8e6b15654
2 parents 372f1a3 + cccc4e8 commit 79bf1a0

File tree

7 files changed

+24
-56
lines changed

7 files changed

+24
-56
lines changed

src/node/miner.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -430,22 +430,4 @@ void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpda
430430
nDescendantsUpdated += UpdatePackagesForAdded(ancestors, mapModifiedTx);
431431
}
432432
}
433-
434-
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
435-
{
436-
// Update nExtraNonce
437-
static uint256 hashPrevBlock;
438-
if (hashPrevBlock != pblock->hashPrevBlock) {
439-
nExtraNonce = 0;
440-
hashPrevBlock = pblock->hashPrevBlock;
441-
}
442-
++nExtraNonce;
443-
unsigned int nHeight = pindexPrev->nHeight + 1; // Height first in coinbase required for block.version=2
444-
CMutableTransaction txCoinbase(*pblock->vtx[0]);
445-
txCoinbase.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce));
446-
assert(txCoinbase.vin[0].scriptSig.size() <= 100);
447-
448-
pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
449-
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
450-
}
451433
} // namespace node

src/node/miner.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,6 @@ class BlockAssembler
200200
int UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set& mapModifiedTx) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs);
201201
};
202202

203-
/** Modify the extranonce in a block */
204-
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
205203
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
206204

207205
/** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */

src/rpc/mining.cpp

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <chainparams.h>
88
#include <consensus/amount.h>
99
#include <consensus/consensus.h>
10+
#include <consensus/merkle.h>
1011
#include <consensus/params.h>
1112
#include <consensus/validation.h>
1213
#include <core_io.h>
@@ -43,7 +44,6 @@
4344

4445
using node::BlockAssembler;
4546
using node::CBlockTemplate;
46-
using node::IncrementExtraNonce;
4747
using node::NodeContext;
4848
using node::RegenerateCommitments;
4949
using node::UpdateTime;
@@ -116,14 +116,10 @@ static RPCHelpMan getnetworkhashps()
116116
};
117117
}
118118

119-
static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t& max_tries, unsigned int& extra_nonce, uint256& block_hash)
119+
static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t& max_tries, uint256& block_hash)
120120
{
121121
block_hash.SetNull();
122-
123-
{
124-
LOCK(cs_main);
125-
IncrementExtraNonce(&block, chainman.ActiveChain().Tip(), extra_nonce);
126-
}
122+
block.hashMerkleRoot = BlockMerkleRoot(block);
127123

128124
CChainParams chainparams(Params());
129125

@@ -149,30 +145,20 @@ static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t&
149145

150146
static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& mempool, const CScript& coinbase_script, int nGenerate, uint64_t nMaxTries)
151147
{
152-
int nHeightEnd = 0;
153-
int nHeight = 0;
154-
155-
{ // Don't keep cs_main locked
156-
LOCK(cs_main);
157-
nHeight = chainman.ActiveChain().Height();
158-
nHeightEnd = nHeight+nGenerate;
159-
}
160-
unsigned int nExtraNonce = 0;
161148
UniValue blockHashes(UniValue::VARR);
162-
while (nHeight < nHeightEnd && !ShutdownRequested())
163-
{
149+
while (nGenerate > 0 && !ShutdownRequested()) {
164150
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(chainman.ActiveChainstate(), mempool, Params()).CreateNewBlock(coinbase_script));
165151
if (!pblocktemplate.get())
166152
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
167153
CBlock *pblock = &pblocktemplate->block;
168154

169155
uint256 block_hash;
170-
if (!GenerateBlock(chainman, *pblock, nMaxTries, nExtraNonce, block_hash)) {
156+
if (!GenerateBlock(chainman, *pblock, nMaxTries, block_hash)) {
171157
break;
172158
}
173159

174160
if (!block_hash.IsNull()) {
175-
++nHeight;
161+
--nGenerate;
176162
blockHashes.push_back(block_hash.GetHex());
177163
}
178164
}
@@ -397,9 +383,8 @@ static RPCHelpMan generateblock()
397383

398384
uint256 block_hash;
399385
uint64_t max_tries{DEFAULT_MAX_TRIES};
400-
unsigned int extra_nonce{0};
401386

402-
if (!GenerateBlock(chainman, block, max_tries, extra_nonce, block_hash) || block_hash.IsNull()) {
387+
if (!GenerateBlock(chainman, block, max_tries, block_hash) || block_hash.IsNull()) {
403388
throw JSONRPCError(RPC_MISC_ERROR, "Failed to make block.");
404389
}
405390

src/test/blockfilter_index_tests.cpp

Lines changed: 7 additions & 4 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>
@@ -18,7 +19,6 @@
1819

1920
using node::BlockAssembler;
2021
using node::CBlockTemplate;
21-
using node::IncrementExtraNonce;
2222

2323
BOOST_AUTO_TEST_SUITE(blockfilter_index_tests)
2424

@@ -76,9 +76,12 @@ CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev,
7676
for (const CMutableTransaction& tx : txns) {
7777
block.vtx.push_back(MakeTransactionRef(tx));
7878
}
79-
// IncrementExtraNonce creates a valid coinbase and merkleRoot
80-
unsigned int extraNonce = 0;
81-
IncrementExtraNonce(&block, prev, extraNonce);
79+
{
80+
CMutableTransaction tx_coinbase{*block.vtx.at(0)};
81+
tx_coinbase.vin.at(0).scriptSig = CScript{} << prev->nHeight + 1;
82+
block.vtx.at(0) = MakeTransactionRef(std::move(tx_coinbase));
83+
block.hashMerkleRoot = BlockMerkleRoot(block);
84+
}
8285

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

test/functional/feature_blockfilterindex_prune.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def run_test(self):
3131
pruneheight = self.nodes[0].pruneblockchain(400)
3232
# the prune heights used here and below are magic numbers that are determined by the
3333
# thresholds at which block files wrap, so they depend on disk serialization and default block file size.
34-
assert_equal(pruneheight, 248)
34+
assert_equal(pruneheight, 249)
3535

3636
self.log.info("check if we can access the tips blockfilter when we have pruned some blocks")
3737
assert_greater_than(len(self.nodes[0].getblockfilter(self.nodes[0].getbestblockhash())['filter']), 0)
@@ -40,19 +40,19 @@ def run_test(self):
4040
assert_greater_than(len(self.nodes[0].getblockfilter(self.nodes[0].getblockhash(2))['filter']), 0)
4141

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

4646
self.log.info("start node without blockfilterindex")
4747
self.restart_node(0, extra_args=["-fastprune", "-prune=1"])
4848

4949
self.log.info("make sure accessing the blockfilters throws an error")
5050
assert_raises_rpc_error(-1, "Index is not enabled for filtertype basic", self.nodes[0].getblockfilter, self.nodes[0].getblockhash(2))
51-
self.generate(self.nodes[0], 502)
51+
self.generate(self.nodes[0], 749)
5252

5353
self.log.info("prune exactly up to the blockfilterindexes best block while blockfilters are disabled")
5454
pruneheight_2 = self.nodes[0].pruneblockchain(1000)
55-
assert_equal(pruneheight_2, 998)
55+
assert_equal(pruneheight_2, 751)
5656
self.restart_node(0, extra_args=["-fastprune", "-prune=1", "-blockfilterindex=1"])
5757
self.log.info("make sure that we can continue with the partially synced index after having pruned up to the index height")
5858
self.sync_index(height=1500)

test/functional/feature_utxo_set_hash.py

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

7171
self.log.info("Test deterministic UTXO set hash results")
72-
assert_equal(node.gettxoutsetinfo()['hash_serialized_2'], "3a570529b4c32e77268de1f81b903c75cc2da53c48df0d125c1e697ba7c8c7b7")
73-
assert_equal(node.gettxoutsetinfo("muhash")['muhash'], "a13e0e70eb8acc786549596e3bc154623f1a5a622ba2f70715f6773ec745f435")
72+
assert_equal(node.gettxoutsetinfo()['hash_serialized_2'], "f9aa4fb5ffd10489b9a6994e70ccf1de8a8bfa2d5f201d9857332e9954b0855d")
73+
assert_equal(node.gettxoutsetinfo("muhash")['muhash'], "d1725b2fe3ef43e55aa4907480aea98d406fc9e0bf8f60169e2305f1fbf5961b")
7474

7575
def run_test(self):
7676
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-
'6fd417acba2a8738b06fee43330c50d58e6a725046c3d843c8dd7e51d46d1ed6')
40+
'09abf0e7b510f61ca6cf33bab104e9ee99b3528b371d27a2d4b39abb800fba7e')
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, '7ae82c986fa5445678d2a21453bb1c86d39e47af13da137640c2b1cf8093691c')
46+
digest, 'b1bacb602eacf5fbc9a7c2ef6eeb0d229c04e98bdf0c2ea5929012cd0eae3830')
4747

4848
assert_equal(
49-
out['txoutset_hash'], 'd4b614f476b99a6e569973bf1c0120d88b1a168076f8ce25691fb41dd1cef149')
49+
out['txoutset_hash'], '1f7e3befd45dc13ae198dfbb22869a9c5c4196f8e9ef9735831af1288033f890')
5050
assert_equal(out['nchaintx'], 101)
5151

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

0 commit comments

Comments
 (0)