Skip to content

Commit

Permalink
Merge pull request #2033 from sipa/kickconflicts
Browse files Browse the repository at this point in the history
Bugfix: remove conflicting transactions from memory pool
  • Loading branch information
sipa committed Dec 1, 2012
2 parents aaef016 + 231b399 commit cd7fb7d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
28 changes: 26 additions & 2 deletions src/main.cpp
Expand Up @@ -818,14 +818,21 @@ bool CTxMemPool::addUnchecked(const uint256& hash, CTransaction &tx)
} }




bool CTxMemPool::remove(CTransaction &tx) bool CTxMemPool::remove(const CTransaction &tx, bool fRecursive)
{ {
// Remove transaction from memory pool // Remove transaction from memory pool
{ {
LOCK(cs); LOCK(cs);
uint256 hash = tx.GetHash(); uint256 hash = tx.GetHash();
if (mapTx.count(hash)) if (mapTx.count(hash))
{ {
if (fRecursive) {
for (unsigned int i = 0; i < tx.vout.size(); i++) {
std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(hash, i));
if (it != mapNextTx.end())
remove(*it->second.ptx, true);
}
}
BOOST_FOREACH(const CTxIn& txin, tx.vin) BOOST_FOREACH(const CTxIn& txin, tx.vin)
mapNextTx.erase(txin.prevout); mapNextTx.erase(txin.prevout);
mapTx.erase(hash); mapTx.erase(hash);
Expand All @@ -835,6 +842,21 @@ bool CTxMemPool::remove(CTransaction &tx)
return true; return true;
} }


bool CTxMemPool::removeConflicts(const CTransaction &tx)
{
// Remove transactions which depend on inputs of tx, recursively
LOCK(cs);
BOOST_FOREACH(const CTxIn &txin, tx.vin) {
std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(txin.prevout);
if (it != mapNextTx.end()) {
const CTransaction &txConflict = *it->second.ptx;
if (txConflict != tx)
remove(txConflict, true);
}
}
return true;
}

void CTxMemPool::clear() void CTxMemPool::clear()
{ {
LOCK(cs); LOCK(cs);
Expand Down Expand Up @@ -1757,8 +1779,10 @@ bool SetBestChain(CBlockIndex* pindexNew)
tx.AcceptToMemoryPool(false); tx.AcceptToMemoryPool(false);


// Delete redundant memory transactions that are in the connected branch // Delete redundant memory transactions that are in the connected branch
BOOST_FOREACH(CTransaction& tx, vDelete) BOOST_FOREACH(CTransaction& tx, vDelete) {
mempool.remove(tx); mempool.remove(tx);
mempool.removeConflicts(tx);
}


// Update best block in wallet (so we can detect restored wallets) // Update best block in wallet (so we can detect restored wallets)
if (!fIsInitialDownload) if (!fIsInitialDownload)
Expand Down
3 changes: 2 additions & 1 deletion src/main.h
Expand Up @@ -1831,7 +1831,8 @@ class CTxMemPool


bool accept(CTransaction &tx, bool fCheckInputs, bool* pfMissingInputs); bool accept(CTransaction &tx, bool fCheckInputs, bool* pfMissingInputs);
bool addUnchecked(const uint256& hash, CTransaction &tx); bool addUnchecked(const uint256& hash, CTransaction &tx);
bool remove(CTransaction &tx); bool remove(const CTransaction &tx, bool fRecursive = false);
bool removeConflicts(const CTransaction &tx);
void clear(); void clear();
void queryHashes(std::vector<uint256>& vtxid); void queryHashes(std::vector<uint256>& vtxid);
void pruneSpent(const uint256& hash, CCoins &coins); void pruneSpent(const uint256& hash, CCoins &coins);
Expand Down

0 comments on commit cd7fb7d

Please sign in to comment.