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 4428105 : CValidationState() : mode(MODE_VALID), nDoS(0), chRejectCode(0), corruptionPossible(false) {} 39 1584 : 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 1584 : chRejectCode = chRejectCodeIn; 46 1442 : strRejectReason = strRejectReasonIn; 47 1584 : corruptionPossible = corruptionIn; 48 1584 : strDebugMessage = strDebugMessageIn; 49 1584 : if (mode == MODE_ERROR) 50 : return ret; 51 1584 : nDoS += level; 52 1584 : mode = MODE_INVALID; 53 1584 : return ret; 54 : } 55 182 : bool Invalid(bool ret = false, 56 : unsigned int _chRejectCode = 0, 57 : const std::string& _strRejectReason = "", 58 : const std::string& _strDebugMessage = "") 59 : { 60 389 : return DoS(0, ret, _chRejectCode, _strRejectReason, false, _strDebugMessage); 61 : } 62 10316 : bool Error(std::string strRejectReasonIn = "") 63 : { 64 10316 : if (mode == MODE_VALID) 65 10316 : strRejectReason = strRejectReasonIn; 66 10316 : mode = MODE_ERROR; 67 10316 : return false; 68 : } 69 66517 : bool IsValid() const 70 : { 71 66517 : return mode == MODE_VALID; 72 : } 73 49068 : bool IsInvalid() const 74 : { 75 49068 : return mode == MODE_INVALID; 76 : } 77 12857 : bool IsError() const 78 : { 79 12857 : return mode == MODE_ERROR; 80 : } 81 48942 : bool IsInvalid(int& nDoSOut) const 82 : { 83 48942 : if (IsInvalid()) { 84 80 : nDoSOut = nDoS; 85 80 : 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 24530 : std::string GetRejectReason() const { return strRejectReason; } 95 10536 : std::string GetDebugMessage() const { return strDebugMessage; } 96 2 : int GetDoSScore() const { return nDoS; } 97 : }; 98 : 99 : #endif // PIVX_CONSENSUS_VALIDATION_H