59 const std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef>& mempool_parents)
63 static_assert(TRUC_ANCESTOR_LIMIT == 2);
64 static_assert(TRUC_DESCENDANT_LIMIT == 2);
69 if (ptx->version == TRUC_VERSION) {
71 if (!
Assume(vsize <= TRUC_MAX_VSIZE)) {
72 return strprintf(
"version=3 tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
73 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(), vsize, TRUC_MAX_VSIZE);
76 if (mempool_parents.size() + in_package_parents.size() + 1 > TRUC_ANCESTOR_LIMIT) {
77 return strprintf(
"tx %s (wtxid=%s) would have too many ancestors",
78 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString());
81 if (mempool_parents.size()) {
82 if (pool.
GetAncestorCount(mempool_parents[0]) + in_package_parents.size() + 1 > TRUC_ANCESTOR_LIMIT) {
83 return strprintf(
"tx %s (wtxid=%s) would have too many ancestors",
84 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString());
88 const bool has_parent{mempool_parents.size() + in_package_parents.size() > 0};
91 if (vsize > TRUC_CHILD_MAX_VSIZE) {
92 return strprintf(
"version=3 child tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
93 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
94 vsize, TRUC_CHILD_MAX_VSIZE);
98 const auto parent_info = [&] {
99 if (mempool_parents.size() > 0) {
100 const auto& mempool_parent = &mempool_parents[0].get();
101 return ParentInfo{mempool_parent->GetTx().GetHash(),
102 mempool_parent->GetTx().GetWitnessHash(),
103 mempool_parent->GetTx().version,
106 auto& parent_index = in_package_parents.front();
107 auto& package_parent = package.at(parent_index);
109 package_parent->GetWitnessHash(),
110 package_parent->version,
116 if (parent_info.m_version != TRUC_VERSION) {
117 return strprintf(
"version=3 tx %s (wtxid=%s) cannot spend from non-version=3 tx %s (wtxid=%s)",
118 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
119 parent_info.m_txid.ToString(), parent_info.m_wtxid.ToString());
122 for (
const auto& package_tx : package) {
124 if (&(*package_tx) == &(*ptx))
continue;
126 for (
auto& input : package_tx->vin) {
130 if (input.prevout.hash == parent_info.m_txid) {
131 return strprintf(
"tx %s (wtxid=%s) would exceed descendant count limit",
132 parent_info.m_txid.ToString(),
133 parent_info.m_wtxid.ToString());
137 if (input.prevout.hash == ptx->GetHash()) {
138 return strprintf(
"tx %s (wtxid=%s) would have too many ancestors",
139 package_tx->GetHash().ToString(), package_tx->GetWitnessHash().ToString());
144 if (parent_info.m_has_mempool_descendant) {
145 return strprintf(
"tx %s (wtxid=%s) would exceed descendant count limit",
146 parent_info.m_txid.ToString(), parent_info.m_wtxid.ToString());
151 for (
auto it : mempool_parents) {
152 if (it.get().GetTx().version == TRUC_VERSION) {
153 return strprintf(
"non-version=3 tx %s (wtxid=%s) cannot spend from version=3 tx %s (wtxid=%s)",
154 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
155 it.get().GetSharedTx()->GetHash().ToString(), it.get().GetSharedTx()->GetWitnessHash().ToString());
158 for (
const auto& index: in_package_parents) {
159 if (package.at(index)->version == TRUC_VERSION) {
160 return strprintf(
"non-version=3 tx %s (wtxid=%s) cannot spend from version=3 tx %s (wtxid=%s)",
161 ptx->GetHash().ToString(),
162 ptx->GetWitnessHash().ToString(),
163 package.at(index)->GetHash().ToString(),
164 package.at(index)->GetWitnessHash().ToString());
172 const std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef>& mempool_parents,
173 const std::set<Txid>& direct_conflicts,
178 for (
const auto& entry_ref : mempool_parents) {
179 const auto& entry = &entry_ref.get();
180 if (ptx->version != TRUC_VERSION && entry->GetTx().version == TRUC_VERSION) {
181 return std::make_pair(
strprintf(
"non-version=3 tx %s (wtxid=%s) cannot spend from version=3 tx %s (wtxid=%s)",
182 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
183 entry->GetSharedTx()->GetHash().ToString(), entry->GetSharedTx()->GetWitnessHash().ToString()),
185 }
else if (ptx->version == TRUC_VERSION && entry->GetTx().version != TRUC_VERSION) {
186 return std::make_pair(
strprintf(
"version=3 tx %s (wtxid=%s) cannot spend from non-version=3 tx %s (wtxid=%s)",
187 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
188 entry->GetSharedTx()->GetHash().ToString(), entry->GetSharedTx()->GetWitnessHash().ToString()),
194 static_assert(TRUC_ANCESTOR_LIMIT == 2);
195 static_assert(TRUC_DESCENDANT_LIMIT == 2);
198 if (ptx->version != TRUC_VERSION)
return std::nullopt;
200 if (vsize > TRUC_MAX_VSIZE) {
201 return std::make_pair(
strprintf(
"version=3 tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
202 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(), vsize, TRUC_MAX_VSIZE),
207 if (mempool_parents.size() + 1 > TRUC_ANCESTOR_LIMIT) {
208 return std::make_pair(
strprintf(
"tx %s (wtxid=%s) would have too many ancestors",
209 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString()),
214 if (mempool_parents.size() > 0) {
218 return std::make_pair(
strprintf(
"tx %s (wtxid=%s) would have too many ancestors",
219 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString()),
223 if (vsize > TRUC_CHILD_MAX_VSIZE) {
224 return std::make_pair(
strprintf(
"version=3 child tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
225 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(), vsize, TRUC_CHILD_MAX_VSIZE),
230 const auto& parent_entry = mempool_parents[0].get();
236 descendants.erase(parent_it);
240 const bool child_will_be_replaced = !descendants.empty() &&
241 std::any_of(descendants.cbegin(), descendants.cend(),
242 [&direct_conflicts](
const CTxMemPool::txiter& child){return direct_conflicts.contains(child->GetTx().GetHash());});
243 if (pool.
GetDescendantCount(parent_entry) + 1 > TRUC_DESCENDANT_LIMIT && !child_will_be_replaced) {
254 return std::make_pair(
strprintf(
"tx %u (wtxid=%s) would exceed descendant count limit",
255 parent_entry.GetSharedTx()->GetHash().ToString(),
256 parent_entry.GetSharedTx()->GetWitnessHash().ToString()),
257 consider_sibling_eviction ? (*descendants.begin())->GetSharedTx() :
nullptr);
std::optional< std::pair< std::string, CTransactionRef > > SingleTRUCChecks(const CTxMemPool &pool, const CTransactionRef &ptx, const std::vector< CTxMemPoolEntry::CTxMemPoolEntryRef > &mempool_parents, const std::set< Txid > &direct_conflicts, int64_t vsize)
Must be called for every transaction, even if not TRUC.
std::optional< std::string > PackageTRUCChecks(const CTxMemPool &pool, const CTransactionRef &ptx, int64_t vsize, const Package &package, const std::vector< CTxMemPoolEntry::CTxMemPoolEntryRef > &mempool_parents)
Must be called for every transaction that is submitted within a package, even if not TRUC.