2013-08-27 07:51:57 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-11-17 03:29:09 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin developers
|
|
|
|
// 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"
|
2014-11-12 06:06:15 +01:00
|
|
|
#include "main.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
|
|
|
|
2014-03-17 13:19:54 +01:00
|
|
|
#include <boost/circular_buffer.hpp>
|
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
using namespace std;
|
|
|
|
|
2014-08-28 15:28:57 +02:00
|
|
|
CTxMemPoolEntry::CTxMemPoolEntry():
|
2014-09-16 10:28:53 +02:00
|
|
|
nFee(0), nTxSize(0), nModSize(0), nTime(0), dPriority(0.0)
|
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,
|
|
|
|
unsigned int _nHeight):
|
|
|
|
tx(_tx), nFee(_nFee), nTime(_nTime), dPriority(_dPriority), nHeight(_nHeight)
|
|
|
|
{
|
|
|
|
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-11-17 03:29:09 +01:00
|
|
|
/**
|
|
|
|
* Keep track of fee/priority for transactions confirmed within N blocks
|
|
|
|
*/
|
2014-03-17 13:19:54 +01:00
|
|
|
class CBlockAverage
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
boost::circular_buffer<CFeeRate> feeSamples;
|
|
|
|
boost::circular_buffer<double> prioritySamples;
|
|
|
|
|
|
|
|
template<typename T> std::vector<T> buf2vec(boost::circular_buffer<T> buf) const
|
|
|
|
{
|
|
|
|
std::vector<T> vec(buf.begin(), buf.end());
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
CBlockAverage() : feeSamples(100), prioritySamples(100) { }
|
|
|
|
|
|
|
|
void RecordFee(const CFeeRate& feeRate) {
|
|
|
|
feeSamples.push_back(feeRate);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecordPriority(double priority) {
|
|
|
|
prioritySamples.push_back(priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t FeeSamples() const { return feeSamples.size(); }
|
|
|
|
size_t GetFeeSamples(std::vector<CFeeRate>& insertInto) const
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(const CFeeRate& f, feeSamples)
|
|
|
|
insertInto.push_back(f);
|
|
|
|
return feeSamples.size();
|
|
|
|
}
|
|
|
|
size_t PrioritySamples() const { return prioritySamples.size(); }
|
|
|
|
size_t GetPrioritySamples(std::vector<double>& insertInto) const
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(double d, prioritySamples)
|
|
|
|
insertInto.push_back(d);
|
|
|
|
return prioritySamples.size();
|
|
|
|
}
|
|
|
|
|
2014-11-17 03:29:09 +01:00
|
|
|
/**
|
|
|
|
* Used as belt-and-suspenders check when reading to detect
|
|
|
|
* file corruption
|
|
|
|
*/
|
2014-07-03 20:25:32 +02:00
|
|
|
bool AreSane(const std::vector<CFeeRate>& vecFee, const CFeeRate& minRelayFee)
|
2014-03-17 13:19:54 +01:00
|
|
|
{
|
|
|
|
BOOST_FOREACH(CFeeRate fee, vecFee)
|
|
|
|
{
|
|
|
|
if (fee < CFeeRate(0))
|
|
|
|
return false;
|
2014-07-03 20:25:32 +02:00
|
|
|
if (fee.GetFeePerK() > minRelayFee.GetFeePerK() * 10000)
|
2014-03-17 13:19:54 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool AreSane(const std::vector<double> vecPriority)
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(double priority, vecPriority)
|
|
|
|
{
|
|
|
|
if (priority < 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Write(CAutoFile& fileout) const
|
|
|
|
{
|
|
|
|
std::vector<CFeeRate> vecFee = buf2vec(feeSamples);
|
|
|
|
fileout << vecFee;
|
|
|
|
std::vector<double> vecPriority = buf2vec(prioritySamples);
|
|
|
|
fileout << vecPriority;
|
|
|
|
}
|
|
|
|
|
2014-07-03 20:25:32 +02:00
|
|
|
void Read(CAutoFile& filein, const CFeeRate& minRelayFee) {
|
2014-03-17 13:19:54 +01:00
|
|
|
std::vector<CFeeRate> vecFee;
|
|
|
|
filein >> vecFee;
|
2014-07-03 20:25:32 +02:00
|
|
|
if (AreSane(vecFee, minRelayFee))
|
2014-03-17 13:19:54 +01:00
|
|
|
feeSamples.insert(feeSamples.end(), vecFee.begin(), vecFee.end());
|
|
|
|
else
|
|
|
|
throw runtime_error("Corrupt fee value in estimates file.");
|
|
|
|
std::vector<double> vecPriority;
|
|
|
|
filein >> vecPriority;
|
|
|
|
if (AreSane(vecPriority))
|
|
|
|
prioritySamples.insert(prioritySamples.end(), vecPriority.begin(), vecPriority.end());
|
|
|
|
else
|
|
|
|
throw runtime_error("Corrupt priority value in estimates file.");
|
|
|
|
if (feeSamples.size() + prioritySamples.size() > 0)
|
|
|
|
LogPrint("estimatefee", "Read %d fee samples and %d priority samples\n",
|
|
|
|
feeSamples.size(), prioritySamples.size());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CMinerPolicyEstimator
|
|
|
|
{
|
|
|
|
private:
|
2014-11-17 03:29:09 +01:00
|
|
|
/**
|
|
|
|
* Records observed averages transactions that confirmed within one block, two blocks,
|
|
|
|
* three blocks etc.
|
|
|
|
*/
|
2014-03-17 13:19:54 +01:00
|
|
|
std::vector<CBlockAverage> history;
|
|
|
|
std::vector<CFeeRate> sortedFeeSamples;
|
|
|
|
std::vector<double> sortedPrioritySamples;
|
|
|
|
|
|
|
|
int nBestSeenHeight;
|
|
|
|
|
2014-11-17 03:29:09 +01:00
|
|
|
/**
|
|
|
|
* nBlocksAgo is 0 based, i.e. transactions that confirmed in the highest seen block are
|
|
|
|
* nBlocksAgo == 0, transactions in the block before that are nBlocksAgo == 1 etc.
|
|
|
|
*/
|
2014-07-03 20:25:32 +02:00
|
|
|
void seenTxConfirm(const CFeeRate& feeRate, const CFeeRate& minRelayFee, double dPriority, int nBlocksAgo)
|
2014-03-17 13:19:54 +01:00
|
|
|
{
|
|
|
|
// Last entry records "everything else".
|
|
|
|
int nBlocksTruncated = min(nBlocksAgo, (int) history.size() - 1);
|
|
|
|
assert(nBlocksTruncated >= 0);
|
|
|
|
|
|
|
|
// We need to guess why the transaction was included in a block-- either
|
|
|
|
// because it is high-priority or because it has sufficient fees.
|
2014-07-03 20:25:32 +02:00
|
|
|
bool sufficientFee = (feeRate > minRelayFee);
|
2014-03-17 13:19:54 +01:00
|
|
|
bool sufficientPriority = AllowFree(dPriority);
|
|
|
|
const char* assignedTo = "unassigned";
|
|
|
|
if (sufficientFee && !sufficientPriority)
|
|
|
|
{
|
|
|
|
history[nBlocksTruncated].RecordFee(feeRate);
|
|
|
|
assignedTo = "fee";
|
|
|
|
}
|
|
|
|
else if (sufficientPriority && !sufficientFee)
|
|
|
|
{
|
|
|
|
history[nBlocksTruncated].RecordPriority(dPriority);
|
|
|
|
assignedTo = "priority";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Neither or both fee and priority sufficient to get confirmed:
|
|
|
|
// don't know why they got confirmed.
|
|
|
|
}
|
|
|
|
LogPrint("estimatefee", "Seen TX confirm: %s : %s fee/%g priority, took %d blocks\n",
|
|
|
|
assignedTo, feeRate.ToString(), dPriority, nBlocksAgo);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
CMinerPolicyEstimator(int nEntries) : nBestSeenHeight(0)
|
|
|
|
{
|
|
|
|
history.resize(nEntries);
|
|
|
|
}
|
|
|
|
|
2014-07-03 20:25:32 +02:00
|
|
|
void seenBlock(const std::vector<CTxMemPoolEntry>& entries, int nBlockHeight, const CFeeRate minRelayFee)
|
2014-03-17 13:19:54 +01:00
|
|
|
{
|
|
|
|
if (nBlockHeight <= nBestSeenHeight)
|
|
|
|
{
|
|
|
|
// Ignore side chains and re-orgs; assuming they are random
|
|
|
|
// they don't affect the estimate.
|
|
|
|
// And if an attacker can re-org the chain at will, then
|
|
|
|
// you've got much bigger problems than "attacker can influence
|
|
|
|
// transaction fees."
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nBestSeenHeight = nBlockHeight;
|
|
|
|
|
|
|
|
// Fill up the history buckets based on how long transactions took
|
|
|
|
// to confirm.
|
|
|
|
std::vector<std::vector<const CTxMemPoolEntry*> > entriesByConfirmations;
|
|
|
|
entriesByConfirmations.resize(history.size());
|
|
|
|
BOOST_FOREACH(const CTxMemPoolEntry& entry, entries)
|
|
|
|
{
|
|
|
|
// How many blocks did it take for miners to include this transaction?
|
|
|
|
int delta = nBlockHeight - entry.GetHeight();
|
|
|
|
if (delta <= 0)
|
|
|
|
{
|
|
|
|
// Re-org made us lose height, this should only happen if we happen
|
|
|
|
// to re-org on a difficulty transition point: very rare!
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((delta-1) >= (int)history.size())
|
|
|
|
delta = history.size(); // Last bucket is catch-all
|
2014-07-29 06:09:57 +02:00
|
|
|
entriesByConfirmations.at(delta-1).push_back(&entry);
|
2014-03-17 13:19:54 +01:00
|
|
|
}
|
|
|
|
for (size_t i = 0; i < entriesByConfirmations.size(); i++)
|
|
|
|
{
|
|
|
|
std::vector<const CTxMemPoolEntry*> &e = entriesByConfirmations.at(i);
|
|
|
|
// Insert at most 10 random entries per bucket, otherwise a single block
|
|
|
|
// can dominate an estimate:
|
|
|
|
if (e.size() > 10) {
|
|
|
|
std::random_shuffle(e.begin(), e.end());
|
|
|
|
e.resize(10);
|
|
|
|
}
|
|
|
|
BOOST_FOREACH(const CTxMemPoolEntry* entry, e)
|
|
|
|
{
|
|
|
|
// Fees are stored and reported as BTC-per-kb:
|
|
|
|
CFeeRate feeRate(entry->GetFee(), entry->GetTxSize());
|
|
|
|
double dPriority = entry->GetPriority(entry->GetHeight()); // Want priority when it went IN
|
2014-07-03 20:25:32 +02:00
|
|
|
seenTxConfirm(feeRate, minRelayFee, dPriority, i);
|
2014-03-17 13:19:54 +01:00
|
|
|
}
|
|
|
|
}
|
2014-07-26 03:29:54 +02:00
|
|
|
|
2014-11-25 18:54:36 +01:00
|
|
|
// After new samples are added, we have to clear the sorted lists,
|
|
|
|
// so they'll be resorted the next time someone asks for an estimate
|
2014-07-26 03:29:54 +02:00
|
|
|
sortedFeeSamples.clear();
|
|
|
|
sortedPrioritySamples.clear();
|
|
|
|
|
2014-03-17 13:19:54 +01:00
|
|
|
for (size_t i = 0; i < history.size(); i++) {
|
|
|
|
if (history[i].FeeSamples() + history[i].PrioritySamples() > 0)
|
|
|
|
LogPrint("estimatefee", "estimates: for confirming within %d blocks based on %d/%d samples, fee=%s, prio=%g\n",
|
|
|
|
i,
|
|
|
|
history[i].FeeSamples(), history[i].PrioritySamples(),
|
|
|
|
estimateFee(i+1).ToString(), estimatePriority(i+1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 03:29:09 +01:00
|
|
|
/**
|
|
|
|
* Can return CFeeRate(0) if we don't have any data for that many blocks back. nBlocksToConfirm is 1 based.
|
|
|
|
*/
|
2014-03-17 13:19:54 +01:00
|
|
|
CFeeRate estimateFee(int nBlocksToConfirm)
|
|
|
|
{
|
|
|
|
nBlocksToConfirm--;
|
|
|
|
|
|
|
|
if (nBlocksToConfirm < 0 || nBlocksToConfirm >= (int)history.size())
|
|
|
|
return CFeeRate(0);
|
|
|
|
|
|
|
|
if (sortedFeeSamples.size() == 0)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < history.size(); i++)
|
|
|
|
history.at(i).GetFeeSamples(sortedFeeSamples);
|
|
|
|
std::sort(sortedFeeSamples.begin(), sortedFeeSamples.end(),
|
|
|
|
std::greater<CFeeRate>());
|
|
|
|
}
|
2014-06-23 16:58:59 +02:00
|
|
|
if (sortedFeeSamples.size() < 11)
|
|
|
|
{
|
|
|
|
// Eleven is Gavin's Favorite Number
|
|
|
|
// ... but we also take a maximum of 10 samples per block so eleven means
|
|
|
|
// we're getting samples from at least two different blocks
|
2014-03-17 13:19:54 +01:00
|
|
|
return CFeeRate(0);
|
2014-06-23 16:58:59 +02:00
|
|
|
}
|
2014-03-17 13:19:54 +01:00
|
|
|
|
|
|
|
int nBucketSize = history.at(nBlocksToConfirm).FeeSamples();
|
|
|
|
|
|
|
|
// Estimates should not increase as number of confirmations goes up,
|
|
|
|
// but the estimates are noisy because confirmations happen discretely
|
|
|
|
// in blocks. To smooth out the estimates, use all samples in the history
|
|
|
|
// and use the nth highest where n is (number of samples in previous bucket +
|
|
|
|
// half the samples in nBlocksToConfirm bucket):
|
|
|
|
size_t nPrevSize = 0;
|
|
|
|
for (int i = 0; i < nBlocksToConfirm; i++)
|
|
|
|
nPrevSize += history.at(i).FeeSamples();
|
|
|
|
size_t index = min(nPrevSize + nBucketSize/2, sortedFeeSamples.size()-1);
|
|
|
|
return sortedFeeSamples[index];
|
|
|
|
}
|
|
|
|
double estimatePriority(int nBlocksToConfirm)
|
|
|
|
{
|
|
|
|
nBlocksToConfirm--;
|
|
|
|
|
|
|
|
if (nBlocksToConfirm < 0 || nBlocksToConfirm >= (int)history.size())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (sortedPrioritySamples.size() == 0)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < history.size(); i++)
|
|
|
|
history.at(i).GetPrioritySamples(sortedPrioritySamples);
|
|
|
|
std::sort(sortedPrioritySamples.begin(), sortedPrioritySamples.end(),
|
|
|
|
std::greater<double>());
|
|
|
|
}
|
2014-06-23 16:58:59 +02:00
|
|
|
if (sortedPrioritySamples.size() < 11)
|
2014-03-17 13:19:54 +01:00
|
|
|
return -1.0;
|
|
|
|
|
|
|
|
int nBucketSize = history.at(nBlocksToConfirm).PrioritySamples();
|
|
|
|
|
|
|
|
// Estimates should not increase as number of confirmations needed goes up,
|
|
|
|
// but the estimates are noisy because confirmations happen discretely
|
|
|
|
// in blocks. To smooth out the estimates, use all samples in the history
|
|
|
|
// and use the nth highest where n is (number of samples in previous buckets +
|
|
|
|
// half the samples in nBlocksToConfirm bucket).
|
|
|
|
size_t nPrevSize = 0;
|
|
|
|
for (int i = 0; i < nBlocksToConfirm; i++)
|
|
|
|
nPrevSize += history.at(i).PrioritySamples();
|
2014-07-26 03:21:10 +02:00
|
|
|
size_t index = min(nPrevSize + nBucketSize/2, sortedPrioritySamples.size()-1);
|
2014-03-17 13:19:54 +01:00
|
|
|
return sortedPrioritySamples[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Write(CAutoFile& fileout) const
|
|
|
|
{
|
|
|
|
fileout << nBestSeenHeight;
|
|
|
|
fileout << history.size();
|
|
|
|
BOOST_FOREACH(const CBlockAverage& entry, history)
|
|
|
|
{
|
|
|
|
entry.Write(fileout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 20:25:32 +02:00
|
|
|
void Read(CAutoFile& filein, const CFeeRate& minRelayFee)
|
2014-03-17 13:19:54 +01:00
|
|
|
{
|
2014-07-29 06:09:57 +02:00
|
|
|
int nFileBestSeenHeight;
|
|
|
|
filein >> nFileBestSeenHeight;
|
2014-03-17 13:19:54 +01:00
|
|
|
size_t numEntries;
|
|
|
|
filein >> numEntries;
|
2014-07-29 06:09:57 +02:00
|
|
|
if (numEntries <= 0 || numEntries > 10000)
|
2014-11-17 03:29:09 +01:00
|
|
|
throw runtime_error("Corrupt estimates file. Must have between 1 and 10k entries.");
|
2014-07-29 06:09:57 +02:00
|
|
|
|
|
|
|
std::vector<CBlockAverage> fileHistory;
|
|
|
|
|
2014-03-17 13:19:54 +01:00
|
|
|
for (size_t i = 0; i < numEntries; i++)
|
|
|
|
{
|
|
|
|
CBlockAverage entry;
|
2014-07-03 20:25:32 +02:00
|
|
|
entry.Read(filein, minRelayFee);
|
2014-07-29 06:09:57 +02:00
|
|
|
fileHistory.push_back(entry);
|
2014-03-17 13:19:54 +01:00
|
|
|
}
|
2014-07-29 06:09:57 +02:00
|
|
|
|
2014-10-31 09:36:30 +01:00
|
|
|
// Now that we've processed the entire fee estimate data file and not
|
|
|
|
// thrown any errors, we can copy it to our history
|
2014-07-29 06:09:57 +02:00
|
|
|
nBestSeenHeight = nFileBestSeenHeight;
|
|
|
|
history = fileHistory;
|
|
|
|
assert(history.size() > 0);
|
2014-03-17 13:19:54 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-08-28 15:28:57 +02:00
|
|
|
CTxMemPool::CTxMemPool(const CFeeRate& _minRelayFee) :
|
|
|
|
nTransactionsUpdated(0),
|
|
|
|
minRelayFee(_minRelayFee)
|
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
|
|
|
|
|
|
|
// 25 blocks is a compromise between using a lot of disk/memory and
|
|
|
|
// trying to give accurate estimates to people who might be willing
|
|
|
|
// to wait a day or two to save a fraction of a penny in fees.
|
|
|
|
// Confirmation times for very-low-fee transactions that take more
|
|
|
|
// than an hour or three to confirm are highly variable.
|
|
|
|
minerPolicyEstimator = new CMinerPolicyEstimator(25);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-11 08:35:14 +01:00
|
|
|
bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry)
|
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);
|
|
|
|
{
|
2013-11-11 08:35:14 +01:00
|
|
|
mapTx[hash] = entry;
|
|
|
|
const CTransaction& tx = mapTx[hash].GetTx();
|
2013-08-27 07:51:57 +02:00
|
|
|
for (unsigned int i = 0; i < tx.vin.size(); i++)
|
2013-11-11 08:35:14 +01:00
|
|
|
mapNextTx[tx.vin[i].prevout] = CInPoint(&tx, i);
|
2013-08-27 07:51:57 +02:00
|
|
|
nTransactionsUpdated++;
|
2014-08-07 05:58:19 +02:00
|
|
|
totalTxSize += entry.GetTxSize();
|
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());
|
|
|
|
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-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);
|
|
|
|
if (!coins || (coins->IsCoinBase() && nMemPoolHeight - coins->nHeight < COINBASE_MATURITY)) {
|
|
|
|
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,
|
|
|
|
std::list<CTransaction>& conflicts)
|
|
|
|
{
|
|
|
|
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]);
|
|
|
|
}
|
2014-07-03 20:25:32 +02:00
|
|
|
minerPolicyEstimator->seenBlock(entries, nBlockHeight, minRelayFee);
|
2014-03-17 13:19:54 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
CValidationState state; CTxUndo undo;
|
|
|
|
assert(CheckInputs(tx, state, mempoolDuplicate, false, 0, false, NULL));
|
|
|
|
UpdateCoins(tx, state, mempoolDuplicate, undo, 1000000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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));
|
|
|
|
CTxUndo undo;
|
|
|
|
UpdateCoins(entry->GetTx(), state, mempoolDuplicate, undo, 1000000);
|
|
|
|
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);
|
|
|
|
fileout << 99900; // version required to read: 0.9.99 or later
|
|
|
|
fileout << CLIENT_VERSION; // version that wrote the file
|
|
|
|
minerPolicyEstimator->Write(fileout);
|
|
|
|
}
|
2014-09-05 17:37:01 +02:00
|
|
|
catch (const std::exception &) {
|
2014-03-17 13:19:54 +01:00
|
|
|
LogPrintf("CTxMemPool::WriteFeeEstimates() : unable to write policy estimator data (non-fatal)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CTxMemPool::ReadFeeEstimates(CAutoFile& filein)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
int nVersionRequired, nVersionThatWrote;
|
|
|
|
filein >> nVersionRequired >> nVersionThatWrote;
|
|
|
|
if (nVersionRequired > CLIENT_VERSION)
|
|
|
|
return error("CTxMemPool::ReadFeeEstimates() : up-version (%d) fee estimate file", nVersionRequired);
|
|
|
|
|
|
|
|
LOCK(cs);
|
2014-07-03 20:25:32 +02:00
|
|
|
minerPolicyEstimator->Read(filein, minRelayFee);
|
2014-03-17 13:19:54 +01:00
|
|
|
}
|
2014-09-05 17:37:01 +02:00
|
|
|
catch (const std::exception &) {
|
2014-03-17 13:19:54 +01:00
|
|
|
LogPrintf("CTxMemPool::ReadFeeEstimates() : unable to read policy estimator data (non-fatal)");
|
|
|
|
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-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);
|
|
|
|
}
|