Add CConnmanTest to mutate g_connman in tests
[bitcoinplatinum.git] / src / policy / fees.cpp
blobc7e57671c0e88b2e904a55ed446feb1c82b175d7
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include "policy/fees.h"
7 #include "policy/policy.h"
9 #include "amount.h"
10 #include "clientversion.h"
11 #include "primitives/transaction.h"
12 #include "random.h"
13 #include "streams.h"
14 #include "txmempool.h"
15 #include "util.h"
17 static constexpr double INF_FEERATE = 1e99;
19 std::string StringForFeeEstimateHorizon(FeeEstimateHorizon horizon) {
20 static const std::map<FeeEstimateHorizon, std::string> horizon_strings = {
21 {FeeEstimateHorizon::SHORT_HALFLIFE, "short"},
22 {FeeEstimateHorizon::MED_HALFLIFE, "medium"},
23 {FeeEstimateHorizon::LONG_HALFLIFE, "long"},
25 auto horizon_string = horizon_strings.find(horizon);
27 if (horizon_string == horizon_strings.end()) return "unknown";
29 return horizon_string->second;
32 std::string StringForFeeReason(FeeReason reason) {
33 static const std::map<FeeReason, std::string> fee_reason_strings = {
34 {FeeReason::NONE, "None"},
35 {FeeReason::HALF_ESTIMATE, "Half Target 60% Threshold"},
36 {FeeReason::FULL_ESTIMATE, "Target 85% Threshold"},
37 {FeeReason::DOUBLE_ESTIMATE, "Double Target 95% Threshold"},
38 {FeeReason::CONSERVATIVE, "Conservative Double Target longer horizon"},
39 {FeeReason::MEMPOOL_MIN, "Mempool Min Fee"},
40 {FeeReason::PAYTXFEE, "PayTxFee set"},
41 {FeeReason::FALLBACK, "Fallback fee"},
42 {FeeReason::REQUIRED, "Minimum Required Fee"},
43 {FeeReason::MAXTXFEE, "MaxTxFee limit"}
45 auto reason_string = fee_reason_strings.find(reason);
47 if (reason_string == fee_reason_strings.end()) return "Unknown";
49 return reason_string->second;
52 bool FeeModeFromString(const std::string& mode_string, FeeEstimateMode& fee_estimate_mode) {
53 static const std::map<std::string, FeeEstimateMode> fee_modes = {
54 {"UNSET", FeeEstimateMode::UNSET},
55 {"ECONOMICAL", FeeEstimateMode::ECONOMICAL},
56 {"CONSERVATIVE", FeeEstimateMode::CONSERVATIVE},
58 auto mode = fee_modes.find(mode_string);
60 if (mode == fee_modes.end()) return false;
62 fee_estimate_mode = mode->second;
63 return true;
66 /**
67 * We will instantiate an instance of this class to track transactions that were
68 * included in a block. We will lump transactions into a bucket according to their
69 * approximate feerate and then track how long it took for those txs to be included in a block
71 * The tracking of unconfirmed (mempool) transactions is completely independent of the
72 * historical tracking of transactions that have been confirmed in a block.
74 class TxConfirmStats
76 private:
77 //Define the buckets we will group transactions into
78 const std::vector<double>& buckets; // The upper-bound of the range for the bucket (inclusive)
79 const std::map<double, unsigned int>& bucketMap; // Map of bucket upper-bound to index into all vectors by bucket
81 // For each bucket X:
82 // Count the total # of txs in each bucket
83 // Track the historical moving average of this total over blocks
84 std::vector<double> txCtAvg;
86 // Count the total # of txs confirmed within Y blocks in each bucket
87 // Track the historical moving average of theses totals over blocks
88 std::vector<std::vector<double>> confAvg; // confAvg[Y][X]
90 // Track moving avg of txs which have been evicted from the mempool
91 // after failing to be confirmed within Y blocks
92 std::vector<std::vector<double>> failAvg; // failAvg[Y][X]
94 // Sum the total feerate of all tx's in each bucket
95 // Track the historical moving average of this total over blocks
96 std::vector<double> avg;
98 // Combine the conf counts with tx counts to calculate the confirmation % for each Y,X
99 // Combine the total value with the tx counts to calculate the avg feerate per bucket
101 double decay;
103 // Resolution (# of blocks) with which confirmations are tracked
104 unsigned int scale;
106 // Mempool counts of outstanding transactions
107 // For each bucket X, track the number of transactions in the mempool
108 // that are unconfirmed for each possible confirmation value Y
109 std::vector<std::vector<int> > unconfTxs; //unconfTxs[Y][X]
110 // transactions still unconfirmed after GetMaxConfirms for each bucket
111 std::vector<int> oldUnconfTxs;
113 void resizeInMemoryCounters(size_t newbuckets);
115 public:
117 * Create new TxConfirmStats. This is called by BlockPolicyEstimator's
118 * constructor with default values.
119 * @param defaultBuckets contains the upper limits for the bucket boundaries
120 * @param maxPeriods max number of periods to track
121 * @param decay how much to decay the historical moving average per block
123 TxConfirmStats(const std::vector<double>& defaultBuckets, const std::map<double, unsigned int>& defaultBucketMap,
124 unsigned int maxPeriods, double decay, unsigned int scale);
126 /** Roll the circular buffer for unconfirmed txs*/
127 void ClearCurrent(unsigned int nBlockHeight);
130 * Record a new transaction data point in the current block stats
131 * @param blocksToConfirm the number of blocks it took this transaction to confirm
132 * @param val the feerate of the transaction
133 * @warning blocksToConfirm is 1-based and has to be >= 1
135 void Record(int blocksToConfirm, double val);
137 /** Record a new transaction entering the mempool*/
138 unsigned int NewTx(unsigned int nBlockHeight, double val);
140 /** Remove a transaction from mempool tracking stats*/
141 void removeTx(unsigned int entryHeight, unsigned int nBestSeenHeight,
142 unsigned int bucketIndex, bool inBlock);
144 /** Update our estimates by decaying our historical moving average and updating
145 with the data gathered from the current block */
146 void UpdateMovingAverages();
149 * Calculate a feerate estimate. Find the lowest value bucket (or range of buckets
150 * to make sure we have enough data points) whose transactions still have sufficient likelihood
151 * of being confirmed within the target number of confirmations
152 * @param confTarget target number of confirmations
153 * @param sufficientTxVal required average number of transactions per block in a bucket range
154 * @param minSuccess the success probability we require
155 * @param requireGreater return the lowest feerate such that all higher values pass minSuccess OR
156 * return the highest feerate such that all lower values fail minSuccess
157 * @param nBlockHeight the current block height
159 double EstimateMedianVal(int confTarget, double sufficientTxVal,
160 double minSuccess, bool requireGreater, unsigned int nBlockHeight,
161 EstimationResult *result = nullptr) const;
163 /** Return the max number of confirms we're tracking */
164 unsigned int GetMaxConfirms() const { return scale * confAvg.size(); }
166 /** Write state of estimation data to a file*/
167 void Write(CAutoFile& fileout) const;
170 * Read saved state of estimation data from a file and replace all internal data structures and
171 * variables with this state.
173 void Read(CAutoFile& filein, int nFileVersion, size_t numBuckets);
177 TxConfirmStats::TxConfirmStats(const std::vector<double>& defaultBuckets,
178 const std::map<double, unsigned int>& defaultBucketMap,
179 unsigned int maxPeriods, double _decay, unsigned int _scale)
180 : buckets(defaultBuckets), bucketMap(defaultBucketMap)
182 decay = _decay;
183 assert(_scale != 0 && "_scale must be non-zero");
184 scale = _scale;
185 confAvg.resize(maxPeriods);
186 for (unsigned int i = 0; i < maxPeriods; i++) {
187 confAvg[i].resize(buckets.size());
189 failAvg.resize(maxPeriods);
190 for (unsigned int i = 0; i < maxPeriods; i++) {
191 failAvg[i].resize(buckets.size());
194 txCtAvg.resize(buckets.size());
195 avg.resize(buckets.size());
197 resizeInMemoryCounters(buckets.size());
200 void TxConfirmStats::resizeInMemoryCounters(size_t newbuckets) {
201 // newbuckets must be passed in because the buckets referred to during Read have not been updated yet.
202 unconfTxs.resize(GetMaxConfirms());
203 for (unsigned int i = 0; i < unconfTxs.size(); i++) {
204 unconfTxs[i].resize(newbuckets);
206 oldUnconfTxs.resize(newbuckets);
209 // Roll the unconfirmed txs circular buffer
210 void TxConfirmStats::ClearCurrent(unsigned int nBlockHeight)
212 for (unsigned int j = 0; j < buckets.size(); j++) {
213 oldUnconfTxs[j] += unconfTxs[nBlockHeight%unconfTxs.size()][j];
214 unconfTxs[nBlockHeight%unconfTxs.size()][j] = 0;
219 void TxConfirmStats::Record(int blocksToConfirm, double val)
221 // blocksToConfirm is 1-based
222 if (blocksToConfirm < 1)
223 return;
224 int periodsToConfirm = (blocksToConfirm + scale - 1)/scale;
225 unsigned int bucketindex = bucketMap.lower_bound(val)->second;
226 for (size_t i = periodsToConfirm; i <= confAvg.size(); i++) {
227 confAvg[i - 1][bucketindex]++;
229 txCtAvg[bucketindex]++;
230 avg[bucketindex] += val;
233 void TxConfirmStats::UpdateMovingAverages()
235 for (unsigned int j = 0; j < buckets.size(); j++) {
236 for (unsigned int i = 0; i < confAvg.size(); i++)
237 confAvg[i][j] = confAvg[i][j] * decay;
238 for (unsigned int i = 0; i < failAvg.size(); i++)
239 failAvg[i][j] = failAvg[i][j] * decay;
240 avg[j] = avg[j] * decay;
241 txCtAvg[j] = txCtAvg[j] * decay;
245 // returns -1 on error conditions
246 double TxConfirmStats::EstimateMedianVal(int confTarget, double sufficientTxVal,
247 double successBreakPoint, bool requireGreater,
248 unsigned int nBlockHeight, EstimationResult *result) const
250 // Counters for a bucket (or range of buckets)
251 double nConf = 0; // Number of tx's confirmed within the confTarget
252 double totalNum = 0; // Total number of tx's that were ever confirmed
253 int extraNum = 0; // Number of tx's still in mempool for confTarget or longer
254 double failNum = 0; // Number of tx's that were never confirmed but removed from the mempool after confTarget
255 int periodTarget = (confTarget + scale - 1)/scale;
257 int maxbucketindex = buckets.size() - 1;
259 // requireGreater means we are looking for the lowest feerate such that all higher
260 // values pass, so we start at maxbucketindex (highest feerate) and look at successively
261 // smaller buckets until we reach failure. Otherwise, we are looking for the highest
262 // feerate such that all lower values fail, and we go in the opposite direction.
263 unsigned int startbucket = requireGreater ? maxbucketindex : 0;
264 int step = requireGreater ? -1 : 1;
266 // We'll combine buckets until we have enough samples.
267 // The near and far variables will define the range we've combined
268 // The best variables are the last range we saw which still had a high
269 // enough confirmation rate to count as success.
270 // The cur variables are the current range we're counting.
271 unsigned int curNearBucket = startbucket;
272 unsigned int bestNearBucket = startbucket;
273 unsigned int curFarBucket = startbucket;
274 unsigned int bestFarBucket = startbucket;
276 bool foundAnswer = false;
277 unsigned int bins = unconfTxs.size();
278 bool newBucketRange = true;
279 bool passing = true;
280 EstimatorBucket passBucket;
281 EstimatorBucket failBucket;
283 // Start counting from highest(default) or lowest feerate transactions
284 for (int bucket = startbucket; bucket >= 0 && bucket <= maxbucketindex; bucket += step) {
285 if (newBucketRange) {
286 curNearBucket = bucket;
287 newBucketRange = false;
289 curFarBucket = bucket;
290 nConf += confAvg[periodTarget - 1][bucket];
291 totalNum += txCtAvg[bucket];
292 failNum += failAvg[periodTarget - 1][bucket];
293 for (unsigned int confct = confTarget; confct < GetMaxConfirms(); confct++)
294 extraNum += unconfTxs[(nBlockHeight - confct)%bins][bucket];
295 extraNum += oldUnconfTxs[bucket];
296 // If we have enough transaction data points in this range of buckets,
297 // we can test for success
298 // (Only count the confirmed data points, so that each confirmation count
299 // will be looking at the same amount of data and same bucket breaks)
300 if (totalNum >= sufficientTxVal / (1 - decay)) {
301 double curPct = nConf / (totalNum + failNum + extraNum);
303 // Check to see if we are no longer getting confirmed at the success rate
304 if ((requireGreater && curPct < successBreakPoint) || (!requireGreater && curPct > successBreakPoint)) {
305 if (passing == true) {
306 // First time we hit a failure record the failed bucket
307 unsigned int failMinBucket = std::min(curNearBucket, curFarBucket);
308 unsigned int failMaxBucket = std::max(curNearBucket, curFarBucket);
309 failBucket.start = failMinBucket ? buckets[failMinBucket - 1] : 0;
310 failBucket.end = buckets[failMaxBucket];
311 failBucket.withinTarget = nConf;
312 failBucket.totalConfirmed = totalNum;
313 failBucket.inMempool = extraNum;
314 failBucket.leftMempool = failNum;
315 passing = false;
317 continue;
319 // Otherwise update the cumulative stats, and the bucket variables
320 // and reset the counters
321 else {
322 failBucket = EstimatorBucket(); // Reset any failed bucket, currently passing
323 foundAnswer = true;
324 passing = true;
325 passBucket.withinTarget = nConf;
326 nConf = 0;
327 passBucket.totalConfirmed = totalNum;
328 totalNum = 0;
329 passBucket.inMempool = extraNum;
330 passBucket.leftMempool = failNum;
331 failNum = 0;
332 extraNum = 0;
333 bestNearBucket = curNearBucket;
334 bestFarBucket = curFarBucket;
335 newBucketRange = true;
340 double median = -1;
341 double txSum = 0;
343 // Calculate the "average" feerate of the best bucket range that met success conditions
344 // Find the bucket with the median transaction and then report the average feerate from that bucket
345 // This is a compromise between finding the median which we can't since we don't save all tx's
346 // and reporting the average which is less accurate
347 unsigned int minBucket = std::min(bestNearBucket, bestFarBucket);
348 unsigned int maxBucket = std::max(bestNearBucket, bestFarBucket);
349 for (unsigned int j = minBucket; j <= maxBucket; j++) {
350 txSum += txCtAvg[j];
352 if (foundAnswer && txSum != 0) {
353 txSum = txSum / 2;
354 for (unsigned int j = minBucket; j <= maxBucket; j++) {
355 if (txCtAvg[j] < txSum)
356 txSum -= txCtAvg[j];
357 else { // we're in the right bucket
358 median = avg[j] / txCtAvg[j];
359 break;
363 passBucket.start = minBucket ? buckets[minBucket-1] : 0;
364 passBucket.end = buckets[maxBucket];
367 // If we were passing until we reached last few buckets with insufficient data, then report those as failed
368 if (passing && !newBucketRange) {
369 unsigned int failMinBucket = std::min(curNearBucket, curFarBucket);
370 unsigned int failMaxBucket = std::max(curNearBucket, curFarBucket);
371 failBucket.start = failMinBucket ? buckets[failMinBucket - 1] : 0;
372 failBucket.end = buckets[failMaxBucket];
373 failBucket.withinTarget = nConf;
374 failBucket.totalConfirmed = totalNum;
375 failBucket.inMempool = extraNum;
376 failBucket.leftMempool = failNum;
379 LogPrint(BCLog::ESTIMATEFEE, "FeeEst: %d %s%.0f%% decay %.5f: feerate: %g from (%g - %g) %.2f%% %.1f/(%.1f %d mem %.1f out) Fail: (%g - %g) %.2f%% %.1f/(%.1f %d mem %.1f out)\n",
380 confTarget, requireGreater ? ">" : "<", 100.0 * successBreakPoint, decay,
381 median, passBucket.start, passBucket.end,
382 100 * passBucket.withinTarget / (passBucket.totalConfirmed + passBucket.inMempool + passBucket.leftMempool),
383 passBucket.withinTarget, passBucket.totalConfirmed, passBucket.inMempool, passBucket.leftMempool,
384 failBucket.start, failBucket.end,
385 100 * failBucket.withinTarget / (failBucket.totalConfirmed + failBucket.inMempool + failBucket.leftMempool),
386 failBucket.withinTarget, failBucket.totalConfirmed, failBucket.inMempool, failBucket.leftMempool);
389 if (result) {
390 result->pass = passBucket;
391 result->fail = failBucket;
392 result->decay = decay;
393 result->scale = scale;
395 return median;
398 void TxConfirmStats::Write(CAutoFile& fileout) const
400 fileout << decay;
401 fileout << scale;
402 fileout << avg;
403 fileout << txCtAvg;
404 fileout << confAvg;
405 fileout << failAvg;
408 void TxConfirmStats::Read(CAutoFile& filein, int nFileVersion, size_t numBuckets)
410 // Read data file and do some very basic sanity checking
411 // buckets and bucketMap are not updated yet, so don't access them
412 // If there is a read failure, we'll just discard this entire object anyway
413 size_t maxConfirms, maxPeriods;
415 // The current version will store the decay with each individual TxConfirmStats and also keep a scale factor
416 if (nFileVersion >= 149900) {
417 filein >> decay;
418 if (decay <= 0 || decay >= 1) {
419 throw std::runtime_error("Corrupt estimates file. Decay must be between 0 and 1 (non-inclusive)");
421 filein >> scale;
422 if (scale == 0) {
423 throw std::runtime_error("Corrupt estimates file. Scale must be non-zero");
427 filein >> avg;
428 if (avg.size() != numBuckets) {
429 throw std::runtime_error("Corrupt estimates file. Mismatch in feerate average bucket count");
431 filein >> txCtAvg;
432 if (txCtAvg.size() != numBuckets) {
433 throw std::runtime_error("Corrupt estimates file. Mismatch in tx count bucket count");
435 filein >> confAvg;
436 maxPeriods = confAvg.size();
437 maxConfirms = scale * maxPeriods;
439 if (maxConfirms <= 0 || maxConfirms > 6 * 24 * 7) { // one week
440 throw std::runtime_error("Corrupt estimates file. Must maintain estimates for between 1 and 1008 (one week) confirms");
442 for (unsigned int i = 0; i < maxPeriods; i++) {
443 if (confAvg[i].size() != numBuckets) {
444 throw std::runtime_error("Corrupt estimates file. Mismatch in feerate conf average bucket count");
448 if (nFileVersion >= 149900) {
449 filein >> failAvg;
450 if (maxPeriods != failAvg.size()) {
451 throw std::runtime_error("Corrupt estimates file. Mismatch in confirms tracked for failures");
453 for (unsigned int i = 0; i < maxPeriods; i++) {
454 if (failAvg[i].size() != numBuckets) {
455 throw std::runtime_error("Corrupt estimates file. Mismatch in one of failure average bucket counts");
458 } else {
459 failAvg.resize(confAvg.size());
460 for (unsigned int i = 0; i < failAvg.size(); i++) {
461 failAvg[i].resize(numBuckets);
465 // Resize the current block variables which aren't stored in the data file
466 // to match the number of confirms and buckets
467 resizeInMemoryCounters(numBuckets);
469 LogPrint(BCLog::ESTIMATEFEE, "Reading estimates: %u buckets counting confirms up to %u blocks\n",
470 numBuckets, maxConfirms);
473 unsigned int TxConfirmStats::NewTx(unsigned int nBlockHeight, double val)
475 unsigned int bucketindex = bucketMap.lower_bound(val)->second;
476 unsigned int blockIndex = nBlockHeight % unconfTxs.size();
477 unconfTxs[blockIndex][bucketindex]++;
478 return bucketindex;
481 void TxConfirmStats::removeTx(unsigned int entryHeight, unsigned int nBestSeenHeight, unsigned int bucketindex, bool inBlock)
483 //nBestSeenHeight is not updated yet for the new block
484 int blocksAgo = nBestSeenHeight - entryHeight;
485 if (nBestSeenHeight == 0) // the BlockPolicyEstimator hasn't seen any blocks yet
486 blocksAgo = 0;
487 if (blocksAgo < 0) {
488 LogPrint(BCLog::ESTIMATEFEE, "Blockpolicy error, blocks ago is negative for mempool tx\n");
489 return; //This can't happen because we call this with our best seen height, no entries can have higher
492 if (blocksAgo >= (int)unconfTxs.size()) {
493 if (oldUnconfTxs[bucketindex] > 0) {
494 oldUnconfTxs[bucketindex]--;
495 } else {
496 LogPrint(BCLog::ESTIMATEFEE, "Blockpolicy error, mempool tx removed from >25 blocks,bucketIndex=%u already\n",
497 bucketindex);
500 else {
501 unsigned int blockIndex = entryHeight % unconfTxs.size();
502 if (unconfTxs[blockIndex][bucketindex] > 0) {
503 unconfTxs[blockIndex][bucketindex]--;
504 } else {
505 LogPrint(BCLog::ESTIMATEFEE, "Blockpolicy error, mempool tx removed from blockIndex=%u,bucketIndex=%u already\n",
506 blockIndex, bucketindex);
509 if (!inBlock && (unsigned int)blocksAgo >= scale) { // Only counts as a failure if not confirmed for entire period
510 assert(scale != 0);
511 unsigned int periodsAgo = blocksAgo / scale;
512 for (size_t i = 0; i < periodsAgo && i < failAvg.size(); i++) {
513 failAvg[i][bucketindex]++;
518 // This function is called from CTxMemPool::removeUnchecked to ensure
519 // txs removed from the mempool for any reason are no longer
520 // tracked. Txs that were part of a block have already been removed in
521 // processBlockTx to ensure they are never double tracked, but it is
522 // of no harm to try to remove them again.
523 bool CBlockPolicyEstimator::removeTx(uint256 hash, bool inBlock)
525 LOCK(cs_feeEstimator);
526 std::map<uint256, TxStatsInfo>::iterator pos = mapMemPoolTxs.find(hash);
527 if (pos != mapMemPoolTxs.end()) {
528 feeStats->removeTx(pos->second.blockHeight, nBestSeenHeight, pos->second.bucketIndex, inBlock);
529 shortStats->removeTx(pos->second.blockHeight, nBestSeenHeight, pos->second.bucketIndex, inBlock);
530 longStats->removeTx(pos->second.blockHeight, nBestSeenHeight, pos->second.bucketIndex, inBlock);
531 mapMemPoolTxs.erase(hash);
532 return true;
533 } else {
534 return false;
538 CBlockPolicyEstimator::CBlockPolicyEstimator()
539 : nBestSeenHeight(0), firstRecordedHeight(0), historicalFirst(0), historicalBest(0), trackedTxs(0), untrackedTxs(0)
541 static_assert(MIN_BUCKET_FEERATE > 0, "Min feerate must be nonzero");
542 size_t bucketIndex = 0;
543 for (double bucketBoundary = MIN_BUCKET_FEERATE; bucketBoundary <= MAX_BUCKET_FEERATE; bucketBoundary *= FEE_SPACING, bucketIndex++) {
544 buckets.push_back(bucketBoundary);
545 bucketMap[bucketBoundary] = bucketIndex;
547 buckets.push_back(INF_FEERATE);
548 bucketMap[INF_FEERATE] = bucketIndex;
549 assert(bucketMap.size() == buckets.size());
551 feeStats = new TxConfirmStats(buckets, bucketMap, MED_BLOCK_PERIODS, MED_DECAY, MED_SCALE);
552 shortStats = new TxConfirmStats(buckets, bucketMap, SHORT_BLOCK_PERIODS, SHORT_DECAY, SHORT_SCALE);
553 longStats = new TxConfirmStats(buckets, bucketMap, LONG_BLOCK_PERIODS, LONG_DECAY, LONG_SCALE);
556 CBlockPolicyEstimator::~CBlockPolicyEstimator()
558 delete feeStats;
559 delete shortStats;
560 delete longStats;
563 void CBlockPolicyEstimator::processTransaction(const CTxMemPoolEntry& entry, bool validFeeEstimate)
565 LOCK(cs_feeEstimator);
566 unsigned int txHeight = entry.GetHeight();
567 uint256 hash = entry.GetTx().GetHash();
568 if (mapMemPoolTxs.count(hash)) {
569 LogPrint(BCLog::ESTIMATEFEE, "Blockpolicy error mempool tx %s already being tracked\n",
570 hash.ToString().c_str());
571 return;
574 if (txHeight != nBestSeenHeight) {
575 // Ignore side chains and re-orgs; assuming they are random they don't
576 // affect the estimate. We'll potentially double count transactions in 1-block reorgs.
577 // Ignore txs if BlockPolicyEstimator is not in sync with chainActive.Tip().
578 // It will be synced next time a block is processed.
579 return;
582 // Only want to be updating estimates when our blockchain is synced,
583 // otherwise we'll miscalculate how many blocks its taking to get included.
584 if (!validFeeEstimate) {
585 untrackedTxs++;
586 return;
588 trackedTxs++;
590 // Feerates are stored and reported as BTC-per-kb:
591 CFeeRate feeRate(entry.GetFee(), entry.GetTxSize());
593 mapMemPoolTxs[hash].blockHeight = txHeight;
594 unsigned int bucketIndex = feeStats->NewTx(txHeight, (double)feeRate.GetFeePerK());
595 mapMemPoolTxs[hash].bucketIndex = bucketIndex;
596 unsigned int bucketIndex2 = shortStats->NewTx(txHeight, (double)feeRate.GetFeePerK());
597 assert(bucketIndex == bucketIndex2);
598 unsigned int bucketIndex3 = longStats->NewTx(txHeight, (double)feeRate.GetFeePerK());
599 assert(bucketIndex == bucketIndex3);
602 bool CBlockPolicyEstimator::processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry* entry)
604 if (!removeTx(entry->GetTx().GetHash(), true)) {
605 // This transaction wasn't being tracked for fee estimation
606 return false;
609 // How many blocks did it take for miners to include this transaction?
610 // blocksToConfirm is 1-based, so a transaction included in the earliest
611 // possible block has confirmation count of 1
612 int blocksToConfirm = nBlockHeight - entry->GetHeight();
613 if (blocksToConfirm <= 0) {
614 // This can't happen because we don't process transactions from a block with a height
615 // lower than our greatest seen height
616 LogPrint(BCLog::ESTIMATEFEE, "Blockpolicy error Transaction had negative blocksToConfirm\n");
617 return false;
620 // Feerates are stored and reported as BTC-per-kb:
621 CFeeRate feeRate(entry->GetFee(), entry->GetTxSize());
623 feeStats->Record(blocksToConfirm, (double)feeRate.GetFeePerK());
624 shortStats->Record(blocksToConfirm, (double)feeRate.GetFeePerK());
625 longStats->Record(blocksToConfirm, (double)feeRate.GetFeePerK());
626 return true;
629 void CBlockPolicyEstimator::processBlock(unsigned int nBlockHeight,
630 std::vector<const CTxMemPoolEntry*>& entries)
632 LOCK(cs_feeEstimator);
633 if (nBlockHeight <= nBestSeenHeight) {
634 // Ignore side chains and re-orgs; assuming they are random
635 // they don't affect the estimate.
636 // And if an attacker can re-org the chain at will, then
637 // you've got much bigger problems than "attacker can influence
638 // transaction fees."
639 return;
642 // Must update nBestSeenHeight in sync with ClearCurrent so that
643 // calls to removeTx (via processBlockTx) correctly calculate age
644 // of unconfirmed txs to remove from tracking.
645 nBestSeenHeight = nBlockHeight;
647 // Update unconfirmed circular buffer
648 feeStats->ClearCurrent(nBlockHeight);
649 shortStats->ClearCurrent(nBlockHeight);
650 longStats->ClearCurrent(nBlockHeight);
652 // Decay all exponential averages
653 feeStats->UpdateMovingAverages();
654 shortStats->UpdateMovingAverages();
655 longStats->UpdateMovingAverages();
657 unsigned int countedTxs = 0;
658 // Update averages with data points from current block
659 for (const auto& entry : entries) {
660 if (processBlockTx(nBlockHeight, entry))
661 countedTxs++;
664 if (firstRecordedHeight == 0 && countedTxs > 0) {
665 firstRecordedHeight = nBestSeenHeight;
666 LogPrint(BCLog::ESTIMATEFEE, "Blockpolicy first recorded height %u\n", firstRecordedHeight);
670 LogPrint(BCLog::ESTIMATEFEE, "Blockpolicy estimates updated by %u of %u block txs, since last block %u of %u tracked, mempool map size %u, max target %u from %s\n",
671 countedTxs, entries.size(), trackedTxs, trackedTxs + untrackedTxs, mapMemPoolTxs.size(),
672 MaxUsableEstimate(), HistoricalBlockSpan() > BlockSpan() ? "historical" : "current");
674 trackedTxs = 0;
675 untrackedTxs = 0;
678 CFeeRate CBlockPolicyEstimator::estimateFee(int confTarget) const
680 // It's not possible to get reasonable estimates for confTarget of 1
681 if (confTarget <= 1)
682 return CFeeRate(0);
684 return estimateRawFee(confTarget, DOUBLE_SUCCESS_PCT, FeeEstimateHorizon::MED_HALFLIFE);
687 CFeeRate CBlockPolicyEstimator::estimateRawFee(int confTarget, double successThreshold, FeeEstimateHorizon horizon, EstimationResult* result) const
689 TxConfirmStats* stats;
690 double sufficientTxs = SUFFICIENT_FEETXS;
691 switch (horizon) {
692 case FeeEstimateHorizon::SHORT_HALFLIFE: {
693 stats = shortStats;
694 sufficientTxs = SUFFICIENT_TXS_SHORT;
695 break;
697 case FeeEstimateHorizon::MED_HALFLIFE: {
698 stats = feeStats;
699 break;
701 case FeeEstimateHorizon::LONG_HALFLIFE: {
702 stats = longStats;
703 break;
705 default: {
706 throw std::out_of_range("CBlockPolicyEstimator::estimateRawFee unknown FeeEstimateHorizon");
710 LOCK(cs_feeEstimator);
711 // Return failure if trying to analyze a target we're not tracking
712 if (confTarget <= 0 || (unsigned int)confTarget > stats->GetMaxConfirms())
713 return CFeeRate(0);
714 if (successThreshold > 1)
715 return CFeeRate(0);
717 double median = stats->EstimateMedianVal(confTarget, sufficientTxs, successThreshold, true, nBestSeenHeight, result);
719 if (median < 0)
720 return CFeeRate(0);
722 return CFeeRate(llround(median));
725 unsigned int CBlockPolicyEstimator::HighestTargetTracked(FeeEstimateHorizon horizon) const
727 switch (horizon) {
728 case FeeEstimateHorizon::SHORT_HALFLIFE: {
729 return shortStats->GetMaxConfirms();
731 case FeeEstimateHorizon::MED_HALFLIFE: {
732 return feeStats->GetMaxConfirms();
734 case FeeEstimateHorizon::LONG_HALFLIFE: {
735 return longStats->GetMaxConfirms();
737 default: {
738 throw std::out_of_range("CBlockPolicyEstimator::HighestTargetTracked unknown FeeEstimateHorizon");
743 unsigned int CBlockPolicyEstimator::BlockSpan() const
745 if (firstRecordedHeight == 0) return 0;
746 assert(nBestSeenHeight >= firstRecordedHeight);
748 return nBestSeenHeight - firstRecordedHeight;
751 unsigned int CBlockPolicyEstimator::HistoricalBlockSpan() const
753 if (historicalFirst == 0) return 0;
754 assert(historicalBest >= historicalFirst);
756 if (nBestSeenHeight - historicalBest > OLDEST_ESTIMATE_HISTORY) return 0;
758 return historicalBest - historicalFirst;
761 unsigned int CBlockPolicyEstimator::MaxUsableEstimate() const
763 // Block spans are divided by 2 to make sure there are enough potential failing data points for the estimate
764 return std::min(longStats->GetMaxConfirms(), std::max(BlockSpan(), HistoricalBlockSpan()) / 2);
767 /** Return a fee estimate at the required successThreshold from the shortest
768 * time horizon which tracks confirmations up to the desired target. If
769 * checkShorterHorizon is requested, also allow short time horizon estimates
770 * for a lower target to reduce the given answer */
771 double CBlockPolicyEstimator::estimateCombinedFee(unsigned int confTarget, double successThreshold, bool checkShorterHorizon, EstimationResult *result) const
773 double estimate = -1;
774 if (confTarget >= 1 && confTarget <= longStats->GetMaxConfirms()) {
775 // Find estimate from shortest time horizon possible
776 if (confTarget <= shortStats->GetMaxConfirms()) { // short horizon
777 estimate = shortStats->EstimateMedianVal(confTarget, SUFFICIENT_TXS_SHORT, successThreshold, true, nBestSeenHeight, result);
779 else if (confTarget <= feeStats->GetMaxConfirms()) { // medium horizon
780 estimate = feeStats->EstimateMedianVal(confTarget, SUFFICIENT_FEETXS, successThreshold, true, nBestSeenHeight, result);
782 else { // long horizon
783 estimate = longStats->EstimateMedianVal(confTarget, SUFFICIENT_FEETXS, successThreshold, true, nBestSeenHeight, result);
785 if (checkShorterHorizon) {
786 EstimationResult tempResult;
787 // If a lower confTarget from a more recent horizon returns a lower answer use it.
788 if (confTarget > feeStats->GetMaxConfirms()) {
789 double medMax = feeStats->EstimateMedianVal(feeStats->GetMaxConfirms(), SUFFICIENT_FEETXS, successThreshold, true, nBestSeenHeight, &tempResult);
790 if (medMax > 0 && (estimate == -1 || medMax < estimate)) {
791 estimate = medMax;
792 if (result) *result = tempResult;
795 if (confTarget > shortStats->GetMaxConfirms()) {
796 double shortMax = shortStats->EstimateMedianVal(shortStats->GetMaxConfirms(), SUFFICIENT_TXS_SHORT, successThreshold, true, nBestSeenHeight, &tempResult);
797 if (shortMax > 0 && (estimate == -1 || shortMax < estimate)) {
798 estimate = shortMax;
799 if (result) *result = tempResult;
804 return estimate;
807 /** Ensure that for a conservative estimate, the DOUBLE_SUCCESS_PCT is also met
808 * at 2 * target for any longer time horizons.
810 double CBlockPolicyEstimator::estimateConservativeFee(unsigned int doubleTarget, EstimationResult *result) const
812 double estimate = -1;
813 EstimationResult tempResult;
814 if (doubleTarget <= shortStats->GetMaxConfirms()) {
815 estimate = feeStats->EstimateMedianVal(doubleTarget, SUFFICIENT_FEETXS, DOUBLE_SUCCESS_PCT, true, nBestSeenHeight, result);
817 if (doubleTarget <= feeStats->GetMaxConfirms()) {
818 double longEstimate = longStats->EstimateMedianVal(doubleTarget, SUFFICIENT_FEETXS, DOUBLE_SUCCESS_PCT, true, nBestSeenHeight, &tempResult);
819 if (longEstimate > estimate) {
820 estimate = longEstimate;
821 if (result) *result = tempResult;
824 return estimate;
827 /** estimateSmartFee returns the max of the feerates calculated with a 60%
828 * threshold required at target / 2, an 85% threshold required at target and a
829 * 95% threshold required at 2 * target. Each calculation is performed at the
830 * shortest time horizon which tracks the required target. Conservative
831 * estimates, however, required the 95% threshold at 2 * target be met for any
832 * longer time horizons also.
834 CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, FeeCalculation *feeCalc, bool conservative) const
836 LOCK(cs_feeEstimator);
838 if (feeCalc) {
839 feeCalc->desiredTarget = confTarget;
840 feeCalc->returnedTarget = confTarget;
843 double median = -1;
844 EstimationResult tempResult;
846 // Return failure if trying to analyze a target we're not tracking
847 if (confTarget <= 0 || (unsigned int)confTarget > longStats->GetMaxConfirms()) {
848 return CFeeRate(0); // error condition
851 // It's not possible to get reasonable estimates for confTarget of 1
852 if (confTarget == 1) confTarget = 2;
854 unsigned int maxUsableEstimate = MaxUsableEstimate();
855 if ((unsigned int)confTarget > maxUsableEstimate) {
856 confTarget = maxUsableEstimate;
858 if (feeCalc) feeCalc->returnedTarget = confTarget;
860 if (confTarget <= 1) return CFeeRate(0); // error condition
862 assert(confTarget > 0); //estimateCombinedFee and estimateConservativeFee take unsigned ints
863 /** true is passed to estimateCombined fee for target/2 and target so
864 * that we check the max confirms for shorter time horizons as well.
865 * This is necessary to preserve monotonically increasing estimates.
866 * For non-conservative estimates we do the same thing for 2*target, but
867 * for conservative estimates we want to skip these shorter horizons
868 * checks for 2*target because we are taking the max over all time
869 * horizons so we already have monotonically increasing estimates and
870 * the purpose of conservative estimates is not to let short term
871 * fluctuations lower our estimates by too much.
873 double halfEst = estimateCombinedFee(confTarget/2, HALF_SUCCESS_PCT, true, &tempResult);
874 if (feeCalc) {
875 feeCalc->est = tempResult;
876 feeCalc->reason = FeeReason::HALF_ESTIMATE;
878 median = halfEst;
879 double actualEst = estimateCombinedFee(confTarget, SUCCESS_PCT, true, &tempResult);
880 if (actualEst > median) {
881 median = actualEst;
882 if (feeCalc) {
883 feeCalc->est = tempResult;
884 feeCalc->reason = FeeReason::FULL_ESTIMATE;
887 double doubleEst = estimateCombinedFee(2 * confTarget, DOUBLE_SUCCESS_PCT, !conservative, &tempResult);
888 if (doubleEst > median) {
889 median = doubleEst;
890 if (feeCalc) {
891 feeCalc->est = tempResult;
892 feeCalc->reason = FeeReason::DOUBLE_ESTIMATE;
896 if (conservative || median == -1) {
897 double consEst = estimateConservativeFee(2 * confTarget, &tempResult);
898 if (consEst > median) {
899 median = consEst;
900 if (feeCalc) {
901 feeCalc->est = tempResult;
902 feeCalc->reason = FeeReason::CONSERVATIVE;
907 if (median < 0) return CFeeRate(0); // error condition
909 return CFeeRate(llround(median));
913 bool CBlockPolicyEstimator::Write(CAutoFile& fileout) const
915 try {
916 LOCK(cs_feeEstimator);
917 fileout << 149900; // version required to read: 0.14.99 or later
918 fileout << CLIENT_VERSION; // version that wrote the file
919 fileout << nBestSeenHeight;
920 if (BlockSpan() > HistoricalBlockSpan()/2) {
921 fileout << firstRecordedHeight << nBestSeenHeight;
923 else {
924 fileout << historicalFirst << historicalBest;
926 fileout << buckets;
927 feeStats->Write(fileout);
928 shortStats->Write(fileout);
929 longStats->Write(fileout);
931 catch (const std::exception&) {
932 LogPrintf("CBlockPolicyEstimator::Write(): unable to write policy estimator data (non-fatal)\n");
933 return false;
935 return true;
938 bool CBlockPolicyEstimator::Read(CAutoFile& filein)
940 try {
941 LOCK(cs_feeEstimator);
942 int nVersionRequired, nVersionThatWrote;
943 filein >> nVersionRequired >> nVersionThatWrote;
944 if (nVersionRequired > CLIENT_VERSION)
945 return error("CBlockPolicyEstimator::Read(): up-version (%d) fee estimate file", nVersionRequired);
947 // Read fee estimates file into temporary variables so existing data
948 // structures aren't corrupted if there is an exception.
949 unsigned int nFileBestSeenHeight;
950 filein >> nFileBestSeenHeight;
952 if (nVersionThatWrote < 149900) {
953 // Read the old fee estimates file for temporary use, but then discard. Will start collecting data from scratch.
954 // decay is stored before buckets in old versions, so pre-read decay and pass into TxConfirmStats constructor
955 double tempDecay;
956 filein >> tempDecay;
957 if (tempDecay <= 0 || tempDecay >= 1)
958 throw std::runtime_error("Corrupt estimates file. Decay must be between 0 and 1 (non-inclusive)");
960 std::vector<double> tempBuckets;
961 filein >> tempBuckets;
962 size_t tempNum = tempBuckets.size();
963 if (tempNum <= 1 || tempNum > 1000)
964 throw std::runtime_error("Corrupt estimates file. Must have between 2 and 1000 feerate buckets");
966 std::map<double, unsigned int> tempMap;
968 std::unique_ptr<TxConfirmStats> tempFeeStats(new TxConfirmStats(tempBuckets, tempMap, MED_BLOCK_PERIODS, tempDecay, 1));
969 tempFeeStats->Read(filein, nVersionThatWrote, tempNum);
970 // if nVersionThatWrote < 139900 then another TxConfirmStats (for priority) follows but can be ignored.
972 tempMap.clear();
973 for (unsigned int i = 0; i < tempBuckets.size(); i++) {
974 tempMap[tempBuckets[i]] = i;
977 else { // nVersionThatWrote >= 149900
978 unsigned int nFileHistoricalFirst, nFileHistoricalBest;
979 filein >> nFileHistoricalFirst >> nFileHistoricalBest;
980 if (nFileHistoricalFirst > nFileHistoricalBest || nFileHistoricalBest > nFileBestSeenHeight) {
981 throw std::runtime_error("Corrupt estimates file. Historical block range for estimates is invalid");
983 std::vector<double> fileBuckets;
984 filein >> fileBuckets;
985 size_t numBuckets = fileBuckets.size();
986 if (numBuckets <= 1 || numBuckets > 1000)
987 throw std::runtime_error("Corrupt estimates file. Must have between 2 and 1000 feerate buckets");
989 std::unique_ptr<TxConfirmStats> fileFeeStats(new TxConfirmStats(buckets, bucketMap, MED_BLOCK_PERIODS, MED_DECAY, MED_SCALE));
990 std::unique_ptr<TxConfirmStats> fileShortStats(new TxConfirmStats(buckets, bucketMap, SHORT_BLOCK_PERIODS, SHORT_DECAY, SHORT_SCALE));
991 std::unique_ptr<TxConfirmStats> fileLongStats(new TxConfirmStats(buckets, bucketMap, LONG_BLOCK_PERIODS, LONG_DECAY, LONG_SCALE));
992 fileFeeStats->Read(filein, nVersionThatWrote, numBuckets);
993 fileShortStats->Read(filein, nVersionThatWrote, numBuckets);
994 fileLongStats->Read(filein, nVersionThatWrote, numBuckets);
996 // Fee estimates file parsed correctly
997 // Copy buckets from file and refresh our bucketmap
998 buckets = fileBuckets;
999 bucketMap.clear();
1000 for (unsigned int i = 0; i < buckets.size(); i++) {
1001 bucketMap[buckets[i]] = i;
1004 // Destroy old TxConfirmStats and point to new ones that already reference buckets and bucketMap
1005 delete feeStats;
1006 delete shortStats;
1007 delete longStats;
1008 feeStats = fileFeeStats.release();
1009 shortStats = fileShortStats.release();
1010 longStats = fileLongStats.release();
1012 nBestSeenHeight = nFileBestSeenHeight;
1013 historicalFirst = nFileHistoricalFirst;
1014 historicalBest = nFileHistoricalBest;
1017 catch (const std::exception& e) {
1018 LogPrintf("CBlockPolicyEstimator::Read(): unable to read policy estimator data (non-fatal): %s\n",e.what());
1019 return false;
1021 return true;
1024 void CBlockPolicyEstimator::FlushUnconfirmed(CTxMemPool& pool) {
1025 int64_t startclear = GetTimeMicros();
1026 std::vector<uint256> txids;
1027 pool.queryHashes(txids);
1028 LOCK(cs_feeEstimator);
1029 for (auto& txid : txids) {
1030 removeTx(txid, false);
1032 int64_t endclear = GetTimeMicros();
1033 LogPrint(BCLog::ESTIMATEFEE, "Recorded %u unconfirmed txs from mempool in %gs\n",txids.size(), (endclear - startclear)*0.000001);
1036 FeeFilterRounder::FeeFilterRounder(const CFeeRate& minIncrementalFee)
1038 CAmount minFeeLimit = std::max(CAmount(1), minIncrementalFee.GetFeePerK() / 2);
1039 feeset.insert(0);
1040 for (double bucketBoundary = minFeeLimit; bucketBoundary <= MAX_FILTER_FEERATE; bucketBoundary *= FEE_FILTER_SPACING) {
1041 feeset.insert(bucketBoundary);
1045 CAmount FeeFilterRounder::round(CAmount currentMinFee)
1047 std::set<double>::iterator it = feeset.lower_bound(currentMinFee);
1048 if ((it != feeset.begin() && insecure_rand.rand32() % 3 != 0) || it == feeset.end()) {
1049 it--;
1051 return static_cast<CAmount>(*it);