Skip to content

Commit

Permalink
log: Use log categories when logging events that P2P peers can trigge…
Browse files Browse the repository at this point in the history
…r arbitrarily

Github-Pull: bitcoin#17828
Rebased-From: e148126
  • Loading branch information
practicalswift authored and luke-jr committed Jan 4, 2020
1 parent f7f35d5 commit 721e636
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace BCLog {
COINDB = (1 << 18),
QT = (1 << 19),
LEVELDB = (1 << 20),
VALIDATION = (1 << 21),
ALL = ~(uint32_t)0,
};

Expand Down
7 changes: 7 additions & 0 deletions src/util/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ bool error(const char* fmt, const Args&... args)
return false;
}

template<typename... Args>
bool error_debug(const BCLog::LogFlags& category, const char* fmt, const Args&... args)
{
LogPrint(category, "ERROR: %s\n", tfm::format(fmt, args...));
return false;
}

std::string FormatException(const std::exception* pex, const char* pszThread);
void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
bool FileCommit(FILE *file);
Expand Down
14 changes: 8 additions & 6 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)

CAmount nFees = 0;
if (!Consensus::CheckTxInputs(tx, state, m_view, GetSpendHeight(m_view), nFees)) {
return error("%s: Consensus::CheckTxInputs: %s, %s", __func__, tx.GetHash().ToString(), FormatStateMessage(state));
return error_debug(BCLog::MEMPOOL, "%s: Consensus::CheckTxInputs: %s, %s", __func__, tx.GetHash().ToString(), FormatStateMessage(state));
}

// Check for non-standard pay-to-script-hash in inputs
Expand Down Expand Up @@ -3556,18 +3556,20 @@ bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, CValidationState
}

if (!CheckBlockHeader(block, state, chainparams.GetConsensus()))
return error("%s: Consensus::CheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
return error_debug(BCLog::VALIDATION, "%s: Consensus::CheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));

// Get prev block index
CBlockIndex* pindexPrev = nullptr;
BlockMap::iterator mi = m_block_index.find(block.hashPrevBlock);
if (mi == m_block_index.end())
return state.Invalid(ValidationInvalidReason::BLOCK_MISSING_PREV, error("%s: prev block not found", __func__), 0, "prev-blk-not-found");
if (mi == m_block_index.end()) {
LogPrint(BCLog::VALIDATION, "ERROR: %s: prev block not found\n", __func__);
return state.Invalid(ValidationInvalidReason::BLOCK_MISSING_PREV, false, 0, "prev-blk-not-found");
}
pindexPrev = (*mi).second;
if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
return state.Invalid(ValidationInvalidReason::BLOCK_INVALID_PREV, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
if (!ContextualCheckBlockHeader(block, state, chainparams, pindexPrev, GetAdjustedTime()))
return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
return error_debug(BCLog::VALIDATION, "%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));

/* Determine if this block descends from any block which has been found
* invalid (m_failed_blocks), then mark pindexPrev and any blocks between
Expand Down Expand Up @@ -3769,7 +3771,7 @@ bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<cons
}
if (!ret) {
GetMainSignals().BlockChecked(*pblock, state);
return error("%s: AcceptBlock FAILED (%s)", __func__, FormatStateMessage(state));
return error_debug(BCLog::VALIDATION, "%s: AcceptBlock FAILED (%s)", __func__, FormatStateMessage(state));
}
}

Expand Down

0 comments on commit 721e636

Please sign in to comment.