refactor: Replace block_hash with block_out

This commit is contained in:
MarcoFalke 2023-03-10 10:45:28 +01:00
parent 8d12127a9c
commit fab9a08e14
No known key found for this signature in database

View File

@ -115,9 +115,9 @@ static RPCHelpMan getnetworkhashps()
};
}
static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t& max_tries, uint256& block_hash)
static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t& max_tries, std::shared_ptr<const CBlock>& block_out)
{
block_hash.SetNull();
block_out.reset();
block.hashMerkleRoot = BlockMerkleRoot(block);
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&
return true;
}
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
if (!chainman.ProcessNewBlock(shared_pblock, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr)) {
block_out = std::make_shared<const CBlock>(block);
if (!chainman.ProcessNewBlock(block_out, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr)) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted");
}
block_hash = block.GetHash();
return true;
}
@ -147,16 +146,15 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler{chainman.ActiveChainstate(), &mempool}.CreateNewBlock(coinbase_script));
if (!pblocktemplate.get())
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
CBlock *pblock = &pblocktemplate->block;
uint256 block_hash;
if (!GenerateBlock(chainman, *pblock, nMaxTries, block_hash)) {
std::shared_ptr<const CBlock> block_out;
if (!GenerateBlock(chainman, pblocktemplate->block, nMaxTries, block_out)) {
break;
}
if (!block_hash.IsNull()) {
if (block_out) {
--nGenerate;
blockHashes.push_back(block_hash.GetHex());
blockHashes.push_back(block_out->GetHash().GetHex());
}
}
return blockHashes;
@ -376,15 +374,15 @@ static RPCHelpMan generateblock()
}
}
uint256 block_hash;
std::shared_ptr<const CBlock> block_out;
uint64_t max_tries{DEFAULT_MAX_TRIES};
if (!GenerateBlock(chainman, block, max_tries, block_hash) || block_hash.IsNull()) {
if (!GenerateBlock(chainman, block, max_tries, block_out) || !block_out) {
throw JSONRPCError(RPC_MISC_ERROR, "Failed to make block.");
}
UniValue obj(UniValue::VOBJ);
obj.pushKV("hash", block_hash.GetHex());
obj.pushKV("hash", block_out->GetHash().GetHex());
return obj;
},
};