Skip to content

Commit 1397afc

Browse files
committed
Merge #19526: log: Avoid treating remote misbehvior as local system error
fa56eda log: Avoid treating remote misbehvior as local system error (MarcoFalke) fa49289 refactor: Switch ValidationState mode to C++11 enum class (MarcoFalke) Pull request description: When logging failures of `CheckBlockHeader` (high-hash), they are always logged as system error. This is problematic for several reasons: * Submitting a blockheader that fails `CheckBlockHeader` over RPC will result in a debug log line that starts with `ERROR`. Proper behaviour should be to log not anything and instead only return the failure reason to the RPC user. This pull does not fix this issue entirely, but is a good first step in the right direction. * A misbehaving peer that sends us an invalid block header that fails `CheckBlockHeader` will result in a debug log line that starts with `ERROR`. Proper behavior should be to log the remote peer misbehavior if logging for that category was enabled. This pull fixes this issue for `CheckBlockHeader` and other functions can be adjusted as well if needed in follow-ups. This should be a good first step in the right direction. ACKs for top commit: practicalswift: re-ACK fa56eda Tree-SHA512: 9793191f5cb57bdff7c93926e94877e8ca2ef89dcebcf9eb155899c733961839ec7c3f9b9f001dc082ada4234fe6e75f6df431301678d6822325840771166d77
2 parents 93decbc + fa56eda commit 1397afc

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

src/consensus/validation.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,37 +75,39 @@ enum class BlockValidationResult {
7575
* by TxValidationState and BlockValidationState for validation information on transactions
7676
* and blocks respectively. */
7777
template <typename Result>
78-
class ValidationState {
78+
class ValidationState
79+
{
7980
private:
80-
enum mode_state {
81-
MODE_VALID, //!< everything ok
82-
MODE_INVALID, //!< network rule violation (DoS value may be set)
83-
MODE_ERROR, //!< run-time error
84-
} m_mode{MODE_VALID};
81+
enum class ModeState {
82+
M_VALID, //!< everything ok
83+
M_INVALID, //!< network rule violation (DoS value may be set)
84+
M_ERROR, //!< run-time error
85+
} m_mode{ModeState::M_VALID};
8586
Result m_result{};
8687
std::string m_reject_reason;
8788
std::string m_debug_message;
89+
8890
public:
8991
bool Invalid(Result result,
90-
const std::string &reject_reason="",
91-
const std::string &debug_message="")
92+
const std::string& reject_reason = "",
93+
const std::string& debug_message = "")
9294
{
9395
m_result = result;
9496
m_reject_reason = reject_reason;
9597
m_debug_message = debug_message;
96-
if (m_mode != MODE_ERROR) m_mode = MODE_INVALID;
98+
if (m_mode != ModeState::M_ERROR) m_mode = ModeState::M_INVALID;
9799
return false;
98100
}
99101
bool Error(const std::string& reject_reason)
100102
{
101-
if (m_mode == MODE_VALID)
103+
if (m_mode == ModeState::M_VALID)
102104
m_reject_reason = reject_reason;
103-
m_mode = MODE_ERROR;
105+
m_mode = ModeState::M_ERROR;
104106
return false;
105107
}
106-
bool IsValid() const { return m_mode == MODE_VALID; }
107-
bool IsInvalid() const { return m_mode == MODE_INVALID; }
108-
bool IsError() const { return m_mode == MODE_ERROR; }
108+
bool IsValid() const { return m_mode == ModeState::M_VALID; }
109+
bool IsInvalid() const { return m_mode == ModeState::M_INVALID; }
110+
bool IsError() const { return m_mode == ModeState::M_ERROR; }
109111
Result GetResult() const { return m_result; }
110112
std::string GetRejectReason() const { return m_reject_reason; }
111113
std::string GetDebugMessage() const { return m_debug_message; }

src/validation.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3641,8 +3641,10 @@ bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationS
36413641
return true;
36423642
}
36433643

3644-
if (!CheckBlockHeader(block, state, chainparams.GetConsensus()))
3645-
return error("%s: Consensus::CheckBlockHeader: %s, %s", __func__, hash.ToString(), state.ToString());
3644+
if (!CheckBlockHeader(block, state, chainparams.GetConsensus())) {
3645+
LogPrint(BCLog::VALIDATION, "%s: Consensus::CheckBlockHeader: %s, %s\n", __func__, hash.ToString(), state.ToString());
3646+
return false;
3647+
}
36463648

36473649
// Get prev block index
36483650
CBlockIndex* pindexPrev = nullptr;

0 commit comments

Comments
 (0)