1 // Copyright (c) 2017-2021 The Limenka developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 5 #ifndef LIMENKA_CONSENSUS_TX_VERIFY_H
6 #define LIMENKA_CONSENSUS_TX_VERIFY_H
7 8 #include <consensus/amount.h>
9 #include <script/script.h>
10 #include <consensus/params.h>
11 12 #include <stdint.h>
13 #include <vector>
14 15 class CBlockIndex;
16 class CCoinsViewCache;
17 class CTransaction;
18 class TxValidationState;
19 20 /** Transaction validation functions */
21 22 class CheckTxInputsRules {
23 using underlying_type = unsigned int;
24 underlying_type m_flags;
25 constexpr explicit CheckTxInputsRules(underlying_type flags) noexcept : m_flags(flags) {}
26 27 enum class Rule {
28 None = 0,
29 OutputSizeLimit = 1 << 0,
30 };
31 32 public:
33 using enum Rule;
34 35 constexpr CheckTxInputsRules(Rule rule) noexcept : m_flags(static_cast<underlying_type>(rule)) {}
36 37 [[nodiscard]] constexpr bool test(CheckTxInputsRules rules) const noexcept {
38 return (m_flags & rules.m_flags) == rules.m_flags;
39 }
40 41 [[nodiscard]] constexpr CheckTxInputsRules operator|(const CheckTxInputsRules other) const noexcept {
42 return CheckTxInputsRules{m_flags | other.m_flags};
43 }
44 };
45 46 namespace Consensus {
47 /**
48 * Check whether all outputs of this transaction satisfy size limits.
49 * Regular outputs must be <= MAX_OUTPUT_SCRIPT_SIZE (34 bytes).
50 * OP_RETURN outputs must be <= MAX_OUTPUT_DATA_SIZE (83 bytes).
51 */
52 bool CheckOutputSizes(const CTransaction& tx, TxValidationState& state);
53 54 /**
55 * Check whether all inputs of this transaction are valid (no double spends and amounts)
56 * This does not modify the UTXO set. This does not check scripts and sigs.
57 * @param[out] txfee Set to the transaction fee if successful.
58 * Preconditions: tx.IsCoinBase() is false.
59 */
60 [[nodiscard]] bool CheckTxInputs(const CTransaction& tx, TxValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, CAmount& txfee, CheckTxInputsRules rules, const Consensus::Params& consensusParams, bool fork_active);
61 62 /** Check that a script only contains whitelisted opcodes (post-activation creation gate). */
63 bool CheckScriptOpcodeWhitelist(const CScript& script);
64 65 /** Check that sub-satoshi companion OP_RETURNs are valid. */
66 67 } // namespace Consensus
68 69 /** Auxiliary functions for transaction validation (ideally should not be exposed) */
70 71 /**
72 * Count ECDSA signature operations the old-fashioned (pre-0.6) way
73 * @return number of sigops this transaction's outputs will produce when spent
74 * @see CTransaction::FetchInputs
75 */
76 unsigned int GetLegacySigOpCount(const CTransaction& tx);
77 78 /**
79 * Count ECDSA signature operations in pay-to-script-hash inputs.
80 *
81 * @param[in] mapInputs Map of previous transactions that have outputs we're spending
82 * @return maximum number of sigops required to validate this transaction's inputs
83 * @see CTransaction::FetchInputs
84 */
85 unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& mapInputs);
86 87 /**
88 * Compute total signature operation cost of a transaction.
89 * @param[in] tx Transaction for which we are computing the cost
90 * @param[in] inputs Map of previous transactions that have outputs we're spending
91 * @param[in] flags Script verification flags
92 * @return Total signature operation cost of tx
93 */
94 int64_t GetTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& inputs, uint32_t flags);
95 96 /**
97 * Check if transaction is final and can be included in a block with the
98 * specified height and time. Consensus critical.
99 */
100 bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime);
101 102 /**
103 * Calculates the block height and previous block's median time past at
104 * which the transaction will be considered final in the context of BIP 68.
105 * For each input that is not sequence locked, the corresponding entries in
106 * prevHeights are set to 0 as they do not affect the calculation.
107 */
108 std::pair<int, int64_t> CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector<int>& prevHeights, const CBlockIndex& block);
109 110 bool EvaluateSequenceLocks(const CBlockIndex& block, std::pair<int, int64_t> lockPair);
111 /**
112 * Check if transaction is final per BIP 68 sequence numbers and can be included in a block.
113 * Consensus critical. Takes as input a list of heights at which tx's inputs (in order) confirmed.
114 */
115 bool SequenceLocks(const CTransaction &tx, int flags, std::vector<int>& prevHeights, const CBlockIndex& block);
116 117 #endif // LIMENKA_CONSENSUS_TX_VERIFY_H
118