2013-08-27 07:51:57 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-12-17 02:47:57 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
2014-11-17 03:29:09 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-08-27 07:51:57 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "txmempool.h"
|
2014-09-14 12:43:56 +02:00
|
|
|
|
2014-10-29 02:33:23 +01:00
|
|
|
#include "clientversion.h"
|
2015-01-24 15:29:29 +01:00
|
|
|
#include "consensus/consensus.h"
|
2015-01-24 15:57:12 +01:00
|
|
|
#include "consensus/validation.h"
|
2014-11-12 06:06:15 +01:00
|
|
|
#include "main.h"
|
2014-08-26 22:28:32 +02:00
|
|
|
#include "policy/fees.h"
|
2014-10-22 21:08:30 +02:00
|
|
|
#include "streams.h"
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
#include "util.h"
|
2014-04-23 00:46:19 +02:00
|
|
|
#include "utilmoneystr.h"
|
2014-09-25 05:32:36 +02:00
|
|
|
#include "version.h"
|
2013-08-27 07:51:57 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2014-08-28 15:28:57 +02:00
|
|
|
CTxMemPoolEntry::CTxMemPoolEntry():
|
2014-08-26 22:28:32 +02:00
|
|
|
nFee(0), nTxSize(0), nModSize(0), nTime(0), dPriority(0.0), hadNoDependencies(false)
|
2013-11-11 08:35:14 +01:00
|
|
|
{
|
|
|
|
nHeight = MEMPOOL_HEIGHT;
|
|
|
|
}
|
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
CTxMemPoolEntry::CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
|
2013-11-11 08:35:14 +01:00
|
|
|
int64_t _nTime, double _dPriority,
|
2014-08-26 22:28:32 +02:00
|
|
|
unsigned int _nHeight, bool poolHasNoInputsOf):
|
|
|
|
tx(_tx), nFee(_nFee), nTime(_nTime), dPriority(_dPriority), nHeight(_nHeight),
|
|
|
|
hadNoDependencies(poolHasNoInputsOf)
|
2013-11-11 08:35:14 +01:00
|
|
|
{
|
|
|
|
nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
|
2014-08-26 18:59:21 +02:00
|
|
|
nModSize = tx.CalculateModifiedSize(nTxSize);
|
2013-11-11 08:35:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CTxMemPoolEntry::CTxMemPoolEntry(const CTxMemPoolEntry& other)
|
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
CTxMemPoolEntry::GetPriority(unsigned int currentHeight) const
|
|
|
|
{
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount nValueIn = tx.GetValueOut()+nFee;
|
2014-08-26 18:59:21 +02:00
|
|
|
double deltaPriority = ((double)(currentHeight-nHeight)*nValueIn)/nModSize;
|
2013-11-11 08:35:14 +01:00
|
|
|
double dResult = dPriority + deltaPriority;
|
|
|
|
return dResult;
|
|
|
|
}
|
|
|
|
|
2014-08-28 15:28:57 +02:00
|
|
|
CTxMemPool::CTxMemPool(const CFeeRate& _minRelayFee) :
|
2014-08-26 22:28:32 +02:00
|
|
|
nTransactionsUpdated(0)
|
2013-08-27 07:51:57 +02:00
|
|
|
{
|
|
|
|
// 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;
|
2014-03-17 13:19:54 +01:00
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
minerPolicyEstimator = new CBlockPolicyEstimator(_minRelayFee);
|
2014-03-17 13:19:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CTxMemPool::~CTxMemPool()
|
|
|
|
{
|
|
|
|
delete minerPolicyEstimator;
|
2013-08-27 07:51:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate)
|
2013-08-27 07:51:57 +02:00
|
|
|
{
|
|
|
|
// Add to memory pool without checking anything.
|
|
|
|
// Used by main.cpp AcceptToMemoryPool(), which DOES do
|
|
|
|
// all the appropriate checks.
|
|
|
|
LOCK(cs);
|
2014-08-26 22:28:32 +02:00
|
|
|
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(&tx, i);
|
|
|
|
nTransactionsUpdated++;
|
|
|
|
totalTxSize += entry.GetTxSize();
|
|
|
|
minerPolicyEstimator->processTransaction(entry, fCurrentEstimate);
|
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-18 23:57:20 +01:00
|
|
|
void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive)
|
2013-08-27 07:51:57 +02:00
|
|
|
{
|
|
|
|
// Remove transaction from memory pool
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
2014-11-18 23:57:20 +01:00
|
|
|
std::deque<uint256> txToRemove;
|
|
|
|
txToRemove.push_back(origTx.GetHash());
|
2015-03-25 18:13:09 +01:00
|
|
|
if (fRecursive && !mapTx.count(origTx.GetHash())) {
|
|
|
|
// If recursively removing but origTx isn't in the mempool
|
|
|
|
// be sure to remove any children that are in the pool. This can
|
|
|
|
// happen during chain re-orgs if origTx isn't re-accepted into
|
|
|
|
// the mempool for any reason.
|
|
|
|
for (unsigned int i = 0; i < origTx.vout.size(); i++) {
|
|
|
|
std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(origTx.GetHash(), i));
|
|
|
|
if (it == mapNextTx.end())
|
|
|
|
continue;
|
|
|
|
txToRemove.push_back(it->second.ptx->GetHash());
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 23:57:20 +01:00
|
|
|
while (!txToRemove.empty())
|
2013-08-27 07:51:57 +02:00
|
|
|
{
|
2014-11-18 23:57:20 +01:00
|
|
|
uint256 hash = txToRemove.front();
|
|
|
|
txToRemove.pop_front();
|
|
|
|
if (!mapTx.count(hash))
|
|
|
|
continue;
|
|
|
|
const CTransaction& tx = mapTx[hash].GetTx();
|
|
|
|
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())
|
|
|
|
continue;
|
|
|
|
txToRemove.push_back(it->second.ptx->GetHash());
|
|
|
|
}
|
|
|
|
}
|
2013-08-27 07:51:57 +02:00
|
|
|
BOOST_FOREACH(const CTxIn& txin, tx.vin)
|
|
|
|
mapNextTx.erase(txin.prevout);
|
2014-08-07 05:58:19 +02:00
|
|
|
|
2014-11-18 23:57:20 +01:00
|
|
|
removed.push_back(tx);
|
2014-08-07 05:58:19 +02:00
|
|
|
totalTxSize -= mapTx[hash].GetTxSize();
|
2013-08-27 07:51:57 +02:00
|
|
|
mapTx.erase(hash);
|
|
|
|
nTransactionsUpdated++;
|
2014-08-26 22:28:32 +02:00
|
|
|
minerPolicyEstimator->removeTx(hash);
|
2013-08-27 07:51:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 05:57:54 +01:00
|
|
|
void CTxMemPool::removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight)
|
|
|
|
{
|
|
|
|
// Remove transactions spending a coinbase which are now immature
|
|
|
|
LOCK(cs);
|
|
|
|
list<CTransaction> transactionsToRemove;
|
|
|
|
for (std::map<uint256, CTxMemPoolEntry>::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
|
|
|
|
const CTransaction& tx = it->second.GetTx();
|
|
|
|
BOOST_FOREACH(const CTxIn& txin, tx.vin) {
|
|
|
|
std::map<uint256, CTxMemPoolEntry>::const_iterator it2 = mapTx.find(txin.prevout.hash);
|
|
|
|
if (it2 != mapTx.end())
|
|
|
|
continue;
|
|
|
|
const CCoins *coins = pcoins->AccessCoins(txin.prevout.hash);
|
|
|
|
if (fSanityCheck) assert(coins);
|
2015-05-13 22:04:39 +02:00
|
|
|
if (!coins || (coins->IsCoinBase() && ((signed long)nMemPoolHeight) - coins->nHeight < COINBASE_MATURITY)) {
|
2014-11-12 05:57:54 +01:00
|
|
|
transactionsToRemove.push_back(tx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
|
|
|
|
list<CTransaction> removed;
|
|
|
|
remove(tx, removed, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 22:38:28 +01:00
|
|
|
void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed)
|
2013-08-27 07:51:57 +02:00
|
|
|
{
|
|
|
|
// Remove transactions which depend on inputs of tx, recursively
|
2014-07-17 14:09:55 +02:00
|
|
|
list<CTransaction> result;
|
2013-08-27 07:51:57 +02:00
|
|
|
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)
|
2014-02-15 22:38:28 +01:00
|
|
|
{
|
|
|
|
remove(txConflict, removed, true);
|
|
|
|
}
|
2013-08-27 07:51:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 03:29:09 +01:00
|
|
|
/**
|
|
|
|
* Called when a block is connected. Removes from mempool and updates the miner fee estimator.
|
|
|
|
*/
|
2014-03-17 13:19:54 +01:00
|
|
|
void CTxMemPool::removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
|
2014-08-26 22:28:32 +02:00
|
|
|
std::list<CTransaction>& conflicts, bool fCurrentEstimate)
|
2014-03-17 13:19:54 +01:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
std::vector<CTxMemPoolEntry> entries;
|
|
|
|
BOOST_FOREACH(const CTransaction& tx, vtx)
|
|
|
|
{
|
|
|
|
uint256 hash = tx.GetHash();
|
|
|
|
if (mapTx.count(hash))
|
|
|
|
entries.push_back(mapTx[hash]);
|
|
|
|
}
|
|
|
|
BOOST_FOREACH(const CTransaction& tx, vtx)
|
|
|
|
{
|
|
|
|
std::list<CTransaction> dummy;
|
|
|
|
remove(tx, dummy, false);
|
|
|
|
removeConflicts(tx, conflicts);
|
2012-07-11 20:52:41 +02:00
|
|
|
ClearPrioritisation(tx.GetHash());
|
2014-03-17 13:19:54 +01:00
|
|
|
}
|
2014-08-26 22:28:32 +02:00
|
|
|
// After the txs in the new block have been removed from the mempool, update policy estimates
|
|
|
|
minerPolicyEstimator->processBlock(nBlockHeight, entries, fCurrentEstimate);
|
2014-03-17 13:19:54 +01:00
|
|
|
}
|
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
void CTxMemPool::clear()
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
mapTx.clear();
|
|
|
|
mapNextTx.clear();
|
2014-08-07 05:58:19 +02:00
|
|
|
totalTxSize = 0;
|
2013-08-27 07:51:57 +02:00
|
|
|
++nTransactionsUpdated;
|
|
|
|
}
|
|
|
|
|
2014-07-19 17:14:23 +02:00
|
|
|
void CTxMemPool::check(const CCoinsViewCache *pcoins) const
|
2013-08-27 07:51:57 +02:00
|
|
|
{
|
|
|
|
if (!fSanityCheck)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
|
|
|
|
|
2014-08-07 05:58:19 +02:00
|
|
|
uint64_t checkTotal = 0;
|
|
|
|
|
2014-11-12 06:06:15 +01:00
|
|
|
CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins));
|
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
LOCK(cs);
|
2014-11-12 06:06:15 +01:00
|
|
|
list<const CTxMemPoolEntry*> waitingOnDependants;
|
2013-11-11 08:35:14 +01:00
|
|
|
for (std::map<uint256, CTxMemPoolEntry>::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
|
2013-08-27 07:51:57 +02:00
|
|
|
unsigned int i = 0;
|
2014-08-07 05:58:19 +02:00
|
|
|
checkTotal += it->second.GetTxSize();
|
2013-11-11 08:35:14 +01:00
|
|
|
const CTransaction& tx = it->second.GetTx();
|
2014-11-12 06:06:15 +01:00
|
|
|
bool fDependsWait = false;
|
2013-11-11 08:35:14 +01:00
|
|
|
BOOST_FOREACH(const CTxIn &txin, tx.vin) {
|
2013-08-27 07:51:57 +02:00
|
|
|
// Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
|
2013-11-11 08:35:14 +01:00
|
|
|
std::map<uint256, CTxMemPoolEntry>::const_iterator it2 = mapTx.find(txin.prevout.hash);
|
2013-08-27 07:51:57 +02:00
|
|
|
if (it2 != mapTx.end()) {
|
2013-11-11 08:35:14 +01:00
|
|
|
const CTransaction& tx2 = it2->second.GetTx();
|
|
|
|
assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull());
|
2014-11-12 06:06:15 +01:00
|
|
|
fDependsWait = true;
|
2013-08-27 07:51:57 +02:00
|
|
|
} else {
|
2014-09-02 21:21:15 +02:00
|
|
|
const CCoins* coins = pcoins->AccessCoins(txin.prevout.hash);
|
|
|
|
assert(coins && coins->IsAvailable(txin.prevout.n));
|
2013-08-27 07:51:57 +02:00
|
|
|
}
|
|
|
|
// Check whether its inputs are marked in mapNextTx.
|
|
|
|
std::map<COutPoint, CInPoint>::const_iterator it3 = mapNextTx.find(txin.prevout);
|
|
|
|
assert(it3 != mapNextTx.end());
|
2013-11-11 08:35:14 +01:00
|
|
|
assert(it3->second.ptx == &tx);
|
2013-08-27 07:51:57 +02:00
|
|
|
assert(it3->second.n == i);
|
|
|
|
i++;
|
|
|
|
}
|
2014-11-12 06:06:15 +01:00
|
|
|
if (fDependsWait)
|
|
|
|
waitingOnDependants.push_back(&it->second);
|
|
|
|
else {
|
2014-10-27 16:07:50 +01:00
|
|
|
CValidationState state;
|
2014-11-12 06:06:15 +01:00
|
|
|
assert(CheckInputs(tx, state, mempoolDuplicate, false, 0, false, NULL));
|
2014-10-27 16:07:50 +01:00
|
|
|
UpdateCoins(tx, state, mempoolDuplicate, 1000000);
|
2014-11-12 06:06:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
unsigned int stepsSinceLastRemove = 0;
|
|
|
|
while (!waitingOnDependants.empty()) {
|
|
|
|
const CTxMemPoolEntry* entry = waitingOnDependants.front();
|
|
|
|
waitingOnDependants.pop_front();
|
|
|
|
CValidationState state;
|
|
|
|
if (!mempoolDuplicate.HaveInputs(entry->GetTx())) {
|
|
|
|
waitingOnDependants.push_back(entry);
|
|
|
|
stepsSinceLastRemove++;
|
|
|
|
assert(stepsSinceLastRemove < waitingOnDependants.size());
|
|
|
|
} else {
|
|
|
|
assert(CheckInputs(entry->GetTx(), state, mempoolDuplicate, false, 0, false, NULL));
|
2014-10-27 16:07:50 +01:00
|
|
|
UpdateCoins(entry->GetTx(), state, mempoolDuplicate, 1000000);
|
2014-11-12 06:06:15 +01:00
|
|
|
stepsSinceLastRemove = 0;
|
|
|
|
}
|
2013-08-27 07:51:57 +02:00
|
|
|
}
|
|
|
|
for (std::map<COutPoint, CInPoint>::const_iterator it = mapNextTx.begin(); it != mapNextTx.end(); it++) {
|
|
|
|
uint256 hash = it->second.ptx->GetHash();
|
2013-11-11 08:35:14 +01:00
|
|
|
map<uint256, CTxMemPoolEntry>::const_iterator it2 = mapTx.find(hash);
|
|
|
|
const CTransaction& tx = it2->second.GetTx();
|
2013-08-27 07:51:57 +02:00
|
|
|
assert(it2 != mapTx.end());
|
2013-11-11 08:35:14 +01:00
|
|
|
assert(&tx == it->second.ptx);
|
|
|
|
assert(tx.vin.size() > it->second.n);
|
2013-08-27 07:51:57 +02:00
|
|
|
assert(it->first == it->second.ptx->vin[it->second.n].prevout);
|
|
|
|
}
|
2014-08-07 05:58:19 +02:00
|
|
|
|
|
|
|
assert(totalTxSize == checkTotal);
|
2013-08-27 07:51:57 +02:00
|
|
|
}
|
|
|
|
|
2013-11-11 08:35:14 +01:00
|
|
|
void CTxMemPool::queryHashes(vector<uint256>& vtxid)
|
2013-08-27 07:51:57 +02:00
|
|
|
{
|
|
|
|
vtxid.clear();
|
|
|
|
|
|
|
|
LOCK(cs);
|
|
|
|
vtxid.reserve(mapTx.size());
|
2013-11-11 08:35:14 +01:00
|
|
|
for (map<uint256, CTxMemPoolEntry>::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi)
|
2013-08-27 07:51:57 +02:00
|
|
|
vtxid.push_back((*mi).first);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CTxMemPool::lookup(uint256 hash, CTransaction& result) const
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
2013-11-11 08:35:14 +01:00
|
|
|
map<uint256, CTxMemPoolEntry>::const_iterator i = mapTx.find(hash);
|
2013-08-27 07:51:57 +02:00
|
|
|
if (i == mapTx.end()) return false;
|
2013-11-11 08:35:14 +01:00
|
|
|
result = i->second.GetTx();
|
2013-08-27 07:51:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2014-03-17 13:19:54 +01:00
|
|
|
CFeeRate CTxMemPool::estimateFee(int nBlocks) const
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
return minerPolicyEstimator->estimateFee(nBlocks);
|
|
|
|
}
|
|
|
|
double CTxMemPool::estimatePriority(int nBlocks) const
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
return minerPolicyEstimator->estimatePriority(nBlocks);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
LOCK(cs);
|
2014-08-26 22:28:32 +02:00
|
|
|
fileout << 109900; // version required to read: 0.10.99 or later
|
2014-03-17 13:19:54 +01:00
|
|
|
fileout << CLIENT_VERSION; // version that wrote the file
|
|
|
|
minerPolicyEstimator->Write(fileout);
|
|
|
|
}
|
2014-12-07 13:29:06 +01:00
|
|
|
catch (const std::exception&) {
|
2015-01-08 11:44:25 +01:00
|
|
|
LogPrintf("CTxMemPool::WriteFeeEstimates(): unable to write policy estimator data (non-fatal)");
|
2014-03-17 13:19:54 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CTxMemPool::ReadFeeEstimates(CAutoFile& filein)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
int nVersionRequired, nVersionThatWrote;
|
|
|
|
filein >> nVersionRequired >> nVersionThatWrote;
|
|
|
|
if (nVersionRequired > CLIENT_VERSION)
|
2015-01-08 11:44:25 +01:00
|
|
|
return error("CTxMemPool::ReadFeeEstimates(): up-version (%d) fee estimate file", nVersionRequired);
|
2014-03-17 13:19:54 +01:00
|
|
|
|
|
|
|
LOCK(cs);
|
2014-08-26 22:28:32 +02:00
|
|
|
minerPolicyEstimator->Read(filein);
|
2014-03-17 13:19:54 +01:00
|
|
|
}
|
2014-12-07 13:29:06 +01:00
|
|
|
catch (const std::exception&) {
|
2015-01-08 11:44:25 +01:00
|
|
|
LogPrintf("CTxMemPool::ReadFeeEstimates(): unable to read policy estimator data (non-fatal)");
|
2014-03-17 13:19:54 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
void CTxMemPool::PrioritiseTransaction(const uint256 hash, const string strHash, double dPriorityDelta, const CAmount& nFeeDelta)
|
2012-07-11 20:52:41 +02:00
|
|
|
{
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
2014-04-23 00:46:19 +02:00
|
|
|
std::pair<double, CAmount> &deltas = mapDeltas[hash];
|
2012-07-11 20:52:41 +02:00
|
|
|
deltas.first += dPriorityDelta;
|
|
|
|
deltas.second += nFeeDelta;
|
|
|
|
}
|
2014-04-23 00:46:19 +02:00
|
|
|
LogPrintf("PrioritiseTransaction: %s priority += %f, fee += %d\n", strHash, dPriorityDelta, FormatMoney(nFeeDelta));
|
2012-07-11 20:52:41 +02:00
|
|
|
}
|
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta)
|
2012-07-11 20:52:41 +02:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
2014-04-23 00:46:19 +02:00
|
|
|
std::map<uint256, std::pair<double, CAmount> >::iterator pos = mapDeltas.find(hash);
|
2012-07-11 20:52:41 +02:00
|
|
|
if (pos == mapDeltas.end())
|
|
|
|
return;
|
2014-04-23 00:46:19 +02:00
|
|
|
const std::pair<double, CAmount> &deltas = pos->second;
|
2012-07-11 20:52:41 +02:00
|
|
|
dPriorityDelta += deltas.first;
|
|
|
|
nFeeDelta += deltas.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTxMemPool::ClearPrioritisation(const uint256 hash)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
mapDeltas.erase(hash);
|
|
|
|
}
|
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < tx.vin.size(); i++)
|
|
|
|
if (exists(tx.vin[i].prevout.hash))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-17 13:19:54 +01:00
|
|
|
|
2014-09-24 03:19:04 +02:00
|
|
|
CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2014-07-19 16:42:48 +02:00
|
|
|
bool CCoinsViewMemPool::GetCoins(const uint256 &txid, CCoins &coins) const {
|
2014-07-23 15:28:45 +02:00
|
|
|
// If an entry in the mempool exists, always return that one, as it's guaranteed to never
|
|
|
|
// conflict with the underlying cache, and it cannot have pruned entries (as it contains full)
|
|
|
|
// transactions. First checking the underlying cache risks returning a pruned entry instead.
|
2013-11-05 02:47:07 +01:00
|
|
|
CTransaction tx;
|
|
|
|
if (mempool.lookup(txid, tx)) {
|
|
|
|
coins = CCoins(tx, MEMPOOL_HEIGHT);
|
|
|
|
return true;
|
|
|
|
}
|
2014-07-23 15:28:45 +02:00
|
|
|
return (base->GetCoins(txid, coins) && !coins.IsPruned());
|
2013-11-05 02:47:07 +01:00
|
|
|
}
|
|
|
|
|
2014-07-19 16:42:48 +02:00
|
|
|
bool CCoinsViewMemPool::HaveCoins(const uint256 &txid) const {
|
2013-11-05 02:47:07 +01:00
|
|
|
return mempool.exists(txid) || base->HaveCoins(txid);
|
|
|
|
}
|