Skip to content

Commit 873972f

Browse files
committed
txmempool: Store pointers to transactions claiming SPKs
1 parent 80627c9 commit 873972f

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

src/kernel/mempool_entry.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ struct LockPoints {
3737

3838
enum MemPool_SPK_State {
3939
MSS_UNSEEN = 0,
40-
MSS_SPENT = 1,
41-
MSS_CREATED = 2,
40+
MSS_SPENT = 1, // .second
41+
MSS_CREATED = 2, // .first
4242
MSS_BOTH = 3,
4343
};
4444

src/txmempool.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,14 @@ void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry, setEntries &setAnces
487487
newit->idx_randomized = txns_randomized.size() - 1;
488488

489489
for (auto& vSPK : entry.mapSPK) {
490-
mapUsedSPK[vSPK.first] = MemPool_SPK_State(mapUsedSPK[vSPK.first] | vSPK.second);
490+
const uint160& SPKKey = vSPK.first;
491+
const MemPool_SPK_State& claims = vSPK.second;
492+
if (claims & MSS_CREATED) {
493+
mapUsedSPK[SPKKey].first = &tx;
494+
}
495+
if (claims & MSS_SPENT) {
496+
mapUsedSPK[SPKKey].second = &tx;
497+
}
491498
}
492499

493500
TRACE3(mempool, added,
@@ -518,6 +525,7 @@ void CTxMemPool::removeUnchecked(txiter it, MemPoolRemovalReason reason)
518525
std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(it->GetTime()).count()
519526
);
520527

528+
const CTransaction& tx = it->GetTx();
521529
for (const CTxIn& txin : it->GetTx().vin)
522530
mapNextTx.erase(txin.prevout);
523531

@@ -535,10 +543,15 @@ void CTxMemPool::removeUnchecked(txiter it, MemPoolRemovalReason reason)
535543
txns_randomized.clear();
536544

537545
for (auto& vSPK : it->mapSPK) {
538-
if (vSPK.second == mapUsedSPK.find(vSPK.first)->second) {
539-
mapUsedSPK.erase(vSPK.first);
540-
} else {
541-
mapUsedSPK[vSPK.first] = MemPool_SPK_State(mapUsedSPK[vSPK.first] & ~vSPK.second);
546+
const uint160& SPKKey = vSPK.first;
547+
if (mapUsedSPK[SPKKey].first == &tx) {
548+
mapUsedSPK[SPKKey].first = NULL;
549+
}
550+
if (mapUsedSPK[SPKKey].second == &tx) {
551+
mapUsedSPK[SPKKey].second = NULL;
552+
}
553+
if (!(mapUsedSPK[SPKKey].first || mapUsedSPK[SPKKey].second)) {
554+
mapUsedSPK.erase(SPKKey);
542555
}
543556
}
544557

src/txmempool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ class CTxMemPool
402402

403403
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
404404

405-
SPKStates_t mapUsedSPK;
405+
std::map<uint160, std::pair<const CTransaction *, const CTransaction *>> mapUsedSPK;
406406

407407
private:
408408
typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;

src/validation.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -959,9 +959,9 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
959959
return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "txn-spk-reused-change");
960960
}
961961
}
962-
mssit = m_pool.mapUsedSPK.find(hashSPK);
963-
if (mssit != m_pool.mapUsedSPK.end()) {
964-
if (mssit->second & MSS_SPENT) {
962+
const auto& SPKit = m_pool.mapUsedSPK.find(hashSPK);
963+
if (SPKit != m_pool.mapUsedSPK.end()) {
964+
if (SPKit->second.second /* Spent */) {
965965
return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "txn-mempool-spk-reused-spend");
966966
}
967967
}

0 commit comments

Comments
 (0)