Add atomic conserved energy and elemental inventory ledger

This commit is contained in:
dingo
2026-09-19 19:42:29 -04:00
parent 7e0aa4d057
commit 8fd3591b0e
12 changed files with 517 additions and 7 deletions
+194
View File
@@ -0,0 +1,194 @@
#include "evo/accounting/ledger.hpp"
#include "evo/core/random.hpp"
#include <algorithm>
#include <array>
#include <iostream>
#include <limits>
#include <stdexcept>
#include <type_traits>
namespace {
using namespace evo::accounting;
static_assert(!std::is_convertible_v<Picojoules, Femtograms>);
static_assert(!std::is_convertible_v<Femtograms, Picojoules>);
void check(bool condition) {
if (!condition) {
throw std::runtime_error("ledger contract failed");
}
}
#define CHECK(expression) check(static_cast<bool>(expression))
Inventory amount(std::uint64_t energy, std::uint64_t carbon = 0, std::uint64_t nitrogen = 0,
std::uint64_t phosphorus = 0, std::uint64_t other = 0) {
return {Picojoules{energy},
{Femtograms{carbon}, Femtograms{nitrogen}, Femtograms{phosphorus}, Femtograms{other}}};
}
std::vector<Inventory> copy(const Ledger& ledger) {
return {ledger.balances().begin(), ledger.balances().end()};
}
void conserved(const Ledger& ledger) {
// Recompute independently from actual balances, not the cached total.
Inventory sum{};
for (const auto& balance : ledger.balances()) {
CHECK(balance.energy.value <= std::numeric_limits<std::uint64_t>::max() - sum.energy.value);
sum.energy.value += balance.energy.value;
for (std::size_t index = 0; index < 4; ++index) {
CHECK(balance.matter[index].value <=
std::numeric_limits<std::uint64_t>::max() - sum.matter[index].value);
sum.matter[index].value += balance.matter[index].value;
}
}
CHECK(sum == ledger.conserved_totals());
}
void finite_source_and_heat() {
const std::array opening{amount(100, 10, 20, 30, 40), Inventory{}, Inventory{}};
auto ledger = Ledger::create(opening);
CHECK(ledger);
// A finite source funds reserve plus explicitly accounted heat.
const std::array capture{Transfer{{1}, {2}, amount(100, 10, 20, 30, 40)},
Transfer{{2}, {3}, amount(25)}};
CHECK(ledger->transfer(1, capture));
CHECK(ledger->balances()[0] == Inventory{});
CHECK(ledger->balances()[1] == amount(75, 10, 20, 30, 40));
CHECK(ledger->balances()[2] == amount(25));
const auto before = copy(*ledger);
const auto duplicate = ledger->transfer(1, capture);
CHECK(!duplicate && duplicate.error() == LedgerError::stale_action);
const auto empty_source = ledger->transfer(2, capture);
CHECK(!empty_source && empty_source.error() == LedgerError::insufficient_inventory);
CHECK(copy(*ledger) == before && ledger->last_action() == 1);
conserved(*ledger);
}
void atomic_rejection_and_limits() {
const std::array opening{amount(10, 10, 10, 10, 10), Inventory{}, Inventory{}};
auto ledger = Ledger::create(opening);
CHECK(ledger);
const auto before = copy(*ledger);
const auto reject = [&](std::uint64_t action, std::span<const Transfer> legs,
LedgerError expected) {
const auto result = ledger->transfer(action, legs);
CHECK(!result && result.error() == expected);
CHECK(copy(*ledger) == before && ledger->last_action() == 0);
};
std::array legs{Transfer{{1}, {2}, amount(5, 5, 5, 5, 5)}, Transfer{{1}, {3}, amount(6)}};
reject(0, legs, LedgerError::stale_action);
reject(1, {}, LedgerError::transfer_limit);
reject(1, legs, LedgerError::insufficient_inventory);
for (std::size_t channel = 0; channel < 4; ++channel) {
legs[1].amount = {};
legs[1].amount.matter[channel] = Femtograms{6};
reject(1, legs, LedgerError::insufficient_inventory);
}
legs[1] = Transfer{{0}, {3}, {}};
reject(1, legs, LedgerError::invalid_reservoir);
legs[1] = Transfer{{1}, {4}, {}};
reject(1, legs, LedgerError::invalid_reservoir);
legs[1] = Transfer{{1}, {1}, {}};
reject(1, legs, LedgerError::same_reservoir);
std::vector<Transfer> large(max_transfers + 1, Transfer{{1}, {2}, {}});
reject(1, large, LedgerError::transfer_limit);
large.pop_back();
CHECK(ledger->transfer(1, large)); // Valid zero transfer consumes its action ID.
CHECK(ledger->last_action() == 1 && copy(*ledger) == before);
const std::array drain{Transfer{{1}, {2}, amount(10, 10, 10, 10, 10)}};
CHECK(ledger->transfer(std::numeric_limits<std::uint64_t>::max(), drain));
CHECK(!ledger->transfer(std::numeric_limits<std::uint64_t>::max(), drain));
conserved(*ledger);
CHECK(!Ledger::create({}));
std::vector<Inventory> many(max_reservoirs + 1);
CHECK(!Ledger::create(many));
many.pop_back();
CHECK(Ledger::create(many));
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
std::array excessive{amount(maximum), amount(1)};
auto invalid = Ledger::create(excessive);
CHECK(!invalid && invalid.error() == LedgerError::overflow);
for (std::size_t channel = 0; channel < 4; ++channel) {
excessive = {};
excessive[0].matter[channel] = Femtograms{maximum};
excessive[1].matter[channel] = Femtograms{1};
invalid = Ledger::create(excessive);
CHECK(!invalid && invalid.error() == LedgerError::overflow);
}
const std::array exact{amount(maximum, maximum, maximum, maximum, maximum), Inventory{}};
auto full = Ledger::create(exact);
CHECK(full);
const std::array move_all{Transfer{{1}, {2}, exact[0]}};
CHECK(full->transfer(1, move_all));
CHECK(full->balances()[0] == Inventory{} && full->balances()[1] == exact[0]);
conserved(*full);
}
void ordered_competition() {
const std::array opening{amount(10), Inventory{}, Inventory{}};
auto ledger = Ledger::create(opening);
CHECK(ledger);
const std::array first{Transfer{{1}, {2}, amount(7)}};
const std::array second{Transfer{{1}, {3}, amount(7)}};
CHECK(ledger->transfer(1, first));
CHECK(!ledger->transfer(2, second));
const std::array corrected{Transfer{{1}, {3}, amount(3)}};
CHECK(ledger->transfer(2, corrected));
conserved(*ledger);
auto chain = Ledger::create(opening);
CHECK(chain);
const std::array unfunded{Transfer{{2}, {3}, amount(10)}, Transfer{{1}, {2}, amount(10)}};
CHECK(!chain->transfer(1, unfunded));
auto funded = unfunded;
std::ranges::reverse(funded);
CHECK(chain->transfer(1, funded));
CHECK(chain->balances()[2] == amount(10));
}
void million_transaction_replay() {
// 32 deterministic streams, each with 31,250 transactions: one million total.
for (std::uint64_t seed = 0; seed < 32; ++seed) {
const std::array opening{amount(1000, 1000, 1000, 1000, 1000),
amount(1000, 1000, 1000, 1000, 1000),
amount(1000, 1000, 1000, 1000, 1000)};
auto ledger = Ledger::create(opening);
CHECK(ledger);
auto replay = *ledger;
auto reference = opening;
evo::Random random{seed};
for (std::uint64_t action = 1; action <= 31'250; ++action) {
const auto from = static_cast<std::size_t>(random.next() % 3);
const auto to = (from + 1 + static_cast<std::size_t>(random.next() % 2)) % 3;
const auto available = reference[from];
Inventory moved{};
moved.energy.value = random.next() % (available.energy.value + 1);
for (std::size_t channel = 0; channel < 4; ++channel) {
moved.matter[channel].value = random.next() % (available.matter[channel].value + 1);
reference[from].matter[channel].value -= moved.matter[channel].value;
reference[to].matter[channel].value += moved.matter[channel].value;
}
reference[from].energy.value -= moved.energy.value;
reference[to].energy.value += moved.energy.value;
const std::array legs{Transfer{ReservoirId{static_cast<std::uint32_t>(from + 1)},
ReservoirId{static_cast<std::uint32_t>(to + 1)}, moved}};
CHECK(ledger->transfer(action, legs));
CHECK(replay.transfer(action, legs));
CHECK(std::ranges::equal(ledger->balances(), reference));
CHECK(std::ranges::equal(ledger->balances(), replay.balances()));
CHECK(ledger->last_action() == replay.last_action());
conserved(*ledger);
}
}
}
} // namespace
int main() try {
finite_source_and_heat();
atomic_rejection_and_limits();
ordered_competition();
million_transaction_replay();
std::cout << "PASS ledger contracts and 1000000 seeded transactions\n";
return 0;
} catch (...) {
return 1;
}