Bitcoin Core  27.99.0
P2P Digital Currency
v3_policy.cpp
Go to the documentation of this file.
1 // Copyright (c) 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/v3_policy.h>
6 
7 #include <coins.h>
8 #include <consensus/amount.h>
9 #include <logging.h>
10 #include <tinyformat.h>
11 #include <util/check.h>
12 
13 #include <algorithm>
14 #include <numeric>
15 #include <vector>
16 
19 std::vector<size_t> FindInPackageParents(const Package& package, const CTransactionRef& ptx)
20 {
21  std::vector<size_t> in_package_parents;
22 
23  std::set<Txid> possible_parents;
24  for (auto &input : ptx->vin) {
25  possible_parents.insert(input.prevout.hash);
26  }
27 
28  for (size_t i{0}; i < package.size(); ++i) {
29  const auto& tx = package.at(i);
30  // We assume the package is sorted, so that we don't need to continue
31  // looking past the transaction itself.
32  if (&(*tx) == &(*ptx)) break;
33  if (possible_parents.count(tx->GetHash())) {
34  in_package_parents.push_back(i);
35  }
36  }
37  return in_package_parents;
38 }
39 
41 struct ParentInfo {
43  const Txid& m_txid;
45  const Wtxid& m_wtxid;
50 
51  ParentInfo() = delete;
52  ParentInfo(const Txid& txid, const Wtxid& wtxid, decltype(CTransaction::nVersion) version, bool has_mempool_descendant) :
53  m_txid{txid}, m_wtxid{wtxid}, m_version{version},
54  m_has_mempool_descendant{has_mempool_descendant}
55  {}
56 };
57 
58 std::optional<std::string> PackageV3Checks(const CTransactionRef& ptx, int64_t vsize,
59  const Package& package,
60  const CTxMemPool::setEntries& mempool_ancestors)
61 {
62  // This function is specialized for these limits, and must be reimplemented if they ever change.
63  static_assert(V3_ANCESTOR_LIMIT == 2);
64  static_assert(V3_DESCENDANT_LIMIT == 2);
65 
66  const auto in_package_parents{FindInPackageParents(package, ptx)};
67 
68  // Now we have all ancestors, so we can start checking v3 rules.
69  if (ptx->nVersion == 3) {
70  if (mempool_ancestors.size() + in_package_parents.size() + 1 > V3_ANCESTOR_LIMIT) {
71  return strprintf("tx %s (wtxid=%s) would have too many ancestors",
72  ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString());
73  }
74 
75  const bool has_parent{mempool_ancestors.size() + in_package_parents.size() > 0};
76  if (has_parent) {
77  // A v3 child cannot be too large.
78  if (vsize > V3_CHILD_MAX_VSIZE) {
79  return strprintf("v3 child tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
80  ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
81  vsize, V3_CHILD_MAX_VSIZE);
82  }
83 
84  // Exactly 1 parent exists, either in mempool or package. Find it.
85  const auto parent_info = [&] {
86  if (mempool_ancestors.size() > 0) {
87  auto& mempool_parent = *mempool_ancestors.begin();
88  Assume(mempool_parent->GetCountWithDescendants() == 1);
89  return ParentInfo{mempool_parent->GetTx().GetHash(),
90  mempool_parent->GetTx().GetWitnessHash(),
91  mempool_parent->GetTx().nVersion,
92  /*has_mempool_descendant=*/mempool_parent->GetCountWithDescendants() > 1};
93  } else {
94  auto& parent_index = in_package_parents.front();
95  auto& package_parent = package.at(parent_index);
96  return ParentInfo{package_parent->GetHash(),
97  package_parent->GetWitnessHash(),
98  package_parent->nVersion,
99  /*has_mempool_descendant=*/false};
100  }
101  }();
102 
103  // If there is a parent, it must have the right version.
104  if (parent_info.m_version != 3) {
105  return strprintf("v3 tx %s (wtxid=%s) cannot spend from non-v3 tx %s (wtxid=%s)",
106  ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
107  parent_info.m_txid.ToString(), parent_info.m_wtxid.ToString());
108  }
109 
110  for (const auto& package_tx : package) {
111  // Skip same tx.
112  if (&(*package_tx) == &(*ptx)) continue;
113 
114  for (auto& input : package_tx->vin) {
115  // Fail if we find another tx with the same parent. We don't check whether the
116  // sibling is to-be-replaced (done in SingleV3Checks) because these transactions
117  // are within the same package.
118  if (input.prevout.hash == parent_info.m_txid) {
119  return strprintf("tx %s (wtxid=%s) would exceed descendant count limit",
120  parent_info.m_txid.ToString(),
121  parent_info.m_wtxid.ToString());
122  }
123 
124  // This tx can't have both a parent and an in-package child.
125  if (input.prevout.hash == ptx->GetHash()) {
126  return strprintf("tx %s (wtxid=%s) would have too many ancestors",
127  package_tx->GetHash().ToString(), package_tx->GetWitnessHash().ToString());
128  }
129  }
130  }
131 
132  // It shouldn't be possible to have any mempool siblings at this point. SingleV3Checks
133  // catches mempool siblings and sibling eviction is not extended to packages. Also, if the package consists of connected transactions,
134  // any tx having a mempool ancestor would mean the package exceeds ancestor limits.
135  if (!Assume(!parent_info.m_has_mempool_descendant)) {
136  return strprintf("tx %s (wtxid=%s) would exceed descendant count limit",
137  parent_info.m_txid.ToString(), parent_info.m_wtxid.ToString());
138  }
139  }
140  } else {
141  // Non-v3 transactions cannot have v3 parents.
142  for (auto it : mempool_ancestors) {
143  if (it->GetTx().nVersion == 3) {
144  return strprintf("non-v3 tx %s (wtxid=%s) cannot spend from v3 tx %s (wtxid=%s)",
145  ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
146  it->GetSharedTx()->GetHash().ToString(), it->GetSharedTx()->GetWitnessHash().ToString());
147  }
148  }
149  for (const auto& index: in_package_parents) {
150  if (package.at(index)->nVersion == 3) {
151  return strprintf("non-v3 tx %s (wtxid=%s) cannot spend from v3 tx %s (wtxid=%s)",
152  ptx->GetHash().ToString(),
153  ptx->GetWitnessHash().ToString(),
154  package.at(index)->GetHash().ToString(),
155  package.at(index)->GetWitnessHash().ToString());
156  }
157  }
158  }
159  return std::nullopt;
160 }
161 
162 std::optional<std::pair<std::string, CTransactionRef>> SingleV3Checks(const CTransactionRef& ptx,
163  const CTxMemPool::setEntries& mempool_ancestors,
164  const std::set<Txid>& direct_conflicts,
165  int64_t vsize)
166 {
167  // Check v3 and non-v3 inheritance.
168  for (const auto& entry : mempool_ancestors) {
169  if (ptx->nVersion != 3 && entry->GetTx().nVersion == 3) {
170  return std::make_pair(strprintf("non-v3 tx %s (wtxid=%s) cannot spend from v3 tx %s (wtxid=%s)",
171  ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
172  entry->GetSharedTx()->GetHash().ToString(), entry->GetSharedTx()->GetWitnessHash().ToString()),
173  nullptr);
174  } else if (ptx->nVersion == 3 && entry->GetTx().nVersion != 3) {
175  return std::make_pair(strprintf("v3 tx %s (wtxid=%s) cannot spend from non-v3 tx %s (wtxid=%s)",
176  ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
177  entry->GetSharedTx()->GetHash().ToString(), entry->GetSharedTx()->GetWitnessHash().ToString()),
178  nullptr);
179  }
180  }
181 
182  // This function is specialized for these limits, and must be reimplemented if they ever change.
183  static_assert(V3_ANCESTOR_LIMIT == 2);
184  static_assert(V3_DESCENDANT_LIMIT == 2);
185 
186  // The rest of the rules only apply to transactions with nVersion=3.
187  if (ptx->nVersion != 3) return std::nullopt;
188 
189  // Check that V3_ANCESTOR_LIMIT would not be violated.
190  if (mempool_ancestors.size() + 1 > V3_ANCESTOR_LIMIT) {
191  return std::make_pair(strprintf("tx %s (wtxid=%s) would have too many ancestors",
192  ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString()),
193  nullptr);
194  }
195 
196  // Remaining checks only pertain to transactions with unconfirmed ancestors.
197  if (mempool_ancestors.size() > 0) {
198  // If this transaction spends V3 parents, it cannot be too large.
199  if (vsize > V3_CHILD_MAX_VSIZE) {
200  return std::make_pair(strprintf("v3 child tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
201  ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(), vsize, V3_CHILD_MAX_VSIZE),
202  nullptr);
203  }
204 
205  // Check the descendant counts of in-mempool ancestors.
206  const auto& parent_entry = *mempool_ancestors.begin();
207  // If there are any ancestors, this is the only child allowed. The parent cannot have any
208  // other descendants. We handle the possibility of multiple children as that case is
209  // possible through a reorg.
210  const auto& children = parent_entry->GetMemPoolChildrenConst();
211  // Don't double-count a transaction that is going to be replaced. This logic assumes that
212  // any descendant of the V3 transaction is a direct child, which makes sense because a V3
213  // transaction can only have 1 descendant.
214  const bool child_will_be_replaced = !children.empty() &&
215  std::any_of(children.cbegin(), children.cend(),
216  [&direct_conflicts](const CTxMemPoolEntry& child){return direct_conflicts.count(child.GetTx().GetHash()) > 0;});
217  if (parent_entry->GetCountWithDescendants() + 1 > V3_DESCENDANT_LIMIT && !child_will_be_replaced) {
218  // Allow sibling eviction for v3 transaction: if another child already exists, even if
219  // we don't conflict inputs with it, consider evicting it under RBF rules. We rely on v3 rules
220  // only permitting 1 descendant, as otherwise we would need to have logic for deciding
221  // which descendant to evict. Skip if this isn't true, e.g. if the transaction has
222  // multiple children or the sibling also has descendants due to a reorg.
223  const bool consider_sibling_eviction{parent_entry->GetCountWithDescendants() == 2 &&
224  children.begin()->get().GetCountWithAncestors() == 2};
225 
226  // Return the sibling if its eviction can be considered. Provide the "descendant count
227  // limit" string either way, as the caller may decide not to do sibling eviction.
228  return std::make_pair(strprintf("tx %u (wtxid=%s) would exceed descendant count limit",
229  parent_entry->GetSharedTx()->GetHash().ToString(),
230  parent_entry->GetSharedTx()->GetWitnessHash().ToString()),
231  consider_sibling_eviction ? children.begin()->get().GetSharedTx() : nullptr);
232  }
233  }
234  return std::nullopt;
235 }
#define Assume(val)
Assume is the identity function.
Definition: check.h:89
const int32_t nVersion
Definition: transaction.h:308
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:66
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:396
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:50
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
Helper for PackageV3Checks, storing info for a mempool or package parent.
Definition: v3_policy.cpp:41
ParentInfo()=delete
decltype(CTransaction::nVersion) m_version
nVersion used to check inheritance of v3 and non-v3
Definition: v3_policy.cpp:47
bool m_has_mempool_descendant
If parent is in mempool, whether it has any descendants in mempool.
Definition: v3_policy.cpp:49
const Txid & m_txid
Txid used to identify this parent by prevout.
Definition: v3_policy.cpp:43
const Wtxid & m_wtxid
Wtxid used for debug string.
Definition: v3_policy.cpp:45
ParentInfo(const Txid &txid, const Wtxid &wtxid, decltype(CTransaction::nVersion) version, bool has_mempool_descendant)
Definition: v3_policy.cpp:52
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1162
std::vector< size_t > FindInPackageParents(const Package &package, const CTransactionRef &ptx)
Helper for PackageV3Checks: Returns a vector containing the indices of transactions (within package) ...
Definition: v3_policy.cpp:19
std::optional< std::string > PackageV3Checks(const CTransactionRef &ptx, int64_t vsize, const Package &package, const CTxMemPool::setEntries &mempool_ancestors)
Must be called for every transaction that is submitted within a package, even if not v3.
Definition: v3_policy.cpp:58
std::optional< std::pair< std::string, CTransactionRef > > SingleV3Checks(const CTransactionRef &ptx, const CTxMemPool::setEntries &mempool_ancestors, const std::set< Txid > &direct_conflicts, int64_t vsize)
Must be called for every transaction, even if not v3.
Definition: v3_policy.cpp:162
static constexpr unsigned int V3_DESCENDANT_LIMIT
Maximum number of transactions including an unconfirmed tx and its descendants.
Definition: v3_policy.h:23
static constexpr int64_t V3_CHILD_MAX_VSIZE
Maximum sigop-adjusted virtual size of a tx which spends from an unconfirmed v3 transaction.
Definition: v3_policy.h:28
static constexpr unsigned int V3_ANCESTOR_LIMIT
Maximum number of transactions including a V3 tx and all its mempool ancestors.
Definition: v3_policy.h:25