21 std::vector<size_t> in_package_parents;
23 std::set<Txid> possible_parents;
24 for (
auto &input : ptx->vin) {
25 possible_parents.insert(input.prevout.hash);
28 for (
size_t i{0}; i < package.size(); ++i) {
29 const auto& tx = package.at(i);
32 if (&(*tx) == &(*ptx))
break;
33 if (possible_parents.count(tx->GetHash())) {
34 in_package_parents.push_back(i);
37 return in_package_parents;
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());
75 const bool has_parent{mempool_ancestors.size() + in_package_parents.size() > 0};
79 return strprintf(
"v3 child tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
80 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
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 mempool_parent->GetCountWithDescendants() > 1};
94 auto& parent_index = in_package_parents.front();
95 auto& package_parent = package.at(parent_index);
97 package_parent->GetWitnessHash(),
98 package_parent->nVersion,
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());
110 for (
const auto& package_tx : package) {
112 if (&(*package_tx) == &(*ptx))
continue;
114 for (
auto& input : package_tx->vin) {
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());
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());
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());
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());
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());
164 const std::set<Txid>& direct_conflicts,
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()),
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()),
187 if (ptx->nVersion != 3)
return std::nullopt;
191 return std::make_pair(
strprintf(
"tx %s (wtxid=%s) would have too many ancestors",
192 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString()),
197 if (mempool_ancestors.size() > 0) {
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),
206 const auto& parent_entry = *mempool_ancestors.begin();
210 const auto& children = parent_entry->GetMemPoolChildrenConst();
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) {
223 const bool consider_sibling_eviction{parent_entry->GetCountWithDescendants() == 2 &&
224 children.begin()->get().GetCountWithAncestors() == 2};
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);
#define Assume(val)
Assume is the identity function.
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
std::set< txiter, CompareIteratorByHash > setEntries
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
std::shared_ptr< const CTransaction > CTransactionRef
Helper for PackageV3Checks, storing info for a mempool or package parent.
decltype(CTransaction::nVersion) m_version
nVersion used to check inheritance of v3 and non-v3
bool m_has_mempool_descendant
If parent is in mempool, whether it has any descendants in mempool.
const Txid & m_txid
Txid used to identify this parent by prevout.
const Wtxid & m_wtxid
Wtxid used for debug string.
ParentInfo(const Txid &txid, const Wtxid &wtxid, decltype(CTransaction::nVersion) version, bool has_mempool_descendant)
std::vector< size_t > FindInPackageParents(const Package &package, const CTransactionRef &ptx)
Helper for PackageV3Checks: Returns a vector containing the indices of transactions (within package) ...
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.
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.
static constexpr unsigned int V3_DESCENDANT_LIMIT
Maximum number of transactions including an unconfirmed tx and its descendants.
static constexpr int64_t V3_CHILD_MAX_VSIZE
Maximum sigop-adjusted virtual size of a tx which spends from an unconfirmed v3 transaction.
static constexpr unsigned int V3_ANCESTOR_LIMIT
Maximum number of transactions including a V3 tx and all its mempool ancestors.