Bitcoin ABC  0.26.3
P2P Digital Currency
validation.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 <cassert>
10 #include <string>
11 
16 enum class TxValidationResult {
18  TX_RESULT_UNSET = 0,
49  TX_UNKNOWN,
50 };
51 
80 };
81 
87 template <typename Result> class ValidationState {
88 private:
89  enum class ModeState {
90  M_VALID,
91  M_INVALID,
92  M_ERROR,
93  } m_mode{ModeState::M_VALID};
94  Result m_result{};
95  std::string m_reject_reason;
96  std::string m_debug_message;
97 
98 public:
99  bool Invalid(Result result, const std::string &reject_reason = "",
100  const std::string &debug_message = "") {
101  m_result = result;
102  m_reject_reason = reject_reason;
103  m_debug_message = debug_message;
104  if (m_mode != ModeState::M_ERROR) {
105  m_mode = ModeState::M_INVALID;
106  }
107  return false;
108  }
109 
110  bool Error(const std::string &reject_reason) {
111  if (m_mode == ModeState::M_VALID) {
112  m_reject_reason = reject_reason;
113  }
114  m_mode = ModeState::M_ERROR;
115  return false;
116  }
117  bool IsValid() const { return m_mode == ModeState::M_VALID; }
118  bool IsInvalid() const { return m_mode == ModeState::M_INVALID; }
119  bool IsError() const { return m_mode == ModeState::M_ERROR; }
120  Result GetResult() const { return m_result; }
121  std::string GetRejectReason() const { return m_reject_reason; }
122  std::string GetDebugMessage() const { return m_debug_message; }
123  std::string ToString() const {
124  if (IsValid()) {
125  return "Valid";
126  }
127 
128  if (!m_debug_message.empty()) {
129  return m_reject_reason + ", " + m_debug_message;
130  }
131 
132  return m_reject_reason;
133  }
134 };
135 
136 class TxValidationState : public ValidationState<TxValidationResult> {};
137 class BlockValidationState : public ValidationState<BlockValidationResult> {};
138 
139 #endif // BITCOIN_CONSENSUS_VALIDATION_H
Template for capturing information about block/transaction validation.
Definition: validation.h:87
@ M_INVALID
network rule violation (DoS value may be set)
bool IsValid() const
Definition: validation.h:117
std::string GetRejectReason() const
Definition: validation.h:121
Result m_result
Definition: validation.h:94
std::string m_reject_reason
Definition: validation.h:95
std::string GetDebugMessage() const
Definition: validation.h:122
bool Error(const std::string &reject_reason)
Definition: validation.h:110
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:99
bool IsError() const
Definition: validation.h:119
Result GetResult() const
Definition: validation.h:120
std::string m_debug_message
Definition: validation.h:96
std::string ToString() const
Definition: validation.h:123
bool IsInvalid() const
Definition: validation.h:118
BlockValidationResult
A "reason" why a block was invalid, suitable for determining whether the provider of the block should...
Definition: validation.h:58
@ BLOCK_CHECKPOINT
the block failed to meet one of our checkpoints
@ 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
TxValidationResult
A "reason" why a transaction was invalid, suitable for determining whether the provider of the transa...
Definition: validation.h:16
@ TX_MISSING_INPUTS
transaction was missing some of its inputs
@ TX_CHILD_BEFORE_PARENT
This tx outputs are already spent in the mempool.
@ TX_MEMPOOL_POLICY
violated mempool's fee/size/descendant/etc limits
@ TX_UNKNOWN
transaction was not validated because package failed
@ TX_PREMATURE_SPEND
transaction spends a coinbase too early, or violates locktime/sequence locks
@ TX_DUPLICATE
Tx already in mempool or in the chain.
@ TX_INPUTS_NOT_STANDARD
inputs failed policy rules
@ TX_CONFLICT
Tx conflicts with another mempool tx, i.e.
@ TX_NOT_STANDARD
otherwise didn't meet our local policy rules
@ 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
@ TX_RECONSIDERABLE
fails some policy, but might be acceptable if submitted in a (different) package