aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2018-08-13 07:18:00 -0400
committerMarcoFalke <falke.marco@gmail.com>2018-08-13 07:18:25 -0400
commita9c56b663439039b683f6ca2bcab12aab7c71366 (patch)
tree382060e6592c12cb13d74b8607fd189baf49319d /src/test
parentef86f2631e3ce5e1664e1913658fa6e807f35ff7 (diff)
parent4b7091a842c2c3a76f4136cb0fdcf1c5904fd237 (diff)
downloadbitcoin-a9c56b663439039b683f6ca2bcab12aab7c71366.tar.xz
Merge #13918: rpc: Replace median fee rate with feerate percentiles in getblockstats
4b7091a842 Replace median fee rate with feerate percentiles (Marcin Jachymiak) Pull request description: Currently, the `medianfeerate` statistic is calculated from the feerate of the middle transaction of a list of transactions sorted by feerate. This PR instead uses the value of the 50th percentile weight unit in the block, and also calculates the feerate at the 10th, 25th, 75th, and 90th percentiles. This more accurately corresponds with what is generally meant by median feerate. Tree-SHA512: 59255e243df90d7afbe69839408c58c9723884b8ab82c66dc24a769e89c6d539db1905374a3f025ff28272fb25a0b90e92d8101103e39a6d9c0d60423a596714
Diffstat (limited to 'src/test')
-rw-r--r--src/test/rpc_tests.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp
index d0f6fba78d..a49796d6f4 100644
--- a/src/test/rpc_tests.cpp
+++ b/src/test/rpc_tests.cpp
@@ -16,6 +16,8 @@
#include <univalue.h>
+#include <rpc/blockchain.h>
+
UniValue CallRPC(std::string args)
{
std::vector<std::string> vArgs;
@@ -336,4 +338,82 @@ BOOST_AUTO_TEST_CASE(rpc_convert_values_generatetoaddress)
BOOST_CHECK_EQUAL(result[2].get_int(), 9);
}
+BOOST_AUTO_TEST_CASE(rpc_getblockstats_calculate_percentiles_by_weight)
+{
+ int64_t total_weight = 200;
+ std::vector<std::pair<CAmount, int64_t>> feerates;
+ CAmount result[NUM_GETBLOCKSTATS_PERCENTILES] = { 0 };
+
+ for (int64_t i = 0; i < 100; i++) {
+ feerates.emplace_back(std::make_pair(1 ,1));
+ }
+
+ for (int64_t i = 0; i < 100; i++) {
+ feerates.emplace_back(std::make_pair(2 ,1));
+ }
+
+ CalculatePercentilesByWeight(result, feerates, total_weight);
+ BOOST_CHECK_EQUAL(result[0], 1);
+ BOOST_CHECK_EQUAL(result[1], 1);
+ BOOST_CHECK_EQUAL(result[2], 1);
+ BOOST_CHECK_EQUAL(result[3], 2);
+ BOOST_CHECK_EQUAL(result[4], 2);
+
+ // Test with more pairs, and two pairs overlapping 2 percentiles.
+ total_weight = 100;
+ CAmount result2[NUM_GETBLOCKSTATS_PERCENTILES] = { 0 };
+ feerates.clear();
+
+ feerates.emplace_back(std::make_pair(1, 9));
+ feerates.emplace_back(std::make_pair(2 , 16)); //10th + 25th percentile
+ feerates.emplace_back(std::make_pair(4 ,50)); //50th + 75th percentile
+ feerates.emplace_back(std::make_pair(5 ,10));
+ feerates.emplace_back(std::make_pair(9 ,15)); // 90th percentile
+
+ CalculatePercentilesByWeight(result2, feerates, total_weight);
+
+ BOOST_CHECK_EQUAL(result2[0], 2);
+ BOOST_CHECK_EQUAL(result2[1], 2);
+ BOOST_CHECK_EQUAL(result2[2], 4);
+ BOOST_CHECK_EQUAL(result2[3], 4);
+ BOOST_CHECK_EQUAL(result2[4], 9);
+
+ // Same test as above, but one of the percentile-overlapping pairs is split in 2.
+ total_weight = 100;
+ CAmount result3[NUM_GETBLOCKSTATS_PERCENTILES] = { 0 };
+ feerates.clear();
+
+ feerates.emplace_back(std::make_pair(1, 9));
+ feerates.emplace_back(std::make_pair(2 , 11)); // 10th percentile
+ feerates.emplace_back(std::make_pair(2 , 5)); // 25th percentile
+ feerates.emplace_back(std::make_pair(4 ,50)); //50th + 75th percentile
+ feerates.emplace_back(std::make_pair(5 ,10));
+ feerates.emplace_back(std::make_pair(9 ,15)); // 90th percentile
+
+ CalculatePercentilesByWeight(result3, feerates, total_weight);
+
+ BOOST_CHECK_EQUAL(result3[0], 2);
+ BOOST_CHECK_EQUAL(result3[1], 2);
+ BOOST_CHECK_EQUAL(result3[2], 4);
+ BOOST_CHECK_EQUAL(result3[3], 4);
+ BOOST_CHECK_EQUAL(result3[4], 9);
+
+ // Test with one transaction spanning all percentiles.
+ total_weight = 104;
+ CAmount result4[NUM_GETBLOCKSTATS_PERCENTILES] = { 0 };
+ feerates.clear();
+
+ feerates.emplace_back(std::make_pair(1, 100));
+ feerates.emplace_back(std::make_pair(2, 1));
+ feerates.emplace_back(std::make_pair(3, 1));
+ feerates.emplace_back(std::make_pair(3, 1));
+ feerates.emplace_back(std::make_pair(999999, 1));
+
+ CalculatePercentilesByWeight(result4, feerates, total_weight);
+
+ for (int64_t i = 0; i < NUM_GETBLOCKSTATS_PERCENTILES; i++) {
+ BOOST_CHECK_EQUAL(result4[i], 1);
+ }
+}
+
BOOST_AUTO_TEST_SUITE_END()