Skip to content

Commit

Permalink
Removal of the fAlreadyChecked flag from the entire `ActivateBestCh…
Browse files Browse the repository at this point in the history
…ain` flow.

Github-Pull: #2295
Rebased-From: 6355774
  • Loading branch information
furszy committed Apr 9, 2021
1 parent f53c824 commit 1f38b90
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
23 changes: 9 additions & 14 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1372,11 +1372,11 @@ static int64_t nTimeTotal = 0;
/** Apply the effects of this block (with given index) on the UTXO set represented by coins.
* Validity checks that depend on the UTXO set are also done; ConnectBlock()
* can fail if those validity checks fail (among other reasons). */
static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool fJustCheck = false, bool fAlreadyChecked = false)
static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool fJustCheck = false)
{
AssertLockHeld(cs_main);
// Check it again in case a previous version let a bad block in
if (!fAlreadyChecked && !CheckBlock(block, state, !fJustCheck, !fJustCheck, !fJustCheck)) {
if (!CheckBlock(block, state, !fJustCheck, !fJustCheck, !fJustCheck)) {
if (state.CorruptionPossible()) {
// We don't write down blocks to disk if they may have been
// corrupted, so this should be impossible unless we're having hardware
Expand Down Expand Up @@ -1997,13 +1997,10 @@ class ConnectTrace {
*
* The block is added to connectTrace if connection succeeds.
*/
bool static ConnectTip(CValidationState& state, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, bool fAlreadyChecked, ConnectTrace& connectTrace)
bool static ConnectTip(CValidationState& state, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace)
{
assert(pindexNew->pprev == chainActive.Tip());

if (pblock == NULL)
fAlreadyChecked = false;

// Read block from disk.
int64_t nTime1 = GetTimeMicros();
std::shared_ptr<const CBlock> pthisBlock;
Expand All @@ -2024,7 +2021,7 @@ bool static ConnectTip(CValidationState& state, CBlockIndex* pindexNew, const st
LogPrint(BCLog::BENCH, " - Load block from disk: %.2fms [%.2fs]\n", (nTime2 - nTime1) * 0.001, nTimeReadFromDisk * 0.000001);
{
CCoinsViewCache view(pcoinsTip);
bool rv = ConnectBlock(blockConnecting, state, pindexNew, view, false, fAlreadyChecked);
bool rv = ConnectBlock(blockConnecting, state, pindexNew, view, false);
GetMainSignals().BlockChecked(blockConnecting, state);
if (!rv) {
if (state.IsInvalid())
Expand Down Expand Up @@ -2144,11 +2141,9 @@ static void PruneBlockIndexCandidates()
* Try to make some progress towards making pindexMostWork the active block.
* pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
*/
static bool ActivateBestChainStep(CValidationState& state, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool fAlreadyChecked, bool& fInvalidFound, ConnectTrace& connectTrace)
static bool ActivateBestChainStep(CValidationState& state, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace)
{
AssertLockHeld(cs_main);
if (pblock == NULL)
fAlreadyChecked = false;
const CBlockIndex* pindexOldTip = chainActive.Tip();
const CBlockIndex* pindexFork = chainActive.FindFork(pindexMostWork);

Expand Down Expand Up @@ -2179,7 +2174,7 @@ static bool ActivateBestChainStep(CValidationState& state, CBlockIndex* pindexMo

// Connect new blocks.
for (CBlockIndex* pindexConnect : reverse_iterate(vpindexToConnect)) {
if (!ConnectTip(state, pindexConnect, (pindexConnect == pindexMostWork) ? pblock : std::shared_ptr<const CBlock>(), fAlreadyChecked, connectTrace)) {
if (!ConnectTip(state, pindexConnect, (pindexConnect == pindexMostWork) ? pblock : std::shared_ptr<const CBlock>(), connectTrace)) {
if (state.IsInvalid()) {
// The block violates a consensus rule.
if (!state.CorruptionPossible())
Expand Down Expand Up @@ -2224,7 +2219,7 @@ static bool ActivateBestChainStep(CValidationState& state, CBlockIndex* pindexMo
* that is already loaded (to avoid loading it again from disk).
*/

bool ActivateBestChain(CValidationState& state, std::shared_ptr<const CBlock> pblock, bool fAlreadyChecked)
bool ActivateBestChain(CValidationState& state, std::shared_ptr<const CBlock> pblock)
{
// Note that while we're often called here from ProcessNewBlock, this is
// far from a guarantee. Things in the P2P/RPC will often end up calling
Expand Down Expand Up @@ -2270,7 +2265,7 @@ bool ActivateBestChain(CValidationState& state, std::shared_ptr<const CBlock> pb

bool fInvalidFound = false;
std::shared_ptr<const CBlock> nullBlockPtr;
if (!ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fAlreadyChecked, fInvalidFound, connectTrace))
if (!ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace))
return false;
blocks_connected = true;

Expand Down Expand Up @@ -3301,7 +3296,7 @@ bool ProcessNewBlock(CValidationState& state, CNode* pfrom, const std::shared_pt
newHeight = pindex->nHeight;
}

if (!ActivateBestChain(state, pblock, checked))
if (!ActivateBestChain(state, pblock))
return error("%s : ActivateBestChain failed", __func__);

if (!fLiteMode) {
Expand Down
2 changes: 1 addition & 1 deletion src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ double ConvertBitsToDouble(unsigned int nBits);
int64_t GetMasternodePayment();

/** Find the best known block, and make it the tip of the block chain */
bool ActivateBestChain(CValidationState& state, std::shared_ptr<const CBlock> pblock = std::shared_ptr<const CBlock>(), bool fAlreadyChecked = false);
bool ActivateBestChain(CValidationState& state, std::shared_ptr<const CBlock> pblock = std::shared_ptr<const CBlock>());
CAmount GetBlockValue(int nHeight);

/** Create a new block index entry for a given block hash */
Expand Down

0 comments on commit 1f38b90

Please sign in to comment.