LCOV - code coverage report
Current view: top level - src/policy - packages.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 36 36 100.0 %
Date: 2026-06-25 07:23:43 Functions: 8 8 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2021 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             : #include <policy/packages.h>
       6             : #include <policy/policy.h>
       7             : #include <primitives/transaction.h>
       8             : #include <uint256.h>
       9             : #include <util/hasher.h>
      10             : 
      11             : #include <algorithm>
      12             : #include <cassert>
      13             : #include <iterator>
      14             : #include <memory>
      15             : #include <numeric>
      16             : #include <unordered_set>
      17             : 
      18         180 : bool CheckPackage(const Package& txns, PackageValidationState& state)
      19             : {
      20         180 :     const unsigned int package_count = txns.size();
      21             : 
      22         180 :     if (package_count > MAX_PACKAGE_COUNT) {
      23           1 :         return state.Invalid(PackageValidationResult::PCKG_POLICY, "package-too-many-transactions");
      24             :     }
      25             : 
      26         179 :     const int64_t total_size = std::accumulate(txns.cbegin(), txns.cend(), 0,
      27        1556 :                                [](int64_t sum, const auto& tx) { return sum + GetVirtualTransactionSize(*tx); });
      28             :     // If the package only contains 1 tx, it's better to report the policy violation on individual tx size.
      29         179 :     if (package_count > 1 && total_size > MAX_PACKAGE_SIZE * 1000) {
      30           1 :         return state.Invalid(PackageValidationResult::PCKG_POLICY, "package-too-large");
      31             :     }
      32             : 
      33             :     // Require the package to be sorted in order of dependency, i.e. parents appear before children.
      34             :     // An unsorted package will fail anyway on missing-inputs, but it's better to quit earlier and
      35             :     // fail on something less ambiguous (missing-inputs could also be an orphan or trying to
      36             :     // spend nonexistent coins).
      37         178 :     std::unordered_set<uint256, SaltedTxidHasher> later_txids;
      38         178 :     std::transform(txns.cbegin(), txns.cend(), std::inserter(later_txids, later_txids.end()),
      39        1552 :                    [](const auto& tx) { return tx->GetHash(); });
      40        1675 :     for (const auto& tx : txns) {
      41        4967 :         for (const auto& input : tx->vin) {
      42        3470 :             if (later_txids.find(input.prevout.hash) != later_txids.end()) {
      43             :                 // The parent is a subsequent transaction in the package.
      44           4 :                 return state.Invalid(PackageValidationResult::PCKG_POLICY, "package-not-sorted");
      45             :             }
      46             :         }
      47        1497 :         later_txids.erase(tx->GetHash());
      48             :     }
      49             : 
      50             :     // Don't allow any conflicting transactions, i.e. spending the same inputs, in a package.
      51         174 :     std::unordered_set<COutPoint, SaltedOutpointHasher> inputs_seen;
      52        1667 :     for (const auto& tx : txns) {
      53        4959 :         for (const auto& input : tx->vin) {
      54        3466 :             if (inputs_seen.find(input.prevout) != inputs_seen.end()) {
      55             :                 // This input is also present in another tx in the package.
      56           4 :                 return state.Invalid(PackageValidationResult::PCKG_POLICY, "conflict-in-package");
      57             :             }
      58             :         }
      59             :         // Batch-add all the inputs for a tx at a time. If we added them 1 at a time, we could
      60             :         // catch duplicate inputs within a single tx.  This is a more severe, consensus error,
      61             :         // and we want to report that from CheckTransaction instead.
      62        1493 :         std::transform(tx->vin.cbegin(), tx->vin.cend(), std::inserter(inputs_seen, inputs_seen.end()),
      63        3462 :                        [](const auto& input) { return input.prevout; });
      64             :     }
      65         170 :     return true;
      66         180 : }
      67             : 
      68          39 : bool IsChildWithParents(const Package& package)
      69             : {
      70         364 :     assert(std::all_of(package.cbegin(), package.cend(), [](const auto& tx){return tx != nullptr;}));
      71          39 :     if (package.size() < 2) return false;
      72             : 
      73             :     // The package is expected to be sorted, so the last transaction is the child.
      74          39 :     const auto& child = package.back();
      75          39 :     std::unordered_set<uint256, SaltedTxidHasher> input_txids;
      76          39 :     std::transform(child->vin.cbegin(), child->vin.cend(),
      77          39 :                    std::inserter(input_txids, input_txids.end()),
      78         212 :                    [](const auto& input) { return input.prevout.hash; });
      79             : 
      80             :     // Every transaction must be a parent of the last transaction in the package.
      81          78 :     return std::all_of(package.cbegin(), package.cend() - 1,
      82         225 :                        [&input_txids](const auto& ptx) { return input_txids.count(ptx->GetHash()) > 0; });
      83          39 : }

Generated by: LCOV version 1.16