da29ecbc |
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 | /** Capture information about block/transaction validation */ |
22 | class CValidationState { |
23 | private: |
24 | enum mode_state { |
25 | MODE_VALID, //! everything ok |
26 | MODE_INVALID, //! network rule violation (DoS value may be set) |
27 | MODE_ERROR, //! run-time error |
28 | } mode; |
29 | int nDoS; |
30 | std::string strRejectReason; |
31 | unsigned char chRejectCode; |
32 | bool corruptionPossible; |
33 | public: |
34 | CValidationState() : mode(MODE_VALID), nDoS(0), chRejectCode(0), corruptionPossible(false) {} |
35 | bool DoS(int level, bool ret = false, |
36 | unsigned char chRejectCodeIn=0, std::string strRejectReasonIn="", |
37 | bool corruptionIn=false) { |
38 | chRejectCode = chRejectCodeIn; |
39 | strRejectReason = strRejectReasonIn; |
40 | corruptionPossible = corruptionIn; |
41 | if (mode == MODE_ERROR) |
42 | return ret; |
43 | nDoS += level; |
44 | mode = MODE_INVALID; |
45 | return ret; |
46 | } |
47 | bool Invalid(bool ret = false, |
48 | unsigned char _chRejectCode=0, std::string _strRejectReason="") { |
49 | return DoS(0, ret, _chRejectCode, _strRejectReason); |
50 | } |
51 | bool Error(std::string strRejectReasonIn="") { |
52 | if (mode == MODE_VALID) |
53 | strRejectReason = strRejectReasonIn; |
54 | mode = MODE_ERROR; |
55 | return false; |
56 | } |
57 | bool IsValid() const { |
58 | return mode == MODE_VALID; |
59 | } |
60 | bool IsInvalid() const { |
61 | return mode == MODE_INVALID; |
62 | } |
63 | bool IsError() const { |
64 | return mode == MODE_ERROR; |
65 | } |
66 | bool IsInvalid(int &nDoSOut) const { |
67 | if (IsInvalid()) { |
68 | nDoSOut = nDoS; |
69 | return true; |
70 | } |
71 | return false; |
72 | } |
73 | bool CorruptionPossible() const { |
74 | return corruptionPossible; |
75 | } |
76 | unsigned char GetRejectCode() const { return chRejectCode; } |
77 | std::string GetRejectReason() const { return strRejectReason; } |
78 | }; |
79 | |
80 | #endif // BITCOIN_CONSENSUS_VALIDATION_H |