1 // Copyright (c) 2021-present 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 #include <util/hasher.h>
13 14 #include <cstdint>
15 #include <unordered_set>
16 #include <vector>
17 18 /** Default maximum number of transactions in a package. */
19 static constexpr uint32_t MAX_PACKAGE_COUNT{25};
20 /** Default maximum total weight of transactions in a package in weight
21 to allow for context-less checks. This must allow a superset of sigops
22 weighted vsize limited transactions to not disallow transactions we would
23 have otherwise accepted individually. */
24 static constexpr uint32_t MAX_PACKAGE_WEIGHT = 404'000;
25 static_assert(MAX_PACKAGE_WEIGHT >= MAX_STANDARD_TX_WEIGHT);
26 27 // Packages are part of a single cluster, so ensure that the package limits are
28 // set within the mempool's cluster size limits.
29 static_assert(DEFAULT_CLUSTER_LIMIT >= MAX_PACKAGE_COUNT);
30 static_assert(MAX_PACKAGE_WEIGHT <= DEFAULT_CLUSTER_SIZE_LIMIT_KVB * WITNESS_SCALE_FACTOR * 1000);
31 32 /** A "reason" why a package was invalid. It may be that one or more of the included
33 * transactions is invalid or the package itself violates our rules.
34 * We don't distinguish between consensus and policy violations right now.
35 */
36 enum class PackageValidationResult {
37 PCKG_RESULT_UNSET = 0, //!< Initial value. The package has not yet been rejected.
38 PCKG_POLICY, //!< The package itself is invalid (e.g. too many transactions).
39 PCKG_TX, //!< At least one tx is invalid.
40 PCKG_MEMPOOL_ERROR, //!< Mempool logic error.
41 };
42 43 /** A package is an ordered list of transactions. The transactions cannot conflict with (spend the
44 * same inputs as) one another. */
45 using Package = std::vector<CTransactionRef>;
46 47 class PackageValidationState : public ValidationState<PackageValidationResult> {};
48 49 /** If any direct dependencies exist between transactions (i.e. a child spending the output of a
50 * parent), checks that all parents appear somewhere in the list before their respective children.
51 * No other ordering is enforced. This function cannot detect indirect dependencies (e.g. a
52 * transaction's grandparent if its parent is not present).
53 * @returns true if sorted. False if any tx spends the output of a tx that appears later in txns.
54 */
55 bool IsTopoSortedPackage(const Package& txns);
56 57 /** Checks that these transactions don't conflict, i.e., spend the same prevout. This includes
58 * checking that there are no duplicate transactions. Since these checks require looking at the inputs
59 * of a transaction, returns false immediately if any transactions have empty vin.
60 *
61 * Does not check consistency of a transaction with oneself; does not check if a transaction spends
62 * the same prevout multiple times (see bad-txns-inputs-duplicate in CheckTransaction()).
63 *
64 * @returns true if there are no conflicts. False if any two transactions spend the same prevout.
65 * */
66 bool IsConsistentPackage(const Package& txns);
67 68 /** Context-free package policy checks:
69 * 1. The number of transactions cannot exceed MAX_PACKAGE_COUNT.
70 * 2. The total weight cannot exceed MAX_PACKAGE_WEIGHT.
71 * 3. If any dependencies exist between transactions, parents must appear before children.
72 * 4. Transactions cannot conflict, i.e., spend the same inputs.
73 */
74 bool IsWellFormedPackage(const Package& txns, PackageValidationState& state);
75 76 /** Context-free check that a package is exactly one child and its parents; not all parents need to
77 * be present, but the package must not contain any transactions that are not the child's parents.
78 * It is expected to be sorted, which means the last transaction must be the child.
79 */
80 bool IsChildWithParents(const Package& package);
81 82 /** Context-free check that a package IsChildWithParents() and none of the parents depend on each
83 * other (the package is a "tree").
84 */
85 bool IsChildWithParentsTree(const Package& package);
86 87 /** Get the hash of the concatenated wtxids of transactions, with wtxids
88 * treated as a little-endian numbers and sorted in ascending numeric order.
89 */
90 uint256 GetPackageHash(const std::vector<CTransactionRef>& transactions);
91 92 #endif // BITCOIN_POLICY_PACKAGES_H
93