Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
# 2 : : // Copyright (c) 2009-2021 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 : :
# 15 : : /** Index marker for when no witness commitment is present in a coinbase transaction. */
# 16 : : static constexpr int NO_WITNESS_COMMITMENT{-1};
# 17 : :
# 18 : : /** Minimum size of a witness commitment structure. Defined in BIP 141. **/
# 19 : : static constexpr size_t MINIMUM_WITNESS_COMMITMENT{38};
# 20 : :
# 21 : : /** A "reason" why a transaction was invalid, suitable for determining whether the
# 22 : : * provider of the transaction should be banned/ignored/disconnected/etc.
# 23 : : */
# 24 : : enum class TxValidationResult {
# 25 : : TX_RESULT_UNSET = 0, //!< initial value. Tx has not yet been rejected
# 26 : : TX_CONSENSUS, //!< invalid by consensus rules
# 27 : : /**
# 28 : : * Invalid by a change to consensus rules more recent than SegWit.
# 29 : : * Currently unused as there are no such consensus rule changes, and any download
# 30 : : * sources realistically need to support SegWit in order to provide useful data,
# 31 : : * so differentiating between always-invalid and invalid-by-pre-SegWit-soft-fork
# 32 : : * is uninteresting.
# 33 : : */
# 34 : : TX_RECENT_CONSENSUS_CHANGE,
# 35 : : TX_INPUTS_NOT_STANDARD, //!< inputs (covered by txid) failed policy rules
# 36 : : TX_NOT_STANDARD, //!< otherwise didn't meet our local policy rules
# 37 : : TX_MISSING_INPUTS, //!< transaction was missing some of its inputs
# 38 : : TX_PREMATURE_SPEND, //!< transaction spends a coinbase too early, or violates locktime/sequence locks
# 39 : : /**
# 40 : : * Transaction might have a witness prior to SegWit
# 41 : : * activation, or witness may have been malleated (which includes
# 42 : : * non-standard witnesses).
# 43 : : */
# 44 : : TX_WITNESS_MUTATED,
# 45 : : /**
# 46 : : * Transaction is missing a witness.
# 47 : : */
# 48 : : TX_WITNESS_STRIPPED,
# 49 : : /**
# 50 : : * Tx already in mempool or conflicts with a tx in the chain
# 51 : : * (if it conflicts with another tx in mempool, we use MEMPOOL_POLICY as it failed to reach the RBF threshold)
# 52 : : * Currently this is only used if the transaction already exists in the mempool or on chain.
# 53 : : */
# 54 : : TX_CONFLICT,
# 55 : : TX_MEMPOOL_POLICY, //!< violated mempool's fee/size/descendant/RBF/etc limits
# 56 : : TX_NO_MEMPOOL, //!< this node does not have a mempool so can't validate the transaction
# 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 : : /**
# 68 : : * Invalid by a change to consensus rules more recent than SegWit.
# 69 : : * Currently unused as there are no such consensus rule changes, and any download
# 70 : : * sources realistically need to support SegWit in order to provide useful data,
# 71 : : * so differentiating between always-invalid and invalid-by-pre-SegWit-soft-fork
# 72 : : * is uninteresting.
# 73 : : */
# 74 : : BLOCK_RECENT_CONSENSUS_CHANGE,
# 75 : : BLOCK_CACHED_INVALID, //!< this block was cached as being invalid and we didn't store the reason why
# 76 : : BLOCK_INVALID_HEADER, //!< invalid proof of work or time too old
# 77 : : BLOCK_MUTATED, //!< the block's data didn't match the data committed to by the PoW
# 78 : : BLOCK_MISSING_PREV, //!< We don't have the previous block the checked one is built on
# 79 : : BLOCK_INVALID_PREV, //!< A block this one builds on is invalid
# 80 : : BLOCK_TIME_FUTURE, //!< block timestamp was > 2 hours in the future (or our clock is bad)
# 81 : : BLOCK_CHECKPOINT, //!< the block failed to meet one of our checkpoints
# 82 : : BLOCK_HEADER_LOW_WORK //!< the block header may be on a too-little-work chain
# 83 : : };
# 84 : :
# 85 : :
# 86 : :
# 87 : : /** Template for capturing information about block/transaction validation. This is instantiated
# 88 : : * by TxValidationState and BlockValidationState for validation information on transactions
# 89 : : * and blocks respectively. */
# 90 : : template <typename Result>
# 91 : : class ValidationState
# 92 : : {
# 93 : : private:
# 94 : : enum class ModeState {
# 95 : : M_VALID, //!< everything ok
# 96 : : M_INVALID, //!< network rule violation (DoS value may be set)
# 97 : : M_ERROR, //!< run-time error
# 98 : : } m_mode{ModeState::M_VALID};
# 99 : : Result m_result{};
# 100 : : std::string m_reject_reason;
# 101 : : std::string m_debug_message;
# 102 : :
# 103 : : public:
# 104 : : bool Invalid(Result result,
# 105 : : const std::string& reject_reason = "",
# 106 : : const std::string& debug_message = "")
# 107 : 85245 : {
# 108 : 85245 : m_result = result;
# 109 : 85245 : m_reject_reason = reject_reason;
# 110 : 85245 : m_debug_message = debug_message;
# 111 [ + - ][ + - ]: 85245 : if (m_mode != ModeState::M_ERROR) m_mode = ModeState::M_INVALID;
# [ + - ]
# 112 : 85245 : return false;
# 113 : 85245 : }
# 114 : : bool Error(const std::string& reject_reason)
# 115 : 1 : {
# 116 [ + - ]: 1 : if (m_mode == ModeState::M_VALID)
# 117 : 1 : m_reject_reason = reject_reason;
# 118 : 1 : m_mode = ModeState::M_ERROR;
# 119 : 1 : return false;
# 120 : 1 : }
# 121 : 305460 : bool IsValid() const { return m_mode == ModeState::M_VALID; }
# 122 : 101382 : bool IsInvalid() const { return m_mode == ModeState::M_INVALID; }
# 123 : 3996 : bool IsError() const { return m_mode == ModeState::M_ERROR; }
# 124 : 12914 : Result GetResult() const { return m_result; }
# 125 : 5740 : std::string GetRejectReason() const { return m_reject_reason; }
# 126 : 3040 : std::string GetDebugMessage() const { return m_debug_message; }
# 127 : : std::string ToString() const
# 128 : 178425 : {
# 129 [ + + ][ - + ]: 178425 : if (IsValid()) {
# 130 : 163956 : return "Valid";
# 131 : 163956 : }
# 132 : :
# 133 [ + + ][ + + ]: 14469 : if (!m_debug_message.empty()) {
# 134 : 1433 : return m_reject_reason + ", " + m_debug_message;
# 135 : 1433 : }
# 136 : :
# 137 : 13036 : return m_reject_reason;
# 138 : 14469 : }
# 139 : : };
# 140 : :
# 141 : : class TxValidationState : public ValidationState<TxValidationResult> {};
# 142 : : class BlockValidationState : public ValidationState<BlockValidationResult> {};
# 143 : :
# 144 : : // These implement the weight = (stripped_size * 4) + witness_size formula,
# 145 : : // using only serialization with and without witness data. As witness_size
# 146 : : // is equal to total_size - stripped_size, this formula is identical to:
# 147 : : // weight = (stripped_size * 3) + total_size.
# 148 : : static inline int64_t GetTransactionWeight(const CTransaction& tx)
# 149 : 152946 : {
# 150 : 152946 : return ::GetSerializeSize(tx, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(tx, PROTOCOL_VERSION);
# 151 : 152946 : }
# 152 : : static inline int64_t GetBlockWeight(const CBlock& block)
# 153 : 155380 : {
# 154 : 155380 : return ::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(block, PROTOCOL_VERSION);
# 155 : 155380 : }
# 156 : : static inline int64_t GetTransactionInputWeight(const CTxIn& txin)
# 157 : 418353 : {
# 158 : : // scriptWitness size is added here because witnesses and txins are split up in segwit serialization.
# 159 : 418353 : return ::GetSerializeSize(txin, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(txin, PROTOCOL_VERSION) + ::GetSerializeSize(txin.scriptWitness.stack, PROTOCOL_VERSION);
# 160 : 418353 : }
# 161 : :
# 162 : : /** Compute at which vout of the block's coinbase transaction the witness commitment occurs, or -1 if not found */
# 163 : : inline int GetWitnessCommitmentIndex(const CBlock& block)
# 164 : 212522 : {
# 165 : 212522 : int commitpos = NO_WITNESS_COMMITMENT;
# 166 [ + - ]: 212522 : if (!block.vtx.empty()) {
# 167 [ + + ]: 598620 : for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
# 168 : 386098 : const CTxOut& vout = block.vtx[0]->vout[o];
# 169 [ + + ]: 386098 : if (vout.scriptPubKey.size() >= MINIMUM_WITNESS_COMMITMENT &&
# 170 [ + + ]: 386098 : vout.scriptPubKey[0] == OP_RETURN &&
# 171 [ + - ]: 386098 : vout.scriptPubKey[1] == 0x24 &&
# 172 [ + - ]: 386098 : vout.scriptPubKey[2] == 0xaa &&
# 173 [ + - ]: 386098 : vout.scriptPubKey[3] == 0x21 &&
# 174 [ + - ]: 386098 : vout.scriptPubKey[4] == 0xa9 &&
# 175 [ + - ]: 386098 : vout.scriptPubKey[5] == 0xed) {
# 176 : 163221 : commitpos = o;
# 177 : 163221 : }
# 178 : 386098 : }
# 179 : 212522 : }
# 180 : 212522 : return commitpos;
# 181 : 212522 : }
# 182 : :
# 183 : : #endif // BITCOIN_CONSENSUS_VALIDATION_H
|