Skip to content

Commit fab9a08

Browse files
author
MarcoFalke
committed
refactor: Replace block_hash with block_out
1 parent 8d12127 commit fab9a08

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/rpc/mining.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ static RPCHelpMan getnetworkhashps()
115115
};
116116
}
117117

118-
static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t& max_tries, uint256& block_hash)
118+
static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t& max_tries, std::shared_ptr<const CBlock>& block_out)
119119
{
120-
block_hash.SetNull();
120+
block_out.reset();
121121
block.hashMerkleRoot = BlockMerkleRoot(block);
122122

123123
while (max_tries > 0 && block.nNonce < std::numeric_limits<uint32_t>::max() && !CheckProofOfWork(block.GetHash(), block.nBits, chainman.GetConsensus()) && !ShutdownRequested()) {
@@ -131,12 +131,11 @@ static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t&
131131
return true;
132132
}
133133

134-
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
135-
if (!chainman.ProcessNewBlock(shared_pblock, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr)) {
134+
block_out = std::make_shared<const CBlock>(block);
135+
if (!chainman.ProcessNewBlock(block_out, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr)) {
136136
throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted");
137137
}
138138

139-
block_hash = block.GetHash();
140139
return true;
141140
}
142141

@@ -147,16 +146,15 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me
147146
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler{chainman.ActiveChainstate(), &mempool}.CreateNewBlock(coinbase_script));
148147
if (!pblocktemplate.get())
149148
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
150-
CBlock *pblock = &pblocktemplate->block;
151149

152-
uint256 block_hash;
153-
if (!GenerateBlock(chainman, *pblock, nMaxTries, block_hash)) {
150+
std::shared_ptr<const CBlock> block_out;
151+
if (!GenerateBlock(chainman, pblocktemplate->block, nMaxTries, block_out)) {
154152
break;
155153
}
156154

157-
if (!block_hash.IsNull()) {
155+
if (block_out) {
158156
--nGenerate;
159-
blockHashes.push_back(block_hash.GetHex());
157+
blockHashes.push_back(block_out->GetHash().GetHex());
160158
}
161159
}
162160
return blockHashes;
@@ -376,15 +374,15 @@ static RPCHelpMan generateblock()
376374
}
377375
}
378376

379-
uint256 block_hash;
377+
std::shared_ptr<const CBlock> block_out;
380378
uint64_t max_tries{DEFAULT_MAX_TRIES};
381379

382-
if (!GenerateBlock(chainman, block, max_tries, block_hash) || block_hash.IsNull()) {
380+
if (!GenerateBlock(chainman, block, max_tries, block_out) || !block_out) {
383381
throw JSONRPCError(RPC_MISC_ERROR, "Failed to make block.");
384382
}
385383

386384
UniValue obj(UniValue::VOBJ);
387-
obj.pushKV("hash", block_hash.GetHex());
385+
obj.pushKV("hash", block_out->GetHash().GetHex());
388386
return obj;
389387
},
390388
};

0 commit comments

Comments
 (0)