Skip to content

Commit c7c9af3

Browse files
committed
Merge pull request #5669
da29ecb Consensus: MOVEONLY: Move CValidationState from main consensus/validation (jtimon) 27afcd8 Consensus: Refactor: Decouple CValidationState from main::AbortNode() (Cory Fields)
2 parents 2b2d5b9 + da29ecb commit c7c9af3

16 files changed

+128
-114
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ BITCOIN_CORE_H = \
9292
compressor.h \
9393
consensus/consensus.h \
9494
consensus/params.h \
95+
consensus/validation.h \
9596
core_io.h \
9697
eccryptoverify.h \
9798
ecwrapper.h \

src/consensus/validation.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2014 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#ifndef BITCOIN_CONSENSUS_VALIDATION_H
7+
#define BITCOIN_CONSENSUS_VALIDATION_H
8+
9+
#include <string>
10+
11+
/** "reject" message codes */
12+
static const unsigned char REJECT_MALFORMED = 0x01;
13+
static const unsigned char REJECT_INVALID = 0x10;
14+
static const unsigned char REJECT_OBSOLETE = 0x11;
15+
static const unsigned char REJECT_DUPLICATE = 0x12;
16+
static const unsigned char REJECT_NONSTANDARD = 0x40;
17+
static const unsigned char REJECT_DUST = 0x41;
18+
static const unsigned char REJECT_INSUFFICIENTFEE = 0x42;
19+
static const unsigned char REJECT_CHECKPOINT = 0x43;
20+
21+
/** Capture information about block/transaction validation */
22+
class CValidationState {
23+
private:
24+
enum mode_state {
25+
MODE_VALID, //! everything ok
26+
MODE_INVALID, //! network rule violation (DoS value may be set)
27+
MODE_ERROR, //! run-time error
28+
} mode;
29+
int nDoS;
30+
std::string strRejectReason;
31+
unsigned char chRejectCode;
32+
bool corruptionPossible;
33+
public:
34+
CValidationState() : mode(MODE_VALID), nDoS(0), chRejectCode(0), corruptionPossible(false) {}
35+
bool DoS(int level, bool ret = false,
36+
unsigned char chRejectCodeIn=0, std::string strRejectReasonIn="",
37+
bool corruptionIn=false) {
38+
chRejectCode = chRejectCodeIn;
39+
strRejectReason = strRejectReasonIn;
40+
corruptionPossible = corruptionIn;
41+
if (mode == MODE_ERROR)
42+
return ret;
43+
nDoS += level;
44+
mode = MODE_INVALID;
45+
return ret;
46+
}
47+
bool Invalid(bool ret = false,
48+
unsigned char _chRejectCode=0, std::string _strRejectReason="") {
49+
return DoS(0, ret, _chRejectCode, _strRejectReason);
50+
}
51+
bool Error(std::string strRejectReasonIn="") {
52+
if (mode == MODE_VALID)
53+
strRejectReason = strRejectReasonIn;
54+
mode = MODE_ERROR;
55+
return false;
56+
}
57+
bool IsValid() const {
58+
return mode == MODE_VALID;
59+
}
60+
bool IsInvalid() const {
61+
return mode == MODE_INVALID;
62+
}
63+
bool IsError() const {
64+
return mode == MODE_ERROR;
65+
}
66+
bool IsInvalid(int &nDoSOut) const {
67+
if (IsInvalid()) {
68+
nDoSOut = nDoS;
69+
return true;
70+
}
71+
return false;
72+
}
73+
bool CorruptionPossible() const {
74+
return corruptionPossible;
75+
}
76+
unsigned char GetRejectCode() const { return chRejectCode; }
77+
std::string GetRejectReason() const { return strRejectReason; }
78+
};
79+
80+
#endif // BITCOIN_CONSENSUS_VALIDATION_H

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "amount.h"
1414
#include "checkpoints.h"
1515
#include "compat/sanity.h"
16+
#include "consensus/validation.h"
1617
#include "key.h"
1718
#include "main.h"
1819
#include "miner.h"

src/main.cpp

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "chainparams.h"
1212
#include "checkpoints.h"
1313
#include "checkqueue.h"
14+
#include "consensus/validation.h"
1415
#include "init.h"
1516
#include "merkleblock.h"
1617
#include "net.h"
@@ -1562,6 +1563,24 @@ bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uin
15621563
return true;
15631564
}
15641565

1566+
/** Abort with a message */
1567+
bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
1568+
{
1569+
strMiscWarning = strMessage;
1570+
LogPrintf("*** %s\n", strMessage);
1571+
uiInterface.ThreadSafeMessageBox(
1572+
userMessage.empty() ? _("Error: A fatal internal error occured, see debug.log for details") : userMessage,
1573+
"", CClientUIInterface::MSG_ERROR);
1574+
StartShutdown();
1575+
return false;
1576+
}
1577+
1578+
bool AbortNode(CValidationState& state, const std::string& strMessage, const std::string& userMessage="")
1579+
{
1580+
AbortNode(strMessage, userMessage);
1581+
return state.Error(strMessage);
1582+
}
1583+
15651584
} // anon namespace
15661585

15671586
/**
@@ -1900,7 +1919,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
19001919
if (!FindUndoPos(state, pindex->nFile, pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
19011920
return error("ConnectBlock(): FindUndoPos failed");
19021921
if (!UndoWriteToDisk(blockundo, pos, pindex->pprev->GetBlockHash()))
1903-
return state.Abort("Failed to write undo data");
1922+
return AbortNode(state, "Failed to write undo data");
19041923

19051924
// update nUndoPos in block index
19061925
pindex->nUndoPos = pos.nPos;
@@ -1913,7 +1932,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
19131932

19141933
if (fTxIndex)
19151934
if (!pblocktree->WriteTxIndex(vPos))
1916-
return state.Abort("Failed to write transaction index");
1935+
return AbortNode(state, "Failed to write transaction index");
19171936

19181937
// add this block to the view's block chain
19191938
view.SetBestBlock(pindex->GetBlockHash());
@@ -2008,7 +2027,7 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
20082027
setDirtyBlockIndex.erase(it++);
20092028
}
20102029
if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
2011-
return state.Abort("Files to write to block index database");
2030+
return AbortNode(state, "Files to write to block index database");
20122031
}
20132032
}
20142033
// Finally remove any pruned files
@@ -2027,7 +2046,7 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
20272046
return state.Error("out of disk space");
20282047
// Flush the chainstate (which may refer to block index entries).
20292048
if (!pcoinsTip->Flush())
2030-
return state.Abort("Failed to write to coin database");
2049+
return AbortNode(state, "Failed to write to coin database");
20312050
nLastFlush = nNow;
20322051
}
20332052
if ((mode == FLUSH_STATE_ALWAYS || mode == FLUSH_STATE_PERIODIC) && nNow > nLastSetChain + (int64_t)DATABASE_WRITE_INTERVAL * 1000000) {
@@ -2036,7 +2055,7 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
20362055
nLastSetChain = nNow;
20372056
}
20382057
} catch (const std::runtime_error& e) {
2039-
return state.Abort(std::string("System error while flushing: ") + e.what());
2058+
return AbortNode(state, std::string("System error while flushing: ") + e.what());
20402059
}
20412060
return true;
20422061
}
@@ -2100,7 +2119,7 @@ bool static DisconnectTip(CValidationState &state) {
21002119
// Read block from disk.
21012120
CBlock block;
21022121
if (!ReadBlockFromDisk(block, pindexDelete))
2103-
return state.Abort("Failed to read block");
2122+
return AbortNode(state, "Failed to read block");
21042123
// Apply the block atomically to the chain state.
21052124
int64_t nStart = GetTimeMicros();
21062125
{
@@ -2151,7 +2170,7 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock *
21512170
CBlock block;
21522171
if (!pblock) {
21532172
if (!ReadBlockFromDisk(block, pindexNew))
2154-
return state.Abort("Failed to read block");
2173+
return AbortNode(state, "Failed to read block");
21552174
pblock = &block;
21562175
}
21572176
// Apply the block atomically to the chain state.
@@ -2858,11 +2877,11 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
28582877
return error("AcceptBlock(): FindBlockPos failed");
28592878
if (dbp == NULL)
28602879
if (!WriteBlockToDisk(block, blockPos))
2861-
return state.Abort("Failed to write block");
2880+
AbortNode(state, "Failed to write block");
28622881
if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
28632882
return error("AcceptBlock(): ReceivedBlockTransactions failed");
28642883
} catch (const std::runtime_error& e) {
2865-
return state.Abort(std::string("System error: ") + e.what());
2884+
return AbortNode(state, std::string("System error: ") + e.what());
28662885
}
28672886

28682887
if (fCheckForPruning)
@@ -2937,24 +2956,6 @@ bool TestBlockValidity(CValidationState &state, const CBlock& block, CBlockIndex
29372956
return true;
29382957
}
29392958

2940-
2941-
2942-
2943-
2944-
2945-
2946-
2947-
bool AbortNode(const std::string &strMessage, const std::string &userMessage) {
2948-
strMiscWarning = strMessage;
2949-
LogPrintf("*** %s\n", strMessage);
2950-
uiInterface.ThreadSafeMessageBox(
2951-
userMessage.empty() ? _("Error: A fatal internal error occured, see debug.log for details") : userMessage,
2952-
"", CClientUIInterface::MSG_ERROR);
2953-
StartShutdown();
2954-
return false;
2955-
}
2956-
2957-
29582959
/**
29592960
* BLOCK PRUNING CODE
29602961
*/

src/main.h

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,6 @@ static const unsigned int DATABASE_FLUSH_INTERVAL = 24 * 60 * 60;
8989
/** Maximum length of reject messages. */
9090
static const unsigned int MAX_REJECT_MESSAGE_LENGTH = 111;
9191

92-
/** "reject" message codes */
93-
static const unsigned char REJECT_MALFORMED = 0x01;
94-
static const unsigned char REJECT_INVALID = 0x10;
95-
static const unsigned char REJECT_OBSOLETE = 0x11;
96-
static const unsigned char REJECT_DUPLICATE = 0x12;
97-
static const unsigned char REJECT_NONSTANDARD = 0x40;
98-
static const unsigned char REJECT_DUST = 0x41;
99-
static const unsigned char REJECT_INSUFFICIENTFEE = 0x42;
100-
static const unsigned char REJECT_CHECKPOINT = 0x43;
101-
10292
struct BlockHasher
10393
{
10494
size_t operator()(const uint256& hash) const { return hash.GetCheapHash(); }
@@ -230,8 +220,6 @@ void UnlinkPrunedFiles(std::set<int>& setFilesToPrune);
230220

231221
/** Create a new block index entry for a given block hash */
232222
CBlockIndex * InsertBlockIndex(uint256 hash);
233-
/** Abort with a message */
234-
bool AbortNode(const std::string &msg, const std::string &userMessage="");
235223
/** Get statistics from node state */
236224
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats);
237225
/** Increase a node's misbehavior score. */
@@ -461,69 +449,6 @@ class CBlockFileInfo
461449
}
462450
};
463451

464-
/** Capture information about block/transaction validation */
465-
class CValidationState {
466-
private:
467-
enum mode_state {
468-
MODE_VALID, //! everything ok
469-
MODE_INVALID, //! network rule violation (DoS value may be set)
470-
MODE_ERROR, //! run-time error
471-
} mode;
472-
int nDoS;
473-
std::string strRejectReason;
474-
unsigned char chRejectCode;
475-
bool corruptionPossible;
476-
public:
477-
CValidationState() : mode(MODE_VALID), nDoS(0), chRejectCode(0), corruptionPossible(false) {}
478-
bool DoS(int level, bool ret = false,
479-
unsigned char chRejectCodeIn=0, std::string strRejectReasonIn="",
480-
bool corruptionIn=false) {
481-
chRejectCode = chRejectCodeIn;
482-
strRejectReason = strRejectReasonIn;
483-
corruptionPossible = corruptionIn;
484-
if (mode == MODE_ERROR)
485-
return ret;
486-
nDoS += level;
487-
mode = MODE_INVALID;
488-
return ret;
489-
}
490-
bool Invalid(bool ret = false,
491-
unsigned char _chRejectCode=0, std::string _strRejectReason="") {
492-
return DoS(0, ret, _chRejectCode, _strRejectReason);
493-
}
494-
bool Error(std::string strRejectReasonIn="") {
495-
if (mode == MODE_VALID)
496-
strRejectReason = strRejectReasonIn;
497-
mode = MODE_ERROR;
498-
return false;
499-
}
500-
bool Abort(const std::string &msg) {
501-
AbortNode(msg);
502-
return Error(msg);
503-
}
504-
bool IsValid() const {
505-
return mode == MODE_VALID;
506-
}
507-
bool IsInvalid() const {
508-
return mode == MODE_INVALID;
509-
}
510-
bool IsError() const {
511-
return mode == MODE_ERROR;
512-
}
513-
bool IsInvalid(int &nDoSOut) const {
514-
if (IsInvalid()) {
515-
nDoSOut = nDoS;
516-
return true;
517-
}
518-
return false;
519-
}
520-
bool CorruptionPossible() const {
521-
return corruptionPossible;
522-
}
523-
unsigned char GetRejectCode() const { return chRejectCode; }
524-
std::string GetRejectReason() const { return strRejectReason; }
525-
};
526-
527452
/** RAII wrapper for VerifyDB: Verify consistency of the block and coin databases */
528453
class CVerifyDB {
529454
public:

src/miner.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "amount.h"
99
#include "chainparams.h"
1010
#include "consensus/consensus.h"
11+
#include "consensus/validation.h"
1112
#include "hash.h"
1213
#include "main.h"
1314
#include "net.h"

src/rpcblockchain.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include "checkpoints.h"
7+
#include "consensus/validation.h"
78
#include "main.h"
9+
#include "primitives/transaction.h"
810
#include "rpcserver.h"
911
#include "sync.h"
1012
#include "util.h"

src/rpcmining.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "amount.h"
77
#include "chainparams.h"
88
#include "consensus/consensus.h"
9+
#include "consensus/validation.h"
910
#include "core_io.h"
1011
#include "init.h"
1112
#include "main.h"

src/rpcrawtransaction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include "base58.h"
7-
#include "primitives/transaction.h"
7+
#include "consensus/validation.h"
88
#include "core_io.h"
99
#include "init.h"
1010
#include "keystore.h"
1111
#include "main.h"
1212
#include "merkleblock.h"
1313
#include "net.h"
14+
#include "primitives/transaction.h"
1415
#include "rpcserver.h"
1516
#include "script/script.h"
1617
#include "script/script_error.h"

src/test/checkblock_tests.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
//
6-
// Unit tests for block.CheckBlock()
7-
//
8-
9-
10-
115
#include "clientversion.h"
6+
#include "consensus/validation.h"
127
#include "main.h"
13-
#include "utiltime.h"
148
#include "test/test_bitcoin.h"
9+
#include "utiltime.h"
1510

1611
#include <cstdio>
1712

0 commit comments

Comments
 (0)