Bitcoin ABC  0.24.7
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,
43 };
44 
81 };
82 
88 template <typename Result> class ValidationState {
89 private:
90  enum class ModeState {
91  M_VALID,
92  M_INVALID,
93  M_ERROR,
94  } m_mode{ModeState::M_VALID};
95  Result m_result{};
96  std::string m_reject_reason;
97  std::string m_debug_message;
98 
99 public:
100  bool Invalid(Result result, const std::string &reject_reason = "",
101  const std::string &debug_message = "") {
102  m_result = result;
103  m_reject_reason = reject_reason;
104  m_debug_message = debug_message;
105  if (m_mode != ModeState::M_ERROR) {
106  m_mode = ModeState::M_INVALID;
107  }
108  return false;
109  }
110 
111  bool Error(const std::string &reject_reason) {
112  if (m_mode == ModeState::M_VALID) {
113  m_reject_reason = reject_reason;
114  }
115  m_mode = ModeState::M_ERROR;
116  return false;
117  }
118  bool IsValid() const { return m_mode == ModeState::M_VALID; }
119  bool IsInvalid() const { return m_mode == ModeState::M_INVALID; }
120  bool IsError() const { return m_mode == ModeState::M_ERROR; }
121  Result GetResult() const { return m_result; }
122  std::string GetRejectReason() const { return m_reject_reason; }
123  std::string GetDebugMessage() const { return m_debug_message; }
124  std::string ToString() const {
125  if (IsValid()) {
126  return "Valid";
127  }
128 
129  if (!m_debug_message.empty()) {
130  return m_reject_reason + ", " + m_debug_message;
131  }
132 
133  return m_reject_reason;
134  }
135 };
136 
137 class TxValidationState : public ValidationState<TxValidationResult> {};
138 class BlockValidationState : public ValidationState<BlockValidationResult> {};
139 
140 #endif // BITCOIN_CONSENSUS_VALIDATION_H
ValidationState< DelegationResult >::ModeState
ModeState
Definition: validation.h:90
BlockValidationResult::BLOCK_CACHED_INVALID
@ BLOCK_CACHED_INVALID
this block was cached as being invalid and we didn't store the reason why
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:95
BlockValidationState
Definition: validation.h:138
ValidationState::ModeState::M_ERROR
@ M_ERROR
run-time error
ValidationState::GetDebugMessage
std::string GetDebugMessage() const
Definition: validation.h:123
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 Currently this is only used if the transact...
TxValidationResult::TX_NOT_STANDARD
@ TX_NOT_STANDARD
otherwise didn't meet our local policy rules
BlockValidationResult
BlockValidationResult
A "reason" why a block was invalid, suitable for determining whether the provider of the block should...
Definition: validation.h:51
ValidationState::ToString
std::string ToString() const
Definition: validation.h:124
ValidationState::IsValid
bool IsValid() const
Definition: validation.h:118
TxValidationState
Definition: validation.h:137
BlockValidationResult::BLOCK_INVALID_PREV
@ BLOCK_INVALID_PREV
A block this one builds on is invalid.
ValidationState::m_debug_message
std::string m_debug_message
Definition: validation.h:97
ValidationState::Invalid
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:100
ValidationState::GetResult
Result GetResult() const
Definition: validation.h:121
TxValidationResult::TX_MEMPOOL_POLICY
@ TX_MEMPOOL_POLICY
violated mempool's fee/size/descendant/etc limits
TxValidationResult::TX_PREMATURE_SPEND
@ TX_PREMATURE_SPEND
transaction spends a coinbase too early, or violates locktime/sequence locks
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:96
TxValidationResult::TX_CONSENSUS
@ TX_CONSENSUS
invalid by consensus rules
TxValidationResult::TX_MISSING_INPUTS
@ TX_MISSING_INPUTS
transaction was missing some of its inputs
BlockValidationResult::BLOCK_FINALIZATION
@ BLOCK_FINALIZATION
block finalization problems
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
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 failed policy rules
ValidationState::ModeState::M_VALID
@ M_VALID
everything ok
ValidationState::GetRejectReason
std::string GetRejectReason() const
Definition: validation.h:122
TxValidationResult
TxValidationResult
A "reason" why a transaction was invalid, suitable for determining whether the provider of the transa...
Definition: validation.h:16
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:111
ValidationState::IsError
bool IsError() const
Definition: validation.h:120
BlockValidationResult::BLOCK_CHECKPOINT
@ BLOCK_CHECKPOINT
the block failed to meet one of our checkpoints
ValidationState::IsInvalid
bool IsInvalid() const
Definition: validation.h:119
TxValidationResult::TX_RECENT_CONSENSUS_CHANGE
@ TX_RECENT_CONSENSUS_CHANGE
Invalid by a recent change to consensus rules.
ValidationState
Template for capturing information about block/transaction validation.
Definition: validation.h:88