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 
18 enum class TxValidationResult {
19  TX_RESULT_UNSET = 0,
20  TX_CONSENSUS,
21 
32 
45 };
46 
53  BLOCK_RESULT_UNSET = 0,
55 
70 };
71 
72 
73 
77 template <typename Result>
79 private:
80  enum mode_state {
84  } m_mode{MODE_VALID};
85  Result m_result{};
86  std::string m_reject_reason;
87  std::string m_debug_message;
88 public:
89  bool Invalid(Result result,
90  const std::string &reject_reason="",
91  const std::string &debug_message="")
92  {
93  m_result = result;
94  m_reject_reason = reject_reason;
95  m_debug_message = debug_message;
96  if (m_mode != MODE_ERROR) m_mode = MODE_INVALID;
97  return false;
98  }
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  Result GetResult() const { return m_result; }
110  std::string GetRejectReason() const { return m_reject_reason; }
111  std::string GetDebugMessage() const { return m_debug_message; }
112  std::string ToString() const
113  {
114  if (IsValid()) {
115  return "Valid";
116  }
117 
118  if (!m_debug_message.empty()) {
119  return m_reject_reason + ", " + m_debug_message;
120  }
121 
122  return m_reject_reason;
123  }
124 };
125 
126 class TxValidationState : public ValidationState<TxValidationResult> {};
127 class BlockValidationState : public ValidationState<BlockValidationResult> {};
128 
129 // These implement the weight = (stripped_size * 4) + witness_size formula,
130 // using only serialization with and without witness data. As witness_size
131 // is equal to total_size - stripped_size, this formula is identical to:
132 // weight = (stripped_size * 3) + total_size.
133 static inline int64_t GetTransactionWeight(const CTransaction& tx)
134 {
136 }
137 static inline int64_t GetBlockWeight(const CBlock& block)
138 {
140 }
141 static inline int64_t GetTransactionInputWeight(const CTxIn& txin)
142 {
143  // scriptWitness size is added here because witnesses and txins are split up in segwit serialization.
145 }
146 
147 #endif // BITCOIN_CONSENSUS_VALIDATION_H
static int64_t GetTransactionWeight(const CTransaction &tx)
Definition: validation.h:133
static const int SERIALIZE_TRANSACTION_NO_WITNESS
Definition: transaction.h:15
invalid by consensus rules
bool Error(const std::string &reject_reason)
Definition: validation.h:99
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
std::string GetDebugMessage() const
Definition: validation.h:111
Template for capturing information about block/transaction validation.
Definition: validation.h:78
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:63
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:546
violated mempool&#39;s fee/size/descendant/RBF/etc limits
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:1104
bool IsValid() const
Definition: validation.h:106
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:89
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:57
static int64_t GetTransactionInputWeight(const CTxIn &txin)
Definition: validation.h:141
std::string ToString() const
Definition: validation.h:112
the block failed to meet one of our checkpoints
Result GetResult() const
Definition: validation.h:109
std::string m_debug_message
Definition: validation.h:87
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.
bool IsInvalid() const
Definition: validation.h:107
Invalid by a change to consensus rules more recent than SegWit.
block timestamp was > 2 hours in the future (or our clock is bad)
std::string m_reject_reason
Definition: validation.h:86
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:251
network rule violation (DoS value may be set)
Definition: validation.h:82
std::string GetRejectReason() const
Definition: validation.h:110
static int64_t GetBlockWeight(const CBlock &block)
Definition: validation.h:137
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
bool IsError() const
Definition: validation.h:108
TxValidationResult
A "reason" why a transaction was invalid, suitable for determining whether the provider of the transa...
Definition: validation.h:18