#include "evo/accounting/rates.hpp" #include #include #include #include namespace { using namespace evo::accounting; void check(bool condition) { if (!condition) throw std::runtime_error("rate contract failed"); } #define CHECK(expression) check(static_cast(expression)) constexpr std::uint64_t tick_50ms = 50'000; constexpr auto u64_max = std::numeric_limits::max(); bool is(const std::expected& r, std::uint64_t value) { return r.has_value() && *r == value; } bool fails(const std::expected& r, RateError e) { return !r.has_value() && r.error() == e; } // At the ratified 50 ms tick, every per-tick value migrates exactly (x20) and converts // back unchanged under either rounding: migration preserves behaviour. void migration_is_exact_at_50ms() { for (const std::uint64_t v : {std::uint64_t{0}, std::uint64_t{1}, std::uint64_t{7}, std::uint64_t{100'000}, std::uint64_t{1'000'000'000}, u64_max / 1'000'000}) { const auto per_second = per_second_from_per_tick(v, tick_50ms); CHECK(is(per_second, v * 20)); CHECK(exact_per_tick(*per_second, tick_50ms)); CHECK(is(per_tick(*per_second, tick_50ms, Rounding::down), v)); CHECK(is(per_tick(*per_second, tick_50ms, Rounding::up), v)); } } // A tick that does not divide the rate rounds by direction, by less than one unit. void directional_rounding() { // 10 per second at 1/3 s: 3.33 per tick. CHECK(!exact_per_tick(10, 333'333)); CHECK(is(per_tick(10, 333'333, Rounding::down), 3)); CHECK(is(per_tick(10, 333'333, Rounding::up), 4)); // Below one unit per tick: a capacity rounds to nothing, a cost to one unit. CHECK(is(per_tick(1, tick_50ms, Rounding::down), 0)); CHECK(is(per_tick(1, tick_50ms, Rounding::up), 1)); // Whole seconds and the fraction combine: 1'000'001 per second over 1 s and 0.5 s. CHECK(is(per_tick(1'000'001, 1'000'000, Rounding::down), 1'000'001)); CHECK(is(per_tick(1'000'001, 500'000, Rounding::down), 500'000)); CHECK(is(per_tick(1'000'001, 500'000, Rounding::up), 500'001)); // The two directions bracket the exact amount, one unit apart. const std::uint64_t rate = 123'457, tick = 16'667; const auto down = *per_tick(rate, tick, Rounding::down); const auto up = *per_tick(rate, tick, Rounding::up); const auto exact_millionths = rate * tick; CHECK(down * 1'000'000 < exact_millionths && exact_millionths < up * 1'000'000); CHECK(up - down == 1); } void bounds() { CHECK(fails(per_tick(1, 0, Rounding::down), RateError::invalid_tick)); CHECK(fails(per_tick(1, max_tick_microseconds + 1, Rounding::up), RateError::invalid_tick)); CHECK(fails(per_second_from_per_tick(1, 0), RateError::invalid_tick)); CHECK(!exact_per_tick(0, 0)); // A one-second tick passes the rate through; the largest rate still converts. CHECK(is(per_tick(u64_max, max_tick_microseconds, Rounding::down), u64_max)); CHECK(is(per_tick(u64_max, max_tick_microseconds, Rounding::up), u64_max)); CHECK(is(per_tick(u64_max, tick_50ms, Rounding::down), u64_max / 20)); CHECK(is(per_tick(u64_max, tick_50ms, Rounding::up), u64_max / 20 + 1)); // The migration refuses what it cannot represent rather than rounding it. CHECK(fails(per_second_from_per_tick(u64_max, tick_50ms), RateError::overflow)); CHECK(fails(per_second_from_per_tick(1, 333'333), RateError::inexact)); CHECK(is(per_second_from_per_tick(3, 3), 1'000'000)); } } // namespace int main() { try { migration_is_exact_at_50ms(); directional_rounding(); bounds(); std::cout << "rate contracts passed\n"; return 0; } catch (const std::exception& e) { std::cerr << e.what() << '\n'; return 1; } }