Branch data Line data Source code
1 : : // Copyright (c) 2021-2022 The Bitcoin Core 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 BITCOIN_POLICY_PACKAGES_H 6 : : #define BITCOIN_POLICY_PACKAGES_H 7 : : 8 : : #include <consensus/consensus.h> 9 : : #include <consensus/validation.h> 10 : : #include <policy/policy.h> 11 : : #include <primitives/transaction.h> 12 : : 13 : : #include <cstdint> 14 : : #include <vector> 15 : : 16 : : /** Default maximum number of transactions in a package. */ 17 : : static constexpr uint32_t MAX_PACKAGE_COUNT{25}; 18 : : /** Default maximum total weight of transactions in a package in weight 19 : : to allow for context-less checks. This must allow a superset of sigops 20 : : weighted vsize limited transactions to not disallow transactions we would 21 : : have otherwise accepted individually. */ 22 : : static constexpr uint32_t MAX_PACKAGE_WEIGHT = 404'000; 23 : : static_assert(MAX_PACKAGE_WEIGHT >= MAX_STANDARD_TX_WEIGHT); 24 : : 25 : : // If a package is to be evaluated, it must be at least as large as the mempool's ancestor/descendant limits, 26 : : // otherwise transactions that would be individually accepted may be rejected in a package erroneously. 27 : : // Since a submitted package must be child-with-unconfirmed-parents (all of the transactions are an ancestor 28 : : // of the child), package limits are ultimately bounded by mempool package limits. Ensure that the 29 : : // defaults reflect this constraint. 30 : : static_assert(DEFAULT_DESCENDANT_LIMIT >= MAX_PACKAGE_COUNT); 31 : : static_assert(DEFAULT_ANCESTOR_LIMIT >= MAX_PACKAGE_COUNT); 32 : : static_assert(MAX_PACKAGE_WEIGHT >= DEFAULT_ANCESTOR_SIZE_LIMIT_KVB * WITNESS_SCALE_FACTOR * 1000); 33 : : static_assert(MAX_PACKAGE_WEIGHT >= DEFAULT_DESCENDANT_SIZE_LIMIT_KVB * WITNESS_SCALE_FACTOR * 1000); 34 : : 35 : : /** A "reason" why a package was invalid. It may be that one or more of the included 36 : : * transactions is invalid or the package itself violates our rules. 37 : : * We don't distinguish between consensus and policy violations right now. 38 : : */ 39 : : enum class PackageValidationResult { 40 : : PCKG_RESULT_UNSET = 0, //!< Initial value. The package has not yet been rejected. 41 : : PCKG_POLICY, //!< The package itself is invalid (e.g. too many transactions). 42 : : PCKG_TX, //!< At least one tx is invalid. 43 : : PCKG_MEMPOOL_ERROR, //!< Mempool logic error. 44 : : }; 45 : : 46 : : /** A package is an ordered list of transactions. The transactions cannot conflict with (spend the 47 : : * same inputs as) one another. */ 48 : : using Package = std::vector<CTransactionRef>; 49 : : 50 : 0 : class PackageValidationState : public ValidationState<PackageValidationResult> {}; 51 : : 52 : : /** Context-free package policy checks: 53 : : * 1. The number of transactions cannot exceed MAX_PACKAGE_COUNT. 54 : : * 2. The total weight cannot exceed MAX_PACKAGE_WEIGHT. 55 : : * 3. If any dependencies exist between transactions, parents must appear before children. 56 : : * 4. Transactions cannot conflict, i.e., spend the same inputs. 57 : : */ 58 : : bool CheckPackage(const Package& txns, PackageValidationState& state); 59 : : 60 : : /** Context-free check that a package is exactly one child and its parents; not all parents need to 61 : : * be present, but the package must not contain any transactions that are not the child's parents. 62 : : * It is expected to be sorted, which means the last transaction must be the child. 63 : : */ 64 : : bool IsChildWithParents(const Package& package); 65 : : 66 : : /** Context-free check that a package IsChildWithParents() and none of the parents depend on each 67 : : * other (the package is a "tree"). 68 : : */ 69 : : bool IsChildWithParentsTree(const Package& package); 70 : : #endif // BITCOIN_POLICY_PACKAGES_H