Bitcoin Core  27.99.0
P2P Digital Currency
feefrac_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2024-present 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 <util/feefrac.h>
6 #include <random.h>
7 
8 #include <boost/test/unit_test.hpp>
9 
10 BOOST_AUTO_TEST_SUITE(feefrac_tests)
11 
12 BOOST_AUTO_TEST_CASE(feefrac_operators)
13 {
14  FeeFrac p1{1000, 100}, p2{500, 300};
15  FeeFrac sum{1500, 400};
16  FeeFrac diff{500, -200};
17  FeeFrac empty{0, 0};
18  FeeFrac zero_fee{0, 1}; // zero-fee allowed
19 
20  BOOST_CHECK(empty == FeeFrac{}); // same as no-args
21 
22  BOOST_CHECK(p1 == p1);
23  BOOST_CHECK(p1 + p2 == sum);
24  BOOST_CHECK(p1 - p2 == diff);
25 
26  FeeFrac p3{2000, 200};
27  BOOST_CHECK(p1 != p3); // feefracs only equal if both fee and size are same
28  BOOST_CHECK(p2 != p3);
29 
30  FeeFrac p4{3000, 300};
31  BOOST_CHECK(p1 == p4-p3);
32  BOOST_CHECK(p1 + p3 == p4);
33 
34  // Fee-rate comparison
35  BOOST_CHECK(p1 > p2);
36  BOOST_CHECK(p1 >= p2);
37  BOOST_CHECK(p1 >= p4-p3);
38  BOOST_CHECK(!(p1 >> p3)); // not strictly better
39  BOOST_CHECK(p1 >> p2); // strictly greater feerate
40 
41  BOOST_CHECK(p2 < p1);
42  BOOST_CHECK(p2 <= p1);
43  BOOST_CHECK(p1 <= p4-p3);
44  BOOST_CHECK(!(p3 << p1)); // not strictly worse
45  BOOST_CHECK(p2 << p1); // strictly lower feerate
46 
47  // "empty" comparisons
48  BOOST_CHECK(!(p1 >> empty)); // << will always result in false
49  BOOST_CHECK(!(p1 << empty));
50  BOOST_CHECK(!(empty >> empty));
51  BOOST_CHECK(!(empty << empty));
52 
53  // empty is always bigger than everything else
54  BOOST_CHECK(empty > p1);
55  BOOST_CHECK(empty > p2);
56  BOOST_CHECK(empty > p3);
57  BOOST_CHECK(empty >= p1);
58  BOOST_CHECK(empty >= p2);
59  BOOST_CHECK(empty >= p3);
60 
61  // check "max" values for comparison
62  FeeFrac oversized_1{4611686000000, 4000000};
63  FeeFrac oversized_2{184467440000000, 100000};
64 
65  BOOST_CHECK(oversized_1 < oversized_2);
66  BOOST_CHECK(oversized_1 <= oversized_2);
67  BOOST_CHECK(oversized_1 << oversized_2);
68  BOOST_CHECK(oversized_1 != oversized_2);
69 
70  // Tests paths that use double arithmetic
71  FeeFrac busted{(static_cast<int64_t>(INT32_MAX)) + 1, INT32_MAX};
72  BOOST_CHECK(!(busted < busted));
73 
74  FeeFrac max_fee{2100000000000000, INT32_MAX};
75  BOOST_CHECK(!(max_fee < max_fee));
76  BOOST_CHECK(!(max_fee > max_fee));
77  BOOST_CHECK(max_fee <= max_fee);
78  BOOST_CHECK(max_fee >= max_fee);
79 
80  FeeFrac max_fee2{1, 1};
81  BOOST_CHECK(max_fee >= max_fee2);
82 
83 }
84 
85 BOOST_AUTO_TEST_CASE(build_diagram_test)
86 {
87  FeeFrac p1{1000, 100};
88  FeeFrac empty{0, 0};
89  FeeFrac zero_fee{0, 1};
90  FeeFrac oversized_1{4611686000000, 4000000};
91  FeeFrac oversized_2{184467440000000, 100000};
92 
93  // Diagram-building will reorder chunks
94  std::vector<FeeFrac> chunks;
95  chunks.push_back(p1);
96  chunks.push_back(zero_fee);
97  chunks.push_back(empty);
98  chunks.push_back(oversized_1);
99  chunks.push_back(oversized_2);
100 
101  // Caller in charge of sorting chunks in case chunk size limit
102  // differs from cluster size limit
103  std::sort(chunks.begin(), chunks.end(), [](const FeeFrac& a, const FeeFrac& b) { return a > b; });
104 
105  // Chunks are now sorted in reverse order (largest first)
106  BOOST_CHECK(chunks[0] == empty); // empty is considered "highest" fee
107  BOOST_CHECK(chunks[1] == oversized_2);
108  BOOST_CHECK(chunks[2] == oversized_1);
109  BOOST_CHECK(chunks[3] == p1);
110  BOOST_CHECK(chunks[4] == zero_fee);
111 
112  std::vector<FeeFrac> generated_diagram{BuildDiagramFromChunks(chunks)};
113  BOOST_CHECK(generated_diagram.size() == 1 + chunks.size());
114 
115  // Prepended with an empty, then the chunks summed in order as above
116  BOOST_CHECK(generated_diagram[0] == empty);
117  BOOST_CHECK(generated_diagram[1] == empty);
118  BOOST_CHECK(generated_diagram[2] == oversized_2);
119  BOOST_CHECK(generated_diagram[3] == oversized_2 + oversized_1);
120  BOOST_CHECK(generated_diagram[4] == oversized_2 + oversized_1 + p1);
121  BOOST_CHECK(generated_diagram[5] == oversized_2 + oversized_1 + p1 + zero_fee);
122 }
123 
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
volatile double sum
Definition: examples.cpp:10
BOOST_AUTO_TEST_CASE(feefrac_operators)
#define BOOST_CHECK(expr)
Definition: object.cpp:17
Data structure storing a fee and size, ordered by increasing fee/size.
Definition: feefrac.h:39
std::vector< FeeFrac > BuildDiagramFromChunks(const Span< const FeeFrac > chunks)
Takes the pre-computed and topologically-valid chunks and generates a fee diagram which starts at Fee...
Definition: feefrac.cpp:10