Bitcoin Core  0.18.99
P2P Digital Currency
validation.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 #include <version.h>
11 #include <consensus/consensus.h>
12 #include <primitives/transaction.h>
13 #include <primitives/block.h>
14 
16 static const unsigned char REJECT_MALFORMED = 0x01;
17 static const unsigned char REJECT_INVALID = 0x10;
18 static const unsigned char REJECT_OBSOLETE = 0x11;
19 static const unsigned char REJECT_DUPLICATE = 0x12;
20 static const unsigned char REJECT_NONSTANDARD = 0x40;
21 // static const unsigned char REJECT_DUST = 0x41; // part of BIP 61
22 static const unsigned char REJECT_INSUFFICIENTFEE = 0x42;
23 static const unsigned char REJECT_CHECKPOINT = 0x43;
24 
31  // txn and blocks:
32  NONE,
33  CONSENSUS,
34 
42  // Only blocks (or headers):
50  // Only loose txn:
54 
68 };
69 
71 {
72  return r == ValidationInvalidReason::NONE ||
81 }
82 
84 {
85  return r == ValidationInvalidReason::NONE ||
95 }
96 
99 private:
100  enum mode_state {
104  } mode;
106  std::string strRejectReason;
107  unsigned int chRejectCode;
108  std::string strDebugMessage;
109 public:
110  CValidationState() : mode(MODE_VALID), m_reason(ValidationInvalidReason::NONE), chRejectCode(0) {}
111  bool Invalid(ValidationInvalidReason reasonIn, bool ret = false,
112  unsigned int chRejectCodeIn=0, const std::string &strRejectReasonIn="",
113  const std::string &strDebugMessageIn="") {
114  m_reason = reasonIn;
115  chRejectCode = chRejectCodeIn;
116  strRejectReason = strRejectReasonIn;
117  strDebugMessage = strDebugMessageIn;
118  if (mode == MODE_ERROR)
119  return ret;
120  mode = MODE_INVALID;
121  return ret;
122  }
123  bool Error(const std::string& strRejectReasonIn) {
124  if (mode == MODE_VALID)
125  strRejectReason = strRejectReasonIn;
126  mode = MODE_ERROR;
127  return false;
128  }
129  bool IsValid() const {
130  return mode == MODE_VALID;
131  }
132  bool IsInvalid() const {
133  return mode == MODE_INVALID;
134  }
135  bool IsError() const {
136  return mode == MODE_ERROR;
137  }
138  ValidationInvalidReason GetReason() const { return m_reason; }
139  unsigned int GetRejectCode() const { return chRejectCode; }
140  std::string GetRejectReason() const { return strRejectReason; }
141  std::string GetDebugMessage() const { return strDebugMessage; }
142 };
143 
144 // These implement the weight = (stripped_size * 4) + witness_size formula,
145 // using only serialization with and without witness data. As witness_size
146 // is equal to total_size - stripped_size, this formula is identical to:
147 // weight = (stripped_size * 3) + total_size.
148 static inline int64_t GetTransactionWeight(const CTransaction& tx)
149 {
151 }
152 static inline int64_t GetBlockWeight(const CBlock& block)
153 {
155 }
156 static inline int64_t GetTransactionInputWeight(const CTxIn& txin)
157 {
158  // scriptWitness size is added here because witnesses and txins are split up in segwit serialization.
160 }
161 
162 #endif // BITCOIN_CONSENSUS_VALIDATION_H
static int64_t GetTransactionWeight(const CTransaction &tx)
Definition: validation.h:148
invalid proof of work or time too old
invalid by consensus rules (excluding any below reasons)
static const int SERIALIZE_TRANSACTION_NO_WITNESS
Definition: transaction.h:15
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
unsigned int chRejectCode
Definition: validation.h:107
this object was cached as being invalid, but we don&#39;t know why
ValidationInvalidReason GetReason() const
Definition: validation.h:138
Definition: block.h:72
static const unsigned char REJECT_INSUFFICIENTFEE
Definition: validation.h:22
CScriptWitness scriptWitness
Only serialized through CTransaction.
Definition: transaction.h:69
ValidationInvalidReason m_reason
Definition: validation.h:105
bool IsValid() const
Definition: validation.h:129
network rule violation (DoS value may be set)
Definition: validation.h:102
std::vector< std::vector< unsigned char > > stack
Definition: script.h:574
ValidationInvalidReason
A "reason" why something was invalid, suitable for determining whether the provider of the object sho...
Definition: validation.h:30
static const unsigned char REJECT_INVALID
Definition: validation.h:17
static const unsigned char REJECT_OBSOLETE
Definition: validation.h:18
A block this one builds on is invalid.
static const unsigned char REJECT_CHECKPOINT
Definition: validation.h:23
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:992
bool IsInvalid() const
Definition: validation.h:132
We don&#39;t have the previous block the checked one is built on.
An input of a transaction.
Definition: transaction.h:63
static int64_t GetTransactionInputWeight(const CTxIn &txin)
Definition: validation.h:156
Tx already in mempool or conflicts with a tx in the chain (if it conflicts with another tx in mempool...
static const unsigned char REJECT_NONSTANDARD
Definition: validation.h:20
std::string GetRejectReason() const
Definition: validation.h:140
the block&#39;s data didn&#39;t match the data committed to by the PoW
bool IsError() const
Definition: validation.h:135
transaction spends a coinbase too early, or violates locktime/sequence locks
the block failed to meet one of our checkpoints
static const unsigned char REJECT_DUPLICATE
Definition: validation.h:19
bool Error(const std::string &strRejectReasonIn)
Definition: validation.h:123
Capture information about block/transaction validation.
Definition: validation.h:98
not actually invalid
bool Invalid(ValidationInvalidReason reasonIn, bool ret=false, unsigned int chRejectCodeIn=0, const std::string &strRejectReasonIn="", const std::string &strDebugMessageIn="")
Definition: validation.h:111
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
Invalid by a change to consensus rules more recent than SegWit.
didn&#39;t meet our local policy rules
a transaction was missing some of its inputs
std::string GetDebugMessage() const
Definition: validation.h:141
unsigned int GetRejectCode() const
Definition: validation.h:139
violated mempool&#39;s fee/size/descendant/RBF/etc limits
std::string strRejectReason
Definition: validation.h:106
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
Transaction might be missing a witness, have a witness prior to SegWit activation, or witness may have been malleated (which includes non-standard witnesses).
std::string strDebugMessage
Definition: validation.h:108
static const unsigned char REJECT_MALFORMED
"reject" message codes
Definition: validation.h:16
static int64_t GetBlockWeight(const CBlock &block)
Definition: validation.h:152
bool IsTransactionReason(ValidationInvalidReason r)
Definition: validation.h:70
block timestamp was > 2 hours in the future (or our clock is bad)
bool IsBlockReason(ValidationInvalidReason r)
Definition: validation.h:83