aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGavin Andresen <gavinandresen@gmail.com>2014-03-17 08:19:54 -0400
committerGavin Andresen <gavinandresen@gmail.com>2014-06-06 10:44:57 -0400
commit171ca7745e77c9f78f26556457fe64e5b2004a75 (patch)
tree0a25f1f328ca66177c79afcf4422ea99d221f981 /src
parent0193fb82a6fe443e5434f2c2af3b252409c19b25 (diff)
estimatefee / estimatepriority RPC methods
New RPC methods: return an estimate of the fee (or priority) a transaction needs to be likely to confirm in a given number of blocks. Mike Hearn created the first version of this method for estimating fees. It works as follows: For transactions that took 1 to N (I picked N=25) blocks to confirm, keep N buckets with at most 100 entries in each recording the fees-per-kilobyte paid by those transactions. (separate buckets are kept for transactions that confirmed because they are high-priority) The buckets are filled as blocks are found, and are saved/restored in a new fee_estiamtes.dat file in the data directory. A few variations on Mike's initial scheme: To estimate the fee needed for a transaction to confirm in X buckets, all of the samples in all of the buckets are used and a median of all of the data is used to make the estimate. For example, imagine 25 buckets each containing the full 100 entries. Those 2,500 samples are sorted, and the estimate of the fee needed to confirm in the very next block is the 50'th-highest-fee-entry in that sorted list; the estimate of the fee needed to confirm in the next two blocks is the 150'th-highest-fee-entry, etc. That algorithm has the nice property that estimates of how much fee you need to pay to get confirmed in block N will always be greater than or equal to the estimate for block N+1. It would clearly be wrong to say "pay 11 uBTC and you'll get confirmed in 3 blocks, but pay 12 uBTC and it will take LONGER". A single block will not contribute more than 10 entries to any one bucket, so a single miner and a large block cannot overwhelm the estimates.
Diffstat (limited to 'src')
-rw-r--r--src/core.h3
-rw-r--r--src/init.cpp14
-rw-r--r--src/main.cpp10
-rw-r--r--src/main.h7
-rw-r--r--src/rpcclient.cpp2
-rw-r--r--src/rpcmining.cpp61
-rw-r--r--src/rpcserver.cpp2
-rw-r--r--src/rpcserver.h2
-rw-r--r--src/txmempool.cpp370
-rw-r--r--src/txmempool.h23
10 files changed, 481 insertions, 13 deletions
diff --git a/src/core.h b/src/core.h
index 9fccffc4b2..0e59129349 100644
--- a/src/core.h
+++ b/src/core.h
@@ -120,6 +120,7 @@ class CFeeRate
private:
int64_t nSatoshisPerK; // unit is satoshis-per-1,000-bytes
public:
+ CFeeRate() : nSatoshisPerK(0) { }
explicit CFeeRate(int64_t _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) { }
CFeeRate(int64_t nFeePaid, size_t nSize);
CFeeRate(const CFeeRate& other) { nSatoshisPerK = other.nSatoshisPerK; }
@@ -132,6 +133,8 @@ public:
friend bool operator==(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK == b.nSatoshisPerK; }
std::string ToString() const;
+
+ IMPLEMENT_SERIALIZE( READWRITE(nSatoshisPerK); )
};
diff --git a/src/init.cpp b/src/init.cpp
index 1aad679b3b..aca5e0c9aa 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -59,6 +59,7 @@ enum BindFlags {
BF_REPORT_ERROR = (1U << 1)
};
+static const char* FEE_ESTIMATES_FILENAME="fee_estimates.dat";
//////////////////////////////////////////////////////////////////////////////
//
@@ -121,6 +122,14 @@ void Shutdown()
#endif
StopNode();
UnregisterNodeSignals(GetNodeSignals());
+
+ boost::filesystem::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
+ CAutoFile est_fileout = CAutoFile(fopen(est_path.string().c_str(), "wb"), SER_DISK, CLIENT_VERSION);
+ if (est_fileout)
+ mempool.WriteFeeEstimates(est_fileout);
+ else
+ LogPrintf("failed to write fee estimates");
+
{
LOCK(cs_main);
#ifdef ENABLE_WALLET
@@ -933,6 +942,11 @@ bool AppInit2(boost::thread_group& threadGroup)
return false;
}
+ boost::filesystem::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
+ CAutoFile est_filein = CAutoFile(fopen(est_path.string().c_str(), "rb"), SER_DISK, CLIENT_VERSION);
+ if (est_filein)
+ mempool.ReadFeeEstimates(est_filein);
+
// ********************************************************* Step 8: load wallet
#ifdef ENABLE_WALLET
if (fDisableWallet) {
diff --git a/src/main.cpp b/src/main.cpp
index 9d6c01d64e..429473d8f8 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -851,6 +851,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
CCoinsView dummy;
CCoinsViewCache view(dummy);
+ int64_t nValueIn = 0;
{
LOCK(pool.cs);
CCoinsViewMemPool viewMemPool(*pcoinsTip, pool);
@@ -879,6 +880,8 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
// Bring the best block into scope
view.GetBestBlock();
+ nValueIn = view.GetValueIn(tx);
+
// we have all inputs cached now, so switch back to dummy, so we don't need to keep lock on mempool
view.SetBackend(dummy);
}
@@ -891,7 +894,6 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
// you should add code here to check that the transaction does a
// reasonable number of ECDSA signature verifications.
- int64_t nValueIn = view.GetValueIn(tx);
int64_t nValueOut = tx.GetValueOut();
int64_t nFees = nValueIn-nValueOut;
double dPriority = view.GetPriority(tx, chainActive.Height());
@@ -2017,11 +2019,7 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew) {
return false;
// Remove conflicting transactions from the mempool.
list<CTransaction> txConflicted;
- BOOST_FOREACH(const CTransaction &tx, block.vtx) {
- list<CTransaction> unused;
- mempool.remove(tx, unused);
- mempool.removeConflicts(tx, txConflicted);
- }
+ mempool.removeForBlock(block.vtx, pindexNew->nHeight, txConflicted);
mempool.check(pcoinsTip);
// Update chainActive & related variables.
UpdateTip(pindexNew);
diff --git a/src/main.h b/src/main.h
index 3fccd32a29..23c8660376 100644
--- a/src/main.h
+++ b/src/main.h
@@ -292,13 +292,6 @@ unsigned int GetLegacySigOpCount(const CTransaction& tx);
unsigned int GetP2SHSigOpCount(const CTransaction& tx, CCoinsViewCache& mapInputs);
-inline bool AllowFree(double dPriority)
-{
- // Large (in bytes) low-priority (new, small-coin) transactions
- // need a fee.
- return dPriority > COIN * 144 / 250;
-}
-
// Check whether all inputs of this transaction are valid (no double spends, scripts & sigs, amounts)
// This does not modify the UTXO set. If pvChecks is not NULL, script checks are pushed onto it
// instead of being performed inline.
diff --git a/src/rpcclient.cpp b/src/rpcclient.cpp
index 4f3c39ce9b..b89a95ad11 100644
--- a/src/rpcclient.cpp
+++ b/src/rpcclient.cpp
@@ -176,6 +176,8 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
if (strMethod == "verifychain" && n > 1) ConvertTo<int64_t>(params[1]);
if (strMethod == "keypoolrefill" && n > 0) ConvertTo<int64_t>(params[0]);
if (strMethod == "getrawmempool" && n > 0) ConvertTo<bool>(params[0]);
+ if (strMethod == "estimatefee" && n > 0) ConvertTo<boost::int64_t>(params[0]);
+ if (strMethod == "estimatepriority" && n > 0) ConvertTo<boost::int64_t>(params[0]);
return params;
}
diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp
index 23876c603d..dd148c6af1 100644
--- a/src/rpcmining.cpp
+++ b/src/rpcmining.cpp
@@ -15,6 +15,7 @@
#endif
#include <stdint.h>
+#include <boost/assign/list_of.hpp>
#include "json/json_spirit_utils.h"
#include "json/json_spirit_value.h"
@@ -626,3 +627,63 @@ Value submitblock(const Array& params, bool fHelp)
return Value::null;
}
+
+Value estimatefee(const Array& params, bool fHelp)
+{
+ if (fHelp || params.size() != 1)
+ throw runtime_error(
+ "estimatefee nblocks\n"
+ "\nEstimates the approximate fee per kilobyte\n"
+ "needed for a transaction to get confirmed\n"
+ "within nblocks blocks.\n"
+ "\nArguments:\n"
+ "1. nblocks (numeric)\n"
+ "\nResult:\n"
+ "n : (numeric) estimated fee-per-kilobyte\n"
+ "\n"
+ "-1.0 is returned if not enough transactions and\n"
+ "blocks have been observed to make an estimate.\n"
+ "\nExample:\n"
+ + HelpExampleCli("estimatefee", "6")
+ );
+
+ RPCTypeCheck(params, boost::assign::list_of(int_type));
+
+ int nBlocks = params[0].get_int();
+ if (nBlocks < 1)
+ nBlocks = 1;
+
+ CFeeRate feeRate = mempool.estimateFee(nBlocks);
+ if (feeRate == CFeeRate(0))
+ return -1.0;
+
+ return ValueFromAmount(feeRate.GetFeePerK());
+}
+
+Value estimatepriority(const Array& params, bool fHelp)
+{
+ if (fHelp || params.size() != 1)
+ throw runtime_error(
+ "estimatepriority nblocks\n"
+ "\nEstimates the approximate priority\n"
+ "a zero-fee transaction needs to get confirmed\n"
+ "within nblocks blocks.\n"
+ "\nArguments:\n"
+ "1. nblocks (numeric)\n"
+ "\nResult:\n"
+ "n : (numeric) estimated priority\n"
+ "\n"
+ "-1.0 is returned if not enough transactions and\n"
+ "blocks have been observed to make an estimate.\n"
+ "\nExample:\n"
+ + HelpExampleCli("estimatepriority", "6")
+ );
+
+ RPCTypeCheck(params, boost::assign::list_of(int_type));
+
+ int nBlocks = params[0].get_int();
+ if (nBlocks < 1)
+ nBlocks = 1;
+
+ return mempool.estimatePriority(nBlocks);
+}
diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp
index 2534a9dcf4..72a7fe83ef 100644
--- a/src/rpcserver.cpp
+++ b/src/rpcserver.cpp
@@ -268,6 +268,8 @@ static const CRPCCommand vRPCCommands[] =
{ "createmultisig", &createmultisig, true, true , false },
{ "validateaddress", &validateaddress, true, false, false }, /* uses wallet if enabled */
{ "verifymessage", &verifymessage, false, false, false },
+ { "estimatefee", &estimatefee, true, true, false },
+ { "estimatepriority", &estimatepriority, true, true, false },
#ifdef ENABLE_WALLET
/* Wallet */
diff --git a/src/rpcserver.h b/src/rpcserver.h
index e8cd2cd0fc..73e8b9426c 100644
--- a/src/rpcserver.h
+++ b/src/rpcserver.h
@@ -133,6 +133,8 @@ extern json_spirit::Value getmininginfo(const json_spirit::Array& params, bool f
extern json_spirit::Value getwork(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getblocktemplate(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value submitblock(const json_spirit::Array& params, bool fHelp);
+extern json_spirit::Value estimatefee(const json_spirit::Array& params, bool fHelp);
+extern json_spirit::Value estimatepriority(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getnewaddress(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp
extern json_spirit::Value getaccountaddress(const json_spirit::Array& params, bool fHelp);
diff --git a/src/txmempool.cpp b/src/txmempool.cpp
index 64c9eac73d..4bf01d4848 100644
--- a/src/txmempool.cpp
+++ b/src/txmempool.cpp
@@ -6,6 +6,8 @@
#include "core.h"
#include "txmempool.h"
+#include <boost/circular_buffer.hpp>
+
using namespace std;
CTxMemPoolEntry::CTxMemPoolEntry()
@@ -35,12 +37,311 @@ CTxMemPoolEntry::GetPriority(unsigned int currentHeight) const
return dResult;
}
+//
+// Keep track of fee/priority for transactions confirmed within N blocks
+//
+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();
+ }
+
+ // Used as belt-and-suspenders check when reading to detect
+ // file corruption
+ bool AreSane(const std::vector<CFeeRate>& vecFee)
+ {
+ BOOST_FOREACH(CFeeRate fee, vecFee)
+ {
+ if (fee < CFeeRate(0))
+ return false;
+ if (fee.GetFee(1000) > CTransaction::minRelayTxFee.GetFee(1000) * 10000)
+ 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;
+ }
+
+ void Read(CAutoFile& filein) {
+ std::vector<CFeeRate> vecFee;
+ filein >> vecFee;
+ if (AreSane(vecFee))
+ 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:
+ // Records observed averages transactions that confirmed within one block, two blocks,
+ // three blocks etc.
+ std::vector<CBlockAverage> history;
+ std::vector<CFeeRate> sortedFeeSamples;
+ std::vector<double> sortedPrioritySamples;
+
+ int nBestSeenHeight;
+
+ // 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.
+ void seenTxConfirm(CFeeRate feeRate, double dPriority, int nBlocksAgo)
+ {
+ // 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.
+ bool sufficientFee = (feeRate > CTransaction::minRelayTxFee);
+ 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);
+ }
+
+ void seenBlock(const std::vector<CTxMemPoolEntry>& entries, int nBlockHeight)
+ {
+ 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
+ entriesByConfirmations[delta-1].push_back(&entry);
+ }
+ 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
+ seenTxConfirm(feeRate, dPriority, i);
+ }
+ }
+ 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));
+ }
+ sortedFeeSamples.clear();
+ sortedPrioritySamples.clear();
+ }
+
+ // Can return CFeeRate(0) if we don't have any data for that many blocks back. nBlocksToConfirm is 1 based.
+ 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>());
+ }
+ if (sortedFeeSamples.size() == 0)
+ return CFeeRate(0);
+
+ 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>());
+ }
+ if (sortedPrioritySamples.size() == 0)
+ 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();
+ size_t index = min(nPrevSize + nBucketSize/2, sortedFeeSamples.size()-1);
+ return sortedPrioritySamples[index];
+ }
+
+ void Write(CAutoFile& fileout) const
+ {
+ fileout << nBestSeenHeight;
+ fileout << history.size();
+ BOOST_FOREACH(const CBlockAverage& entry, history)
+ {
+ entry.Write(fileout);
+ }
+ }
+
+ void Read(CAutoFile& filein)
+ {
+ filein >> nBestSeenHeight;
+ size_t numEntries;
+ filein >> numEntries;
+ history.clear();
+ for (size_t i = 0; i < numEntries; i++)
+ {
+ CBlockAverage entry;
+ entry.Read(filein);
+ history.push_back(entry);
+ }
+ }
+};
+
+
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;
+
+ // 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;
}
void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins)
@@ -128,6 +429,28 @@ void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>
}
}
+// Called when a block is connected. Removes from mempool and updates the miner fee estimator.
+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]);
+ }
+ minerPolicyEstimator->seenBlock(entries, nBlockHeight);
+ BOOST_FOREACH(const CTransaction& tx, vtx)
+ {
+ std::list<CTransaction> dummy;
+ remove(tx, dummy, false);
+ removeConflicts(tx, conflicts);
+ }
+}
+
+
void CTxMemPool::clear()
{
LOCK(cs);
@@ -195,6 +518,53 @@ bool CTxMemPool::lookup(uint256 hash, CTransaction& result) const
return true;
}
+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);
+ }
+ catch (std::exception &e) {
+ 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);
+ minerPolicyEstimator->Read(filein);
+ }
+ catch (std::exception &e) {
+ LogPrintf("CTxMemPool::ReadFeeEstimates() : unable to read policy estimator data (non-fatal)");
+ return false;
+ }
+ return true;
+}
+
+
CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView &baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
bool CCoinsViewMemPool::GetCoins(const uint256 &txid, CCoins &coins) {
diff --git a/src/txmempool.h b/src/txmempool.h
index 4509e95778..b2915aa842 100644
--- a/src/txmempool.h
+++ b/src/txmempool.h
@@ -11,6 +11,13 @@
#include "core.h"
#include "sync.h"
+inline bool AllowFree(double dPriority)
+{
+ // Large (in bytes) low-priority (new, small-coin) transactions
+ // need a fee.
+ return dPriority > COIN * 144 / 250;
+}
+
/** Fake height value used in CCoins to signify they are only in the memory pool (since 0.8) */
static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
@@ -41,6 +48,8 @@ public:
unsigned int GetHeight() const { return nHeight; }
};
+class CMinerPolicyEstimator;
+
/*
* CTxMemPool stores valid-according-to-the-current-best-chain
* transactions that may be included in the next block.
@@ -56,6 +65,7 @@ class CTxMemPool
private:
bool fSanityCheck; // Normally false, true if -checkmempool or -regtest
unsigned int nTransactionsUpdated;
+ CMinerPolicyEstimator* minerPolicyEstimator;
public:
mutable CCriticalSection cs;
@@ -63,6 +73,7 @@ public:
std::map<COutPoint, CInPoint> mapNextTx;
CTxMemPool();
+ ~CTxMemPool();
/*
* If sanity-checking is turned on, check makes sure the pool is
@@ -76,6 +87,8 @@ public:
bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry);
void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
+ void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
+ std::list<CTransaction>& conflicts);
void clear();
void queryHashes(std::vector<uint256>& vtxid);
void pruneSpent(const uint256& hash, CCoins &coins);
@@ -95,6 +108,16 @@ public:
}
bool lookup(uint256 hash, CTransaction& result) const;
+
+ // Estimate fee rate needed to get into the next
+ // nBlocks
+ CFeeRate estimateFee(int nBlocks) const;
+ // Estimate priority needed to get into the next
+ // nBlocks
+ double estimatePriority(int nBlocks) const;
+ // Write/Read estimates to disk
+ bool WriteFeeEstimates(CAutoFile& fileout) const;
+ bool ReadFeeEstimates(CAutoFile& filein);
};
/** CCoinsView that brings transactions from a memorypool into view.