Skip to content

Commit

Permalink
Consensus: Introduce ValidationResult class
Browse files Browse the repository at this point in the history
  • Loading branch information
jtimon committed Jan 17, 2015
1 parent 9ca6e04 commit 9e67b59
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ BITCOIN_CORE_H = \
coins.h \
compat.h \
compressor.h \
consensus/validation.h \
core_io.h \
crypter.h \
db.h \
Expand Down
37 changes: 37 additions & 0 deletions src/consensus/validation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 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_CONSENSUS_VALIDATION_H
#define BITCOIN_CONSENSUS_VALIDATION_H

#include <string>

/** "reject" message codes */
static const unsigned char REJECT_MALFORMED = 0x01;
static const unsigned char REJECT_INVALID = 0x10;
static const unsigned char REJECT_OBSOLETE = 0x11;
static const unsigned char REJECT_DUPLICATE = 0x12;
static const unsigned char REJECT_NONSTANDARD = 0x40;
static const unsigned char REJECT_DUST = 0x41;
static const unsigned char REJECT_INSUFFICIENTFEE = 0x42;
static const unsigned char REJECT_CHECKPOINT = 0x43;

class ValidationResult
{
public:
const int nDoS;
const bool fValid;
const std::string error;
const unsigned char rejectCode;
const std::string reason;
const bool fCorruption;

ValidationResult(const int nDoSIn, const bool fValidIn = false, const std::string& errorIn="",
const unsigned char rejectCodeIn=0, const std::string& reasonIn="",
const bool fCorruptionIn=false)
: nDoS(nDoSIn), fValid(fValidIn), error(errorIn), rejectCode(rejectCodeIn), reason(reasonIn), fCorruption(fCorruptionIn) {}
};

#endif // BITCOIN_CONSENSUS_VALIDATION_H
16 changes: 16 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2968,6 +2968,22 @@ bool static LoadBlockIndexDB()
return true;
}

bool CValidationState::ApplyResult(const ValidationResult& result)
{
chRejectCode = result.rejectCode;
strRejectReason = result.reason;
corruptionPossible = result.fCorruption;
if (mode == MODE_ERROR)
return result.fValid;
nDoS += result.nDoS;
mode = MODE_INVALID;
if (result.fValid)
return true;
if (result.error == "")
return false;
return error(result.error.c_str());
}

CVerifyDB::CVerifyDB()
{
uiInterface.ShowProgress(_("Verifying blocks..."), 0);
Expand Down
26 changes: 7 additions & 19 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "chain.h"
#include "chainparams.h"
#include "coins.h"
#include "consensus/validation.h"
#include "primitives/block.h"
#include "primitives/transaction.h"
#include "net.h"
Expand Down Expand Up @@ -94,16 +95,6 @@ static const unsigned int DATABASE_WRITE_INTERVAL = 3600;
/** Maximum length of reject messages. */
static const unsigned int MAX_REJECT_MESSAGE_LENGTH = 111;

/** "reject" message codes */
static const unsigned char REJECT_MALFORMED = 0x01;
static const unsigned char REJECT_INVALID = 0x10;
static const unsigned char REJECT_OBSOLETE = 0x11;
static const unsigned char REJECT_DUPLICATE = 0x12;
static const unsigned char REJECT_NONSTANDARD = 0x40;
static const unsigned char REJECT_DUST = 0x41;
static const unsigned char REJECT_INSUFFICIENTFEE = 0x42;
static const unsigned char REJECT_CHECKPOINT = 0x43;

struct BlockHasher
{
size_t operator()(const uint256& hash) const { return hash.GetCheapHash(); }
Expand Down Expand Up @@ -438,21 +429,18 @@ class CValidationState {
bool corruptionPossible;
public:
CValidationState() : mode(MODE_VALID), nDoS(0), chRejectCode(0), corruptionPossible(false) {}
// !Modifies the CValidationState from the received ValidationResult and logs the later's error attribute unless it's empty
bool ApplyResult(const ValidationResult& result);
// !Deprecated in favor of CValidationState::ApplyResult()
bool DoS(int level, bool ret = false,
unsigned char chRejectCodeIn=0, std::string strRejectReasonIn="",
bool corruptionIn=false) {
chRejectCode = chRejectCodeIn;
strRejectReason = strRejectReasonIn;
corruptionPossible = corruptionIn;
if (mode == MODE_ERROR)
return ret;
nDoS += level;
mode = MODE_INVALID;
return ret;
return this->ApplyResult(ValidationResult(level, ret, "", chRejectCodeIn, strRejectReasonIn, corruptionIn));
}
// !Deprecated in favor of CValidationState::ApplyResult()
bool Invalid(bool ret = false,
unsigned char _chRejectCode=0, std::string _strRejectReason="") {
return DoS(0, ret, _chRejectCode, _strRejectReason);
return this->ApplyResult(ValidationResult(0, ret, "", _chRejectCode, _strRejectReason));
}
bool Error(std::string strRejectReasonIn="") {
if (mode == MODE_VALID)
Expand Down

0 comments on commit 9e67b59

Please sign in to comment.