|
|
@ -8,6 +8,33 @@ |
|
|
|
|
|
|
|
using namespace std; |
|
|
|
|
|
|
|
CTxMemPoolEntry::CTxMemPoolEntry() |
|
|
|
{ |
|
|
|
nHeight = MEMPOOL_HEIGHT; |
|
|
|
} |
|
|
|
|
|
|
|
CTxMemPoolEntry::CTxMemPoolEntry(const CTransaction& _tx, int64_t _nFee, |
|
|
|
int64_t _nTime, double _dPriority, |
|
|
|
unsigned int _nHeight): |
|
|
|
tx(_tx), nFee(_nFee), nTime(_nTime), dPriority(_dPriority), nHeight(_nHeight) |
|
|
|
{ |
|
|
|
nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION); |
|
|
|
} |
|
|
|
|
|
|
|
CTxMemPoolEntry::CTxMemPoolEntry(const CTxMemPoolEntry& other) |
|
|
|
{ |
|
|
|
*this = other; |
|
|
|
} |
|
|
|
|
|
|
|
double |
|
|
|
CTxMemPoolEntry::GetPriority(unsigned int currentHeight) const |
|
|
|
{ |
|
|
|
int64_t nValueIn = tx.GetValueOut()+nFee; |
|
|
|
double deltaPriority = ((double)(currentHeight-nHeight)*nValueIn)/nTxSize; |
|
|
|
double dResult = dPriority + deltaPriority; |
|
|
|
return dResult; |
|
|
|
} |
|
|
|
|
|
|
|
CTxMemPool::CTxMemPool() |
|
|
|
{ |
|
|
|
// Sanity checks off by default for performance, because otherwise
|
|
|
@ -42,16 +69,17 @@ void CTxMemPool::AddTransactionsUpdated(unsigned int n) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool CTxMemPool::addUnchecked(const uint256& hash, const CTransaction &tx) |
|
|
|
bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry) |
|
|
|
{ |
|
|
|
// Add to memory pool without checking anything.
|
|
|
|
// Used by main.cpp AcceptToMemoryPool(), which DOES do
|
|
|
|
// all the appropriate checks.
|
|
|
|
LOCK(cs); |
|
|
|
{ |
|
|
|
mapTx[hash] = tx; |
|
|
|
mapTx[hash] = entry; |
|
|
|
const CTransaction& tx = mapTx[hash].GetTx(); |
|
|
|
for (unsigned int i = 0; i < tx.vin.size(); i++) |
|
|
|
mapNextTx[tx.vin[i].prevout] = CInPoint(&mapTx[hash], i); |
|
|
|
mapNextTx[tx.vin[i].prevout] = CInPoint(&tx, i); |
|
|
|
nTransactionsUpdated++; |
|
|
|
} |
|
|
|
return true; |
|
|
@ -113,13 +141,15 @@ void CTxMemPool::check(CCoinsViewCache *pcoins) const |
|
|
|
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++) { |
|
|
|
for (std::map<uint256, CTxMemPoolEntry>::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) { |
|
|
|
unsigned int i = 0; |
|
|
|
BOOST_FOREACH(const CTxIn &txin, it->second.vin) { |
|
|
|
const CTransaction& tx = it->second.GetTx(); |
|
|
|
BOOST_FOREACH(const CTxIn &txin, tx.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); |
|
|
|
std::map<uint256, CTxMemPoolEntry>::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()); |
|
|
|
const CTransaction& tx2 = it2->second.GetTx(); |
|
|
|
assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull()); |
|
|
|
} else { |
|
|
|
CCoins &coins = pcoins->GetCoins(txin.prevout.hash); |
|
|
|
assert(coins.IsAvailable(txin.prevout.n)); |
|
|
@ -127,37 +157,38 @@ void CTxMemPool::check(CCoinsViewCache *pcoins) const |
|
|
|
// 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.ptx == &tx); |
|
|
|
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); |
|
|
|
map<uint256, CTxMemPoolEntry>::const_iterator it2 = mapTx.find(hash); |
|
|
|
const CTransaction& tx = it2->second.GetTx(); |
|
|
|
assert(it2 != mapTx.end()); |
|
|
|
assert(&it2->second == it->second.ptx); |
|
|
|
assert(it2->second.vin.size() > it->second.n); |
|
|
|
assert(&tx == it->second.ptx); |
|
|
|
assert(tx.vin.size() > it->second.n); |
|
|
|
assert(it->first == it->second.ptx->vin[it->second.n].prevout); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void CTxMemPool::queryHashes(std::vector<uint256>& vtxid) |
|
|
|
void CTxMemPool::queryHashes(vector<uint256>& vtxid) |
|
|
|
{ |
|
|
|
vtxid.clear(); |
|
|
|
|
|
|
|
LOCK(cs); |
|
|
|
vtxid.reserve(mapTx.size()); |
|
|
|
for (map<uint256, CTransaction>::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi) |
|
|
|
for (map<uint256, CTxMemPoolEntry>::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); |
|
|
|
map<uint256, CTxMemPoolEntry>::const_iterator i = mapTx.find(hash); |
|
|
|
if (i == mapTx.end()) return false; |
|
|
|
result = i->second; |
|
|
|
result = i->second.GetTx(); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|