Skip to content

Commit e6b4aa6

Browse files
committed
miner: Pass in chainman to RegenerateCommitments
Pass in chainman instead of prev_block so that we can enforce the block.hashPrevBlock refers to prev_block invariant in the function itself. We should probably rethink BlockAssembler's API and somehow include commitment regeneration functionality in there. Something like a variant of CreateNewBlock that takes in a std::vector<TxRef> and return a CBlock instead of CBlockTemplate. That could avoid reaching for LookupBlockIndex at all.
1 parent 9ecade1 commit e6b4aa6

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

src/miner.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,21 @@ int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParam
3939
return nNewTime - nOldTime;
4040
}
4141

42-
void RegenerateCommitments(CBlock& block, CBlockIndex* prev_block)
42+
void RegenerateCommitments(CBlock& block, ChainstateManager& chainman)
4343
{
4444
CMutableTransaction tx{*block.vtx.at(0)};
4545
tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block));
4646
block.vtx.at(0) = MakeTransactionRef(tx);
4747

48-
WITH_LOCK(::cs_main, assert(g_chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock) == prev_block));
48+
CBlockIndex* prev_block;
49+
{
50+
// TODO: Temporary scope to check correctness of refactored code.
51+
// Should be removed manually after merge of
52+
// https://github.com/bitcoin/bitcoin/pull/20158
53+
LOCK(::cs_main);
54+
assert(std::addressof(g_chainman.m_blockman) == std::addressof(chainman.m_blockman));
55+
prev_block = chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock);
56+
}
4957
GenerateCoinbaseCommitment(block, prev_block, Params().GetConsensus());
5058

5159
block.hashMerkleRoot = BlockMerkleRoot(block);

src/miner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,6 @@ void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned
203203
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
204204

205205
/** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */
206-
void RegenerateCommitments(CBlock& block, CBlockIndex* prev_block);
206+
void RegenerateCommitments(CBlock& block, ChainstateManager& chainman);
207207

208208
#endif // BITCOIN_MINER_H

src/rpc/mining.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,7 @@ static RPCHelpMan generateblock()
378378

379379
// Add transactions
380380
block.vtx.insert(block.vtx.end(), txs.begin(), txs.end());
381-
CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
382-
RegenerateCommitments(block, prev_block);
381+
RegenerateCommitments(block, chainman);
383382

384383
{
385384
LOCK(cs_main);

src/test/util/setup_common.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransa
246246
for (const CMutableTransaction& tx : txns) {
247247
block.vtx.push_back(MakeTransactionRef(tx));
248248
}
249-
CBlockIndex* prev_block = WITH_LOCK(::cs_main, return g_chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
250-
RegenerateCommitments(block, prev_block);
249+
RegenerateCommitments(block, *Assert(m_node.chainman));
251250

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

0 commit comments

Comments
 (0)