2016-12-31 19:01:21 +01:00
|
|
|
// Copyright (c) 2011-2016 The Bitcoin Core developers
|
2014-08-26 22:28:32 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2016-01-03 18:54:50 +01:00
|
|
|
#include "policy/policy.h"
|
2014-08-26 22:28:32 +02:00
|
|
|
#include "policy/fees.h"
|
|
|
|
#include "txmempool.h"
|
|
|
|
#include "uint256.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#include "test/test_bitcoin.h"
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(policyestimator_tests, BasicTestingSetup)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
|
|
|
|
{
|
2017-02-14 22:54:46 +01:00
|
|
|
CBlockPolicyEstimator feeEst;
|
|
|
|
CTxMemPool mpool(&feeEst);
|
2015-11-14 23:04:15 +01:00
|
|
|
TestMemPoolEntryHelper entry;
|
2014-08-26 22:28:32 +02:00
|
|
|
CAmount basefee(2000);
|
|
|
|
CAmount deltaFee(100);
|
2016-03-21 18:04:40 +01:00
|
|
|
std::vector<CAmount> feeV;
|
2014-08-26 22:28:32 +02:00
|
|
|
|
2016-03-21 18:04:40 +01:00
|
|
|
// Populate vectors of increasing fees
|
2014-08-26 22:28:32 +02:00
|
|
|
for (int j = 0; j < 10; j++) {
|
2016-03-21 18:04:40 +01:00
|
|
|
feeV.push_back(basefee * (j+1));
|
2014-08-26 22:28:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store the hashes of transactions that have been
|
2016-03-21 18:04:40 +01:00
|
|
|
// added to the mempool by their associate fee
|
2014-08-26 22:28:32 +02:00
|
|
|
// txHashes[j] is populated with transactions either of
|
2016-03-21 18:04:40 +01:00
|
|
|
// fee = basefee * (j+1)
|
2014-08-26 22:28:32 +02:00
|
|
|
std::vector<uint256> txHashes[10];
|
|
|
|
|
|
|
|
// Create a transaction template
|
|
|
|
CScript garbage;
|
|
|
|
for (unsigned int i = 0; i < 128; i++)
|
|
|
|
garbage.push_back('X');
|
|
|
|
CMutableTransaction tx;
|
|
|
|
tx.vin.resize(1);
|
|
|
|
tx.vin[0].scriptSig = garbage;
|
|
|
|
tx.vout.resize(1);
|
|
|
|
tx.vout[0].nValue=0LL;
|
2016-01-03 18:54:50 +01:00
|
|
|
CFeeRate baseRate(basefee, GetVirtualTransactionSize(tx));
|
2014-08-26 22:28:32 +02:00
|
|
|
|
|
|
|
// Create a fake block
|
2016-11-11 02:34:17 +01:00
|
|
|
std::vector<CTransactionRef> block;
|
2014-08-26 22:28:32 +02:00
|
|
|
int blocknum = 0;
|
|
|
|
|
|
|
|
// Loop through 200 blocks
|
2017-03-09 20:02:00 +01:00
|
|
|
// At a decay .9952 and 4 fee transactions per block
|
|
|
|
// This makes the tx count about 2.5 per bucket, well above the 0.1 threshold
|
2014-08-26 22:28:32 +02:00
|
|
|
while (blocknum < 200) {
|
2016-03-21 18:04:40 +01:00
|
|
|
for (int j = 0; j < 10; j++) { // For each fee
|
|
|
|
for (int k = 0; k < 4; k++) { // add 4 fee txs
|
2014-08-26 22:28:32 +02:00
|
|
|
tx.vin[0].prevout.n = 10000*blocknum+100*j+k; // make transaction unique
|
|
|
|
uint256 hash = tx.GetHash();
|
2017-01-20 04:46:50 +01:00
|
|
|
mpool.addUnchecked(hash, entry.Fee(feeV[j]).Time(GetTime()).Height(blocknum).FromTx(tx));
|
2014-08-26 22:28:32 +02:00
|
|
|
txHashes[j].push_back(hash);
|
|
|
|
}
|
|
|
|
}
|
2016-03-21 18:04:40 +01:00
|
|
|
//Create blocks where higher fee txs are included more often
|
2014-08-26 22:28:32 +02:00
|
|
|
for (int h = 0; h <= blocknum%10; h++) {
|
2016-03-21 18:04:40 +01:00
|
|
|
// 10/10 blocks add highest fee transactions
|
2014-08-26 22:28:32 +02:00
|
|
|
// 9/10 blocks add 2nd highest and so on until ...
|
2016-03-21 18:04:40 +01:00
|
|
|
// 1/10 blocks add lowest fee transactions
|
2014-08-26 22:28:32 +02:00
|
|
|
while (txHashes[9-h].size()) {
|
2016-11-11 02:34:17 +01:00
|
|
|
CTransactionRef ptx = mpool.get(txHashes[9-h].back());
|
2016-06-07 13:44:56 +02:00
|
|
|
if (ptx)
|
2016-11-11 02:26:00 +01:00
|
|
|
block.push_back(ptx);
|
2014-08-26 22:28:32 +02:00
|
|
|
txHashes[9-h].pop_back();
|
|
|
|
}
|
|
|
|
}
|
2016-08-15 13:10:57 +02:00
|
|
|
mpool.removeForBlock(block, ++blocknum);
|
2014-08-26 22:28:32 +02:00
|
|
|
block.clear();
|
2017-03-09 20:02:00 +01:00
|
|
|
// Check after just a few txs that combining buckets works as expected
|
|
|
|
if (blocknum == 3) {
|
|
|
|
// At this point we should need to combine 3 buckets to get enough data points
|
|
|
|
// So estimateFee(1) should fail and estimateFee(2) should return somewhere around
|
|
|
|
// 9*baserate. estimateFee(2) %'s are 100,100,90 = average 97%
|
2017-02-15 21:23:34 +01:00
|
|
|
BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
|
2017-03-09 20:02:00 +01:00
|
|
|
BOOST_CHECK(feeEst.estimateFee(2).GetFeePerK() < 9*baseRate.GetFeePerK() + deltaFee);
|
|
|
|
BOOST_CHECK(feeEst.estimateFee(2).GetFeePerK() > 9*baseRate.GetFeePerK() - deltaFee);
|
2014-08-26 22:28:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CAmount> origFeeEst;
|
|
|
|
// Highest feerate is 10*baseRate and gets in all blocks,
|
|
|
|
// second highest feerate is 9*baseRate and gets in 9/10 blocks = 90%,
|
|
|
|
// third highest feerate is 8*base rate, and gets in 8/10 blocks = 80%,
|
2016-11-29 18:18:44 +01:00
|
|
|
// so estimateFee(1) would return 10*baseRate but is hardcoded to return failure
|
2015-11-16 21:18:15 +01:00
|
|
|
// Second highest feerate has 100% chance of being included by 2 blocks,
|
|
|
|
// so estimateFee(2) should return 9*baseRate etc...
|
2014-08-26 22:28:32 +02:00
|
|
|
for (int i = 1; i < 10;i++) {
|
2017-02-15 21:23:34 +01:00
|
|
|
origFeeEst.push_back(feeEst.estimateFee(i).GetFeePerK());
|
2016-11-29 18:18:44 +01:00
|
|
|
if (i > 2) { // Fee estimates should be monotonically decreasing
|
2014-08-26 22:28:32 +02:00
|
|
|
BOOST_CHECK(origFeeEst[i-1] <= origFeeEst[i-2]);
|
|
|
|
}
|
2015-11-16 21:18:15 +01:00
|
|
|
int mult = 11-i;
|
2017-04-03 17:31:27 +02:00
|
|
|
if (i % 2 == 0) { //At scale 2, test logic is only correct for even targets
|
2016-11-29 18:18:44 +01:00
|
|
|
BOOST_CHECK(origFeeEst[i-1] < mult*baseRate.GetFeePerK() + deltaFee);
|
|
|
|
BOOST_CHECK(origFeeEst[i-1] > mult*baseRate.GetFeePerK() - deltaFee);
|
|
|
|
}
|
2014-08-26 22:28:32 +02:00
|
|
|
}
|
2017-03-09 20:02:00 +01:00
|
|
|
// Fill out rest of the original estimates
|
|
|
|
for (int i = 10; i <= 48; i++) {
|
|
|
|
origFeeEst.push_back(feeEst.estimateFee(i).GetFeePerK());
|
|
|
|
}
|
2014-08-26 22:28:32 +02:00
|
|
|
|
|
|
|
// Mine 50 more blocks with no transactions happening, estimates shouldn't change
|
|
|
|
// We haven't decayed the moving average enough so we still have enough data points in every bucket
|
|
|
|
while (blocknum < 250)
|
2016-08-15 13:10:57 +02:00
|
|
|
mpool.removeForBlock(block, ++blocknum);
|
2014-08-26 22:28:32 +02:00
|
|
|
|
2017-02-15 21:23:34 +01:00
|
|
|
BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
|
2016-11-29 18:18:44 +01:00
|
|
|
for (int i = 2; i < 10;i++) {
|
2017-02-15 21:23:34 +01:00
|
|
|
BOOST_CHECK(feeEst.estimateFee(i).GetFeePerK() < origFeeEst[i-1] + deltaFee);
|
|
|
|
BOOST_CHECK(feeEst.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
|
2014-08-26 22:28:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Mine 15 more blocks with lots of transactions happening and not getting mined
|
|
|
|
// Estimates should go up
|
|
|
|
while (blocknum < 265) {
|
2016-03-21 18:04:40 +01:00
|
|
|
for (int j = 0; j < 10; j++) { // For each fee multiple
|
|
|
|
for (int k = 0; k < 4; k++) { // add 4 fee txs
|
2014-08-26 22:28:32 +02:00
|
|
|
tx.vin[0].prevout.n = 10000*blocknum+100*j+k;
|
|
|
|
uint256 hash = tx.GetHash();
|
2017-01-20 04:46:50 +01:00
|
|
|
mpool.addUnchecked(hash, entry.Fee(feeV[j]).Time(GetTime()).Height(blocknum).FromTx(tx));
|
2014-08-26 22:28:32 +02:00
|
|
|
txHashes[j].push_back(hash);
|
|
|
|
}
|
|
|
|
}
|
2016-08-15 13:10:57 +02:00
|
|
|
mpool.removeForBlock(block, ++blocknum);
|
2014-08-26 22:28:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 1; i < 10;i++) {
|
2017-02-15 21:23:34 +01:00
|
|
|
BOOST_CHECK(feeEst.estimateFee(i) == CFeeRate(0) || feeEst.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
|
2014-08-26 22:28:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mine all those transactions
|
|
|
|
// Estimates should still not be below original
|
|
|
|
for (int j = 0; j < 10; j++) {
|
|
|
|
while(txHashes[j].size()) {
|
2016-11-11 02:34:17 +01:00
|
|
|
CTransactionRef ptx = mpool.get(txHashes[j].back());
|
2016-06-07 13:44:56 +02:00
|
|
|
if (ptx)
|
2016-11-11 02:26:00 +01:00
|
|
|
block.push_back(ptx);
|
2014-08-26 22:28:32 +02:00
|
|
|
txHashes[j].pop_back();
|
|
|
|
}
|
|
|
|
}
|
2017-03-09 21:26:05 +01:00
|
|
|
mpool.removeForBlock(block, 266);
|
2014-08-26 22:28:32 +02:00
|
|
|
block.clear();
|
2017-02-15 21:23:34 +01:00
|
|
|
BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
|
2016-11-29 18:18:44 +01:00
|
|
|
for (int i = 2; i < 10;i++) {
|
2017-03-09 21:26:05 +01:00
|
|
|
BOOST_CHECK(feeEst.estimateFee(i) == CFeeRate(0) || feeEst.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
|
2014-08-26 22:28:32 +02:00
|
|
|
}
|
|
|
|
|
2017-03-09 21:26:05 +01:00
|
|
|
// Mine 400 more blocks where everything is mined every block
|
2015-11-16 21:18:15 +01:00
|
|
|
// Estimates should be below original estimates
|
2017-03-09 21:26:05 +01:00
|
|
|
while (blocknum < 665) {
|
2016-03-21 18:04:40 +01:00
|
|
|
for (int j = 0; j < 10; j++) { // For each fee multiple
|
|
|
|
for (int k = 0; k < 4; k++) { // add 4 fee txs
|
2014-08-26 22:28:32 +02:00
|
|
|
tx.vin[0].prevout.n = 10000*blocknum+100*j+k;
|
|
|
|
uint256 hash = tx.GetHash();
|
2017-01-20 04:46:50 +01:00
|
|
|
mpool.addUnchecked(hash, entry.Fee(feeV[j]).Time(GetTime()).Height(blocknum).FromTx(tx));
|
2016-11-11 02:34:17 +01:00
|
|
|
CTransactionRef ptx = mpool.get(hash);
|
2016-06-07 13:44:56 +02:00
|
|
|
if (ptx)
|
2016-11-11 02:26:00 +01:00
|
|
|
block.push_back(ptx);
|
2016-03-21 18:04:40 +01:00
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-15 13:10:57 +02:00
|
|
|
mpool.removeForBlock(block, ++blocknum);
|
2014-08-26 22:28:32 +02:00
|
|
|
block.clear();
|
|
|
|
}
|
2017-02-15 21:23:34 +01:00
|
|
|
BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
|
2017-04-03 17:31:27 +02:00
|
|
|
for (int i = 2; i < 9; i++) { // At 9, the original estimate was already at the bottom (b/c scale = 2)
|
2017-02-15 21:23:34 +01:00
|
|
|
BOOST_CHECK(feeEst.estimateFee(i).GetFeePerK() < origFeeEst[i-1] - deltaFee);
|
2014-08-26 22:28:32 +02:00
|
|
|
}
|
2015-11-16 21:23:33 +01:00
|
|
|
|
|
|
|
// Test that if the mempool is limited, estimateSmartFee won't return a value below the mempool min fee
|
2017-01-20 04:46:50 +01:00
|
|
|
mpool.addUnchecked(tx.GetHash(), entry.Fee(feeV[5]).Time(GetTime()).Height(blocknum).FromTx(tx));
|
2016-03-21 18:04:40 +01:00
|
|
|
// evict that transaction which should set a mempool min fee of minRelayTxFee + feeV[5]
|
2015-11-16 21:23:33 +01:00
|
|
|
mpool.TrimToSize(1);
|
2016-03-21 18:04:40 +01:00
|
|
|
BOOST_CHECK(mpool.GetMinFee(1).GetFeePerK() > feeV[5]);
|
2015-11-16 21:23:33 +01:00
|
|
|
for (int i = 1; i < 10; i++) {
|
2017-03-07 17:33:44 +01:00
|
|
|
BOOST_CHECK(feeEst.estimateSmartFee(i, NULL, mpool).GetFeePerK() >= feeEst.estimateRawFee(i, 0.85, FeeEstimateHorizon::MED_HALFLIFE).GetFeePerK());
|
2017-02-15 21:23:34 +01:00
|
|
|
BOOST_CHECK(feeEst.estimateSmartFee(i, NULL, mpool).GetFeePerK() >= mpool.GetMinFee(1).GetFeePerK());
|
2015-11-16 21:23:33 +01:00
|
|
|
}
|
2014-08-26 22:28:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|