diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -360,7 +360,6 @@ util/threadnames.cpp util/time.cpp util/url.cpp - util/validation.cpp # obj/build.h "${BUILD_HEADER}" diff --git a/src/consensus/validation.h b/src/consensus/validation.h --- a/src/consensus/validation.h +++ b/src/consensus/validation.h @@ -120,6 +120,17 @@ bool IsError() const { return m_mode == MODE_ERROR; } std::string GetRejectReason() const { return m_reject_reason; } std::string GetDebugMessage() const { return m_debug_message; } + std::string ToString() const { + if (IsValid()) { + return "Valid"; + } + + if (!m_debug_message.empty()) { + return m_reject_reason + ", " + m_debug_message; + } + + return m_reject_reason; + } }; inline ValidationState::~ValidationState(){}; diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -58,7 +58,6 @@ #include #include #include -#include #include #include #include @@ -1366,8 +1365,7 @@ // connected in the active best chain BlockValidationState state; if (!ActivateBestChain(config, state)) { - LogPrintf("Failed to connect best block (%s)\n", - FormatStateMessage(state)); + LogPrintf("Failed to connect best block (%s)\n", state.ToString()); StartShutdown(); return; } diff --git a/src/miner.cpp b/src/miner.cpp --- a/src/miner.cpp +++ b/src/miner.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include @@ -229,8 +228,7 @@ .withCheckPoW(false) .withCheckMerkleRoot(false))) { throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", - __func__, - FormatStateMessage(state))); + __func__, state.ToString())); } int64_t nTime2 = GetTimeMicros(); diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -30,7 +30,6 @@ #include #include #include -#include #include #include @@ -1670,7 +1669,7 @@ BlockValidationState state; if (!ActivateBestChain(config, state, a_recent_block)) { LogPrint(BCLog::NET, "failed to activate chain (%s)\n", - FormatStateMessage(state)); + state.ToString()); } } @@ -2713,7 +2712,7 @@ BlockValidationState state; if (!ActivateBestChain(config, state, a_recent_block)) { LogPrint(BCLog::NET, "failed to activate chain (%s)\n", - FormatStateMessage(state)); + state.ToString()); } } @@ -3023,7 +3022,7 @@ LogPrintf("Not relaying invalid transaction %s from " "whitelisted peer=%d (%s)\n", tx.GetId().ToString(), pfrom->GetId(), - FormatStateMessage(state)); + state.ToString()); } else { LogPrintf("Force relaying tx %s from whitelisted peer=%d\n", tx.GetId().ToString(), pfrom->GetId()); @@ -3052,8 +3051,7 @@ if (state.IsInvalid()) { LogPrint(BCLog::MEMPOOLREJ, "%s from peer=%d was not accepted: %s\n", - tx.GetHash().ToString(), pfrom->GetId(), - FormatStateMessage(state)); + tx.GetHash().ToString(), pfrom->GetId(), state.ToString()); MaybePunishNodeForTx(pfrom->GetId(), state); } return true; @@ -3622,7 +3620,7 @@ BlockValidationState state; if (!ActivateBestChain(config, state)) { LogPrint(BCLog::NET, "failed to activate chain (%s)\n", - FormatStateMessage(state)); + state.ToString()); } } diff --git a/src/node/transaction.cpp b/src/node/transaction.cpp --- a/src/node/transaction.cpp +++ b/src/node/transaction.cpp @@ -12,7 +12,6 @@ #include #include #include -#include #include #include @@ -53,7 +52,7 @@ TxValidationState state; if (!AcceptToMemoryPool(config, *node.mempool, state, std::move(tx), false /* bypass_limits */, max_tx_fee)) { - err_string = FormatStateMessage(state); + err_string = state.ToString(); if (state.IsInvalid()) { if (state.GetResult() == TxValidationResult::TX_MISSING_INPUTS) { diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -33,7 +33,6 @@ #include #include #include -#include #include #include #include // For VersionBitsDeploymentInfo @@ -1832,7 +1831,7 @@ } if (!state.IsValid()) { - throw JSONRPCError(RPC_DATABASE_ERROR, FormatStateMessage(state)); + throw JSONRPCError(RPC_DATABASE_ERROR, state.ToString()); } return NullUniValue; @@ -1872,7 +1871,7 @@ } if (!state.IsValid()) { - throw JSONRPCError(RPC_DATABASE_ERROR, FormatStateMessage(state)); + throw JSONRPCError(RPC_DATABASE_ERROR, state.ToString()); } return NullUniValue; @@ -1950,7 +1949,7 @@ ActivateBestChain(config, state); if (!state.IsValid()) { - throw JSONRPCError(RPC_DATABASE_ERROR, FormatStateMessage(state)); + throw JSONRPCError(RPC_DATABASE_ERROR, state.ToString()); } return NullUniValue; diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include @@ -394,7 +393,7 @@ } if (state.IsError()) { - throw JSONRPCError(RPC_VERIFY_ERROR, FormatStateMessage(state)); + throw JSONRPCError(RPC_VERIFY_ERROR, state.ToString()); } if (state.IsInvalid()) { @@ -949,7 +948,7 @@ return NullUniValue; } if (state.IsError()) { - throw JSONRPCError(RPC_VERIFY_ERROR, FormatStateMessage(state)); + throw JSONRPCError(RPC_VERIFY_ERROR, state.ToString()); } throw JSONRPCError(RPC_VERIFY_ERROR, state.GetRejectReason()); } diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -30,7 +30,6 @@ #include #include #include -#include #include #include #include @@ -161,8 +160,8 @@ { BlockValidationState state; if (!ActivateBestChain(config, state)) { - throw std::runtime_error(strprintf("ActivateBestChain failed. (%s)", - FormatStateMessage(state))); + throw std::runtime_error( + strprintf("ActivateBestChain failed. (%s)", state.ToString())); } } constexpr int script_check_threads = 2; diff --git a/src/util/validation.h b/src/util/validation.h deleted file mode 100644 --- a/src/util/validation.h +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2019 The Bitcoin Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#ifndef BITCOIN_UTIL_VALIDATION_H -#define BITCOIN_UTIL_VALIDATION_H - -#include - -class ValidationState; - -/** Convert ValidationState to a human-readable message for logging */ -std::string FormatStateMessage(const ValidationState &state); - -#endif // BITCOIN_UTIL_VALIDATION_H diff --git a/src/util/validation.cpp b/src/util/validation.cpp deleted file mode 100644 --- a/src/util/validation.cpp +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2019 The Bitcoin Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#include - -#include -#include - -std::string FormatStateMessage(const ValidationState &state) { - if (state.IsValid()) { - return "Valid"; - } - - const std::string debug_message = state.GetDebugMessage(); - if (!debug_message.empty()) { - return strprintf("%s, %s", state.GetRejectReason(), debug_message); - } - - return strprintf("%s", state.GetRejectReason()); -} diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -46,7 +46,6 @@ #include #include #include -#include #include #include @@ -455,7 +454,7 @@ if (!Consensus::CheckTxInputs(tx, state, view, GetSpendHeight(view), nFees)) { return error("%s: Consensus::CheckTxInputs: %s, %s", __func__, - tx.GetId().ToString(), FormatStateMessage(state)); + tx.GetId().ToString(), state.ToString()); } const uint32_t nextBlockScriptVerifyFlags = @@ -570,7 +569,7 @@ // being activated yet (during IBD). return error("%s: BUG! PLEASE REPORT THIS! CheckInputs failed " "against next-block but not STANDARD flags %s, %s", - __func__, txid.ToString(), FormatStateMessage(state)); + __func__, txid.ToString(), state.ToString()); } if (nSigChecksStandard != nSigChecksConsensus) { @@ -1493,7 +1492,7 @@ "hardware failure; shutting down"); } return error("%s: Consensus::CheckBlock: %s", __func__, - FormatStateMessage(state)); + state.ToString()); } // Verify that the view's current state corresponds to the previous block @@ -1742,7 +1741,7 @@ tx_state.GetDebugMessage()); return error("%s: Consensus::CheckTxInputs: %s, %s", __func__, - tx.GetId().ToString(), FormatStateMessage(state)); + tx.GetId().ToString(), state.ToString()); } nFees += txfee; } @@ -1802,7 +1801,7 @@ tx_state.GetRejectReason(), tx_state.GetDebugMessage()); return error("ConnectBlock(): CheckInputs on %s failed with %s", - tx.GetId().ToString(), FormatStateMessage(state)); + tx.GetId().ToString(), state.ToString()); } control.Add(vChecks); @@ -2069,7 +2068,7 @@ const CChainParams &chainparams = Params(); if (!this->FlushStateToDisk(chainparams, state, FlushStateMode::ALWAYS)) { LogPrintf("%s: failed to flush state (%s)\n", __func__, - FormatStateMessage(state)); + state.ToString()); } } @@ -2079,7 +2078,7 @@ const CChainParams &chainparams = Params(); if (!this->FlushStateToDisk(chainparams, state, FlushStateMode::NONE)) { LogPrintf("%s: failed to flush state (%s)\n", __func__, - FormatStateMessage(state)); + state.ToString()); } } @@ -2399,7 +2398,7 @@ return error("%s: ConnectBlock %s failed, %s", __func__, pindexNew->GetBlockHash().ToString(), - FormatStateMessage(state)); + state.ToString()); } // Update the finalized block. @@ -2409,7 +2408,7 @@ !MarkBlockAsFinal(config, state, pindexToFinalize)) { return error("ConnectTip(): MarkBlockAsFinal %s failed (%s)", pindexNew->GetBlockHash().ToString(), - FormatStateMessage(state)); + state.ToString()); } nTime3 = GetTimeMicros(); @@ -3889,7 +3888,7 @@ if (!CheckBlockHeader(block, state, chainparams.GetConsensus(), BlockValidationOptions(config))) { return error("%s: Consensus::CheckBlockHeader: %s, %s", __func__, - hash.ToString(), FormatStateMessage(state)); + hash.ToString(), state.ToString()); } // Get prev block index @@ -3911,7 +3910,7 @@ if (!ContextualCheckBlockHeader(chainparams, block, state, pindexPrev, GetAdjustedTime())) { return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", - __func__, hash.ToString(), FormatStateMessage(state)); + __func__, hash.ToString(), state.ToString()); } /* Determine if this block descends from any block which has been found @@ -4152,7 +4151,7 @@ setDirtyBlockIndex.insert(pindex); } - return error("%s: %s (block %s)", __func__, FormatStateMessage(state), + return error("%s: %s (block %s)", __func__, state.ToString(), block.GetHash().ToString()); } @@ -4236,7 +4235,7 @@ if (!ret) { GetMainSignals().BlockChecked(*pblock, state); return error("%s: AcceptBlock FAILED (%s)", __func__, - FormatStateMessage(state)); + state.ToString()); } } @@ -4246,7 +4245,7 @@ BlockValidationState state; if (!::ChainstateActive().ActivateBestChain(config, state, pblock)) { return error("%s: ActivateBestChain failed (%s)", __func__, - FormatStateMessage(state)); + state.ToString()); } return true; @@ -4268,18 +4267,18 @@ if (!ContextualCheckBlockHeader(params, block, state, pindexPrev, GetAdjustedTime())) { return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, - FormatStateMessage(state)); + state.ToString()); } if (!CheckBlock(block, state, params.GetConsensus(), validationOptions)) { return error("%s: Consensus::CheckBlock: %s", __func__, - FormatStateMessage(state)); + state.ToString()); } if (!ContextualCheckBlock(block, state, params.GetConsensus(), pindexPrev)) { return error("%s: Consensus::ContextualCheckBlock: %s", __func__, - FormatStateMessage(state)); + state.ToString()); } if (!::ChainstateActive().ConnectBlock(block, state, &indexDummy, viewNew, @@ -4393,7 +4392,7 @@ if (!::ChainstateActive().FlushStateToDisk( chainparams, state, FlushStateMode::NONE, nManualPruneHeight)) { LogPrintf("%s: failed to flush state (%s)\n", __func__, - FormatStateMessage(state)); + state.ToString()); } } @@ -4759,8 +4758,7 @@ BlockValidationOptions(config))) { return error("%s: *** found bad block at %d, hash=%s (%s)\n", __func__, pindex->nHeight, - pindex->GetBlockHash().ToString(), - FormatStateMessage(state)); + pindex->GetBlockHash().ToString(), state.ToString()); } // check level 2: verify undo validity @@ -4842,7 +4840,7 @@ return error("VerifyDB(): *** found unconnectable block at %d, " "hash=%s (%s)", pindex->nHeight, pindex->GetBlockHash().ToString(), - FormatStateMessage(state)); + state.ToString()); } } } diff --git a/src/validationinterface.cpp b/src/validationinterface.cpp --- a/src/validationinterface.cpp +++ b/src/validationinterface.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include @@ -261,7 +260,7 @@ void CMainSignals::BlockChecked(const CBlock &block, const BlockValidationState &state) { LOG_EVENT("%s: block hash=%s state=%s", __func__, - block.GetHash().ToString(), FormatStateMessage(state)); + block.GetHash().ToString(), state.ToString()); m_internals->Iterate([&](CValidationInterface &callbacks) { callbacks.BlockChecked(block, state); }); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -30,7 +30,6 @@ #include #include #include -#include #include #include