Fabcoin Core  0.16.2
P2P Digital Currency
validation.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 FABCOIN_CONSENSUS_VALIDATION_H
7 #define FABCOIN_CONSENSUS_VALIDATION_H
8 
9 #include <string>
10 #include <version.h>
11 #include <consensus/consensus.h>
12 #include <consensus/params.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;
25 
28 private:
29  enum mode_state {
33  } mode;
34  int nDoS;
35  std::string strRejectReason;
36  unsigned int chRejectCode;
38  std::string strDebugMessage;
39 public:
40  CValidationState() : mode(MODE_VALID), nDoS(0), chRejectCode(0), corruptionPossible(false) {}
41  bool DoS(int level, bool ret = false,
42  unsigned int chRejectCodeIn=0, const std::string &strRejectReasonIn="",
43  bool corruptionIn=false,
44  const std::string &strDebugMessageIn="") {
45  chRejectCode = chRejectCodeIn;
46  strRejectReason = strRejectReasonIn;
47  corruptionPossible = corruptionIn;
48  strDebugMessage = strDebugMessageIn;
49  if (mode == MODE_ERROR)
50  return ret;
51  nDoS += level;
52  mode = MODE_INVALID;
53  return ret;
54  }
55  bool Invalid(bool ret = false,
56  unsigned int _chRejectCode=0, const std::string &_strRejectReason="",
57  const std::string &_strDebugMessage="") {
58  return DoS(0, ret, _chRejectCode, _strRejectReason, false, _strDebugMessage);
59  }
60  bool Error(const std::string& strRejectReasonIn) {
61  if (mode == MODE_VALID)
62  strRejectReason = strRejectReasonIn;
63  mode = MODE_ERROR;
64  return false;
65  }
66  bool IsValid() const {
67  return mode == MODE_VALID;
68  }
69  bool IsInvalid() const {
70  return mode == MODE_INVALID;
71  }
72  bool IsError() const {
73  return mode == MODE_ERROR;
74  }
75  bool IsInvalid(int &nDoSOut) const {
76  if (IsInvalid()) {
77  nDoSOut = nDoS;
78  return true;
79  }
80  return false;
81  }
82  bool CorruptionPossible() const {
83  return corruptionPossible;
84  }
86  corruptionPossible = true;
87  }
88  unsigned int GetRejectCode() const { return chRejectCode; }
89  std::string GetRejectReason() const { return strRejectReason; }
90  std::string GetDebugMessage() const { return strDebugMessage; }
91 };
92 
93 /*
94 static inline int64_t GetTransactionWeight(const CTransaction& tx)
95 {
96  return ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR -1) + ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
97 }
98 */
99 
100 static inline int64_t GetBlockWeight(const CBlock& block, const Consensus::Params& params)
101 {
102  // This implements the weight = (stripped_size * 4) + witness_size formula,
103  // using only serialization with and without witness data. As witness_size
104  // is equal to total_size - stripped_size, this formula is identical to:
105  // weight = (stripped_size * 3) + total_size.
106 
107  return ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS ) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION );
108 }
109 
110 #endif // FABCOIN_CONSENSUS_VALIDATION_H
unsigned int chRejectCode
Definition: validation.h:36
bool IsError() const
Definition: validation.h:72
Definition: block.h:155
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:989
enum CValidationState::mode_state mode
bool CorruptionPossible() const
Definition: validation.h:82
network rule violation (DoS value may be set)
Definition: validation.h:31
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:41
bool corruptionPossible
Definition: validation.h:37
void SetCorruptionPossible()
Definition: validation.h:85
bool IsValid() const
Definition: validation.h:66
Parameters that influence chain consensus.
Definition: params.h:39
bool IsInvalid(int &nDoSOut) const
Definition: validation.h:75
bool IsInvalid() const
Definition: validation.h:69
bool Error(const std::string &strRejectReasonIn)
Definition: validation.h:60
std::string GetRejectReason() const
Definition: validation.h:89
unsigned int GetRejectCode() const
Definition: validation.h:88
Capture information about block/transaction validation.
Definition: validation.h:27
std::string GetDebugMessage() const
Definition: validation.h:90
std::string strRejectReason
Definition: validation.h:35
std::string strDebugMessage
Definition: validation.h:38
bool Invalid(bool ret=false, unsigned int _chRejectCode=0, const std::string &_strRejectReason="", const std::string &_strDebugMessage="")
Definition: validation.h:55