You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validation.h 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2015 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. #ifndef BITCOIN_CONSENSUS_VALIDATION_H
  6. #define BITCOIN_CONSENSUS_VALIDATION_H
  7. #include <string>
  8. /** "reject" message codes */
  9. static const unsigned char REJECT_MALFORMED = 0x01;
  10. static const unsigned char REJECT_INVALID = 0x10;
  11. static const unsigned char REJECT_OBSOLETE = 0x11;
  12. static const unsigned char REJECT_DUPLICATE = 0x12;
  13. static const unsigned char REJECT_NONSTANDARD = 0x40;
  14. static const unsigned char REJECT_DUST = 0x41;
  15. static const unsigned char REJECT_INSUFFICIENTFEE = 0x42;
  16. static const unsigned char REJECT_CHECKPOINT = 0x43;
  17. /** Capture information about block/transaction validation */
  18. class CValidationState {
  19. private:
  20. enum mode_state {
  21. MODE_VALID, //! everything ok
  22. MODE_INVALID, //! network rule violation (DoS value may be set)
  23. MODE_ERROR, //! run-time error
  24. } mode;
  25. int nDoS;
  26. std::string strRejectReason;
  27. unsigned int chRejectCode;
  28. bool corruptionPossible;
  29. std::string strDebugMessage;
  30. public:
  31. CValidationState() : mode(MODE_VALID), nDoS(0), chRejectCode(0), corruptionPossible(false) {}
  32. bool DoS(int level, bool ret = false,
  33. unsigned int chRejectCodeIn=0, const std::string &strRejectReasonIn="",
  34. bool corruptionIn=false,
  35. const std::string &strDebugMessageIn="") {
  36. chRejectCode = chRejectCodeIn;
  37. strRejectReason = strRejectReasonIn;
  38. corruptionPossible = corruptionIn;
  39. strDebugMessage = strDebugMessageIn;
  40. if (mode == MODE_ERROR)
  41. return ret;
  42. nDoS += level;
  43. mode = MODE_INVALID;
  44. return ret;
  45. }
  46. bool Invalid(bool ret = false,
  47. unsigned int _chRejectCode=0, const std::string &_strRejectReason="",
  48. const std::string &_strDebugMessage="") {
  49. return DoS(0, ret, _chRejectCode, _strRejectReason, false, _strDebugMessage);
  50. }
  51. bool Error(const 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 int GetRejectCode() const { return chRejectCode; }
  77. std::string GetRejectReason() const { return strRejectReason; }
  78. std::string GetDebugMessage() const { return strDebugMessage; }
  79. };
  80. #endif // BITCOIN_CONSENSUS_VALIDATION_H