Branch data Line data Source code
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/truc_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 : :
17 : : /** Helper for PackageTRUCChecks: Returns a vector containing the indices of transactions (within
18 : : * package) that are direct parents of ptx. */
19 : 750 : std::vector<size_t> FindInPackageParents(const Package& package, const CTransactionRef& ptx)
20 : : {
21 : 750 : std::vector<size_t> in_package_parents;
22 : :
23 : 750 : std::set<Txid> possible_parents;
24 [ + + ]: 2152 : for (auto &input : ptx->vin) {
25 [ + - ]: 1402 : possible_parents.insert(input.prevout.hash);
26 : : }
27 : :
28 [ - + + - ]: 5789 : for (size_t i{0}; i < package.size(); ++i) {
29 [ + - ]: 5789 : 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 [ + + ]: 5789 : if (&(*tx) == &(*ptx)) break;
33 [ + + ]: 5039 : if (possible_parents.count(tx->GetHash())) {
34 [ + - ]: 596 : in_package_parents.push_back(i);
35 : : }
36 : : }
37 : 750 : return in_package_parents;
38 : 750 : }
39 : :
40 : : /** Helper for PackageTRUCChecks, storing info for a mempool or package parent. */
41 : : struct ParentInfo {
42 : : /** Txid used to identify this parent by prevout */
43 : : const Txid& m_txid;
44 : : /** Wtxid used for debug string */
45 : : const Wtxid& m_wtxid;
46 : : /** version used to check inheritance of TRUC and non-TRUC */
47 : : decltype(CTransaction::version) m_version;
48 : : /** If parent is in mempool, whether it has any descendants in mempool. */
49 : : bool m_has_mempool_descendant;
50 : :
51 : : ParentInfo() = delete;
52 : 44 : ParentInfo(const Txid& txid, const Wtxid& wtxid, decltype(CTransaction::version) version, bool has_mempool_descendant) :
53 : 44 : m_txid{txid}, m_wtxid{wtxid}, m_version{version},
54 : 44 : m_has_mempool_descendant{has_mempool_descendant}
55 : 44 : {}
56 : : };
57 : :
58 : 750 : std::optional<std::string> PackageTRUCChecks(const CTxMemPool& pool, const CTransactionRef& ptx, int64_t vsize,
59 : : const Package& package,
60 : : const std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef>& mempool_parents)
61 : : {
62 : 750 : AssertLockHeld(pool.cs);
63 : : // This function is specialized for these limits, and must be reimplemented if they ever change.
64 : 750 : static_assert(TRUC_ANCESTOR_LIMIT == 2);
65 : 750 : static_assert(TRUC_DESCENDANT_LIMIT == 2);
66 : :
67 : 750 : const auto in_package_parents{FindInPackageParents(package, ptx)};
68 : :
69 : : // Now we have all parents, so we can start checking TRUC rules.
70 [ + + ]: 750 : if (ptx->version == TRUC_VERSION) {
71 : : // SingleTRUCChecks should have checked this already.
72 [ - + ]: 91 : if (!Assume(vsize <= TRUC_MAX_VSIZE)) {
73 [ # # ]: 0 : return strprintf("version=3 tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
74 [ # # # # : 0 : ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(), vsize, TRUC_MAX_VSIZE);
# # ]
75 : : }
76 : :
77 [ - + - + : 91 : if (mempool_parents.size() + in_package_parents.size() + 1 > TRUC_ANCESTOR_LIMIT) {
+ + ]
78 [ + - ]: 6 : return strprintf("tx %s (wtxid=%s) would have too many ancestors",
79 [ + - + - : 9 : ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString());
+ - ]
80 : : }
81 : :
82 [ + + ]: 88 : if (mempool_parents.size()) {
83 [ + - - + : 3 : if (pool.GetAncestorCount(mempool_parents[0]) + in_package_parents.size() + 1 > TRUC_ANCESTOR_LIMIT) {
- + ]
84 [ # # ]: 0 : return strprintf("tx %s (wtxid=%s) would have too many ancestors",
85 [ # # # # : 0 : ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString());
# # ]
86 : : }
87 : : }
88 : :
89 [ - + - + ]: 88 : const bool has_parent{mempool_parents.size() + in_package_parents.size() > 0};
90 [ + + ]: 88 : if (has_parent) {
91 : : // A TRUC child cannot be too large.
92 [ + + ]: 47 : if (vsize > TRUC_CHILD_MAX_VSIZE) {
93 [ + - ]: 6 : return strprintf("version=3 child tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
94 [ + - + - : 6 : ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
+ - ]
95 : 3 : vsize, TRUC_CHILD_MAX_VSIZE);
96 : : }
97 : :
98 : : // Exactly 1 parent exists, either in mempool or package. Find it.
99 : 88 : const auto parent_info = [&] {
100 [ - + + + ]: 44 : if (mempool_parents.size() > 0) {
101 : 3 : const auto& mempool_parent = &mempool_parents[0].get();
102 : 3 : return ParentInfo{mempool_parent->GetTx().GetHash(),
103 : 3 : mempool_parent->GetTx().GetWitnessHash(),
104 : 3 : mempool_parent->GetTx().version,
105 : 3 : /*has_mempool_descendant=*/pool.GetDescendantCount(*mempool_parent) > 1};
106 : : } else {
107 : 41 : auto& parent_index = in_package_parents.front();
108 : 41 : auto& package_parent = package.at(parent_index);
109 : 41 : return ParentInfo{package_parent->GetHash(),
110 : : package_parent->GetWitnessHash(),
111 : 41 : package_parent->version,
112 : 41 : /*has_mempool_descendant=*/false};
113 : : }
114 [ + - ]: 44 : }();
115 : :
116 : : // If there is a parent, it must have the right version.
117 [ + + ]: 44 : if (parent_info.m_version != TRUC_VERSION) {
118 : 3 : return strprintf("version=3 tx %s (wtxid=%s) cannot spend from non-version=3 tx %s (wtxid=%s)",
119 [ + - + - : 6 : ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
+ - ]
120 [ + - + - : 9 : parent_info.m_txid.ToString(), parent_info.m_wtxid.ToString());
+ - ]
121 : : }
122 : :
123 [ + + ]: 124 : for (const auto& package_tx : package) {
124 : : // Skip same tx.
125 [ + + ]: 90 : if (&(*package_tx) == &(*ptx)) continue;
126 : :
127 [ + + ]: 93 : for (auto& input : package_tx->vin) {
128 : : // Fail if we find another tx with the same parent. We don't check whether the
129 : : // sibling is to-be-replaced (done in SingleTRUCChecks) because these transactions
130 : : // are within the same package.
131 [ + + ]: 50 : if (input.prevout.hash == parent_info.m_txid) {
132 : 4 : return strprintf("tx %s (wtxid=%s) would exceed descendant count limit",
133 [ + - ]: 8 : parent_info.m_txid.ToString(),
134 [ + - + - ]: 12 : parent_info.m_wtxid.ToString());
135 : : }
136 : :
137 : : // This tx can't have both a parent and an in-package child.
138 [ + + ]: 46 : if (input.prevout.hash == ptx->GetHash()) {
139 [ + - ]: 6 : return strprintf("tx %s (wtxid=%s) would have too many ancestors",
140 [ + - + - : 9 : package_tx->GetHash().ToString(), package_tx->GetWitnessHash().ToString());
+ - ]
141 : : }
142 : : }
143 : : }
144 : :
145 [ - + ]: 34 : if (parent_info.m_has_mempool_descendant) {
146 : 0 : return strprintf("tx %s (wtxid=%s) would exceed descendant count limit",
147 [ # # # # : 0 : parent_info.m_txid.ToString(), parent_info.m_wtxid.ToString());
# # ]
148 : : }
149 : : }
150 : : } else {
151 : : // Non-TRUC transactions cannot have TRUC parents.
152 [ + + ]: 677 : for (auto it : mempool_parents) {
153 [ + + ]: 19 : if (it.get().GetTx().version == TRUC_VERSION) {
154 : 1 : return strprintf("non-version=3 tx %s (wtxid=%s) cannot spend from version=3 tx %s (wtxid=%s)",
155 [ + - + - : 2 : ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
+ - ]
156 [ + - + - : 7 : it.get().GetSharedTx()->GetHash().ToString(), it.get().GetSharedTx()->GetWitnessHash().ToString());
+ - + - +
- + - +
- ]
157 : : }
158 : : }
159 [ + + ]: 1198 : for (const auto& index: in_package_parents) {
160 [ + - + + ]: 544 : if (package.at(index)->version == TRUC_VERSION) {
161 : 4 : return strprintf("non-version=3 tx %s (wtxid=%s) cannot spend from version=3 tx %s (wtxid=%s)",
162 [ + - + - ]: 8 : ptx->GetHash().ToString(),
163 [ + - + - ]: 8 : ptx->GetWitnessHash().ToString(),
164 [ + - + - ]: 8 : package.at(index)->GetHash().ToString(),
165 [ + - + - : 12 : package.at(index)->GetWitnessHash().ToString());
+ - ]
166 : : }
167 : : }
168 : : }
169 : 729 : return std::nullopt;
170 : 750 : }
171 : :
172 : 30628 : std::optional<std::pair<std::string, CTransactionRef>> SingleTRUCChecks(const CTxMemPool& pool, const CTransactionRef& ptx,
173 : : const std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef>& mempool_parents,
174 : : const std::set<Txid>& direct_conflicts,
175 : : int64_t vsize)
176 : : {
177 : 30628 : AssertLockHeld(pool.cs);
178 : : // Check TRUC and non-TRUC inheritance.
179 [ + + ]: 36047 : for (const auto& entry_ref : mempool_parents) {
180 [ + + ]: 5424 : const auto& entry = &entry_ref.get();
181 [ + + + + ]: 5424 : if (ptx->version != TRUC_VERSION && entry->GetTx().version == TRUC_VERSION) {
182 : 6 : return std::make_pair(strprintf("non-version=3 tx %s (wtxid=%s) cannot spend from version=3 tx %s (wtxid=%s)",
183 [ + - + - : 6 : ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
+ - ]
184 [ + - + - : 18 : entry->GetSharedTx()->GetHash().ToString(), entry->GetSharedTx()->GetWitnessHash().ToString()),
+ - + - +
- + - +
- ]
185 : 3 : nullptr);
186 [ + + + + ]: 5421 : } else if (ptx->version == TRUC_VERSION && entry->GetTx().version != TRUC_VERSION) {
187 : 4 : return std::make_pair(strprintf("version=3 tx %s (wtxid=%s) cannot spend from non-version=3 tx %s (wtxid=%s)",
188 [ + - + - : 4 : ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
+ - ]
189 [ + - + - : 10 : entry->GetSharedTx()->GetHash().ToString(), entry->GetSharedTx()->GetWitnessHash().ToString()),
+ - + - +
- ]
190 : 2 : nullptr);
191 : : }
192 : : }
193 : :
194 : : // This function is specialized for these limits, and must be reimplemented if they ever change.
195 : 30623 : static_assert(TRUC_ANCESTOR_LIMIT == 2);
196 : 30623 : static_assert(TRUC_DESCENDANT_LIMIT == 2);
197 : :
198 : : // The rest of the rules only apply to transactions with version=3.
199 [ + + ]: 30623 : if (ptx->version != TRUC_VERSION) return std::nullopt;
200 : :
201 [ + + ]: 773 : if (vsize > TRUC_MAX_VSIZE) {
202 : 2 : return std::make_pair(strprintf("version=3 tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
203 [ + - + - ]: 2 : ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(), vsize, TRUC_MAX_VSIZE),
204 : 2 : nullptr);
205 : : }
206 : :
207 : : // Check that TRUC_ANCESTOR_LIMIT would not be violated.
208 [ - + + + ]: 772 : if (mempool_parents.size() + 1 > TRUC_ANCESTOR_LIMIT) {
209 : 2 : return std::make_pair(strprintf("tx %s (wtxid=%s) would have too many ancestors",
210 [ + - + - ]: 2 : ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString()),
211 : 2 : nullptr);
212 : : }
213 : :
214 : : // Remaining checks only pertain to transactions with unconfirmed ancestors.
215 [ + + ]: 771 : if (mempool_parents.size() > 0) {
216 : : // Ensure that the in-mempool parent doesn't have any additional
217 : : // ancestors, as that would also be a violation.
218 [ + + ]: 66 : if (pool.GetAncestorCount(mempool_parents[0]) + 1 > TRUC_ANCESTOR_LIMIT) {
219 : 6 : return std::make_pair(strprintf("tx %s (wtxid=%s) would have too many ancestors",
220 [ + - + - ]: 6 : ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString()),
221 : 6 : nullptr);
222 : : }
223 : : // If this transaction spends TRUC parents, it cannot be too large.
224 [ + + ]: 63 : if (vsize > TRUC_CHILD_MAX_VSIZE) {
225 : 6 : return std::make_pair(strprintf("version=3 child tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
226 [ + - + - ]: 6 : ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(), vsize, TRUC_CHILD_MAX_VSIZE),
227 : 6 : nullptr);
228 : : }
229 : :
230 : : // Check the descendant counts of in-mempool parents.
231 [ + - ]: 60 : const auto& parent_entry = mempool_parents[0].get();
232 : : // If there are any parents, this is the only child allowed. The parent cannot have any
233 : : // other descendants. We handle the possibility of multiple children as that case is
234 : : // possible through a reorg.
235 [ + - ]: 60 : CTxMemPool::setEntries descendants;
236 [ + - ]: 60 : auto parent_it = pool.CalculateDescendants(parent_entry, descendants);
237 : 60 : descendants.erase(parent_it);
238 : : // Don't double-count a transaction that is going to be replaced. This logic assumes that
239 : : // any descendant of the TRUC transaction is a direct child, which makes sense because a
240 : : // TRUC transaction can only have 1 descendant.
241 [ + + + + ]: 88 : const bool child_will_be_replaced = !descendants.empty() &&
242 : 28 : std::any_of(descendants.cbegin(), descendants.cend(),
243 : 32 : [&direct_conflicts](const CTxMemPool::txiter& child){return direct_conflicts.count(child->GetTx().GetHash()) > 0;});
244 [ + - + + : 60 : if (pool.GetDescendantCount(parent_entry) + 1 > TRUC_DESCENDANT_LIMIT && !child_will_be_replaced) {
+ + ]
245 : : // Allow sibling eviction for TRUC transaction: if another child already exists, even if
246 : : // we don't conflict inputs with it, consider evicting it under RBF rules. We rely on TRUC rules
247 : : // only permitting 1 descendant, as otherwise we would need to have logic for deciding
248 : : // which descendant to evict. Skip if this isn't true, e.g. if the transaction has
249 : : // multiple children or the sibling also has descendants due to a reorg.
250 [ + - + + : 39 : const bool consider_sibling_eviction{pool.GetDescendantCount(parent_entry) == 2 &&
- + ]
251 [ + - ]: 18 : pool.GetAncestorCount(**descendants.begin()) == 2};
252 : :
253 : : // Return the sibling if its eviction can be considered. Provide the "descendant count
254 : : // limit" string either way, as the caller may decide not to do sibling eviction.
255 [ + - ]: 60 : return std::make_pair(strprintf("tx %u (wtxid=%s) would exceed descendant count limit",
256 [ + - + - : 63 : parent_entry.GetSharedTx()->GetHash().ToString(),
+ - ]
257 [ + - + - : 63 : parent_entry.GetSharedTx()->GetWitnessHash().ToString()),
+ - ]
258 [ + - + - ]: 81 : consider_sibling_eviction ? (*descendants.begin())->GetSharedTx() : nullptr);
259 : : }
260 : 60 : }
261 : 744 : return std::nullopt;
262 : : }
|