Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2016 The Bitcoin Core developers 3 : // Copyright (c) 2020-2021 The PIVX Core developers 4 : // Distributed under the MIT software license, see the accompanying 5 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 : 7 : #ifndef PIVX_CONSENSUS_VALIDATION_H 8 : #define PIVX_CONSENSUS_VALIDATION_H 9 : 10 : #include <string> 11 : 12 : /** "reject" message codes */ 13 : static const unsigned char REJECT_MALFORMED = 0x01; 14 : static const unsigned char REJECT_INVALID = 0x10; 15 : static const unsigned char REJECT_OBSOLETE = 0x11; 16 : static const unsigned char REJECT_DUPLICATE = 0x12; 17 : static const unsigned char REJECT_NONSTANDARD = 0x40; 18 : static const unsigned char REJECT_DUST = 0x41; 19 : static const unsigned char REJECT_INSUFFICIENTFEE = 0x42; 20 : static const unsigned char REJECT_CHECKPOINT = 0x43; 21 : 22 : /** Capture information about block/transaction validation */ 23 : class CValidationState 24 : { 25 : private: 26 : enum mode_state { 27 : MODE_VALID, //! everything ok 28 : MODE_INVALID, //! network rule violation (DoS value may be set) 29 : MODE_ERROR, //! run-time error 30 : } mode; 31 : int nDoS; 32 : std::string strRejectReason; 33 : unsigned int chRejectCode; 34 : bool corruptionPossible; 35 : std::string strDebugMessage; 36 : 37 : public: 38 3098303 : CValidationState() : mode(MODE_VALID), nDoS(0), chRejectCode(0), corruptionPossible(false) {} 39 3218 : bool DoS(int level, bool ret = false, 40 : unsigned int chRejectCodeIn = 0, 41 : std::string strRejectReasonIn = "", 42 : bool corruptionIn = false, 43 : const std::string& strDebugMessageIn = "") 44 : { 45 3218 : chRejectCode = chRejectCodeIn; 46 3107 : strRejectReason = strRejectReasonIn; 47 3218 : corruptionPossible = corruptionIn; 48 3218 : strDebugMessage = strDebugMessageIn; 49 3218 : if (mode == MODE_ERROR) 50 : return ret; 51 3218 : nDoS += level; 52 3218 : mode = MODE_INVALID; 53 3218 : return ret; 54 : } 55 163 : bool Invalid(bool ret = false, 56 : unsigned int _chRejectCode = 0, 57 : const std::string& _strRejectReason = "", 58 : const std::string& _strDebugMessage = "") 59 : { 60 347 : return DoS(0, ret, _chRejectCode, _strRejectReason, false, _strDebugMessage); 61 : } 62 10296 : bool Error(std::string strRejectReasonIn = "") 63 : { 64 10296 : if (mode == MODE_VALID) 65 10296 : strRejectReason = strRejectReasonIn; 66 10296 : mode = MODE_ERROR; 67 10296 : return false; 68 : } 69 65816 : bool IsValid() const 70 : { 71 65816 : return mode == MODE_VALID; 72 : } 73 48188 : bool IsInvalid() const 74 : { 75 48188 : return mode == MODE_INVALID; 76 : } 77 12778 : bool IsError() const 78 : { 79 12778 : return mode == MODE_ERROR; 80 : } 81 48062 : bool IsInvalid(int& nDoSOut) const 82 : { 83 48062 : if (IsInvalid()) { 84 83 : nDoSOut = nDoS; 85 83 : return true; 86 : } 87 : return false; 88 : } 89 99 : bool CorruptionPossible() const 90 : { 91 99 : return corruptionPossible; 92 : } 93 64 : unsigned int GetRejectCode() const { return chRejectCode; } 94 24597 : std::string GetRejectReason() const { return strRejectReason; } 95 10517 : std::string GetDebugMessage() const { return strDebugMessage; } 96 2 : int GetDoSScore() const { return nDoS; } 97 : }; 98 : 99 : #endif // PIVX_CONSENSUS_VALIDATION_H