Bitcoin Core  0.19.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 
18 enum class TxValidationResult {
20  TX_CONSENSUS,
21 
32 
45 };
46 
55 
70 };
71 
72 
73 
78 private:
79  enum mode_state {
83  } m_mode;
84  std::string m_reject_reason;
85  std::string m_debug_message;
86 protected:
87  void Invalid(const std::string &reject_reason="",
88  const std::string &debug_message="")
89  {
90  m_reject_reason = reject_reason;
91  m_debug_message = debug_message;
92  if (m_mode != MODE_ERROR) m_mode = MODE_INVALID;
93  }
94 public:
95  // ValidationState is abstract. Have a pure virtual destructor.
96  virtual ~ValidationState() = 0;
97 
98  ValidationState() : m_mode(MODE_VALID) {}
99  bool Error(const std::string& reject_reason)
100  {
101  if (m_mode == MODE_VALID)
102  m_reject_reason = reject_reason;
103  m_mode = MODE_ERROR;
104  return false;
105  }
106  bool IsValid() const { return m_mode == MODE_VALID; }
107  bool IsInvalid() const { return m_mode == MODE_INVALID; }
108  bool IsError() const { return m_mode == MODE_ERROR; }
109  std::string GetRejectReason() const { return m_reject_reason; }
110  std::string GetDebugMessage() const { return m_debug_message; }
111 };
112 
114 
116 private:
118 public:
120  const std::string &reject_reason="",
121  const std::string &debug_message="")
122  {
123  m_result = result;
124  ValidationState::Invalid(reject_reason, debug_message);
125  return false;
126  }
127  TxValidationResult GetResult() const { return m_result; }
128 };
129 
131 private:
133 public:
135  const std::string &reject_reason="",
136  const std::string &debug_message="") {
137  m_result = result;
138  ValidationState::Invalid(reject_reason, debug_message);
139  return false;
140  }
141  BlockValidationResult GetResult() const { return m_result; }
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
static const int SERIALIZE_TRANSACTION_NO_WITNESS
Definition: transaction.h:15
invalid by consensus rules
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
bool IsValid() const
Definition: validation.h:106
Base class for capturing information about block/transaction validation.
Definition: validation.h:77
Definition: block.h:72
We don&#39;t have the previous block the checked one is built on.
invalid proof of work or time too old
bool IsInvalid() const
Definition: validation.h:107
CScriptWitness scriptWitness
Only serialized through CTransaction.
Definition: transaction.h:69
BlockValidationResult
A "reason" why a block was invalid, suitable for determining whether the provider of the block should...
Definition: validation.h:52
transaction was missing some of its inputs
std::vector< std::vector< unsigned char > > stack
Definition: script.h:574
std::string GetRejectReason() const
Definition: validation.h:109
violated mempool&#39;s fee/size/descendant/RBF/etc limits
bool IsError() const
Definition: validation.h:108
transaction spends a coinbase too early, or violates locktime/sequence locks
initial value. Tx has not yet been rejected
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1004
void Invalid(const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:87
BlockValidationResult GetResult() const
Definition: validation.h:141
initial value. Block has not yet been rejected
Invalid by a change to consensus rules more recent than SegWit.
Transaction might be missing a witness, have a witness prior to SegWit activation, or witness may have been malleated (which includes non-standard witnesses).
this block was cached as being invalid and we didn&#39;t store the reason why
An input of a transaction.
Definition: transaction.h:63
static int64_t GetTransactionInputWeight(const CTxIn &txin)
Definition: validation.h:156
the block failed to meet one of our checkpoints
TxValidationResult GetResult() const
Definition: validation.h:127
bool Invalid(BlockValidationResult result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:134
std::string GetDebugMessage() const
Definition: validation.h:110
invalid by consensus rules (excluding any below reasons)
the block&#39;s data didn&#39;t match the data committed to by the PoW
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
A block this one builds on is invalid.
virtual ~ValidationState()=0
Definition: validation.h:113
bool Invalid(TxValidationResult result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:119
Invalid by a change to consensus rules more recent than SegWit.
network rule violation (DoS value may be set)
Definition: validation.h:81
block timestamp was > 2 hours in the future (or our clock is bad)
std::string m_debug_message
Definition: validation.h:85
std::string m_reject_reason
Definition: validation.h:84
bool Error(const std::string &reject_reason)
Definition: validation.h:99
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
static int64_t GetBlockWeight(const CBlock &block)
Definition: validation.h:152
Tx already in mempool or conflicts with a tx in the chain (if it conflicts with another tx in mempool...
didn&#39;t meet our local policy rules
TxValidationResult
A "reason" why a transaction was invalid, suitable for determining whether the provider of the transa...
Definition: validation.h:18