| 77 | } |
| 78 | |
| 79 | bool IsWellFormedPackage(const Package& txns, PackageValidationState& state) |
| 80 | { |
| 81 | const unsigned int package_count = txns.size(); |
| 82 | |
| 83 | if (package_count > MAX_PACKAGE_COUNT) { |
| 84 | return state.Invalid(PackageValidationResult::PCKG_POLICY, "package-too-many-transactions"); |
| 85 | } |
| 86 | |
| 87 | const int64_t total_weight = std::accumulate(txns.cbegin(), txns.cend(), 0, |
| 88 | [](int64_t sum, const auto& tx) { return sum + GetTransactionWeight(*tx); }); |
| 89 | // If the package only contains 1 tx, it's better to report the policy violation on individual tx weight. |
| 90 | if (package_count > 1 && total_weight > MAX_PACKAGE_WEIGHT) { |
| 91 | return state.Invalid(PackageValidationResult::PCKG_POLICY, "package-too-large"); |
| 92 | } |
| 93 | |
| 94 | std::unordered_set<Txid, SaltedTxidHasher> later_txids; |
| 95 | std::transform(txns.cbegin(), txns.cend(), std::inserter(later_txids, later_txids.end()), |
| 96 | [](const auto& tx) { return tx->GetHash(); }); |
| 97 | |
| 98 | // Package must not contain any duplicate transactions, which is checked by txid. This also |
| 99 | // includes transactions with duplicate wtxids and same-txid-different-witness transactions. |
| 100 | if (later_txids.size() != txns.size()) { |
| 101 | return state.Invalid(PackageValidationResult::PCKG_POLICY, "package-contains-duplicates"); |
| 102 | } |
| 103 | |
| 104 | // Require the package to be sorted in order of dependency, i.e. parents appear before children. |
| 105 | // An unsorted package will fail anyway on missing-inputs, but it's better to quit earlier and |
| 106 | // fail on something less ambiguous (missing-inputs could also be an orphan or trying to |
| 107 | // spend nonexistent coins). |
| 108 | if (!IsTopoSortedPackage(txns, later_txids)) { |
| 109 | return state.Invalid(PackageValidationResult::PCKG_POLICY, "package-not-sorted"); |
| 110 | } |
| 111 | |
| 112 | // Don't allow any conflicting transactions, i.e. spending the same inputs, in a package. |
| 113 | if (!IsConsistentPackage(txns)) { |
| 114 | return state.Invalid(PackageValidationResult::PCKG_POLICY, "conflict-in-package"); |
| 115 | } |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | bool IsChildWithParents(const Package& package) |
| 120 | { |