feefrac: add support for evaluating at given size

This commit is contained in:
Pieter Wuille 2024-07-29 16:06:30 -04:00
parent 78e16b4f53
commit 1f142c8b88
3 changed files with 67 additions and 1 deletions

View file

@ -17,6 +17,27 @@ BOOST_AUTO_TEST_CASE(feefrac_operators)
FeeFrac empty{0, 0};
FeeFrac zero_fee{0, 1}; // zero-fee allowed
BOOST_CHECK_EQUAL(zero_fee.EvaluateFee(0), 0);
BOOST_CHECK_EQUAL(zero_fee.EvaluateFee(1), 0);
BOOST_CHECK_EQUAL(zero_fee.EvaluateFee(1000000), 0);
BOOST_CHECK_EQUAL(zero_fee.EvaluateFee(0x7fffffff), 0);
BOOST_CHECK_EQUAL(p1.EvaluateFee(0), 0);
BOOST_CHECK_EQUAL(p1.EvaluateFee(1), 10);
BOOST_CHECK_EQUAL(p1.EvaluateFee(100000000), 1000000000);
BOOST_CHECK_EQUAL(p1.EvaluateFee(0x7fffffff), int64_t(0x7fffffff) * 10);
FeeFrac neg{-1001, 100};
BOOST_CHECK_EQUAL(neg.EvaluateFee(0), 0);
BOOST_CHECK_EQUAL(neg.EvaluateFee(1), -11);
BOOST_CHECK_EQUAL(neg.EvaluateFee(2), -21);
BOOST_CHECK_EQUAL(neg.EvaluateFee(3), -31);
BOOST_CHECK_EQUAL(neg.EvaluateFee(100), -1001);
BOOST_CHECK_EQUAL(neg.EvaluateFee(101), -1012);
BOOST_CHECK_EQUAL(neg.EvaluateFee(100000000), -1001000000);
BOOST_CHECK_EQUAL(neg.EvaluateFee(100000001), -1001000011);
BOOST_CHECK_EQUAL(neg.EvaluateFee(0x7fffffff), -21496311307);
BOOST_CHECK(empty == FeeFrac{}); // same as no-args
BOOST_CHECK(p1 == p1);
@ -67,6 +88,16 @@ BOOST_AUTO_TEST_CASE(feefrac_operators)
BOOST_CHECK(oversized_1 << oversized_2);
BOOST_CHECK(oversized_1 != oversized_2);
BOOST_CHECK_EQUAL(oversized_1.EvaluateFee(0), 0);
BOOST_CHECK_EQUAL(oversized_1.EvaluateFee(1), 1152921);
BOOST_CHECK_EQUAL(oversized_1.EvaluateFee(2), 2305843);
BOOST_CHECK_EQUAL(oversized_1.EvaluateFee(1548031267), 1784758530396540);
// Test cases on the threshold where FeeFrac::EvaluateFee start using Mul/Div.
BOOST_CHECK_EQUAL(FeeFrac(0x1ffffffff, 123456789).EvaluateFee(98765432), 6871947728);
BOOST_CHECK_EQUAL(FeeFrac(0x200000000, 123456789).EvaluateFee(98765432), 6871947729);
BOOST_CHECK_EQUAL(FeeFrac(0x200000001, 123456789).EvaluateFee(98765432), 6871947730);
// Tests paths that use double arithmetic
FeeFrac busted{(static_cast<int64_t>(INT32_MAX)) + 1, INT32_MAX};
BOOST_CHECK(!(busted < busted));
@ -77,6 +108,13 @@ BOOST_AUTO_TEST_CASE(feefrac_operators)
BOOST_CHECK(max_fee <= max_fee);
BOOST_CHECK(max_fee >= max_fee);
BOOST_CHECK_EQUAL(max_fee.EvaluateFee(0), 0);
BOOST_CHECK_EQUAL(max_fee.EvaluateFee(1), 977888);
BOOST_CHECK_EQUAL(max_fee.EvaluateFee(2), 1955777);
BOOST_CHECK_EQUAL(max_fee.EvaluateFee(3), 2933666);
BOOST_CHECK_EQUAL(max_fee.EvaluateFee(1256796054), 1229006664189047);
BOOST_CHECK_EQUAL(max_fee.EvaluateFee(INT32_MAX), 2100000000000000);
FeeFrac max_fee2{1, 1};
BOOST_CHECK(max_fee >= max_fee2);

View file

@ -159,7 +159,8 @@ FUZZ_TARGET(feefrac_mul_div)
// If the result is not representable by an int64_t, bail out.
if ((is_negative && quot_abs > MAX_ABS_INT64) || (!is_negative && quot_abs >= MAX_ABS_INT64)) {
// If 0 <= mul32 <= div, then the result is guaranteed to be representable.
// If 0 <= mul32 <= div, then the result is guaranteed to be representable. In the context
// of the Evaluate call below, this corresponds to 0 <= at_size <= feefrac.size.
assert(mul32 < 0 || mul32 > div);
return;
}
@ -172,4 +173,10 @@ FUZZ_TARGET(feefrac_mul_div)
// Verify the behavior of FeeFrac::MulFallback + FeeFrac::DivFallback.
auto res_fallback = FeeFrac::DivFallback(FeeFrac::MulFallback(mul64, mul32), div);
assert(res == res_fallback);
// Verify the behavior of FeeFrac::Evaluate.
if (mul32 > 0) {
auto res_fee = FeeFrac{mul64, div}.EvaluateFee(mul32);
assert(res == res_fee);
}
}

View file

@ -182,6 +182,27 @@ struct FeeFrac
std::swap(a.fee, b.fee);
std::swap(a.size, b.size);
}
/** Compute the fee for a given size `at_size` using this object's feerate.
*
* This effectively corresponds to evaluating (this->fee * at_size) / this->size, with the
* result rounded down (even for negative feerates).
*
* Requires this->size > 0, at_size >= 0, and that the correct result fits in a int64_t. This
* is guaranteed to be the case when 0 <= at_size <= this->size.
*/
int64_t EvaluateFee(int32_t at_size) const noexcept
{
Assume(size > 0);
Assume(at_size >= 0);
if (fee >= 0 && fee < 0x200000000) [[likely]] {
// Common case where (this->fee * at_size) is guaranteed to fit in a uint64_t.
return (uint64_t(fee) * at_size) / uint32_t(size);
} else {
// Otherwise, use Mul and Div.
return Div(Mul(fee, at_size), size);
}
}
};
/** Compare the feerate diagrams implied by the provided sorted chunks data.