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