/root/bitcoin/src/txmempool.cpp
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-present The Bitcoin Core developers |
3 | | // Distributed under the MIT software license, see the accompanying |
4 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | | |
6 | | #include <txmempool.h> |
7 | | |
8 | | #include <chain.h> |
9 | | #include <coins.h> |
10 | | #include <common/system.h> |
11 | | #include <consensus/consensus.h> |
12 | | #include <consensus/tx_verify.h> |
13 | | #include <consensus/validation.h> |
14 | | #include <policy/policy.h> |
15 | | #include <policy/settings.h> |
16 | | #include <random.h> |
17 | | #include <tinyformat.h> |
18 | | #include <util/check.h> |
19 | | #include <util/feefrac.h> |
20 | | #include <util/log.h> |
21 | | #include <util/moneystr.h> |
22 | | #include <util/overflow.h> |
23 | | #include <util/result.h> |
24 | | #include <util/time.h> |
25 | | #include <util/trace.h> |
26 | | #include <util/translation.h> |
27 | | #include <validationinterface.h> |
28 | | |
29 | | #include <algorithm> |
30 | | #include <cmath> |
31 | | #include <numeric> |
32 | | #include <optional> |
33 | | #include <ranges> |
34 | | #include <string_view> |
35 | | #include <utility> |
36 | | |
37 | | TRACEPOINT_SEMAPHORE(mempool, added); |
38 | | TRACEPOINT_SEMAPHORE(mempool, removed); |
39 | | |
40 | | bool TestLockPointValidity(CChain& active_chain, const LockPoints& lp) |
41 | 0 | { |
42 | 0 | AssertLockHeld(cs_main); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
43 | | // If there are relative lock times then the maxInputBlock will be set |
44 | | // If there are no relative lock times, the LockPoints don't depend on the chain |
45 | 0 | if (lp.maxInputBlock) { |
46 | | // Check whether active_chain is an extension of the block at which the LockPoints |
47 | | // calculation was valid. If not LockPoints are no longer valid |
48 | 0 | if (!active_chain.Contains(lp.maxInputBlock)) { |
49 | 0 | return false; |
50 | 0 | } |
51 | 0 | } |
52 | | |
53 | | // LockPoints still valid |
54 | 0 | return true; |
55 | 0 | } |
56 | | |
57 | | std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> CTxMemPool::GetChildren(const CTxMemPoolEntry& entry) const |
58 | 0 | { |
59 | 0 | std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> ret; |
60 | 0 | const auto& hash = entry.GetTx().GetHash(); |
61 | 0 | { |
62 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
63 | 0 | auto iter = mapNextTx.lower_bound(COutPoint(hash, 0)); |
64 | 0 | for (; iter != mapNextTx.end() && iter->first->hash == hash; ++iter) { |
65 | 0 | ret.emplace_back(*(iter->second)); |
66 | 0 | } |
67 | 0 | } |
68 | 0 | std::ranges::sort(ret, CompareIteratorByHash{}); |
69 | 0 | auto removed = std::ranges::unique(ret, [](auto& a, auto& b) noexcept { return &a.get() == &b.get(); }); |
70 | 0 | ret.erase(removed.begin(), removed.end()); |
71 | 0 | return ret; |
72 | 0 | } |
73 | | |
74 | | std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> CTxMemPool::GetParents(const CTxMemPoolEntry& entry) const |
75 | 0 | { |
76 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
77 | 0 | std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> ret; |
78 | 0 | std::set<Txid> inputs; |
79 | 0 | for (const auto& txin : entry.GetTx().vin) { |
80 | 0 | inputs.insert(txin.prevout.hash); |
81 | 0 | } |
82 | 0 | for (const auto& hash : inputs) { |
83 | 0 | std::optional<txiter> piter = GetIter(hash); |
84 | 0 | if (piter) { |
85 | 0 | ret.emplace_back(**piter); |
86 | 0 | } |
87 | 0 | } |
88 | 0 | return ret; |
89 | 0 | } |
90 | | |
91 | | void CTxMemPool::UpdateTransactionsFromBlock(const std::vector<Txid>& vHashesToUpdate) |
92 | 0 | { |
93 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
94 | | |
95 | | // Iterate in reverse, so that whenever we are looking at a transaction |
96 | | // we are sure that all in-mempool descendants have already been processed. |
97 | 0 | for (const Txid& hash : vHashesToUpdate | std::views::reverse) { |
98 | | // calculate children from mapNextTx |
99 | 0 | txiter it = mapTx.find(hash); |
100 | 0 | if (it == mapTx.end()) { |
101 | 0 | continue; |
102 | 0 | } |
103 | 0 | auto iter = mapNextTx.lower_bound(COutPoint(hash, 0)); |
104 | 0 | { |
105 | 0 | for (; iter != mapNextTx.end() && iter->first->hash == hash; ++iter) { |
106 | 0 | txiter childIter = iter->second; |
107 | 0 | assert(childIter != mapTx.end()); |
108 | | // Add dependencies that are discovered between transactions in the |
109 | | // block and transactions that were in the mempool to txgraph. |
110 | 0 | m_txgraph->AddDependency(/*parent=*/*it, /*child=*/*childIter); |
111 | 0 | } |
112 | 0 | } |
113 | 0 | } |
114 | | |
115 | 0 | auto txs_to_remove = m_txgraph->Trim(); // Enforce cluster size limits. |
116 | 0 | for (auto txptr : txs_to_remove) { |
117 | 0 | const CTxMemPoolEntry& entry = *(static_cast<const CTxMemPoolEntry*>(txptr)); |
118 | 0 | removeUnchecked(mapTx.iterator_to(entry), MemPoolRemovalReason::SIZELIMIT); |
119 | 0 | } |
120 | 0 | } |
121 | | |
122 | | bool CTxMemPool::HasDescendants(const Txid& txid) const |
123 | 0 | { |
124 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
125 | 0 | auto entry = GetEntry(txid); |
126 | 0 | if (!entry) return false; |
127 | 0 | return m_txgraph->GetDescendants(*entry, TxGraph::Level::MAIN).size() > 1; |
128 | 0 | } |
129 | | |
130 | | CTxMemPool::setEntries CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry) const |
131 | 0 | { |
132 | 0 | auto ancestors = m_txgraph->GetAncestors(entry, TxGraph::Level::MAIN); |
133 | 0 | setEntries ret; |
134 | 0 | if (ancestors.size() > 0) { |
135 | 0 | for (auto ancestor : ancestors) { |
136 | 0 | if (ancestor != &entry) { |
137 | 0 | ret.insert(mapTx.iterator_to(static_cast<const CTxMemPoolEntry&>(*ancestor))); |
138 | 0 | } |
139 | 0 | } |
140 | 0 | return ret; |
141 | 0 | } |
142 | | |
143 | | // If we didn't get anything back, the transaction is not in the graph. |
144 | | // Find each parent and call GetAncestors on each. |
145 | 0 | setEntries staged_parents; |
146 | 0 | const CTransaction &tx = entry.GetTx(); |
147 | | |
148 | | // Get parents of this transaction that are in the mempool |
149 | 0 | for (unsigned int i = 0; i < tx.vin.size(); i++) { |
150 | 0 | std::optional<txiter> piter = GetIter(tx.vin[i].prevout.hash); |
151 | 0 | if (piter) { |
152 | 0 | staged_parents.insert(*piter); |
153 | 0 | } |
154 | 0 | } |
155 | |
|
156 | 0 | for (const auto& parent : staged_parents) { |
157 | 0 | auto parent_ancestors = m_txgraph->GetAncestors(*parent, TxGraph::Level::MAIN); |
158 | 0 | for (auto ancestor : parent_ancestors) { |
159 | 0 | ret.insert(mapTx.iterator_to(static_cast<const CTxMemPoolEntry&>(*ancestor))); |
160 | 0 | } |
161 | 0 | } |
162 | |
|
163 | 0 | return ret; |
164 | 0 | } |
165 | | |
166 | | static CTxMemPool::Options&& Flatten(CTxMemPool::Options&& opts, bilingual_str& error) |
167 | 0 | { |
168 | 0 | opts.check_ratio = std::clamp<int>(opts.check_ratio, 0, 1'000'000); |
169 | 0 | int64_t cluster_limit_bytes = opts.limits.cluster_size_vbytes * 40; |
170 | 0 | if (opts.max_size_bytes < 0 || (opts.max_size_bytes > 0 && opts.max_size_bytes < cluster_limit_bytes)) { |
171 | 0 | error = strprintf(_("-maxmempool must be at least %d MB"), std::ceil(cluster_limit_bytes / 1'000'000.0));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
172 | 0 | } |
173 | 0 | return std::move(opts); |
174 | 0 | } |
175 | | |
176 | | CTxMemPool::CTxMemPool(Options opts, bilingual_str& error) |
177 | 0 | : m_opts{Flatten(std::move(opts), error)} |
178 | 0 | { |
179 | 0 | m_txgraph = MakeTxGraph( |
180 | 0 | /*max_cluster_count=*/m_opts.limits.cluster_count, |
181 | 0 | /*max_cluster_size=*/m_opts.limits.cluster_size_vbytes * WITNESS_SCALE_FACTOR, |
182 | 0 | /*acceptable_cost=*/ACCEPTABLE_COST, |
183 | 0 | /*fallback_order=*/[&](const TxGraph::Ref& a, const TxGraph::Ref& b) noexcept { |
184 | 0 | const Txid& txid_a = static_cast<const CTxMemPoolEntry&>(a).GetTx().GetHash(); |
185 | 0 | const Txid& txid_b = static_cast<const CTxMemPoolEntry&>(b).GetTx().GetHash(); |
186 | 0 | return txid_a <=> txid_b; |
187 | 0 | }); |
188 | 0 | } |
189 | | |
190 | | bool CTxMemPool::isSpent(const COutPoint& outpoint) const |
191 | 0 | { |
192 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
193 | 0 | return mapNextTx.count(outpoint); |
194 | 0 | } |
195 | | |
196 | | unsigned int CTxMemPool::GetTransactionsUpdated() const |
197 | 0 | { |
198 | 0 | return nTransactionsUpdated; |
199 | 0 | } |
200 | | |
201 | | void CTxMemPool::AddTransactionsUpdated(unsigned int n) |
202 | 0 | { |
203 | 0 | nTransactionsUpdated += n; |
204 | 0 | } |
205 | | |
206 | | void CTxMemPool::Apply(ChangeSet* changeset) |
207 | 0 | { |
208 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
209 | 0 | m_txgraph->CommitStaging(); |
210 | |
|
211 | 0 | RemoveStaged(changeset->m_to_remove, MemPoolRemovalReason::REPLACED); |
212 | |
|
213 | 0 | for (size_t i=0; i<changeset->m_entry_vec.size(); ++i) { |
214 | 0 | auto tx_entry = changeset->m_entry_vec[i]; |
215 | | // First splice this entry into mapTx. |
216 | 0 | auto node_handle = changeset->m_to_add.extract(tx_entry); |
217 | 0 | auto result = mapTx.insert(std::move(node_handle)); |
218 | |
|
219 | 0 | Assume(result.inserted); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
220 | 0 | txiter it = result.position; |
221 | |
|
222 | 0 | addNewTransaction(it); |
223 | 0 | } |
224 | 0 | if (!m_txgraph->DoWork(/*max_cost=*/POST_CHANGE_COST)) { |
225 | 0 | LogDebug(BCLog::MEMPOOL, "Mempool in non-optimal ordering after addition(s)."); Line | Count | Source | 115 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 106 | 0 | do { \ | 107 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 108 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 109 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 110 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 89 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 111 | 0 | } \ | 112 | 0 | } while (0) |
|
|
226 | 0 | } |
227 | 0 | } |
228 | | |
229 | | void CTxMemPool::addNewTransaction(CTxMemPool::txiter newit) |
230 | 0 | { |
231 | 0 | const CTxMemPoolEntry& entry = *newit; |
232 | | |
233 | | // Update cachedInnerUsage to include contained transaction's usage. |
234 | | // (When we update the entry for in-mempool parents, memory usage will be |
235 | | // further updated.) |
236 | 0 | cachedInnerUsage += entry.DynamicMemoryUsage(); |
237 | |
|
238 | 0 | const CTransaction& tx = newit->GetTx(); |
239 | 0 | for (unsigned int i = 0; i < tx.vin.size(); i++) { |
240 | 0 | mapNextTx.insert(std::make_pair(&tx.vin[i].prevout, newit)); |
241 | 0 | } |
242 | | // Don't bother worrying about child transactions of this one. |
243 | | // Normal case of a new transaction arriving is that there can't be any |
244 | | // children, because such children would be orphans. |
245 | | // An exception to that is if a transaction enters that used to be in a block. |
246 | | // In that case, our disconnect block logic will call UpdateTransactionsFromBlock |
247 | | // to clean up the mess we're leaving here. |
248 | |
|
249 | 0 | nTransactionsUpdated++; |
250 | 0 | totalTxSize += entry.GetTxSize(); |
251 | 0 | m_total_fee += entry.GetFee(); |
252 | |
|
253 | 0 | txns_randomized.emplace_back(tx.GetWitnessHash(), newit); |
254 | 0 | newit->idx_randomized = txns_randomized.size() - 1; |
255 | |
|
256 | 0 | TRACEPOINT(mempool, added, |
257 | 0 | entry.GetTx().GetHash().data(), |
258 | 0 | entry.GetTxSize(), |
259 | 0 | entry.GetFee() |
260 | 0 | ); |
261 | 0 | } |
262 | | |
263 | | void CTxMemPool::removeUnchecked(txiter it, MemPoolRemovalReason reason) |
264 | 0 | { |
265 | | // We increment mempool sequence value no matter removal reason |
266 | | // even if not directly reported below. |
267 | 0 | uint64_t mempool_sequence = GetAndIncrementSequence(); |
268 | |
|
269 | 0 | if (reason != MemPoolRemovalReason::BLOCK && m_opts.signals) { |
270 | | // Notify clients that a transaction has been removed from the mempool |
271 | | // for any reason except being included in a block. Clients interested |
272 | | // in transactions included in blocks can subscribe to the BlockConnected |
273 | | // notification. |
274 | 0 | m_opts.signals->TransactionRemovedFromMempool(it->GetSharedTx(), reason, mempool_sequence); |
275 | 0 | } |
276 | 0 | TRACEPOINT(mempool, removed, |
277 | 0 | it->GetTx().GetHash().data(), |
278 | 0 | RemovalReasonToString(reason).c_str(), |
279 | 0 | it->GetTxSize(), |
280 | 0 | it->GetFee(), |
281 | 0 | std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(it->GetTime()).count() |
282 | 0 | ); |
283 | |
|
284 | 0 | for (const CTxIn& txin : it->GetTx().vin) |
285 | 0 | mapNextTx.erase(txin.prevout); |
286 | |
|
287 | 0 | RemoveUnbroadcastTx(it->GetTx().GetHash(), true /* add logging because unchecked */); |
288 | |
|
289 | 0 | if (txns_randomized.size() > 1) { |
290 | | // Remove entry from txns_randomized by replacing it with the back and deleting the back. |
291 | 0 | txns_randomized[it->idx_randomized] = std::move(txns_randomized.back()); |
292 | 0 | txns_randomized[it->idx_randomized].second->idx_randomized = it->idx_randomized; |
293 | 0 | txns_randomized.pop_back(); |
294 | 0 | if (txns_randomized.size() * 2 < txns_randomized.capacity()) { |
295 | 0 | txns_randomized.shrink_to_fit(); |
296 | 0 | } |
297 | 0 | } else { |
298 | 0 | txns_randomized.clear(); |
299 | 0 | } |
300 | |
|
301 | 0 | totalTxSize -= it->GetTxSize(); |
302 | 0 | m_total_fee -= it->GetFee(); |
303 | 0 | cachedInnerUsage -= it->DynamicMemoryUsage(); |
304 | 0 | mapTx.erase(it); |
305 | 0 | nTransactionsUpdated++; |
306 | 0 | } |
307 | | |
308 | | // Calculates descendants of given entry and adds to setDescendants. |
309 | | void CTxMemPool::CalculateDescendants(txiter entryit, setEntries& setDescendants) const |
310 | 0 | { |
311 | 0 | (void)CalculateDescendants(*entryit, setDescendants); |
312 | 0 | return; |
313 | 0 | } |
314 | | |
315 | | CTxMemPool::txiter CTxMemPool::CalculateDescendants(const CTxMemPoolEntry& entry, setEntries& setDescendants) const |
316 | 0 | { |
317 | 0 | for (auto tx : m_txgraph->GetDescendants(entry, TxGraph::Level::MAIN)) { |
318 | 0 | setDescendants.insert(mapTx.iterator_to(static_cast<const CTxMemPoolEntry&>(*tx))); |
319 | 0 | } |
320 | 0 | return mapTx.iterator_to(entry); |
321 | 0 | } |
322 | | |
323 | | void CTxMemPool::removeRecursive(CTxMemPool::txiter to_remove, MemPoolRemovalReason reason) |
324 | 0 | { |
325 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
326 | 0 | Assume(!m_have_changeset); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
327 | 0 | auto descendants = m_txgraph->GetDescendants(*to_remove, TxGraph::Level::MAIN); |
328 | 0 | for (auto tx: descendants) { |
329 | 0 | removeUnchecked(mapTx.iterator_to(static_cast<const CTxMemPoolEntry&>(*tx)), reason); |
330 | 0 | } |
331 | 0 | } |
332 | | |
333 | | void CTxMemPool::removeRecursive(const CTransaction &origTx, MemPoolRemovalReason reason) |
334 | 0 | { |
335 | | // Remove transaction from memory pool |
336 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
337 | 0 | Assume(!m_have_changeset); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
338 | 0 | txiter origit = mapTx.find(origTx.GetHash()); |
339 | 0 | if (origit != mapTx.end()) { |
340 | 0 | removeRecursive(origit, reason); |
341 | 0 | } else { |
342 | | // When recursively removing but origTx isn't in the mempool |
343 | | // be sure to remove any descendants that are in the pool. This can |
344 | | // happen during chain re-orgs if origTx isn't re-accepted into |
345 | | // the mempool for any reason. |
346 | 0 | auto iter = mapNextTx.lower_bound(COutPoint(origTx.GetHash(), 0)); |
347 | 0 | std::vector<const TxGraph::Ref*> to_remove; |
348 | 0 | while (iter != mapNextTx.end() && iter->first->hash == origTx.GetHash()) { |
349 | 0 | to_remove.emplace_back(&*(iter->second)); |
350 | 0 | ++iter; |
351 | 0 | } |
352 | 0 | auto all_removes = m_txgraph->GetDescendantsUnion(to_remove, TxGraph::Level::MAIN); |
353 | 0 | for (auto ref : all_removes) { |
354 | 0 | auto tx = mapTx.iterator_to(static_cast<const CTxMemPoolEntry&>(*ref)); |
355 | 0 | removeUnchecked(tx, reason); |
356 | 0 | } |
357 | 0 | } |
358 | 0 | } |
359 | | |
360 | | void CTxMemPool::removeForReorg(CChain& chain, std::function<bool(txiter)> check_final_and_mature) |
361 | 0 | { |
362 | | // Remove transactions spending a coinbase which are now immature and no-longer-final transactions |
363 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
364 | 0 | AssertLockHeld(::cs_main); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
365 | 0 | Assume(!m_have_changeset); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
366 | |
|
367 | 0 | std::vector<const TxGraph::Ref*> to_remove; |
368 | 0 | for (txiter it = mapTx.begin(); it != mapTx.end(); it++) { |
369 | 0 | if (check_final_and_mature(it)) { |
370 | 0 | to_remove.emplace_back(&*it); |
371 | 0 | } |
372 | 0 | } |
373 | |
|
374 | 0 | auto all_to_remove = m_txgraph->GetDescendantsUnion(to_remove, TxGraph::Level::MAIN); |
375 | |
|
376 | 0 | for (auto ref : all_to_remove) { |
377 | 0 | auto it = mapTx.iterator_to(static_cast<const CTxMemPoolEntry&>(*ref)); |
378 | 0 | removeUnchecked(it, MemPoolRemovalReason::REORG); |
379 | 0 | } |
380 | 0 | for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) { |
381 | 0 | assert(TestLockPointValidity(chain, it->GetLockPoints())); |
382 | 0 | } |
383 | 0 | if (!m_txgraph->DoWork(/*max_cost=*/POST_CHANGE_COST)) { |
384 | 0 | LogDebug(BCLog::MEMPOOL, "Mempool in non-optimal ordering after reorg."); Line | Count | Source | 115 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 106 | 0 | do { \ | 107 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 108 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 109 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 110 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 89 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 111 | 0 | } \ | 112 | 0 | } while (0) |
|
|
385 | 0 | } |
386 | 0 | } |
387 | | |
388 | | void CTxMemPool::removeConflicts(const CTransaction &tx) |
389 | 0 | { |
390 | | // Remove transactions which depend on inputs of tx, recursively |
391 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
392 | 0 | for (const CTxIn &txin : tx.vin) { |
393 | 0 | auto it = mapNextTx.find(txin.prevout); |
394 | 0 | if (it != mapNextTx.end()) { |
395 | 0 | const CTransaction &txConflict = it->second->GetTx(); |
396 | 0 | if (Assume(txConflict.GetHash() != tx.GetHash())) Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
397 | 0 | { |
398 | 0 | ClearPrioritisation(txConflict.GetHash()); |
399 | 0 | removeRecursive(it->second, MemPoolRemovalReason::CONFLICT); |
400 | 0 | } |
401 | 0 | } |
402 | 0 | } |
403 | 0 | } |
404 | | |
405 | | void CTxMemPool::removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight) |
406 | 0 | { |
407 | | // Remove confirmed txs and conflicts when a new block is connected, updating the fee logic |
408 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
409 | 0 | Assume(!m_have_changeset); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
410 | 0 | std::vector<RemovedMempoolTransactionInfo> txs_removed_for_block; |
411 | 0 | if (mapTx.size() || mapNextTx.size() || mapDeltas.size()) { |
412 | 0 | txs_removed_for_block.reserve(vtx.size()); |
413 | 0 | for (const auto& tx : vtx) { |
414 | 0 | txiter it = mapTx.find(tx->GetHash()); |
415 | 0 | if (it != mapTx.end()) { |
416 | 0 | txs_removed_for_block.emplace_back(*it); |
417 | 0 | removeUnchecked(it, MemPoolRemovalReason::BLOCK); |
418 | 0 | } |
419 | 0 | removeConflicts(*tx); |
420 | 0 | ClearPrioritisation(tx->GetHash()); |
421 | 0 | } |
422 | 0 | } |
423 | 0 | if (m_opts.signals) { |
424 | 0 | m_opts.signals->MempoolTransactionsRemovedForBlock(txs_removed_for_block, nBlockHeight); |
425 | 0 | } |
426 | 0 | lastRollingFeeUpdate = GetTime(); |
427 | 0 | blockSinceLastRollingFeeBump = true; |
428 | 0 | if (!m_txgraph->DoWork(/*max_cost=*/POST_CHANGE_COST)) { |
429 | 0 | LogDebug(BCLog::MEMPOOL, "Mempool in non-optimal ordering after block."); Line | Count | Source | 115 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 106 | 0 | do { \ | 107 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 108 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 109 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 110 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 89 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 111 | 0 | } \ | 112 | 0 | } while (0) |
|
|
430 | 0 | } |
431 | 0 | } |
432 | | |
433 | | void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendheight) const |
434 | 0 | { |
435 | 0 | if (m_opts.check_ratio == 0) return; |
436 | | |
437 | 0 | if (FastRandomContext().randrange(m_opts.check_ratio) >= 1) return; |
438 | | |
439 | 0 | AssertLockHeld(::cs_main); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
440 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
441 | 0 | LogDebug(BCLog::MEMPOOL, "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size()); Line | Count | Source | 115 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 106 | 0 | do { \ | 107 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 108 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 109 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 110 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 89 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 111 | 0 | } \ | 112 | 0 | } while (0) |
|
|
442 | |
|
443 | 0 | uint64_t checkTotal = 0; |
444 | 0 | CAmount check_total_fee{0}; |
445 | 0 | CAmount check_total_modified_fee{0}; |
446 | 0 | int64_t check_total_adjusted_weight{0}; |
447 | 0 | uint64_t innerUsage = 0; |
448 | |
|
449 | 0 | assert(!m_txgraph->IsOversized(TxGraph::Level::MAIN)); |
450 | 0 | m_txgraph->SanityCheck(); |
451 | |
|
452 | 0 | CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(&active_coins_tip)); |
453 | |
|
454 | 0 | const auto score_with_topo{GetSortedScoreWithTopology()}; |
455 | | |
456 | | // Number of chunks is bounded by number of transactions. |
457 | 0 | const auto diagram{GetFeerateDiagram()}; |
458 | 0 | assert(diagram.size() <= score_with_topo.size() + 1); |
459 | 0 | assert(diagram.size() >= 1); |
460 | | |
461 | 0 | std::optional<Wtxid> last_wtxid = std::nullopt; |
462 | 0 | auto diagram_iter = diagram.cbegin(); |
463 | |
|
464 | 0 | for (const auto& it : score_with_topo) { |
465 | | // GetSortedScoreWithTopology() contains the same chunks as the feerate |
466 | | // diagram. We do not know where the chunk boundaries are, but we can |
467 | | // check that there are points at which they match the cumulative fee |
468 | | // and weight. |
469 | | // The feerate diagram should never get behind the current transaction |
470 | | // size totals. |
471 | 0 | assert(diagram_iter->size >= check_total_adjusted_weight); |
472 | 0 | if (diagram_iter->fee == check_total_modified_fee && |
473 | 0 | diagram_iter->size == check_total_adjusted_weight) { |
474 | 0 | ++diagram_iter; |
475 | 0 | } |
476 | 0 | checkTotal += it->GetTxSize(); |
477 | 0 | check_total_adjusted_weight += it->GetAdjustedWeight(); |
478 | 0 | check_total_fee += it->GetFee(); |
479 | 0 | check_total_modified_fee += it->GetModifiedFee(); |
480 | 0 | innerUsage += it->DynamicMemoryUsage(); |
481 | 0 | const CTransaction& tx = it->GetTx(); |
482 | | |
483 | | // CompareMiningScoreWithTopology should agree with GetSortedScoreWithTopology() |
484 | 0 | if (last_wtxid) { |
485 | 0 | assert(CompareMiningScoreWithTopology(*last_wtxid, tx.GetWitnessHash())); |
486 | 0 | } |
487 | 0 | last_wtxid = tx.GetWitnessHash(); |
488 | |
|
489 | 0 | std::set<CTxMemPoolEntry::CTxMemPoolEntryRef, CompareIteratorByHash> setParentCheck; |
490 | 0 | std::set<CTxMemPoolEntry::CTxMemPoolEntryRef, CompareIteratorByHash> setParentsStored; |
491 | 0 | for (const CTxIn &txin : tx.vin) { |
492 | | // Check that every mempool transaction's inputs refer to available coins, or other mempool tx's. |
493 | 0 | indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash); |
494 | 0 | if (it2 != mapTx.end()) { |
495 | 0 | const CTransaction& tx2 = it2->GetTx(); |
496 | 0 | assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull()); |
497 | 0 | setParentCheck.insert(*it2); |
498 | 0 | } |
499 | | // We are iterating through the mempool entries sorted |
500 | | // topologically and by mining score. All parents must have been |
501 | | // checked before their children and their coins added to the |
502 | | // mempoolDuplicate coins cache. |
503 | 0 | assert(mempoolDuplicate.HaveCoin(txin.prevout)); |
504 | | // Check whether its inputs are marked in mapNextTx. |
505 | 0 | auto it3 = mapNextTx.find(txin.prevout); |
506 | 0 | assert(it3 != mapNextTx.end()); |
507 | 0 | assert(it3->first == &txin.prevout); |
508 | 0 | assert(&it3->second->GetTx() == &tx); |
509 | 0 | } |
510 | 0 | auto comp = [](const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) -> bool { |
511 | 0 | return a.GetTx().GetHash() == b.GetTx().GetHash(); |
512 | 0 | }; |
513 | 0 | for (auto &txentry : GetParents(*it)) { |
514 | 0 | setParentsStored.insert(dynamic_cast<const CTxMemPoolEntry&>(txentry.get())); |
515 | 0 | } |
516 | 0 | assert(setParentCheck.size() == setParentsStored.size()); |
517 | 0 | assert(std::equal(setParentCheck.begin(), setParentCheck.end(), setParentsStored.begin(), comp)); |
518 | | |
519 | | // Check children against mapNextTx |
520 | 0 | std::set<CTxMemPoolEntry::CTxMemPoolEntryRef, CompareIteratorByHash> setChildrenCheck; |
521 | 0 | std::set<CTxMemPoolEntry::CTxMemPoolEntryRef, CompareIteratorByHash> setChildrenStored; |
522 | 0 | auto iter = mapNextTx.lower_bound(COutPoint(it->GetTx().GetHash(), 0)); |
523 | 0 | for (; iter != mapNextTx.end() && iter->first->hash == it->GetTx().GetHash(); ++iter) { |
524 | 0 | txiter childit = iter->second; |
525 | 0 | assert(childit != mapTx.end()); // mapNextTx points to in-mempool transactions |
526 | 0 | setChildrenCheck.insert(*childit); |
527 | 0 | } |
528 | 0 | for (auto &txentry : GetChildren(*it)) { |
529 | 0 | setChildrenStored.insert(dynamic_cast<const CTxMemPoolEntry&>(txentry.get())); |
530 | 0 | } |
531 | 0 | assert(setChildrenCheck.size() == setChildrenStored.size()); |
532 | 0 | assert(std::equal(setChildrenCheck.begin(), setChildrenCheck.end(), setChildrenStored.begin(), comp)); |
533 | | |
534 | 0 | TxValidationState dummy_state; // Not used. CheckTxInputs() should always pass |
535 | 0 | CAmount txfee = 0; |
536 | 0 | assert(!tx.IsCoinBase()); |
537 | 0 | assert(Consensus::CheckTxInputs(tx, dummy_state, mempoolDuplicate, spendheight, txfee)); |
538 | 0 | for (const auto& input: tx.vin) mempoolDuplicate.SpendCoin(input.prevout); |
539 | 0 | AddCoins(mempoolDuplicate, tx, std::numeric_limits<int>::max()); |
540 | 0 | } |
541 | 0 | for (auto it = mapNextTx.cbegin(); it != mapNextTx.cend(); it++) { |
542 | 0 | indexed_transaction_set::const_iterator it2 = it->second; |
543 | 0 | assert(it2 != mapTx.end()); |
544 | 0 | } |
545 | | |
546 | 0 | ++diagram_iter; |
547 | 0 | assert(diagram_iter == diagram.cend()); |
548 | | |
549 | 0 | assert(totalTxSize == checkTotal); |
550 | 0 | assert(m_total_fee == check_total_fee); |
551 | 0 | assert(diagram.back().fee == check_total_modified_fee); |
552 | 0 | assert(diagram.back().size == check_total_adjusted_weight); |
553 | 0 | assert(innerUsage == cachedInnerUsage); |
554 | 0 | } |
555 | | |
556 | | bool CTxMemPool::CompareMiningScoreWithTopology(const Wtxid& hasha, const Wtxid& hashb) const |
557 | 0 | { |
558 | | /* Return `true` if hasha should be considered sooner than hashb, namely when: |
559 | | * a is not in the mempool but b is, or |
560 | | * both are in the mempool but a is sorted before b in the total mempool ordering |
561 | | * (which takes dependencies and (chunk) feerates into account). |
562 | | */ |
563 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
564 | 0 | auto j{GetIter(hashb)}; |
565 | 0 | if (!j.has_value()) return false; |
566 | 0 | auto i{GetIter(hasha)}; |
567 | 0 | if (!i.has_value()) return true; |
568 | | |
569 | 0 | return m_txgraph->CompareMainOrder(*i.value(), *j.value()) < 0; |
570 | 0 | } |
571 | | |
572 | | std::vector<CTxMemPool::indexed_transaction_set::const_iterator> CTxMemPool::GetSortedScoreWithTopology() const |
573 | 0 | { |
574 | 0 | std::vector<indexed_transaction_set::const_iterator> iters; |
575 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
576 | |
|
577 | 0 | iters.reserve(mapTx.size()); |
578 | |
|
579 | 0 | for (indexed_transaction_set::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi) { |
580 | 0 | iters.push_back(mi); |
581 | 0 | } |
582 | 0 | std::sort(iters.begin(), iters.end(), [this](const auto& a, const auto& b) EXCLUSIVE_LOCKS_REQUIRED(cs) noexcept { |
583 | 0 | return m_txgraph->CompareMainOrder(*a, *b) < 0; |
584 | 0 | }); |
585 | 0 | return iters; |
586 | 0 | } |
587 | | |
588 | | std::vector<CTxMemPoolEntryRef> CTxMemPool::entryAll() const |
589 | 0 | { |
590 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
591 | |
|
592 | 0 | std::vector<CTxMemPoolEntryRef> ret; |
593 | 0 | ret.reserve(mapTx.size()); |
594 | 0 | for (const auto& it : GetSortedScoreWithTopology()) { |
595 | 0 | ret.emplace_back(*it); |
596 | 0 | } |
597 | 0 | return ret; |
598 | 0 | } |
599 | | |
600 | | std::vector<TxMempoolInfo> CTxMemPool::infoAll() const |
601 | 0 | { |
602 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
603 | 0 | auto iters = GetSortedScoreWithTopology(); |
604 | |
|
605 | 0 | std::vector<TxMempoolInfo> ret; |
606 | 0 | ret.reserve(mapTx.size()); |
607 | 0 | for (auto it : iters) { |
608 | 0 | ret.push_back(GetInfo(it)); |
609 | 0 | } |
610 | |
|
611 | 0 | return ret; |
612 | 0 | } |
613 | | |
614 | | const CTxMemPoolEntry* CTxMemPool::GetEntry(const Txid& txid) const |
615 | 0 | { |
616 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
617 | 0 | const auto i = mapTx.find(txid); |
618 | 0 | return i == mapTx.end() ? nullptr : &(*i); |
619 | 0 | } |
620 | | |
621 | | CTransactionRef CTxMemPool::get(const Txid& hash) const |
622 | 0 | { |
623 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
624 | 0 | indexed_transaction_set::const_iterator i = mapTx.find(hash); |
625 | 0 | if (i == mapTx.end()) |
626 | 0 | return nullptr; |
627 | 0 | return i->GetSharedTx(); |
628 | 0 | } |
629 | | |
630 | | void CTxMemPool::PrioritiseTransaction(const Txid& hash, const CAmount& nFeeDelta) |
631 | 0 | { |
632 | 0 | { |
633 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
634 | 0 | CAmount &delta = mapDeltas[hash]; |
635 | 0 | delta = SaturatingAdd(delta, nFeeDelta); |
636 | 0 | txiter it = mapTx.find(hash); |
637 | 0 | if (it != mapTx.end()) { |
638 | | // PrioritiseTransaction calls stack on previous ones. Set the new |
639 | | // transaction fee to be current modified fee + feedelta. |
640 | 0 | it->UpdateModifiedFee(nFeeDelta); |
641 | 0 | m_txgraph->SetTransactionFee(*it, it->GetModifiedFee()); |
642 | 0 | ++nTransactionsUpdated; |
643 | 0 | } |
644 | 0 | if (delta == 0) { |
645 | 0 | mapDeltas.erase(hash); |
646 | 0 | LogInfo("PrioritiseTransaction: %s (%sin mempool) delta cleared\n", hash.ToString(), it == mapTx.end() ? "not " : "");Line | Count | Source | 95 | 0 | #define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 89 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
647 | 0 | } else { |
648 | 0 | LogInfo("PrioritiseTransaction: %s (%sin mempool) fee += %s, new delta=%s\n",Line | Count | Source | 95 | 0 | #define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 89 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
649 | 0 | hash.ToString(), |
650 | 0 | it == mapTx.end() ? "not " : "", |
651 | 0 | FormatMoney(nFeeDelta), |
652 | 0 | FormatMoney(delta)); |
653 | 0 | } |
654 | 0 | } |
655 | 0 | } |
656 | | |
657 | | void CTxMemPool::ApplyDelta(const Txid& hash, CAmount &nFeeDelta) const |
658 | 0 | { |
659 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
660 | 0 | std::map<Txid, CAmount>::const_iterator pos = mapDeltas.find(hash); |
661 | 0 | if (pos == mapDeltas.end()) |
662 | 0 | return; |
663 | 0 | const CAmount &delta = pos->second; |
664 | 0 | nFeeDelta += delta; |
665 | 0 | } |
666 | | |
667 | | void CTxMemPool::ClearPrioritisation(const Txid& hash) |
668 | 0 | { |
669 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
670 | 0 | mapDeltas.erase(hash); |
671 | 0 | } |
672 | | |
673 | | std::vector<CTxMemPool::delta_info> CTxMemPool::GetPrioritisedTransactions() const |
674 | 0 | { |
675 | 0 | AssertLockNotHeld(cs); Line | Count | Source | 147 | 0 | #define AssertLockNotHeld(cs) AssertLockNotHeldInline(#cs, __FILE__, __LINE__, &cs) |
|
676 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
677 | 0 | std::vector<delta_info> result; |
678 | 0 | result.reserve(mapDeltas.size()); |
679 | 0 | for (const auto& [txid, delta] : mapDeltas) { |
680 | 0 | const auto iter{mapTx.find(txid)}; |
681 | 0 | const bool in_mempool{iter != mapTx.end()}; |
682 | 0 | std::optional<CAmount> modified_fee; |
683 | 0 | if (in_mempool) modified_fee = iter->GetModifiedFee(); |
684 | 0 | result.emplace_back(delta_info{in_mempool, delta, modified_fee, txid}); |
685 | 0 | } |
686 | 0 | return result; |
687 | 0 | } |
688 | | |
689 | | const CTransaction* CTxMemPool::GetConflictTx(const COutPoint& prevout) const |
690 | 0 | { |
691 | 0 | const auto it = mapNextTx.find(prevout); |
692 | 0 | return it == mapNextTx.end() ? nullptr : &(it->second->GetTx()); |
693 | 0 | } |
694 | | |
695 | | std::optional<CTxMemPool::txiter> CTxMemPool::GetIter(const Txid& txid) const |
696 | 0 | { |
697 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
698 | 0 | auto it = mapTx.find(txid); |
699 | 0 | return it != mapTx.end() ? std::make_optional(it) : std::nullopt; |
700 | 0 | } |
701 | | |
702 | | std::optional<CTxMemPool::txiter> CTxMemPool::GetIter(const Wtxid& wtxid) const |
703 | 0 | { |
704 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
705 | 0 | auto it{mapTx.project<0>(mapTx.get<index_by_wtxid>().find(wtxid))}; |
706 | 0 | return it != mapTx.end() ? std::make_optional(it) : std::nullopt; |
707 | 0 | } |
708 | | |
709 | | CTxMemPool::setEntries CTxMemPool::GetIterSet(const std::set<Txid>& hashes) const |
710 | 0 | { |
711 | 0 | CTxMemPool::setEntries ret; |
712 | 0 | for (const auto& h : hashes) { |
713 | 0 | const auto mi = GetIter(h); |
714 | 0 | if (mi) ret.insert(*mi); |
715 | 0 | } |
716 | 0 | return ret; |
717 | 0 | } |
718 | | |
719 | | std::vector<CTxMemPool::txiter> CTxMemPool::GetIterVec(const std::vector<Txid>& txids) const |
720 | 0 | { |
721 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
722 | 0 | std::vector<txiter> ret; |
723 | 0 | ret.reserve(txids.size()); |
724 | 0 | for (const auto& txid : txids) { |
725 | 0 | const auto it{GetIter(txid)}; |
726 | 0 | if (!it) return {}; |
727 | 0 | ret.push_back(*it); |
728 | 0 | } |
729 | 0 | return ret; |
730 | 0 | } |
731 | | |
732 | | bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const |
733 | 0 | { |
734 | 0 | for (unsigned int i = 0; i < tx.vin.size(); i++) |
735 | 0 | if (exists(tx.vin[i].prevout.hash)) |
736 | 0 | return false; |
737 | 0 | return true; |
738 | 0 | } |
739 | | |
740 | 0 | CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { } |
741 | | |
742 | | std::optional<Coin> CCoinsViewMemPool::GetCoin(const COutPoint& outpoint) const |
743 | 0 | { |
744 | | // Check to see if the inputs are made available by another tx in the package. |
745 | | // These Coins would not be available in the underlying CoinsView. |
746 | 0 | if (auto it = m_temp_added.find(outpoint); it != m_temp_added.end()) { |
747 | 0 | return it->second; |
748 | 0 | } |
749 | | |
750 | | // If an entry in the mempool exists, always return that one, as it's guaranteed to never |
751 | | // conflict with the underlying cache, and it cannot have pruned entries (as it contains full) |
752 | | // transactions. First checking the underlying cache risks returning a pruned entry instead. |
753 | 0 | CTransactionRef ptx = mempool.get(outpoint.hash); |
754 | 0 | if (ptx) { |
755 | 0 | if (outpoint.n < ptx->vout.size()) { |
756 | 0 | Coin coin(ptx->vout[outpoint.n], MEMPOOL_HEIGHT, false); |
757 | 0 | m_non_base_coins.emplace(outpoint); |
758 | 0 | return coin; |
759 | 0 | } |
760 | 0 | return std::nullopt; |
761 | 0 | } |
762 | 0 | return base->GetCoin(outpoint); |
763 | 0 | } |
764 | | |
765 | | void CCoinsViewMemPool::PackageAddTransaction(const CTransactionRef& tx) |
766 | 0 | { |
767 | 0 | for (unsigned int n = 0; n < tx->vout.size(); ++n) { |
768 | 0 | m_temp_added.emplace(COutPoint(tx->GetHash(), n), Coin(tx->vout[n], MEMPOOL_HEIGHT, false)); |
769 | 0 | m_non_base_coins.emplace(tx->GetHash(), n); |
770 | 0 | } |
771 | 0 | } |
772 | | void CCoinsViewMemPool::Reset() |
773 | 0 | { |
774 | 0 | m_temp_added.clear(); |
775 | 0 | m_non_base_coins.clear(); |
776 | 0 | } |
777 | | |
778 | 0 | size_t CTxMemPool::DynamicMemoryUsage() const { |
779 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
780 | | // Estimate the overhead of mapTx to be 9 pointers (3 pointers per index) + an allocation, as no exact formula for boost::multi_index_contained is implemented. |
781 | 0 | return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 9 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + memusage::DynamicUsage(txns_randomized) + m_txgraph->GetMainMemoryUsage() + cachedInnerUsage; |
782 | 0 | } |
783 | | |
784 | 0 | void CTxMemPool::RemoveUnbroadcastTx(const Txid& txid, const bool unchecked) { |
785 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
786 | |
|
787 | 0 | if (m_unbroadcast_txids.erase(txid)) |
788 | 0 | { |
789 | 0 | LogDebug(BCLog::MEMPOOL, "Removed %s from set of unbroadcast txns%s", txid.GetHex(), (unchecked ? " before confirmation that txn was sent out" : "")); Line | Count | Source | 115 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 106 | 0 | do { \ | 107 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 108 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 109 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 110 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 89 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 111 | 0 | } \ | 112 | 0 | } while (0) |
|
|
790 | 0 | } |
791 | 0 | } |
792 | | |
793 | 0 | void CTxMemPool::RemoveStaged(setEntries &stage, MemPoolRemovalReason reason) { |
794 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
795 | 0 | for (txiter it : stage) { |
796 | 0 | removeUnchecked(it, reason); |
797 | 0 | } |
798 | 0 | } |
799 | | |
800 | | bool CTxMemPool::CheckPolicyLimits(const CTransactionRef& tx) |
801 | 0 | { |
802 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
803 | | // Use ChangeSet interface to check whether the cluster count |
804 | | // limits would be violated. Note that the changeset will be destroyed |
805 | | // when it goes out of scope. |
806 | 0 | auto changeset = GetChangeSet(); |
807 | 0 | (void) changeset->StageAddition(tx, /*fee=*/0, /*time=*/0, /*entry_height=*/0, /*entry_sequence=*/0, /*spends_coinbase=*/false, /*sigops_cost=*/0, LockPoints{}); |
808 | 0 | return changeset->CheckMemPoolPolicyLimits(); |
809 | 0 | } |
810 | | |
811 | | int CTxMemPool::Expire(std::chrono::seconds time) |
812 | 0 | { |
813 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
814 | 0 | Assume(!m_have_changeset); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
815 | 0 | indexed_transaction_set::index<entry_time>::type::iterator it = mapTx.get<entry_time>().begin(); |
816 | 0 | setEntries toremove; |
817 | 0 | while (it != mapTx.get<entry_time>().end() && it->GetTime() < time) { |
818 | 0 | toremove.insert(mapTx.project<0>(it)); |
819 | 0 | it++; |
820 | 0 | } |
821 | 0 | setEntries stage; |
822 | 0 | for (txiter removeit : toremove) { |
823 | 0 | CalculateDescendants(removeit, stage); |
824 | 0 | } |
825 | 0 | RemoveStaged(stage, MemPoolRemovalReason::EXPIRY); |
826 | 0 | return stage.size(); |
827 | 0 | } |
828 | | |
829 | 0 | CFeeRate CTxMemPool::GetMinFee(size_t sizelimit) const { |
830 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
831 | 0 | if (!blockSinceLastRollingFeeBump || rollingMinimumFeeRate == 0) |
832 | 0 | return CFeeRate(llround(rollingMinimumFeeRate)); |
833 | | |
834 | 0 | int64_t time = GetTime(); |
835 | 0 | if (time > lastRollingFeeUpdate + 10) { |
836 | 0 | double halflife = ROLLING_FEE_HALFLIFE; |
837 | 0 | if (DynamicMemoryUsage() < sizelimit / 4) |
838 | 0 | halflife /= 4; |
839 | 0 | else if (DynamicMemoryUsage() < sizelimit / 2) |
840 | 0 | halflife /= 2; |
841 | |
|
842 | 0 | rollingMinimumFeeRate = rollingMinimumFeeRate / pow(2.0, (time - lastRollingFeeUpdate) / halflife); |
843 | 0 | lastRollingFeeUpdate = time; |
844 | |
|
845 | 0 | if (rollingMinimumFeeRate < (double)m_opts.incremental_relay_feerate.GetFeePerK() / 2) { |
846 | 0 | rollingMinimumFeeRate = 0; |
847 | 0 | return CFeeRate(0); |
848 | 0 | } |
849 | 0 | } |
850 | 0 | return std::max(CFeeRate(llround(rollingMinimumFeeRate)), m_opts.incremental_relay_feerate); |
851 | 0 | } |
852 | | |
853 | 0 | void CTxMemPool::trackPackageRemoved(const CFeeRate& rate) { |
854 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
855 | 0 | if (rate.GetFeePerK() > rollingMinimumFeeRate) { |
856 | 0 | rollingMinimumFeeRate = rate.GetFeePerK(); |
857 | 0 | blockSinceLastRollingFeeBump = false; |
858 | 0 | } |
859 | 0 | } |
860 | | |
861 | 0 | void CTxMemPool::TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining) { |
862 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
863 | 0 | Assume(!m_have_changeset); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
864 | |
|
865 | 0 | unsigned nTxnRemoved = 0; |
866 | 0 | CFeeRate maxFeeRateRemoved(0); |
867 | |
|
868 | 0 | while (!mapTx.empty() && DynamicMemoryUsage() > sizelimit) { |
869 | 0 | const auto &[worst_chunk, feeperweight] = m_txgraph->GetWorstMainChunk(); |
870 | 0 | FeePerVSize feerate = ToFeePerVSize(feeperweight); |
871 | 0 | CFeeRate removed{feerate.fee, feerate.size}; |
872 | | |
873 | | // We set the new mempool min fee to the feerate of the removed set, plus the |
874 | | // "minimum reasonable fee rate" (ie some value under which we consider txn |
875 | | // to have 0 fee). This way, we don't allow txn to enter mempool with feerate |
876 | | // equal to txn which were removed with no block in between. |
877 | 0 | removed += m_opts.incremental_relay_feerate; |
878 | 0 | trackPackageRemoved(removed); |
879 | 0 | maxFeeRateRemoved = std::max(maxFeeRateRemoved, removed); |
880 | |
|
881 | 0 | nTxnRemoved += worst_chunk.size(); |
882 | |
|
883 | 0 | std::vector<CTransaction> txn; |
884 | 0 | if (pvNoSpendsRemaining) { |
885 | 0 | txn.reserve(worst_chunk.size()); |
886 | 0 | for (auto ref : worst_chunk) { |
887 | 0 | txn.emplace_back(static_cast<const CTxMemPoolEntry&>(*ref).GetTx()); |
888 | 0 | } |
889 | 0 | } |
890 | |
|
891 | 0 | setEntries stage; |
892 | 0 | for (auto ref : worst_chunk) { |
893 | 0 | stage.insert(mapTx.iterator_to(static_cast<const CTxMemPoolEntry&>(*ref))); |
894 | 0 | } |
895 | 0 | for (auto e : stage) { |
896 | 0 | removeUnchecked(e, MemPoolRemovalReason::SIZELIMIT); |
897 | 0 | } |
898 | 0 | if (pvNoSpendsRemaining) { |
899 | 0 | for (const CTransaction& tx : txn) { |
900 | 0 | for (const CTxIn& txin : tx.vin) { |
901 | 0 | if (exists(txin.prevout.hash)) continue; |
902 | 0 | pvNoSpendsRemaining->push_back(txin.prevout); |
903 | 0 | } |
904 | 0 | } |
905 | 0 | } |
906 | 0 | } |
907 | |
|
908 | 0 | if (maxFeeRateRemoved > CFeeRate(0)) { |
909 | 0 | LogDebug(BCLog::MEMPOOL, "Removed %u txn, rolling minimum fee bumped to %s\n", nTxnRemoved, maxFeeRateRemoved.ToString()); Line | Count | Source | 115 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 106 | 0 | do { \ | 107 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 108 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 109 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 110 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 89 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 111 | 0 | } \ | 112 | 0 | } while (0) |
|
|
910 | 0 | } |
911 | 0 | } |
912 | | |
913 | | std::tuple<size_t, size_t, CAmount> CTxMemPool::CalculateAncestorData(const CTxMemPoolEntry& entry) const |
914 | 0 | { |
915 | 0 | auto ancestors = m_txgraph->GetAncestors(entry, TxGraph::Level::MAIN); |
916 | |
|
917 | 0 | size_t ancestor_count = ancestors.size(); |
918 | 0 | size_t ancestor_size = 0; |
919 | 0 | CAmount ancestor_fees = 0; |
920 | 0 | for (auto tx: ancestors) { |
921 | 0 | const CTxMemPoolEntry& anc = static_cast<const CTxMemPoolEntry&>(*tx); |
922 | 0 | ancestor_size += anc.GetTxSize(); |
923 | 0 | ancestor_fees += anc.GetModifiedFee(); |
924 | 0 | } |
925 | 0 | return {ancestor_count, ancestor_size, ancestor_fees}; |
926 | 0 | } |
927 | | |
928 | | std::tuple<size_t, size_t, CAmount> CTxMemPool::CalculateDescendantData(const CTxMemPoolEntry& entry) const |
929 | 0 | { |
930 | 0 | auto descendants = m_txgraph->GetDescendants(entry, TxGraph::Level::MAIN); |
931 | 0 | size_t descendant_count = descendants.size(); |
932 | 0 | size_t descendant_size = 0; |
933 | 0 | CAmount descendant_fees = 0; |
934 | |
|
935 | 0 | for (auto tx: descendants) { |
936 | 0 | const CTxMemPoolEntry &desc = static_cast<const CTxMemPoolEntry&>(*tx); |
937 | 0 | descendant_size += desc.GetTxSize(); |
938 | 0 | descendant_fees += desc.GetModifiedFee(); |
939 | 0 | } |
940 | 0 | return {descendant_count, descendant_size, descendant_fees}; |
941 | 0 | } |
942 | | |
943 | 0 | void CTxMemPool::GetTransactionAncestry(const Txid& txid, size_t& ancestors, size_t& cluster_count, size_t* const ancestorsize, CAmount* const ancestorfees) const { |
944 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
945 | 0 | auto it = mapTx.find(txid); |
946 | 0 | ancestors = cluster_count = 0; |
947 | 0 | if (it != mapTx.end()) { |
948 | 0 | auto [ancestor_count, ancestor_size, ancestor_fees] = CalculateAncestorData(*it); |
949 | 0 | ancestors = ancestor_count; |
950 | 0 | if (ancestorsize) *ancestorsize = ancestor_size; |
951 | 0 | if (ancestorfees) *ancestorfees = ancestor_fees; |
952 | 0 | cluster_count = m_txgraph->GetCluster(*it, TxGraph::Level::MAIN).size(); |
953 | 0 | } |
954 | 0 | } |
955 | | |
956 | | bool CTxMemPool::GetLoadTried() const |
957 | 0 | { |
958 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
959 | 0 | return m_load_tried; |
960 | 0 | } |
961 | | |
962 | | void CTxMemPool::SetLoadTried(bool load_tried) |
963 | 0 | { |
964 | 0 | LOCK(cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
965 | 0 | m_load_tried = load_tried; |
966 | 0 | } |
967 | | |
968 | | std::vector<CTxMemPool::txiter> CTxMemPool::GatherClusters(const std::vector<Txid>& txids) const |
969 | 0 | { |
970 | 0 | AssertLockHeld(cs); Line | Count | Source | 142 | 0 | #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) |
|
971 | |
|
972 | 0 | std::vector<CTxMemPool::txiter> ret; |
973 | 0 | std::set<const CTxMemPoolEntry*> unique_cluster_representatives; |
974 | 0 | for (auto txid : txids) { |
975 | 0 | auto it = mapTx.find(txid); |
976 | 0 | if (it != mapTx.end()) { |
977 | | // Note that TxGraph::GetCluster will return results in graph |
978 | | // order, which is deterministic (as long as we are not modifying |
979 | | // the graph). |
980 | 0 | auto cluster = m_txgraph->GetCluster(*it, TxGraph::Level::MAIN); |
981 | 0 | if (unique_cluster_representatives.insert(static_cast<const CTxMemPoolEntry*>(&(**cluster.begin()))).second) { |
982 | 0 | for (auto tx : cluster) { |
983 | 0 | ret.emplace_back(mapTx.iterator_to(static_cast<const CTxMemPoolEntry&>(*tx))); |
984 | 0 | } |
985 | 0 | } |
986 | 0 | } |
987 | 0 | } |
988 | 0 | if (ret.size() > 500) { |
989 | 0 | return {}; |
990 | 0 | } |
991 | 0 | return ret; |
992 | 0 | } |
993 | | |
994 | | util::Result<std::pair<std::vector<FeeFrac>, std::vector<FeeFrac>>> CTxMemPool::ChangeSet::CalculateChunksForRBF() |
995 | 0 | { |
996 | 0 | LOCK(m_pool->cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
997 | |
|
998 | 0 | if (!CheckMemPoolPolicyLimits()) { |
999 | 0 | return util::Error{Untranslated("cluster size limit exceeded")}; |
1000 | 0 | } |
1001 | | |
1002 | 0 | return m_pool->m_txgraph->GetMainStagingDiagrams(); |
1003 | 0 | } |
1004 | | |
1005 | | CTxMemPool::ChangeSet::TxHandle CTxMemPool::ChangeSet::StageAddition(const CTransactionRef& tx, const CAmount fee, int64_t time, unsigned int entry_height, uint64_t entry_sequence, bool spends_coinbase, int64_t sigops_cost, LockPoints lp) |
1006 | 0 | { |
1007 | 0 | LOCK(m_pool->cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
1008 | 0 | Assume(m_to_add.find(tx->GetHash()) == m_to_add.end()); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
1009 | 0 | Assume(!m_dependencies_processed); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
1010 | | |
1011 | | // We need to process dependencies after adding a new transaction. |
1012 | 0 | m_dependencies_processed = false; |
1013 | |
|
1014 | 0 | CAmount delta{0}; |
1015 | 0 | m_pool->ApplyDelta(tx->GetHash(), delta); |
1016 | |
|
1017 | 0 | FeePerWeight feerate(fee, GetSigOpsAdjustedWeight(GetTransactionWeight(*tx), sigops_cost, ::nBytesPerSigOp)); |
1018 | 0 | auto newit = m_to_add.emplace(tx, fee, time, entry_height, entry_sequence, spends_coinbase, sigops_cost, lp).first; |
1019 | 0 | m_pool->m_txgraph->AddTransaction(const_cast<CTxMemPoolEntry&>(*newit), feerate); |
1020 | 0 | if (delta) { |
1021 | 0 | newit->UpdateModifiedFee(delta); |
1022 | 0 | m_pool->m_txgraph->SetTransactionFee(*newit, newit->GetModifiedFee()); |
1023 | 0 | } |
1024 | |
|
1025 | 0 | m_entry_vec.push_back(newit); |
1026 | |
|
1027 | 0 | return newit; |
1028 | 0 | } |
1029 | | |
1030 | | void CTxMemPool::ChangeSet::StageRemoval(CTxMemPool::txiter it) |
1031 | 0 | { |
1032 | 0 | LOCK(m_pool->cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
1033 | 0 | m_pool->m_txgraph->RemoveTransaction(*it); |
1034 | 0 | m_to_remove.insert(it); |
1035 | 0 | } |
1036 | | |
1037 | | void CTxMemPool::ChangeSet::Apply() |
1038 | 0 | { |
1039 | 0 | LOCK(m_pool->cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
1040 | 0 | if (!m_dependencies_processed) { |
1041 | 0 | ProcessDependencies(); |
1042 | 0 | } |
1043 | 0 | m_pool->Apply(this); |
1044 | 0 | m_to_add.clear(); |
1045 | 0 | m_to_remove.clear(); |
1046 | 0 | m_entry_vec.clear(); |
1047 | 0 | m_ancestors.clear(); |
1048 | 0 | } |
1049 | | |
1050 | | void CTxMemPool::ChangeSet::ProcessDependencies() |
1051 | 0 | { |
1052 | 0 | LOCK(m_pool->cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
1053 | 0 | Assume(!m_dependencies_processed); // should only call this once. Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
1054 | 0 | for (const auto& entryptr : m_entry_vec) { |
1055 | 0 | for (const auto &txin : entryptr->GetSharedTx()->vin) { |
1056 | 0 | std::optional<txiter> piter = m_pool->GetIter(txin.prevout.hash); |
1057 | 0 | if (!piter) { |
1058 | 0 | auto it = m_to_add.find(txin.prevout.hash); |
1059 | 0 | if (it != m_to_add.end()) { |
1060 | 0 | piter = std::make_optional(it); |
1061 | 0 | } |
1062 | 0 | } |
1063 | 0 | if (piter) { |
1064 | 0 | m_pool->m_txgraph->AddDependency(/*parent=*/**piter, /*child=*/*entryptr); |
1065 | 0 | } |
1066 | 0 | } |
1067 | 0 | } |
1068 | 0 | m_dependencies_processed = true; |
1069 | 0 | return; |
1070 | 0 | } |
1071 | | |
1072 | | bool CTxMemPool::ChangeSet::CheckMemPoolPolicyLimits() |
1073 | 0 | { |
1074 | 0 | LOCK(m_pool->cs); Line | Count | Source | 266 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
1075 | 0 | if (!m_dependencies_processed) { |
1076 | 0 | ProcessDependencies(); |
1077 | 0 | } |
1078 | |
|
1079 | 0 | return !m_pool->m_txgraph->IsOversized(TxGraph::Level::TOP); |
1080 | 0 | } |
1081 | | |
1082 | | std::vector<FeePerWeight> CTxMemPool::GetFeerateDiagram() const |
1083 | 0 | { |
1084 | 0 | FeePerWeight zero{}; |
1085 | 0 | std::vector<FeePerWeight> ret; |
1086 | |
|
1087 | 0 | ret.emplace_back(zero); |
1088 | |
|
1089 | 0 | StartBlockBuilding(); |
1090 | |
|
1091 | 0 | std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> dummy; |
1092 | |
|
1093 | 0 | FeePerWeight last_selection = GetBlockBuilderChunk(dummy); |
1094 | 0 | while (last_selection != FeePerWeight{}) { |
1095 | 0 | last_selection += ret.back(); |
1096 | 0 | ret.emplace_back(last_selection); |
1097 | 0 | IncludeBuilderChunk(); |
1098 | 0 | last_selection = GetBlockBuilderChunk(dummy); |
1099 | 0 | } |
1100 | 0 | StopBlockBuilding(); |
1101 | 0 | return ret; |
1102 | 0 | } |