Branch data Line data Source code
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 : :
9 : : #include <consensus/consensus.h>
10 : : #include <primitives/block.h>
11 : : #include <primitives/transaction.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 : :
21 : : /** Index marker for when no witness commitment is present in a coinbase transaction. */
22 : : inline constexpr int NO_WITNESS_COMMITMENT{-1};
23 : :
24 : : /** Minimum size of a witness commitment structure. Defined in BIP 141. **/
25 : : inline constexpr size_t MINIMUM_WITNESS_COMMITMENT{38};
26 : :
27 : : /** A "reason" why a transaction was invalid, suitable for determining whether the
28 : : * provider of the transaction should be banned/ignored/disconnected/etc.
29 : : */
30 : : enum class TxValidationResult {
31 : : TX_RESULT_UNSET = 0, //!< initial value. Tx has not yet been rejected
32 : : TX_CONSENSUS, //!< invalid by consensus rules
33 : : TX_INPUTS_NOT_STANDARD, //!< inputs (covered by txid) failed policy rules
34 : : TX_NOT_STANDARD, //!< otherwise didn't meet our local policy rules
35 : : TX_MISSING_INPUTS, //!< transaction was missing some of its inputs
36 : : TX_PREMATURE_SPEND, //!< transaction spends a coinbase too early, or violates locktime/sequence locks
37 : : /**
38 : : * Transaction might have a witness prior to SegWit
39 : : * activation, or witness may have been malleated (which includes
40 : : * non-standard witnesses).
41 : : */
42 : : TX_WITNESS_MUTATED,
43 : : /**
44 : : * Transaction is missing a witness.
45 : : */
46 : : TX_WITNESS_STRIPPED,
47 : : /**
48 : : * Tx already in mempool or conflicts with a tx in the chain
49 : : * (if it conflicts with another tx in mempool, we use MEMPOOL_POLICY as it failed to reach the RBF threshold)
50 : : * Currently this is only used if the transaction already exists in the mempool or on chain.
51 : : */
52 : : TX_CONFLICT,
53 : : TX_MEMPOOL_POLICY, //!< violated mempool's fee/size/descendant/RBF/etc limits
54 : : TX_NO_MEMPOOL, //!< this node does not have a mempool so can't validate the transaction
55 : : TX_RECONSIDERABLE, //!< fails some policy, but might be acceptable if submitted in a (different) package
56 : : TX_UNKNOWN, //!< transaction was not validated because package failed
57 : : };
58 : :
59 : : /** A "reason" why a block was invalid, suitable for determining whether the
60 : : * provider of the block should be banned/ignored/disconnected/etc.
61 : : * These are much more granular than the rejection codes, which may be more
62 : : * useful for some other use-cases.
63 : : */
64 : : enum class BlockValidationResult {
65 : : BLOCK_RESULT_UNSET = 0, //!< initial value. Block has not yet been rejected
66 : : BLOCK_CONSENSUS, //!< invalid by consensus rules (excluding any below reasons)
67 : : BLOCK_CACHED_INVALID, //!< this block was cached as being invalid and we didn't store the reason why
68 : : BLOCK_INVALID_HEADER, //!< invalid proof of work or time too old
69 : : BLOCK_MUTATED, //!< the block's data didn't match the data committed to by the PoW
70 : : BLOCK_MISSING_PREV, //!< We don't have the previous block the checked one is built on
71 : : BLOCK_INVALID_PREV, //!< A block this one builds on is invalid
72 : : BLOCK_TIME_FUTURE, //!< block timestamp was > 2 hours in the future (or our clock is bad)
73 : : BLOCK_HEADER_LOW_WORK //!< the block header may be on a too-little-work chain
74 : : };
75 : :
76 : :
77 : :
78 : : /** Template for capturing information about block/transaction validation. This is instantiated
79 : : * by TxValidationState and BlockValidationState for validation information on transactions
80 : : * and blocks respectively. */
81 : : template <typename Result>
82 [ + + + + : 9452120 : class ValidationState
+ - ][ + -
+ - + - +
- + - + -
+ - + - +
- ][ + - +
+ + - ]
[ # # ]
83 : : {
84 : : private:
85 : : enum class ModeState {
86 : : M_VALID, //!< everything ok
87 : : M_INVALID, //!< network rule violation (DoS value may be set)
88 : : M_ERROR, //!< run-time error
89 : : } m_mode{ModeState::M_VALID};
90 : : Result m_result{};
91 : : std::string m_reject_reason;
92 : : std::string m_debug_message;
93 : :
94 : : public:
95 : 56694 : bool Invalid(Result result,
96 : : const std::string& reject_reason = "",
97 [ + - + - : 16346 : const std::string& debug_message = "")
+ - + - +
- ][ + - +
- + - - -
- - + - +
- + - + -
+ - - - -
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - - -
- - + - +
- + - + -
+ - + - +
- + - + -
+ - ][ - -
- - - - +
- + - + -
+ - - - -
- - - - -
- - + - +
- + - - -
- - + - +
- - - - -
- - - - -
- - - + -
- - - - -
- - - - -
- - + - -
- + - ]
98 : : {
99 : 56694 : m_result = result;
100 : 56694 : m_reject_reason = reject_reason;
101 : 56694 : m_debug_message = debug_message;
102 [ + - ]: 56694 : if (m_mode != ModeState::M_ERROR) m_mode = ModeState::M_INVALID;
103 : 56694 : return false;
104 : : }
105 : 1 : bool Error(const std::string& reject_reason)
106 : : {
107 [ - - + - ]: 1 : if (m_mode == ModeState::M_VALID)
108 [ - - ]: 1 : m_reject_reason = reject_reason;
109 : 0 : m_mode = ModeState::M_ERROR;
110 : : return false;
111 : : }
112 [ + + - + : 571760 : bool IsValid() const { return m_mode == ModeState::M_VALID; }
- + - + -
+ + + + +
+ + + + ]
[ + + + +
+ + + - +
- - - +
- ][ + + +
+ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ][ + + +
- + + + +
+ - + - #
# ][ - + -
- - - - +
- + + - -
+ + - +
+ ][ + + -
+ + + ][ -
- - + # #
# # ][ # #
# # # # #
# ]
113 [ + + + - : 191009 : bool IsInvalid() const { return m_mode == ModeState::M_INVALID; }
+ - + + +
+ ][ + + +
- + - + +
+ - + + ]
[ + - + -
+ - + - ]
[ + - # #
# # # # ]
[ - - + -
+ - + + +
- ]
114 [ + - ]: 2303 : bool IsError() const { return m_mode == ModeState::M_ERROR; }
115 [ + + + + : 23847 : Result GetResult() const { return m_result; }
- - + + +
+ + + +
+ ][ + - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ + +
+ - + - +
+ + + + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ][ +
- + - + +
+ + + + +
+ + - ][ +
+ - - - -
- - - - -
- - + + -
+ + - - +
- - + ][ +
+ + + + +
# # # #
# ][ # # #
# # # # #
# # # ][ +
+ + + +
+ ]
116 [ - + + - : 7524 : std::string GetRejectReason() const { return m_reject_reason; }
- + + - -
+ + - - +
+ - - + +
- - + +
- ][ - + +
- - + + +
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ - +
+ - - + +
- - + + -
- + + - -
+ + - - +
+ - - + +
- - + + -
- + + - -
+ + - - +
+ - - + +
- - + + -
- + + - -
+ + - - +
+ - - + +
- - + + -
- + + - -
+ + - ][ -
- - - - +
+ - - - -
- - - -
- ][ # # #
# # # #
# ][ - + ]
117 [ - + + - : 1440 : std::string GetDebugMessage() const { return m_debug_message; }
- + + - -
+ + + - +
+ + - + +
- - + +
- ][ - + +
- - + +
- ][ - - -
- - + + -
- - - - -
- - - ]
[ # # # # ]
[ - + ]
118 : 151880 : std::string ToString() const
119 : : {
120 [ + + ]: 151880 : if (IsValid()) {
121 : 136214 : return "Valid";
122 : : }
123 : :
124 [ + + ]: 15666 : if (!m_debug_message.empty()) {
125 [ + - ]: 23976 : return m_reject_reason + ", " + m_debug_message;
126 : : }
127 : :
128 [ - + ]: 3678 : return m_reject_reason;
129 : : }
130 : : };
131 : :
132 [ + - + - : 26129450 : class TxValidationState : public ValidationState<TxValidationResult> {};
+ - + - +
+ + - + -
+ - - - -
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - - - -
+ + ][ + +
+ - # # #
# # # ][ +
- + - + -
+ - + - +
- + - +
- ][ + - +
- + - + -
+ - + - +
- # # ][ -
- - - + -
+ - + + +
- - - - -
- - - - +
- + - - -
- - + + +
- + - - -
- - - - -
- + - + -
+ - - - -
- - - - -
- - - - -
- - - +
- ][ + + #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
133 [ + + + + : 2001307 : class 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.
139 : 1020366 : static inline int32_t GetTransactionWeight(const CTransaction& tx)
140 : : {
141 : 1020366 : return ::GetSerializeSize(TX_NO_WITNESS(tx)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(tx));
142 : : }
143 : 228760 : static inline int64_t GetBlockWeight(const CBlock& block)
144 : : {
145 : 228760 : return ::GetSerializeSize(TX_NO_WITNESS(block)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(block));
146 : : }
147 : 2497 : static inline int64_t GetTransactionInputWeight(const CTxIn& txin)
148 : : {
149 : : // scriptWitness size is added here because witnesses and txins are split up in segwit serialization.
150 : 2497 : return ::GetSerializeSize(TX_NO_WITNESS(txin)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(txin)) + ::GetSerializeSize(txin.scriptWitness.stack);
151 : : }
152 : :
153 : : /** Compute at which vout of the block's coinbase transaction the witness commitment occurs, or -1 if not found */
154 : 343602 : inline int GetWitnessCommitmentIndex(const CBlock& block)
155 : : {
156 : 343602 : int commitpos = NO_WITNESS_COMMITMENT;
157 [ + + ]: 343602 : if (!block.vtx.empty()) {
158 [ - + + + ]: 946376 : for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
159 [ + + ]: 602775 : const CTxOut& vout = block.vtx[0]->vout[o];
160 [ + + + + ]: 1136896 : if (vout.scriptPubKey.size() >= MINIMUM_WITNESS_COMMITMENT &&
161 [ + + + + ]: 270830 : vout.scriptPubKey[0] == OP_RETURN &&
162 [ + + + - ]: 262349 : vout.scriptPubKey[1] == 0x24 &&
163 [ + - + - ]: 249220 : vout.scriptPubKey[2] == 0xaa &&
164 [ + - + - ]: 249220 : vout.scriptPubKey[3] == 0x21 &&
165 [ + + + - : 520992 : vout.scriptPubKey[4] == 0xa9 &&
+ - ]
166 [ + - ]: 249220 : vout.scriptPubKey[5] == 0xed) {
167 : 249220 : commitpos = o;
168 : : }
169 : : }
170 : : }
171 : 343602 : return commitpos;
172 : : }
173 : :
174 : : #endif // BITCOIN_CONSENSUS_VALIDATION_H
|