Skip to content

Commit 0aed17e

Browse files
committed
Refactor FormatStateMessage into ValidationState
1 parent fe63d79 commit 0aed17e

15 files changed

+47
-87
lines changed

src/Makefile.am

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ BITCOIN_CORE_H = \
229229
util/time.h \
230230
util/translation.h \
231231
util/url.h \
232-
util/validation.h \
233232
util/vector.h \
234233
validation.h \
235234
validationinterface.h \
@@ -528,7 +527,6 @@ libbitcoin_util_a_SOURCES = \
528527
util/string.cpp \
529528
util/time.cpp \
530529
util/url.cpp \
531-
util/validation.cpp \
532530
$(BITCOIN_CORE_H)
533531

534532
if GLIBC_BACK_COMPAT

src/consensus/validation.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ class ValidationState {
108108
bool IsError() const { return m_mode == MODE_ERROR; }
109109
std::string GetRejectReason() const { return m_reject_reason; }
110110
std::string GetDebugMessage() const { return m_debug_message; }
111+
std::string ToString() const
112+
{
113+
if (IsValid()) {
114+
return "Valid";
115+
}
116+
117+
if (!m_debug_message.empty()) {
118+
return m_reject_reason + ", " + m_debug_message;
119+
}
120+
121+
return m_reject_reason;
122+
}
111123
};
112124

113125
inline ValidationState::~ValidationState() {};

src/init.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
#include <util/system.h>
5252
#include <util/threadnames.h>
5353
#include <util/translation.h>
54-
#include <util/validation.h>
5554
#include <util/asmap.h>
5655
#include <validation.h>
5756
#include <hash.h>
@@ -710,7 +709,7 @@ static void ThreadImport(std::vector<fs::path> vImportFiles)
710709
// scan for better chains in the block chain database, that are not yet connected in the active best chain
711710
BlockValidationState state;
712711
if (!ActivateBestChain(state, chainparams)) {
713-
LogPrintf("Failed to connect best block (%s)\n", FormatStateMessage(state));
712+
LogPrintf("Failed to connect best block (%s)\n", state.ToString());
714713
StartShutdown();
715714
return;
716715
}

src/miner.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <timedata.h>
2121
#include <util/moneystr.h>
2222
#include <util/system.h>
23-
#include <util/validation.h>
2423

2524
#include <algorithm>
2625
#include <utility>
@@ -167,7 +166,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
167166

168167
BlockValidationState state;
169168
if (!TestBlockValidity(state, chainparams, *pblock, pindexPrev, false, false)) {
170-
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state)));
169+
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString()));
171170
}
172171
int64_t nTime2 = GetTimeMicros();
173172

src/net_processing.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <txmempool.h>
2727
#include <util/system.h>
2828
#include <util/strencodings.h>
29-
#include <util/validation.h>
3029

3130
#include <memory>
3231
#include <typeinfo>
@@ -1432,7 +1431,7 @@ void static ProcessGetBlockData(CNode* pfrom, const CChainParams& chainparams, c
14321431
if (need_activate_chain) {
14331432
BlockValidationState state;
14341433
if (!ActivateBestChain(state, Params(), a_recent_block)) {
1435-
LogPrint(BCLog::NET, "failed to activate chain (%s)\n", FormatStateMessage(state));
1434+
LogPrint(BCLog::NET, "failed to activate chain (%s)\n", state.ToString());
14361435
}
14371436
}
14381437

@@ -2342,7 +2341,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
23422341
}
23432342
BlockValidationState state;
23442343
if (!ActivateBestChain(state, Params(), a_recent_block)) {
2345-
LogPrint(BCLog::NET, "failed to activate chain (%s)\n", FormatStateMessage(state));
2344+
LogPrint(BCLog::NET, "failed to activate chain (%s)\n", state.ToString());
23462345
}
23472346
}
23482347

@@ -2636,7 +2635,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
26362635
{
26372636
LogPrint(BCLog::MEMPOOLREJ, "%s from peer=%d was not accepted: %s\n", tx.GetHash().ToString(),
26382637
pfrom->GetId(),
2639-
FormatStateMessage(state));
2638+
state.ToString());
26402639
MaybePunishNodeForTx(pfrom->GetId(), state);
26412640
}
26422641
return true;

src/node/transaction.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <net.h>
88
#include <net_processing.h>
99
#include <node/context.h>
10-
#include <util/validation.h>
1110
#include <validation.h>
1211
#include <validationinterface.h>
1312
#include <node/transaction.h>
@@ -41,7 +40,7 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
4140
TxValidationState state;
4241
if (!AcceptToMemoryPool(*node.mempool, state, std::move(tx),
4342
nullptr /* plTxnReplaced */, false /* bypass_limits */, max_tx_fee)) {
44-
err_string = FormatStateMessage(state);
43+
err_string = state.ToString();
4544
if (state.IsInvalid()) {
4645
if (state.GetResult() == TxValidationResult::TX_MISSING_INPUTS) {
4746
return TransactionError::MISSING_INPUTS;

src/rpc/blockchain.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include <undo.h>
3232
#include <util/strencodings.h>
3333
#include <util/system.h>
34-
#include <util/validation.h>
3534
#include <validation.h>
3635
#include <validationinterface.h>
3736
#include <warnings.h>
@@ -1486,7 +1485,7 @@ static UniValue preciousblock(const JSONRPCRequest& request)
14861485
PreciousBlock(state, Params(), pblockindex);
14871486

14881487
if (!state.IsValid()) {
1489-
throw JSONRPCError(RPC_DATABASE_ERROR, FormatStateMessage(state));
1488+
throw JSONRPCError(RPC_DATABASE_ERROR, state.ToString());
14901489
}
14911490

14921491
return NullUniValue;
@@ -1524,7 +1523,7 @@ static UniValue invalidateblock(const JSONRPCRequest& request)
15241523
}
15251524

15261525
if (!state.IsValid()) {
1527-
throw JSONRPCError(RPC_DATABASE_ERROR, FormatStateMessage(state));
1526+
throw JSONRPCError(RPC_DATABASE_ERROR, state.ToString());
15281527
}
15291528

15301529
return NullUniValue;
@@ -1561,7 +1560,7 @@ static UniValue reconsiderblock(const JSONRPCRequest& request)
15611560
ActivateBestChain(state, Params());
15621561

15631562
if (!state.IsValid()) {
1564-
throw JSONRPCError(RPC_DATABASE_ERROR, FormatStateMessage(state));
1563+
throw JSONRPCError(RPC_DATABASE_ERROR, state.ToString());
15651564
}
15661565

15671566
return NullUniValue;

src/rpc/mining.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include <util/fees.h>
2929
#include <util/strencodings.h>
3030
#include <util/system.h>
31-
#include <util/validation.h>
3231
#include <validation.h>
3332
#include <validationinterface.h>
3433
#include <versionbitsinfo.h>
@@ -307,7 +306,7 @@ static UniValue BIP22ValidationResult(const BlockValidationState& state)
307306
return NullUniValue;
308307

309308
if (state.IsError())
310-
throw JSONRPCError(RPC_VERIFY_ERROR, FormatStateMessage(state));
309+
throw JSONRPCError(RPC_VERIFY_ERROR, state.ToString());
311310
if (state.IsInvalid())
312311
{
313312
std::string strRejectReason = state.GetRejectReason();
@@ -823,7 +822,7 @@ static UniValue submitheader(const JSONRPCRequest& request)
823822
ProcessNewBlockHeaders({h}, state, Params());
824823
if (state.IsValid()) return NullUniValue;
825824
if (state.IsError()) {
826-
throw JSONRPCError(RPC_VERIFY_ERROR, FormatStateMessage(state));
825+
throw JSONRPCError(RPC_VERIFY_ERROR, state.ToString());
827826
}
828827
throw JSONRPCError(RPC_VERIFY_ERROR, state.GetRejectReason());
829828
}

src/test/util/setup_common.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include <util/strencodings.h>
2626
#include <util/time.h>
2727
#include <util/translation.h>
28-
#include <util/validation.h>
2928
#include <validation.h>
3029
#include <validationinterface.h>
3130

@@ -123,7 +122,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
123122

124123
BlockValidationState state;
125124
if (!ActivateBestChain(state, chainparams)) {
126-
throw std::runtime_error(strprintf("ActivateBestChain failed. (%s)", FormatStateMessage(state)));
125+
throw std::runtime_error(strprintf("ActivateBestChain failed. (%s)", state.ToString()));
127126
}
128127

129128
// Start script-checking threads. Set g_parallel_script_checks to true so they are used.

src/util/validation.cpp

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)