Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
validation.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-present 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
10#include <primitives/block.h>
12#include <script/script.h>
13#include <serialize.h>
14
15#include <cstddef>
16#include <cstdint>
17#include <string>
18#include <vector>
19
20
22inline constexpr int NO_WITNESS_COMMITMENT{-1};
23
25inline constexpr size_t MINIMUM_WITNESS_COMMITMENT{38};
26
58
75
76
77
81template <typename Result>
83{
84private:
90 Result m_result{};
91 std::string m_reject_reason;
92 std::string m_debug_message;
93
94public:
95 bool Invalid(Result result,
96 const std::string& reject_reason = "",
97 const std::string& debug_message = "")
98 {
99 m_result = result;
103 return false;
104 }
105 bool Error(const std::string& reject_reason)
106 {
110 return false;
111 }
112 bool IsValid() const { return m_mode == ModeState::M_VALID; }
113 bool IsInvalid() const { return m_mode == ModeState::M_INVALID; }
114 bool IsError() const { return m_mode == ModeState::M_ERROR; }
115 Result GetResult() const { return m_result; }
116 std::string GetRejectReason() const { return m_reject_reason; }
117 std::string GetDebugMessage() const { return m_debug_message; }
118 std::string ToString() const
119 {
120 if (IsValid()) {
121 return "Valid";
122 }
123
124 if (!m_debug_message.empty()) {
125 return m_reject_reason + ", " + m_debug_message;
126 }
127
128 return m_reject_reason;
129 }
130};
131
132class TxValidationState : public ValidationState<TxValidationResult> {};
133class BlockValidationState : public ValidationState<BlockValidationResult> {};
134
135// These implement the weight = (stripped_size * 4) + witness_size formula,
136// using only serialization with and without witness data. As witness_size
137// is equal to total_size - stripped_size, this formula is identical to:
138// weight = (stripped_size * 3) + total_size.
140{
141 return ::GetSerializeSize(TX_NO_WITNESS(tx)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(tx));
142}
143static inline int64_t GetBlockWeight(const CBlock& block)
144{
145 return ::GetSerializeSize(TX_NO_WITNESS(block)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(block));
146}
148{
149 // scriptWitness size is added here because witnesses and txins are split up in segwit serialization.
150 return ::GetSerializeSize(TX_NO_WITNESS(txin)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(txin)) + ::GetSerializeSize(txin.scriptWitness.stack);
151}
152
154inline int GetWitnessCommitmentIndex(const CBlock& block)
155{
157 if (!block.vtx.empty()) {
158 for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
159 const CTxOut& vout = block.vtx[0]->vout[o];
161 vout.scriptPubKey[0] == OP_RETURN &&
162 vout.scriptPubKey[1] == 0x24 &&
163 vout.scriptPubKey[2] == 0xaa &&
164 vout.scriptPubKey[3] == 0x21 &&
165 vout.scriptPubKey[4] == 0xa9 &&
166 vout.scriptPubKey[5] == 0xed) {
167 commitpos = o;
168 }
169 }
170 }
171 return commitpos;
172}
173
174#endif // BITCOIN_CONSENSUS_VALIDATION_H
Definition block.h:74
std::vector< CTransactionRef > vtx
Definition block.h:77
The basic transaction that is broadcasted on the network and contained in blocks.
An input of a transaction.
Definition transaction.h:63
An output of a transaction.
CScript scriptPubKey
Template for capturing information about block/transaction validation.
Definition validation.h:83
@ M_INVALID
network rule violation (DoS value may be set)
bool IsValid() const
Definition validation.h:112
std::string GetRejectReason() const
Definition validation.h:116
enum ValidationState::ModeState M_VALID
std::string m_reject_reason
Definition validation.h:91
std::string GetDebugMessage() const
Definition validation.h:117
bool Error(const std::string &reject_reason)
Definition validation.h:105
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition validation.h:95
bool IsError() const
Definition validation.h:114
Result GetResult() const
Definition validation.h:115
std::string m_debug_message
Definition validation.h:92
std::string ToString() const
Definition validation.h:118
bool IsInvalid() const
Definition validation.h:113
size_type size() const
Definition prevector.h:247
constexpr int NO_WITNESS_COMMITMENT
Index marker for when no witness commitment is present in a coinbase transaction.
Definition validation.h:22
constexpr size_t MINIMUM_WITNESS_COMMITMENT
Minimum size of a witness commitment structure.
Definition validation.h:25
static int64_t GetTransactionInputWeight(const CTxIn &txin)
Definition validation.h:147
static int64_t GetBlockWeight(const CBlock &block)
Definition validation.h:143
static int32_t GetTransactionWeight(const CTransaction &tx)
Definition validation.h:139
BlockValidationResult
A "reason" why a block was invalid, suitable for determining whether the provider of the block should...
Definition validation.h:64
@ 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:154
TxValidationResult
A "reason" why a transaction was invalid, suitable for determining whether the provider of the transa...
Definition validation.h:30
@ TX_MISSING_INPUTS
transaction was missing some of its inputs
@ TX_MEMPOOL_POLICY
violated mempool's fee/size/descendant/RBF/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_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_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
@ TX_RECONSIDERABLE
fails some policy, but might be acceptable if submitted in a (different) package
constexpr int WITNESS_SCALE_FACTOR
Definition consensus.h:21
constexpr TransactionSerParams TX_NO_WITNESS
constexpr TransactionSerParams TX_WITH_WITNESS
@ OP_RETURN
Definition script.h:112
uint64_t GetSerializeSize(const T &t)
Definition serialize.h:1157
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82