Skip to content

Commit ce8e225

Browse files
instagibbssdaftuarsipa
committed
Add FeeFrac utils
Co-authored-by: Suhas Daftuar <[email protected]> Co-authored-by: Pieter Wuille <[email protected]>
1 parent 02c7fd8 commit ce8e225

File tree

3 files changed

+249
-0
lines changed

3 files changed

+249
-0
lines changed

src/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ BITCOIN_CORE_H = \
302302
util/error.h \
303303
util/exception.h \
304304
util/fastrange.h \
305+
util/feefrac.h \
305306
util/fees.h \
306307
util/fs.h \
307308
util/fs_helpers.h \
@@ -741,6 +742,7 @@ libbitcoin_util_a_SOURCES = \
741742
util/check.cpp \
742743
util/error.cpp \
743744
util/exception.cpp \
745+
util/feefrac.cpp \
744746
util/fees.cpp \
745747
util/fs.cpp \
746748
util/fs_helpers.cpp \
@@ -983,6 +985,7 @@ libbitcoinkernel_la_SOURCES = \
983985
util/batchpriority.cpp \
984986
util/chaintype.cpp \
985987
util/check.cpp \
988+
util/feefrac.cpp \
986989
util/fs.cpp \
987990
util/fs_helpers.cpp \
988991
util/hasher.cpp \

src/util/feefrac.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) 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 <algorithm>
7+
#include <array>
8+
#include <vector>
9+
10+
std::vector<FeeFrac> BuildDiagramFromChunks(const Span<const FeeFrac> chunks)
11+
{
12+
std::vector<FeeFrac> diagram;
13+
diagram.reserve(chunks.size() + 1);
14+
15+
diagram.emplace_back(0, 0);
16+
for (auto& chunk : chunks) {
17+
diagram.emplace_back(diagram.back() + chunk);
18+
}
19+
return diagram;
20+
}
21+
22+
std::partial_ordering CompareFeerateDiagram(Span<const FeeFrac> dia0, Span<const FeeFrac> dia1)
23+
{
24+
/** Array to allow indexed access to input diagrams. */
25+
const std::array<Span<const FeeFrac>, 2> dias = {dia0, dia1};
26+
/** How many elements we have processed in each input. */
27+
size_t next_index[2] = {1, 1};
28+
/** Whether the corresponding input is strictly better than the other at least in one place. */
29+
bool better_somewhere[2] = {false, false};
30+
/** Get the first unprocessed point in diagram number dia. */
31+
const auto next_point = [&](int dia) { return dias[dia][next_index[dia]]; };
32+
/** Get the last processed point in diagram number dia. */
33+
const auto prev_point = [&](int dia) { return dias[dia][next_index[dia] - 1]; };
34+
35+
// Diagrams should be non-empty, and first elements zero in size and fee
36+
Assert(!dia0.empty() && !dia1.empty());
37+
Assert(prev_point(0).IsEmpty());
38+
Assert(prev_point(1).IsEmpty());
39+
40+
do {
41+
bool done_0 = next_index[0] == dias[0].size();
42+
bool done_1 = next_index[1] == dias[1].size();
43+
if (done_0 && done_1) break;
44+
45+
// Determine which diagram has the first unprocessed point. If a single side is finished, use the
46+
// other one. Only up to one can be done due to check above.
47+
const int unproc_side = (done_0 || done_1) ? done_0 : next_point(0).size > next_point(1).size;
48+
49+
// Let `P` be the next point on diagram unproc_side, and `A` and `B` the previous and next points
50+
// on the other diagram. We want to know if P lies above or below the line AB. To determine this, we
51+
// compute the slopes of line AB and of line AP, and compare them. These slopes are fee per size,
52+
// and can thus be expressed as FeeFracs.
53+
const FeeFrac& point_p = next_point(unproc_side);
54+
const FeeFrac& point_a = prev_point(!unproc_side);
55+
56+
// Slope of AP can be negative, unlike AB
57+
const auto slope_ap = point_p - point_a;
58+
Assume(slope_ap.size > 0);
59+
std::weak_ordering cmp = std::weak_ordering::equivalent;
60+
if (done_0 || done_1) {
61+
// If a single side has no points left, act as if AB has slope tail_feerate(of 0).
62+
Assume(!(done_0 && done_1));
63+
cmp = FeeRateCompare(slope_ap, FeeFrac(0, 1));
64+
} else {
65+
// If both sides have points left, compute B, and the slope of AB explicitly.
66+
const FeeFrac& point_b = next_point(!unproc_side);
67+
const auto slope_ab = point_b - point_a;
68+
Assume(slope_ab.size >= slope_ap.size);
69+
cmp = FeeRateCompare(slope_ap, slope_ab);
70+
71+
// If B and P have the same size, B can be marked as processed (in addition to P, see
72+
// below), as we've already performed a comparison at this size.
73+
if (point_b.size == point_p.size) ++next_index[!unproc_side];
74+
}
75+
// If P lies above AB, unproc_side is better in P. If P lies below AB, then !unproc_side is
76+
// better in P.
77+
if (std::is_gt(cmp)) better_somewhere[unproc_side] = true;
78+
if (std::is_lt(cmp)) better_somewhere[!unproc_side] = true;
79+
++next_index[unproc_side];
80+
} while(true);
81+
82+
// If both diagrams are better somewhere, they are incomparable.
83+
if (better_somewhere[0] && better_somewhere[1]) return std::partial_ordering::unordered;
84+
// Otherwise compare the better_somewhere values.
85+
return better_somewhere[0] <=> better_somewhere[1];
86+
}

src/util/feefrac.h

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Copyright (c) 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+
#ifndef BITCOIN_UTIL_FEEFRAC_H
6+
#define BITCOIN_UTIL_FEEFRAC_H
7+
8+
#include <stdint.h>
9+
#include <compare>
10+
#include <vector>
11+
#include <span.h>
12+
#include <util/check.h>
13+
14+
/** Data structure storing a fee and size, ordered by increasing fee/size.
15+
*
16+
* The size of a FeeFrac cannot be zero unless the fee is also zero.
17+
*
18+
* FeeFracs have a total ordering, first by increasing feerate (ratio of fee over size), and then
19+
* by decreasing size. The empty FeeFrac (fee and size both 0) sorts last. So for example, the
20+
* following FeeFracs are in sorted order:
21+
*
22+
* - fee=0 size=1 (feerate 0)
23+
* - fee=1 size=2 (feerate 0.5)
24+
* - fee=2 size=3 (feerate 0.667...)
25+
* - fee=2 size=2 (feerate 1)
26+
* - fee=1 size=1 (feerate 1)
27+
* - fee=3 size=2 (feerate 1.5)
28+
* - fee=2 size=1 (feerate 2)
29+
* - fee=0 size=0 (undefined feerate)
30+
*
31+
* A FeeFrac is considered "better" if it sorts after another, by this ordering. All standard
32+
* comparison operators (<=>, ==, !=, >, <, >=, <=) respect this ordering.
33+
*
34+
* The CompareFeeFrac, and >> and << operators only compare feerate and treat equal feerate but
35+
* different size as equivalent. The empty FeeFrac is neither lower or higher in feerate than any
36+
* other.
37+
*/
38+
struct FeeFrac
39+
{
40+
/** Fallback version for Mul (see below).
41+
*
42+
* Separate to permit testing on platforms where it isn't actually needed.
43+
*/
44+
static inline std::pair<int64_t, uint32_t> MulFallback(int64_t a, int32_t b) noexcept
45+
{
46+
// Otherwise, emulate 96-bit multiplication using two 64-bit multiplies.
47+
int64_t low = int64_t{static_cast<uint32_t>(a)} * b;
48+
int64_t high = (a >> 32) * b;
49+
return {high + (low >> 32), static_cast<uint32_t>(low)};
50+
}
51+
52+
// Compute a * b, returning an unspecified but totally ordered type.
53+
#ifdef __SIZEOF_INT128__
54+
static inline __int128 Mul(int64_t a, int32_t b) noexcept
55+
{
56+
// If __int128 is available, use 128-bit wide multiply.
57+
return __int128{a} * b;
58+
}
59+
#else
60+
static constexpr auto Mul = MulFallback;
61+
#endif
62+
63+
int64_t fee;
64+
int32_t size;
65+
66+
/** Construct an IsEmpty() FeeFrac. */
67+
inline FeeFrac() noexcept : fee{0}, size{0} {}
68+
69+
/** Construct a FeeFrac with specified fee and size. */
70+
inline FeeFrac(int64_t f, int32_t s) noexcept : fee{f}, size{s} {}
71+
72+
inline FeeFrac(const FeeFrac&) noexcept = default;
73+
inline FeeFrac& operator=(const FeeFrac&) noexcept = default;
74+
75+
/** Check if this is empty (size and fee are 0). */
76+
bool inline IsEmpty() const noexcept {
77+
return size == 0;
78+
}
79+
80+
/** Add fee and size of another FeeFrac to this one. */
81+
void inline operator+=(const FeeFrac& other) noexcept
82+
{
83+
fee += other.fee;
84+
size += other.size;
85+
}
86+
87+
/** Subtract fee and size of another FeeFrac from this one. */
88+
void inline operator-=(const FeeFrac& other) noexcept
89+
{
90+
fee -= other.fee;
91+
size -= other.size;
92+
}
93+
94+
/** Sum fee and size. */
95+
friend inline FeeFrac operator+(const FeeFrac& a, const FeeFrac& b) noexcept
96+
{
97+
return {a.fee + b.fee, a.size + b.size};
98+
}
99+
100+
/** Subtract both fee and size. */
101+
friend inline FeeFrac operator-(const FeeFrac& a, const FeeFrac& b) noexcept
102+
{
103+
return {a.fee - b.fee, a.size - b.size};
104+
}
105+
106+
/** Check if two FeeFrac objects are equal (both same fee and same size). */
107+
friend inline bool operator==(const FeeFrac& a, const FeeFrac& b) noexcept
108+
{
109+
return a.fee == b.fee && a.size == b.size;
110+
}
111+
112+
/** Compare two FeeFracs just by feerate. */
113+
friend inline std::weak_ordering FeeRateCompare(const FeeFrac& a, const FeeFrac& b) noexcept
114+
{
115+
auto cross_a = Mul(a.fee, b.size), cross_b = Mul(b.fee, a.size);
116+
return cross_a <=> cross_b;
117+
}
118+
119+
/** Check if a FeeFrac object has strictly lower feerate than another. */
120+
friend inline bool operator<<(const FeeFrac& a, const FeeFrac& b) noexcept
121+
{
122+
auto cross_a = Mul(a.fee, b.size), cross_b = Mul(b.fee, a.size);
123+
return cross_a < cross_b;
124+
}
125+
126+
/** Check if a FeeFrac object has strictly higher feerate than another. */
127+
friend inline bool operator>>(const FeeFrac& a, const FeeFrac& b) noexcept
128+
{
129+
auto cross_a = Mul(a.fee, b.size), cross_b = Mul(b.fee, a.size);
130+
return cross_a > cross_b;
131+
}
132+
133+
/** Compare two FeeFracs. <, >, <=, and >= are auto-generated from this. */
134+
friend inline std::strong_ordering operator<=>(const FeeFrac& a, const FeeFrac& b) noexcept
135+
{
136+
auto cross_a = Mul(a.fee, b.size), cross_b = Mul(b.fee, a.size);
137+
if (cross_a == cross_b) return b.size <=> a.size;
138+
return cross_a <=> cross_b;
139+
}
140+
141+
/** Swap two FeeFracs. */
142+
friend inline void swap(FeeFrac& a, FeeFrac& b) noexcept
143+
{
144+
std::swap(a.fee, b.fee);
145+
std::swap(a.size, b.size);
146+
}
147+
};
148+
149+
/** Takes the pre-computed and topologically-valid chunks and generates a fee diagram which starts at FeeFrac of (0, 0) */
150+
std::vector<FeeFrac> BuildDiagramFromChunks(Span<const FeeFrac> chunks);
151+
152+
/** Compares two feerate diagrams. The shorter one is implicitly
153+
* extended with a horizontal straight line.
154+
*
155+
* A feerate diagram consists of a list of (fee, size) points with the property that size
156+
* is strictly increasing and that the first entry is (0, 0).
157+
*/
158+
std::partial_ordering CompareFeerateDiagram(Span<const FeeFrac> dia0, Span<const FeeFrac> dia1);
159+
160+
#endif // BITCOIN_UTIL_FEEFRAC_H

0 commit comments

Comments
 (0)