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