mirror of https://github.com/lukechilds/komodo.git
Gavin Andresen
11 years ago
11 changed files with 317 additions and 246 deletions
@ -0,0 +1,162 @@ |
|||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|||
// Copyright (c) 2009-2013 The Bitcoin developers
|
|||
// Distributed under the MIT/X11 software license, see the accompanying
|
|||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|||
|
|||
#include "core.h" |
|||
#include "txmempool.h" |
|||
|
|||
using namespace std; |
|||
|
|||
CTxMemPool::CTxMemPool() |
|||
{ |
|||
// Sanity checks off by default for performance, because otherwise
|
|||
// accepting transactions becomes O(N^2) where N is the number
|
|||
// of transactions in the pool
|
|||
fSanityCheck = false; |
|||
} |
|||
|
|||
void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins) |
|||
{ |
|||
LOCK(cs); |
|||
|
|||
std::map<COutPoint, CInPoint>::iterator it = mapNextTx.lower_bound(COutPoint(hashTx, 0)); |
|||
|
|||
// iterate over all COutPoints in mapNextTx whose hash equals the provided hashTx
|
|||
while (it != mapNextTx.end() && it->first.hash == hashTx) { |
|||
coins.Spend(it->first.n); // and remove those outputs from coins
|
|||
it++; |
|||
} |
|||
} |
|||
|
|||
unsigned int CTxMemPool::GetTransactionsUpdated() const |
|||
{ |
|||
LOCK(cs); |
|||
return nTransactionsUpdated; |
|||
} |
|||
|
|||
void CTxMemPool::AddTransactionsUpdated(unsigned int n) |
|||
{ |
|||
LOCK(cs); |
|||
nTransactionsUpdated += n; |
|||
} |
|||
|
|||
|
|||
bool CTxMemPool::addUnchecked(const uint256& hash, const CTransaction &tx) |
|||
{ |
|||
// Add to memory pool without checking anything.
|
|||
// Used by main.cpp AcceptToMemoryPool(), which DOES do
|
|||
// all the appropriate checks.
|
|||
LOCK(cs); |
|||
{ |
|||
mapTx[hash] = tx; |
|||
for (unsigned int i = 0; i < tx.vin.size(); i++) |
|||
mapNextTx[tx.vin[i].prevout] = CInPoint(&mapTx[hash], i); |
|||
nTransactionsUpdated++; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
|
|||
bool CTxMemPool::remove(const CTransaction &tx, bool fRecursive) |
|||
{ |
|||
// Remove transaction from memory pool
|
|||
{ |
|||
LOCK(cs); |
|||
uint256 hash = tx.GetHash(); |
|||
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); |
|||
} |
|||
} |
|||
if (mapTx.count(hash)) |
|||
{ |
|||
BOOST_FOREACH(const CTxIn& txin, tx.vin) |
|||
mapNextTx.erase(txin.prevout); |
|||
mapTx.erase(hash); |
|||
nTransactionsUpdated++; |
|||
} |
|||
} |
|||
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() |
|||
{ |
|||
LOCK(cs); |
|||
mapTx.clear(); |
|||
mapNextTx.clear(); |
|||
++nTransactionsUpdated; |
|||
} |
|||
|
|||
void CTxMemPool::check(CTxMemPool::CoinLookupFunc fnLookup) const |
|||
{ |
|||
if (!fSanityCheck) |
|||
return; |
|||
|
|||
LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size()); |
|||
|
|||
LOCK(cs); |
|||
for (std::map<uint256, CTransaction>::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) { |
|||
unsigned int i = 0; |
|||
BOOST_FOREACH(const CTxIn &txin, it->second.vin) { |
|||
// Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
|
|||
std::map<uint256, CTransaction>::const_iterator it2 = mapTx.find(txin.prevout.hash); |
|||
if (it2 != mapTx.end()) { |
|||
assert(it2->second.vout.size() > txin.prevout.n && !it2->second.vout[txin.prevout.n].IsNull()); |
|||
} else { |
|||
CCoins &coins = (*fnLookup)(txin.prevout.hash); |
|||
assert(coins.IsAvailable(txin.prevout.n)); |
|||
} |
|||
// Check whether its inputs are marked in mapNextTx.
|
|||
std::map<COutPoint, CInPoint>::const_iterator it3 = mapNextTx.find(txin.prevout); |
|||
assert(it3 != mapNextTx.end()); |
|||
assert(it3->second.ptx == &it->second); |
|||
assert(it3->second.n == i); |
|||
i++; |
|||
} |
|||
} |
|||
for (std::map<COutPoint, CInPoint>::const_iterator it = mapNextTx.begin(); it != mapNextTx.end(); it++) { |
|||
uint256 hash = it->second.ptx->GetHash(); |
|||
std::map<uint256, CTransaction>::const_iterator it2 = mapTx.find(hash); |
|||
assert(it2 != mapTx.end()); |
|||
assert(&it2->second == it->second.ptx); |
|||
assert(it2->second.vin.size() > it->second.n); |
|||
assert(it->first == it->second.ptx->vin[it->second.n].prevout); |
|||
} |
|||
} |
|||
|
|||
void CTxMemPool::queryHashes(std::vector<uint256>& vtxid) |
|||
{ |
|||
vtxid.clear(); |
|||
|
|||
LOCK(cs); |
|||
vtxid.reserve(mapTx.size()); |
|||
for (map<uint256, CTransaction>::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi) |
|||
vtxid.push_back((*mi).first); |
|||
} |
|||
|
|||
bool CTxMemPool::lookup(uint256 hash, CTransaction& result) const |
|||
{ |
|||
LOCK(cs); |
|||
std::map<uint256, CTransaction>::const_iterator i = mapTx.find(hash); |
|||
if (i == mapTx.end()) return false; |
|||
result = i->second; |
|||
return true; |
|||
} |
@ -0,0 +1,67 @@ |
|||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|||
// Copyright (c) 2009-2013 The Bitcoin developers
|
|||
// Distributed under the MIT/X11 software license, see the accompanying
|
|||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|||
#ifndef BITCOIN_TXMEMPOOL_H |
|||
#define BITCOIN_TXMEMPOOL_H |
|||
|
|||
#include "core.h" |
|||
|
|||
/*
|
|||
* CTxMemPool stores valid-according-to-the-current-best-chain |
|||
* transactions that may be included in the next block. |
|||
* |
|||
* Transactions are added when they are seen on the network |
|||
* (or created by the local node), but not all transactions seen |
|||
* are added to the pool: if a new transaction double-spends |
|||
* an input of a transaction in the pool, it is dropped, |
|||
* as are non-standard transactions. |
|||
*/ |
|||
class CTxMemPool |
|||
{ |
|||
private: |
|||
bool fSanityCheck; // Normally false, true if -checkmempool or -regtest
|
|||
unsigned int nTransactionsUpdated; |
|||
|
|||
public: |
|||
mutable CCriticalSection cs; |
|||
std::map<uint256, CTransaction> mapTx; |
|||
std::map<COutPoint, CInPoint> mapNextTx; |
|||
|
|||
CTxMemPool(); |
|||
|
|||
/*
|
|||
* If sanity-checking is turned on, check makes sure the pool is |
|||
* consistent (does not contain two transactions that spend the same inputs, |
|||
* all inputs are in the mapNextTx array). If sanity-checking is turned off, |
|||
* check does nothing. |
|||
*/ |
|||
typedef CCoins& (*CoinLookupFunc)(const uint256&); |
|||
void check(CoinLookupFunc fnLookup) const; |
|||
void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; } |
|||
|
|||
bool addUnchecked(const uint256& hash, const CTransaction &tx); |
|||
bool remove(const CTransaction &tx, bool fRecursive = false); |
|||
bool removeConflicts(const CTransaction &tx); |
|||
void clear(); |
|||
void queryHashes(std::vector<uint256>& vtxid); |
|||
void pruneSpent(const uint256& hash, CCoins &coins); |
|||
unsigned int GetTransactionsUpdated() const; |
|||
void AddTransactionsUpdated(unsigned int n); |
|||
|
|||
unsigned long size() |
|||
{ |
|||
LOCK(cs); |
|||
return mapTx.size(); |
|||
} |
|||
|
|||
bool exists(uint256 hash) |
|||
{ |
|||
LOCK(cs); |
|||
return (mapTx.count(hash) != 0); |
|||
} |
|||
|
|||
bool lookup(uint256 hash, CTransaction& result) const; |
|||
}; |
|||
|
|||
#endif /* BITCOIN_TXMEMPOOL_H */ |
Loading…
Reference in new issue