Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/policy/fees.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-2022 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.
5
6
#include <policy/fees.h>
7
8
#include <common/system.h>
9
#include <consensus/amount.h>
10
#include <kernel/mempool_entry.h>
11
#include <logging.h>
12
#include <policy/feerate.h>
13
#include <primitives/transaction.h>
14
#include <random.h>
15
#include <serialize.h>
16
#include <streams.h>
17
#include <sync.h>
18
#include <tinyformat.h>
19
#include <uint256.h>
20
#include <util/fs.h>
21
#include <util/serfloat.h>
22
#include <util/time.h>
23
24
#include <algorithm>
25
#include <cassert>
26
#include <chrono>
27
#include <cmath>
28
#include <cstddef>
29
#include <cstdint>
30
#include <exception>
31
#include <stdexcept>
32
#include <utility>
33
34
// The current format written, and the version required to read. Must be
35
// increased to at least 289900+1 on the next breaking change.
36
constexpr int CURRENT_FEES_FILE_VERSION{149900};
37
38
static constexpr double INF_FEERATE = 1e99;
39
40
std::string StringForFeeEstimateHorizon(FeeEstimateHorizon horizon)
41
0
{
42
0
    switch (horizon) {
  Branch (42:13): [True: 0, False: 0]
43
0
    case FeeEstimateHorizon::SHORT_HALFLIFE: return "short";
  Branch (43:5): [True: 0, False: 0]
44
0
    case FeeEstimateHorizon::MED_HALFLIFE: return "medium";
  Branch (44:5): [True: 0, False: 0]
45
0
    case FeeEstimateHorizon::LONG_HALFLIFE: return "long";
  Branch (45:5): [True: 0, False: 0]
46
0
    } // no default case, so the compiler can warn about missing cases
47
0
    assert(false);
  Branch (47:5): [Folded - Ignored]
48
0
}
49
50
namespace {
51
52
struct EncodedDoubleFormatter
53
{
54
    template<typename Stream> void Ser(Stream &s, double v)
55
343M
    {
56
343M
        s << EncodeDouble(v);
57
343M
    }
58
59
    template<typename Stream> void Unser(Stream& s, double& v)
60
0
    {
61
0
        uint64_t encoded;
62
0
        s >> encoded;
63
0
        v = DecodeDouble(encoded);
64
0
    }
65
};
66
67
} // namespace
68
69
/**
70
 * We will instantiate an instance of this class to track transactions that were
71
 * included in a block. We will lump transactions into a bucket according to their
72
 * approximate feerate and then track how long it took for those txs to be included in a block
73
 *
74
 * The tracking of unconfirmed (mempool) transactions is completely independent of the
75
 * historical tracking of transactions that have been confirmed in a block.
76
 */
77
class TxConfirmStats
78
{
79
private:
80
    //Define the buckets we will group transactions into
81
    const std::vector<double>& buckets;              // The upper-bound of the range for the bucket (inclusive)
82
    const std::map<double, unsigned int>& bucketMap; // Map of bucket upper-bound to index into all vectors by bucket
83
84
    // For each bucket X:
85
    // Count the total # of txs in each bucket
86
    // Track the historical moving average of this total over blocks
87
    std::vector<double> txCtAvg;
88
89
    // Count the total # of txs confirmed within Y blocks in each bucket
90
    // Track the historical moving average of these totals over blocks
91
    std::vector<std::vector<double>> confAvg; // confAvg[Y][X]
92
93
    // Track moving avg of txs which have been evicted from the mempool
94
    // after failing to be confirmed within Y blocks
95
    std::vector<std::vector<double>> failAvg; // failAvg[Y][X]
96
97
    // Sum the total feerate of all tx's in each bucket
98
    // Track the historical moving average of this total over blocks
99
    std::vector<double> m_feerate_avg;
100
101
    // Combine the conf counts with tx counts to calculate the confirmation % for each Y,X
102
    // Combine the total value with the tx counts to calculate the avg feerate per bucket
103
104
    double decay;
105
106
    // Resolution (# of blocks) with which confirmations are tracked
107
    unsigned int scale;
108
109
    // Mempool counts of outstanding transactions
110
    // For each bucket X, track the number of transactions in the mempool
111
    // that are unconfirmed for each possible confirmation value Y
112
    std::vector<std::vector<int> > unconfTxs;  //unconfTxs[Y][X]
113
    // transactions still unconfirmed after GetMaxConfirms for each bucket
114
    std::vector<int> oldUnconfTxs;
115
116
    void resizeInMemoryCounters(size_t newbuckets);
117
118
public:
119
    /**
120
     * Create new TxConfirmStats. This is called by BlockPolicyEstimator's
121
     * constructor with default values.
122
     * @param defaultBuckets contains the upper limits for the bucket boundaries
123
     * @param maxPeriods max number of periods to track
124
     * @param decay how much to decay the historical moving average per block
125
     */
126
    TxConfirmStats(const std::vector<double>& defaultBuckets, const std::map<double, unsigned int>& defaultBucketMap,
127
                   unsigned int maxPeriods, double decay, unsigned int scale);
128
129
    /** Roll the circular buffer for unconfirmed txs*/
130
    void ClearCurrent(unsigned int nBlockHeight);
131
132
    /**
133
     * Record a new transaction data point in the current block stats
134
     * @param blocksToConfirm the number of blocks it took this transaction to confirm
135
     * @param val the feerate of the transaction
136
     * @warning blocksToConfirm is 1-based and has to be >= 1
137
     */
138
    void Record(int blocksToConfirm, double val);
139
140
    /** Record a new transaction entering the mempool*/
141
    unsigned int NewTx(unsigned int nBlockHeight, double val);
142
143
    /** Remove a transaction from mempool tracking stats*/
144
    void removeTx(unsigned int entryHeight, unsigned int nBestSeenHeight,
145
                  unsigned int bucketIndex, bool inBlock);
146
147
    /** Update our estimates by decaying our historical moving average and updating
148
        with the data gathered from the current block */
149
    void UpdateMovingAverages();
150
151
    /**
152
     * Calculate a feerate estimate.  Find the lowest value bucket (or range of buckets
153
     * to make sure we have enough data points) whose transactions still have sufficient likelihood
154
     * of being confirmed within the target number of confirmations
155
     * @param confTarget target number of confirmations
156
     * @param sufficientTxVal required average number of transactions per block in a bucket range
157
     * @param minSuccess the success probability we require
158
     * @param nBlockHeight the current block height
159
     */
160
    double EstimateMedianVal(int confTarget, double sufficientTxVal,
161
                             double minSuccess, unsigned int nBlockHeight,
162
                             EstimationResult *result = nullptr) const;
163
164
    /** Return the max number of confirms we're tracking */
165
2.25M
    unsigned int GetMaxConfirms() const { return scale * confAvg.size(); }
166
167
    /** Write state of estimation data to a file*/
168
    void Write(AutoFile& fileout) const;
169
170
    /**
171
     * Read saved state of estimation data from a file and replace all internal data structures and
172
     * variables with this state.
173
     */
174
    void Read(AutoFile& filein, size_t numBuckets);
175
};
176
177
178
TxConfirmStats::TxConfirmStats(const std::vector<double>& defaultBuckets,
179
                                const std::map<double, unsigned int>& defaultBucketMap,
180
                               unsigned int maxPeriods, double _decay, unsigned int _scale)
181
33.2k
    : buckets(defaultBuckets), bucketMap(defaultBucketMap), decay(_decay), scale(_scale)
182
33.2k
{
183
33.2k
    assert(_scale != 0 && "_scale must be non-zero");
  Branch (183:5): [True: 33.2k, False: 0]
  Branch (183:5): [Folded - Ignored]
  Branch (183:5): [True: 33.2k, False: 0]
184
33.2k
    confAvg.resize(maxPeriods);
185
33.2k
    failAvg.resize(maxPeriods);
186
898k
    for (unsigned int i = 0; i < maxPeriods; i++) {
  Branch (186:30): [True: 865k, False: 33.2k]
187
865k
        confAvg[i].resize(buckets.size());
188
865k
        failAvg[i].resize(buckets.size());
189
865k
    }
190
191
33.2k
    txCtAvg.resize(buckets.size());
192
33.2k
    m_feerate_avg.resize(buckets.size());
193
194
33.2k
    resizeInMemoryCounters(buckets.size());
195
33.2k
}
196
197
33.2k
void TxConfirmStats::resizeInMemoryCounters(size_t newbuckets) {
198
    // newbuckets must be passed in because the buckets referred to during Read have not been updated yet.
199
33.2k
    unconfTxs.resize(GetMaxConfirms());
200
11.8M
    for (unsigned int i = 0; i < unconfTxs.size(); i++) {
  Branch (200:30): [True: 11.8M, False: 33.2k]
201
11.8M
        unconfTxs[i].resize(newbuckets);
202
11.8M
    }
203
33.2k
    oldUnconfTxs.resize(newbuckets);
204
33.2k
}
205
206
// Roll the unconfirmed txs circular buffer
207
void TxConfirmStats::ClearCurrent(unsigned int nBlockHeight)
208
6.66M
{
209
1.27G
    for (unsigned int j = 0; j < buckets.size(); j++) {
  Branch (209:30): [True: 1.26G, False: 6.66M]
210
1.26G
        oldUnconfTxs[j] += unconfTxs[nBlockHeight % unconfTxs.size()][j];
211
1.26G
        unconfTxs[nBlockHeight%unconfTxs.size()][j] = 0;
212
1.26G
    }
213
6.66M
}
214
215
216
void TxConfirmStats::Record(int blocksToConfirm, double feerate)
217
582
{
218
    // blocksToConfirm is 1-based
219
582
    if (blocksToConfirm < 1)
  Branch (219:9): [True: 0, False: 582]
220
0
        return;
221
582
    int periodsToConfirm = (blocksToConfirm + scale - 1) / scale;
222
582
    unsigned int bucketindex = bucketMap.lower_bound(feerate)->second;
223
15.7k
    for (size_t i = periodsToConfirm; i <= confAvg.size(); i++) {
  Branch (223:39): [True: 15.1k, False: 582]
224
15.1k
        confAvg[i - 1][bucketindex]++;
225
15.1k
    }
226
582
    txCtAvg[bucketindex]++;
227
582
    m_feerate_avg[bucketindex] += feerate;
228
582
}
229
230
void TxConfirmStats::UpdateMovingAverages()
231
6.66M
{
232
6.66M
    assert(confAvg.size() == failAvg.size());
  Branch (232:5): [True: 6.66M, False: 0]
233
1.27G
    for (unsigned int j = 0; j < buckets.size(); j++) {
  Branch (233:30): [True: 1.26G, False: 6.66M]
234
34.1G
        for (unsigned int i = 0; i < confAvg.size(); i++) {
  Branch (234:34): [True: 32.9G, False: 1.26G]
235
32.9G
            confAvg[i][j] *= decay;
236
32.9G
            failAvg[i][j] *= decay;
237
32.9G
        }
238
1.26G
        m_feerate_avg[j] *= decay;
239
1.26G
        txCtAvg[j] *= decay;
240
1.26G
    }
241
6.66M
}
242
243
// returns -1 on error conditions
244
double TxConfirmStats::EstimateMedianVal(int confTarget, double sufficientTxVal,
245
                                         double successBreakPoint, unsigned int nBlockHeight,
246
                                         EstimationResult *result) const
247
0
{
248
    // Counters for a bucket (or range of buckets)
249
0
    double nConf = 0; // Number of tx's confirmed within the confTarget
250
0
    double totalNum = 0; // Total number of tx's that were ever confirmed
251
0
    int extraNum = 0;  // Number of tx's still in mempool for confTarget or longer
252
0
    double failNum = 0; // Number of tx's that were never confirmed but removed from the mempool after confTarget
253
0
    const int periodTarget = (confTarget + scale - 1) / scale;
254
0
    const int maxbucketindex = buckets.size() - 1;
255
256
    // We'll combine buckets until we have enough samples.
257
    // The near and far variables will define the range we've combined
258
    // The best variables are the last range we saw which still had a high
259
    // enough confirmation rate to count as success.
260
    // The cur variables are the current range we're counting.
261
0
    unsigned int curNearBucket = maxbucketindex;
262
0
    unsigned int bestNearBucket = maxbucketindex;
263
0
    unsigned int curFarBucket = maxbucketindex;
264
0
    unsigned int bestFarBucket = maxbucketindex;
265
266
    // We'll always group buckets into sets that meet sufficientTxVal --
267
    // this ensures that we're using consistent groups between different
268
    // confirmation targets.
269
0
    double partialNum = 0;
270
271
0
    bool foundAnswer = false;
272
0
    unsigned int bins = unconfTxs.size();
273
0
    bool newBucketRange = true;
274
0
    bool passing = true;
275
0
    EstimatorBucket passBucket;
276
0
    EstimatorBucket failBucket;
277
278
    // Start counting from highest feerate transactions
279
0
    for (int bucket = maxbucketindex; bucket >= 0; --bucket) {
  Branch (279:39): [True: 0, False: 0]
280
0
        if (newBucketRange) {
  Branch (280:13): [True: 0, False: 0]
281
0
            curNearBucket = bucket;
282
0
            newBucketRange = false;
283
0
        }
284
0
        curFarBucket = bucket;
285
0
        nConf += confAvg[periodTarget - 1][bucket];
286
0
        partialNum += txCtAvg[bucket];
287
0
        totalNum += txCtAvg[bucket];
288
0
        failNum += failAvg[periodTarget - 1][bucket];
289
0
        for (unsigned int confct = confTarget; confct < GetMaxConfirms(); confct++)
  Branch (289:48): [True: 0, False: 0]
290
0
            extraNum += unconfTxs[(nBlockHeight - confct) % bins][bucket];
291
0
        extraNum += oldUnconfTxs[bucket];
292
        // If we have enough transaction data points in this range of buckets,
293
        // we can test for success
294
        // (Only count the confirmed data points, so that each confirmation count
295
        // will be looking at the same amount of data and same bucket breaks)
296
297
0
        if (partialNum < sufficientTxVal / (1 - decay)) {
  Branch (297:13): [True: 0, False: 0]
298
            // the buckets we've added in this round aren't sufficient
299
            // so keep adding
300
0
            continue;
301
0
        } else {
302
0
            partialNum = 0; // reset for the next range we'll add
303
304
0
            double curPct = nConf / (totalNum + failNum + extraNum);
305
306
            // Check to see if we are no longer getting confirmed at the success rate
307
0
            if (curPct < successBreakPoint) {
  Branch (307:17): [True: 0, False: 0]
308
0
                if (passing == true) {
  Branch (308:21): [True: 0, False: 0]
309
                    // First time we hit a failure record the failed bucket
310
0
                    unsigned int failMinBucket = std::min(curNearBucket, curFarBucket);
311
0
                    unsigned int failMaxBucket = std::max(curNearBucket, curFarBucket);
312
0
                    failBucket.start = failMinBucket ? buckets[failMinBucket - 1] : 0;
  Branch (312:40): [True: 0, False: 0]
313
0
                    failBucket.end = buckets[failMaxBucket];
314
0
                    failBucket.withinTarget = nConf;
315
0
                    failBucket.totalConfirmed = totalNum;
316
0
                    failBucket.inMempool = extraNum;
317
0
                    failBucket.leftMempool = failNum;
318
0
                    passing = false;
319
0
                }
320
0
                continue;
321
0
            }
322
            // Otherwise update the cumulative stats, and the bucket variables
323
            // and reset the counters
324
0
            else {
325
0
                failBucket = EstimatorBucket(); // Reset any failed bucket, currently passing
326
0
                foundAnswer = true;
327
0
                passing = true;
328
0
                passBucket.withinTarget = nConf;
329
0
                nConf = 0;
330
0
                passBucket.totalConfirmed = totalNum;
331
0
                totalNum = 0;
332
0
                passBucket.inMempool = extraNum;
333
0
                passBucket.leftMempool = failNum;
334
0
                failNum = 0;
335
0
                extraNum = 0;
336
0
                bestNearBucket = curNearBucket;
337
0
                bestFarBucket = curFarBucket;
338
0
                newBucketRange = true;
339
0
            }
340
0
        }
341
0
    }
342
343
0
    double median = -1;
344
0
    double txSum = 0;
345
346
    // Calculate the "average" feerate of the best bucket range that met success conditions
347
    // Find the bucket with the median transaction and then report the average feerate from that bucket
348
    // This is a compromise between finding the median which we can't since we don't save all tx's
349
    // and reporting the average which is less accurate
350
0
    unsigned int minBucket = std::min(bestNearBucket, bestFarBucket);
351
0
    unsigned int maxBucket = std::max(bestNearBucket, bestFarBucket);
352
0
    for (unsigned int j = minBucket; j <= maxBucket; j++) {
  Branch (352:38): [True: 0, False: 0]
353
0
        txSum += txCtAvg[j];
354
0
    }
355
0
    if (foundAnswer && txSum != 0) {
  Branch (355:9): [True: 0, False: 0]
  Branch (355:24): [True: 0, False: 0]
356
0
        txSum = txSum / 2;
357
0
        for (unsigned int j = minBucket; j <= maxBucket; j++) {
  Branch (357:42): [True: 0, False: 0]
358
0
            if (txCtAvg[j] < txSum)
  Branch (358:17): [True: 0, False: 0]
359
0
                txSum -= txCtAvg[j];
360
0
            else { // we're in the right bucket
361
0
                median = m_feerate_avg[j] / txCtAvg[j];
362
0
                break;
363
0
            }
364
0
        }
365
366
0
        passBucket.start = minBucket ? buckets[minBucket-1] : 0;
  Branch (366:28): [True: 0, False: 0]
367
0
        passBucket.end = buckets[maxBucket];
368
0
    }
369
370
    // If we were passing until we reached last few buckets with insufficient data, then report those as failed
371
0
    if (passing && !newBucketRange) {
  Branch (371:9): [True: 0, False: 0]
  Branch (371:20): [True: 0, False: 0]
372
0
        unsigned int failMinBucket = std::min(curNearBucket, curFarBucket);
373
0
        unsigned int failMaxBucket = std::max(curNearBucket, curFarBucket);
374
0
        failBucket.start = failMinBucket ? buckets[failMinBucket - 1] : 0;
  Branch (374:28): [True: 0, False: 0]
375
0
        failBucket.end = buckets[failMaxBucket];
376
0
        failBucket.withinTarget = nConf;
377
0
        failBucket.totalConfirmed = totalNum;
378
0
        failBucket.inMempool = extraNum;
379
0
        failBucket.leftMempool = failNum;
380
0
    }
381
382
0
    float passed_within_target_perc = 0.0;
383
0
    float failed_within_target_perc = 0.0;
384
0
    if ((passBucket.totalConfirmed + passBucket.inMempool + passBucket.leftMempool)) {
  Branch (384:9): [True: 0, False: 0]
385
0
        passed_within_target_perc = 100 * passBucket.withinTarget / (passBucket.totalConfirmed + passBucket.inMempool + passBucket.leftMempool);
386
0
    }
387
0
    if ((failBucket.totalConfirmed + failBucket.inMempool + failBucket.leftMempool)) {
  Branch (387:9): [True: 0, False: 0]
388
0
        failed_within_target_perc = 100 * failBucket.withinTarget / (failBucket.totalConfirmed + failBucket.inMempool + failBucket.leftMempool);
389
0
    }
390
391
0
    LogDebug(BCLog::ESTIMATEFEE, "FeeEst: %d > %.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",
392
0
             confTarget, 100.0 * successBreakPoint, decay,
393
0
             median, passBucket.start, passBucket.end,
394
0
             passed_within_target_perc,
395
0
             passBucket.withinTarget, passBucket.totalConfirmed, passBucket.inMempool, passBucket.leftMempool,
396
0
             failBucket.start, failBucket.end,
397
0
             failed_within_target_perc,
398
0
             failBucket.withinTarget, failBucket.totalConfirmed, failBucket.inMempool, failBucket.leftMempool);
399
400
401
0
    if (result) {
  Branch (401:9): [True: 0, False: 0]
402
0
        result->pass = passBucket;
403
0
        result->fail = failBucket;
404
0
        result->decay = decay;
405
0
        result->scale = scale;
406
0
    }
407
0
    return median;
408
0
}
409
410
void TxConfirmStats::Write(AutoFile& fileout) const
411
33.2k
{
412
33.2k
    fileout << Using<EncodedDoubleFormatter>(decay);
413
33.2k
    fileout << scale;
414
33.2k
    fileout << Using<VectorFormatter<EncodedDoubleFormatter>>(m_feerate_avg);
415
33.2k
    fileout << Using<VectorFormatter<EncodedDoubleFormatter>>(txCtAvg);
416
33.2k
    fileout << Using<VectorFormatter<VectorFormatter<EncodedDoubleFormatter>>>(confAvg);
417
33.2k
    fileout << Using<VectorFormatter<VectorFormatter<EncodedDoubleFormatter>>>(failAvg);
418
33.2k
}
419
420
void TxConfirmStats::Read(AutoFile& filein, size_t numBuckets)
421
0
{
422
    // Read data file and do some very basic sanity checking
423
    // buckets and bucketMap are not updated yet, so don't access them
424
    // If there is a read failure, we'll just discard this entire object anyway
425
0
    size_t maxConfirms, maxPeriods;
426
427
    // The current version will store the decay with each individual TxConfirmStats and also keep a scale factor
428
0
    filein >> Using<EncodedDoubleFormatter>(decay);
429
0
    if (decay <= 0 || decay >= 1) {
  Branch (429:9): [True: 0, False: 0]
  Branch (429:23): [True: 0, False: 0]
430
0
        throw std::runtime_error("Corrupt estimates file. Decay must be between 0 and 1 (non-inclusive)");
431
0
    }
432
0
    filein >> scale;
433
0
    if (scale == 0) {
  Branch (433:9): [True: 0, False: 0]
434
0
        throw std::runtime_error("Corrupt estimates file. Scale must be non-zero");
435
0
    }
436
437
0
    filein >> Using<VectorFormatter<EncodedDoubleFormatter>>(m_feerate_avg);
438
0
    if (m_feerate_avg.size() != numBuckets) {
  Branch (438:9): [True: 0, False: 0]
439
0
        throw std::runtime_error("Corrupt estimates file. Mismatch in feerate average bucket count");
440
0
    }
441
0
    filein >> Using<VectorFormatter<EncodedDoubleFormatter>>(txCtAvg);
442
0
    if (txCtAvg.size() != numBuckets) {
  Branch (442:9): [True: 0, False: 0]
443
0
        throw std::runtime_error("Corrupt estimates file. Mismatch in tx count bucket count");
444
0
    }
445
0
    filein >> Using<VectorFormatter<VectorFormatter<EncodedDoubleFormatter>>>(confAvg);
446
0
    maxPeriods = confAvg.size();
447
0
    maxConfirms = scale * maxPeriods;
448
449
0
    if (maxConfirms <= 0 || maxConfirms > 6 * 24 * 7) { // one week
  Branch (449:9): [True: 0, False: 0]
  Branch (449:29): [True: 0, False: 0]
450
0
        throw std::runtime_error("Corrupt estimates file.  Must maintain estimates for between 1 and 1008 (one week) confirms");
451
0
    }
452
0
    for (unsigned int i = 0; i < maxPeriods; i++) {
  Branch (452:30): [True: 0, False: 0]
453
0
        if (confAvg[i].size() != numBuckets) {
  Branch (453:13): [True: 0, False: 0]
454
0
            throw std::runtime_error("Corrupt estimates file. Mismatch in feerate conf average bucket count");
455
0
        }
456
0
    }
457
458
0
    filein >> Using<VectorFormatter<VectorFormatter<EncodedDoubleFormatter>>>(failAvg);
459
0
    if (maxPeriods != failAvg.size()) {
  Branch (459:9): [True: 0, False: 0]
460
0
        throw std::runtime_error("Corrupt estimates file. Mismatch in confirms tracked for failures");
461
0
    }
462
0
    for (unsigned int i = 0; i < maxPeriods; i++) {
  Branch (462:30): [True: 0, False: 0]
463
0
        if (failAvg[i].size() != numBuckets) {
  Branch (463:13): [True: 0, False: 0]
464
0
            throw std::runtime_error("Corrupt estimates file. Mismatch in one of failure average bucket counts");
465
0
        }
466
0
    }
467
468
    // Resize the current block variables which aren't stored in the data file
469
    // to match the number of confirms and buckets
470
0
    resizeInMemoryCounters(numBuckets);
471
472
0
    LogDebug(BCLog::ESTIMATEFEE, "Reading estimates: %u buckets counting confirms up to %u blocks\n",
473
0
             numBuckets, maxConfirms);
474
0
}
475
476
unsigned int TxConfirmStats::NewTx(unsigned int nBlockHeight, double val)
477
23.3k
{
478
23.3k
    unsigned int bucketindex = bucketMap.lower_bound(val)->second;
479
23.3k
    unsigned int blockIndex = nBlockHeight % unconfTxs.size();
480
23.3k
    unconfTxs[blockIndex][bucketindex]++;
481
23.3k
    return bucketindex;
482
23.3k
}
483
484
void TxConfirmStats::removeTx(unsigned int entryHeight, unsigned int nBestSeenHeight, unsigned int bucketindex, bool inBlock)
485
23.3k
{
486
    //nBestSeenHeight is not updated yet for the new block
487
23.3k
    int blocksAgo = nBestSeenHeight - entryHeight;
488
23.3k
    if (nBestSeenHeight == 0)  // the BlockPolicyEstimator hasn't seen any blocks yet
  Branch (488:9): [True: 0, False: 23.3k]
489
0
        blocksAgo = 0;
490
23.3k
    if (blocksAgo < 0) {
  Branch (490:9): [True: 0, False: 23.3k]
491
0
        LogDebug(BCLog::ESTIMATEFEE, "Blockpolicy error, blocks ago is negative for mempool tx\n");
492
0
        return;  //This can't happen because we call this with our best seen height, no entries can have higher
493
0
    }
494
495
23.3k
    if (blocksAgo >= (int)unconfTxs.size()) {
  Branch (495:9): [True: 0, False: 23.3k]
496
0
        if (oldUnconfTxs[bucketindex] > 0) {
  Branch (496:13): [True: 0, False: 0]
497
0
            oldUnconfTxs[bucketindex]--;
498
0
        } else {
499
0
            LogDebug(BCLog::ESTIMATEFEE, "Blockpolicy error, mempool tx removed from >25 blocks,bucketIndex=%u already\n",
500
0
                     bucketindex);
501
0
        }
502
0
    }
503
23.3k
    else {
504
23.3k
        unsigned int blockIndex = entryHeight % unconfTxs.size();
505
23.3k
        if (unconfTxs[blockIndex][bucketindex] > 0) {
  Branch (505:13): [True: 23.3k, False: 0]
506
23.3k
            unconfTxs[blockIndex][bucketindex]--;
507
23.3k
        } else {
508
0
            LogDebug(BCLog::ESTIMATEFEE, "Blockpolicy error, mempool tx removed from blockIndex=%u,bucketIndex=%u already\n",
509
0
                     blockIndex, bucketindex);
510
0
        }
511
23.3k
    }
512
23.3k
    if (!inBlock && (unsigned int)blocksAgo >= scale) { // Only counts as a failure if not confirmed for entire period
  Branch (512:9): [True: 22.8k, False: 582]
  Branch (512:21): [True: 695, False: 22.1k]
513
695
        assert(scale != 0);
  Branch (513:9): [True: 695, False: 0]
514
695
        unsigned int periodsAgo = blocksAgo / scale;
515
1.58k
        for (size_t i = 0; i < periodsAgo && i < failAvg.size(); i++) {
  Branch (515:28): [True: 889, False: 695]
  Branch (515:46): [True: 889, False: 0]
516
889
            failAvg[i][bucketindex]++;
517
889
        }
518
695
    }
519
23.3k
}
520
521
bool CBlockPolicyEstimator::removeTx(uint256 hash)
522
11.2k
{
523
11.2k
    LOCK(m_cs_fee_estimator);
524
11.2k
    return _removeTx(hash, /*inBlock=*/false);
525
11.2k
}
526
527
bool CBlockPolicyEstimator::_removeTx(const uint256& hash, bool inBlock)
528
18.2k
{
529
18.2k
    AssertLockHeld(m_cs_fee_estimator);
530
18.2k
    std::map<uint256, TxStatsInfo>::iterator pos = mapMemPoolTxs.find(hash);
531
18.2k
    if (pos != mapMemPoolTxs.end()) {
  Branch (531:9): [True: 7.79k, False: 10.4k]
532
7.79k
        feeStats->removeTx(pos->second.blockHeight, nBestSeenHeight, pos->second.bucketIndex, inBlock);
533
7.79k
        shortStats->removeTx(pos->second.blockHeight, nBestSeenHeight, pos->second.bucketIndex, inBlock);
534
7.79k
        longStats->removeTx(pos->second.blockHeight, nBestSeenHeight, pos->second.bucketIndex, inBlock);
535
7.79k
        mapMemPoolTxs.erase(hash);
536
7.79k
        return true;
537
10.4k
    } else {
538
10.4k
        return false;
539
10.4k
    }
540
18.2k
}
541
542
CBlockPolicyEstimator::CBlockPolicyEstimator(const fs::path& estimation_filepath, const bool read_stale_estimates)
543
11.0k
    : m_estimation_filepath{estimation_filepath}
544
11.0k
{
545
11.0k
    static_assert(MIN_BUCKET_FEERATE > 0, "Min feerate must be nonzero");
546
11.0k
    size_t bucketIndex = 0;
547
548
2.10M
    for (double bucketBoundary = MIN_BUCKET_FEERATE; bucketBoundary <= MAX_BUCKET_FEERATE; bucketBoundary *= FEE_SPACING, bucketIndex++) {
  Branch (548:54): [True: 2.09M, False: 11.0k]
549
2.09M
        buckets.push_back(bucketBoundary);
550
2.09M
        bucketMap[bucketBoundary] = bucketIndex;
551
2.09M
    }
552
11.0k
    buckets.push_back(INF_FEERATE);
553
11.0k
    bucketMap[INF_FEERATE] = bucketIndex;
554
11.0k
    assert(bucketMap.size() == buckets.size());
  Branch (554:5): [True: 11.0k, False: 0]
555
556
11.0k
    feeStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, MED_BLOCK_PERIODS, MED_DECAY, MED_SCALE));
557
11.0k
    shortStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, SHORT_BLOCK_PERIODS, SHORT_DECAY, SHORT_SCALE));
558
11.0k
    longStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, LONG_BLOCK_PERIODS, LONG_DECAY, LONG_SCALE));
559
560
11.0k
    AutoFile est_file{fsbridge::fopen(m_estimation_filepath, "rb")};
561
562
11.0k
    if (est_file.IsNull()) {
  Branch (562:9): [True: 11.0k, False: 0]
563
11.0k
        LogPrintf("%s is not found. Continue anyway.\n", fs::PathToString(m_estimation_filepath));
564
11.0k
        return;
565
11.0k
    }
566
567
0
    std::chrono::hours file_age = GetFeeEstimatorFileAge();
568
0
    if (file_age > MAX_FILE_AGE && !read_stale_estimates) {
  Branch (568:9): [True: 0, False: 0]
  Branch (568:36): [True: 0, False: 0]
569
0
        LogPrintf("Fee estimation file %s too old (age=%lld > %lld hours) and will not be used to avoid serving stale estimates.\n", fs::PathToString(m_estimation_filepath), Ticks<std::chrono::hours>(file_age), Ticks<std::chrono::hours>(MAX_FILE_AGE));
570
0
        return;
571
0
    }
572
573
0
    if (!Read(est_file)) {
  Branch (573:9): [True: 0, False: 0]
574
0
        LogPrintf("Failed to read fee estimates from %s. Continue anyway.\n", fs::PathToString(m_estimation_filepath));
575
0
    }
576
0
}
577
578
11.0k
CBlockPolicyEstimator::~CBlockPolicyEstimator() = default;
579
580
void CBlockPolicyEstimator::TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t /*unused*/)
581
43.1k
{
582
43.1k
    processTransaction(tx);
583
43.1k
}
584
585
void CBlockPolicyEstimator::TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason /*unused*/, uint64_t /*unused*/)
586
11.2k
{
587
11.2k
    removeTx(tx->GetHash());
588
11.2k
}
589
590
void CBlockPolicyEstimator::MempoolTransactionsRemovedForBlock(const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block, unsigned int nBlockHeight)
591
2.23M
{
592
2.23M
    processBlock(txs_removed_for_block, nBlockHeight);
593
2.23M
}
594
595
void CBlockPolicyEstimator::processTransaction(const NewMempoolTransactionInfo& tx)
596
43.1k
{
597
43.1k
    LOCK(m_cs_fee_estimator);
598
43.1k
    const unsigned int txHeight = tx.info.txHeight;
599
43.1k
    const auto& hash = tx.info.m_tx->GetHash();
600
43.1k
    if (mapMemPoolTxs.count(hash)) {
  Branch (600:9): [True: 65, False: 43.0k]
601
65
        LogDebug(BCLog::ESTIMATEFEE, "Blockpolicy error mempool tx %s already being tracked\n",
602
65
                 hash.ToString());
603
65
        return;
604
65
    }
605
606
43.0k
    if (txHeight != nBestSeenHeight) {
  Branch (606:9): [True: 971, False: 42.1k]
607
        // Ignore side chains and re-orgs; assuming they are random they don't
608
        // affect the estimate.  We'll potentially double count transactions in 1-block reorgs.
609
        // Ignore txs if BlockPolicyEstimator is not in sync with ActiveChain().Tip().
610
        // It will be synced next time a block is processed.
611
971
        return;
612
971
    }
613
    // This transaction should only count for fee estimation if:
614
    // - it's not being re-added during a reorg which bypasses typical mempool fee limits
615
    // - the node is not behind
616
    // - the transaction is not dependent on any other transactions in the mempool
617
    // - it's not part of a package.
618
42.1k
    const bool validForFeeEstimation = !tx.m_mempool_limit_bypassed && !tx.m_submitted_in_package && tx.m_chainstate_is_current && tx.m_has_no_mempool_parents;
  Branch (618:40): [True: 38.1k, False: 3.95k]
  Branch (618:72): [True: 37.9k, False: 182]
  Branch (618:102): [True: 28.4k, False: 9.53k]
  Branch (618:132): [True: 7.79k, False: 20.6k]
619
620
    // Only want to be updating estimates when our blockchain is synced,
621
    // otherwise we'll miscalculate how many blocks its taking to get included.
622
42.1k
    if (!validForFeeEstimation) {
  Branch (622:9): [True: 34.3k, False: 7.79k]
623
34.3k
        untrackedTxs++;
624
34.3k
        return;
625
34.3k
    }
626
7.79k
    trackedTxs++;
627
628
    // Feerates are stored and reported as BTC-per-kb:
629
7.79k
    const CFeeRate feeRate(tx.info.m_fee, tx.info.m_virtual_transaction_size);
630
631
7.79k
    mapMemPoolTxs[hash].blockHeight = txHeight;
632
7.79k
    unsigned int bucketIndex = feeStats->NewTx(txHeight, static_cast<double>(feeRate.GetFeePerK()));
633
7.79k
    mapMemPoolTxs[hash].bucketIndex = bucketIndex;
634
7.79k
    unsigned int bucketIndex2 = shortStats->NewTx(txHeight, static_cast<double>(feeRate.GetFeePerK()));
635
7.79k
    assert(bucketIndex == bucketIndex2);
  Branch (635:5): [True: 7.79k, False: 0]
636
7.79k
    unsigned int bucketIndex3 = longStats->NewTx(txHeight, static_cast<double>(feeRate.GetFeePerK()));
637
7.79k
    assert(bucketIndex == bucketIndex3);
  Branch (637:5): [True: 7.79k, False: 0]
638
7.79k
}
639
640
bool CBlockPolicyEstimator::processBlockTx(unsigned int nBlockHeight, const RemovedMempoolTransactionInfo& tx)
641
1.22k
{
642
1.22k
    AssertLockHeld(m_cs_fee_estimator);
643
1.22k
    if (!_removeTx(tx.info.m_tx->GetHash(), true)) {
  Branch (643:9): [True: 1.03k, False: 194]
644
        // This transaction wasn't being tracked for fee estimation
645
1.03k
        return false;
646
1.03k
    }
647
648
    // How many blocks did it take for miners to include this transaction?
649
    // blocksToConfirm is 1-based, so a transaction included in the earliest
650
    // possible block has confirmation count of 1
651
194
    int blocksToConfirm = nBlockHeight - tx.info.txHeight;
652
194
    if (blocksToConfirm <= 0) {
  Branch (652:9): [True: 0, False: 194]
653
        // This can't happen because we don't process transactions from a block with a height
654
        // lower than our greatest seen height
655
0
        LogDebug(BCLog::ESTIMATEFEE, "Blockpolicy error Transaction had negative blocksToConfirm\n");
656
0
        return false;
657
0
    }
658
659
    // Feerates are stored and reported as BTC-per-kb:
660
194
    CFeeRate feeRate(tx.info.m_fee, tx.info.m_virtual_transaction_size);
661
662
194
    feeStats->Record(blocksToConfirm, static_cast<double>(feeRate.GetFeePerK()));
663
194
    shortStats->Record(blocksToConfirm, static_cast<double>(feeRate.GetFeePerK()));
664
194
    longStats->Record(blocksToConfirm, static_cast<double>(feeRate.GetFeePerK()));
665
194
    return true;
666
194
}
667
668
void CBlockPolicyEstimator::processBlock(const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block,
669
                                         unsigned int nBlockHeight)
670
2.23M
{
671
2.23M
    LOCK(m_cs_fee_estimator);
672
2.23M
    if (nBlockHeight <= nBestSeenHeight) {
  Branch (672:9): [True: 14.9k, False: 2.22M]
673
        // Ignore side chains and re-orgs; assuming they are random
674
        // they don't affect the estimate.
675
        // And if an attacker can re-org the chain at will, then
676
        // you've got much bigger problems than "attacker can influence
677
        // transaction fees."
678
14.9k
        return;
679
14.9k
    }
680
681
    // Must update nBestSeenHeight in sync with ClearCurrent so that
682
    // calls to removeTx (via processBlockTx) correctly calculate age
683
    // of unconfirmed txs to remove from tracking.
684
2.22M
    nBestSeenHeight = nBlockHeight;
685
686
    // Update unconfirmed circular buffer
687
2.22M
    feeStats->ClearCurrent(nBlockHeight);
688
2.22M
    shortStats->ClearCurrent(nBlockHeight);
689
2.22M
    longStats->ClearCurrent(nBlockHeight);
690
691
    // Decay all exponential averages
692
2.22M
    feeStats->UpdateMovingAverages();
693
2.22M
    shortStats->UpdateMovingAverages();
694
2.22M
    longStats->UpdateMovingAverages();
695
696
2.22M
    unsigned int countedTxs = 0;
697
    // Update averages with data points from current block
698
2.22M
    for (const auto& tx : txs_removed_for_block) {
  Branch (698:25): [True: 1.22k, False: 2.22M]
699
1.22k
        if (processBlockTx(nBlockHeight, tx))
  Branch (699:13): [True: 194, False: 1.03k]
700
194
            countedTxs++;
701
1.22k
    }
702
703
2.22M
    if (firstRecordedHeight == 0 && countedTxs > 0) {
  Branch (703:9): [True: 2.22M, False: 33]
  Branch (703:37): [True: 126, False: 2.22M]
704
126
        firstRecordedHeight = nBestSeenHeight;
705
126
        LogDebug(BCLog::ESTIMATEFEE, "Blockpolicy first recorded height %u\n", firstRecordedHeight);
706
126
    }
707
708
709
2.22M
    LogDebug(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",
710
2.22M
             countedTxs, txs_removed_for_block.size(), trackedTxs, trackedTxs + untrackedTxs, mapMemPoolTxs.size(),
711
2.22M
             MaxUsableEstimate(), HistoricalBlockSpan() > BlockSpan() ? "historical" : "current");
712
713
2.22M
    trackedTxs = 0;
714
2.22M
    untrackedTxs = 0;
715
2.22M
}
716
717
CFeeRate CBlockPolicyEstimator::estimateFee(int confTarget) const
718
0
{
719
    // It's not possible to get reasonable estimates for confTarget of 1
720
0
    if (confTarget <= 1)
  Branch (720:9): [True: 0, False: 0]
721
0
        return CFeeRate(0);
722
723
0
    return estimateRawFee(confTarget, DOUBLE_SUCCESS_PCT, FeeEstimateHorizon::MED_HALFLIFE);
724
0
}
725
726
CFeeRate CBlockPolicyEstimator::estimateRawFee(int confTarget, double successThreshold, FeeEstimateHorizon horizon, EstimationResult* result) const
727
0
{
728
0
    TxConfirmStats* stats = nullptr;
729
0
    double sufficientTxs = SUFFICIENT_FEETXS;
730
0
    switch (horizon) {
  Branch (730:13): [True: 0, False: 0]
731
0
    case FeeEstimateHorizon::SHORT_HALFLIFE: {
  Branch (731:5): [True: 0, False: 0]
732
0
        stats = shortStats.get();
733
0
        sufficientTxs = SUFFICIENT_TXS_SHORT;
734
0
        break;
735
0
    }
736
0
    case FeeEstimateHorizon::MED_HALFLIFE: {
  Branch (736:5): [True: 0, False: 0]
737
0
        stats = feeStats.get();
738
0
        break;
739
0
    }
740
0
    case FeeEstimateHorizon::LONG_HALFLIFE: {
  Branch (740:5): [True: 0, False: 0]
741
0
        stats = longStats.get();
742
0
        break;
743
0
    }
744
0
    } // no default case, so the compiler can warn about missing cases
745
0
    assert(stats);
  Branch (745:5): [True: 0, False: 0]
746
747
0
    LOCK(m_cs_fee_estimator);
748
    // Return failure if trying to analyze a target we're not tracking
749
0
    if (confTarget <= 0 || (unsigned int)confTarget > stats->GetMaxConfirms())
  Branch (749:9): [True: 0, False: 0]
  Branch (749:28): [True: 0, False: 0]
750
0
        return CFeeRate(0);
751
0
    if (successThreshold > 1)
  Branch (751:9): [True: 0, False: 0]
752
0
        return CFeeRate(0);
753
754
0
    double median = stats->EstimateMedianVal(confTarget, sufficientTxs, successThreshold, nBestSeenHeight, result);
755
756
0
    if (median < 0)
  Branch (756:9): [True: 0, False: 0]
757
0
        return CFeeRate(0);
758
759
0
    return CFeeRate(llround(median));
760
0
}
761
762
unsigned int CBlockPolicyEstimator::HighestTargetTracked(FeeEstimateHorizon horizon) const
763
0
{
764
0
    LOCK(m_cs_fee_estimator);
765
0
    switch (horizon) {
  Branch (765:13): [True: 0, False: 0]
766
0
    case FeeEstimateHorizon::SHORT_HALFLIFE: {
  Branch (766:5): [True: 0, False: 0]
767
0
        return shortStats->GetMaxConfirms();
768
0
    }
769
0
    case FeeEstimateHorizon::MED_HALFLIFE: {
  Branch (769:5): [True: 0, False: 0]
770
0
        return feeStats->GetMaxConfirms();
771
0
    }
772
0
    case FeeEstimateHorizon::LONG_HALFLIFE: {
  Branch (772:5): [True: 0, False: 0]
773
0
        return longStats->GetMaxConfirms();
774
0
    }
775
0
    } // no default case, so the compiler can warn about missing cases
776
0
    assert(false);
  Branch (776:5): [Folded - Ignored]
777
0
}
778
779
unsigned int CBlockPolicyEstimator::BlockSpan() const
780
4.45M
{
781
4.45M
    if (firstRecordedHeight == 0) return 0;
  Branch (781:9): [True: 4.45M, False: 444]
782
4.45M
    assert(nBestSeenHeight >= firstRecordedHeight);
  Branch (782:5): [True: 444, False: 0]
783
784
444
    return nBestSeenHeight - firstRecordedHeight;
785
444
}
786
787
unsigned int CBlockPolicyEstimator::HistoricalBlockSpan() const
788
4.45M
{
789
4.45M
    if (historicalFirst == 0) return 0;
  Branch (789:9): [True: 4.45M, False: 0]
790
4.45M
    assert(historicalBest >= historicalFirst);
  Branch (790:5): [True: 0, False: 0]
791
792
0
    if (nBestSeenHeight - historicalBest > OLDEST_ESTIMATE_HISTORY) return 0;
  Branch (792:9): [True: 0, False: 0]
793
794
0
    return historicalBest - historicalFirst;
795
0
}
796
797
unsigned int CBlockPolicyEstimator::MaxUsableEstimate() const
798
2.22M
{
799
    // Block spans are divided by 2 to make sure there are enough potential failing data points for the estimate
800
2.22M
    return std::min(longStats->GetMaxConfirms(), std::max(BlockSpan(), HistoricalBlockSpan()) / 2);
801
2.22M
}
802
803
/** Return a fee estimate at the required successThreshold from the shortest
804
 * time horizon which tracks confirmations up to the desired target.  If
805
 * checkShorterHorizon is requested, also allow short time horizon estimates
806
 * for a lower target to reduce the given answer */
807
double CBlockPolicyEstimator::estimateCombinedFee(unsigned int confTarget, double successThreshold, bool checkShorterHorizon, EstimationResult *result) const
808
0
{
809
0
    double estimate = -1;
810
0
    if (confTarget >= 1 && confTarget <= longStats->GetMaxConfirms()) {
  Branch (810:9): [True: 0, False: 0]
  Branch (810:28): [True: 0, False: 0]
811
        // Find estimate from shortest time horizon possible
812
0
        if (confTarget <= shortStats->GetMaxConfirms()) { // short horizon
  Branch (812:13): [True: 0, False: 0]
813
0
            estimate = shortStats->EstimateMedianVal(confTarget, SUFFICIENT_TXS_SHORT, successThreshold, nBestSeenHeight, result);
814
0
        }
815
0
        else if (confTarget <= feeStats->GetMaxConfirms()) { // medium horizon
  Branch (815:18): [True: 0, False: 0]
816
0
            estimate = feeStats->EstimateMedianVal(confTarget, SUFFICIENT_FEETXS, successThreshold, nBestSeenHeight, result);
817
0
        }
818
0
        else { // long horizon
819
0
            estimate = longStats->EstimateMedianVal(confTarget, SUFFICIENT_FEETXS, successThreshold, nBestSeenHeight, result);
820
0
        }
821
0
        if (checkShorterHorizon) {
  Branch (821:13): [True: 0, False: 0]
822
0
            EstimationResult tempResult;
823
            // If a lower confTarget from a more recent horizon returns a lower answer use it.
824
0
            if (confTarget > feeStats->GetMaxConfirms()) {
  Branch (824:17): [True: 0, False: 0]
825
0
                double medMax = feeStats->EstimateMedianVal(feeStats->GetMaxConfirms(), SUFFICIENT_FEETXS, successThreshold, nBestSeenHeight, &tempResult);
826
0
                if (medMax > 0 && (estimate == -1 || medMax < estimate)) {
  Branch (826:21): [True: 0, False: 0]
  Branch (826:36): [True: 0, False: 0]
  Branch (826:54): [True: 0, False: 0]
827
0
                    estimate = medMax;
828
0
                    if (result) *result = tempResult;
  Branch (828:25): [True: 0, False: 0]
829
0
                }
830
0
            }
831
0
            if (confTarget > shortStats->GetMaxConfirms()) {
  Branch (831:17): [True: 0, False: 0]
832
0
                double shortMax = shortStats->EstimateMedianVal(shortStats->GetMaxConfirms(), SUFFICIENT_TXS_SHORT, successThreshold, nBestSeenHeight, &tempResult);
833
0
                if (shortMax > 0 && (estimate == -1 || shortMax < estimate)) {
  Branch (833:21): [True: 0, False: 0]
  Branch (833:38): [True: 0, False: 0]
  Branch (833:56): [True: 0, False: 0]
834
0
                    estimate = shortMax;
835
0
                    if (result) *result = tempResult;
  Branch (835:25): [True: 0, False: 0]
836
0
                }
837
0
            }
838
0
        }
839
0
    }
840
0
    return estimate;
841
0
}
842
843
/** Ensure that for a conservative estimate, the DOUBLE_SUCCESS_PCT is also met
844
 * at 2 * target for any longer time horizons.
845
 */
846
double CBlockPolicyEstimator::estimateConservativeFee(unsigned int doubleTarget, EstimationResult *result) const
847
0
{
848
0
    double estimate = -1;
849
0
    EstimationResult tempResult;
850
0
    if (doubleTarget <= shortStats->GetMaxConfirms()) {
  Branch (850:9): [True: 0, False: 0]
851
0
        estimate = feeStats->EstimateMedianVal(doubleTarget, SUFFICIENT_FEETXS, DOUBLE_SUCCESS_PCT, nBestSeenHeight, result);
852
0
    }
853
0
    if (doubleTarget <= feeStats->GetMaxConfirms()) {
  Branch (853:9): [True: 0, False: 0]
854
0
        double longEstimate = longStats->EstimateMedianVal(doubleTarget, SUFFICIENT_FEETXS, DOUBLE_SUCCESS_PCT, nBestSeenHeight, &tempResult);
855
0
        if (longEstimate > estimate) {
  Branch (855:13): [True: 0, False: 0]
856
0
            estimate = longEstimate;
857
0
            if (result) *result = tempResult;
  Branch (857:17): [True: 0, False: 0]
858
0
        }
859
0
    }
860
0
    return estimate;
861
0
}
862
863
/** estimateSmartFee returns the max of the feerates calculated with a 60%
864
 * threshold required at target / 2, an 85% threshold required at target and a
865
 * 95% threshold required at 2 * target.  Each calculation is performed at the
866
 * shortest time horizon which tracks the required target.  Conservative
867
 * estimates, however, required the 95% threshold at 2 * target be met for any
868
 * longer time horizons also.
869
 */
870
CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, FeeCalculation *feeCalc, bool conservative) const
871
0
{
872
0
    LOCK(m_cs_fee_estimator);
873
874
0
    if (feeCalc) {
  Branch (874:9): [True: 0, False: 0]
875
0
        feeCalc->desiredTarget = confTarget;
876
0
        feeCalc->returnedTarget = confTarget;
877
0
    }
878
879
0
    double median = -1;
880
0
    EstimationResult tempResult;
881
882
    // Return failure if trying to analyze a target we're not tracking
883
0
    if (confTarget <= 0 || (unsigned int)confTarget > longStats->GetMaxConfirms()) {
  Branch (883:9): [True: 0, False: 0]
  Branch (883:28): [True: 0, False: 0]
884
0
        return CFeeRate(0);  // error condition
885
0
    }
886
887
    // It's not possible to get reasonable estimates for confTarget of 1
888
0
    if (confTarget == 1) confTarget = 2;
  Branch (888:9): [True: 0, False: 0]
889
890
0
    unsigned int maxUsableEstimate = MaxUsableEstimate();
891
0
    if ((unsigned int)confTarget > maxUsableEstimate) {
  Branch (891:9): [True: 0, False: 0]
892
0
        confTarget = maxUsableEstimate;
893
0
    }
894
0
    if (feeCalc) feeCalc->returnedTarget = confTarget;
  Branch (894:9): [True: 0, False: 0]
895
896
0
    if (confTarget <= 1) return CFeeRate(0); // error condition
  Branch (896:9): [True: 0, False: 0]
897
898
0
    assert(confTarget > 0); //estimateCombinedFee and estimateConservativeFee take unsigned ints
  Branch (898:5): [True: 0, False: 0]
899
    /** true is passed to estimateCombined fee for target/2 and target so
900
     * that we check the max confirms for shorter time horizons as well.
901
     * This is necessary to preserve monotonically increasing estimates.
902
     * For non-conservative estimates we do the same thing for 2*target, but
903
     * for conservative estimates we want to skip these shorter horizons
904
     * checks for 2*target because we are taking the max over all time
905
     * horizons so we already have monotonically increasing estimates and
906
     * the purpose of conservative estimates is not to let short term
907
     * fluctuations lower our estimates by too much.
908
     *
909
     * Note: In certain rare edge cases, monotonically increasing estimates may
910
     * not be guaranteed. Specifically, given two targets N and M, where M > N,
911
     * if a sub-estimate for target N fails to return a valid fee rate, while
912
     * target M has valid fee rate for that sub-estimate, target M may result
913
     * in a higher fee rate estimate than target N.
914
     *
915
     * See: https://github.com/bitcoin/bitcoin/issues/11800#issuecomment-349697807
916
     */
917
0
    double halfEst = estimateCombinedFee(confTarget/2, HALF_SUCCESS_PCT, true, &tempResult);
918
0
    if (feeCalc) {
  Branch (918:9): [True: 0, False: 0]
919
0
        feeCalc->est = tempResult;
920
0
        feeCalc->reason = FeeReason::HALF_ESTIMATE;
921
0
    }
922
0
    median = halfEst;
923
0
    double actualEst = estimateCombinedFee(confTarget, SUCCESS_PCT, true, &tempResult);
924
0
    if (actualEst > median) {
  Branch (924:9): [True: 0, False: 0]
925
0
        median = actualEst;
926
0
        if (feeCalc) {
  Branch (926:13): [True: 0, False: 0]
927
0
            feeCalc->est = tempResult;
928
0
            feeCalc->reason = FeeReason::FULL_ESTIMATE;
929
0
        }
930
0
    }
931
0
    double doubleEst = estimateCombinedFee(2 * confTarget, DOUBLE_SUCCESS_PCT, !conservative, &tempResult);
932
0
    if (doubleEst > median) {
  Branch (932:9): [True: 0, False: 0]
933
0
        median = doubleEst;
934
0
        if (feeCalc) {
  Branch (934:13): [True: 0, False: 0]
935
0
            feeCalc->est = tempResult;
936
0
            feeCalc->reason = FeeReason::DOUBLE_ESTIMATE;
937
0
        }
938
0
    }
939
940
0
    if (conservative || median == -1) {
  Branch (940:9): [True: 0, False: 0]
  Branch (940:25): [True: 0, False: 0]
941
0
        double consEst =  estimateConservativeFee(2 * confTarget, &tempResult);
942
0
        if (consEst > median) {
  Branch (942:13): [True: 0, False: 0]
943
0
            median = consEst;
944
0
            if (feeCalc) {
  Branch (944:17): [True: 0, False: 0]
945
0
                feeCalc->est = tempResult;
946
0
                feeCalc->reason = FeeReason::CONSERVATIVE;
947
0
            }
948
0
        }
949
0
    }
950
951
0
    if (median < 0) return CFeeRate(0); // error condition
  Branch (951:9): [True: 0, False: 0]
952
953
0
    return CFeeRate(llround(median));
954
0
}
955
956
11.0k
void CBlockPolicyEstimator::Flush() {
957
11.0k
    FlushUnconfirmed();
958
11.0k
    FlushFeeEstimates();
959
11.0k
}
960
961
void CBlockPolicyEstimator::FlushFeeEstimates()
962
11.0k
{
963
11.0k
    AutoFile est_file{fsbridge::fopen(m_estimation_filepath, "wb")};
964
11.0k
    if (est_file.IsNull() || !Write(est_file)) {
  Branch (964:9): [True: 0, False: 11.0k]
  Branch (964:30): [True: 0, False: 11.0k]
965
0
        LogPrintf("Failed to write fee estimates to %s. Continue anyway.\n", fs::PathToString(m_estimation_filepath));
966
11.0k
    } else {
967
11.0k
        LogPrintf("Flushed fee estimates to %s.\n", fs::PathToString(m_estimation_filepath.filename()));
968
11.0k
    }
969
11.0k
}
970
971
bool CBlockPolicyEstimator::Write(AutoFile& fileout) const
972
11.0k
{
973
11.0k
    try {
974
11.0k
        LOCK(m_cs_fee_estimator);
975
11.0k
        fileout << CURRENT_FEES_FILE_VERSION;
976
11.0k
        fileout << int{0}; // Unused dummy field. Written files may contain any value in [0, 289900]
977
11.0k
        fileout << nBestSeenHeight;
978
11.0k
        if (BlockSpan() > HistoricalBlockSpan()/2) {
  Branch (978:13): [True: 26, False: 11.0k]
979
26
            fileout << firstRecordedHeight << nBestSeenHeight;
980
26
        }
981
11.0k
        else {
982
11.0k
            fileout << historicalFirst << historicalBest;
983
11.0k
        }
984
11.0k
        fileout << Using<VectorFormatter<EncodedDoubleFormatter>>(buckets);
985
11.0k
        feeStats->Write(fileout);
986
11.0k
        shortStats->Write(fileout);
987
11.0k
        longStats->Write(fileout);
988
11.0k
    }
989
11.0k
    catch (const std::exception&) {
990
0
        LogWarning("Unable to write policy estimator data (non-fatal)");
991
0
        return false;
992
0
    }
993
11.0k
    return true;
994
11.0k
}
995
996
bool CBlockPolicyEstimator::Read(AutoFile& filein)
997
0
{
998
0
    try {
999
0
        LOCK(m_cs_fee_estimator);
1000
0
        int nVersionRequired, dummy;
1001
0
        filein >> nVersionRequired >> dummy;
1002
0
        if (nVersionRequired > CURRENT_FEES_FILE_VERSION) {
  Branch (1002:13): [True: 0, False: 0]
1003
0
            throw std::runtime_error{strprintf("File version (%d) too high to be read.", nVersionRequired)};
1004
0
        }
1005
1006
        // Read fee estimates file into temporary variables so existing data
1007
        // structures aren't corrupted if there is an exception.
1008
0
        unsigned int nFileBestSeenHeight;
1009
0
        filein >> nFileBestSeenHeight;
1010
1011
0
        if (nVersionRequired < CURRENT_FEES_FILE_VERSION) {
  Branch (1011:13): [True: 0, False: 0]
1012
0
            LogWarning("Incompatible old fee estimation data (non-fatal). Version: %d", nVersionRequired);
1013
0
        } else { // nVersionRequired == CURRENT_FEES_FILE_VERSION
1014
0
            unsigned int nFileHistoricalFirst, nFileHistoricalBest;
1015
0
            filein >> nFileHistoricalFirst >> nFileHistoricalBest;
1016
0
            if (nFileHistoricalFirst > nFileHistoricalBest || nFileHistoricalBest > nFileBestSeenHeight) {
  Branch (1016:17): [True: 0, False: 0]
  Branch (1016:63): [True: 0, False: 0]
1017
0
                throw std::runtime_error("Corrupt estimates file. Historical block range for estimates is invalid");
1018
0
            }
1019
0
            std::vector<double> fileBuckets;
1020
0
            filein >> Using<VectorFormatter<EncodedDoubleFormatter>>(fileBuckets);
1021
0
            size_t numBuckets = fileBuckets.size();
1022
0
            if (numBuckets <= 1 || numBuckets > 1000) {
  Branch (1022:17): [True: 0, False: 0]
  Branch (1022:36): [True: 0, False: 0]
1023
0
                throw std::runtime_error("Corrupt estimates file. Must have between 2 and 1000 feerate buckets");
1024
0
            }
1025
1026
0
            std::unique_ptr<TxConfirmStats> fileFeeStats(new TxConfirmStats(buckets, bucketMap, MED_BLOCK_PERIODS, MED_DECAY, MED_SCALE));
1027
0
            std::unique_ptr<TxConfirmStats> fileShortStats(new TxConfirmStats(buckets, bucketMap, SHORT_BLOCK_PERIODS, SHORT_DECAY, SHORT_SCALE));
1028
0
            std::unique_ptr<TxConfirmStats> fileLongStats(new TxConfirmStats(buckets, bucketMap, LONG_BLOCK_PERIODS, LONG_DECAY, LONG_SCALE));
1029
0
            fileFeeStats->Read(filein, numBuckets);
1030
0
            fileShortStats->Read(filein, numBuckets);
1031
0
            fileLongStats->Read(filein, numBuckets);
1032
1033
            // Fee estimates file parsed correctly
1034
            // Copy buckets from file and refresh our bucketmap
1035
0
            buckets = fileBuckets;
1036
0
            bucketMap.clear();
1037
0
            for (unsigned int i = 0; i < buckets.size(); i++) {
  Branch (1037:38): [True: 0, False: 0]
1038
0
                bucketMap[buckets[i]] = i;
1039
0
            }
1040
1041
            // Destroy old TxConfirmStats and point to new ones that already reference buckets and bucketMap
1042
0
            feeStats = std::move(fileFeeStats);
1043
0
            shortStats = std::move(fileShortStats);
1044
0
            longStats = std::move(fileLongStats);
1045
1046
0
            nBestSeenHeight = nFileBestSeenHeight;
1047
0
            historicalFirst = nFileHistoricalFirst;
1048
0
            historicalBest = nFileHistoricalBest;
1049
0
        }
1050
0
    }
1051
0
    catch (const std::exception& e) {
1052
0
        LogWarning("Unable to read policy estimator data (non-fatal): %s", e.what());
1053
0
        return false;
1054
0
    }
1055
0
    return true;
1056
0
}
1057
1058
void CBlockPolicyEstimator::FlushUnconfirmed()
1059
11.0k
{
1060
11.0k
    const auto startclear{SteadyClock::now()};
1061
11.0k
    LOCK(m_cs_fee_estimator);
1062
11.0k
    size_t num_entries = mapMemPoolTxs.size();
1063
    // Remove every entry in mapMemPoolTxs
1064
16.8k
    while (!mapMemPoolTxs.empty()) {
  Branch (1064:12): [True: 5.75k, False: 11.0k]
1065
5.75k
        auto mi = mapMemPoolTxs.begin();
1066
5.75k
        _removeTx(mi->first, false); // this calls erase() on mapMemPoolTxs
1067
5.75k
    }
1068
11.0k
    const auto endclear{SteadyClock::now()};
1069
11.0k
    LogDebug(BCLog::ESTIMATEFEE, "Recorded %u unconfirmed txs from mempool in %.3fs\n", num_entries, Ticks<SecondsDouble>(endclear - startclear));
1070
11.0k
}
1071
1072
std::chrono::hours CBlockPolicyEstimator::GetFeeEstimatorFileAge()
1073
0
{
1074
0
    auto file_time{fs::last_write_time(m_estimation_filepath)};
1075
0
    auto now{fs::file_time_type::clock::now()};
1076
0
    return std::chrono::duration_cast<std::chrono::hours>(now - file_time);
1077
0
}
1078
1079
static std::set<double> MakeFeeSet(const CFeeRate& min_incremental_fee,
1080
                                   double max_filter_fee_rate,
1081
                                   double fee_filter_spacing)
1082
11.0k
{
1083
11.0k
    std::set<double> fee_set;
1084
1085
11.0k
    const CAmount min_fee_limit{std::max(CAmount(1), min_incremental_fee.GetFeePerK() / 2)};
1086
11.0k
    fee_set.insert(0);
1087
11.0k
    for (double bucket_boundary = min_fee_limit;
1088
1.16M
         bucket_boundary <= max_filter_fee_rate;
  Branch (1088:10): [True: 1.15M, False: 11.0k]
1089
1.15M
         bucket_boundary *= fee_filter_spacing) {
1090
1091
1.15M
        fee_set.insert(bucket_boundary);
1092
1.15M
    }
1093
1094
11.0k
    return fee_set;
1095
11.0k
}
1096
1097
FeeFilterRounder::FeeFilterRounder(const CFeeRate& minIncrementalFee, FastRandomContext& rng)
1098
11.0k
    : m_fee_set{MakeFeeSet(minIncrementalFee, MAX_FILTER_FEERATE, FEE_FILTER_SPACING)},
1099
11.0k
      insecure_rand{rng}
1100
11.0k
{
1101
11.0k
}
1102
1103
CAmount FeeFilterRounder::round(CAmount currentMinFee)
1104
253k
{
1105
253k
    AssertLockNotHeld(m_insecure_rand_mutex);
1106
253k
    std::set<double>::iterator it = m_fee_set.lower_bound(currentMinFee);
1107
253k
    if (it == m_fee_set.end() ||
  Branch (1107:9): [True: 11.0k, False: 242k]
  Branch (1107:9): [True: 11.0k, False: 242k]
1108
253k
        (it != m_fee_set.begin() &&
  Branch (1108:10): [True: 0, False: 242k]
1109
242k
         WITH_LOCK(m_insecure_rand_mutex, return insecure_rand.rand32()) % 3 != 0)) {
  Branch (1109:10): [True: 0, False: 0]
1110
11.0k
        --it;
1111
11.0k
    }
1112
253k
    return static_cast<CAmount>(*it);
1113
253k
}