diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -792,6 +792,11 @@ EXCLUSIVE_LOCKS_REQUIRED(peer.m_getdata_requests_mutex) LOCKS_EXCLUDED(cs_main); + /** Process a new block. Perform any post-processing housekeeping */ + void ProcessBlock(const Config &config, CNode &node, + const std::shared_ptr &block, + bool force_processing); + /** Relay map. */ typedef std::map MapRelay; MapRelay mapRelay GUARDED_BY(cs_main); @@ -3433,6 +3438,19 @@ }); }; +void PeerManagerImpl::ProcessBlock(const Config &config, CNode &node, + const std::shared_ptr &block, + bool force_processing) { + bool new_block{false}; + m_chainman.ProcessNewBlock(config, block, force_processing, &new_block); + if (new_block) { + node.m_last_block_time = GetTime(); + } else { + LOCK(cs_main); + mapBlockSource.erase(block->GetHash()); + } +} + void PeerManagerImpl::ProcessMessage( const Config &config, CNode &pfrom, const std::string &msg_type, CDataStream &vRecv, const std::chrono::microseconds time_received, @@ -4715,8 +4733,7 @@ mapBlockSource.emplace(pblock->GetHash(), std::make_pair(pfrom.GetId(), false)); } - bool fNewBlock = false; - // Setting fForceProcessing to true means that we bypass some of + // Setting force_processing to true means that we bypass some of // our anti-DoS protections in AcceptBlock, which filters // unrequested blocks that might be trying to waste our resources // (eg disk space). Because we only try to reconstruct blocks when @@ -4725,15 +4742,7 @@ // we have a chain with at least nMinimumChainWork), and we ignore // compact blocks with less work than our tip, it is safe to treat // reconstructed compact blocks as having been requested. - m_chainman.ProcessNewBlock(config, pblock, - /*fForceProcessing=*/true, &fNewBlock); - if (fNewBlock) { - pfrom.m_last_block_time = GetTime(); - } else { - LOCK(cs_main); - mapBlockSource.erase(pblock->GetHash()); - } - + ProcessBlock(config, pfrom, pblock, /*force_processing=*/true); // hold cs_main for CBlockIndex::IsValid() LOCK(cs_main); if (pindex->IsValid(BlockValidity::TRANSACTIONS)) { @@ -4827,7 +4836,6 @@ } } // Don't hold cs_main when we call into ProcessNewBlock if (fBlockRead) { - bool fNewBlock = false; // Since we requested this block (it was in mapBlocksInFlight), // force it to be processed, even if it would not be a candidate for // new tip (missing previous block, chain not long enough, etc) @@ -4835,14 +4843,7 @@ // disk-space attacks), but this should be safe due to the // protections in the compact block handler -- see related comment // in compact block optimistic reconstruction handling. - m_chainman.ProcessNewBlock(config, pblock, - /*fForceProcessing=*/true, &fNewBlock); - if (fNewBlock) { - pfrom.m_last_block_time = GetTime(); - } else { - LOCK(cs_main); - mapBlockSource.erase(pblock->GetHash()); - } + ProcessBlock(config, pfrom, pblock, /*force_processing=*/true); } return; } @@ -4911,14 +4912,7 @@ // cs_main in ProcessNewBlock is fine. mapBlockSource.emplace(hash, std::make_pair(pfrom.GetId(), true)); } - bool fNewBlock = false; - m_chainman.ProcessNewBlock(config, pblock, forceProcessing, &fNewBlock); - if (fNewBlock) { - pfrom.m_last_block_time = GetTime(); - } else { - LOCK(cs_main); - mapBlockSource.erase(hash); - } + ProcessBlock(config, pfrom, pblock, forceProcessing); return; } diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -1324,27 +1324,27 @@ * block is made active. Note that it does not, however, guarantee that the * specific block passed to it has been checked for validity! * - * If you want to *possibly* get feedback on whether pblock is valid, you + * If you want to *possibly* get feedback on whether block is valid, you * must install a CValidationInterface (see validationinterface.h) - this * will have its BlockChecked method called whenever *any* block completes * validation. * - * Note that we guarantee that either the proof-of-work is valid on pblock, + * Note that we guarantee that either the proof-of-work is valid on block, * or (and possibly also) BlockChecked will have been called. * * May not be called in a validationinterface callback. * * @param[in] config The global config. - * @param[in] pblock The block we want to process. - * @param[in] fForceProcessing Process this block even if unrequested; + * @param[in] block The block we want to process. + * @param[in] force_processing Process this block even if unrequested; * used for non-network block sources. - * @param[out] fNewBlock A boolean which is set to indicate if the block + * @param[out] new_block A boolean which is set to indicate if the block * was first received via this call. * @returns If the block was processed, independently of block validity */ bool ProcessNewBlock(const Config &config, - const std::shared_ptr pblock, - bool fForceProcessing, bool *fNewBlock) + const std::shared_ptr &block, + bool force_processing, bool *new_block) LOCKS_EXCLUDED(cs_main); /** diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -4236,13 +4236,13 @@ } bool ChainstateManager::ProcessNewBlock( - const Config &config, const std::shared_ptr pblock, - bool fForceProcessing, bool *fNewBlock) { + const Config &config, const std::shared_ptr &block, + bool force_processing, bool *new_block) { AssertLockNotHeld(cs_main); { - if (fNewBlock) { - *fNewBlock = false; + if (new_block) { + *new_block = false; } BlockValidationState state; @@ -4256,16 +4256,16 @@ // Ensure that CheckBlock() passes before calling AcceptBlock, as // belt-and-suspenders. bool ret = - CheckBlock(*pblock, state, config.GetChainParams().GetConsensus(), + CheckBlock(*block, state, config.GetChainParams().GetConsensus(), BlockValidationOptions(config)); if (ret) { // Store to disk ret = ActiveChainstate().AcceptBlock( - config, pblock, state, fForceProcessing, nullptr, fNewBlock); + config, block, state, force_processing, nullptr, new_block); } if (!ret) { - GetMainSignals().BlockChecked(*pblock, state); + GetMainSignals().BlockChecked(*block, state); return error("%s: AcceptBlock FAILED (%s)", __func__, state.ToString()); } @@ -4275,7 +4275,7 @@ // Only used to report errors, not invalidity - ignore it BlockValidationState state; - if (!ActiveChainstate().ActivateBestChain(config, state, pblock)) { + if (!ActiveChainstate().ActivateBestChain(config, state, block)) { return error("%s: ActivateBestChain failed (%s)", __func__, state.ToString()); }