1 // Copyright (c) 2021-2022 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_POLICY_PACKAGES_H
6 #define LIMENKA_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 // If a package is to be evaluated, it must be at least as large as the mempool's ancestor/descendant limits,
28 // otherwise transactions that would be individually accepted may be rejected in a package erroneously.
29 // Since a submitted package must be child-with-unconfirmed-parents (all of the transactions are an ancestor
30 // of the child), package limits are ultimately bounded by mempool package limits. Ensure that the
31 // defaults reflect this constraint.
32 static_assert(DEFAULT_DESCENDANT_LIMIT >= MAX_PACKAGE_COUNT);
33 static_assert(DEFAULT_ANCESTOR_LIMIT >= MAX_PACKAGE_COUNT);
34 static_assert(MAX_PACKAGE_WEIGHT >= DEFAULT_ANCESTOR_SIZE_LIMIT_KVB * WITNESS_SCALE_FACTOR * 1000);
35 static_assert(MAX_PACKAGE_WEIGHT >= DEFAULT_DESCENDANT_SIZE_LIMIT_KVB * WITNESS_SCALE_FACTOR * 1000);
36 37 /** A "reason" why a package was invalid. It may be that one or more of the included
38 * transactions is invalid or the package itself violates our rules.
39 * We don't distinguish between consensus and policy violations right now.
40 */
41 enum class PackageValidationResult {
42 PCKG_RESULT_UNSET = 0, //!< Initial value. The package has not yet been rejected.
43 PCKG_POLICY, //!< The package itself is invalid (e.g. too many transactions).
44 PCKG_TX, //!< At least one tx is invalid.
45 PCKG_MEMPOOL_ERROR, //!< Mempool logic error.
46 };
47 48 /** A package is an ordered list of transactions. The transactions cannot conflict with (spend the
49 * same inputs as) one another. */
50 using Package = std::vector<CTransactionRef>;
51 52 class PackageValidationState : public ValidationState<PackageValidationResult> {};
53 54 /** If any direct dependencies exist between transactions (i.e. a child spending the output of a
55 * parent), checks that all parents appear somewhere in the list before their respective children.
56 * No other ordering is enforced. This function cannot detect indirect dependencies (e.g. a
57 * transaction's grandparent if its parent is not present).
58 * @returns true if sorted. False if any tx spends the output of a tx that appears later in txns.
59 */
60 bool IsTopoSortedPackage(const Package& txns);
61 62 /** Checks that these transactions don't conflict, i.e., spend the same prevout. This includes
63 * checking that there are no duplicate transactions. Since these checks require looking at the inputs
64 * of a transaction, returns false immediately if any transactions have empty vin.
65 *
66 * Does not check consistency of a transaction with oneself; does not check if a transaction spends
67 * the same prevout multiple times (see bad-txns-inputs-duplicate in CheckTransaction()).
68 *
69 * @returns true if there are no conflicts. False if any two transactions spend the same prevout.
70 * */
71 bool IsConsistentPackage(const Package& txns);
72 73 /** Context-free package policy checks:
74 * 1. The number of transactions cannot exceed MAX_PACKAGE_COUNT.
75 * 2. The total weight cannot exceed MAX_PACKAGE_WEIGHT.
76 * 3. If any dependencies exist between transactions, parents must appear before children.
77 * 4. Transactions cannot conflict, i.e., spend the same inputs.
78 */
79 bool IsWellFormedPackage(const Package& txns, PackageValidationState& state, bool require_sorted);
80 81 /** Context-free check that a package is exactly one child and its parents; not all parents need to
82 * be present, but the package must not contain any transactions that are not the child's parents.
83 * It is expected to be sorted, which means the last transaction must be the child.
84 */
85 bool IsChildWithParents(const Package& package);
86 87 /** Context-free check that a package IsChildWithParents() and none of the parents depend on each
88 * other (the package is a "tree").
89 */
90 bool IsChildWithParentsTree(const Package& package);
91 92 /** Get the hash of the concatenated wtxids of transactions, with wtxids
93 * treated as a little-endian numbers and sorted in ascending numeric order.
94 */
95 uint256 GetPackageHash(const std::vector<CTransactionRef>& transactions);
96 97 #endif // LIMENKA_POLICY_PACKAGES_H
98