Skip to content

Commit ecf956e

Browse files
committed
feefrac: add support for evaluating at given size
1 parent 7963aec commit ecf956e

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

src/test/feefrac_tests.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,28 @@ BOOST_AUTO_TEST_CASE(feefrac_operators)
1515
FeeFrac sum{1500, 400};
1616
FeeFrac diff{500, -200};
1717
FeeFrac empty{0, 0};
18-
[[maybe_unused]] FeeFrac zero_fee{0, 1}; // zero-fee allowed
18+
FeeFrac zero_fee{0, 1}; // zero-fee allowed
19+
20+
BOOST_CHECK_EQUAL(zero_fee.EvaluateFee(0), 0);
21+
BOOST_CHECK_EQUAL(zero_fee.EvaluateFee(1), 0);
22+
BOOST_CHECK_EQUAL(zero_fee.EvaluateFee(1000000), 0);
23+
BOOST_CHECK_EQUAL(zero_fee.EvaluateFee(0x7fffffff), 0);
24+
25+
BOOST_CHECK_EQUAL(p1.EvaluateFee(0), 0);
26+
BOOST_CHECK_EQUAL(p1.EvaluateFee(1), 10);
27+
BOOST_CHECK_EQUAL(p1.EvaluateFee(100000000), 1000000000);
28+
BOOST_CHECK_EQUAL(p1.EvaluateFee(0x7fffffff), int64_t(0x7fffffff) * 10);
29+
30+
FeeFrac neg{-1001, 100};
31+
BOOST_CHECK_EQUAL(neg.EvaluateFee(0), 0);
32+
BOOST_CHECK_EQUAL(neg.EvaluateFee(1), -11);
33+
BOOST_CHECK_EQUAL(neg.EvaluateFee(2), -21);
34+
BOOST_CHECK_EQUAL(neg.EvaluateFee(3), -31);
35+
BOOST_CHECK_EQUAL(neg.EvaluateFee(100), -1001);
36+
BOOST_CHECK_EQUAL(neg.EvaluateFee(101), -1012);
37+
BOOST_CHECK_EQUAL(neg.EvaluateFee(100000000), -1001000000);
38+
BOOST_CHECK_EQUAL(neg.EvaluateFee(100000001), -1001000011);
39+
BOOST_CHECK_EQUAL(neg.EvaluateFee(0x7fffffff), -21496311307);
1940

2041
BOOST_CHECK(empty == FeeFrac{}); // same as no-args
2142

@@ -67,6 +88,16 @@ BOOST_AUTO_TEST_CASE(feefrac_operators)
6788
BOOST_CHECK(oversized_1 << oversized_2);
6889
BOOST_CHECK(oversized_1 != oversized_2);
6990

91+
BOOST_CHECK_EQUAL(oversized_1.EvaluateFee(0), 0);
92+
BOOST_CHECK_EQUAL(oversized_1.EvaluateFee(1), 1152921);
93+
BOOST_CHECK_EQUAL(oversized_1.EvaluateFee(2), 2305843);
94+
BOOST_CHECK_EQUAL(oversized_1.EvaluateFee(1548031267), 1784758530396540);
95+
96+
// Test cases on the threshold where FeeFrac::EvaluateFee start using Mul/Div.
97+
BOOST_CHECK_EQUAL(FeeFrac(0x1ffffffff, 123456789).EvaluateFee(98765432), 6871947728);
98+
BOOST_CHECK_EQUAL(FeeFrac(0x200000000, 123456789).EvaluateFee(98765432), 6871947729);
99+
BOOST_CHECK_EQUAL(FeeFrac(0x200000001, 123456789).EvaluateFee(98765432), 6871947730);
100+
70101
// Tests paths that use double arithmetic
71102
FeeFrac busted{(static_cast<int64_t>(INT32_MAX)) + 1, INT32_MAX};
72103
BOOST_CHECK(!(busted < busted));
@@ -77,6 +108,13 @@ BOOST_AUTO_TEST_CASE(feefrac_operators)
77108
BOOST_CHECK(max_fee <= max_fee);
78109
BOOST_CHECK(max_fee >= max_fee);
79110

111+
BOOST_CHECK_EQUAL(max_fee.EvaluateFee(0), 0);
112+
BOOST_CHECK_EQUAL(max_fee.EvaluateFee(1), 977888);
113+
BOOST_CHECK_EQUAL(max_fee.EvaluateFee(2), 1955777);
114+
BOOST_CHECK_EQUAL(max_fee.EvaluateFee(3), 2933666);
115+
BOOST_CHECK_EQUAL(max_fee.EvaluateFee(1256796054), 1229006664189047);
116+
BOOST_CHECK_EQUAL(max_fee.EvaluateFee(INT32_MAX), 2100000000000000);
117+
80118
FeeFrac max_fee2{1, 1};
81119
BOOST_CHECK(max_fee >= max_fee2);
82120

src/test/fuzz/feefrac.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ FUZZ_TARGET(feefrac_div_fallback)
136136
assert(Abs256(res) == quot_abs);
137137

138138
// Compare approximately with floating-point.
139-
long double expect = std::floorl(num_high * 4294967296.0L + num_low) / den;
139+
long double expect = std::floor(num_high * 4294967296.0L + num_low) / den;
140140
// Expect to be accurate within 50 bits of precision, +- 1 sat.
141141
if (expect == 0.0L) {
142142
assert(res >= -1 && res <= 1);
@@ -173,7 +173,8 @@ FUZZ_TARGET(feefrac_mul_div)
173173

174174
// If the result is not representable by an int64_t, bail out.
175175
if ((is_negative && quot_abs > MAX_ABS_INT64) || (!is_negative && quot_abs >= MAX_ABS_INT64)) {
176-
// If 0 <= mul32 <= div, then the result is guaranteed to be representable.
176+
// If 0 <= mul32 <= div, then the result is guaranteed to be representable. In the context
177+
// of the Evaluate call below, this corresponds to 0 <= at_size <= feefrac.size.
177178
assert(mul32 < 0 || mul32 > div);
178179
return;
179180
}
@@ -188,7 +189,7 @@ FUZZ_TARGET(feefrac_mul_div)
188189
assert(res == res_fallback);
189190

190191
// Compare approximately with floating-point.
191-
long double expect = std::floorl(static_cast<long double>(mul32) * mul64 / div);
192+
long double expect = std::floor(static_cast<long double>(mul32) * mul64 / div);
192193
// Expect to be accurate within 50 bits of precision, +- 1 sat.
193194
if (expect == 0.0L) {
194195
assert(res >= -1 && res <= 1);
@@ -199,4 +200,10 @@ FUZZ_TARGET(feefrac_mul_div)
199200
assert(res >= expect * 1.000000000000001L - 1.0L);
200201
assert(res <= expect * 0.999999999999999L + 1.0L);
201202
}
203+
204+
// Verify the behavior of FeeFrac::Evaluate.
205+
if (mul32 >= 0) {
206+
auto res_fee = FeeFrac{mul64, div}.EvaluateFee(mul32);
207+
assert(res == res_fee);
208+
}
202209
}

src/util/feefrac.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,27 @@ struct FeeFrac
182182
std::swap(a.fee, b.fee);
183183
std::swap(a.size, b.size);
184184
}
185+
186+
/** Compute the fee for a given size `at_size` using this object's feerate.
187+
*
188+
* This effectively corresponds to evaluating (this->fee * at_size) / this->size, with the
189+
* result rounded down (even for negative feerates).
190+
*
191+
* Requires this->size > 0, at_size >= 0, and that the correct result fits in a int64_t. This
192+
* is guaranteed to be the case when 0 <= at_size <= this->size.
193+
*/
194+
int64_t EvaluateFee(int32_t at_size) const noexcept
195+
{
196+
Assume(size > 0);
197+
Assume(at_size >= 0);
198+
if (fee >= 0 && fee < 0x200000000) [[likely]] {
199+
// Common case where (this->fee * at_size) is guaranteed to fit in a uint64_t.
200+
return (uint64_t(fee) * at_size) / uint32_t(size);
201+
} else {
202+
// Otherwise, use Mul and Div.
203+
return Div(Mul(fee, at_size), size);
204+
}
205+
}
185206
};
186207

187208
/** Compare the feerate diagrams implied by the provided sorted chunks data.

0 commit comments

Comments
 (0)