@@ -22,6 +22,35 @@ static const unsigned char REJECT_NONSTANDARD = 0x40;
2222static const unsigned char REJECT_INSUFFICIENTFEE = 0x42 ;
2323static const unsigned char REJECT_CHECKPOINT = 0x43 ;
2424
25+ /* * Reject codes greater or equal to this can be returned by AcceptToMemPool
26+ * for transactions, to signal internal conditions. They cannot and should not
27+ * be sent over the P2P network.
28+ *
29+ * These error codes are not consensus, but consensus changes should avoid using them
30+ * unnecessarily so as not to cause needless churn in core-based clients.
31+ */
32+ static const unsigned int REJECT_INTERNAL = 0x100 ;
33+ /* * Too high fee. Can not be triggered by P2P transactions */
34+ static const unsigned int REJECT_HIGHFEE = 0x100 ;
35+
36+ enum class DoS_SEVERITY : int {
37+ NONE = 0 ,
38+ LOW = 1 ,
39+ MEDIUM = 10 ,
40+ ELEVATED = 20 ,
41+ HIGH = 50 ,
42+ CRITICAL = 100 ,
43+ };
44+ inline int ToBanScore (const DoS_SEVERITY x) {
45+ return (int ) x;
46+ }
47+ inline bool operator >(const DoS_SEVERITY lhs, const DoS_SEVERITY rhs) {
48+ return ((int ) lhs) > ((int ) rhs);
49+ }
50+ enum class CORRUPTION_POSSIBLE : bool {
51+ False = 0 ,
52+ True = 1
53+ };
2554/* * Capture information about block/transaction validation */
2655class CValidationState {
2756private:
@@ -30,37 +59,71 @@ class CValidationState {
3059 MODE_INVALID, // !< network rule violation (DoS value may be set)
3160 MODE_ERROR, // !< run-time error
3261 } mode;
33- int nDoS;
62+ DoS_SEVERITY nDoS;
3463 std::string strRejectReason;
3564 unsigned int chRejectCode;
36- bool corruptionPossible;
65+ CORRUPTION_POSSIBLE corruptionPossible;
3766 std::string strDebugMessage;
38- public:
39- CValidationState () : mode(MODE_VALID), nDoS(0 ), chRejectCode(0 ), corruptionPossible(false ) {}
40- bool DoS (int level, bool ret = false ,
67+ void DoS (DoS_SEVERITY level,
4168 unsigned int chRejectCodeIn=0 , const std::string &strRejectReasonIn=" " ,
42- bool corruptionIn=false ,
69+ CORRUPTION_POSSIBLE corruptionIn=CORRUPTION_POSSIBLE::False ,
4370 const std::string &strDebugMessageIn=" " ) {
4471 chRejectCode = chRejectCodeIn;
4572 strRejectReason = strRejectReasonIn;
4673 corruptionPossible = corruptionIn;
4774 strDebugMessage = strDebugMessageIn;
4875 if (mode == MODE_ERROR)
49- return ret ;
50- nDoS += level;
76+ return ;
77+ nDoS = (DoS_SEVERITY) ((( unsigned int ) nDoS) + (( unsigned int ) level)) ;
5178 mode = MODE_INVALID;
52- return ret;
5379 }
54- bool Invalid (bool ret = false ,
55- unsigned int _chRejectCode=0 , const std::string &_strRejectReason=" " ,
80+ public:
81+ CValidationState () : mode(MODE_VALID), nDoS(DoS_SEVERITY::NONE), chRejectCode(0 ), corruptionPossible(CORRUPTION_POSSIBLE::False) {}
82+ void BadBlockHeader (const std::string &_strRejectReason,
83+ const std::string &_strDebugMessage=" " , DoS_SEVERITY level=DoS_SEVERITY::CRITICAL, unsigned int _chRejectCode=REJECT_INVALID) {
84+ DoS (level, _chRejectCode, _strRejectReason, CORRUPTION_POSSIBLE::False, _strDebugMessage);
85+ }
86+ void CorruptBlockHeader (const std::string &_strRejectReason,
87+ const std::string &_strDebugMessage=" " , DoS_SEVERITY level=DoS_SEVERITY::CRITICAL) {
88+ DoS (level, REJECT_INVALID, _strRejectReason, CORRUPTION_POSSIBLE::True, _strDebugMessage);
89+ }
90+ void ForkingBlockHeaderDisallowed () {
91+ DoS (DoS_SEVERITY::CRITICAL, REJECT_CHECKPOINT, " bad-fork-prior-to-checkpoint" );
92+ }
93+ void BadBlock (const std::string &_strRejectReason,
94+ const std::string &_strDebugMessage=" " , DoS_SEVERITY level=DoS_SEVERITY::CRITICAL) {
95+ DoS (level, REJECT_INVALID, _strRejectReason, CORRUPTION_POSSIBLE::False, _strDebugMessage);
96+ }
97+ void CorruptBlock (const std::string &_strRejectReason,
98+ const std::string &_strDebugMessage=" " , DoS_SEVERITY level=DoS_SEVERITY::CRITICAL) {
99+ DoS (level, REJECT_INVALID, _strRejectReason, CORRUPTION_POSSIBLE::True, _strDebugMessage);
100+ }
101+ void BadTx (const std::string &_strRejectReason,
102+ const std::string &_strDebugMessage=" " , DoS_SEVERITY level=DoS_SEVERITY::CRITICAL, unsigned int _chRejectCode=REJECT_INVALID) {
103+ DoS (level, _chRejectCode, _strRejectReason, CORRUPTION_POSSIBLE::False, _strDebugMessage);
104+ }
105+ void CorruptTx (const std::string &_strRejectReason,
106+ const std::string &_strDebugMessage=" " , DoS_SEVERITY level=DoS_SEVERITY::CRITICAL) {
107+ DoS (level, REJECT_INVALID, _strRejectReason, CORRUPTION_POSSIBLE::True, _strDebugMessage);
108+ }
109+ void NonStandardTx (const std::string &_strRejectReason,
110+ const std::string &_strDebugMessage=" " , CORRUPTION_POSSIBLE corrupted=CORRUPTION_POSSIBLE::False,
111+ DoS_SEVERITY level=DoS_SEVERITY::NONE) {
112+ DoS (level, REJECT_NONSTANDARD, _strRejectReason, corrupted, _strDebugMessage);
113+ }
114+ void DuplicateData (const std::string &_strRejectReason,
115+ const std::string &_strDebugMessage=" " ) {
116+ DoS (DoS_SEVERITY::NONE, REJECT_DUPLICATE, _strRejectReason, CORRUPTION_POSSIBLE::False, _strDebugMessage);
117+ }
118+ void RejectFee (unsigned int _chRejectCode, const std::string &_strRejectReason,
56119 const std::string &_strDebugMessage=" " ) {
57- return DoS (0 , ret, _chRejectCode, _strRejectReason, false , _strDebugMessage);
120+ assert (_chRejectCode == REJECT_INSUFFICIENTFEE || _chRejectCode == REJECT_HIGHFEE);
121+ DoS (DoS_SEVERITY::NONE, _chRejectCode, _strRejectReason, CORRUPTION_POSSIBLE::False, _strDebugMessage);
58122 }
59- bool Error (const std::string& strRejectReasonIn) {
123+ void Error (const std::string& strRejectReasonIn) {
60124 if (mode == MODE_VALID)
61125 strRejectReason = strRejectReasonIn;
62126 mode = MODE_ERROR;
63- return false ;
64127 }
65128 bool IsValid () const {
66129 return mode == MODE_VALID;
@@ -71,22 +134,23 @@ class CValidationState {
71134 bool IsError () const {
72135 return mode == MODE_ERROR;
73136 }
74- bool IsInvalid (int &nDoSOut) const {
137+ bool IsInvalid (DoS_SEVERITY &nDoSOut) const {
75138 if (IsInvalid ()) {
76139 nDoSOut = nDoS;
77140 return true ;
78141 }
79142 return false ;
80143 }
81144 bool CorruptionPossible () const {
82- return corruptionPossible;
145+ return corruptionPossible != CORRUPTION_POSSIBLE::False ;
83146 }
84147 void SetCorruptionPossible () {
85- corruptionPossible = true ;
148+ corruptionPossible = CORRUPTION_POSSIBLE::True ;
86149 }
87150 unsigned int GetRejectCode () const { return chRejectCode; }
88151 std::string GetRejectReason () const { return strRejectReason; }
89152 std::string GetDebugMessage () const { return strDebugMessage; }
153+ void SetDebugMessage (const std::string& msg){ strDebugMessage = msg; }
90154};
91155
92156// These implement the weight = (stripped_size * 4) + witness_size formula,
0 commit comments