Bitcoin Core  24.99.0
P2P Digital Currency
validation.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 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,
57 };
58 
65  BLOCK_RESULT_UNSET = 0,
83 };
84 
85 
86 
90 template <typename Result>
92 {
93 private:
94  enum class ModeState {
95  M_VALID,
96  M_INVALID,
97  M_ERROR,
98  } m_mode{ModeState::M_VALID};
100  std::string m_reject_reason;
101  std::string m_debug_message;
102 
103 public:
104  bool Invalid(Result result,
105  const std::string& reject_reason = "",
106  const std::string& debug_message = "")
107  {
108  m_result = result;
109  m_reject_reason = reject_reason;
110  m_debug_message = debug_message;
111  if (m_mode != ModeState::M_ERROR) m_mode = ModeState::M_INVALID;
112  return false;
113  }
114  bool Error(const std::string& reject_reason)
115  {
116  if (m_mode == ModeState::M_VALID)
117  m_reject_reason = reject_reason;
118  m_mode = ModeState::M_ERROR;
119  return false;
120  }
121  bool IsValid() const { return m_mode == ModeState::M_VALID; }
122  bool IsInvalid() const { return m_mode == ModeState::M_INVALID; }
123  bool IsError() const { return m_mode == ModeState::M_ERROR; }
124  Result GetResult() const { return m_result; }
125  std::string GetRejectReason() const { return m_reject_reason; }
126  std::string GetDebugMessage() const { return m_debug_message; }
127  std::string ToString() const
128  {
129  if (IsValid()) {
130  return "Valid";
131  }
132 
133  if (!m_debug_message.empty()) {
134  return m_reject_reason + ", " + m_debug_message;
135  }
136 
137  return m_reject_reason;
138  }
139 };
140 
141 class TxValidationState : public ValidationState<TxValidationResult> {};
142 class BlockValidationState : public ValidationState<BlockValidationResult> {};
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 
163 inline int GetWitnessCommitmentIndex(const CBlock& block)
164 {
165  int commitpos = NO_WITNESS_COMMITMENT;
166  if (!block.vtx.empty()) {
167  for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
168  const CTxOut& vout = block.vtx[0]->vout[o];
170  vout.scriptPubKey[0] == OP_RETURN &&
171  vout.scriptPubKey[1] == 0x24 &&
172  vout.scriptPubKey[2] == 0xaa &&
173  vout.scriptPubKey[3] == 0x21 &&
174  vout.scriptPubKey[4] == 0xa9 &&
175  vout.scriptPubKey[5] == 0xed) {
176  commitpos = o;
177  }
178  }
179  }
180  return commitpos;
181 }
182 
183 #endif // BITCOIN_CONSENSUS_VALIDATION_H
Definition: block.h:69
std::vector< CTransactionRef > vtx
Definition: block.h:72
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:295
An input of a transaction.
Definition: transaction.h:75
CScriptWitness scriptWitness
Only serialized through CTransaction.
Definition: transaction.h:80
An output of a transaction.
Definition: transaction.h:158
CScript scriptPubKey
Definition: transaction.h:161
Template for capturing information about block/transaction validation.
Definition: validation.h:92
@ M_INVALID
network rule violation (DoS value may be set)
bool IsValid() const
Definition: validation.h:121
std::string GetRejectReason() const
Definition: validation.h:125
Result m_result
Definition: validation.h:99
std::string m_reject_reason
Definition: validation.h:100
std::string GetDebugMessage() const
Definition: validation.h:126
bool Error(const std::string &reject_reason)
Definition: validation.h:114
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:104
bool IsError() const
Definition: validation.h:123
Result GetResult() const
Definition: validation.h:124
std::string m_debug_message
Definition: validation.h:101
std::string ToString() const
Definition: validation.h:127
bool IsInvalid() const
Definition: validation.h:122
size_type size() const
Definition: prevector.h:284
static constexpr int NO_WITNESS_COMMITMENT
Index marker for when no witness commitment is present in a coinbase transaction.
Definition: validation.h:16
static constexpr size_t MINIMUM_WITNESS_COMMITMENT
Minimum size of a witness commitment structure.
Definition: validation.h:19
static int64_t GetTransactionInputWeight(const CTxIn &txin)
Definition: validation.h:156
static int64_t GetBlockWeight(const CBlock &block)
Definition: validation.h:152
static int64_t GetTransactionWeight(const CTransaction &tx)
Definition: validation.h:148
BlockValidationResult
A "reason" why a block was invalid, suitable for determining whether the provider of the block should...
Definition: validation.h:64
@ BLOCK_CHECKPOINT
the block failed to meet one of our checkpoints
@ BLOCK_RECENT_CONSENSUS_CHANGE
Invalid by a change to consensus rules more recent than SegWit.
@ BLOCK_HEADER_LOW_WORK
the block header may be on a too-little-work chain
@ BLOCK_INVALID_HEADER
invalid proof of work or time too old
@ BLOCK_CACHED_INVALID
this block was cached as being invalid and we didn't store the reason why
@ BLOCK_CONSENSUS
invalid by consensus rules (excluding any below reasons)
@ BLOCK_MISSING_PREV
We don't have the previous block the checked one is built on.
@ BLOCK_INVALID_PREV
A block this one builds on is invalid.
@ BLOCK_MUTATED
the block's data didn't match the data committed to by the PoW
@ BLOCK_TIME_FUTURE
block timestamp was > 2 hours in the future (or our clock is bad)
@ BLOCK_RESULT_UNSET
initial value. Block has not yet been rejected
int GetWitnessCommitmentIndex(const CBlock &block)
Compute at which vout of the block's coinbase transaction the witness commitment occurs,...
Definition: validation.h:163
TxValidationResult
A "reason" why a transaction was invalid, suitable for determining whether the provider of the transa...
Definition: validation.h:24
@ TX_MISSING_INPUTS
transaction was missing some of its inputs
@ TX_MEMPOOL_POLICY
violated mempool's fee/size/descendant/RBF/etc limits
@ TX_PREMATURE_SPEND
transaction spends a coinbase too early, or violates locktime/sequence locks
@ TX_INPUTS_NOT_STANDARD
inputs (covered by txid) failed policy rules
@ TX_WITNESS_STRIPPED
Transaction is missing a witness.
@ TX_CONFLICT
Tx already in mempool or conflicts with a tx in the chain (if it conflicts with another tx in mempool...
@ TX_RECENT_CONSENSUS_CHANGE
Invalid by a change to consensus rules more recent than SegWit.
@ TX_NOT_STANDARD
otherwise didn't meet our local policy rules
@ TX_WITNESS_MUTATED
Transaction might have a witness prior to SegWit activation, or witness may have been malleated (whic...
@ TX_NO_MEMPOOL
this node does not have a mempool so can't validate the transaction
@ TX_RESULT_UNSET
initial value. Tx has not yet been rejected
@ TX_CONSENSUS
invalid by consensus rules
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
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:32
@ OP_RETURN
Definition: script.h:107
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1109
std::vector< std::vector< unsigned char > > stack
Definition: script.h:566
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12