Skip to content

Commit 535424a

Browse files
committed
Merge bitcoin#28903: refactor: Make CTxMemPoolEntry only explicitly copyable
705e3f1 refactor: Make CTxMemPoolEntry only explicitly copyable (TheCharlatan) Pull request description: This has the goal of prohibiting users from accidentally creating runtime failures, e.g. by interacting with iterator_to with a copied entry. This was brought up here: bitcoin#28886 (comment). CTxMemPoolEntry is already implicitly not move-constructable. So be explicit about this and use a std::list to collect the values in the policy_estimator fuzz test instead of a std::vector. ACKs for top commit: maflcko: ACK 705e3f1 🌯 achow101: ACK 705e3f1 ajtowns: ACK 705e3f1 ismaelsadeeq: ACK 705e3f1 Tree-SHA512: 62056905c679c919d00f9ae065ed66ac986e7e7062015aea542843d8deecda57104d7a68d002f7b20afa3164f8e9215d2d2d002c167224129540e3b1bd0712cc
2 parents 30a0557 + 705e3f1 commit 535424a

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/kernel/mempool_entry.h

+12
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ class CTxMemPoolEntry
7171
typedef std::set<CTxMemPoolEntryRef, CompareIteratorByHash> Children;
7272

7373
private:
74+
CTxMemPoolEntry(const CTxMemPoolEntry&) = default;
75+
struct ExplicitCopyTag {
76+
explicit ExplicitCopyTag() = default;
77+
};
78+
7479
const CTransactionRef tx;
7580
mutable Parents m_parents;
7681
mutable Children m_children;
@@ -122,6 +127,13 @@ class CTxMemPoolEntry
122127
nModFeesWithAncestors{nFee},
123128
nSigOpCostWithAncestors{sigOpCost} {}
124129

130+
CTxMemPoolEntry(ExplicitCopyTag, const CTxMemPoolEntry& entry) : CTxMemPoolEntry(entry) {}
131+
CTxMemPoolEntry& operator=(const CTxMemPoolEntry&) = delete;
132+
CTxMemPoolEntry(CTxMemPoolEntry&&) = delete;
133+
CTxMemPoolEntry& operator=(CTxMemPoolEntry&&) = delete;
134+
135+
static constexpr ExplicitCopyTag ExplicitCopy{};
136+
125137
const CTransaction& GetTx() const { return *this->tx; }
126138
CTransactionRef GetSharedTx() const { return this->tx; }
127139
const CAmount& GetFee() const { return nFee; }

src/test/fuzz/policy_estimator.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ FUZZ_TARGET(policy_estimator, .init = initialize_policy_estimator)
5050
}
5151
},
5252
[&] {
53-
std::vector<CTxMemPoolEntry> mempool_entries;
53+
std::list<CTxMemPoolEntry> mempool_entries;
5454
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
5555
{
5656
const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
@@ -59,7 +59,7 @@ FUZZ_TARGET(policy_estimator, .init = initialize_policy_estimator)
5959
break;
6060
}
6161
const CTransaction tx{*mtx};
62-
mempool_entries.push_back(ConsumeTxMemPoolEntry(fuzzed_data_provider, tx));
62+
mempool_entries.emplace_back(CTxMemPoolEntry::ExplicitCopy, ConsumeTxMemPoolEntry(fuzzed_data_provider, tx));
6363
}
6464
std::vector<const CTxMemPoolEntry*> ptrs;
6565
ptrs.reserve(mempool_entries.size());

src/txmempool.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry, setEntries &setAnces
438438
// Add to memory pool without checking anything.
439439
// Used by AcceptToMemoryPool(), which DOES do
440440
// all the appropriate checks.
441-
indexed_transaction_set::iterator newit = mapTx.insert(entry).first;
441+
indexed_transaction_set::iterator newit = mapTx.emplace(CTxMemPoolEntry::ExplicitCopy, entry).first;
442442

443443
// Update transaction for any feeDelta created by PrioritiseTransaction
444444
CAmount delta{0};

0 commit comments

Comments
 (0)