Raven Core  3.0.0
P2P Digital Currency
validation.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven 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 RAVEN_CONSENSUS_VALIDATION_H
8 #define RAVEN_CONSENSUS_VALIDATION_H
9 
10 #include <string>
11 #include "version.h"
12 #include "consensus/consensus.h"
13 #include "primitives/transaction.h"
14 #include "primitives/block.h"
15 
17 static const unsigned char REJECT_MALFORMED = 0x01;
18 static const unsigned char REJECT_INVALID = 0x10;
19 static const unsigned char REJECT_OBSOLETE = 0x11;
20 static const unsigned char REJECT_DUPLICATE = 0x12;
21 static const unsigned char REJECT_NONSTANDARD = 0x40;
22 // static const unsigned char REJECT_DUST = 0x41; // part of BIP 61
23 static const unsigned char REJECT_INSUFFICIENTFEE = 0x42;
24 static const unsigned char REJECT_CHECKPOINT = 0x43;
26 static const unsigned char REJECT_MAXREORGDEPTH = 0x44;
31 private:
32  enum mode_state {
36  } mode;
37  int nDoS;
38  std::string strRejectReason;
39  unsigned int chRejectCode;
41  std::string strDebugMessage;
42 public:
43  CValidationState() : mode(MODE_VALID), nDoS(0), chRejectCode(0), corruptionPossible(false) {}
44  bool DoS(int level, bool ret = false,
45  unsigned int chRejectCodeIn=0, const std::string &strRejectReasonIn="",
46  bool corruptionIn=false,
47  const std::string &strDebugMessageIn="") {
48  chRejectCode = chRejectCodeIn;
49  strRejectReason = strRejectReasonIn;
50  corruptionPossible = corruptionIn;
51  strDebugMessage = strDebugMessageIn;
52  if (mode == MODE_ERROR)
53  return ret;
54  nDoS += level;
55  mode = MODE_INVALID;
56  return ret;
57  }
58  bool Invalid(bool ret = false,
59  unsigned int _chRejectCode=0, const std::string &_strRejectReason="",
60  const std::string &_strDebugMessage="") {
61  return DoS(0, ret, _chRejectCode, _strRejectReason, false, _strDebugMessage);
62  }
63  bool Error(const std::string& strRejectReasonIn) {
64  if (mode == MODE_VALID)
65  strRejectReason = strRejectReasonIn;
66  mode = MODE_ERROR;
67  return false;
68  }
69  bool IsValid() const {
70  return mode == MODE_VALID;
71  }
72  bool IsInvalid() const {
73  return mode == MODE_INVALID;
74  }
75  bool IsError() const {
76  return mode == MODE_ERROR;
77  }
78  bool IsInvalid(int &nDoSOut) const {
79  if (IsInvalid()) {
80  nDoSOut = nDoS;
81  return true;
82  }
83  return false;
84  }
85  bool CorruptionPossible() const {
86  return corruptionPossible;
87  }
89  corruptionPossible = true;
90  }
91  unsigned int GetRejectCode() const { return chRejectCode; }
92  std::string GetRejectReason() const { return strRejectReason; }
93  std::string GetDebugMessage() const { return strDebugMessage; }
94 };
95 
96 // These implement the weight = (stripped_size * 4) + witness_size formula,
97 // using only serialization with and without witness data. As witness_size
98 // is equal to total_size - stripped_size, this formula is identical to:
99 // weight = (stripped_size * 3) + total_size.
100 static inline int64_t GetTransactionWeight(const CTransaction& tx)
101 {
102  return ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
103 }
104 static inline int64_t GetBlockWeight(const CBlock& block)
105 {
106  return ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION);
107 }
108 
109 #endif // RAVEN_CONSENSUS_VALIDATION_H
unsigned int chRejectCode
Definition: validation.h:39
Definition: block.h:73
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:967
enum CValidationState::mode_state mode
bool IsValid() const
Definition: validation.h:69
network rule violation (DoS value may be set)
Definition: validation.h:34
bool DoS(int level, bool ret=false, unsigned int chRejectCodeIn=0, const std::string &strRejectReasonIn="", bool corruptionIn=false, const std::string &strDebugMessageIn="")
Definition: validation.h:44
bool IsInvalid(int &nDoSOut) const
Definition: validation.h:78
bool corruptionPossible
Definition: validation.h:40
void SetCorruptionPossible()
Definition: validation.h:88
bool IsInvalid() const
Definition: validation.h:72
std::string GetRejectReason() const
Definition: validation.h:92
bool IsError() const
Definition: validation.h:75
bool Error(const std::string &strRejectReasonIn)
Definition: validation.h:63
RVN END.
Definition: validation.h:30
std::string GetDebugMessage() const
Definition: validation.h:93
unsigned int GetRejectCode() const
Definition: validation.h:91
bool CorruptionPossible() const
Definition: validation.h:85
std::string strRejectReason
Definition: validation.h:38
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
std::string strDebugMessage
Definition: validation.h:41
bool Invalid(bool ret=false, unsigned int _chRejectCode=0, const std::string &_strRejectReason="", const std::string &_strDebugMessage="")
Definition: validation.h:58