Skip to content

Commit 9e67b59

Browse files
committed
Consensus: Introduce ValidationResult class
1 parent 9ca6e04 commit 9e67b59

File tree

4 files changed

+61
-19
lines changed

4 files changed

+61
-19
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ BITCOIN_CORE_H = \
8888
coins.h \
8989
compat.h \
9090
compressor.h \
91+
consensus/validation.h \
9192
core_io.h \
9293
crypter.h \
9394
db.h \

src/consensus/validation.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
class ValidationResult
22+
{
23+
public:
24+
const int nDoS;
25+
const bool fValid;
26+
const std::string error;
27+
const unsigned char rejectCode;
28+
const std::string reason;
29+
const bool fCorruption;
30+
31+
ValidationResult(const int nDoSIn, const bool fValidIn = false, const std::string& errorIn="",
32+
const unsigned char rejectCodeIn=0, const std::string& reasonIn="",
33+
const bool fCorruptionIn=false)
34+
: nDoS(nDoSIn), fValid(fValidIn), error(errorIn), rejectCode(rejectCodeIn), reason(reasonIn), fCorruption(fCorruptionIn) {}
35+
};
36+
37+
#endif // BITCOIN_CONSENSUS_VALIDATION_H

src/main.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2968,6 +2968,22 @@ bool static LoadBlockIndexDB()
29682968
return true;
29692969
}
29702970

2971+
bool CValidationState::ApplyResult(const ValidationResult& result)
2972+
{
2973+
chRejectCode = result.rejectCode;
2974+
strRejectReason = result.reason;
2975+
corruptionPossible = result.fCorruption;
2976+
if (mode == MODE_ERROR)
2977+
return result.fValid;
2978+
nDoS += result.nDoS;
2979+
mode = MODE_INVALID;
2980+
if (result.fValid)
2981+
return true;
2982+
if (result.error == "")
2983+
return false;
2984+
return error(result.error.c_str());
2985+
}
2986+
29712987
CVerifyDB::CVerifyDB()
29722988
{
29732989
uiInterface.ShowProgress(_("Verifying blocks..."), 0);

src/main.h

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "chain.h"
1515
#include "chainparams.h"
1616
#include "coins.h"
17+
#include "consensus/validation.h"
1718
#include "primitives/block.h"
1819
#include "primitives/transaction.h"
1920
#include "net.h"
@@ -94,16 +95,6 @@ static const unsigned int DATABASE_WRITE_INTERVAL = 3600;
9495
/** Maximum length of reject messages. */
9596
static const unsigned int MAX_REJECT_MESSAGE_LENGTH = 111;
9697

97-
/** "reject" message codes */
98-
static const unsigned char REJECT_MALFORMED = 0x01;
99-
static const unsigned char REJECT_INVALID = 0x10;
100-
static const unsigned char REJECT_OBSOLETE = 0x11;
101-
static const unsigned char REJECT_DUPLICATE = 0x12;
102-
static const unsigned char REJECT_NONSTANDARD = 0x40;
103-
static const unsigned char REJECT_DUST = 0x41;
104-
static const unsigned char REJECT_INSUFFICIENTFEE = 0x42;
105-
static const unsigned char REJECT_CHECKPOINT = 0x43;
106-
10798
struct BlockHasher
10899
{
109100
size_t operator()(const uint256& hash) const { return hash.GetCheapHash(); }
@@ -438,21 +429,18 @@ class CValidationState {
438429
bool corruptionPossible;
439430
public:
440431
CValidationState() : mode(MODE_VALID), nDoS(0), chRejectCode(0), corruptionPossible(false) {}
432+
// !Modifies the CValidationState from the received ValidationResult and logs the later's error attribute unless it's empty
433+
bool ApplyResult(const ValidationResult& result);
434+
// !Deprecated in favor of CValidationState::ApplyResult()
441435
bool DoS(int level, bool ret = false,
442436
unsigned char chRejectCodeIn=0, std::string strRejectReasonIn="",
443437
bool corruptionIn=false) {
444-
chRejectCode = chRejectCodeIn;
445-
strRejectReason = strRejectReasonIn;
446-
corruptionPossible = corruptionIn;
447-
if (mode == MODE_ERROR)
448-
return ret;
449-
nDoS += level;
450-
mode = MODE_INVALID;
451-
return ret;
438+
return this->ApplyResult(ValidationResult(level, ret, "", chRejectCodeIn, strRejectReasonIn, corruptionIn));
452439
}
440+
// !Deprecated in favor of CValidationState::ApplyResult()
453441
bool Invalid(bool ret = false,
454442
unsigned char _chRejectCode=0, std::string _strRejectReason="") {
455-
return DoS(0, ret, _chRejectCode, _strRejectReason);
443+
return this->ApplyResult(ValidationResult(0, ret, "", _chRejectCode, _strRejectReason));
456444
}
457445
bool Error(std::string strRejectReasonIn="") {
458446
if (mode == MODE_VALID)

0 commit comments

Comments
 (0)