Bitcoin Core  26.1.0
P2P Digital Currency
packages.cpp
Go to the documentation of this file.
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 #include <policy/packages.h>
6 #include <policy/policy.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 bool CheckPackage(const Package& txns, PackageValidationState& state)
19 {
20  const unsigned int package_count = txns.size();
21 
22  if (package_count > MAX_PACKAGE_COUNT) {
23  return state.Invalid(PackageValidationResult::PCKG_POLICY, "package-too-many-transactions");
24  }
25 
26  const int64_t total_weight = std::accumulate(txns.cbegin(), txns.cend(), 0,
27  [](int64_t sum, const auto& tx) { return sum + GetTransactionWeight(*tx); });
28  // If the package only contains 1 tx, it's better to report the policy violation on individual tx weight.
29  if (package_count > 1 && total_weight > MAX_PACKAGE_WEIGHT) {
30  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  std::unordered_set<uint256, SaltedTxidHasher> later_txids;
38  std::transform(txns.cbegin(), txns.cend(), std::inserter(later_txids, later_txids.end()),
39  [](const auto& tx) { return tx->GetHash(); });
40 
41  // Package must not contain any duplicate transactions, which is checked by txid. This also
42  // includes transactions with duplicate wtxids and same-txid-different-witness transactions.
43  if (later_txids.size() != txns.size()) {
44  return state.Invalid(PackageValidationResult::PCKG_POLICY, "package-contains-duplicates");
45  }
46 
47  for (const auto& tx : txns) {
48  for (const auto& input : tx->vin) {
49  if (later_txids.find(input.prevout.hash) != later_txids.end()) {
50  // The parent is a subsequent transaction in the package.
51  return state.Invalid(PackageValidationResult::PCKG_POLICY, "package-not-sorted");
52  }
53  }
54  later_txids.erase(tx->GetHash());
55  }
56 
57  // Don't allow any conflicting transactions, i.e. spending the same inputs, in a package.
58  std::unordered_set<COutPoint, SaltedOutpointHasher> inputs_seen;
59  for (const auto& tx : txns) {
60  for (const auto& input : tx->vin) {
61  if (inputs_seen.find(input.prevout) != inputs_seen.end()) {
62  // This input is also present in another tx in the package.
63  return state.Invalid(PackageValidationResult::PCKG_POLICY, "conflict-in-package");
64  }
65  }
66  // Batch-add all the inputs for a tx at a time. If we added them 1 at a time, we could
67  // catch duplicate inputs within a single tx. This is a more severe, consensus error,
68  // and we want to report that from CheckTransaction instead.
69  std::transform(tx->vin.cbegin(), tx->vin.cend(), std::inserter(inputs_seen, inputs_seen.end()),
70  [](const auto& input) { return input.prevout; });
71  }
72  return true;
73 }
74 
75 bool IsChildWithParents(const Package& package)
76 {
77  assert(std::all_of(package.cbegin(), package.cend(), [](const auto& tx){return tx != nullptr;}));
78  if (package.size() < 2) return false;
79 
80  // The package is expected to be sorted, so the last transaction is the child.
81  const auto& child = package.back();
82  std::unordered_set<uint256, SaltedTxidHasher> input_txids;
83  std::transform(child->vin.cbegin(), child->vin.cend(),
84  std::inserter(input_txids, input_txids.end()),
85  [](const auto& input) { return input.prevout.hash; });
86 
87  // Every transaction must be a parent of the last transaction in the package.
88  return std::all_of(package.cbegin(), package.cend() - 1,
89  [&input_txids](const auto& ptx) { return input_txids.count(ptx->GetHash()) > 0; });
90 }
91 
92 bool IsChildWithParentsTree(const Package& package)
93 {
94  if (!IsChildWithParents(package)) return false;
95  std::unordered_set<uint256, SaltedTxidHasher> parent_txids;
96  std::transform(package.cbegin(), package.cend() - 1, std::inserter(parent_txids, parent_txids.end()),
97  [](const auto& ptx) { return ptx->GetHash(); });
98  // Each parent must not have an input who is one of the other parents.
99  return std::all_of(package.cbegin(), package.cend() - 1, [&](const auto& ptx) {
100  for (const auto& input : ptx->vin) {
101  if (parent_txids.count(input.prevout.hash) > 0) return false;
102  }
103  return true;
104  });
105 }
assert(!tx.IsCoinBase())
The package itself is invalid (e.g. too many transactions).
bool IsChildWithParents(const Package &package)
Context-free check that a package is exactly one child and its parents; not all parents need to be pr...
Definition: packages.cpp:75
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:48
bool IsChildWithParentsTree(const Package &package)
Context-free check that a package IsChildWithParents() and none of the parents depend on each other (...
Definition: packages.cpp:92
bool CheckPackage(const Package &txns, PackageValidationState &state)
Context-free package policy checks:
Definition: packages.cpp:18
static int32_t GetTransactionWeight(const CTransaction &tx)
Definition: validation.h:148
volatile double sum
Definition: examples.cpp:10
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:104
static constexpr uint32_t MAX_PACKAGE_COUNT
Default maximum number of transactions in a package.
Definition: packages.h:17
static constexpr uint32_t MAX_PACKAGE_WEIGHT
Default maximum total weight of transactions in a package in weight to allow for context-less checks...
Definition: packages.h:22