Bitcoin Core 22.99.0
P2P Digital Currency
policyestimator_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2020 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <policy/fees.h>
6#include <policy/policy.h>
7#include <txmempool.h>
8#include <uint256.h>
9#include <util/time.h>
10
12
13#include <boost/test/unit_test.hpp>
14
15BOOST_FIXTURE_TEST_SUITE(policyestimator_tests, BasicTestingSetup)
16
17BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
18{
20 CTxMemPool mpool(&feeEst);
21 LOCK2(cs_main, mpool.cs);
23 CAmount basefee(2000);
24 CAmount deltaFee(100);
25 std::vector<CAmount> feeV;
26
27 // Populate vectors of increasing fees
28 for (int j = 0; j < 10; j++) {
29 feeV.push_back(basefee * (j+1));
30 }
31
32 // Store the hashes of transactions that have been
33 // added to the mempool by their associate fee
34 // txHashes[j] is populated with transactions either of
35 // fee = basefee * (j+1)
36 std::vector<uint256> txHashes[10];
37
38 // Create a transaction template
39 CScript garbage;
40 for (unsigned int i = 0; i < 128; i++)
41 garbage.push_back('X');
43 tx.vin.resize(1);
44 tx.vin[0].scriptSig = garbage;
45 tx.vout.resize(1);
46 tx.vout[0].nValue=0LL;
47 CFeeRate baseRate(basefee, GetVirtualTransactionSize(CTransaction(tx)));
48
49 // Create a fake block
50 std::vector<CTransactionRef> block;
51 int blocknum = 0;
52
53 // Loop through 200 blocks
54 // At a decay .9952 and 4 fee transactions per block
55 // This makes the tx count about 2.5 per bucket, well above the 0.1 threshold
56 while (blocknum < 200) {
57 for (int j = 0; j < 10; j++) { // For each fee
58 for (int k = 0; k < 4; k++) { // add 4 fee txs
59 tx.vin[0].prevout.n = 10000*blocknum+100*j+k; // make transaction unique
60 uint256 hash = tx.GetHash();
61 mpool.addUnchecked(entry.Fee(feeV[j]).Time(GetTime()).Height(blocknum).FromTx(tx));
62 txHashes[j].push_back(hash);
63 }
64 }
65 //Create blocks where higher fee txs are included more often
66 for (int h = 0; h <= blocknum%10; h++) {
67 // 10/10 blocks add highest fee transactions
68 // 9/10 blocks add 2nd highest and so on until ...
69 // 1/10 blocks add lowest fee transactions
70 while (txHashes[9-h].size()) {
71 CTransactionRef ptx = mpool.get(txHashes[9-h].back());
72 if (ptx)
73 block.push_back(ptx);
74 txHashes[9-h].pop_back();
75 }
76 }
77 mpool.removeForBlock(block, ++blocknum);
78 block.clear();
79 // Check after just a few txs that combining buckets works as expected
80 if (blocknum == 3) {
81 // At this point we should need to combine 3 buckets to get enough data points
82 // So estimateFee(1) should fail and estimateFee(2) should return somewhere around
83 // 9*baserate. estimateFee(2) %'s are 100,100,90 = average 97%
84 BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
85 BOOST_CHECK(feeEst.estimateFee(2).GetFeePerK() < 9*baseRate.GetFeePerK() + deltaFee);
86 BOOST_CHECK(feeEst.estimateFee(2).GetFeePerK() > 9*baseRate.GetFeePerK() - deltaFee);
87 }
88 }
89
90 std::vector<CAmount> origFeeEst;
91 // Highest feerate is 10*baseRate and gets in all blocks,
92 // second highest feerate is 9*baseRate and gets in 9/10 blocks = 90%,
93 // third highest feerate is 8*base rate, and gets in 8/10 blocks = 80%,
94 // so estimateFee(1) would return 10*baseRate but is hardcoded to return failure
95 // Second highest feerate has 100% chance of being included by 2 blocks,
96 // so estimateFee(2) should return 9*baseRate etc...
97 for (int i = 1; i < 10;i++) {
98 origFeeEst.push_back(feeEst.estimateFee(i).GetFeePerK());
99 if (i > 2) { // Fee estimates should be monotonically decreasing
100 BOOST_CHECK(origFeeEst[i-1] <= origFeeEst[i-2]);
101 }
102 int mult = 11-i;
103 if (i % 2 == 0) { //At scale 2, test logic is only correct for even targets
104 BOOST_CHECK(origFeeEst[i-1] < mult*baseRate.GetFeePerK() + deltaFee);
105 BOOST_CHECK(origFeeEst[i-1] > mult*baseRate.GetFeePerK() - deltaFee);
106 }
107 }
108 // Fill out rest of the original estimates
109 for (int i = 10; i <= 48; i++) {
110 origFeeEst.push_back(feeEst.estimateFee(i).GetFeePerK());
111 }
112
113 // Mine 50 more blocks with no transactions happening, estimates shouldn't change
114 // We haven't decayed the moving average enough so we still have enough data points in every bucket
115 while (blocknum < 250)
116 mpool.removeForBlock(block, ++blocknum);
117
118 BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
119 for (int i = 2; i < 10;i++) {
120 BOOST_CHECK(feeEst.estimateFee(i).GetFeePerK() < origFeeEst[i-1] + deltaFee);
121 BOOST_CHECK(feeEst.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
122 }
123
124
125 // Mine 15 more blocks with lots of transactions happening and not getting mined
126 // Estimates should go up
127 while (blocknum < 265) {
128 for (int j = 0; j < 10; j++) { // For each fee multiple
129 for (int k = 0; k < 4; k++) { // add 4 fee txs
130 tx.vin[0].prevout.n = 10000*blocknum+100*j+k;
131 uint256 hash = tx.GetHash();
132 mpool.addUnchecked(entry.Fee(feeV[j]).Time(GetTime()).Height(blocknum).FromTx(tx));
133 txHashes[j].push_back(hash);
134 }
135 }
136 mpool.removeForBlock(block, ++blocknum);
137 }
138
139 for (int i = 1; i < 10;i++) {
140 BOOST_CHECK(feeEst.estimateFee(i) == CFeeRate(0) || feeEst.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
141 }
142
143 // Mine all those transactions
144 // Estimates should still not be below original
145 for (int j = 0; j < 10; j++) {
146 while(txHashes[j].size()) {
147 CTransactionRef ptx = mpool.get(txHashes[j].back());
148 if (ptx)
149 block.push_back(ptx);
150 txHashes[j].pop_back();
151 }
152 }
153 mpool.removeForBlock(block, 266);
154 block.clear();
155 BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
156 for (int i = 2; i < 10;i++) {
157 BOOST_CHECK(feeEst.estimateFee(i) == CFeeRate(0) || feeEst.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
158 }
159
160 // Mine 400 more blocks where everything is mined every block
161 // Estimates should be below original estimates
162 while (blocknum < 665) {
163 for (int j = 0; j < 10; j++) { // For each fee multiple
164 for (int k = 0; k < 4; k++) { // add 4 fee txs
165 tx.vin[0].prevout.n = 10000*blocknum+100*j+k;
166 uint256 hash = tx.GetHash();
167 mpool.addUnchecked(entry.Fee(feeV[j]).Time(GetTime()).Height(blocknum).FromTx(tx));
168 CTransactionRef ptx = mpool.get(hash);
169 if (ptx)
170 block.push_back(ptx);
171
172 }
173 }
174 mpool.removeForBlock(block, ++blocknum);
175 block.clear();
176 }
177 BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
178 for (int i = 2; i < 9; i++) { // At 9, the original estimate was already at the bottom (b/c scale = 2)
179 BOOST_CHECK(feeEst.estimateFee(i).GetFeePerK() < origFeeEst[i-1] - deltaFee);
180 }
181}
182
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:118
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
Definition: fees.h:132
CFeeRate estimateFee(int confTarget) const
DEPRECATED.
Definition: fees.cpp:653
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:30
CAmount GetFeePerK() const
Return the fee in satoshis for a size of 1000 bytes.
Definition: feerate.h:57
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:260
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:424
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:511
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void addUnchecked(const CTxMemPoolEntry &entry, bool validFeeEstimate=true) EXCLUSIVE_LOCKS_REQUIRED(cs
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
Definition: txmempool.h:582
CTransactionRef get(const uint256 &hash) const
Definition: txmempool.cpp:911
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
Definition: txmempool.cpp:679
void push_back(const T &value)
Definition: prevector.h:437
256-bit opaque blob.
Definition: uint256.h:124
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK(expr)
Definition: object.cpp:17
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Compute the virtual transaction size (weight reinterpreted as bytes).
Definition: policy.cpp:285
BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
Basic testing setup.
Definition: setup_common.h:76
A mutable version of CTransaction.
Definition: transaction.h:345
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:63
std::vector< CTxOut > vout
Definition: transaction.h:347
std::vector< CTxIn > vin
Definition: transaction.h:346
Definition: setup_common.h:183
TestMemPoolEntryHelper & Time(int64_t _time)
Definition: setup_common.h:201
TestMemPoolEntryHelper & Fee(CAmount _fee)
Definition: setup_common.h:200
#define LOCK2(cs1, cs2)
Definition: sync.h:227
int64_t GetTime()
DEPRECATED Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable)
Definition: time.cpp:26