Bitcoin Core  0.20.99
P2P Digital Currency
validation.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 constexpr int NO_WITNESS_COMMITMENT{-1};
17 
19 static constexpr size_t MINIMUM_WITNESS_COMMITMENT{38};
20 
24 enum class TxValidationResult {
25  TX_RESULT_UNSET = 0,
26  TX_CONSENSUS,
27 
39 
56 };
57 
64  BLOCK_RESULT_UNSET = 0,
66 
81 };
82 
83 
84 
88 template <typename Result>
90 {
91 private:
92  enum class ModeState {
93  M_VALID,
94  M_INVALID,
95  M_ERROR,
96  } m_mode{ModeState::M_VALID};
97  Result m_result{};
98  std::string m_reject_reason;
99  std::string m_debug_message;
100 
101 public:
102  bool Invalid(Result result,
103  const std::string& reject_reason = "",
104  const std::string& debug_message = "")
105  {
106  m_result = result;
107  m_reject_reason = reject_reason;
108  m_debug_message = debug_message;
109  if (m_mode != ModeState::M_ERROR) m_mode = ModeState::M_INVALID;
110  return false;
111  }
112  bool Error(const std::string& reject_reason)
113  {
114  if (m_mode == ModeState::M_VALID)
115  m_reject_reason = reject_reason;
116  m_mode = ModeState::M_ERROR;
117  return false;
118  }
119  bool IsValid() const { return m_mode == ModeState::M_VALID; }
120  bool IsInvalid() const { return m_mode == ModeState::M_INVALID; }
121  bool IsError() const { return m_mode == ModeState::M_ERROR; }
122  Result GetResult() const { return m_result; }
123  std::string GetRejectReason() const { return m_reject_reason; }
124  std::string GetDebugMessage() const { return m_debug_message; }
125  std::string ToString() const
126  {
127  if (IsValid()) {
128  return "Valid";
129  }
130 
131  if (!m_debug_message.empty()) {
132  return m_reject_reason + ", " + m_debug_message;
133  }
134 
135  return m_reject_reason;
136  }
137 };
138 
139 class TxValidationState : public ValidationState<TxValidationResult> {};
140 class BlockValidationState : public ValidationState<BlockValidationResult> {};
141 
142 // These implement the weight = (stripped_size * 4) + witness_size formula,
143 // using only serialization with and without witness data. As witness_size
144 // is equal to total_size - stripped_size, this formula is identical to:
145 // weight = (stripped_size * 3) + total_size.
146 static inline int64_t GetTransactionWeight(const CTransaction& tx)
147 {
149 }
150 static inline int64_t GetBlockWeight(const CBlock& block)
151 {
153 }
154 static inline int64_t GetTransactionInputWeight(const CTxIn& txin)
155 {
156  // scriptWitness size is added here because witnesses and txins are split up in segwit serialization.
158 }
159 
161 inline int GetWitnessCommitmentIndex(const CBlock& block)
162 {
163  int commitpos = NO_WITNESS_COMMITMENT;
164  if (!block.vtx.empty()) {
165  for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
166  const CTxOut& vout = block.vtx[0]->vout[o];
168  vout.scriptPubKey[0] == OP_RETURN &&
169  vout.scriptPubKey[1] == 0x24 &&
170  vout.scriptPubKey[2] == 0xaa &&
171  vout.scriptPubKey[3] == 0x21 &&
172  vout.scriptPubKey[4] == 0xa9 &&
173  vout.scriptPubKey[5] == 0xed) {
174  commitpos = o;
175  }
176  }
177  }
178  return commitpos;
179 }
180 
181 #endif // BITCOIN_CONSENSUS_VALIDATION_H
static int64_t GetTransactionWeight(const CTransaction &tx)
Definition: validation.h:146
static const int SERIALIZE_TRANSACTION_NO_WITNESS
A flag that is ORed into the protocol version to designate that a transaction should be (un)serialize...
Definition: transaction.h:23
invalid by consensus rules
bool Error(const std::string &reject_reason)
Definition: validation.h:112
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
std::string GetDebugMessage() const
Definition: validation.h:124
CScript scriptPubKey
Definition: transaction.h:132
Template for capturing information about block/transaction validation.
Definition: validation.h:89
Definition: block.h:62
We don&#39;t have the previous block the checked one is built on.
invalid proof of work or time too old
CScriptWitness scriptWitness
Only serialized through CTransaction.
Definition: transaction.h:71
BlockValidationResult
A "reason" why a block was invalid, suitable for determining whether the provider of the block should...
Definition: validation.h:63
transaction was missing some of its inputs
static constexpr size_t MINIMUM_WITNESS_COMMITMENT
Minimum size of a witness commitment structure.
Definition: validation.h:19
std::vector< std::vector< unsigned char > > stack
Definition: script.h:560
violated mempool&#39;s fee/size/descendant/RBF/etc limits
inputs (covered by txid) failed policy rules
transaction spends a coinbase too early, or violates locktime/sequence locks
static constexpr int NO_WITNESS_COMMITMENT
Index marker for when no witness commitment is present in a coinbase transaction. ...
Definition: validation.h:16
initial value. Tx has not yet been rejected
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1116
bool IsValid() const
Definition: validation.h:119
initial value. Block has not yet been rejected
Invalid by a change to consensus rules more recent than SegWit.
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:102
Transaction might have a witness prior to SegWit activation, or witness may have been malleated (whic...
this block was cached as being invalid and we didn&#39;t store the reason why
An input of a transaction.
Definition: transaction.h:65
static int64_t GetTransactionInputWeight(const CTxIn &txin)
Definition: validation.h:154
std::string ToString() const
Definition: validation.h:125
the block failed to meet one of our checkpoints
Result GetResult() const
Definition: validation.h:122
An output of a transaction.
Definition: transaction.h:128
std::string m_debug_message
Definition: validation.h:99
Transaction is missing a witness.
invalid by consensus rules (excluding any below reasons)
std::vector< CTransactionRef > vtx
Definition: block.h:66
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.
bool IsInvalid() const
Definition: validation.h:120
Invalid by a change to consensus rules more recent than SegWit.
size_type size() const
Definition: prevector.h:282
block timestamp was > 2 hours in the future (or our clock is bad)
std::string m_reject_reason
Definition: validation.h:98
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:259
std::string GetRejectReason() const
Definition: validation.h:123
int GetWitnessCommitmentIndex(const CBlock &block)
Compute at which vout of the block&#39;s coinbase transaction the witness commitment occurs, or -1 if not found.
Definition: validation.h:161
static int64_t GetBlockWeight(const CBlock &block)
Definition: validation.h:150
Tx already in mempool or conflicts with a tx in the chain (if it conflicts with another tx in mempool...
otherwise didn&#39;t meet our local policy rules
bool IsError() const
Definition: validation.h:121
TxValidationResult
A "reason" why a transaction was invalid, suitable for determining whether the provider of the transa...
Definition: validation.h:24