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>
10 #include <clientversion.h>
11 #include <primitives/transaction.h>
14 #include <txmempool.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
;
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.
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
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
103 // Resolution (# of blocks) with which confirmations are tracked
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
);
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
)
183 assert(_scale
!= 0 && "_scale must be non-zero");
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)
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;
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
;
319 // Otherwise update the cumulative stats, and the bucket variables
320 // and reset the counters
322 failBucket
= EstimatorBucket(); // Reset any failed bucket, currently passing
325 passBucket
.withinTarget
= nConf
;
327 passBucket
.totalConfirmed
= totalNum
;
329 passBucket
.inMempool
= extraNum
;
330 passBucket
.leftMempool
= failNum
;
333 bestNearBucket
= curNearBucket
;
334 bestFarBucket
= curFarBucket
;
335 newBucketRange
= true;
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
++) {
352 if (foundAnswer
&& txSum
!= 0) {
354 for (unsigned int j
= minBucket
; j
<= maxBucket
; j
++) {
355 if (txCtAvg
[j
] < txSum
)
357 else { // we're in the right bucket
358 median
= avg
[j
] / txCtAvg
[j
];
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
);
390 result
->pass
= passBucket
;
391 result
->fail
= failBucket
;
392 result
->decay
= decay
;
393 result
->scale
= scale
;
398 void TxConfirmStats::Write(CAutoFile
& fileout
) const
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) {
418 if (decay
<= 0 || decay
>= 1) {
419 throw std::runtime_error("Corrupt estimates file. Decay must be between 0 and 1 (non-inclusive)");
423 throw std::runtime_error("Corrupt estimates file. Scale must be non-zero");
428 if (avg
.size() != numBuckets
) {
429 throw std::runtime_error("Corrupt estimates file. Mismatch in feerate average bucket count");
432 if (txCtAvg
.size() != numBuckets
) {
433 throw std::runtime_error("Corrupt estimates file. Mismatch in tx count bucket count");
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) {
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");
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
]++;
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
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
]--;
496 LogPrint(BCLog::ESTIMATEFEE
, "Blockpolicy error, mempool tx removed from >25 blocks,bucketIndex=%u already\n",
501 unsigned int blockIndex
= entryHeight
% unconfTxs
.size();
502 if (unconfTxs
[blockIndex
][bucketindex
] > 0) {
503 unconfTxs
[blockIndex
][bucketindex
]--;
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
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
);
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
= std::unique_ptr
<TxConfirmStats
>(new TxConfirmStats(buckets
, bucketMap
, MED_BLOCK_PERIODS
, MED_DECAY
, MED_SCALE
));
552 shortStats
= std::unique_ptr
<TxConfirmStats
>(new TxConfirmStats(buckets
, bucketMap
, SHORT_BLOCK_PERIODS
, SHORT_DECAY
, SHORT_SCALE
));
553 longStats
= std::unique_ptr
<TxConfirmStats
>(new TxConfirmStats(buckets
, bucketMap
, LONG_BLOCK_PERIODS
, LONG_DECAY
, LONG_SCALE
));
556 CBlockPolicyEstimator::~CBlockPolicyEstimator()
560 void CBlockPolicyEstimator::processTransaction(const CTxMemPoolEntry
& entry
, bool validFeeEstimate
)
562 LOCK(cs_feeEstimator
);
563 unsigned int txHeight
= entry
.GetHeight();
564 uint256 hash
= entry
.GetTx().GetHash();
565 if (mapMemPoolTxs
.count(hash
)) {
566 LogPrint(BCLog::ESTIMATEFEE
, "Blockpolicy error mempool tx %s already being tracked\n",
567 hash
.ToString().c_str());
571 if (txHeight
!= nBestSeenHeight
) {
572 // Ignore side chains and re-orgs; assuming they are random they don't
573 // affect the estimate. We'll potentially double count transactions in 1-block reorgs.
574 // Ignore txs if BlockPolicyEstimator is not in sync with chainActive.Tip().
575 // It will be synced next time a block is processed.
579 // Only want to be updating estimates when our blockchain is synced,
580 // otherwise we'll miscalculate how many blocks its taking to get included.
581 if (!validFeeEstimate
) {
587 // Feerates are stored and reported as BTC-per-kb:
588 CFeeRate
feeRate(entry
.GetFee(), entry
.GetTxSize());
590 mapMemPoolTxs
[hash
].blockHeight
= txHeight
;
591 unsigned int bucketIndex
= feeStats
->NewTx(txHeight
, (double)feeRate
.GetFeePerK());
592 mapMemPoolTxs
[hash
].bucketIndex
= bucketIndex
;
593 unsigned int bucketIndex2
= shortStats
->NewTx(txHeight
, (double)feeRate
.GetFeePerK());
594 assert(bucketIndex
== bucketIndex2
);
595 unsigned int bucketIndex3
= longStats
->NewTx(txHeight
, (double)feeRate
.GetFeePerK());
596 assert(bucketIndex
== bucketIndex3
);
599 bool CBlockPolicyEstimator::processBlockTx(unsigned int nBlockHeight
, const CTxMemPoolEntry
* entry
)
601 if (!removeTx(entry
->GetTx().GetHash(), true)) {
602 // This transaction wasn't being tracked for fee estimation
606 // How many blocks did it take for miners to include this transaction?
607 // blocksToConfirm is 1-based, so a transaction included in the earliest
608 // possible block has confirmation count of 1
609 int blocksToConfirm
= nBlockHeight
- entry
->GetHeight();
610 if (blocksToConfirm
<= 0) {
611 // This can't happen because we don't process transactions from a block with a height
612 // lower than our greatest seen height
613 LogPrint(BCLog::ESTIMATEFEE
, "Blockpolicy error Transaction had negative blocksToConfirm\n");
617 // Feerates are stored and reported as BTC-per-kb:
618 CFeeRate
feeRate(entry
->GetFee(), entry
->GetTxSize());
620 feeStats
->Record(blocksToConfirm
, (double)feeRate
.GetFeePerK());
621 shortStats
->Record(blocksToConfirm
, (double)feeRate
.GetFeePerK());
622 longStats
->Record(blocksToConfirm
, (double)feeRate
.GetFeePerK());
626 void CBlockPolicyEstimator::processBlock(unsigned int nBlockHeight
,
627 std::vector
<const CTxMemPoolEntry
*>& entries
)
629 LOCK(cs_feeEstimator
);
630 if (nBlockHeight
<= nBestSeenHeight
) {
631 // Ignore side chains and re-orgs; assuming they are random
632 // they don't affect the estimate.
633 // And if an attacker can re-org the chain at will, then
634 // you've got much bigger problems than "attacker can influence
635 // transaction fees."
639 // Must update nBestSeenHeight in sync with ClearCurrent so that
640 // calls to removeTx (via processBlockTx) correctly calculate age
641 // of unconfirmed txs to remove from tracking.
642 nBestSeenHeight
= nBlockHeight
;
644 // Update unconfirmed circular buffer
645 feeStats
->ClearCurrent(nBlockHeight
);
646 shortStats
->ClearCurrent(nBlockHeight
);
647 longStats
->ClearCurrent(nBlockHeight
);
649 // Decay all exponential averages
650 feeStats
->UpdateMovingAverages();
651 shortStats
->UpdateMovingAverages();
652 longStats
->UpdateMovingAverages();
654 unsigned int countedTxs
= 0;
655 // Update averages with data points from current block
656 for (const auto& entry
: entries
) {
657 if (processBlockTx(nBlockHeight
, entry
))
661 if (firstRecordedHeight
== 0 && countedTxs
> 0) {
662 firstRecordedHeight
= nBestSeenHeight
;
663 LogPrint(BCLog::ESTIMATEFEE
, "Blockpolicy first recorded height %u\n", firstRecordedHeight
);
667 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",
668 countedTxs
, entries
.size(), trackedTxs
, trackedTxs
+ untrackedTxs
, mapMemPoolTxs
.size(),
669 MaxUsableEstimate(), HistoricalBlockSpan() > BlockSpan() ? "historical" : "current");
675 CFeeRate
CBlockPolicyEstimator::estimateFee(int confTarget
) const
677 // It's not possible to get reasonable estimates for confTarget of 1
681 return estimateRawFee(confTarget
, DOUBLE_SUCCESS_PCT
, FeeEstimateHorizon::MED_HALFLIFE
);
684 CFeeRate
CBlockPolicyEstimator::estimateRawFee(int confTarget
, double successThreshold
, FeeEstimateHorizon horizon
, EstimationResult
* result
) const
686 TxConfirmStats
* stats
;
687 double sufficientTxs
= SUFFICIENT_FEETXS
;
689 case FeeEstimateHorizon::SHORT_HALFLIFE
: {
690 stats
= shortStats
.get();
691 sufficientTxs
= SUFFICIENT_TXS_SHORT
;
694 case FeeEstimateHorizon::MED_HALFLIFE
: {
695 stats
= feeStats
.get();
698 case FeeEstimateHorizon::LONG_HALFLIFE
: {
699 stats
= longStats
.get();
703 throw std::out_of_range("CBlockPolicyEstimator::estimateRawFee unknown FeeEstimateHorizon");
707 LOCK(cs_feeEstimator
);
708 // Return failure if trying to analyze a target we're not tracking
709 if (confTarget
<= 0 || (unsigned int)confTarget
> stats
->GetMaxConfirms())
711 if (successThreshold
> 1)
714 double median
= stats
->EstimateMedianVal(confTarget
, sufficientTxs
, successThreshold
, true, nBestSeenHeight
, result
);
719 return CFeeRate(llround(median
));
722 unsigned int CBlockPolicyEstimator::HighestTargetTracked(FeeEstimateHorizon horizon
) const
725 case FeeEstimateHorizon::SHORT_HALFLIFE
: {
726 return shortStats
->GetMaxConfirms();
728 case FeeEstimateHorizon::MED_HALFLIFE
: {
729 return feeStats
->GetMaxConfirms();
731 case FeeEstimateHorizon::LONG_HALFLIFE
: {
732 return longStats
->GetMaxConfirms();
735 throw std::out_of_range("CBlockPolicyEstimator::HighestTargetTracked unknown FeeEstimateHorizon");
740 unsigned int CBlockPolicyEstimator::BlockSpan() const
742 if (firstRecordedHeight
== 0) return 0;
743 assert(nBestSeenHeight
>= firstRecordedHeight
);
745 return nBestSeenHeight
- firstRecordedHeight
;
748 unsigned int CBlockPolicyEstimator::HistoricalBlockSpan() const
750 if (historicalFirst
== 0) return 0;
751 assert(historicalBest
>= historicalFirst
);
753 if (nBestSeenHeight
- historicalBest
> OLDEST_ESTIMATE_HISTORY
) return 0;
755 return historicalBest
- historicalFirst
;
758 unsigned int CBlockPolicyEstimator::MaxUsableEstimate() const
760 // Block spans are divided by 2 to make sure there are enough potential failing data points for the estimate
761 return std::min(longStats
->GetMaxConfirms(), std::max(BlockSpan(), HistoricalBlockSpan()) / 2);
764 /** Return a fee estimate at the required successThreshold from the shortest
765 * time horizon which tracks confirmations up to the desired target. If
766 * checkShorterHorizon is requested, also allow short time horizon estimates
767 * for a lower target to reduce the given answer */
768 double CBlockPolicyEstimator::estimateCombinedFee(unsigned int confTarget
, double successThreshold
, bool checkShorterHorizon
, EstimationResult
*result
) const
770 double estimate
= -1;
771 if (confTarget
>= 1 && confTarget
<= longStats
->GetMaxConfirms()) {
772 // Find estimate from shortest time horizon possible
773 if (confTarget
<= shortStats
->GetMaxConfirms()) { // short horizon
774 estimate
= shortStats
->EstimateMedianVal(confTarget
, SUFFICIENT_TXS_SHORT
, successThreshold
, true, nBestSeenHeight
, result
);
776 else if (confTarget
<= feeStats
->GetMaxConfirms()) { // medium horizon
777 estimate
= feeStats
->EstimateMedianVal(confTarget
, SUFFICIENT_FEETXS
, successThreshold
, true, nBestSeenHeight
, result
);
779 else { // long horizon
780 estimate
= longStats
->EstimateMedianVal(confTarget
, SUFFICIENT_FEETXS
, successThreshold
, true, nBestSeenHeight
, result
);
782 if (checkShorterHorizon
) {
783 EstimationResult tempResult
;
784 // If a lower confTarget from a more recent horizon returns a lower answer use it.
785 if (confTarget
> feeStats
->GetMaxConfirms()) {
786 double medMax
= feeStats
->EstimateMedianVal(feeStats
->GetMaxConfirms(), SUFFICIENT_FEETXS
, successThreshold
, true, nBestSeenHeight
, &tempResult
);
787 if (medMax
> 0 && (estimate
== -1 || medMax
< estimate
)) {
789 if (result
) *result
= tempResult
;
792 if (confTarget
> shortStats
->GetMaxConfirms()) {
793 double shortMax
= shortStats
->EstimateMedianVal(shortStats
->GetMaxConfirms(), SUFFICIENT_TXS_SHORT
, successThreshold
, true, nBestSeenHeight
, &tempResult
);
794 if (shortMax
> 0 && (estimate
== -1 || shortMax
< estimate
)) {
796 if (result
) *result
= tempResult
;
804 /** Ensure that for a conservative estimate, the DOUBLE_SUCCESS_PCT is also met
805 * at 2 * target for any longer time horizons.
807 double CBlockPolicyEstimator::estimateConservativeFee(unsigned int doubleTarget
, EstimationResult
*result
) const
809 double estimate
= -1;
810 EstimationResult tempResult
;
811 if (doubleTarget
<= shortStats
->GetMaxConfirms()) {
812 estimate
= feeStats
->EstimateMedianVal(doubleTarget
, SUFFICIENT_FEETXS
, DOUBLE_SUCCESS_PCT
, true, nBestSeenHeight
, result
);
814 if (doubleTarget
<= feeStats
->GetMaxConfirms()) {
815 double longEstimate
= longStats
->EstimateMedianVal(doubleTarget
, SUFFICIENT_FEETXS
, DOUBLE_SUCCESS_PCT
, true, nBestSeenHeight
, &tempResult
);
816 if (longEstimate
> estimate
) {
817 estimate
= longEstimate
;
818 if (result
) *result
= tempResult
;
824 /** estimateSmartFee returns the max of the feerates calculated with a 60%
825 * threshold required at target / 2, an 85% threshold required at target and a
826 * 95% threshold required at 2 * target. Each calculation is performed at the
827 * shortest time horizon which tracks the required target. Conservative
828 * estimates, however, required the 95% threshold at 2 * target be met for any
829 * longer time horizons also.
831 CFeeRate
CBlockPolicyEstimator::estimateSmartFee(int confTarget
, FeeCalculation
*feeCalc
, bool conservative
) const
833 LOCK(cs_feeEstimator
);
836 feeCalc
->desiredTarget
= confTarget
;
837 feeCalc
->returnedTarget
= confTarget
;
841 EstimationResult tempResult
;
843 // Return failure if trying to analyze a target we're not tracking
844 if (confTarget
<= 0 || (unsigned int)confTarget
> longStats
->GetMaxConfirms()) {
845 return CFeeRate(0); // error condition
848 // It's not possible to get reasonable estimates for confTarget of 1
849 if (confTarget
== 1) confTarget
= 2;
851 unsigned int maxUsableEstimate
= MaxUsableEstimate();
852 if ((unsigned int)confTarget
> maxUsableEstimate
) {
853 confTarget
= maxUsableEstimate
;
855 if (feeCalc
) feeCalc
->returnedTarget
= confTarget
;
857 if (confTarget
<= 1) return CFeeRate(0); // error condition
859 assert(confTarget
> 0); //estimateCombinedFee and estimateConservativeFee take unsigned ints
860 /** true is passed to estimateCombined fee for target/2 and target so
861 * that we check the max confirms for shorter time horizons as well.
862 * This is necessary to preserve monotonically increasing estimates.
863 * For non-conservative estimates we do the same thing for 2*target, but
864 * for conservative estimates we want to skip these shorter horizons
865 * checks for 2*target because we are taking the max over all time
866 * horizons so we already have monotonically increasing estimates and
867 * the purpose of conservative estimates is not to let short term
868 * fluctuations lower our estimates by too much.
870 double halfEst
= estimateCombinedFee(confTarget
/2, HALF_SUCCESS_PCT
, true, &tempResult
);
872 feeCalc
->est
= tempResult
;
873 feeCalc
->reason
= FeeReason::HALF_ESTIMATE
;
876 double actualEst
= estimateCombinedFee(confTarget
, SUCCESS_PCT
, true, &tempResult
);
877 if (actualEst
> median
) {
880 feeCalc
->est
= tempResult
;
881 feeCalc
->reason
= FeeReason::FULL_ESTIMATE
;
884 double doubleEst
= estimateCombinedFee(2 * confTarget
, DOUBLE_SUCCESS_PCT
, !conservative
, &tempResult
);
885 if (doubleEst
> median
) {
888 feeCalc
->est
= tempResult
;
889 feeCalc
->reason
= FeeReason::DOUBLE_ESTIMATE
;
893 if (conservative
|| median
== -1) {
894 double consEst
= estimateConservativeFee(2 * confTarget
, &tempResult
);
895 if (consEst
> median
) {
898 feeCalc
->est
= tempResult
;
899 feeCalc
->reason
= FeeReason::CONSERVATIVE
;
904 if (median
< 0) return CFeeRate(0); // error condition
906 return CFeeRate(llround(median
));
910 bool CBlockPolicyEstimator::Write(CAutoFile
& fileout
) const
913 LOCK(cs_feeEstimator
);
914 fileout
<< 149900; // version required to read: 0.14.99 or later
915 fileout
<< CLIENT_VERSION
; // version that wrote the file
916 fileout
<< nBestSeenHeight
;
917 if (BlockSpan() > HistoricalBlockSpan()/2) {
918 fileout
<< firstRecordedHeight
<< nBestSeenHeight
;
921 fileout
<< historicalFirst
<< historicalBest
;
924 feeStats
->Write(fileout
);
925 shortStats
->Write(fileout
);
926 longStats
->Write(fileout
);
928 catch (const std::exception
&) {
929 LogPrintf("CBlockPolicyEstimator::Write(): unable to write policy estimator data (non-fatal)\n");
935 bool CBlockPolicyEstimator::Read(CAutoFile
& filein
)
938 LOCK(cs_feeEstimator
);
939 int nVersionRequired
, nVersionThatWrote
;
940 filein
>> nVersionRequired
>> nVersionThatWrote
;
941 if (nVersionRequired
> CLIENT_VERSION
)
942 return error("CBlockPolicyEstimator::Read(): up-version (%d) fee estimate file", nVersionRequired
);
944 // Read fee estimates file into temporary variables so existing data
945 // structures aren't corrupted if there is an exception.
946 unsigned int nFileBestSeenHeight
;
947 filein
>> nFileBestSeenHeight
;
949 if (nVersionThatWrote
< 149900) {
950 // Read the old fee estimates file for temporary use, but then discard. Will start collecting data from scratch.
951 // decay is stored before buckets in old versions, so pre-read decay and pass into TxConfirmStats constructor
954 if (tempDecay
<= 0 || tempDecay
>= 1)
955 throw std::runtime_error("Corrupt estimates file. Decay must be between 0 and 1 (non-inclusive)");
957 std::vector
<double> tempBuckets
;
958 filein
>> tempBuckets
;
959 size_t tempNum
= tempBuckets
.size();
960 if (tempNum
<= 1 || tempNum
> 1000)
961 throw std::runtime_error("Corrupt estimates file. Must have between 2 and 1000 feerate buckets");
963 std::map
<double, unsigned int> tempMap
;
965 std::unique_ptr
<TxConfirmStats
> tempFeeStats(new TxConfirmStats(tempBuckets
, tempMap
, MED_BLOCK_PERIODS
, tempDecay
, 1));
966 tempFeeStats
->Read(filein
, nVersionThatWrote
, tempNum
);
967 // if nVersionThatWrote < 139900 then another TxConfirmStats (for priority) follows but can be ignored.
970 for (unsigned int i
= 0; i
< tempBuckets
.size(); i
++) {
971 tempMap
[tempBuckets
[i
]] = i
;
974 else { // nVersionThatWrote >= 149900
975 unsigned int nFileHistoricalFirst
, nFileHistoricalBest
;
976 filein
>> nFileHistoricalFirst
>> nFileHistoricalBest
;
977 if (nFileHistoricalFirst
> nFileHistoricalBest
|| nFileHistoricalBest
> nFileBestSeenHeight
) {
978 throw std::runtime_error("Corrupt estimates file. Historical block range for estimates is invalid");
980 std::vector
<double> fileBuckets
;
981 filein
>> fileBuckets
;
982 size_t numBuckets
= fileBuckets
.size();
983 if (numBuckets
<= 1 || numBuckets
> 1000)
984 throw std::runtime_error("Corrupt estimates file. Must have between 2 and 1000 feerate buckets");
986 std::unique_ptr
<TxConfirmStats
> fileFeeStats(new TxConfirmStats(buckets
, bucketMap
, MED_BLOCK_PERIODS
, MED_DECAY
, MED_SCALE
));
987 std::unique_ptr
<TxConfirmStats
> fileShortStats(new TxConfirmStats(buckets
, bucketMap
, SHORT_BLOCK_PERIODS
, SHORT_DECAY
, SHORT_SCALE
));
988 std::unique_ptr
<TxConfirmStats
> fileLongStats(new TxConfirmStats(buckets
, bucketMap
, LONG_BLOCK_PERIODS
, LONG_DECAY
, LONG_SCALE
));
989 fileFeeStats
->Read(filein
, nVersionThatWrote
, numBuckets
);
990 fileShortStats
->Read(filein
, nVersionThatWrote
, numBuckets
);
991 fileLongStats
->Read(filein
, nVersionThatWrote
, numBuckets
);
993 // Fee estimates file parsed correctly
994 // Copy buckets from file and refresh our bucketmap
995 buckets
= fileBuckets
;
997 for (unsigned int i
= 0; i
< buckets
.size(); i
++) {
998 bucketMap
[buckets
[i
]] = i
;
1001 // Destroy old TxConfirmStats and point to new ones that already reference buckets and bucketMap
1002 feeStats
= std::move(fileFeeStats
);
1003 shortStats
= std::move(fileShortStats
);
1004 longStats
= std::move(fileLongStats
);
1006 nBestSeenHeight
= nFileBestSeenHeight
;
1007 historicalFirst
= nFileHistoricalFirst
;
1008 historicalBest
= nFileHistoricalBest
;
1011 catch (const std::exception
& e
) {
1012 LogPrintf("CBlockPolicyEstimator::Read(): unable to read policy estimator data (non-fatal): %s\n",e
.what());
1018 void CBlockPolicyEstimator::FlushUnconfirmed(CTxMemPool
& pool
) {
1019 int64_t startclear
= GetTimeMicros();
1020 std::vector
<uint256
> txids
;
1021 pool
.queryHashes(txids
);
1022 LOCK(cs_feeEstimator
);
1023 for (auto& txid
: txids
) {
1024 removeTx(txid
, false);
1026 int64_t endclear
= GetTimeMicros();
1027 LogPrint(BCLog::ESTIMATEFEE
, "Recorded %u unconfirmed txs from mempool in %gs\n",txids
.size(), (endclear
- startclear
)*0.000001);
1030 FeeFilterRounder::FeeFilterRounder(const CFeeRate
& minIncrementalFee
)
1032 CAmount minFeeLimit
= std::max(CAmount(1), minIncrementalFee
.GetFeePerK() / 2);
1034 for (double bucketBoundary
= minFeeLimit
; bucketBoundary
<= MAX_FILTER_FEERATE
; bucketBoundary
*= FEE_FILTER_SPACING
) {
1035 feeset
.insert(bucketBoundary
);
1039 CAmount
FeeFilterRounder::round(CAmount currentMinFee
)
1041 std::set
<double>::iterator it
= feeset
.lower_bound(currentMinFee
);
1042 if ((it
!= feeset
.begin() && insecure_rand
.rand32() % 3 != 0) || it
== feeset
.end()) {
1045 return static_cast<CAmount
>(*it
);